fixed item filter population not being visible until the container is reopened

Closes #78
This commit is contained in:
Ell 2021-12-28 12:55:11 +01:00
parent 463e4378bb
commit 8ead508776
3 changed files with 21 additions and 7 deletions

View file

@ -37,8 +37,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) {
@ -69,12 +68,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) {
var changed = false;
// populate filter from inventories // populate filter from inventories
var filters = this.pipe.getFilters(); var filters = this.pipe.getFilters();
for (var direction : Direction.values()) { for (var direction : Direction.values()) {
@ -90,12 +90,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 (var filter : filters) { for (var 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();
} }
} }
@ -125,6 +128,11 @@ public class ItemFilter extends ItemStackHandler {
} }
} }
public void load() {
if (this.stack.hasTag())
this.deserializeNBT(this.stack.getTag().getCompound("filter"));
}
@Override @Override
public CompoundTag serializeNBT() { public CompoundTag serializeNBT() {
var nbt = super.serializeNBT(); var nbt = super.serializeNBT();
@ -155,5 +163,7 @@ public class ItemFilter extends ItemStackHandler {
public interface IFilteredContainer { public interface IFilteredContainer {
ItemFilter getFilter(); ItemFilter getFilter();
default void onFilterPopulated() {}
} }
} }

View file

@ -99,9 +99,8 @@ public class PacketButton {
} }
}), }),
FILTER_CHANGE((pos, data, player) -> { FILTER_CHANGE((pos, data, player) -> {
var container = (IFilteredContainer) player.containerMenu; if (player.containerMenu instanceof IFilteredContainer filtered)
var filter = container.getFilter(); filtered.getFilter().onButtonPacket(filtered, data[0]);
filter.onButtonPacket(data[0]);
}), }),
STACK_SIZE_MODULE_BUTTON((pos, data, player) -> { STACK_SIZE_MODULE_BUTTON((pos, data, player) -> {
var container = (AbstractPipeContainer<?>) player.containerMenu; var container = (AbstractPipeContainer<?>) player.containerMenu;

View file

@ -6,7 +6,6 @@ import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.MenuType; import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.Slot;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -18,6 +17,12 @@ public class FilterModuleContainer extends AbstractPipeContainer<FilterModuleIte
super(type, id, player, pos, moduleIndex); super(type, id, player, pos, moduleIndex);
} }
@Override
public void onFilterPopulated() {
// reload the filter so that it displays correctly on the client
this.filter.load();
}
@Override @Override
protected void addSlots() { protected void addSlots() {
this.filter = this.module.getItemFilter(this.moduleStack, this.tile); this.filter = this.module.getItemFilter(this.moduleStack, this.tile);