Advertisement
lethinh

ItemHandlerBase

May 18th, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. package thinh.eligibleadapter.tile.inventory;
  2.  
  3. import net.minecraft.item.ItemStack;
  4. import net.minecraft.tileentity.TileEntity;
  5. import net.minecraftforge.items.ItemStackHandler;
  6. import thinh.eligibleadapter.tile.TileSolderingStation;
  7.  
  8. public class ItemHandlerBase extends ItemStackHandler {
  9.  
  10.     private final TileEntity tile;
  11.  
  12.     public ItemHandlerBase(int size, TileEntity tile) {
  13.         super(size);
  14.         this.tile = tile;
  15.     }
  16.  
  17.     @Override public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) {
  18.         if (!this.canInsert(slot, stack)) {
  19.             return stack;
  20.         }
  21.  
  22.         return super.insertItem(slot, stack, simulate);
  23.     }
  24.  
  25.     @Override public ItemStack extractItem(int slot, int amount, boolean simulate) {
  26.         if (!this.canExtract(slot, this.getStackInSlot(slot))) {
  27.             return null;
  28.         }
  29.  
  30.         return super.extractItem(slot, amount, simulate);
  31.     }
  32.  
  33.     @Override protected void onContentsChanged(int slot) {
  34.         super.onContentsChanged(slot);
  35.  
  36.         if (this.tile != null) {
  37.             this.tile.markDirty();
  38.         }
  39.     }
  40.  
  41.     @Override protected int getStackLimit(int slot, ItemStack stack) {
  42.         return this.getStackLimit();
  43.     }
  44.  
  45.     @Override public ItemStack getStackInSlot(int slot) {
  46.         assert this.tile != null;
  47.  
  48.         if (this.tile instanceof TileSolderingStation) {
  49.             if (slot >= TileSolderingStation.contents.getSlots()) {
  50.                 return TileSolderingStation.craftMatrix.getStackInSlot(slot - TileSolderingStation.contents.getSlots());
  51.             }
  52.         }
  53.  
  54.         return super.getStackInSlot(slot);
  55.     }
  56.  
  57.     @Override public void setStackInSlot(int slot, ItemStack stack) {
  58.         assert this.tile != null;
  59.  
  60.         if (this.tile instanceof TileSolderingStation) {
  61.             if (slot >= TileSolderingStation.contents.getSlots()) {
  62.                 TileSolderingStation solderingStation = (TileSolderingStation) this.tile;
  63.                 solderingStation.craftMatrix.setStackInSlot(slot - TileSolderingStation.contents.getSlots(), stack);
  64.                 solderingStation.updateInput();
  65.             }
  66.         }
  67.  
  68.         super.setStackInSlot(slot, stack);
  69.     }
  70.  
  71.     public int getStackLimit() {
  72.         return 64;
  73.     }
  74.  
  75.     public boolean canInsert(int slot, ItemStack stack) {
  76.         return true;
  77.     }
  78.  
  79.     public boolean canExtract(int slot, ItemStack stack) {
  80.         return true;
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement