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);
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) {
@ -69,12 +68,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) {
var changed = false;
// populate filter from inventories
var filters = this.pipe.getFilters();
for (var direction : Direction.values()) {
@ -90,12 +90,15 @@ public class ItemFilter extends ItemStackHandler {
// try inserting into ourselves and any filter increase modifiers
for (var filter : filters) {
if (ItemHandlerHelper.insertItem(filter, copy, false).isEmpty()) {
changed = true;
filter.save();
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
public CompoundTag serializeNBT() {
var nbt = super.serializeNBT();
@ -155,5 +163,7 @@ public class ItemFilter extends ItemStackHandler {
public interface IFilteredContainer {
ItemFilter getFilter();
default void onFilterPopulated() {}
}
}

View file

@ -99,9 +99,8 @@ public class PacketButton {
}
}),
FILTER_CHANGE((pos, data, player) -> {
var container = (IFilteredContainer) player.containerMenu;
var filter = container.getFilter();
filter.onButtonPacket(data[0]);
if (player.containerMenu instanceof IFilteredContainer filtered)
filtered.getFilter().onButtonPacket(filtered, data[0]);
}),
STACK_SIZE_MODULE_BUTTON((pos, data, player) -> {
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.world.entity.player.Player;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.Slot;
import javax.annotation.Nullable;
@ -18,6 +17,12 @@ public class FilterModuleContainer extends AbstractPipeContainer<FilterModuleIte
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
protected void addSlots() {
this.filter = this.module.getItemFilter(this.moduleStack, this.tile);