From 92293c1b89c893e420aa872fe492b7e079d39e05 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Sat, 4 May 2024 08:19:04 +1000 Subject: [PATCH] #1004: Improve field rename handling and centralize conversion between bukkit and string more By: DerFrZocker --- .../src/main/java/org/bukkit/Registry.java | 8 ++++++++ .../src/main/java/org/bukkit/UnsafeValues.java | 8 ++++++++ .../java/org/bukkit/block/banner/Pattern.java | 18 ++++++++++++++++-- .../java/org/bukkit/inventory/ItemStack.java | 9 ++++++++- .../java/org/bukkit/potion/PotionEffect.java | 7 ++++--- 5 files changed, 44 insertions(+), 6 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/Registry.java b/paper-api/src/main/java/org/bukkit/Registry.java index 4f8c54a84c..5dfb6af7d4 100644 --- a/paper-api/src/main/java/org/bukkit/Registry.java +++ b/paper-api/src/main/java/org/bukkit/Registry.java @@ -322,6 +322,7 @@ public interface Registry extends Iterable { static final class SimpleRegistry & Keyed> implements Registry { + private final Class type; private final Map map; protected SimpleRegistry(@NotNull Class type) { @@ -338,6 +339,7 @@ public interface Registry extends Iterable { } map = builder.build(); + this.type = type; } @Nullable @@ -357,5 +359,11 @@ public interface Registry extends Iterable { public Iterator iterator() { return map.values().iterator(); } + + @ApiStatus.Internal + @Deprecated(since = "1.20.6", forRemoval = true) + public Class getType() { + return this.type; + } } } diff --git a/paper-api/src/main/java/org/bukkit/UnsafeValues.java b/paper-api/src/main/java/org/bukkit/UnsafeValues.java index ef3c0410ba..ce9bb54a6e 100644 --- a/paper-api/src/main/java/org/bukkit/UnsafeValues.java +++ b/paper-api/src/main/java/org/bukkit/UnsafeValues.java @@ -105,6 +105,8 @@ public interface UnsafeValues { * @param key of the potion type * @return an internal potion data */ + @ApiStatus.Internal + @Deprecated(since = "1.20.2", forRemoval = true) PotionType.InternalPotionData getInternalPotionData(NamespacedKey key); @ApiStatus.Internal @@ -120,4 +122,10 @@ public interface UnsafeValues { @ApiStatus.Internal @NotNull DamageSource.Builder createDamageSourceBuilder(@NotNull DamageType damageType); + + @ApiStatus.Internal + String get(Class aClass, String value); + + @ApiStatus.Internal + B get(Registry registry, NamespacedKey key); } diff --git a/paper-api/src/main/java/org/bukkit/block/banner/Pattern.java b/paper-api/src/main/java/org/bukkit/block/banner/Pattern.java index 5c293ab0b7..de815683bb 100644 --- a/paper-api/src/main/java/org/bukkit/block/banner/Pattern.java +++ b/paper-api/src/main/java/org/bukkit/block/banner/Pattern.java @@ -1,9 +1,13 @@ package org.bukkit.block.banner; +import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; import java.util.Map; import java.util.NoSuchElementException; +import org.bukkit.Bukkit; import org.bukkit.DyeColor; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.configuration.serialization.SerializableAs; import org.jetbrains.annotations.NotNull; @@ -36,7 +40,17 @@ public class Pattern implements ConfigurationSerializable { */ public Pattern(@NotNull Map map) { color = DyeColor.legacyValueOf(getString(map, COLOR)); - pattern = PatternType.getByIdentifier(getString(map, PATTERN)); + + String value = getString(map, PATTERN); + PatternType patternType = PatternType.getByIdentifier(value); + + if (patternType == null) { + patternType = Bukkit.getUnsafe().get(Registry.BANNER_PATTERN, NamespacedKey.fromString(value)); + } + + Preconditions.checkNotNull(patternType, "Pattern type for key %s cannot be null", value); + + pattern = patternType; } private static String getString(@NotNull Map map, @NotNull Object key) { @@ -52,7 +66,7 @@ public class Pattern implements ConfigurationSerializable { public Map serialize() { return ImmutableMap.of( COLOR, color.toString(), - PATTERN, pattern.getIdentifier() + PATTERN, pattern.getKey().toString() ); } diff --git a/paper-api/src/main/java/org/bukkit/inventory/ItemStack.java b/paper-api/src/main/java/org/bukkit/inventory/ItemStack.java index 257b5db1aa..692f67c4e2 100644 --- a/paper-api/src/main/java/org/bukkit/inventory/ItemStack.java +++ b/paper-api/src/main/java/org/bukkit/inventory/ItemStack.java @@ -3,9 +3,12 @@ package org.bukkit.inventory; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; import java.util.LinkedHashMap; +import java.util.Locale; import java.util.Map; import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; import org.bukkit.Translatable; import org.bukkit.Utility; import org.bukkit.configuration.serialization.ConfigurationSerializable; @@ -530,7 +533,11 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat Map map = (Map) raw; for (Map.Entry entry : map.entrySet()) { - Enchantment enchantment = Enchantment.getByName(entry.getKey().toString()); + String stringKey = entry.getKey().toString(); + stringKey = Bukkit.getUnsafe().get(Enchantment.class, stringKey); + NamespacedKey key = NamespacedKey.fromString(stringKey.toLowerCase(Locale.ROOT)); + + Enchantment enchantment = Bukkit.getUnsafe().get(Registry.ENCHANTMENT, key); if ((enchantment != null) && (entry.getValue() instanceof Integer)) { result.addUnsafeEnchantment(enchantment, (Integer) entry.getValue()); diff --git a/paper-api/src/main/java/org/bukkit/potion/PotionEffect.java b/paper-api/src/main/java/org/bukkit/potion/PotionEffect.java index 88f720aba1..89d49ce1e9 100644 --- a/paper-api/src/main/java/org/bukkit/potion/PotionEffect.java +++ b/paper-api/src/main/java/org/bukkit/potion/PotionEffect.java @@ -4,8 +4,10 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; import java.util.Map; import java.util.NoSuchElementException; +import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.NamespacedKey; +import org.bukkit.Registry; import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.configuration.serialization.SerializableAs; import org.bukkit.entity.LivingEntity; @@ -110,9 +112,8 @@ public class PotionEffect implements ConfigurationSerializable { @NotNull private static PotionEffectType getEffectType(@NotNull Map map) { PotionEffectType effect; - if (map.get(TYPE) instanceof String) { - String type = (String) map.get(TYPE); - effect = PotionEffectType.getByKey(NamespacedKey.fromString(type)); + if (map.get(TYPE) instanceof String value) { + effect = Bukkit.getUnsafe().get(Registry.EFFECT, NamespacedKey.fromString(value)); } else { int type = getInt(map, TYPE); effect = PotionEffectType.getById(type);