From 9ff65e3b064edb3bb9f7bf3f512a75ba7b1259b3 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 30 Mar 2022 14:43:33 +0200 Subject: [PATCH] backport 8ead5087764d17b237071e0cd5f8f41c45789c1f --- .../ellpeck/prettypipes/misc/ItemFilter.java | 18 +++++++++++++++--- .../prettypipes/packets/PacketButton.java | 6 ++---- .../insertion/FilterModuleContainer.java | 7 +++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java b/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java index 53ef1b0..d50082c 100644 --- a/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java +++ b/src/main/java/de/ellpeck/prettypipes/misc/ItemFilter.java @@ -38,8 +38,7 @@ public class ItemFilter extends ItemStackHandler { super(size); this.stack = stack; this.pipe = pipe; - if (stack.hasTag()) - this.deserializeNBT(stack.getTag().getCompound("filter")); + this.load(); } public List getSlots(int x, int y) { @@ -70,12 +69,13 @@ public class ItemFilter extends ItemStackHandler { return buttons; } - public void onButtonPacket(int id) { + public void onButtonPacket(IFilteredContainer menu, int id) { if (id == 0 && this.canModifyWhitelist) { this.isWhitelist = !this.isWhitelist; this.modified = true; this.save(); } else if (id == 1 && this.canPopulateFromInventories) { + boolean changed = false; // populate filter from inventories List filters = this.pipe.getFilters(); for (Direction direction : Direction.values()) { @@ -91,12 +91,15 @@ public class ItemFilter extends ItemStackHandler { // try inserting into ourselves and any filter increase modifiers for (ItemFilter filter : filters) { if (ItemHandlerHelper.insertItem(filter, copy, false).isEmpty()) { + changed = true; filter.save(); break; } } } } + if (changed) + menu.onFilterPopulated(); } } @@ -126,6 +129,11 @@ public class ItemFilter extends ItemStackHandler { } } + public void load() { + if (this.stack.hasTag()) + this.deserializeNBT(this.stack.getTag().getCompound("filter")); + } + @Override public CompoundNBT serializeNBT() { CompoundNBT nbt = super.serializeNBT(); @@ -154,6 +162,10 @@ public class ItemFilter extends ItemStackHandler { } public interface IFilteredContainer { + ItemFilter getFilter(); + + default void onFilterPopulated() {} + } } diff --git a/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java b/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java index 477ca23..885f902 100644 --- a/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java +++ b/src/main/java/de/ellpeck/prettypipes/packets/PacketButton.java @@ -2,7 +2,6 @@ package de.ellpeck.prettypipes.packets; import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.items.IModule; -import de.ellpeck.prettypipes.misc.ItemFilter; import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer; import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; @@ -100,9 +99,8 @@ public class PacketButton { } }), FILTER_CHANGE((pos, data, player) -> { - IFilteredContainer container = (IFilteredContainer) player.openContainer; - ItemFilter filter = container.getFilter(); - filter.onButtonPacket(data[0]); + if (player.openContainer instanceof IFilteredContainer) + ((IFilteredContainer) player.openContainer).getFilter().onButtonPacket((IFilteredContainer) player.openContainer, data[0]); }), STACK_SIZE_MODULE_BUTTON((pos, data, player) -> { AbstractPipeContainer container = (AbstractPipeContainer) player.openContainer; diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleContainer.java b/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleContainer.java index 74d4815..6f10d5c 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleContainer.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/modules/insertion/FilterModuleContainer.java @@ -35,4 +35,11 @@ public class FilterModuleContainer extends AbstractPipeContainer