Guest User

ContainerAdvancedFurnace.java

a guest
Apr 4th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.85 KB | None | 0 0
  1. package com.herobrine.future.tile.advancedfurnace;
  2.  
  3. import net.minecraft.entity.player.EntityPlayer;
  4. import net.minecraft.entity.player.InventoryPlayer;
  5. import net.minecraft.inventory.Container;
  6. import net.minecraft.inventory.IContainerListener;
  7. import net.minecraft.inventory.IInventory;
  8. import net.minecraft.inventory.Slot;
  9. import net.minecraft.item.ItemStack;
  10. import net.minecraft.item.crafting.FurnaceRecipes;
  11. import net.minecraft.tileentity.TileEntityFurnace;
  12. import net.minecraftforge.fml.relauncher.Side;
  13. import net.minecraftforge.fml.relauncher.SideOnly;
  14. import net.minecraftforge.items.CapabilityItemHandler;
  15. import net.minecraftforge.items.IItemHandler;
  16. import net.minecraftforge.items.SlotItemHandler;
  17.  
  18. public class ContainerAdvancedFurnace extends Container {
  19.     public final TileAdvancedFurnace te;
  20.     public final InventoryPlayer playerInventory;
  21.     private int fuelLeft, progress, itemOriginalFuel;
  22.     private boolean isBurningBinary;
  23.  
  24.     public ContainerAdvancedFurnace(InventoryPlayer playerInv, TileAdvancedFurnace te) {
  25.         this.te = te;
  26.         this.playerInventory = playerInv;
  27.         addOwnSlots();
  28.         addPlayerSlots(playerInv);
  29.     }
  30.  
  31.     private void addOwnSlots() {
  32.         IItemHandler handler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
  33.         addSlotToContainer(new SlotItemHandler(handler, 0, 56, 17));
  34.         addSlotToContainer(new SlotItemHandler(handler, 1, 56, 53));
  35.         addSlotToContainer(new SlotItemHandler(handler, 2, 116, 35));
  36.     }
  37.  
  38.     private void addPlayerSlots(IInventory playerInv) {
  39.         for (int row = 0; row < 3; ++row) {
  40.             for (int col = 0; col < 9; ++col) {//Inventory
  41.                 int x = 9 + col * 18 - 1;
  42.                 int y = row * 18 + 70 + 14;
  43.                 this.addSlotToContainer(new Slot(playerInv, col + row * 9 + 9, x, y));
  44.             }
  45.         }
  46.  
  47.         for (int row = 0; row < 9; ++row) {//Hotbar
  48.             int x = 9 + row * 18 - 1;
  49.             int y = 58 + 70 + 14;
  50.             this.addSlotToContainer(new Slot(playerInv, row, x, y));
  51.         }
  52.     }
  53.  
  54.     @Override
  55.     public boolean canInteractWith(EntityPlayer playerIn) {
  56.         return te.canInteractWith(playerIn);
  57.     }
  58.  
  59.     @Override
  60.     public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) { // Re-uses the transferStackInSlot from ContainerFurnace
  61.         ItemStack itemstack = ItemStack.EMPTY;
  62.         Slot slot = this.inventorySlots.get(index);
  63.  
  64.         if (slot != null && slot.getHasStack()) {
  65.             ItemStack itemstack1 = slot.getStack();
  66.             itemstack = itemstack1.copy();
  67.  
  68.             if (index == 2) {
  69.                 if (!this.mergeItemStack(itemstack1, 3, 39, true)) {
  70.                     return ItemStack.EMPTY;
  71.                 }
  72.  
  73.                 slot.onSlotChange(itemstack1, itemstack);
  74.             }
  75.             else if (index != 1 && index != 0) {
  76.                 if (!FurnaceRecipes.instance().getSmeltingResult(itemstack1).isEmpty()) {
  77.                     if (!this.mergeItemStack(itemstack1, 0, 1, false)) {
  78.                         return ItemStack.EMPTY;
  79.                     }
  80.                 }
  81.                 else if (TileEntityFurnace.isItemFuel(itemstack1)) {
  82.                     if (!this.mergeItemStack(itemstack1, 1, 2, false)) {
  83.                         return ItemStack.EMPTY;
  84.                     }
  85.                 }
  86.                 else if (index < 30) {
  87.                     if (!this.mergeItemStack(itemstack1, 30, 39, false)) {
  88.                         return ItemStack.EMPTY;
  89.                     }
  90.                 }
  91.                 else if (index < 39 && !this.mergeItemStack(itemstack1, 3, 30, false)) {
  92.                     return ItemStack.EMPTY;
  93.                 }
  94.             }
  95.             else if (!this.mergeItemStack(itemstack1, 3, 39, false)) {
  96.                 return ItemStack.EMPTY;
  97.             }
  98.  
  99.             if (itemstack1.isEmpty()) {
  100.                 slot.putStack(ItemStack.EMPTY);
  101.             }
  102.             else {
  103.                 slot.onSlotChanged();
  104.             }
  105.  
  106.             if (itemstack1.getCount() == itemstack.getCount()) {
  107.                 return ItemStack.EMPTY;
  108.             }
  109.             slot.onTake(playerIn, itemstack1);
  110.         }
  111.  
  112.         return itemstack;
  113.     }
  114.  
  115.     @Override
  116.     public void detectAndSendChanges() {
  117.         super.detectAndSendChanges();
  118.  
  119.         for (IContainerListener listener : listeners) {
  120.             if (this.progress != te.progress) listener.sendWindowProperty(this, 1, te.progress);
  121.             if (this.itemOriginalFuel != te.itemOriginalFuel) listener.sendWindowProperty(this, 2, te.itemOriginalFuel);
  122.             if (this.isBurningBinary != te.isBurning) listener.sendWindowProperty(this, 3, boolVal(te.isBurning));
  123.             if (this.fuelLeft != te.fuelLeft) listener.sendWindowProperty(this, 4, te.fuelLeft);
  124.         }
  125.  
  126.         this.fuelLeft = te.fuelLeft;
  127.         this.progress = te.progress;
  128.         this.itemOriginalFuel = te.itemOriginalFuel;
  129.         this.isBurningBinary = te.isBurning;
  130.     }
  131.  
  132.     @Override
  133.     @SideOnly(Side.CLIENT)
  134.     public void updateProgressBar(int id, int data) {
  135.         switch (id) {
  136.             case 1: {
  137.                 te.progress = data;
  138.             }
  139.             case 2: {
  140.                 te.itemOriginalFuel = data;
  141.             }
  142.             case 3: {
  143.                 te.isBurning = binVal(data);
  144.             }
  145.             case 4: {
  146.                 te.fuelLeft = data;
  147.             }
  148.         }
  149.     }
  150.  
  151.     @Override
  152.     public void addListener(IContainerListener listener) {
  153.         super.addListener(listener);
  154.         //addListener(listener.sendAllWindowProperties(this, this.te.combinedInventoryHandler);
  155.     }
  156.  
  157.     protected boolean binVal(int binary) {
  158.         return binary != 0;
  159.     }
  160.  
  161.     protected int boolVal(boolean bool) {
  162.         return bool ? 1 : 0;
  163.     }
  164. }
Add Comment
Please, Sign In to add comment