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);
this.stack = stack;
this.pipe = pipe;
if (stack.hasTag())
this.deserializeNBT(stack.getTag().getCompound("filter"));
this.load();
}
public List<Slot> 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<ItemFilter> 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() {}
}
}

View file

@ -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;

View file

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