<commit_before>package net.mcft.copy.betterstorage.misc.handlers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import net.mcft.copy.betterstorage.BetterStorage;
import net.mcft.copy.betterstorage.api.BetterStorageBackpack;
import net.mcft.copy.betterstorage.config.GlobalConfig;
import net.mcft.copy.betterstorage.content.BetterStorageItems;
import net.mcft.copy.betterstorage.content.BetterStorageTiles;
import net.mcft.copy.betterstorage.entity.EntityFrienderman;
import net.mcft.copy.betterstorage.inventory.InventoryStacks;
import net.mcft.copy.betterstorage.item.ItemBackpack;
import net.mcft.copy.betterstorage.item.ItemEnderBackpack;
import net.mcft.copy.betterstorage.misc.EquipmentSlot;
import net.mcft.copy.betterstorage.misc.PropertiesBackpack;
import net.mcft.copy.betterstorage.network.packet.PacketSyncSetting;
import net.mcft.copy.betterstorage.tile.TileEnderBackpack;
import net.mcft.copy.betterstorage.utils.DirectionUtils;
import net.mcft.copy.betterstorage.utils.EntityUtils;
import net.mcft.copy.betterstorage.utils.NbtUtils;
import net.mcft.copy.betterstorage.utils.RandomUtils;
import net.mcft.copy.betterstorage.utils.ReflectionUtils;
import net.mcft.copy.betterstorage.utils.StackUtils;
import net.mcft.copy.betterstorage.utils.WorldUtils;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.monster.EntityEnderman;
import net.minecraft.entity.monster.EntityPigZombie;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.WeightedRandomChestContent;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.ChestGenHooks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.event.entity.living.LivingSpawnEvent.SpecialSpawn;
import net.minecraftforge.event.entity.player.EntityInteractEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.Action;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.Event.Result;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerChangedDimensionEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerRespawnEvent;

public class BackpackHandler {
	
	/** Random items to be found in a backpack. There's also
	    chance the backpack will contain some dungeon loot. */
	private static final WeightedRandomChestContent[] randomBackpackItems = new WeightedRandomChestContent[]{
		
		makeWeightedContent(Items.stick, 8, 20, 100),
		makeWeightedContent(Blocks.planks, 2, 10, 100),
		makeWeightedContent(Blocks.log, 1, 8, 40),
		makeWeightedContent(Blocks.cobblestone, 6, 16, 80),
		
		makeWeightedContent(Items.wooden_pickaxe, 50, 35),
		makeWeightedContent(Items.wooden_pickaxe, 20, 10),
		makeWeightedContent(Items.stone_pickaxe, 120, 10),
		makeWeightedContent(Items.stone_pickaxe, 80, 5),
		makeWeightedContent(Items.iron_pickaxe, 220, 2),
		
		makeWeightedContent(Items.wooden_sword, 40, 30),
		makeWeightedContent(Items.stone_sword, 60, 5),
		
		makeWeightedContent(Items.bow, 200, 1, 1, 10),
		makeWeightedContent(Items.bow, 50, 1, 1, 3),
		makeWeightedContent(Items.fishing_rod, 20, 1, 1, 4),
		makeWeightedContent(Items.compass, 1, 1, 6),
		makeWeightedContent(Items.clock, 1, 1, 5),
		
		makeWeightedContent(Blocks.torch, 6, 24, 30),
		makeWeightedContent(Items.arrow, 2, 12, 10),
		makeWeightedContent(Items.rotten_flesh, 0, 3, 6, 15),
		makeWeightedContent(Items.bone, 2, 5, 20),
		makeWeightedContent(Items.string, 3, 10, 15),
		
		makeWeightedContent(Items.apple, 2, 5, 15),
		makeWeightedContent(Items.bread, 2, 4, 10),
		makeWeightedContent(Items.wheat, 3, 6, 10),
		makeWeightedContent(Items.carrot, 1, 2, 8),
		makeWeightedContent(Items.potato, 1, 2, 5),
		makeWeightedContent(Items.fish, 1, 4, 5),
		makeWeightedContent(Items.cooked_fished, 1, 2, 4),
		
		makeWeightedContent(Items.coal, 3, 9, 20),
		makeWeightedContent(Items.coal, 20, 32, 5),
		makeWeightedContent(Blocks.iron_ore, 2, 5, 15),
		makeWeightedContent(Blocks.iron_ore, 10, 20, 2),
		makeWeightedContent(Blocks.gold_ore, 2, 7, 8),
		makeWeightedContent(Items.diamond, 1, 2, 1),
		makeWeightedContent(Items.emerald, 1, 1, 1),
		
	};

	private static WeightedRandomChestContent makeWeightedContent(Item item, int damage, int min, int max, int weight) {
		return new WeightedRandomChestContent(item, damage, min, max, weight);
	}
	private static WeightedRandomChestContent makeWeightedContent(Item item, int min, int max, int weight) {
		return makeWeightedContent(item, 0, min, max, weight);
	}
	private static WeightedRandomChestContent makeWeightedContent(Item item, int damage, int weight) {
		return makeWeightedContent(item, damage, 1, 1, weight);
	}
	private static WeightedRandomChestContent makeWeightedContent(Block block, int min, int max, int weight) {
		return makeWeightedContent(Item.getItemFromBlock(block), min, max, weight);
	}
	
	public BackpackHandler() {
		MinecraftForge.EVENT_BUS.register(this);
		FMLCommonHandler.instance().bus().register(this);
		
		BetterStorageBackpack.spawnWithBackpack(EntityZombie.class, 1.0 / 800);
		BetterStorageBackpack.spawnWithBackpack(EntitySkeleton.class, 1.0 / 1200);
		BetterStorageBackpack.spawnWithBackpack(EntityPigZombie.class, 1.0 / 1000);
		BetterStorageBackpack.spawnWithBackpack(EntityEnderman.class, 1.0 / 80);
	}
	
	@SubscribeEvent
	public void onEntityConstructing(EntityConstructing event) {
		// Initialize backpack data for every living entity.
		// If the entity does have a backpack, it will be loaded automatically.
		if (event.entity instanceof EntityLivingBase)
			ItemBackpack.initBackpackData((EntityLivingBase)event.entity);
	}
	
	@SubscribeEvent
	public void onPlayerInteract(PlayerInteractEvent event) {
		if (event.isCanceled()) return;
		// When player right clicks a block, attempt to place backpack.
		if (event.action == Action.RIGHT_CLICK_BLOCK)
			if (ItemBackpack.onPlaceBackpack(event.entityPlayer, event.x, event.y, event.z, event.face)) {
				event.useBlock = Result.DENY;
				event.useItem = Result.DENY;
			}
	}
	
	@SubscribeEvent
	public void onEntityInteract(EntityInteractEvent event) {
		
		// Right clicking the back of an entity that
		// has a backpack will open a GUI for that it.
		
		if (event.entity.worldObj.isRemote ||
		    !(event.entity instanceof EntityPlayerMP) ||
		    !(event.target instanceof EntityLivingBase) ||
		    ((event.target instanceof EntityPlayer) &&
		     !BetterStorage.globalConfig.getBoolean(GlobalConfig.enableBackpackInteraction))) return;
		
		EntityPlayerMP player = (EntityPlayerMP)event.entity;
		EntityLivingBase target = (EntityLivingBase)event.target;
		if (ItemBackpack.openBackpack(player, target))
			player.swingItem();
		
	}
	
	@SubscribeEvent
	public void onSpecialSpawn(SpecialSpawn event) {
		
		// When a mob spawns naturally, see if it has a chance to spawn with a backpack.
		EntityLivingBase entity = event.entityLiving;
		World world = entity.worldObj;
		double probability = 0.0;
		
		for (BetterStorageBackpack.BackpackSpawnEntry entry : BetterStorageBackpack.spawnWithBackpack) {
			if (!entity.getClass().equals(entry.entityClass)) continue;
			probability = entry.probability;
			break;
		}
		
		if (!RandomUtils.getBoolean(probability) || entity.isChild()) return;
		
		// If entity is a vanilla enderman, replace it with a friendly one.
		if (entity.getClass().equals(EntityEnderman.class)) {
			if ((BetterStorageTiles.enderBackpack != null) &&<commit_msg>Fix #279, backpack interaction with fake players<commit_after>package net.mcft.copy.betterstorage.misc.handlers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import net.mcft.copy.betterstorage.BetterStorage;
import net.mcft.copy.betterstorage.api.BetterStorageBackpack;
import net.mcft.copy.betterstorage.config.GlobalConfig;
import net.mcft.copy.betterstorage.content.BetterStorageItems;
import net.mcft.copy.betterstorage.content.BetterStorageTiles;
import net.mcft.copy.betterstorage.entity.EntityFrienderman;
import net.mcft.copy.betterstorage.inventory.InventoryStacks;
import net.mcft.copy.betterstorage.item.ItemBackpack;
import net.mcft.copy.betterstorage.item.ItemEnderBackpack;
import net.mcft.copy.betterstorage.misc.EquipmentSlot;
import net.mcft.copy.betterstorage.misc.PropertiesBackpack;
import net.mcft.copy.betterstorage.network.packet.PacketSyncSetting;
import net.mcft.copy.betterstorage.tile.TileEnderBackpack;
import net.mcft.copy.betterstorage.utils.DirectionUtils;
import net.mcft.copy.betterstorage.utils.EntityUtils;
import net.mcft.copy.betterstorage.utils.NbtUtils;
import net.mcft.copy.betterstorage.utils.RandomUtils;
import net.mcft.copy.betterstorage.utils.ReflectionUtils;
import net.mcft.copy.betterstorage.utils.StackUtils;
import net.mcft.copy.betterstorage.utils.WorldUtils;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.monster.EntityEnderman;
import net.minecraft.entity.monster.EntityPigZombie;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.WeightedRandomChestContent;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.ChestGenHooks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.event.entity.living.LivingSpawnEvent.SpecialSpawn;
import net.minecraftforge.event.entity.player.EntityInteractEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.Action;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.Event.Result;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerChangedDimensionEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerRespawnEvent;

public class BackpackHandler {
	
	/** Random items to be found in a backpack. There's also
	    chance the backpack will contain some dungeon loot. */
	private static final WeightedRandomChestContent[] randomBackpackItems = new WeightedRandomChestContent[]{
		
		makeWeightedContent(Items.stick, 8, 20, 100),
		makeWeightedContent(Blocks.planks, 2, 10, 100),
		makeWeightedContent(Blocks.log, 1, 8, 40),
		makeWeightedContent(Blocks.cobblestone, 6, 16, 80),
		
		makeWeightedContent(Items.wooden_pickaxe, 50, 35),
		makeWeightedContent(Items.wooden_pickaxe, 20, 10),
		makeWeightedContent(Items.stone_pickaxe, 120, 10),
		makeWeightedContent(Items.stone_pickaxe, 80, 5),
		makeWeightedContent(Items.iron_pickaxe, 220, 2),
		
		makeWeightedContent(Items.wooden_sword, 40, 30),
		makeWeightedContent(Items.stone_sword, 60, 5),
		
		makeWeightedContent(Items.bow, 200, 1, 1, 10),
		makeWeightedContent(Items.bow, 50, 1, 1, 3),
		makeWeightedContent(Items.fishing_rod, 20, 1, 1, 4),
		makeWeightedContent(Items.compass, 1, 1, 6),
		makeWeightedContent(Items.clock, 1, 1, 5),
		
		makeWeightedContent(Blocks.torch, 6, 24, 30),
		makeWeightedContent(Items.arrow, 2, 12, 10),
		makeWeightedContent(Items.rotten_flesh, 0, 3, 6, 15),
		makeWeightedContent(Items.bone, 2, 5, 20),
		makeWeightedContent(Items.string, 3, 10, 15),
		
		makeWeightedContent(Items.apple, 2, 5, 15),
		makeWeightedContent(Items.bread, 2, 4, 10),
		makeWeightedContent(Items.wheat, 3, 6, 10),
		makeWeightedContent(Items.carrot, 1, 2, 8),
		makeWeightedContent(Items.potato, 1, 2, 5),
		makeWeightedContent(Items.fish, 1, 4, 5),
		makeWeightedContent(Items.cooked_fished, 1, 2, 4),
		
		makeWeightedContent(Items.coal, 3, 9, 20),
		makeWeightedContent(Items.coal, 20, 32, 5),
		makeWeightedContent(Blocks.iron_ore, 2, 5, 15),
		makeWeightedContent(Blocks.iron_ore, 10, 20, 2),
		makeWeightedContent(Blocks.gold_ore, 2, 7, 8),
		makeWeightedContent(Items.diamond, 1, 2, 1),
		makeWeightedContent(Items.emerald, 1, 1, 1),
		
	};

	private static WeightedRandomChestContent makeWeightedContent(Item item, int damage, int min, int max, int weight) {
		return new WeightedRandomChestContent(item, damage, min, max, weight);
	}
	private static WeightedRandomChestContent makeWeightedContent(Item item, int min, int max, int weight) {
		return makeWeightedContent(item, 0, min, max, weight);
	}
	private static WeightedRandomChestContent makeWeightedContent(Item item, int damage, int weight) {
		return makeWeightedContent(item, damage, 1, 1, weight);
	}
	private static WeightedRandomChestContent makeWeightedContent(Block block, int min, int max, int weight) {
		return makeWeightedContent(Item.getItemFromBlock(block), min, max, weight);
	}
	
	public BackpackHandler() {
		MinecraftForge.EVENT_BUS.register(this);
		FMLCommonHandler.instance().bus().register(this);
		
		BetterStorageBackpack.spawnWithBackpack(EntityZombie.class, 1.0 / 800);
		BetterStorageBackpack.spawnWithBackpack(EntitySkeleton.class, 1.0 / 1200);
		BetterStorageBackpack.spawnWithBackpack(EntityPigZombie.class, 1.0 / 1000);
		BetterStorageBackpack.spawnWithBackpack(EntityEnderman.class, 1.0 / 80);
	}
	
	@SubscribeEvent
	public void onEntityConstructing(EntityConstructing event) {
		// Initialize backpack data for every living entity.
		// If the entity does have a backpack, it will be loaded automatically.
		if (event.entity instanceof EntityLivingBase)
			ItemBackpack.initBackpackData((EntityLivingBase)event.entity);
	}
	
	@SubscribeEvent
	public void onPlayerInteract(PlayerInteractEvent event) {
		if (event.isCanceled()) return;
		// When player right clicks a block, attempt to place backpack.
		if (event.action == Action.RIGHT_CLICK_BLOCK)
			if (ItemBackpack.onPlaceBackpack(event.entityPlayer, event.x, event.y, event.z, event.face)) {
				event.useBlock = Result.DENY;
				event.useItem = Result.DENY;
			}
	}
	
	@SubscribeEvent
	public void onEntityInteract(EntityInteractEvent event) {
		
		// Right clicking the back of an entity that
		// has a backpack will open a GUI for that it.
		
		if (event.entity.worldObj.isRemote ||
		    !(event.entity instanceof EntityPlayerMP) ||
		    !(event.target instanceof EntityLivingBase) ||
		    (((EntityPlayerMP)event.entity).playerNetServerHandler == null) ||
		    ((event.target instanceof EntityPlayer) &&
		     !BetterStorage.globalConfig.getBoolean(GlobalConfig.enableBackpackInteraction))) return;
		
		EntityPlayerMP player = (EntityPlayerMP)event.entity;
		EntityLivingBase target = (EntityLivingBase)event.target;
		if (ItemBackpack.openBackpack(player, target))
			player.swingItem();
		
	}
	
	@SubscribeEvent
	public void onSpecialSpawn(SpecialSpawn event) {
		
		// When a mob spawns naturally, see if it has a chance to spawn with a backpack.
		EntityLivingBase entity = event.entityLiving;
		World world = entity.worldObj;
		double probability = 0.0;
		
		for (BetterStorageBackpack.BackpackSpawnEntry entry : BetterStorageBackpack.spawnWithBackpack) {
			if (!entity.getClass().equals(entry.entityClass)) continue;
			probability = entry.probability;
			break;
		}
		
		if (!RandomUtils.getBoolean(probability) || entity.isChild()) return;
		
		// If entity is a vanilla enderman, replace it with a friendly one.
		if (entity.getClass().equals(EntityEnderman.class)) {
			if ((BetterStorageTiles.enderBackpack != null) &&
{'fraction_non_alphanumeric': 0.10565167243367936, 'fraction_numerical': 0.020011534025374856, 'mean_word_length': 5.734368932038835, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7062083', 'n_tokens_mistral': 6192, 'n_tokens_neox': 5461, 'n_words': 1092}