ReAdding Damage Filtering, and NBT filtering.

This commit is contained in:
Flanks255 2024-05-30 17:02:24 -05:00
parent 764ded5855
commit 6389668bf9
8 changed files with 98 additions and 44 deletions

View file

@ -79,7 +79,7 @@ public class CommonEvents {
boolean changed = false; boolean changed = false;
boolean isVoid = ((Sack) invStack.getItem()).isVoid; boolean isVoid = ((Sack) invStack.getItem()).isVoid;
FilterSettings filter = new FilterSettings(4, false, false); FilterSettings filter = new FilterSettings(4, false, false, false, false);
filter.readFromNBT(invStack.getOrCreateTag(), "Filter"); filter.readFromNBT(invStack.getOrCreateTag(), "Filter");
if (isVoid) { if (isVoid) {

View file

@ -32,7 +32,7 @@ import java.util.UUID;
public class SackContainer extends AbstractContainerMenu implements IButtonReactor { public class SackContainer extends AbstractContainerMenu implements IButtonReactor {
public final FilterSettings filter = new FilterSettings(4, false,false); public final FilterSettings filter = new FilterSettings(4, false,false, false, false);
private final ItemStackHandlerAA bagInventory; private final ItemStackHandlerAA bagInventory;
private final Inventory inventory; private final Inventory inventory;
public boolean autoInsert; public boolean autoInsert;

View file

@ -19,7 +19,7 @@ import net.minecraft.world.item.ItemStack;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
public class VoidSackContainer extends AbstractContainerMenu implements IButtonReactor { public class VoidSackContainer extends AbstractContainerMenu implements IButtonReactor {
public final FilterSettings filter = new FilterSettings(4, false,false); public final FilterSettings filter = new FilterSettings(4, false,false, false, false);
private final ItemStackHandlerAA bagInventory; private final ItemStackHandlerAA bagInventory;
private final Inventory inventory; private final Inventory inventory;

View file

@ -32,6 +32,8 @@ public class FilterSettingsGui {
public Button whitelistButton; public Button whitelistButton;
public Button modButton; public Button modButton;
public Button damageButton;
public Button nbtButton;
public FilterSettingsGui(FilterSettings settings, int x, int y, boolean vertical, Consumer<AbstractButton> buttonConsumer, Consumer<Integer> clickConsumer, int idOffset) { public FilterSettingsGui(FilterSettings settings, int x, int y, boolean vertical, Consumer<AbstractButton> buttonConsumer, Consumer<Integer> clickConsumer, int idOffset) {
this.theSettings = settings; this.theSettings = settings;
@ -42,10 +44,12 @@ public class FilterSettingsGui {
}) })
.bounds(x, y, 16, 12).build(); .bounds(x, y, 16, 12).build();
buttonConsumer.accept(this.whitelistButton); buttonConsumer.accept(this.whitelistButton);
if (vertical) if (vertical)
y += 14; y += 14;
else else
x += 18; x += 18;
this.modButton = Button.builder(Component.literal("MO"), $ -> { this.modButton = Button.builder(Component.literal("MO"), $ -> {
theSettings.respectMod = !theSettings.respectMod; theSettings.respectMod = !theSettings.respectMod;
clickConsumer.accept(idOffset + 1); clickConsumer.accept(idOffset + 1);
@ -53,17 +57,33 @@ public class FilterSettingsGui {
.bounds(x, y, 16, 12).build(); .bounds(x, y, 16, 12).build();
buttonConsumer.accept(this.modButton); buttonConsumer.accept(this.modButton);
if (vertical)
y += 14;
else
x += 18;
this.damageButton = Button.builder(Component.literal("DM"), $ -> {
theSettings.matchDamage = !theSettings.matchDamage;
clickConsumer.accept(idOffset + 2);
})
.bounds(x, y, 16, 12).build();
buttonConsumer.accept(this.damageButton);
if (vertical)
y += 14;
else
x += 18;
this.nbtButton = Button.builder(Component.literal("DA"), $ -> {
theSettings.matchNBT = !theSettings.matchNBT;
clickConsumer.accept(idOffset + 2);
})
.bounds(x, y, 16, 12).build();
buttonConsumer.accept(this.nbtButton);
this.tick(); this.tick();
} }
/* public void buttonClicked(int id) {
CompoundTag data = new CompoundTag();
data.putInt("ButtonID", id);
data.putInt("PlayerID", Minecraft.getInstance().player.getId());
data.putString("WorldID", Minecraft.getInstance().level.dimension().location().toString());
PacketDistributor.SERVER.noArg().send(new PacketClientToServer(data, PacketHandler.GUI_BUTTON_TO_CONTAINER_HANDLER));
}*/
public void tick() { public void tick() {
this.whitelistButton.setMessage(Component.literal("WH").withStyle(this.theSettings.isWhitelist this.whitelistButton.setMessage(Component.literal("WH").withStyle(this.theSettings.isWhitelist
? ChatFormatting.DARK_GREEN ? ChatFormatting.DARK_GREEN
@ -71,6 +91,12 @@ public class FilterSettingsGui {
this.modButton.setMessage(Component.literal("MO").withStyle(this.theSettings.respectMod this.modButton.setMessage(Component.literal("MO").withStyle(this.theSettings.respectMod
? ChatFormatting.DARK_GREEN ? ChatFormatting.DARK_GREEN
: ChatFormatting.RED)); : ChatFormatting.RED));
this.damageButton.setMessage(Component.literal("DM").withStyle(this.theSettings.matchDamage
? ChatFormatting.DARK_GREEN
: ChatFormatting.RED));
this.nbtButton.setMessage(Component.literal("DA").withStyle(this.theSettings.matchNBT
? ChatFormatting.DARK_GREEN
: ChatFormatting.RED));
} }
public void drawHover(GuiGraphics guiGraphics, int mouseX, int mouseY) { public void drawHover(GuiGraphics guiGraphics, int mouseX, int mouseY) {
@ -78,16 +104,22 @@ public class FilterSettingsGui {
List<Component> list = new ArrayList<>(); List<Component> list = new ArrayList<>();
if (this.whitelistButton.isMouseOver(mouseX, mouseY)) { if (this.whitelistButton.isMouseOver(mouseX, mouseY)) {
list.add((this.theSettings.isWhitelist list.add((this.theSettings.isWhitelist
? Component.translatable("info." + ActuallyAdditions.MODID + ".gui.whitelist") ? Component.translatable("info.actuallyadditions.gui.whitelist")
: Component.translatable("info." + ActuallyAdditions.MODID + ".gui.blacklist")).withStyle(ChatFormatting.BOLD)); : Component.translatable("info.actuallyadditions.gui.blacklist")).withStyle(ChatFormatting.BOLD));
list.add(Component.translatable("info." + ActuallyAdditions.MODID + ".gui.whitelistInfo")); list.add(Component.translatable("info.actuallyadditions.gui.whitelistInfo"));
} else if (this.modButton.isMouseOver(mouseX, mouseY)) { } else if (this.modButton.isMouseOver(mouseX, mouseY)) {
list.add((this.theSettings.respectMod list.add((this.theSettings.respectMod
? Component.translatable("info." + ActuallyAdditions.MODID + ".gui.respectMod") ? Component.translatable("info.actuallyadditions.gui.respectMod")
: Component.translatable("info." + ActuallyAdditions.MODID + ".gui.ignoreMod")).withStyle(ChatFormatting.BOLD)); : Component.translatable("info.actuallyadditions.gui.ignoreMod")).withStyle(ChatFormatting.BOLD));
list.add(Component.translatable("info." + ActuallyAdditions.MODID + ".gui.respectModInfo")); list.add(Component.translatable("info.actuallyadditions.gui.respectModInfo"));
} else if (this.modButton.isMouseOver(mouseX, mouseY)) {
list.add((this.theSettings.matchDamage
? Component.translatable("info.actuallyadditions.gui.respectDamage")
: Component.translatable("info.actuallyadditions.gui.ignoreDamage")).withStyle(ChatFormatting.BOLD));
} else if (this.modButton.isMouseOver(mouseX, mouseY)) {
list.add((this.theSettings.matchNBT
? Component.translatable("info.actuallyadditions.gui.respectNBT")
: Component.translatable("info.actuallyadditions.gui.ignoreNBT")).withStyle(ChatFormatting.BOLD));
} }
//TODO tooltips still jank //TODO tooltips still jank
if (!list.isEmpty()) { if (!list.isEmpty()) {

View file

@ -25,15 +25,21 @@ public class FilterSettings {
public final ItemStackHandlerAA filterInventory; public final ItemStackHandlerAA filterInventory;
public boolean isWhitelist; public boolean isWhitelist;
public boolean respectMod; public boolean respectMod;
public boolean matchDamage;
public boolean matchNBT;
private boolean lastWhitelist; private boolean lastWhitelist;
private boolean lastRespectMod; private boolean lastRespectMod;
private boolean lastMatchDamage;
private boolean lastMatchNBT;
public enum Buttons { public enum Buttons {
WHITELIST, WHITELIST,
MOD MOD,
DAMAGE,
NBT
} }
public FilterSettings(int slots, boolean defaultWhitelist, boolean defaultRespectMod) { public FilterSettings(int slots, boolean defaultWhitelist, boolean defaultRespectMod, boolean defaultMatchDamage, boolean defaultMatchNBT) {
this.filterInventory = new ItemStackHandlerAA(slots) { this.filterInventory = new ItemStackHandlerAA(slots) {
@Override @Override
protected void onContentsChanged(int slot) { protected void onContentsChanged(int slot) {
@ -44,11 +50,13 @@ public class FilterSettings {
this.isWhitelist = defaultWhitelist; this.isWhitelist = defaultWhitelist;
this.respectMod = defaultRespectMod; this.respectMod = defaultRespectMod;
this.matchDamage = defaultMatchDamage;
this.matchNBT = defaultMatchNBT;
} }
public void onContentsChanged() {} public void onContentsChanged() {}
public static boolean check(ItemStack stack, ItemStackHandlerAA filter, boolean whitelist, boolean mod) { public static boolean check(ItemStack stack, ItemStackHandlerAA filter, boolean whitelist, boolean mod, boolean damage, boolean nbt) {
if (!stack.isEmpty()) { if (!stack.isEmpty()) {
for (int i = 0; i < filter.getSlots(); i++) { for (int i = 0; i < filter.getSlots(); i++) {
ItemStack slot = filter.getStackInSlot(i); ItemStack slot = filter.getStackInSlot(i);
@ -59,7 +67,7 @@ public class FilterSettings {
DrillItem.loadSlotsFromNBT(inv, slot); DrillItem.loadSlotsFromNBT(inv, slot);
for (int k = 0; k < inv.getSlots(); k++) { for (int k = 0; k < inv.getSlots(); k++) {
ItemStack filterSlot = inv.getStackInSlot(k); ItemStack filterSlot = inv.getStackInSlot(k);
if (!filterSlot.isEmpty() && areEqualEnough(filterSlot, stack, mod)) { if (!filterSlot.isEmpty() && areEqualEnough(filterSlot, stack, mod, damage, nbt)) {
return whitelist; return whitelist;
} }
} }
@ -75,7 +83,7 @@ public class FilterSettings {
} }
} }
} }
else if (areEqualEnough(slot, stack, mod)) { else if (areEqualEnough(slot, stack, mod, damage, nbt)) {
return whitelist; return whitelist;
} }
} }
@ -84,20 +92,31 @@ public class FilterSettings {
return !whitelist; return !whitelist;
} }
private static boolean areEqualEnough(ItemStack first, ItemStack second, boolean mod) { private static boolean areEqualEnough(ItemStack first, ItemStack second, boolean mod, boolean damage, boolean nbt) {
Item firstItem = first.getItem(); if (mod && checkMod(first, second) && checkDamage(first, second, damage)) {
Item secondItem = second.getItem();
if (mod && BuiltInRegistries.ITEM.getKey(firstItem).getNamespace().equals(BuiltInRegistries.ITEM.getKey(secondItem).getNamespace())) {
return true; return true;
} }
return checkItem(first, second, nbt) && checkDamage(first, second, damage);
}
return firstItem == secondItem; public static boolean checkDamage(ItemStack first, ItemStack second, boolean damage) {
return !damage || first.getDamageValue() == second.getDamageValue();
}
public static boolean checkItem(ItemStack first, ItemStack second, boolean nbt) {
return nbt? ItemStack.isSameItemSameTags(first, second) : ItemStack.isSameItem(first, second);
}
public static boolean checkMod(ItemStack first, ItemStack second) {
return BuiltInRegistries.ITEM.getKey(first.getItem()).getNamespace().equals(BuiltInRegistries.ITEM.getKey(second.getItem()).getNamespace());
} }
public void writeToNBT(CompoundTag tag, String name) { public void writeToNBT(CompoundTag tag, String name) {
CompoundTag compound = new CompoundTag(); CompoundTag compound = new CompoundTag();
compound.putBoolean("Whitelist", this.isWhitelist); compound.putBoolean("Whitelist", this.isWhitelist);
compound.putBoolean("Mod", this.respectMod); compound.putBoolean("Mod", this.respectMod);
compound.putBoolean("Damage", this.matchDamage);
compound.putBoolean("NBT", this.matchNBT);
compound.put("Items", filterInventory.serializeNBT()); compound.put("Items", filterInventory.serializeNBT());
tag.put(name, compound); tag.put(name, compound);
} }
@ -106,16 +125,20 @@ public class FilterSettings {
CompoundTag compound = tag.getCompound(name); CompoundTag compound = tag.getCompound(name);
this.isWhitelist = compound.getBoolean("Whitelist"); this.isWhitelist = compound.getBoolean("Whitelist");
this.respectMod = compound.getBoolean("Mod"); this.respectMod = compound.getBoolean("Mod");
this.matchDamage = compound.getBoolean("Damage");
this.matchNBT = compound.getBoolean("NBT");
this.filterInventory.deserializeNBT(compound.getCompound("Items")); this.filterInventory.deserializeNBT(compound.getCompound("Items"));
} }
public boolean needsUpdateSend() { public boolean needsUpdateSend() {
return this.lastWhitelist != this.isWhitelist || this.lastRespectMod != this.respectMod; return this.lastWhitelist != this.isWhitelist || this.lastRespectMod != this.respectMod || this.lastMatchDamage != this.matchDamage || this.lastMatchNBT != this.matchNBT;
} }
public void updateLasts() { public void updateLasts() {
this.lastWhitelist = this.isWhitelist; this.lastWhitelist = this.isWhitelist;
this.lastRespectMod = this.respectMod; this.lastRespectMod = this.respectMod;
this.lastMatchDamage = this.matchDamage;
this.lastMatchNBT = this.matchNBT;
} }
public void onButtonPressed(int id) { public void onButtonPressed(int id) {
@ -123,11 +146,15 @@ public class FilterSettings {
this.isWhitelist = !this.isWhitelist; this.isWhitelist = !this.isWhitelist;
} else if (id == Buttons.MOD.ordinal()) { } else if (id == Buttons.MOD.ordinal()) {
this.respectMod = !this.respectMod; this.respectMod = !this.respectMod;
} else if (id == Buttons.DAMAGE.ordinal()) {
this.matchDamage = !this.matchDamage;
} else if (id == Buttons.NBT.ordinal()) {
this.matchNBT = !this.matchNBT;
} }
} }
public boolean check(ItemStack stack) { public boolean check(ItemStack stack) {
return !this.needsCheck() || check(stack, this.filterInventory, this.isWhitelist, this.respectMod); return !this.needsCheck() || check(stack, this.filterInventory, this.isWhitelist, this.respectMod, this.matchDamage, this.matchNBT);
} }
public boolean needsCheck() { public boolean needsCheck() {

View file

@ -35,8 +35,8 @@ import java.util.Optional;
public class TileEntityLaserRelayItemAdvanced extends TileEntityLaserRelayItem implements IButtonReactor, MenuProvider { public class TileEntityLaserRelayItemAdvanced extends TileEntityLaserRelayItem implements IButtonReactor, MenuProvider {
public FilterSettings leftFilter = new FilterSettings(12, true, false); public FilterSettings leftFilter = new FilterSettings(12, true, false, false, false);
public FilterSettings rightFilter = new FilterSettings(12, true, false); public FilterSettings rightFilter = new FilterSettings(12, true, false, false, false);
public TileEntityLaserRelayItemAdvanced(BlockPos pos, BlockState state) { public TileEntityLaserRelayItemAdvanced(BlockPos pos, BlockState state) {
super(ActuallyBlocks.LASER_RELAY_ITEM_ADVANCED.getTileEntityType(), pos, state); super(ActuallyBlocks.LASER_RELAY_ITEM_ADVANCED.getTileEntityType(), pos, state);
@ -118,7 +118,7 @@ public class TileEntityLaserRelayItemAdvanced extends TileEntityLaserRelayItem i
ItemStack copy = stack.copy(); ItemStack copy = stack.copy();
copy.setCount(1); copy.setCount(1);
if (!FilterSettings.check(copy, usedSettings.filterInventory, true, usedSettings.respectMod)) { if (!FilterSettings.check(copy, usedSettings.filterInventory, true, usedSettings.respectMod, usedSettings.matchDamage, usedSettings.matchNBT)) {
for (int k = 0; k < usedSettings.filterInventory.getSlots(); k++) { for (int k = 0; k < usedSettings.filterInventory.getSlots(); k++) {
ItemStack slot = usedSettings.filterInventory.getStackInSlot(k); ItemStack slot = usedSettings.filterInventory.getStackInSlot(k);
if (!slot.isEmpty()) { if (!slot.isEmpty()) {

View file

@ -39,7 +39,7 @@ import java.util.List;
public class TileEntityRangedCollector extends TileEntityInventoryBase implements IButtonReactor, MenuProvider { public class TileEntityRangedCollector extends TileEntityInventoryBase implements IButtonReactor, MenuProvider {
public static final int RANGE = 6; public static final int RANGE = 6;
public FilterSettings filter = new FilterSettings(12, true, false); public FilterSettings filter = new FilterSettings(12, true, false, false, false);
public TileEntityRangedCollector(BlockPos pos, BlockState state) { public TileEntityRangedCollector(BlockPos pos, BlockState state) {
super(ActuallyBlocks.RANGED_COLLECTOR.getTileEntityType(), pos, state, 6); super(ActuallyBlocks.RANGED_COLLECTOR.getTileEntityType(), pos, state, 6);

View file

@ -466,18 +466,13 @@
"info.actuallyadditions.gui.whitelist": "Whitelist", "info.actuallyadditions.gui.whitelist": "Whitelist",
"info.actuallyadditions.gui.blacklist": "Blacklist", "info.actuallyadditions.gui.blacklist": "Blacklist",
"info.actuallyadditions.gui.whitelistInfo": "To let all items through, an empty blacklist can be used, to let no items through, an empty whitelist can be used. To configure certain items, place them or configured Item Filters in the slots.", "info.actuallyadditions.gui.whitelistInfo": "To let all items through, an empty blacklist can be used, to let no items through, an empty whitelist can be used. To configure certain items, place them or configured Item Filters in the slots.",
"info.actuallyadditions.gui.respectMeta": "Respecting Metadata", "info.actuallyadditions.gui.respectDamage": "Respecting Damage",
"info.actuallyadditions.gui.ignoreMeta": "Ignoring Metadata", "info.actuallyadditions.gui.ignoreDamage": "Ignoring Damage",
"info.actuallyadditions.gui.respectNBT": "Respecting NBT", "info.actuallyadditions.gui.respectNBT": "Respecting NBT Data",
"info.actuallyadditions.gui.ignoreNBT": "Ignoring NBT", "info.actuallyadditions.gui.ignoreNBT": "Ignoring NBT Data",
"info.actuallyadditions.gui.ignoreMod": "Mod Mode Off", "info.actuallyadditions.gui.ignoreMod": "Mod Mode Off",
"info.actuallyadditions.gui.respectMod": "Mod Mode On", "info.actuallyadditions.gui.respectMod": "Mod Mode On",
"info.actuallyadditions.gui.respectModInfo": "If this is enabled, the filter will compare the mods items come from §cinstead of comparing the items themselves§r. This can be useful for storage systems with mod-based chests. Can also be combined with the other options, but that normally isn't very useful.", "info.actuallyadditions.gui.respectModInfo": "If this is enabled, the filter will compare the mods items come from §cinstead of comparing the items themselves§r. This can be useful for storage systems with mod-based chests. Can also be combined with the other options, but that normally isn't very useful.",
"info.actuallyadditions.gui.ignoreOredict": "Ignoring OreDictionary",
"info.actuallyadditions.gui.respectOredictSoft": "Soft Respecting OreDictionary",
"info.actuallyadditions.gui.respectOredictHard": "Hard Respecting OreDictionary",
"info.actuallyadditions.gui.respectOredictInfo.one": "The item being passed only has to contain §2only one§r of the OreDictionary tags of the item in the filter.",
"info.actuallyadditions.gui.respectOredictInfo.all": "The item being passed has to contain §2all§r of the OreDictionary tags of the item in the filter.",
"info.actuallyadditions.gui.autosplititems.on": "Auto-Split Items On", "info.actuallyadditions.gui.autosplititems.on": "Auto-Split Items On",
"info.actuallyadditions.gui.autosplititems.off": "Auto-Split Items Off", "info.actuallyadditions.gui.autosplititems.off": "Auto-Split Items Off",
"info.actuallyadditions.gui.inbound": "INBOUND", "info.actuallyadditions.gui.inbound": "INBOUND",