Advertisement
Guest User

tileentitycarpenter

a guest
Aug 24th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.68 KB | None | 0 0
  1. package minefantasy.mf2.block.tileentity;
  2.  
  3. import java.util.Random;
  4.  
  5. import minefantasy.mf2.MineFantasyII;
  6. import minefantasy.mf2.api.MineFantasyAPI;
  7. import minefantasy.mf2.api.crafting.BaseRecipeMF;
  8. import minefantasy.mf2.api.crafting.CraftingProjectMF;
  9. import minefantasy.mf2.item.food.FoodListMF;
  10. import minefantasy.mf2.item.list.ComponentListMF;
  11. import minefantasy.mf2.item.list.ToolListMF;
  12. import net.minecraft.entity.item.EntityItem;
  13. import net.minecraft.entity.player.EntityPlayer;
  14. import net.minecraft.init.Items;
  15. import net.minecraft.inventory.IInventory;
  16. import net.minecraft.item.ItemStack;
  17. import net.minecraft.nbt.NBTTagCompound;
  18. import net.minecraft.nbt.NBTTagList;
  19. import net.minecraft.tileentity.TileEntity;
  20.  
  21. public class TileEntityCarpenter extends TileEntity implements IInventory
  22. {
  23.     private int tier;
  24.     private ItemStack[] inventory;
  25.     private Random rand = new Random();
  26.     private CraftingProjectMF project;
  27.    
  28.     public TileEntityCarpenter()
  29.     {
  30.         this(0);
  31.     }
  32.     public TileEntityCarpenter(int tier)
  33.     {
  34.         inventory = new ItemStack[9];
  35.         this.tier=tier;
  36.     }
  37.    
  38.     @Override
  39.     public void readFromNBT(NBTTagCompound nbt)
  40.     {
  41.         super.readFromNBT(nbt);
  42.         tier = nbt.getInteger("tier");
  43.        
  44.         NBTTagList savedItems = nbt.getTagList("Items", 10);
  45.         this.inventory = new ItemStack[this.getSizeInventory()];
  46.  
  47.         for (int i = 0; i < savedItems.tagCount(); ++i)
  48.         {
  49.             NBTTagCompound savedSlot = savedItems.getCompoundTagAt(i);
  50.             byte slotNum = savedSlot.getByte("Slot");
  51.  
  52.             if (slotNum >= 0 && slotNum < this.inventory.length)
  53.             {
  54.                 this.inventory[slotNum] = ItemStack.loadItemStackFromNBT(savedSlot);
  55.             }
  56.         }
  57.         if(nbt.hasKey("project"))
  58.         {
  59.             project = new CraftingProjectMF();
  60.             project.readFromNBT(nbt.getCompoundTag("project"));
  61.         }
  62.     }
  63.    
  64.     @Override
  65.     public void writeToNBT(NBTTagCompound nbt)
  66.     {
  67.         super.writeToNBT(nbt);
  68.         nbt.setInteger("tier", tier);
  69.        
  70.         NBTTagList savedItems = new NBTTagList();
  71.  
  72.         for (int i = 0; i < this.inventory.length; ++i)
  73.         {
  74.             if (this.inventory[i] != null)
  75.             {
  76.                 NBTTagCompound savedSlot = new NBTTagCompound();
  77.                 savedSlot.setByte("Slot", (byte)i);
  78.                 this.inventory[i].writeToNBT(savedSlot);
  79.                 savedItems.appendTag(savedSlot);
  80.             }
  81.         }
  82.  
  83.         nbt.setTag("Items", savedItems);
  84.        
  85.         if(project != null)
  86.         {
  87.             NBTTagCompound saveProject = new NBTTagCompound();
  88.             project.writeToNBT(saveProject);
  89.             nbt.setTag("project", saveProject);
  90.         }
  91.     }
  92.  
  93.     @Override
  94.     public int getSizeInventory()
  95.     {
  96.         return inventory.length;
  97.     }
  98.  
  99.     @Override
  100.     public ItemStack getStackInSlot(int slot)
  101.     {
  102.         return inventory[slot];
  103.     }
  104.  
  105.     @Override
  106.     public ItemStack decrStackSize(int slot, int count)
  107.     {
  108.         ItemStack item = inventory[slot];
  109.        
  110.         item.stackSize -= count;
  111.         if(item.stackSize <= 0)
  112.         {
  113.             item = inventory[slot] = null;
  114.         }
  115.        
  116.         return item;
  117.     }
  118.  
  119.     @Override
  120.     public ItemStack getStackInSlotOnClosing(int slot)
  121.     {
  122.         return null;
  123.     }
  124.  
  125.     @Override
  126.     public void setInventorySlotContents(int slot, ItemStack item)
  127.     {
  128.         inventory[slot] = item;
  129.     }
  130.  
  131.     @Override
  132.     public String getInventoryName()
  133.     {
  134.         return "gui.carpentermf.name";
  135.     }
  136.  
  137.     @Override
  138.     public boolean hasCustomInventoryName()
  139.     {
  140.         return false;
  141.     }
  142.  
  143.     @Override
  144.     public int getInventoryStackLimit()
  145.     {
  146.         return 64;
  147.     }
  148.  
  149.     @Override
  150.     public boolean isUseableByPlayer(EntityPlayer user)
  151.     {
  152.         return user.getDistance(xCoord+0.5D, yCoord+0.5D, zCoord+0.5D) < 8D;
  153.     }
  154.  
  155.     @Override
  156.     public void openInventory()
  157.     {
  158.     }
  159.  
  160.     @Override
  161.     public void closeInventory()
  162.     {
  163.     }
  164.  
  165.     @Override
  166.     public boolean isItemValidForSlot(int slot, ItemStack item)
  167.     {
  168.         return true;
  169.     }
  170.    
  171.     public boolean tryCraft(EntityPlayer user)
  172.     {
  173.         if(project != null)
  174.         {
  175.             if(project.getResult() != null && project.hasFinished())
  176.             {
  177.                 user.entityDropItem(project.getResult(), 1.0F);
  178.                 clearProject(false);
  179.                 return true;
  180.             }
  181.             if(project.hitWithTool(user.getHeldItem(), 0))
  182.             {
  183.                 return true;
  184.             }
  185.         }
  186.        
  187.         return false;
  188.     }
  189.    
  190.     public void clearProject(boolean drop)
  191.     {
  192.         if(project != null && drop)
  193.         {
  194.             project.dropItems(worldObj, xCoord, yCoord, zCoord);
  195.         }
  196.         project = null;
  197.     }
  198.     private void consumeItems(EntityPlayer user, ItemStack[] items)
  199.     {
  200.     }
  201.     private void dropItem(ItemStack itemstack)
  202.     {
  203.         if (itemstack != null)
  204.         {
  205.             float f = this.rand .nextFloat() * 0.8F + 0.1F;
  206.             float f1 = this.rand.nextFloat() * 0.8F + 0.1F;
  207.             float f2 = this.rand.nextFloat() * 0.8F + 0.1F;
  208.  
  209.             while (itemstack.stackSize > 0)
  210.             {
  211.                 int j1 = this.rand.nextInt(21) + 10;
  212.  
  213.                 if (j1 > itemstack.stackSize)
  214.                 {
  215.                     j1 = itemstack.stackSize;
  216.                 }
  217.  
  218.                 itemstack.stackSize -= j1;
  219.                 EntityItem entityitem = new EntityItem(worldObj, (double)((float)xCoord + f), (double)((float)yCoord + f1), (double)((float)zCoord + f2), new ItemStack(itemstack.getItem(), j1, itemstack.getItemDamage()));
  220.  
  221.                 if (itemstack.hasTagCompound())
  222.                 {
  223.                     entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
  224.                 }
  225.  
  226.                 float f3 = 0.05F;
  227.                 entityitem.motionX = (double)((float)this.rand.nextGaussian() * f3);
  228.                 entityitem.motionY = (double)((float)this.rand.nextGaussian() * f3 + 0.2F);
  229.                 entityitem.motionZ = (double)((float)this.rand.nextGaussian() * f3);
  230.                 worldObj.spawnEntityInWorld(entityitem);
  231.             }
  232.         }
  233.     }
  234.    
  235.     public boolean hasItems(EntityPlayer user, ItemStack[] items)
  236.     {
  237.         for(ItemStack check: items)
  238.         {
  239.             if(!hasItems(user, check))
  240.             {
  241.                 return false;
  242.             }
  243.         }
  244.         return true;
  245.     }
  246.     public boolean hasItems(EntityPlayer user, ItemStack item)
  247.     {
  248.         return hasItems(user, item, item.stackSize);
  249.     }
  250.     public boolean hasItems(EntityPlayer user, ItemStack item, int number)
  251.     {
  252.         int count = 0;
  253.         for(int a = 0; a < user.inventory.getSizeInventory(); a++)
  254.         {
  255.             ItemStack slot = user.inventory.getStackInSlot(a);
  256.             if(slot != null && slot.isItemEqual(item))
  257.             {
  258.                 count += slot.stackSize;
  259.             }
  260.         }
  261.         return count >= number;
  262.     }
  263.     public void setProject(BaseRecipeMF recipe)
  264.     {
  265.         MineFantasyAPI.debugMsg("Set Project: " + recipe.getResult().getDisplayName());
  266.         project = new CraftingProjectMF(recipe);
  267.     }
  268.     public boolean hasProject()
  269.     {
  270.         return project != null;
  271.     }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement