Vaerys_Dawn

Automation Slot doesn't stop invalid items

Jan 28th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1.  
  2. //EnderBeeconTileEntity
  3.  
  4.     public AutomationSensitiveItemStackHandler h = new EnderBeeconTileEntity.TileStackHandler(2, getAcceptor(), getRemover());
  5.  
  6.     public AutomationSensitiveItemStackHandler.IAcceptor getAcceptor() {
  7.         return (slot, stack, automation) -> !automation || slot == 0;
  8.     }
  9.  
  10.     public AutomationSensitiveItemStackHandler.IRemover getRemover() {
  11.         return (slot, automation) -> !automation || slot == 1;
  12.     }
  13.  
  14.     protected class TileStackHandler extends AutomationSensitiveItemStackHandler {
  15.         protected TileStackHandler(int slots, IAcceptor acceptor, IRemover remover) {
  16.             super(slots, acceptor, remover);
  17.         }
  18.  
  19.         @Override
  20.         protected void onContentsChanged(int slot) {
  21.             super.onContentsChanged(slot);
  22.             markDirty();
  23.         }
  24.     }
  25.  
  26. // EnderBeeconContainer
  27.  
  28.             this.addSlot(new SlotItemHandlerUnconditioned(enderBeeconTileEntity.h, EnderBeeconTileEntity.HONEY_BOTTLE_INPUT, 184, 34) {
  29.                 public boolean isItemValid(ItemStack stack) {
  30.                     if (stack.getItem() instanceof BucketItem) {
  31.                         BucketItem bucket = (BucketItem) stack.getItem();
  32.                         return bucket.getFluid().isIn(enderBeeconTileEntity.honeyFluidTag);
  33.                     } else {
  34.                         return stack.getItem().isIn(enderBeeconTileEntity.honeyBottleTag);
  35.                     }
  36.                 }
  37.             });
  38.  
  39.  
  40.  
  41. /** * @author Shadows
  42.  * Taken from Actually Additions owned by Ellpeck
  43.  */
  44. public class AutomationSensitiveItemStackHandler extends ItemStackHandler {
  45.  
  46.     public static final IAcceptor ACCEPT_TRUE = (a, b, c) -> true;
  47.     public static final IRemover REMOVE_TRUE = (a, b) -> true;
  48.     public static final IAcceptor ACCEPT_FALSE = (a, b, c) -> false;
  49.     public static final IRemover REMOVE_FALSE = (a, b) -> false;
  50.  
  51.     IAcceptor acceptor;
  52.     IRemover remover;
  53.  
  54.     public AutomationSensitiveItemStackHandler(NonNullList<ItemStack> stacks, IAcceptor acceptor, IRemover remover) {
  55.         super(stacks);
  56.         this.acceptor = acceptor;
  57.         this.remover = remover;
  58.     }
  59.  
  60.     public AutomationSensitiveItemStackHandler(int slots, IAcceptor acceptor, IRemover remover) {
  61.         super(slots);
  62.         this.acceptor = acceptor;
  63.         this.remover = remover;
  64.     }
  65.  
  66.     public AutomationSensitiveItemStackHandler(NonNullList<ItemStack> stacks) {
  67.         this(stacks, ACCEPT_TRUE, REMOVE_TRUE);
  68.     }
  69.  
  70.     public AutomationSensitiveItemStackHandler(int slots) {
  71.         this(slots, ACCEPT_TRUE, REMOVE_TRUE);
  72.     }
  73.  
  74.     public NonNullList<ItemStack> getItems() {
  75.         return this.stacks;
  76.     }
  77.  
  78.     @Nonnull
  79.     @Override
  80.     public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
  81.         return this.insertItem(slot, stack, simulate, true);
  82.     }
  83.  
  84.     public ItemStack insertItem(int slot, ItemStack stack, boolean simulate, boolean fromAutomation) {
  85.         if (this.canAccept(slot, stack, fromAutomation)) return super.insertItem(slot, stack, simulate);
  86.         return stack;
  87.     }
  88.  
  89.     public void deserializeNBTWithoutCheckingSize(CompoundNBT nbt)
  90.     {
  91.         ListNBT tagList = nbt.getList("Items", Constants.NBT.TAG_COMPOUND);
  92.         for (int i = 0; i < tagList.size(); i++)
  93.         {
  94.             CompoundNBT itemTags = tagList.getCompound(i);
  95.             int slot = itemTags.getInt("Slot");
  96.  
  97.             if (slot >= 0 && slot < stacks.size())
  98.             {
  99.                 stacks.set(slot, ItemStack.read(itemTags));
  100.             }
  101.         }
  102.         onLoad();
  103.     }
  104.  
  105.     @Nonnull
  106.     @Override
  107.     public ItemStack extractItem(int slot, int amount, boolean simulate) {
  108.         return this.extractItem(slot, amount, simulate, true);
  109.     }
  110.  
  111.     public ItemStack extractItem(int slot, int amount, boolean simulate, boolean byAutomation) {
  112.         if (!this.canRemove(slot, byAutomation)) return ItemStack.EMPTY;
  113.         return super.extractItem(slot, amount, simulate);
  114.     }
  115.  
  116.     public final boolean canAccept(int slot, ItemStack stack, boolean automation) {
  117.         return this.getAcceptor().canAccept(slot, stack, automation);
  118.     }
  119.  
  120.     public final boolean canRemove(int slot, boolean automation) {
  121.         return this.getRemover().canRemove(slot, automation);
  122.     }
  123.  
  124.     public IAcceptor getAcceptor() {
  125.         return this.acceptor;
  126.     }
  127.  
  128.     public IRemover getRemover() {
  129.         return this.remover;
  130.     }
  131.  
  132.     public interface IAcceptor {
  133.         boolean canAccept(int slot, ItemStack stack, boolean automation);
  134.     }
  135.  
  136.     public interface IRemover {
  137.         boolean canRemove(int slot, boolean automation);
  138.     }
  139. }
  140.  
Add Comment
Please, Sign In to add comment