Guest User

TileEntityBoiler

a guest
May 13th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.51 KB | None | 0 0
  1. package com.chef.mod.tileentity;
  2.  
  3. import net.minecraft.block.Block;
  4. import net.minecraft.block.material.Material;
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.entity.player.InventoryPlayer;
  7. import net.minecraft.init.Blocks;
  8. import net.minecraft.init.Items;
  9. import net.minecraft.inventory.Container;
  10. import net.minecraft.inventory.IInventory;
  11. import net.minecraft.inventory.ISidedInventory;
  12. import net.minecraft.inventory.SlotFurnaceFuel;
  13. import net.minecraft.item.Item;
  14. import net.minecraft.item.ItemBlock;
  15. import net.minecraft.item.ItemHoe;
  16. import net.minecraft.item.ItemStack;
  17. import net.minecraft.item.ItemSword;
  18. import net.minecraft.item.ItemTool;
  19. import net.minecraft.nbt.NBTTagCompound;
  20. import net.minecraft.nbt.NBTTagList;
  21. import net.minecraft.server.gui.IUpdatePlayerListBox;
  22. import net.minecraft.tileentity.TileEntityLockable;
  23. import net.minecraft.util.EnumFacing;
  24. import net.minecraftforge.fml.relauncher.Side;
  25. import net.minecraftforge.fml.relauncher.SideOnly;
  26.  
  27. import com.chef.mod.Debugger;
  28. import com.chef.mod.blocks.Boiler;
  29. import com.chef.mod.container.ContainerBoiler;
  30. import com.chef.mod.crafting.BoilerRecipes;
  31.  
  32. public class TileEntityBoiler extends TileEntityLockable implements IUpdatePlayerListBox, ISidedInventory {
  33.     private static final int[] slotsTop = new int[] { 0, 1 };
  34.     private static final int[] slotsBottom = new int[] { 3 };
  35.     private static final int[] slotsSides = new int[] { 2 };
  36.  
  37.     private ItemStack slots[];
  38.  
  39.     public TileEntityBoiler() {
  40.         slots = new ItemStack[4];
  41.     }
  42.  
  43.     private int burnTime;
  44.  
  45.     private int currentItemBurnTime;
  46.  
  47.     public static int heath = 0;
  48.  
  49.     public static int water;
  50.  
  51.     private int cookTime;
  52.  
  53.     public int cookingFurnaceSpeed = 1340;
  54.  
  55.     public static final int maxWater = 40000;
  56.  
  57.     // Speed of the furnace. The higher the number, the slower it goes. Normal
  58.     // [200].
  59.  
  60.     private String furnaceCustomName;
  61.  
  62.     public int getSizeInventory() {
  63.  
  64.         return this.slots.length;
  65.     }
  66.  
  67.     public ItemStack getStackInSlot(int index) {
  68.         return this.slots[index];
  69.     }
  70.  
  71.     public ItemStack decrStackSize(int index, int count) {
  72.         if (this.slots[index] != null) {
  73.             ItemStack itemstack;
  74.  
  75.             if (this.slots[index].stackSize <= count) {
  76.                 itemstack = this.slots[index];
  77.                 this.slots[index] = null;
  78.                 return itemstack;
  79.             } else {
  80.                 itemstack = this.slots[index].splitStack(count);
  81.  
  82.                 if (this.slots[index].stackSize == 0) {
  83.                     this.slots[index] = null;
  84.                 }
  85.  
  86.                 return itemstack;
  87.             }
  88.         } else {
  89.             return null;
  90.         }
  91.     }
  92.  
  93.     public ItemStack getStackInSlotOnClosing(int index) {
  94.         if (this.slots[index] != null) {
  95.             ItemStack itemstack = this.slots[index];
  96.             this.slots[index] = null;
  97.             return itemstack;
  98.         } else {
  99.             return null;
  100.         }
  101.     }
  102.  
  103.     @Override
  104.     public void setInventorySlotContents(int i, ItemStack itemstack) {
  105.         slots[i] = itemstack;
  106.         if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
  107.             itemstack.stackSize = getInventoryStackLimit();
  108.         }
  109.     }
  110.  
  111.     public String getName() {
  112.         return this.hasCustomName() ? this.furnaceCustomName : "container.boiler";
  113.     }
  114.  
  115.     public boolean hasCustomName() {
  116.         return this.furnaceCustomName != null && this.furnaceCustomName.length() > 0;
  117.     }
  118.  
  119.     public void setCustomInventoryName(String string) {
  120.         this.furnaceCustomName = string;
  121.     }
  122.  
  123.     public void readFromNBT(NBTTagCompound compound) {
  124.  
  125.         super.readFromNBT(compound);
  126.         NBTTagList nbttaglist = compound.getTagList("Items", 10);
  127.         this.slots = new ItemStack[this.getSizeInventory()];
  128.  
  129.         for (int i = 0; i < nbttaglist.tagCount(); ++i) {
  130.             NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
  131.             byte b0 = nbttagcompound1.getByte("Slot");
  132.  
  133.             if (b0 >= 0 && b0 < this.slots.length) {
  134.                 this.slots[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  135.             }
  136.         }
  137.  
  138.         this.burnTime = compound.getShort("BurnTime");
  139.         this.cookTime = compound.getShort("CookTime");
  140.         this.heath = compound.getShort("Heath");
  141.         this.water = compound.getShort("Water");
  142.  
  143.         this.currentItemBurnTime = getItemBurnTime(this.slots[1]);
  144.  
  145.         if (compound.hasKey("CustomName", 8)) {
  146.             this.furnaceCustomName = compound.getString("CustomName");
  147.         }
  148.     }
  149.  
  150.     public void writeToNBT(NBTTagCompound compound) {
  151.  
  152.         super.writeToNBT(compound);
  153.         compound.setShort("BurnTime", (short) this.burnTime);
  154.         compound.setShort("CookTime", (short) this.cookTime);
  155.         compound.setShort("Heath", (short) this.heath);
  156.         compound.setShort("Water", (short) this.water);
  157.  
  158.         NBTTagList nbttaglist = new NBTTagList();
  159.  
  160.         for (int i = 0; i < this.slots.length; ++i) {
  161.             if (this.slots[i] != null) {
  162.                 NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  163.                 nbttagcompound1.setByte("Slot", (byte) i);
  164.                 this.slots[i].writeToNBT(nbttagcompound1);
  165.                 nbttaglist.appendTag(nbttagcompound1);
  166.             }
  167.         }
  168.  
  169.         compound.setTag("Items", nbttaglist);
  170.  
  171.         if (this.hasCustomName()) {
  172.             compound.setString("CustomName", this.furnaceCustomName);
  173.         }
  174.     }
  175.  
  176.     public int getInventoryStackLimit() {
  177.         return 64;
  178.     }
  179.  
  180.     public boolean isBurning() {
  181.  
  182.         return this.burnTime > 0;
  183.  
  184.     }
  185.  
  186.     public boolean hasWater() {
  187.  
  188.         return this.water > 0;
  189.  
  190.     }
  191.  
  192.     @SideOnly(Side.CLIENT)
  193.     public static boolean isBurning(IInventory iinventory) {
  194.         return iinventory.getField(0) > 0;
  195.     }
  196.  
  197.     @SideOnly(Side.CLIENT)
  198.     public static boolean hasWater(IInventory iinventory) {
  199.         return iinventory.getField(2) > 2;
  200.     }
  201.  
  202.     public void update() {
  203.         boolean flag = this.burnTime > 0;
  204.         boolean flag2 = this.water > 0;
  205.         boolean flag3 = false;
  206.  
  207.         if (this.isBurning()) {
  208.  
  209.             this.burnTime--;
  210.            
  211.         }
  212.  
  213.         //20 Ticks = 1 second, 600 ticks = 30 seconds
  214.         if (this.hasWater() && this.isBurning() && this.heath >= 600) {
  215.  
  216.             this.water--;
  217.  
  218.         }
  219.  
  220.         if (!this.worldObj.isRemote) {
  221.  
  222.             if (!this.isBurning() && this.hasWater() && this.canCook()) {
  223.                 this.currentItemBurnTime = this.burnTime = getItemBurnTime(this.slots[1]);
  224.  
  225.                 if (this.isBurning()) {
  226.                     flag3 = true;
  227.  
  228.                     if (this.slots[1] != null) {
  229.                         this.slots[1].stackSize--;
  230.  
  231.                         if (this.slots[1].stackSize == 0) {
  232.                             this.slots[1] = slots[1].getItem().getContainerItem(slots[1]);
  233.                         }
  234.                     }
  235.                 }
  236.             }
  237.  
  238.             if (this.isItemWater(this.slots[2]) && this.water <= (this.maxWater - this.getItemWater(this.slots[2]))) {
  239.                 this.water += getItemWater(this.slots[2]);
  240.  
  241.                 if (this.slots[2] != null) {
  242.                     flag3 = true;
  243.  
  244.                     this.slots[2].stackSize--;
  245.  
  246.                     if (this.slots[2].stackSize == 0) {
  247.                         this.slots[2] = this.slots[2].getItem().getContainerItem(this.slots[2]);
  248.                     }
  249.                 }
  250.             }
  251.  
  252.             if (this.isBurning() && this.water > 10000 && this.canCook()) {
  253.  
  254.                 this.heath++;
  255.  
  256.                 if (this.heath >= 300) {
  257.  
  258.                     this.cookTime++;
  259.  
  260.                 }
  261.                
  262.                 if (this.heath >= 1200) {
  263.                    
  264.                     this.heath = 1200;
  265.                    
  266.                 }
  267.  
  268.                 if (this.cookTime >= (this.cookingFurnaceSpeed - this.heath)) {
  269.                     this.cookTime = 0;
  270.                     this.smeltItem();
  271.                     flag3 = true;
  272.                 }
  273.                
  274.             } else {
  275.  
  276.                 this.cookTime = 0;
  277.             }
  278.  
  279.             if (flag != this.hasWater()) {
  280.                 flag3 = true;
  281.                 Boiler.setState(this.hasWater(), this.worldObj, this.pos);
  282.             }
  283.         }
  284.  
  285.         if (flag3) {
  286.             this.markDirty();
  287.         }
  288.        
  289.         //Debugger.log("Heath of furnace: " + heath);
  290.     }
  291.  
  292.     private boolean canCook() {
  293.  
  294.         if (slots[0] == null) {
  295.             return false;
  296.         } else {
  297.  
  298.             ItemStack itemstack = BoilerRecipes.getBoilingResult(slots[0].getItem());
  299.  
  300.             if (itemstack == null)
  301.                 return false;
  302.             if (this.slots[3] == null)
  303.                 return true;
  304.             if (!this.slots[3].isItemEqual(itemstack))
  305.                 return false;
  306.  
  307.             int result = this.slots[3].stackSize + itemstack.stackSize;
  308.  
  309.             return result <= getInventoryStackLimit() && result <= this.slots[3].getMaxStackSize();
  310.         }
  311.     }
  312.  
  313.     public void smeltItem() {
  314.  
  315.         if (this.canCook()) {
  316.             ItemStack itemstack = BoilerRecipes.getBoilingResult(this.slots[0].getItem());
  317.  
  318.             if (this.slots[3] == null) {
  319.                 this.slots[3] = itemstack.copy();
  320.             } else if (this.slots[3].getItem() == itemstack.getItem()) {
  321.                 this.slots[3].stackSize += itemstack.stackSize; // Forge BugFix:
  322.                                                                 // Results may
  323.                                                                 // have multiple
  324.                                                                 // items
  325.             }
  326.  
  327.             if (slots[0].stackSize <= 0) {
  328.                 slots[0] = new ItemStack(slots[0].getItem().setFull3D());
  329.             } else {
  330.                 slots[0].stackSize--;
  331.             }
  332.  
  333.             if (slots[0].stackSize <= 0) {
  334.                 slots[0] = null;
  335.             }
  336.         }
  337.     }
  338.  
  339.     public static int getItemBurnTime(ItemStack itemstack) {
  340.         if (itemstack == null) {
  341.             return 0;
  342.         } else {
  343.             Item item = itemstack.getItem();
  344.  
  345.             if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air) {
  346.                 Block block = Block.getBlockFromItem(item);
  347.  
  348.                 if (block == Blocks.wooden_slab) {
  349.                     return 150;
  350.                 }
  351.  
  352.                 if (block.getMaterial() == Material.wood) {
  353.                     return 300;
  354.                 }
  355.  
  356.                 if (block == Blocks.coal_block) {
  357.                     return 16000;
  358.                 }
  359.             }
  360.  
  361.             if (item instanceof ItemTool && ((ItemTool) item).getToolMaterialName().equals("WOOD"))
  362.                 return 200;
  363.             if (item instanceof ItemSword && ((ItemSword) item).getToolMaterialName().equals("WOOD"))
  364.                 return 200;
  365.             if (item instanceof ItemHoe && ((ItemHoe) item).getMaterialName().equals("WOOD"))
  366.                 return 200;
  367.             if (item == Items.stick)
  368.                 return 100;
  369.             if (item == Items.coal)
  370.                 return 1600;
  371.             if (item == Items.lava_bucket)
  372.                 return 20000;
  373.             if (item == Item.getItemFromBlock(Blocks.sapling))
  374.                 return 100;
  375.             if (item == Items.blaze_rod)
  376.                 return 2400;
  377.             return net.minecraftforge.fml.common.registry.GameRegistry.getFuelValue(itemstack);
  378.         }
  379.     }
  380.  
  381.     public static boolean isItemFuel(ItemStack itemstack) {
  382.         return getItemBurnTime(itemstack) > 0;
  383.     }
  384.  
  385.     public static int getItemWater(ItemStack itemstack) {
  386.         if (itemstack == null) {
  387.  
  388.             return 0;
  389.  
  390.         } else {
  391.  
  392.             Item item = itemstack.getItem();
  393.  
  394.             if (item == Items.water_bucket)
  395.                 return 10000;
  396.  
  397.             return net.minecraftforge.fml.common.registry.GameRegistry.getFuelValue(itemstack);
  398.         }
  399.     }
  400.  
  401.     public static boolean isItemWater(ItemStack itemstack) {
  402.  
  403.         return getItemWater(itemstack) > 0;
  404.        
  405.     }
  406.  
  407.     public boolean isUseableByPlayer(EntityPlayer player) {
  408.         return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double) this.pos.getX() + 0.5D,
  409.                 (double) this.pos.getY() + 0.5D, (double) this.pos.getZ() + 0.5D) <= 64.0D;
  410.     }
  411.  
  412.     public void openInventory(EntityPlayer player) {
  413.     }
  414.  
  415.     public void closeInventory(EntityPlayer player) {
  416.     }
  417.  
  418.     public boolean isItemValidForSlot(int index, ItemStack stack) {
  419.         return index == 2 ? false : (index != 1 ? true : isItemFuel(stack) || SlotFurnaceFuel.isBucket(stack));
  420.     }
  421.  
  422.     public int[] getSlotsForFace(EnumFacing side) {
  423.         return side == EnumFacing.DOWN ? slotsBottom : (side == EnumFacing.UP ? slotsTop : slotsSides);
  424.     }
  425.  
  426.     public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) {
  427.         return this.isItemValidForSlot(index, itemStackIn);
  428.     }
  429.  
  430.     public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction) {
  431.         if (direction == EnumFacing.DOWN && index == 1) {
  432.             Item item = stack.getItem();
  433.  
  434.             if (item != Items.water_bucket && item != Items.bucket) {
  435.                 return false;
  436.             }
  437.         }
  438.  
  439.         return true;
  440.     }
  441.  
  442.     public int getBurnTimeRemainingScaled(int i) {
  443.         if (this.currentItemBurnTime == 0) {
  444.             this.currentItemBurnTime = getItemBurnTime(this.slots[1]);
  445.         }
  446.         return this.burnTime * i / this.currentItemBurnTime;
  447.     }
  448.  
  449.     public int getWaterRemainingScaled(int i) {
  450.  
  451.         return (this.water * i) / this.maxWater;
  452.  
  453.     }
  454.  
  455.     public int getCookProgressScaled(int i) {
  456.         return this.cookTime * i / (this.cookingFurnaceSpeed - this.heath);
  457.     }
  458.  
  459.     public String getGuiID() {
  460.         return "chef:boiler";
  461.     }
  462.  
  463.     public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
  464.         return new ContainerBoiler(playerInventory, this);
  465.     }
  466.  
  467.     public int getField(int id) {
  468.         switch (id) {
  469.         case 0:
  470.             return this.burnTime;
  471.         case 1:
  472.             return this.currentItemBurnTime;
  473.         case 2:
  474.             return this.water;
  475.         case 3:
  476.             return this.heath;
  477.         case 4:
  478.             return this.cookTime;
  479.         default:
  480.             return 0;
  481.         }
  482.     }
  483.  
  484.     public void setField(int id, int value) {
  485.         switch (id) {
  486.         case 0:
  487.             this.burnTime = value;
  488.             break;
  489.         case 1:
  490.             this.currentItemBurnTime = value;
  491.             break;
  492.         case 2:
  493.             this.water = value;
  494.             break;
  495.         case 3:
  496.             this.heath = value;
  497.             break;
  498.         case 4:
  499.             this.cookTime = value;
  500.             break;
  501.         }
  502.     }
  503.  
  504.     public int getFieldCount() {
  505.         return 5;
  506.     }
  507.  
  508.     public void clear() {
  509.         for (int i = 0; i < this.slots.length; ++i) {
  510.             this.slots[i] = null;
  511.         }
  512.     }
  513. }
Advertisement
Add Comment
Please, Sign In to add comment