Guest User

TileEntityIceCreamMaker.java

a guest
May 17th, 2015
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.52 KB | None | 0 0
  1. package com.chef.mod.tileentity;
  2.  
  3. import net.minecraft.block.state.IBlockState;
  4. import net.minecraft.entity.player.EntityPlayer;
  5. import net.minecraft.entity.player.InventoryPlayer;
  6. import net.minecraft.init.Items;
  7. import net.minecraft.inventory.Container;
  8. import net.minecraft.inventory.IInventory;
  9. import net.minecraft.inventory.ISidedInventory;
  10. import net.minecraft.inventory.SlotFurnaceFuel;
  11. import net.minecraft.item.Item;
  12. import net.minecraft.item.ItemStack;
  13. import net.minecraft.nbt.NBTTagCompound;
  14. import net.minecraft.nbt.NBTTagList;
  15. import net.minecraft.server.gui.IUpdatePlayerListBox;
  16. import net.minecraft.tileentity.TileEntityLockable;
  17. import net.minecraft.util.EnumFacing;
  18. import net.minecraft.util.ResourceLocation;
  19. import net.minecraftforge.fml.relauncher.Side;
  20. import net.minecraftforge.fml.relauncher.SideOnly;
  21.  
  22. import com.chef.mod.Debugger;
  23. import com.chef.mod.Reference;
  24. import com.chef.mod.blocks.IceCreamMaker;
  25. import com.chef.mod.blocks.IceCreamMaker.EnumIce;
  26. import com.chef.mod.container.ContainerIceCreamMaker;
  27. import com.chef.mod.crafting.IceCreamMakerRecipes;
  28. import com.chef.mod.init.MyItems;
  29.  
  30. public class TileEntityIceCreamMaker extends TileEntityLockable implements IUpdatePlayerListBox, ISidedInventory {
  31.     private static final int[] slotsTop = new int[] { 0, 1 };
  32.     private static final int[] slotsBottom = new int[] { 3 };
  33.     private static final int[] slotsSides = new int[] { 2 };
  34.  
  35.     private ItemStack slots[];
  36.  
  37.     public TileEntityIceCreamMaker() {
  38.         slots = new ItemStack[4];
  39.     }
  40.  
  41.     public int ice;
  42.  
  43.     public int cookTime;
  44.  
  45.     public static final int maxIce = 10000;
  46.  
  47.     public static final int iceCreamMakerSpeed = 100;
  48.  
  49.     private String iceMakerCustomName;
  50.  
  51.     public int getSizeInventory() {
  52.         return this.slots.length;
  53.     }
  54.  
  55.     public ItemStack getStackInSlot(int index) {
  56.         return this.slots[index];
  57.     }
  58.  
  59.     public ItemStack decrStackSize(int index, int count) {
  60.         if (this.slots[index] != null) {
  61.             ItemStack itemstack;
  62.  
  63.             if (this.slots[index].stackSize <= count) {
  64.                 itemstack = this.slots[index];
  65.                 this.slots[index] = null;
  66.                 return itemstack;
  67.             } else {
  68.                 itemstack = this.slots[index].splitStack(count);
  69.  
  70.                 if (this.slots[index].stackSize == 0) {
  71.                     this.slots[index] = null;
  72.                 }
  73.  
  74.                 return itemstack;
  75.             }
  76.         } else {
  77.             return null;
  78.         }
  79.     }
  80.  
  81.     public ItemStack getStackInSlotOnClosing(int index) {
  82.         if (this.slots[index] != null) {
  83.             ItemStack itemstack = this.slots[index];
  84.             this.slots[index] = null;
  85.             return itemstack;
  86.         } else {
  87.             return null;
  88.         }
  89.     }
  90.  
  91.     @Override
  92.     public void setInventorySlotContents(int i, ItemStack itemstack) {
  93.         slots[i] = itemstack;
  94.         if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
  95.             itemstack.stackSize = getInventoryStackLimit();
  96.         }
  97.     }
  98.  
  99.     public String getName() {
  100.         return this.hasCustomName() ? this.iceMakerCustomName : "container.iceCreamMaker";
  101.     }
  102.  
  103.     public boolean hasCustomName() {
  104.         return this.iceMakerCustomName != null && this.iceMakerCustomName.length() > 0;
  105.     }
  106.  
  107.     public void setCustomInventoryName(String string) {
  108.         this.iceMakerCustomName = string;
  109.     }
  110.  
  111.     public void readFromNBT(NBTTagCompound compound) {
  112.         super.readFromNBT(compound);
  113.         NBTTagList nbttaglist = compound.getTagList("Items", 10);
  114.         this.slots = new ItemStack[this.getSizeInventory()];
  115.  
  116.         for (int i = 0; i < nbttaglist.tagCount(); ++i) {
  117.             NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
  118.             byte b0 = nbttagcompound1.getByte("Slot");
  119.  
  120.             if (b0 >= 0 && b0 < this.slots.length) {
  121.                 this.slots[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  122.             }
  123.         }
  124.  
  125.         ice = compound.getShort("PowerTime");
  126.         cookTime = compound.getShort("CookTime");
  127.  
  128.         if (compound.hasKey("CustomName", 8)) {
  129.             this.iceMakerCustomName = compound.getString("CustomName");
  130.         }
  131.     }
  132.  
  133.     public void writeToNBT(NBTTagCompound compound) {
  134.         super.writeToNBT(compound);
  135.  
  136.         compound.setShort("PowerTime", (short) ice);
  137.         compound.setShort("CookTime", (short) cookTime);
  138.  
  139.         NBTTagList nbttaglist = new NBTTagList();
  140.  
  141.         for (int i = 0; i < this.slots.length; ++i) {
  142.             if (this.slots[i] != null) {
  143.                 NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  144.                 nbttagcompound1.setByte("Slot", (byte) i);
  145.                 this.slots[i].writeToNBT(nbttagcompound1);
  146.                 nbttaglist.appendTag(nbttagcompound1);
  147.             }
  148.         }
  149.  
  150.         compound.setTag("Items", nbttaglist);
  151.  
  152.         if (this.hasCustomName()) {
  153.             compound.setString("CustomName", this.iceMakerCustomName);
  154.         }
  155.     }
  156.  
  157.     public int getInventoryStackLimit() {
  158.         return 64;
  159.     }
  160.  
  161.     public boolean hasIce() {
  162.         return ice > 0;
  163.     }
  164.  
  165.     public boolean isIcing() {
  166.         return this.cookTime > 0;
  167.     }
  168.  
  169.     @SideOnly(Side.CLIENT)
  170.     public static boolean isActive(IInventory iinventory) {
  171.         return iinventory.getField(0) > 0;
  172.     }
  173.  
  174.     public void update() {
  175.  
  176.         boolean flag = this.hasIce();
  177.         boolean flag1 = false;
  178.  
  179.         if (hasIce() && this.isIcing()) {
  180.  
  181.             this.ice--;
  182.         }
  183.  
  184.         if (!worldObj.isRemote) {
  185.             if (this.hasItemPower(this.slots[2]) && this.ice <= (this.maxIce - this.getItemPower(this.slots[2]))) {
  186.                 this.ice += getItemPower(this.slots[2]);
  187.  
  188.                 if (this.slots[2] != null) {
  189.                     flag1 = true;
  190.  
  191.                     this.slots[2].stackSize--;
  192.  
  193.                     if (this.slots[2].stackSize == 0) {
  194.                         this.slots[2] = this.slots[2].getItem().getContainerItem(this.slots[2]);
  195.                     }
  196.                 }
  197.             }
  198.  
  199.             if (this.isIcing()) {
  200.  
  201.                 flag1 = true;
  202.                 IceCreamMaker.setState(this.isIcing(), this.worldObj, this.pos);
  203.  
  204.             } else {
  205.                
  206.                 IceCreamMaker.setState(this.isIcing(), this.worldObj, this.pos);
  207.                
  208.             }
  209.            
  210.             if (hasIce() && canIce()) {
  211.                 cookTime++;
  212.  
  213.                 if (this.cookTime == this.iceCreamMakerSpeed) {
  214.                     this.cookTime = 0;
  215.                     this.iceItem();
  216.                     flag1 = true;
  217.                 }
  218.             } else {
  219.                 cookTime = 0;
  220.             }
  221.         }
  222.  
  223.         if (flag1) {
  224.            
  225.             this.markDirty();
  226.         }
  227.  
  228.         IceCreamMaker.setIceAmount(this.ice);
  229.         Debugger.log("Ice amount stored in IceCreamMaker: " + this.ice);
  230.     }
  231.  
  232.     private boolean canIce() {
  233.  
  234.         if (slots[0] == null || slots[1] == null) {
  235.             return false;
  236.  
  237.         } else {
  238.  
  239.             ItemStack itemstack = IceCreamMakerRecipes.getIceCreamResult(slots[0].getItem(), slots[1].getItem());
  240.  
  241.             if (itemstack == null)
  242.                 return false;
  243.             if (this.slots[3] == null)
  244.                 return true;
  245.             if (!this.slots[3].isItemEqual(itemstack))
  246.                 return false;
  247.  
  248.             int result = this.slots[3].stackSize + itemstack.stackSize;
  249.  
  250.             return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());
  251.         }
  252.     }
  253.  
  254.     public void iceItem() {
  255.         if (this.canIce()) {
  256.             ItemStack itemstack = IceCreamMakerRecipes.getIceCreamResult(this.slots[0].getItem(), slots[1].getItem());
  257.  
  258.             if (this.slots[3] == null) {
  259.                 this.slots[3] = itemstack.copy();
  260.             } else if (this.slots[3].getItem() == itemstack.getItem()) {
  261.                 this.slots[3].stackSize += itemstack.stackSize; // Forge BugFix:
  262.                                                                 // Results may
  263.                                                                 // have multiple
  264.                                                                 // items
  265.             }
  266.  
  267.             for (int i = 0; i < 2; i++) {
  268.                 if (slots[i].stackSize <= 0) {
  269.                     slots[i] = new ItemStack(slots[i].getItem().setFull3D());
  270.                 } else {
  271.                     slots[i].stackSize--;
  272.                 }
  273.  
  274.                 if (slots[i].stackSize <= 0) {
  275.                     slots[i] = null;
  276.                 }
  277.             }
  278.         }
  279.     }
  280.  
  281.     public static int getItemPower(ItemStack itemstack) {
  282.         if (itemstack == null) {
  283.             return 0;
  284.         } else {
  285.             Item item = itemstack.getItem();
  286.  
  287.             if (item == MyItems.ice_shard)
  288.                 return 50;
  289.  
  290.             return 0;
  291.         }
  292.     }
  293.  
  294.     public static boolean hasItemPower(ItemStack itemstack) {
  295.         return getItemPower(itemstack) > 0;
  296.     }
  297.  
  298.     public boolean isUseableByPlayer(EntityPlayer player) {
  299.         return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double) this.pos.getX() + 0.5D, (double) this.pos.getY() + 0.5D, (double) this.pos.getZ() + 0.5D) <= 64.0D;
  300.     }
  301.  
  302.     public void openInventory(EntityPlayer player) {
  303.     }
  304.  
  305.     public void closeInventory(EntityPlayer player) {
  306.     }
  307.  
  308.     public boolean isItemValidForSlot(int index, ItemStack stack) {
  309.         return index == 2 ? false : (index != 1 ? true : hasItemPower(stack) || SlotFurnaceFuel.isBucket(stack));
  310.     }
  311.  
  312.     public int[] getSlotsForFace(EnumFacing side) {
  313.         return side == EnumFacing.DOWN ? slotsBottom : (side == EnumFacing.UP ? slotsTop : slotsSides);
  314.     }
  315.  
  316.     public boolean canInsertItem(int index, ItemStack itemStackIn, EnumFacing direction) {
  317.         return this.isItemValidForSlot(index, itemStackIn);
  318.     }
  319.  
  320.     public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction) {
  321.         if (direction == EnumFacing.DOWN && index == 1) {
  322.             Item item = stack.getItem();
  323.  
  324.             if (item != Items.water_bucket && item != Items.bucket) {
  325.                 return false;
  326.             }
  327.         }
  328.  
  329.         return true;
  330.     }
  331.  
  332.     public int getIceMakerProgressScaled(int i) {
  333.         return (cookTime * i) / iceCreamMakerSpeed;
  334.     }
  335.  
  336.     public int getIceRemainingScaled(int i) {
  337.         return (ice * i) / maxIce;
  338.     }
  339.  
  340.     public String getGuiID() {
  341.         return "chef:iceCreamMaker";
  342.     }
  343.  
  344.     public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) {
  345.         return new ContainerIceCreamMaker(playerInventory, this);
  346.     }
  347.  
  348.     public int getField(int id) {
  349.         switch (id) {
  350.         case 0:
  351.             // Burn time
  352.             return this.ice;
  353.         case 1:
  354.             // Cook time
  355.             return this.cookTime;
  356.         default:
  357.             return 0;
  358.         }
  359.     }
  360.  
  361.     public void setField(int id, int value) {
  362.         switch (id) {
  363.         case 0:
  364.             this.ice = value;
  365.             break;
  366.         case 1:
  367.             this.cookTime = value;
  368.             break;
  369.         }
  370.     }
  371.  
  372.     public int getFieldCount() {
  373.         return 3;
  374.     }
  375.  
  376.     public void clear() {
  377.         for (int i = 0; i < this.slots.length; ++i) {
  378.             this.slots[i] = null;
  379.         }
  380.     }
  381. }
Advertisement
Add Comment
Please, Sign In to add comment