<commit_before>package com.insane.simplelabels.tile;

import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.util.ForgeDirection;
import powercrystals.minefactoryreloaded.api.IDeepStorageUnit;

import com.insane.simplelabels.MessageLabelUpdate;
import com.insane.simplelabels.PacketHandler;
import com.insane.simplelabels.SimpleLabels;

public class TileLabel extends TileEntity
{

	private ItemStack storedItem, storedItemForRender;
	private IDeepStorageUnit dsu;
	private ForgeDirection dsuDirection = ForgeDirection.UNKNOWN;
	private int placedDirection;

	private long clickTime = -20;

	public void init(int meta)
	{
		dsuDirection = ForgeDirection.getOrientation(meta);
	}

	@Override
	public void updateEntity()
	{
		if (!this.worldObj.isRemote)
		{
			dsu = getDSU();
			if (dsu != null && !ItemStack.areItemStacksEqual(getLabelStack(false), dsu.getStoredItemType()))
			{
				setLabelStack(dsu.getStoredItemType());
				this.markDirty();
				this.sendPacket();
				worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
			}
		}
	}

	public boolean onRightClick(boolean sneaking)
	{
		if ( (dsu=getDSU()) == null)
			return false;

		ItemStack stored = dsu.getStoredItemType();

		if (stored == null)
			return false;

		if (this.worldObj.getTotalWorldTime() - this.clickTime > 2L)
		{

			int extractAmount = sneaking == true ? stored.getMaxStackSize() : 1;

			if (extractAmount > stored.stackSize)
				extractAmount = stored.stackSize;

			EntityItem dropItem = new EntityItem(this.worldObj);
			ItemStack dropStack = stored.copy(); dropStack.stackSize = extractAmount;

			dropItem.setEntityItemStack(dropStack);
			ForgeDirection dir = ForgeDirection.getOrientation(placedDirection);
			dropItem.setPosition(xCoord+0.5+dir.offsetX, yCoord+0.5+dir.offsetY, zCoord+0.5+dir.offsetZ);

			float var15 = 0.05F;
			dropItem.motionX = (float)this.worldObj.rand.nextGaussian() * var15;
			dropItem.motionY = (float)this.worldObj.rand.nextGaussian() * var15 + 0.2F;
			dropItem.motionZ = (float)this.worldObj.rand.nextGaussian() * var15;

			this.worldObj.spawnEntityInWorld(dropItem);

			dsu.setStoredItemCount(stored.stackSize - extractAmount);

			this.clickTime = this.worldObj.getTotalWorldTime();
			
			return true;
		}
		return false;
	}

	public void addFromPlayer(EntityPlayer player)
	{
		ItemStack heldStack = player.inventory.getCurrentItem();
		if (heldStack != null)
		{
			heldStack.stackSize -= this.addStack(heldStack);

			if (heldStack.stackSize == 0)
				player.inventory.setInventorySlotContents(player.inventory.currentItem, null);
		}

		if (this.worldObj.getTotalWorldTime() - this.clickTime < 10L)
		{
			InventoryPlayer playerInv = player.inventory;
			for (int invSlot = 0 ; invSlot < playerInv.getSizeInventory(); ++invSlot)
			{
				ItemStack slotStack = playerInv.getStackInSlot(invSlot);

				int input = this.addStack(slotStack);
				if (input > 0)
				{
					slotStack.stackSize -= input;
					if (slotStack.stackSize == 0)
						playerInv.setInventorySlotContents(invSlot, (ItemStack) null);
				}
			}
		}

		this.clickTime = this.worldObj.getTotalWorldTime();
		SimpleLabels.proxy.updatePlayerInventory(player);

		this.markDirty();
	}

	private int addStack(ItemStack stack)
	{
		if (stack == null)
			return 0;

		if ( (dsu=getDSU()) == null)
			return 0;

		ItemStack stored = dsu.getStoredItemType();
		if (stored == null)
		{
			dsu.setStoredItemType(stack, stack.stackSize);
			return stack.stackSize;
		}

		if (stored.getItem() == stack.getItem() && stored.getItemDamage() == stack.getItemDamage())
		{
			int addAmount = stack.stackSize;
			if (dsu.getMaxStoredCount() < stored.stackSize + stack.stackSize)
				addAmount = dsu.getMaxStoredCount() - stored.stackSize;

			dsu.setStoredItemCount(stored.stackSize + addAmount);

			return addAmount;
		}

		return 0;

	}

	public void setPlacedDirection(int newDirection)
	{
		this.placedDirection = newDirection;
	}

	public int getPlacedDirection()
	{
		return this.placedDirection;
	}

	private IDeepStorageUnit getDSU()
	{
		return (IDeepStorageUnit) this.worldObj.getTileEntity(this.xCoord - dsuDirection.offsetX, this.yCoord - dsuDirection.offsetY, this.zCoord
				- dsuDirection.offsetZ);
	}<commit_msg>Check NBT for adding.<commit_after>package com.insane.simplelabels.tile;

import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.util.ForgeDirection;
import powercrystals.minefactoryreloaded.api.IDeepStorageUnit;

import com.insane.simplelabels.MessageLabelUpdate;
import com.insane.simplelabels.PacketHandler;
import com.insane.simplelabels.SimpleLabels;

public class TileLabel extends TileEntity
{

	private ItemStack storedItem, storedItemForRender;
	private IDeepStorageUnit dsu;
	private ForgeDirection dsuDirection = ForgeDirection.UNKNOWN;
	private int placedDirection;

	private long clickTime = -20;

	public void init(int meta)
	{
		dsuDirection = ForgeDirection.getOrientation(meta);
	}

	@Override
	public void updateEntity()
	{
		if (!this.worldObj.isRemote)
		{
			dsu = getDSU();
			if (dsu != null && !ItemStack.areItemStacksEqual(getLabelStack(false), dsu.getStoredItemType()))
			{
				setLabelStack(dsu.getStoredItemType());
				this.markDirty();
				this.sendPacket();
				worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
			}
		}
	}

	public boolean onRightClick(boolean sneaking)
	{
		if ( (dsu=getDSU()) == null)
			return false;

		ItemStack stored = dsu.getStoredItemType();

		if (stored == null)
			return false;

		if (this.worldObj.getTotalWorldTime() - this.clickTime > 2L)
		{

			int extractAmount = sneaking == true ? stored.getMaxStackSize() : 1;

			if (extractAmount > stored.stackSize)
				extractAmount = stored.stackSize;

			EntityItem dropItem = new EntityItem(this.worldObj);
			ItemStack dropStack = stored.copy(); dropStack.stackSize = extractAmount;

			dropItem.setEntityItemStack(dropStack);
			ForgeDirection dir = ForgeDirection.getOrientation(placedDirection);
			dropItem.setPosition(xCoord+0.5+dir.offsetX, yCoord+0.5+dir.offsetY, zCoord+0.5+dir.offsetZ);

			float var15 = 0.05F;
			dropItem.motionX = (float)this.worldObj.rand.nextGaussian() * var15;
			dropItem.motionY = (float)this.worldObj.rand.nextGaussian() * var15 + 0.2F;
			dropItem.motionZ = (float)this.worldObj.rand.nextGaussian() * var15;

			this.worldObj.spawnEntityInWorld(dropItem);

			dsu.setStoredItemCount(stored.stackSize - extractAmount);

			this.clickTime = this.worldObj.getTotalWorldTime();
			
			return true;
		}
		return false;
	}

	public void addFromPlayer(EntityPlayer player)
	{
		ItemStack heldStack = player.inventory.getCurrentItem();
		if (heldStack != null)
		{
			heldStack.stackSize -= this.addStack(heldStack);

			if (heldStack.stackSize == 0)
				player.inventory.setInventorySlotContents(player.inventory.currentItem, null);
		}

		if (this.worldObj.getTotalWorldTime() - this.clickTime < 10L)
		{
			InventoryPlayer playerInv = player.inventory;
			for (int invSlot = 0 ; invSlot < playerInv.getSizeInventory(); ++invSlot)
			{
				ItemStack slotStack = playerInv.getStackInSlot(invSlot);

				int input = this.addStack(slotStack);
				if (input > 0)
				{
					slotStack.stackSize -= input;
					if (slotStack.stackSize == 0)
						playerInv.setInventorySlotContents(invSlot, (ItemStack) null);
				}
			}
		}

		this.clickTime = this.worldObj.getTotalWorldTime();
		SimpleLabels.proxy.updatePlayerInventory(player);

		this.markDirty();
	}

	private int addStack(ItemStack stack)
	{
		if (stack == null)
			return 0;

		if ( (dsu=getDSU()) == null)
			return 0;

		ItemStack stored = dsu.getStoredItemType();
		if (stored == null)
		{
			dsu.setStoredItemType(stack, stack.stackSize);
			return stack.stackSize;
		}

		if (stacksEqual(stored, stack, true))
		{
			int addAmount = stack.stackSize;
			if (dsu.getMaxStoredCount() < stored.stackSize + stack.stackSize)
				addAmount = dsu.getMaxStoredCount() - stored.stackSize;

			dsu.setStoredItemCount(stored.stackSize + addAmount);

			return addAmount;
		}

		return 0;

	}
	
	public static boolean stacksEqual(ItemStack s1, ItemStack s2, boolean nbtSensitive)
	{
		if (s1 == null | s2 == null) return false;
		if (!s1.isItemEqual(s2)) return false;
		if (!nbtSensitive) return true;

		if (s1.getTagCompound() == s2.getTagCompound()) return true;
		if (s1.getTagCompound() == null || s2.getTagCompound() == null) return false;
		return s1.getTagCompound().equals(s2.getTagCompound());
	}

	public void setPlacedDirection(int newDirection)
	{
		this.placedDirection = newDirection;
	}

	public int getPlacedDirection()
	{
		return this.placedDirection;
	}

	private IDeepStorageUnit getDSU()
	{
		return (IDeepStorageUnit) this.worldObj.getTileEntity(this.xCoord - dsuDirection.offsetX, this.yCoord - dsuDirection.offsetY, this.zCoord
				- dsuDirection.offsetZ);
	}
{'fraction_non_alphanumeric': 0.11432121776949364, 'fraction_numerical': 0.008284146215180697, 'mean_word_length': 4.4875, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '5009293', 'n_tokens_mistral': 3628, 'n_tokens_neox': 3194, 'n_words': 578}