Advertisement
Guest User

TileNetherrackFurnace.java

a guest
Jul 1st, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.85 KB | None | 0 0
  1. package qwertyasdef.alchemtrans.Tile;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.inventory.ISidedInventory;
  5. import net.minecraft.inventory.ItemStackHelper;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.nbt.NBTTagCompound;
  8. import net.minecraft.tileentity.TileEntity;
  9. import net.minecraft.util.EnumFacing;
  10. import net.minecraft.util.ITickable;
  11. import net.minecraft.util.NonNullList;
  12. import net.minecraft.util.text.ITextComponent;
  13. import net.minecraft.util.text.TextComponentString;
  14. import net.minecraft.util.text.TextComponentTranslation;
  15.  
  16. public class TileNetherrackFurnace extends TileEntity implements ITickable, ISidedInventory {
  17.  
  18.     private String customName;
  19.  
  20.     private int cookTime;
  21.     private int totalCookTime;
  22.     private NonNullList<ItemStack> inventory = NonNullList.<ItemStack>withSize(2, ItemStack.EMPTY);
  23.  
  24.     private int[] SLOTS_UP = {0};
  25.     private int[] SLOTS_DOWN = {1};
  26.     private int[] SLOTS_SIDE = {0, 1};
  27.  
  28.     public TileNetherrackFurnace() {
  29.     }
  30.  
  31.     public String getCustomName() {
  32.         return this.customName;
  33.     }
  34.  
  35.     public void setCustomName(String customName) {
  36.         this.customName = customName;
  37.     }
  38.  
  39.     @Override
  40.     public String getName() {
  41.         return this.hasCustomName() ? this.customName : "container.netherrack_furnace";
  42.     }
  43.  
  44.     @Override
  45.     public boolean hasCustomName() {
  46.         return this.customName != null && !this.customName.equals("");
  47.     }
  48.  
  49.     @Override
  50.     public ITextComponent getDisplayName() {
  51.         return this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName());
  52.     }
  53.  
  54.     // IInventory
  55.     @Override
  56.     public int getSizeInventory() {
  57.         return this.inventory.size();
  58.     }
  59.  
  60.     @Override
  61.     public boolean isEmpty() {
  62.         for (int i = 0; i < getSizeInventory(); i++) {
  63.             if (this.getStackInSlot(i) != ItemStack.EMPTY) {
  64.                 return false;
  65.             }
  66.         }
  67.         return true;
  68.     }
  69.  
  70.     @Override
  71.     public ItemStack getStackInSlot(int index) {
  72.         if (index < 0 || index >= this.getSizeInventory())
  73.             return null;
  74.         return this.inventory.get(index);
  75.     }
  76.  
  77.     @Override
  78.     public ItemStack decrStackSize(int index, int count) {
  79.         if (this.getStackInSlot(index) != ItemStack.EMPTY) {
  80.             ItemStack itemstack;
  81.  
  82.             if (this.getStackInSlot(index).getCount() <= count) {
  83.                 itemstack = this.getStackInSlot(index);
  84.                 this.setInventorySlotContents(index, ItemStack.EMPTY);
  85.                 this.markDirty();
  86.                 return itemstack;
  87.             } else {
  88.                 itemstack = this.getStackInSlot(index).splitStack(count);
  89.  
  90.                 if (this.getStackInSlot(index).getCount() <= 0) {
  91.                     this.setInventorySlotContents(index, ItemStack.EMPTY);
  92.                 } else {
  93.                     //Just to show that changes happened
  94.                     this.setInventorySlotContents(index, this.getStackInSlot(index));
  95.                 }
  96.  
  97.                 this.markDirty();
  98.                 return itemstack;
  99.             }
  100.         } else {
  101.             return ItemStack.EMPTY;
  102.         }
  103.     }
  104.  
  105.     @Override
  106.     public ItemStack removeStackFromSlot(int index) {
  107.         ItemStack stack = this.getStackInSlot(index);
  108.         this.setInventorySlotContents(index, ItemStack.EMPTY);
  109.         return stack;
  110.     }
  111.  
  112.     @Override
  113.     public void setInventorySlotContents(int index, ItemStack stack) {
  114.         if (index < 0 || index >= this.getSizeInventory()) {
  115.             return;
  116.         }
  117.  
  118.         if (stack != ItemStack.EMPTY && stack.getCount() > this.getInventoryStackLimit()) {
  119.             stack.setCount(this.getInventoryStackLimit());
  120.         }
  121.  
  122.         if (stack != ItemStack.EMPTY && stack.getCount() == 0) {
  123.             stack = ItemStack.EMPTY;
  124.         }
  125.  
  126.         this.inventory.set(index, stack);
  127.         this.markDirty();
  128.     }
  129.  
  130.     @Override
  131.     public int getInventoryStackLimit() {
  132.         return 64;
  133.     }
  134.  
  135.     @Override
  136.     public boolean isUsableByPlayer(EntityPlayer player) {
  137.         return this.getWorld().getTileEntity(this.getPos()) == this && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64;
  138.     }
  139.  
  140.     @Override
  141.     public void openInventory(EntityPlayer player) {
  142.     }
  143.  
  144.     @Override
  145.     public void closeInventory(EntityPlayer player) {
  146.     }
  147.  
  148.     @Override
  149.     public boolean isItemValidForSlot(int index, ItemStack stack) {
  150.         return index == 0;
  151.     }
  152.  
  153.     @Override
  154.     public int getField(int id) {
  155.         switch (id) {
  156.             case 0:
  157.                 return this.cookTime;
  158.             case 1:
  159.                 return this.totalCookTime;
  160.             default:
  161.                 return 0;
  162.         }
  163.     }
  164.  
  165.     @Override
  166.     public void setField(int id, int value) {
  167.         switch (id) {
  168.             case 0:
  169.                 this.cookTime = value;
  170.                 break;
  171.             case 1:
  172.                 this.totalCookTime = value;
  173.                 break;
  174.         }
  175.     }
  176.  
  177.     @Override
  178.     public int getFieldCount() {
  179.         return 2;
  180.     }
  181.  
  182.     @Override
  183.     public void clear() {
  184.         for (int i = 0; i < this.getSizeInventory(); i++) {
  185.             this.setInventorySlotContents(i, ItemStack.EMPTY);
  186.         }
  187.     }
  188.  
  189.     // ISidedInventory
  190.     @Override
  191.     public int[] getSlotsForFace(EnumFacing side) {
  192.         switch (side) {
  193.             case UP:
  194.                 return SLOTS_UP;
  195.             case DOWN:
  196.                 return SLOTS_DOWN;
  197.             default:
  198.                 return SLOTS_SIDE;
  199.         }
  200.     }
  201.  
  202.     @Override
  203.     public boolean canInsertItem(int index, ItemStack itemIn, EnumFacing side) {
  204.         return this.isItemValidForSlot(index, itemIn);
  205.     }
  206.  
  207.     @Override
  208.     public boolean canExtractItem(int index, ItemStack itemIn, EnumFacing side) {
  209.         return true;
  210.     }
  211.  
  212.     // ITickable
  213.     @Override
  214.     public void update() {
  215.         ItemStack input = this.getStackInSlot(0);
  216.         if (canSmelt()) {
  217.             if (this.totalCookTime == 0) {
  218.                 this.totalCookTime = getCookTime(input);
  219.             }
  220.  
  221.             ++this.cookTime;
  222.             System.out.println("Cooking: cookTime = " + this.cookTime);
  223.  
  224.             if (this.cookTime >= this.totalCookTime) {
  225.                 smeltItem();
  226.                 input = this.getStackInSlot(0);
  227.                 System.out.println("Smelted item!");
  228.  
  229.                 if (input != ItemStack.EMPTY) {
  230.                     this.totalCookTime = getCookTime(input);
  231.                 } else {
  232.                     this.totalCookTime = 0;
  233.                 }
  234.                 this.cookTime = 0;
  235.             }
  236.         } else {
  237.             this.cookTime = 0;
  238.         }
  239.     }
  240.  
  241.     public int getCookTime(ItemStack stack) {
  242.         return 160;
  243.     }
  244.  
  245.     private boolean canSmelt() {
  246.         ItemStack input = this.getStackInSlot(0);
  247.         ItemStack result = input.copy();
  248.         ItemStack output = this.getStackInSlot(1);
  249.  
  250.         if (input.isEmpty()) {
  251.             return false;
  252.         }
  253.  
  254.         if (result.isEmpty()) {
  255.             return false;
  256.         }
  257.  
  258.         if (result.getItem() == output.getItem() && result.getCount() + output.getCount() <= this.getInventoryStackLimit()) {
  259.             return true;
  260.         }
  261.  
  262.         return false;
  263.     }
  264.  
  265.     public void smeltItem() {
  266.         if (this.canSmelt()) {
  267.             ItemStack input = this.getStackInSlot(0);
  268.             ItemStack result = input.copy();
  269.             ItemStack output = this.getStackInSlot(1);
  270.  
  271.             input.shrink(1);
  272.             if (input.getCount() <= 0) {
  273.                 this.setInventorySlotContents(0, ItemStack.EMPTY);
  274.             }
  275.  
  276.             if (output.isEmpty()) {
  277.                 this.setInventorySlotContents(1, result.copy());
  278.             } else {
  279.                 output.grow(result.getCount());
  280.             }
  281.         }
  282.     }
  283.  
  284.     // TileEntity
  285.     @Override
  286.     public NBTTagCompound writeToNBT(NBTTagCompound compound) {
  287.         super.writeToNBT(compound);
  288.  
  289.         if (this.hasCustomName()) {
  290.             compound.setString("CustomName", this.getCustomName());
  291.         }
  292.  
  293.         compound.setInteger("CookTime", this.cookTime);
  294.         compound.setInteger("TotalCookTime", this.totalCookTime);
  295.  
  296.         ItemStackHelper.saveAllItems(compound, this.inventory);
  297.  
  298.         return compound;
  299.     }
  300.  
  301.     @Override
  302.     public void readFromNBT(NBTTagCompound compound) {
  303.         super.readFromNBT(compound);
  304.  
  305.         if (compound.hasKey("CustomName", 8)) {
  306.             this.setCustomName(compound.getString("CustomName"));
  307.         }
  308.  
  309.         this.cookTime = compound.getInteger("CookTime");
  310.         this.totalCookTime = compound.getInteger("TotalCookTime");
  311.  
  312.         ItemStackHelper.loadAllItems(compound, this.inventory);
  313.  
  314.     }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement