Advertisement
redfoxhint

TileEntityAlloyOven Class

Oct 23rd, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.21 KB | None | 0 0
  1. package com.arucraft.tileentity;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.init.Items;
  5. import net.minecraft.inventory.ISidedInventory;
  6. import net.minecraft.item.Item;
  7. import net.minecraft.item.ItemStack;
  8. import net.minecraft.nbt.NBTTagCompound;
  9. import net.minecraft.nbt.NBTTagList;
  10. import net.minecraft.tileentity.TileEntity;
  11.  
  12. import com.arucraft.blocks.AlloyOven;
  13. import com.arucraft.crafting.AlloyOvenRecipes;
  14.  
  15. public class TileEntityAlloyOven extends TileEntity implements ISidedInventory {
  16.  
  17.     private ItemStack slots[];
  18.  
  19.     public int tripPower;
  20.     public int tripCooktime;
  21.     private static final int maxPower  = 10000;
  22.     public static final int mashingSpeed = 50;
  23.  
  24.     private static final int[] slots_top = new int[] {0, 1, 2};
  25.     private static final int[] slots_bottom = new int [] {4} ;
  26.     private static final int[] slots_side = new int [] {3};
  27.  
  28.  
  29.  
  30.     private String CustomName;
  31.  
  32.     public TileEntityAlloyOven() {
  33.         slots = new ItemStack[5];
  34.     }
  35.  
  36.     @Override
  37.     public int getSizeInventory() {
  38.         return slots.length;
  39.     }
  40.  
  41.     @Override
  42.     public ItemStack getStackInSlot(int i) {
  43.         return slots[i];
  44.     }
  45.  
  46.     @Override
  47.     public ItemStack decrStackSize(int i, int j) {
  48.         if (slots[i] != null) {
  49.             if (slots[i].stackSize <= j) {
  50.                 ItemStack itemstack = slots[i];
  51.                 slots[i] = null;
  52.                 return itemstack;
  53.             }
  54.  
  55.             ItemStack itemstack1 = slots[i].splitStack(j);
  56.  
  57.             if (slots[i].stackSize == 0) {
  58.                 slots[i] = null;
  59.             }
  60.  
  61.             return itemstack1;
  62.         }else{
  63.             return null;
  64.         }
  65.     }
  66.  
  67.     public void readFromNBT (NBTTagCompound nbt) {
  68.         super.readFromNBT(nbt);
  69.         NBTTagList list = nbt.getTagList("Items", 10);
  70.         slots = new ItemStack[getSizeInventory()];
  71.  
  72.         for (int i = 0; i < list.tagCount(); i++) {
  73.             NBTTagCompound nbt1 = (NBTTagCompound)list.getCompoundTagAt(i);
  74.             byte b0 = nbt1.getByte("Slot");
  75.  
  76.             if (b0 >= 0 && b0 < slots.length) {
  77.                 slots[b0] = ItemStack.loadItemStackFromNBT(nbt1);
  78.             }
  79.         }
  80.  
  81.         tripPower = nbt.getShort("PowerTime");
  82.         tripCooktime = nbt.getShort("CookTime");   
  83.     }
  84.  
  85.     public void writeToNBT(NBTTagCompound nbt) {
  86.         super.writeToNBT(nbt);
  87.         nbt.setShort("PowerTime", (short)tripPower);
  88.         nbt.setShort("CookTime", (short)tripCooktime);
  89.         NBTTagList list = new NBTTagList();
  90.  
  91.         for (int i = 0; i < slots.length; i++) {
  92.             if (slots[i] != null) {
  93.                 NBTTagCompound nbt1 = new NBTTagCompound();
  94.                 nbt1.setByte("Slot", (byte)i);
  95.                 slots[i].writeToNBT(nbt1);
  96.                 list.appendTag(nbt1);
  97.             }
  98.         }
  99.  
  100.         nbt.setTag("Items", list);
  101.  
  102.     }
  103.  
  104.  
  105.  
  106.     @Override
  107.     public ItemStack getStackInSlotOnClosing(int i) {
  108.         if (slots[i] != null) {
  109.             ItemStack itemstack = slots[i];
  110.             slots[i] = null;
  111.             return itemstack;
  112.         }else{
  113.             return null;
  114.         }
  115.     }
  116.     @Override
  117.     public void setInventorySlotContents(int i, ItemStack itemstack) {
  118.         slots[i] = itemstack;
  119.         if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
  120.             itemstack.stackSize = getInventoryStackLimit();
  121.         }
  122.  
  123.  
  124.  
  125.     }
  126.  
  127.     public void setGuiDisplayName (String name) {
  128.         this.CustomName = name;
  129.  
  130.     }
  131.     @Override
  132.     public String getInventoryName() {
  133.         return this.hasCustomInventoryName() ? this.CustomName : "container.alloyoven";
  134.     }
  135.     @Override
  136.     public boolean hasCustomInventoryName() {
  137.         return this.CustomName != null && this.CustomName.length() > 0;
  138.  
  139.  
  140.     }
  141.     @Override
  142.     public int getInventoryStackLimit() {
  143.         return 64;
  144.     }
  145.     @Override
  146.     public boolean isUseableByPlayer(EntityPlayer player) {
  147.         if (worldObj.getTileEntity(xCoord, yCoord, zCoord) != this) {
  148.             return false;
  149.  
  150.         }else{
  151.             return player.getDistanceSq((double)xCoord + 0.5D, (double)yCoord + 0.5D, (double)zCoord + 0.5D) <+ 64;
  152.         }
  153.     }
  154.  
  155.     public void openInventory() {}
  156.     public void closeInventory() {}
  157.  
  158.     @Override
  159.     public boolean isItemValidForSlot(int i, ItemStack itemstack) {
  160.         return i == 3 ? false : (i == 1 ? hasItemPower(itemstack) : true);
  161.     }
  162.  
  163.     public boolean hasItemPower(ItemStack itemstack) {
  164.         return getItemPower(itemstack) > 0;
  165.     }
  166.  
  167.     private static int getItemPower (ItemStack itemstack) {
  168.         if (itemstack == null) {
  169.             return 0;
  170.         }else{
  171.             Item item = itemstack.getItem();
  172.             //fuel items go here
  173.             if (item == Items.coal) return 50;
  174.  
  175.             return 0;
  176.         }
  177.     }
  178.  
  179.     @Override
  180.     public int[] getAccessibleSlotsFromSide(int i) {
  181.         return i == 0 ? slots_bottom : (i == 1 ? slots_top : slots_side);
  182.     }
  183.  
  184.     @Override
  185.     public boolean canInsertItem(int var1, ItemStack itemstack, int var3) {
  186.         return this.isItemValidForSlot(var1, itemstack);
  187.  
  188.     }
  189.  
  190.     @Override
  191.     public boolean canExtractItem(int i, ItemStack itemstack, int j) {
  192.         return j != 0 || i != 3 || itemstack.getItem() == Items.bucket;
  193.     }
  194.  
  195.     public int getOvenProgressScaled(int i) {
  196.         return (tripCooktime * i) / this.mashingSpeed;
  197.     }
  198.  
  199.     public int getPowerRemainingScaled(int i) {
  200.         return (tripPower * i) / maxPower;
  201.     }
  202.  
  203.     private boolean canMakeIngot() {
  204.  
  205.         if (slots[0] == null || slots[1] == null || slots[2] == null) {
  206.             return false;
  207.         }
  208.  
  209.         ItemStack itemstack = AlloyOvenRecipes.getMakingResult(slots[0].getItem(), slots[1].getItem(), slots[2].getItem());
  210.  
  211.         if (itemstack == null) {
  212.             return false;
  213.         }
  214.  
  215.         if (slots[4] == null) {
  216.             return true;
  217.         }
  218.  
  219.         if (!slots[4].isItemEqual(itemstack)) {
  220.             return false;
  221.         }
  222.  
  223.         if (slots[4].stackSize < getInventoryStackLimit() && slots[4].stackSize < slots[3].getMaxStackSize()) {
  224.             return true;
  225.         }else{
  226.             return slots[4].stackSize < itemstack.getMaxStackSize();
  227.         }
  228.  
  229.     }
  230.  
  231.     private void makeIngot() {
  232.         if (canMakeIngot()) {
  233.             ItemStack itemstack = AlloyOvenRecipes.getMakingResult(slots[0].getItem(), slots[1].getItem(), slots[2].getItem());
  234.  
  235.             if (slots[4] == null) {
  236.                 slots[4] = itemstack.copy();
  237.             }else if (slots[4].isItemEqual(itemstack)) {
  238.                 slots[4].stackSize += itemstack.stackSize;
  239.             }
  240.  
  241.             for (int i = 0; i < 2; i++) {
  242.                 if (slots[i].stackSize <= 0) {
  243.                     slots[i] = new ItemStack(slots[i].getItem().setFull3D());
  244.                 }else{
  245.                     slots[i].stackSize--;
  246.                 }
  247.  
  248.                 if (slots[i].stackSize <= 0) {
  249.                     slots[i] = null;
  250.                 }
  251.  
  252.             }
  253.  
  254.         }
  255.  
  256.     }
  257.  
  258.     public boolean hasPower() {
  259.         return tripPower > 0;
  260.     }
  261.  
  262.     public boolean isMaking() {
  263.         return this.tripCooktime > 0;
  264.     }
  265.  
  266.     public void updateEntity() {
  267.         boolean flag = this.hasPower();
  268.         boolean flag1 = false;
  269.  
  270.         if(hasPower() && this.makeIngot()) {
  271.             this.tripPower--;
  272.         }
  273.  
  274.         if(!worldObj.isRemote) {
  275.             if (this.hasItemPower(this.slots[3]) && this.tripPower < (this.maxPower - this.getItemPower(this.slots[3]))) {
  276.                 this.tripPower += getItemPower(this.slots[3]);
  277.  
  278.                 if(this.slots[3] != null) {
  279.                     flag1 = true;
  280.  
  281.                     this.slots[3].stackSize--;
  282.  
  283.                     if(this.slots[3].stackSize == 0) {
  284.                         this.slots[3] = this.slots[3].getItem().getContainerItem(this.slots[3]);
  285.                     }
  286.                 }
  287.             }
  288.            
  289.             if (hasPower() && canMakeIngot()) {
  290.                 tripCooktime++;
  291.                
  292.                 if (this.tripCooktime == this.mashingSpeed) {
  293.                     this.tripCooktime = 0;
  294.                     this.makeIngot();
  295.                     flag1 = true;
  296.                 }else{
  297.                     tripCooktime = 0;
  298.                 }
  299.                
  300.                 if (flag != this.hasPower()) {
  301.                     flag1 = true;
  302.                     AlloyOven.updateBlockState(this.hasPower(), this.worldObj, this.xCoord, this.yCoord, this.zCoord);
  303.                        
  304.                 }
  305.                
  306.             }
  307.            
  308.             if (flag1) {
  309.                 this.markDirty();
  310.             }
  311.            
  312.         }
  313.  
  314.     }
  315.  
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement