Guest User

TileEntityIceCreamMaker

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