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.
This commit is contained in:
FlorianMichael 2024-05-20 00:17:05 +02:00
parent 90dfd7d4a2
commit 458b4daf15
No known key found for this signature in database
GPG Key ID: C2FB87E71C425126
2 changed files with 84 additions and 2 deletions

View File

@ -0,0 +1,82 @@
/*
* This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus
* Copyright (C) 2021-2024 FlorianMichael/EnZaXD <florian.michael07@gmail.com> 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 <http://www.gnu.org/licenses/>.
*/
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<String, Enchantment> 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<Enchantment> getOrEmpty(final @Nullable Identifier identifier) {
if (identifier == null) {
return Optional.empty();
}
return Optional.ofNullable(ENCHANTMENT_REGISTRY.get(identifier.getPath()));
}
}

View File

@ -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.item.TooltipAppender;
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 @@ public abstract class MixinItemStack {
final NbtCompound enchantment = (NbtCompound) element;
final String id = enchantment.getString("id");
final Optional<Enchantment> value = Registries.ENCHANTMENT.getOrEmpty(Identifier.tryParse(id));
final Optional<Enchantment> 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)));