From 458b4daf15f86cf5afd475dba81116d2223a6220 Mon Sep 17 00:00:00 2001 From: FlorianMichael <60033407+FlorianMichael@users.noreply.github.com> Date: Mon, 20 May 2024 00:17:05 +0200 Subject: [PATCH] Rewrite 1.14.4 enchantment registry properly Adds a 1.14 enchantment name -> 1.20.5 enchantment element registry to validate enchantments and get the correct one even when names change. --- .../fixes/versioned/Enchantments1_14_4.java | 82 +++++++++++++++++++ .../fixes/minecraft/item/MixinItemStack.java | 4 +- 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/florianmichael/viafabricplus/fixes/versioned/Enchantments1_14_4.java diff --git a/src/main/java/de/florianmichael/viafabricplus/fixes/versioned/Enchantments1_14_4.java b/src/main/java/de/florianmichael/viafabricplus/fixes/versioned/Enchantments1_14_4.java new file mode 100644 index 00000000..2ec03503 --- /dev/null +++ b/src/main/java/de/florianmichael/viafabricplus/fixes/versioned/Enchantments1_14_4.java @@ -0,0 +1,82 @@ +/* + * This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus + * Copyright (C) 2021-2024 FlorianMichael/EnZaXD and RK_01/RaphiMC + * Copyright (C) 2023-2024 contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package de.florianmichael.viafabricplus.fixes.versioned; + +import net.minecraft.enchantment.Enchantment; +import net.minecraft.enchantment.Enchantments; +import net.minecraft.util.Identifier; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public class Enchantments1_14_4 { + + private static final Map ENCHANTMENT_REGISTRY = new HashMap<>(); + + static { + ENCHANTMENT_REGISTRY.put("protection", Enchantments.PROTECTION); + ENCHANTMENT_REGISTRY.put("fire_protection", Enchantments.FIRE_PROTECTION); + ENCHANTMENT_REGISTRY.put("feather_falling", Enchantments.FEATHER_FALLING); + ENCHANTMENT_REGISTRY.put("blast_protection", Enchantments.BLAST_PROTECTION); + ENCHANTMENT_REGISTRY.put("projectile_protection", Enchantments.PROJECTILE_PROTECTION); + ENCHANTMENT_REGISTRY.put("respiration", Enchantments.RESPIRATION); + ENCHANTMENT_REGISTRY.put("aqua_affinity", Enchantments.AQUA_AFFINITY); + ENCHANTMENT_REGISTRY.put("thorns", Enchantments.THORNS); + ENCHANTMENT_REGISTRY.put("depth_strider", Enchantments.DEPTH_STRIDER); + ENCHANTMENT_REGISTRY.put("frost_walker", Enchantments.FROST_WALKER); + ENCHANTMENT_REGISTRY.put("binding_curse", Enchantments.BINDING_CURSE); + ENCHANTMENT_REGISTRY.put("sharpness", Enchantments.SHARPNESS); + ENCHANTMENT_REGISTRY.put("smite", Enchantments.SMITE); + ENCHANTMENT_REGISTRY.put("bane_of_arthropods", Enchantments.BANE_OF_ARTHROPODS); + ENCHANTMENT_REGISTRY.put("knockback", Enchantments.KNOCKBACK); + ENCHANTMENT_REGISTRY.put("fire_aspect", Enchantments.FIRE_ASPECT); + ENCHANTMENT_REGISTRY.put("looting", Enchantments.LOOTING); + ENCHANTMENT_REGISTRY.put("sweeping", Enchantments.SWEEPING_EDGE); + ENCHANTMENT_REGISTRY.put("efficiency", Enchantments.EFFICIENCY); + ENCHANTMENT_REGISTRY.put("silk_touch", Enchantments.SILK_TOUCH); + ENCHANTMENT_REGISTRY.put("unbreaking", Enchantments.UNBREAKING); + ENCHANTMENT_REGISTRY.put("fortune", Enchantments.FORTUNE); + ENCHANTMENT_REGISTRY.put("power", Enchantments.POWER); + ENCHANTMENT_REGISTRY.put("punch", Enchantments.PUNCH); + ENCHANTMENT_REGISTRY.put("flame", Enchantments.FLAME); + ENCHANTMENT_REGISTRY.put("infinity", Enchantments.INFINITY); + ENCHANTMENT_REGISTRY.put("luck_of_the_sea", Enchantments.LUCK_OF_THE_SEA); + ENCHANTMENT_REGISTRY.put("lure", Enchantments.LURE); + ENCHANTMENT_REGISTRY.put("loyalty", Enchantments.LOYALTY); + ENCHANTMENT_REGISTRY.put("impaling", Enchantments.IMPALING); + ENCHANTMENT_REGISTRY.put("riptide", Enchantments.RIPTIDE); + ENCHANTMENT_REGISTRY.put("channeling", Enchantments.CHANNELING); + ENCHANTMENT_REGISTRY.put("multishot", Enchantments.MULTISHOT); + ENCHANTMENT_REGISTRY.put("quick_charge", Enchantments.QUICK_CHARGE); + ENCHANTMENT_REGISTRY.put("piercing", Enchantments.PIERCING); + ENCHANTMENT_REGISTRY.put("mending", Enchantments.MENDING); + ENCHANTMENT_REGISTRY.put("vanishing_curse", Enchantments.VANISHING_CURSE); + } + + public static Optional getOrEmpty(final @Nullable Identifier identifier) { + if (identifier == null) { + return Optional.empty(); + } + return Optional.ofNullable(ENCHANTMENT_REGISTRY.get(identifier.getPath())); + } + +} diff --git a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java index decc4538..8b8223a8 100644 --- a/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java +++ b/src/main/java/de/florianmichael/viafabricplus/injection/mixin/fixes/minecraft/item/MixinItemStack.java @@ -20,6 +20,7 @@ package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft.item; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; +import de.florianmichael.viafabricplus.fixes.versioned.Enchantments1_14_4; import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator; import de.florianmichael.viafabricplus.util.ItemUtil; import net.minecraft.client.item.TooltipType; @@ -34,7 +35,6 @@ import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtList; -import net.minecraft.registry.Registries; import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.text.Text; import net.minecraft.util.Identifier; @@ -91,7 +91,7 @@ private double removeAttackDamageValueFromCalculation(PlayerEntity instance, Reg final NbtCompound enchantment = (NbtCompound) element; final String id = enchantment.getString("id"); - final Optional value = Registries.ENCHANTMENT.getOrEmpty(Identifier.tryParse(id)); + final Optional value = Enchantments1_14_4.getOrEmpty(Identifier.tryParse(id)); value.ifPresent(e -> { final int lvl = enchantment.getInt("lvl"); tooltip.accept(e.getName(MathHelper.clamp(lvl, Short.MIN_VALUE, Short.MAX_VALUE)));