Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package minefantasy.mf2.block.tileentity;
- import java.util.Random;
- import minefantasy.mf2.MineFantasyII;
- import minefantasy.mf2.api.MineFantasyAPI;
- import minefantasy.mf2.api.crafting.BaseRecipeMF;
- import minefantasy.mf2.api.crafting.CraftingProjectMF;
- import minefantasy.mf2.item.food.FoodListMF;
- import minefantasy.mf2.item.list.ComponentListMF;
- import minefantasy.mf2.item.list.ToolListMF;
- import net.minecraft.entity.item.EntityItem;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.init.Items;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.item.ItemStack;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.tileentity.TileEntity;
- public class TileEntityCarpenter extends TileEntity implements IInventory
- {
- private int tier;
- private ItemStack[] inventory;
- private Random rand = new Random();
- private CraftingProjectMF project;
- public TileEntityCarpenter()
- {
- this(0);
- }
- public TileEntityCarpenter(int tier)
- {
- inventory = new ItemStack[9];
- this.tier=tier;
- }
- @Override
- public void readFromNBT(NBTTagCompound nbt)
- {
- super.readFromNBT(nbt);
- tier = nbt.getInteger("tier");
- NBTTagList savedItems = nbt.getTagList("Items", 10);
- this.inventory = new ItemStack[this.getSizeInventory()];
- for (int i = 0; i < savedItems.tagCount(); ++i)
- {
- NBTTagCompound savedSlot = savedItems.getCompoundTagAt(i);
- byte slotNum = savedSlot.getByte("Slot");
- if (slotNum >= 0 && slotNum < this.inventory.length)
- {
- this.inventory[slotNum] = ItemStack.loadItemStackFromNBT(savedSlot);
- }
- }
- if(nbt.hasKey("project"))
- {
- project = new CraftingProjectMF();
- project.readFromNBT(nbt.getCompoundTag("project"));
- }
- }
- @Override
- public void writeToNBT(NBTTagCompound nbt)
- {
- super.writeToNBT(nbt);
- nbt.setInteger("tier", tier);
- NBTTagList savedItems = new NBTTagList();
- for (int i = 0; i < this.inventory.length; ++i)
- {
- if (this.inventory[i] != null)
- {
- NBTTagCompound savedSlot = new NBTTagCompound();
- savedSlot.setByte("Slot", (byte)i);
- this.inventory[i].writeToNBT(savedSlot);
- savedItems.appendTag(savedSlot);
- }
- }
- nbt.setTag("Items", savedItems);
- if(project != null)
- {
- NBTTagCompound saveProject = new NBTTagCompound();
- project.writeToNBT(saveProject);
- nbt.setTag("project", saveProject);
- }
- }
- @Override
- public int getSizeInventory()
- {
- return inventory.length;
- }
- @Override
- public ItemStack getStackInSlot(int slot)
- {
- return inventory[slot];
- }
- @Override
- public ItemStack decrStackSize(int slot, int count)
- {
- ItemStack item = inventory[slot];
- item.stackSize -= count;
- if(item.stackSize <= 0)
- {
- item = inventory[slot] = null;
- }
- return item;
- }
- @Override
- public ItemStack getStackInSlotOnClosing(int slot)
- {
- return null;
- }
- @Override
- public void setInventorySlotContents(int slot, ItemStack item)
- {
- inventory[slot] = item;
- }
- @Override
- public String getInventoryName()
- {
- return "gui.carpentermf.name";
- }
- @Override
- public boolean hasCustomInventoryName()
- {
- return false;
- }
- @Override
- public int getInventoryStackLimit()
- {
- return 64;
- }
- @Override
- public boolean isUseableByPlayer(EntityPlayer user)
- {
- return user.getDistance(xCoord+0.5D, yCoord+0.5D, zCoord+0.5D) < 8D;
- }
- @Override
- public void openInventory()
- {
- }
- @Override
- public void closeInventory()
- {
- }
- @Override
- public boolean isItemValidForSlot(int slot, ItemStack item)
- {
- return true;
- }
- public boolean tryCraft(EntityPlayer user)
- {
- if(project != null)
- {
- if(project.getResult() != null && project.hasFinished())
- {
- user.entityDropItem(project.getResult(), 1.0F);
- clearProject(false);
- return true;
- }
- if(project.hitWithTool(user.getHeldItem(), 0))
- {
- return true;
- }
- }
- return false;
- }
- public void clearProject(boolean drop)
- {
- if(project != null && drop)
- {
- project.dropItems(worldObj, xCoord, yCoord, zCoord);
- }
- project = null;
- }
- private void consumeItems(EntityPlayer user, ItemStack[] items)
- {
- }
- private void dropItem(ItemStack itemstack)
- {
- if (itemstack != null)
- {
- float f = this.rand .nextFloat() * 0.8F + 0.1F;
- float f1 = this.rand.nextFloat() * 0.8F + 0.1F;
- float f2 = this.rand.nextFloat() * 0.8F + 0.1F;
- while (itemstack.stackSize > 0)
- {
- int j1 = this.rand.nextInt(21) + 10;
- if (j1 > itemstack.stackSize)
- {
- j1 = itemstack.stackSize;
- }
- itemstack.stackSize -= j1;
- EntityItem entityitem = new EntityItem(worldObj, (double)((float)xCoord + f), (double)((float)yCoord + f1), (double)((float)zCoord + f2), new ItemStack(itemstack.getItem(), j1, itemstack.getItemDamage()));
- if (itemstack.hasTagCompound())
- {
- entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
- }
- float f3 = 0.05F;
- entityitem.motionX = (double)((float)this.rand.nextGaussian() * f3);
- entityitem.motionY = (double)((float)this.rand.nextGaussian() * f3 + 0.2F);
- entityitem.motionZ = (double)((float)this.rand.nextGaussian() * f3);
- worldObj.spawnEntityInWorld(entityitem);
- }
- }
- }
- public boolean hasItems(EntityPlayer user, ItemStack[] items)
- {
- for(ItemStack check: items)
- {
- if(!hasItems(user, check))
- {
- return false;
- }
- }
- return true;
- }
- public boolean hasItems(EntityPlayer user, ItemStack item)
- {
- return hasItems(user, item, item.stackSize);
- }
- public boolean hasItems(EntityPlayer user, ItemStack item, int number)
- {
- int count = 0;
- for(int a = 0; a < user.inventory.getSizeInventory(); a++)
- {
- ItemStack slot = user.inventory.getStackInSlot(a);
- if(slot != null && slot.isItemEqual(item))
- {
- count += slot.stackSize;
- }
- }
- return count >= number;
- }
- public void setProject(BaseRecipeMF recipe)
- {
- MineFantasyAPI.debugMsg("Set Project: " + recipe.getResult().getDisplayName());
- project = new CraftingProjectMF(recipe);
- }
- public boolean hasProject()
- {
- return project != null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement