This commit is contained in:
Ell 2022-03-30 14:43:33 +02:00
parent db58a7b105
commit 9ff65e3b06
3 changed files with 24 additions and 7 deletions

View file

@ -38,8 +38,7 @@ public class ItemFilter extends ItemStackHandler {
super(size); super(size);
this.stack = stack; this.stack = stack;
this.pipe = pipe; this.pipe = pipe;
if (stack.hasTag()) this.load();
this.deserializeNBT(stack.getTag().getCompound("filter"));
} }
public List<Slot> getSlots(int x, int y) { public List<Slot> getSlots(int x, int y) {
@ -70,12 +69,13 @@ public class ItemFilter extends ItemStackHandler {
return buttons; return buttons;
} }
public void onButtonPacket(int id) { public void onButtonPacket(IFilteredContainer menu, int id) {
if (id == 0 && this.canModifyWhitelist) { if (id == 0 && this.canModifyWhitelist) {
this.isWhitelist = !this.isWhitelist; this.isWhitelist = !this.isWhitelist;
this.modified = true; this.modified = true;
this.save(); this.save();
} else if (id == 1 && this.canPopulateFromInventories) { } else if (id == 1 && this.canPopulateFromInventories) {
boolean changed = false;
// populate filter from inventories // populate filter from inventories
List<ItemFilter> filters = this.pipe.getFilters(); List<ItemFilter> filters = this.pipe.getFilters();
for (Direction direction : Direction.values()) { for (Direction direction : Direction.values()) {
@ -91,12 +91,15 @@ public class ItemFilter extends ItemStackHandler {
// try inserting into ourselves and any filter increase modifiers // try inserting into ourselves and any filter increase modifiers
for (ItemFilter filter : filters) { for (ItemFilter filter : filters) {
if (ItemHandlerHelper.insertItem(filter, copy, false).isEmpty()) { if (ItemHandlerHelper.insertItem(filter, copy, false).isEmpty()) {
changed = true;
filter.save(); filter.save();
break; 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 @Override
public CompoundNBT serializeNBT() { public CompoundNBT serializeNBT() {
CompoundNBT nbt = super.serializeNBT(); CompoundNBT nbt = super.serializeNBT();
@ -154,6 +162,10 @@ public class ItemFilter extends ItemStackHandler {
} }
public interface IFilteredContainer { public interface IFilteredContainer {
ItemFilter getFilter(); ItemFilter getFilter();
default void onFilterPopulated() {}
} }
} }

View file

@ -2,7 +2,6 @@ package de.ellpeck.prettypipes.packets;
import de.ellpeck.prettypipes.Utility; import de.ellpeck.prettypipes.Utility;
import de.ellpeck.prettypipes.items.IModule; import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer; import de.ellpeck.prettypipes.misc.ItemFilter.IFilteredContainer;
import de.ellpeck.prettypipes.pipe.PipeTileEntity; import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer; import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
@ -100,9 +99,8 @@ public class PacketButton {
} }
}), }),
FILTER_CHANGE((pos, data, player) -> { FILTER_CHANGE((pos, data, player) -> {
IFilteredContainer container = (IFilteredContainer) player.openContainer; if (player.openContainer instanceof IFilteredContainer)
ItemFilter filter = container.getFilter(); ((IFilteredContainer) player.openContainer).getFilter().onButtonPacket((IFilteredContainer) player.openContainer, data[0]);
filter.onButtonPacket(data[0]);
}), }),
STACK_SIZE_MODULE_BUTTON((pos, data, player) -> { STACK_SIZE_MODULE_BUTTON((pos, data, player) -> {
AbstractPipeContainer<?> container = (AbstractPipeContainer<?>) player.openContainer; AbstractPipeContainer<?> container = (AbstractPipeContainer<?>) player.openContainer;

View file

@ -35,4 +35,11 @@ public class FilterModuleContainer extends AbstractPipeContainer<FilterModuleIte
public ItemFilter getFilter() { public ItemFilter getFilter() {
return this.filter; return this.filter;
} }
@Override
public void onFilterPopulated() {
// reload the filter so that it displays correctly on the client
this.filter.load();
}
} }