From 6c500583d5985c2165c458e99af8831510784413 Mon Sep 17 00:00:00 2001 From: Cassian Date: Thu, 15 Oct 2026 19:00:27 -0400 Subject: [PATCH 1/5] feat: exact component match on item acceptors --- .../block/entity/ItemAcceptorBlockEntity.java | 57 ++++++++++++++----- .../ingame/ItemAcceptorBlockEditScreen.java | 10 ++-- .../packet/C2SEditItemAcceptorBlock.java | 4 +- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java b/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java index 6b423651..c1857b47 100644 --- a/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java +++ b/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java @@ -1,12 +1,16 @@ package dev.hephaestus.glowcase.block.entity; +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.serialization.Codec; import dev.hephaestus.glowcase.Glowcase; -import java.util.HashMap; + import java.util.List; import java.util.Locale; -import java.util.Map; + +import net.minecraft.commands.arguments.item.ItemParser; import net.minecraft.core.BlockPos; +import net.minecraft.core.RegistryAccess; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; import net.minecraft.resources.Identifier; @@ -20,7 +24,7 @@ import net.minecraft.world.level.storage.ValueOutput; public class ItemAcceptorBlockEntity extends GlowcaseBlockEntity { - private Identifier item = Identifier.withDefaultNamespace("air"); + private String item = "minecraft:air"; public int count = 1; public int pulse = 4; public OutputDirection outputDirection = OutputDirection.BACK; @@ -35,7 +39,7 @@ public ItemAcceptorBlockEntity(BlockPos pos, BlockState state) { protected void saveAdditional(ValueOutput view) { super.saveAdditional(view); - view.store("item", Identifier.CODEC, this.item); + view.store("item", Codec.STRING, this.item); view.putInt("count", this.count); view.putInt("pulse", this.pulse); view.putBoolean("is_item_tag", this.isItemTag); @@ -46,26 +50,32 @@ protected void saveAdditional(ValueOutput view) { protected void loadAdditional(ValueInput view) { super.loadAdditional(view); - this.setItem(view.read("item", Identifier.CODEC).orElse(Identifier.withDefaultNamespace("air"))); + this.setItem(view.read("item", Codec.STRING).orElse("minecraft:air")); this.count = view.getIntOr("count", 1); this.pulse = view.getIntOr("pulse", 4); this.isItemTag = view.getBooleanOr("is_item_tag", false); this.outputDirection = view.read("output_direction", OutputDirection.CODEC).orElse(OutputDirection.BACK); } - public Identifier getItem() { + public String getItem() { return item; } - public void setItem(Identifier item) { + public void setItem(String item) { if (item == null) { return; } this.item = item; - TagKey itemTag = TagKey.create(Registries.ITEM, item); - itemTagList = BuiltInRegistries.ITEM.stream().filter(it -> it.getDefaultInstance().is(itemTag)).toList(); + if (Identifier.tryParse(item) != null) { + TagKey itemTag = TagKey.create(Registries.ITEM, Identifier.parse(item)); + itemTagList = BuiltInRegistries.ITEM.stream().filter(it -> it.getDefaultInstance().is(itemTag)).toList(); + } + } + + public static boolean isItem(String text, RegistryAccess registryAccess) { + return Identifier.tryParse(text) != null || getItemStack(text, registryAccess) != null; } public ItemStack getDisplayItemStack() { @@ -76,16 +86,33 @@ public ItemStack getDisplayItemStack() { return itemTagList.get((int) (Util.getMillis() / 1000f) % itemTagList.size()).getDefaultInstance(); } else { - return BuiltInRegistries.ITEM.getValue(item).getDefaultInstance(); + return getItemStack(item, level.registryAccess()); } } - public boolean isItemAccepted(ItemStack stack) { - boolean isEqual = isItemTag - ? stack.is(TagKey.create(Registries.ITEM, item)) - : stack.is(BuiltInRegistries.ITEM.getValue(item)); + private static ItemStack getItemStack(String text, RegistryAccess registryAccess) { + if (!Identifier.isValidPath(text)) { + try { + return new ItemParser(registryAccess).parse(new StringReader(text.toString())).createItemStack(1); + } catch (CommandSyntaxException e) { + return ItemStack.EMPTY; + } + } else if (Identifier.tryParse(text) != null) { + return BuiltInRegistries.ITEM.getValue(Identifier.parse(text)).getDefaultInstance(); + } + return ItemStack.EMPTY; + } - return isEqual && stack.getCount() >= count; + public boolean isItemAccepted(ItemStack stack) { + if (!Identifier.isValidPath(item)) { + return ItemStack.isSameItemSameComponents(stack, getDisplayItemStack()) && stack.getCount() >= count; + } else { + Identifier parse = Identifier.parse(item); + boolean isEqual = isItemTag + ? stack.is(TagKey.create(Registries.ITEM, parse)) + : stack.is(BuiltInRegistries.ITEM.getValue(parse)); + return isEqual && stack.getCount() >= count; + } } public int getPulse() { diff --git a/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemAcceptorBlockEditScreen.java b/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemAcceptorBlockEditScreen.java index 3b5cd0de..f9361247 100644 --- a/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemAcceptorBlockEditScreen.java +++ b/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemAcceptorBlockEditScreen.java @@ -26,11 +26,11 @@ public ItemAcceptorBlockEditScreen(ItemAcceptorBlockEntity blockEntity) { public void init() { super.init(); - Identifier item = this.blockEntity.getItem(); + String item = this.blockEntity.getItem(); this.itemWidget = new GlowcaseEditBox(this.font, width / 2 - 100, height / 2 - 25, 150, 20, Component.empty()); this.itemWidget.setMaxLength(128); - if (!item.equals(Identifier.withDefaultNamespace("air"))) { + if (!item.equals(Identifier.withDefaultNamespace("air").toString())) { this.itemWidget.setValue((this.blockEntity.isItemTag ? "#" : "") + item); } this.itemWidget.setHint(TextUtils.placeholder("gui.glowcase.item_or_tag")); @@ -83,11 +83,11 @@ public CustomPacketPayload getUpdatePayload() { text = text.substring(1); } - if (!text.isEmpty() && Identifier.tryParse(text) instanceof Identifier id) { - this.blockEntity.setItem(id); + if (!text.isEmpty() && ItemAcceptorBlockEntity.isItem(text, this.blockEntity.getLevel().registryAccess())) { + this.blockEntity.setItem(text); this.blockEntity.isItemTag = isItemTag; } else { - this.blockEntity.setItem(Identifier.withDefaultNamespace("air")); + this.blockEntity.setItem("minecraft:air"); } if (Ints.tryParse(countWidget.getValue()) instanceof Integer integer) { diff --git a/src/main/java/dev/hephaestus/glowcase/packet/C2SEditItemAcceptorBlock.java b/src/main/java/dev/hephaestus/glowcase/packet/C2SEditItemAcceptorBlock.java index 284425f8..7bf2d96d 100644 --- a/src/main/java/dev/hephaestus/glowcase/packet/C2SEditItemAcceptorBlock.java +++ b/src/main/java/dev/hephaestus/glowcase/packet/C2SEditItemAcceptorBlock.java @@ -11,11 +11,11 @@ import net.minecraft.server.level.ServerLevel; import net.minecraft.world.level.block.entity.BlockEntity; -public record C2SEditItemAcceptorBlock(BlockPos pos, Identifier item, int count, int pulse, boolean isItemTag, ItemAcceptorBlockEntity.OutputDirection outputDirection) implements C2SEditBlockEntity { +public record C2SEditItemAcceptorBlock(BlockPos pos, String item, int count, int pulse, boolean isItemTag, ItemAcceptorBlockEntity.OutputDirection outputDirection) implements C2SEditBlockEntity { public static final Type ID = new Type<>(Glowcase.id("channel.item_acceptor.save")); public static final StreamCodec PACKET_CODEC = StreamCodec.composite( BlockPos.STREAM_CODEC, C2SEditItemAcceptorBlock::pos, - Identifier.STREAM_CODEC, C2SEditItemAcceptorBlock::item, + ByteBufCodecs.STRING_UTF8, C2SEditItemAcceptorBlock::item, ByteBufCodecs.INT, C2SEditItemAcceptorBlock::count, ByteBufCodecs.INT, C2SEditItemAcceptorBlock::pulse, ByteBufCodecs.BOOL, C2SEditItemAcceptorBlock::isItemTag, From 852b351945c7194bee0b7bd348010d921df68810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ampflower=20=F0=9F=8C=BA?= Date: Sat, 17 Oct 2026 07:18:19 -0700 Subject: [PATCH 2/5] refactor: rewrite and simplify control flow --- .../block/entity/ItemAcceptorBlockEntity.java | 38 +++++++++++++------ .../ingame/ItemAcceptorBlockEditScreen.java | 3 +- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java b/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java index c1857b47..a3bf8bec 100644 --- a/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java +++ b/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java @@ -4,10 +4,6 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.serialization.Codec; import dev.hephaestus.glowcase.Glowcase; - -import java.util.List; -import java.util.Locale; - import net.minecraft.commands.arguments.item.ItemParser; import net.minecraft.core.BlockPos; import net.minecraft.core.RegistryAccess; @@ -23,6 +19,9 @@ import net.minecraft.world.level.storage.ValueInput; import net.minecraft.world.level.storage.ValueOutput; +import java.util.List; +import java.util.Locale; + public class ItemAcceptorBlockEntity extends GlowcaseBlockEntity { private String item = "minecraft:air"; public int count = 1; @@ -90,16 +89,31 @@ public ItemStack getDisplayItemStack() { } } + public static boolean isInvalid(final String id) { + return id == null || id.isBlank() || id.startsWith("..:") || id.equals("air") || id.equals("minecraft:air"); + } + private static ItemStack getItemStack(String text, RegistryAccess registryAccess) { - if (!Identifier.isValidPath(text)) { - try { - return new ItemParser(registryAccess).parse(new StringReader(text.toString())).createItemStack(1); - } catch (CommandSyntaxException e) { - return ItemStack.EMPTY; - } - } else if (Identifier.tryParse(text) != null) { - return BuiltInRegistries.ITEM.getValue(Identifier.parse(text)).getDefaultInstance(); + if (isInvalid(text)) { + return ItemStack.EMPTY; + } + + final Identifier parsed = Identifier.tryParse(text); + + if (parsed != null) { + return BuiltInRegistries.ITEM.getValue(parsed).getDefaultInstance(); + } + + // Admittedly, I might say this should be implemented with a predicate. + // It's, not wrong, but I'd check /clear too. + try { + return new ItemParser(registryAccess) + .parse(new StringReader(text)) + .createItemStack(1); + } catch (CommandSyntaxException ignored) { + // Optionally log it, or flag an error condition? } + return ItemStack.EMPTY; } diff --git a/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemAcceptorBlockEditScreen.java b/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemAcceptorBlockEditScreen.java index f9361247..b46f593d 100644 --- a/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemAcceptorBlockEditScreen.java +++ b/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemAcceptorBlockEditScreen.java @@ -10,7 +10,6 @@ import net.minecraft.client.gui.components.StringWidget; import net.minecraft.network.chat.Component; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; -import net.minecraft.resources.Identifier; public class ItemAcceptorBlockEditScreen extends BlockEditorScreen { private GlowcaseEditBox itemWidget; @@ -30,7 +29,7 @@ public void init() { this.itemWidget = new GlowcaseEditBox(this.font, width / 2 - 100, height / 2 - 25, 150, 20, Component.empty()); this.itemWidget.setMaxLength(128); - if (!item.equals(Identifier.withDefaultNamespace("air").toString())) { + if (!ItemAcceptorBlockEntity.isInvalid(item)) { this.itemWidget.setValue((this.blockEntity.isItemTag ? "#" : "") + item); } this.itemWidget.setHint(TextUtils.placeholder("gui.glowcase.item_or_tag")); From f773e12471f6a746db1ca090ac5645ccaa2ab35e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ampflower=20=F0=9F=8C=BA?= Date: Sat, 17 Oct 2026 13:35:24 -0700 Subject: [PATCH 3/5] fix: strictly validate whether item exists, check not-empty --- .../glowcase/block/entity/ItemAcceptorBlockEntity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java b/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java index a3bf8bec..8b5f7df3 100644 --- a/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java +++ b/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java @@ -74,7 +74,7 @@ public void setItem(String item) { } public static boolean isItem(String text, RegistryAccess registryAccess) { - return Identifier.tryParse(text) != null || getItemStack(text, registryAccess) != null; + return !getItemStack(text, registryAccess).isEmpty(); } public ItemStack getDisplayItemStack() { From 51ac50917019c11d4211638b960e4ede67a995e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ampflower=20=F0=9F=8C=BA?= Date: Sat, 17 Oct 2026 14:02:03 -0700 Subject: [PATCH 4/5] fix: tags once again work correctly --- .../block/entity/ItemAcceptorBlockEntity.java | 14 +++++++++++--- .../ingame/ItemAcceptorBlockEditScreen.java | 19 ++++++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java b/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java index 8b5f7df3..b28bf77a 100644 --- a/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java +++ b/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java @@ -67,10 +67,18 @@ public void setItem(String item) { this.item = item; - if (Identifier.tryParse(item) != null) { - TagKey itemTag = TagKey.create(Registries.ITEM, Identifier.parse(item)); - itemTagList = BuiltInRegistries.ITEM.stream().filter(it -> it.getDefaultInstance().is(itemTag)).toList(); + if (!this.isItemTag) { + return; + } + + final Identifier itemTagId = Identifier.tryParse(item); + + if (itemTagId == null) { + return; } + + TagKey itemTag = TagKey.create(Registries.ITEM, itemTagId); + itemTagList = BuiltInRegistries.ITEM.stream().filter(it -> it.getDefaultInstance().is(itemTag)).toList(); } public static boolean isItem(String text, RegistryAccess registryAccess) { diff --git a/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemAcceptorBlockEditScreen.java b/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemAcceptorBlockEditScreen.java index b46f593d..cfab1ed0 100644 --- a/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemAcceptorBlockEditScreen.java +++ b/src/main/java/dev/hephaestus/glowcase/client/gui/screen/ingame/ItemAcceptorBlockEditScreen.java @@ -10,6 +10,7 @@ import net.minecraft.client.gui.components.StringWidget; import net.minecraft.network.chat.Component; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.Identifier; public class ItemAcceptorBlockEditScreen extends BlockEditorScreen { private GlowcaseEditBox itemWidget; @@ -77,16 +78,24 @@ public void init() { @Override public CustomPacketPayload getUpdatePayload() { String text = itemWidget.getValue(); - boolean isItemTag = text.startsWith("#"); - if (isItemTag) { + + if (text.startsWith("#")) { text = text.substring(1); - } - if (!text.isEmpty() && ItemAcceptorBlockEntity.isItem(text, this.blockEntity.getLevel().registryAccess())) { + if (Identifier.tryParse(text) != null) { + this.blockEntity.setItem(text); + this.blockEntity.isItemTag = true; + } else { + this.blockEntity.setItem("minecraft:air"); + this.blockEntity.isItemTag = false; + } + + } else if (ItemAcceptorBlockEntity.isItem(text, this.blockEntity.getLevel().registryAccess())) { this.blockEntity.setItem(text); - this.blockEntity.isItemTag = isItemTag; + this.blockEntity.isItemTag = false; } else { this.blockEntity.setItem("minecraft:air"); + this.blockEntity.isItemTag = false; } if (Ints.tryParse(countWidget.getValue()) instanceof Integer integer) { From dc33c65cbface05d3c74948ee4aa4ab4d5d6aa2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ampflower=20=F0=9F=8C=BA?= Date: Sat, 17 Oct 2026 14:08:15 -0700 Subject: [PATCH 5/5] fix: setItem must be called after isItemTag --- .../glowcase/block/entity/ItemAcceptorBlockEntity.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java b/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java index b28bf77a..f866749c 100644 --- a/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java +++ b/src/main/java/dev/hephaestus/glowcase/block/entity/ItemAcceptorBlockEntity.java @@ -38,10 +38,10 @@ public ItemAcceptorBlockEntity(BlockPos pos, BlockState state) { protected void saveAdditional(ValueOutput view) { super.saveAdditional(view); - view.store("item", Codec.STRING, this.item); view.putInt("count", this.count); view.putInt("pulse", this.pulse); view.putBoolean("is_item_tag", this.isItemTag); + view.store("item", Codec.STRING, this.item); view.store("output_direction", OutputDirection.CODEC, this.outputDirection); } @@ -49,10 +49,10 @@ protected void saveAdditional(ValueOutput view) { protected void loadAdditional(ValueInput view) { super.loadAdditional(view); - this.setItem(view.read("item", Codec.STRING).orElse("minecraft:air")); this.count = view.getIntOr("count", 1); this.pulse = view.getIntOr("pulse", 4); this.isItemTag = view.getBooleanOr("is_item_tag", false); + this.setItem(view.read("item", Codec.STRING).orElse("minecraft:air")); this.outputDirection = view.read("output_direction", OutputDirection.CODEC).orElse(OutputDirection.BACK); }