Skip to content

Commit

Permalink
Don't rely on in_enchanting_table tag below 1.21. Fixes #665
Browse files Browse the repository at this point in the history
  • Loading branch information
Earthcomputer committed Oct 18, 2024
1 parent 29ab4df commit 6e2681a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,7 +44,14 @@ public static void register(CommandDispatcher<FabricClientCommandSource> dispatc
}

private static boolean enchantmentPredicate(Item item, Holder<Enchantment> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ public enum LegacyEnchantment {
this.flags = combinedFlags;
}

@Nullable
public static LegacyEnchantment byEnchantmentKey(ResourceKey<Enchantment> enchantmentKey) {
return BY_KEY.get(enchantmentKey);
}

public boolean isTreasure() {
return (flags & Flags.TREASURE) != 0;
}
Expand All @@ -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) {
Expand Down

0 comments on commit 6e2681a

Please sign in to comment.