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

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
Bukkit/Spigot 2024-05-04 08:19:04 +10:00
parent a3c30e3ee2
commit 92293c1b89
5 changed files with 44 additions and 6 deletions

View file

@ -322,6 +322,7 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
static final class SimpleRegistry<T extends Enum<T> & Keyed> implements Registry<T> {
private final Class<T> type;
private final Map<NamespacedKey, T> map;
protected SimpleRegistry(@NotNull Class<T> type) {
@ -338,6 +339,7 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
}
map = builder.build();
this.type = type;
}
@Nullable
@ -357,5 +359,11 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
public Iterator<T> iterator() {
return map.values().iterator();
}
@ApiStatus.Internal
@Deprecated(since = "1.20.6", forRemoval = true)
public Class<T> getType() {
return this.type;
}
}
}

View file

@ -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 extends Keyed> B get(Registry<B> registry, NamespacedKey key);
}

View file

@ -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<String, Object> 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<String, Object> serialize() {
return ImmutableMap.<String, Object>of(
COLOR, color.toString(),
PATTERN, pattern.getIdentifier()
PATTERN, pattern.getKey().toString()
);
}

View file

@ -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());

View file

@ -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);