#1393: Improve field rename handling and centralize conversion between bukkit and string more

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2024-05-04 08:19:07 +10:00
parent f91094ddfd
commit acdb83379e
17 changed files with 313 additions and 90 deletions

View File

@@ -1,5 +1,7 @@
package org.bukkit.craftbukkit.enchantments;
import com.google.common.base.Preconditions;
import java.util.Locale;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
@@ -9,6 +11,8 @@ import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.legacy.FieldRename;
import org.bukkit.craftbukkit.util.ApiVersion;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.enchantments.EnchantmentTarget;
@@ -29,6 +33,25 @@ public class CraftEnchantment extends Enchantment implements Handleable<net.mine
return CraftRegistry.bukkitToMinecraft(bukkit);
}
public static String bukkitToString(Enchantment bukkit) {
Preconditions.checkArgument(bukkit != null);
return bukkit.getKey().toString();
}
public static Enchantment stringToBukkit(String string) {
Preconditions.checkArgument(string != null);
// We currently do not have any version-dependent remapping, so we can use current version
// First convert from when only the names where saved
string = FieldRename.convertEnchantmentName(ApiVersion.CURRENT, string);
string = string.toLowerCase(Locale.ROOT);
NamespacedKey key = NamespacedKey.fromString(string);
// Now also convert from when keys where saved
return CraftRegistry.get(Registry.ENCHANTMENT, key, ApiVersion.CURRENT);
}
private final NamespacedKey key;
private final net.minecraft.world.item.enchantment.Enchantment handle;
private final int id;