Skip to content

Commit c58f844

Browse files
Doc94Lulu13022002
andauthored
Check for valid NamespacedKey in OldEnum#valueOf (#13596)
Co-authored-by: Lulu13022002 <41980282+Lulu13022002@users.noreply.github.com>
1 parent 75bbf4f commit c58f844

File tree

32 files changed

+202
-164
lines changed

32 files changed

+202
-164
lines changed

paper-api/src/main/java/io/papermc/paper/datacomponent/DataComponentTypes.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@
4848
import io.papermc.paper.registry.tag.TagKey;
4949
import java.util.List;
5050
import net.kyori.adventure.key.Key;
51+
import net.kyori.adventure.key.KeyPattern;
5152
import net.kyori.adventure.text.Component;
5253
import org.bukkit.Art;
5354
import org.bukkit.DyeColor;
5455
import org.bukkit.FireworkEffect;
5556
import org.bukkit.MusicInstrument;
56-
import org.bukkit.NamespacedKey;
5757
import org.bukkit.Registry;
5858
import org.bukkit.block.banner.PatternType;
5959
import org.bukkit.damage.DamageType;
@@ -82,8 +82,6 @@
8282
import org.jetbrains.annotations.ApiStatus;
8383
import org.jspecify.annotations.NullMarked;
8484

85-
import static java.util.Objects.requireNonNull;
86-
8785
/**
8886
* All the different types of data that {@link org.bukkit.inventory.ItemStack ItemStacks}
8987
* and {@link org.bukkit.inventory.ItemType ItemTypes} can have.
@@ -392,22 +390,21 @@ public final class DataComponentTypes {
392390
public static final DataComponentType.Valued<DyeColor> SHEEP_COLOR = valued("sheep/color");
393391
public static final DataComponentType.Valued<DyeColor> SHULKER_COLOR = valued("shulker/color");
394392

395-
private static DataComponentType.NonValued unvalued(final String name) {
396-
final DataComponentType dataComponentType = requireNonNull(Registry.DATA_COMPONENT_TYPE.get(NamespacedKey.minecraft(name)), name + " unvalued data component type couldn't be found, this is a bug.");
393+
private static DataComponentType.NonValued unvalued(final @KeyPattern.Value String key) {
394+
final DataComponentType dataComponentType = Registry.DATA_COMPONENT_TYPE.getOrThrow(Key.key(Key.MINECRAFT_NAMESPACE, key));
397395
if (dataComponentType instanceof DataComponentType.NonValued) {
398396
return (DataComponentType.NonValued) dataComponentType;
399397
}
400-
throw new IllegalStateException(name + " is not a valid unvalued type, it is a " + dataComponentType.getClass().getTypeName());
398+
throw new IllegalStateException(key + " is not a valid unvalued type, it is a " + dataComponentType.getClass().getTypeName());
401399
}
402400

403401
@SuppressWarnings("unchecked")
404-
private static <T> DataComponentType.Valued<T> valued(final String name) {
405-
DataComponentType dataComponentType = requireNonNull(Registry.DATA_COMPONENT_TYPE.get(NamespacedKey.minecraft(name)), name + " valued data component type couldn't be found, this is a bug.");
402+
private static <T> DataComponentType.Valued<T> valued(final @KeyPattern.Value String key) {
403+
final DataComponentType dataComponentType = Registry.DATA_COMPONENT_TYPE.getOrThrow(Key.key(Key.MINECRAFT_NAMESPACE, key));
406404
if (dataComponentType instanceof DataComponentType.Valued) {
407405
return (DataComponentType.Valued<T>) dataComponentType;
408406
}
409-
throw new IllegalStateException(name + " is not a valid valued type, it is a " + dataComponentType.getClass().getTypeName());
410-
407+
throw new IllegalStateException(key + " is not a valid valued type, it is a " + dataComponentType.getClass().getTypeName());
411408
}
412409

413410
private DataComponentTypes() {

paper-api/src/main/java/org/bukkit/Art.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package org.bukkit;
22

33
import com.google.common.base.Preconditions;
4-
import com.google.common.collect.Lists;
54
import io.papermc.paper.registry.RegistryAccess;
6-
import io.papermc.paper.registry.RegistryBuilderFactory;
75
import io.papermc.paper.registry.RegistryKey;
8-
import io.papermc.paper.registry.data.InlinedRegistryBuilderProvider;
9-
import io.papermc.paper.registry.data.PaintingVariantRegistryEntry;
106
import java.util.Locale;
11-
import java.util.function.Consumer;
7+
import net.kyori.adventure.key.Key;
8+
import net.kyori.adventure.key.KeyPattern;
129
import org.bukkit.util.OldEnum;
13-
import org.jetbrains.annotations.ApiStatus;
1410
import org.jetbrains.annotations.NotNull;
1511
import org.jetbrains.annotations.Nullable;
1612

@@ -129,8 +125,8 @@ public interface Art extends OldEnum<Art>, Keyed {
129125
// End generate - Art
130126

131127
@NotNull
132-
private static Art getArt(@NotNull String key) {
133-
return RegistryAccess.registryAccess().getRegistry(RegistryKey.PAINTING_VARIANT).getOrThrow(NamespacedKey.minecraft(key));
128+
private static Art getArt(@NotNull @KeyPattern.Value String key) {
129+
return RegistryAccess.registryAccess().getRegistry(RegistryKey.PAINTING_VARIANT).getOrThrow(Key.key(Key.MINECRAFT_NAMESPACE, key));
134130
}
135131

136132
/**
@@ -209,7 +205,7 @@ private static Art getArt(@NotNull String key) {
209205
@Deprecated(since = "1.6.2", forRemoval = true)
210206
@Nullable
211207
static Art getById(int id) {
212-
for (Art art : Registry.ART) {
208+
for (Art art : RegistryAccess.registryAccess().getRegistry(RegistryKey.PAINTING_VARIANT)) {
213209
if (id == art.getId()) {
214210
return art;
215211
}
@@ -231,8 +227,12 @@ static Art getById(int id) {
231227
@Nullable
232228
static Art getByName(@NotNull String name) {
233229
Preconditions.checkArgument(name != null, "Name cannot be null");
230+
final NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
231+
if (key == null) {
232+
return null;
233+
}
234234

235-
return Bukkit.getUnsafe().get(RegistryKey.PAINTING_VARIANT, NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
235+
return Bukkit.getUnsafe().get(RegistryKey.PAINTING_VARIANT, key);
236236
}
237237

238238
/**
@@ -243,18 +243,19 @@ static Art getByName(@NotNull String name) {
243243
@NotNull
244244
@Deprecated(since = "1.21.3", forRemoval = true) @org.jetbrains.annotations.ApiStatus.ScheduledForRemoval(inVersion = "1.22") // Paper - will be removed via asm-utils
245245
static Art valueOf(@NotNull String name) {
246-
Art art = Bukkit.getUnsafe().get(RegistryKey.PAINTING_VARIANT, NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
246+
final NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
247+
final Art art = key == null ? null : Bukkit.getUnsafe().get(RegistryKey.PAINTING_VARIANT, key);
247248
Preconditions.checkArgument(art != null, "No art found with the name %s", name);
248249
return art;
249250
}
250251

251252
/**
252253
* @return an array of all known arts.
253-
* @deprecated use {@link Registry#iterator()}.
254+
* @deprecated use {@link Registry#stream()}.
254255
*/
255256
@NotNull
256257
@Deprecated(since = "1.21.3", forRemoval = true) @org.jetbrains.annotations.ApiStatus.ScheduledForRemoval(inVersion = "1.22") // Paper - will be removed via asm-utils
257258
static Art[] values() {
258-
return Lists.newArrayList(Registry.ART).toArray(new Art[0]);
259+
return RegistryAccess.registryAccess().getRegistry(RegistryKey.PAINTING_VARIANT).stream().toArray(Art[]::new);
259260
}
260261
}

paper-api/src/main/java/org/bukkit/Fluid.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package org.bukkit;
22

33
import com.google.common.base.Preconditions;
4-
import com.google.common.collect.Lists;
54
import io.papermc.paper.registry.RegistryKey;
65
import java.util.Locale;
6+
import net.kyori.adventure.key.Key;
7+
import net.kyori.adventure.key.KeyPattern;
78
import org.bukkit.util.OldEnum;
89
import org.jetbrains.annotations.NotNull;
910

@@ -25,8 +26,8 @@ public interface Fluid extends OldEnum<Fluid>, Keyed {
2526
// End generate - Fluid
2627

2728
@NotNull
28-
private static Fluid getFluid(@NotNull String key) {
29-
return Registry.FLUID.getOrThrow(NamespacedKey.minecraft(key));
29+
private static Fluid getFluid(@NotNull @KeyPattern.Value String key) {
30+
return Registry.FLUID.getOrThrow(Key.key(Key.MINECRAFT_NAMESPACE, key));
3031
}
3132

3233
/**
@@ -37,18 +38,19 @@ private static Fluid getFluid(@NotNull String key) {
3738
@NotNull
3839
@Deprecated(since = "1.21.3", forRemoval = true) @org.jetbrains.annotations.ApiStatus.ScheduledForRemoval(inVersion = "1.22") // Paper - will be removed via asm-utils
3940
static Fluid valueOf(@NotNull String name) {
40-
Fluid fluid = Bukkit.getUnsafe().get(RegistryKey.FLUID, NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
41+
final NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
42+
Fluid fluid = key == null ? null : Bukkit.getUnsafe().get(RegistryKey.FLUID, key);
4143
Preconditions.checkArgument(fluid != null, "No fluid found with the name %s", name);
4244
return fluid;
4345
}
4446

4547
/**
4648
* @return an array of all known fluids.
47-
* @deprecated use {@link Registry#iterator()}.
49+
* @deprecated use {@link Registry#stream()}.
4850
*/
4951
@NotNull
5052
@Deprecated(since = "1.21.3", forRemoval = true) @org.jetbrains.annotations.ApiStatus.ScheduledForRemoval(inVersion = "1.22") // Paper - will be removed via asm-utils
5153
static Fluid[] values() {
52-
return Lists.newArrayList(Registry.FLUID).toArray(new Fluid[0]);
54+
return Registry.FLUID.stream().toArray(Fluid[]::new);
5355
}
5456
}

paper-api/src/main/java/org/bukkit/GameEvent.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.bukkit;
22

3-
import com.google.common.collect.Lists;
43
import java.util.Collection;
5-
import java.util.Collections;
4+
import net.kyori.adventure.key.Key;
5+
import net.kyori.adventure.key.KeyPattern;
66
import org.jetbrains.annotations.NotNull;
77
import org.jetbrains.annotations.Nullable;
88

@@ -250,17 +250,17 @@ public static GameEvent getByKey(@NotNull NamespacedKey namespacedKey) {
250250
* Returns the set of all GameEvents.
251251
*
252252
* @return the memoryKeys
253-
* @deprecated use {@link Registry#iterator()}.
253+
* @deprecated use {@link Registry#stream()}.
254254
*/
255255
@NotNull
256256
@Deprecated(since = "1.20.1")
257257
public static Collection<GameEvent> values() {
258-
return Collections.unmodifiableCollection(Lists.newArrayList(Registry.GAME_EVENT));
258+
return Registry.GAME_EVENT.stream().toList();
259259
}
260260

261261
@NotNull
262-
private static GameEvent getEvent(@NotNull String key) {
263-
return Registry.GAME_EVENT.getOrThrow(NamespacedKey.minecraft(key));
262+
private static GameEvent getEvent(@NotNull @KeyPattern.Value String key) {
263+
return Registry.GAME_EVENT.getOrThrow(Key.key(Key.MINECRAFT_NAMESPACE, key));
264264
}
265265
// Paper start
266266
/**

paper-api/src/main/java/org/bukkit/GameRules.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.bukkit;
22

3+
import net.kyori.adventure.key.Key;
34
import net.kyori.adventure.key.KeyPattern;
45
import org.jetbrains.annotations.ApiStatus;
56
import org.jspecify.annotations.NullMarked;
@@ -132,8 +133,9 @@ public final class GameRules {
132133
public static final GameRule<Boolean> WATER_SOURCE_CONVERSION = getRule("water_source_conversion");
133134
// End generate - GameRules
134135

136+
@SuppressWarnings("unchecked")
135137
private static <T> GameRule<T> getRule(@KeyPattern.Value String key) {
136-
return (GameRule<T>) Registry.GAME_RULE.getOrThrow(NamespacedKey.minecraft(key));
138+
return (GameRule<T>) Registry.GAME_RULE.getOrThrow(Key.key(Key.MINECRAFT_NAMESPACE, key));
137139
}
138140

139141
private GameRules() {

paper-api/src/main/java/org/bukkit/JukeboxSong.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import io.papermc.paper.registry.RegistryAccess;
44
import io.papermc.paper.registry.RegistryKey;
5+
import net.kyori.adventure.key.Key;
6+
import net.kyori.adventure.key.KeyPattern;
57
import net.kyori.adventure.text.Component;
68
import org.jspecify.annotations.NullMarked;
79

@@ -55,8 +57,8 @@ public interface JukeboxSong extends Keyed, Translatable {
5557
JukeboxSong WARD = get("ward");
5658
// End generate - JukeboxSong
5759

58-
private static JukeboxSong get(String key) {
59-
return RegistryAccess.registryAccess().getRegistry(RegistryKey.JUKEBOX_SONG).getOrThrow(NamespacedKey.minecraft(key));
60+
private static JukeboxSong get(@KeyPattern.Value String key) {
61+
return RegistryAccess.registryAccess().getRegistry(RegistryKey.JUKEBOX_SONG).getOrThrow(Key.key(Key.MINECRAFT_NAMESPACE, key));
6062
}
6163

6264
/**

paper-api/src/main/java/org/bukkit/MusicInstrument.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package org.bukkit;
22

3-
import com.google.common.collect.Lists;
43
import io.papermc.paper.registry.RegistryAccess;
54
import io.papermc.paper.registry.RegistryBuilderFactory;
65
import io.papermc.paper.registry.RegistryKey;
76
import io.papermc.paper.registry.data.InlinedRegistryBuilderProvider;
87
import io.papermc.paper.registry.data.InstrumentRegistryEntry;
98
import java.util.Collection;
10-
import java.util.Collections;
119
import java.util.function.Consumer;
10+
import net.kyori.adventure.key.Key;
11+
import net.kyori.adventure.key.KeyPattern;
1212
import net.kyori.adventure.text.Component;
1313
import org.jetbrains.annotations.ApiStatus;
1414
import org.jspecify.annotations.NullMarked;
@@ -46,8 +46,8 @@ public static MusicInstrument create(final Consumer<RegistryBuilderFactory<Music
4646
public static final MusicInstrument YEARN_GOAT_HORN = getInstrument("yearn_goat_horn");
4747
// End generate - MusicInstrument
4848

49-
private static MusicInstrument getInstrument(final String key) {
50-
return RegistryAccess.registryAccess().getRegistry(RegistryKey.INSTRUMENT).getOrThrow(NamespacedKey.minecraft(key));
49+
private static MusicInstrument getInstrument(final @KeyPattern.Value String key) {
50+
return RegistryAccess.registryAccess().getRegistry(RegistryKey.INSTRUMENT).getOrThrow(Key.key(Key.MINECRAFT_NAMESPACE, key));
5151
}
5252

5353
/**
@@ -60,7 +60,7 @@ private static MusicInstrument getInstrument(final String key) {
6060
@Nullable
6161
@Deprecated(since = "1.20.1")
6262
public static MusicInstrument getByKey(final NamespacedKey namespacedKey) {
63-
return Registry.INSTRUMENT.get(namespacedKey);
63+
return RegistryAccess.registryAccess().getRegistry(RegistryKey.INSTRUMENT).get(namespacedKey);
6464
}
6565

6666
/**
@@ -71,7 +71,7 @@ public static MusicInstrument getByKey(final NamespacedKey namespacedKey) {
7171
*/
7272
@Deprecated(since = "1.20.1")
7373
public static Collection<MusicInstrument> values() {
74-
return Collections.unmodifiableCollection(Lists.newArrayList(Registry.INSTRUMENT));
74+
return RegistryAccess.registryAccess().getRegistry(RegistryKey.INSTRUMENT).stream().toList();
7575
}
7676

7777
/**

paper-api/src/main/java/org/bukkit/Sound.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package org.bukkit;
22

33
import com.google.common.base.Preconditions;
4-
import com.google.common.collect.Lists;
54
import io.papermc.paper.registry.RegistryKey;
65
import java.util.Locale;
6+
import net.kyori.adventure.key.Key;
7+
import net.kyori.adventure.key.KeyPattern;
78
import org.bukkit.util.OldEnum;
89
import org.jetbrains.annotations.NotNull;
910

@@ -3701,8 +3702,8 @@ public interface Sound extends OldEnum<Sound>, Keyed, net.kyori.adventure.sound.
37013702
// End generate - Sound
37023703

37033704
@NotNull
3704-
private static Sound getSound(@NotNull String key) {
3705-
return Registry.SOUNDS.getOrThrow(NamespacedKey.minecraft(key));
3705+
private static Sound getSound(@NotNull @KeyPattern.Value String key) {
3706+
return Registry.SOUNDS.getOrThrow(Key.key(Key.MINECRAFT_NAMESPACE, key));
37063707
}
37073708

37083709
/**
@@ -3713,9 +3714,14 @@ private static Sound getSound(@NotNull String key) {
37133714
@NotNull
37143715
@Deprecated(since = "1.21.3", forRemoval = true) @org.jetbrains.annotations.ApiStatus.ScheduledForRemoval(inVersion = "1.22") // Paper - will be removed via asm-utils
37153716
static Sound valueOf(@NotNull String name) {
3716-
Sound sound = Bukkit.getUnsafe().get(RegistryKey.SOUND_EVENT, NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
3717-
if (sound != null) {
3718-
return sound;
3717+
final NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
3718+
Sound sound;
3719+
3720+
if (key != null) {
3721+
sound = Bukkit.getUnsafe().get(RegistryKey.SOUND_EVENT, key);
3722+
if (sound != null) {
3723+
return sound;
3724+
}
37193725
}
37203726

37213727
// Sound keys can have dots in them which where converted to _. Since converting
@@ -3742,12 +3748,12 @@ static Sound valueOf(@NotNull String name) {
37423748

37433749
/**
37443750
* @return an array of all known sounds.
3745-
* @deprecated use {@link Registry#iterator()}.
3751+
* @deprecated use {@link Registry#stream()}.
37463752
*/
37473753
@NotNull
37483754
@Deprecated(since = "1.21.3", forRemoval = true) @org.jetbrains.annotations.ApiStatus.ScheduledForRemoval(inVersion = "1.22") // Paper - will be removed via asm-utils
37493755
static Sound[] values() {
3750-
return Lists.newArrayList(Registry.SOUNDS).toArray(new Sound[0]);
3756+
return Registry.SOUNDS.stream().toArray(Sound[]::new);
37513757
}
37523758

37533759
// Paper start

paper-api/src/main/java/org/bukkit/UnsafeValues.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.bukkit.advancement.Advancement;
88
import org.bukkit.attribute.Attribute;
99
import org.bukkit.attribute.AttributeModifier;
10-
import org.bukkit.block.Biome;
1110
import org.bukkit.block.data.BlockData;
1211
import org.bukkit.damage.DamageSource;
1312
import org.bukkit.damage.DamageType;
@@ -142,7 +141,7 @@ public interface UnsafeValues {
142141
String get(Class<?> aClass, String value);
143142

144143
@ApiStatus.Internal
145-
<B extends Keyed> B get(RegistryKey<B> registry, NamespacedKey key);
144+
@Nullable <B extends Keyed> B get(RegistryKey<B> registry, NamespacedKey key);
146145

147146
// Paper start
148147
@Deprecated(forRemoval = true)

0 commit comments

Comments
 (0)