From 6e2681a22c991f4653f579dbfe088f6295a18d34 Mon Sep 17 00:00:00 2001 From: joe Date: Fri, 18 Oct 2024 13:26:57 +0100 Subject: [PATCH] Don't rely on `in_enchanting_table` tag below 1.21. Fixes #665 --- .../clientcommands/command/CEnchantCommand.java | 12 +++++++++++- .../clientcommands/features/LegacyEnchantment.java | 9 +++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/earthcomputer/clientcommands/command/CEnchantCommand.java b/src/main/java/net/earthcomputer/clientcommands/command/CEnchantCommand.java index 1ef484ad4..3c427e821 100644 --- a/src/main/java/net/earthcomputer/clientcommands/command/CEnchantCommand.java +++ b/src/main/java/net/earthcomputer/clientcommands/command/CEnchantCommand.java @@ -5,9 +5,12 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.earthcomputer.clientcommands.Configs; import net.earthcomputer.clientcommands.features.EnchantmentCracker; +import net.earthcomputer.clientcommands.features.LegacyEnchantment; import net.earthcomputer.clientcommands.features.PlayerRandCracker; +import net.earthcomputer.clientcommands.util.MultiVersionCompat; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.minecraft.ChatFormatting; +import net.minecraft.Optionull; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.commands.CommandBuildContext; @@ -41,7 +44,14 @@ public static void register(CommandDispatcher dispatc } private static boolean enchantmentPredicate(Item item, Holder ench) { - return ench.is(EnchantmentTags.IN_ENCHANTING_TABLE) && (item == Items.BOOK || ench.value().canEnchant(new ItemStack(item))); + boolean inEnchantingTable; + if (MultiVersionCompat.INSTANCE.getProtocolVersion() < MultiVersionCompat.V1_21) { + LegacyEnchantment legacyEnch = LegacyEnchantment.byEnchantmentKey(ench.unwrapKey().orElseThrow()); + inEnchantingTable = legacyEnch != null && legacyEnch.inEnchantmentTable(); + } else { + inEnchantingTable = ench.is(EnchantmentTags.IN_ENCHANTING_TABLE); + } + return inEnchantingTable && (item == Items.BOOK || ench.value().canEnchant(new ItemStack(item))); } private static int cenchant(FabricClientCommandSource source, ItemAndEnchantmentsPredicate itemAndEnchantmentsPredicate) throws CommandSyntaxException { diff --git a/src/main/java/net/earthcomputer/clientcommands/features/LegacyEnchantment.java b/src/main/java/net/earthcomputer/clientcommands/features/LegacyEnchantment.java index 0c3f40b97..7d9c447dc 100644 --- a/src/main/java/net/earthcomputer/clientcommands/features/LegacyEnchantment.java +++ b/src/main/java/net/earthcomputer/clientcommands/features/LegacyEnchantment.java @@ -134,6 +134,11 @@ public enum LegacyEnchantment { this.flags = combinedFlags; } + @Nullable + public static LegacyEnchantment byEnchantmentKey(ResourceKey enchantmentKey) { + return BY_KEY.get(enchantmentKey); + } + public boolean isTreasure() { return (flags & Flags.TREASURE) != 0; } @@ -142,6 +147,10 @@ public boolean isDiscoverable() { return (flags & Flags.NON_DISCOVERABLE) == 0; } + public boolean inEnchantmentTable() { + return !isTreasure() && isDiscoverable(); + } + public boolean isCompatible(LegacyEnchantment other, int version) { ExclusiveSet set = EXCLUSIVE_SETS.get(this); if (set == null) {