Skip to content

Commit 431b0e7

Browse files
committed
OldEnum#valueOf check for valid NamespacedKey
1 parent 4873e3f commit 431b0e7

File tree

11 files changed

+51
-14
lines changed

11 files changed

+51
-14
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ 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+
Preconditions.checkArgument(key != null, "Invalid name %s", name);
248+
Art art = Bukkit.getUnsafe().get(RegistryKey.PAINTING_VARIANT, key);
247249
Preconditions.checkArgument(art != null, "No art found with the name %s", name);
248250
return art;
249251
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ private static Fluid getFluid(@NotNull String key) {
3737
@NotNull
3838
@Deprecated(since = "1.21.3", forRemoval = true) @org.jetbrains.annotations.ApiStatus.ScheduledForRemoval(inVersion = "1.22") // Paper - will be removed via asm-utils
3939
static Fluid valueOf(@NotNull String name) {
40-
Fluid fluid = Bukkit.getUnsafe().get(RegistryKey.FLUID, NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
40+
final NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
41+
Preconditions.checkArgument(key != null, "Invalid name %s", name);
42+
Fluid fluid = Bukkit.getUnsafe().get(RegistryKey.FLUID, key);
4143
Preconditions.checkArgument(fluid != null, "No fluid found with the name %s", name);
4244
return fluid;
4345
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3713,9 +3713,14 @@ private static Sound getSound(@NotNull String key) {
37133713
@NotNull
37143714
@Deprecated(since = "1.21.3", forRemoval = true) @org.jetbrains.annotations.ApiStatus.ScheduledForRemoval(inVersion = "1.22") // Paper - will be removed via asm-utils
37153715
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;
3716+
final NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
3717+
Sound sound;
3718+
3719+
if (key != null) {
3720+
sound = Bukkit.getUnsafe().get(RegistryKey.SOUND_EVENT, key);
3721+
if (sound != null) {
3722+
return sound;
3723+
}
37193724
}
37203725

37213726
// Sound keys can have dots in them which where converted to _. Since converting

paper-api/src/main/java/org/bukkit/attribute/Attribute.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ private static Attribute getAttribute(@NotNull String key) {
177177
@NotNull
178178
@Deprecated(since = "1.21.3", forRemoval = true) @org.jetbrains.annotations.ApiStatus.ScheduledForRemoval(inVersion = "1.22") // Paper - will be removed via asm-utils
179179
static Attribute valueOf(@NotNull String name) {
180-
Attribute attribute = Bukkit.getUnsafe().get(RegistryKey.ATTRIBUTE, NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
180+
final NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
181+
Preconditions.checkArgument(key != null, "Invalid name %s", name);
182+
Attribute attribute = Bukkit.getUnsafe().get(RegistryKey.ATTRIBUTE, key);
181183
Preconditions.checkArgument(attribute != null, "No attribute found with the name %s", name);
182184
return attribute;
183185
}

paper-api/src/main/java/org/bukkit/block/Biome.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ static Biome valueOf(@NotNull String name) {
181181
return Biome.CUSTOM;
182182
}
183183

184-
Biome biome = Bukkit.getUnsafe().get(RegistryKey.BIOME, NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
184+
final NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
185+
Preconditions.checkArgument(key != null, "Invalid name %s", name);
186+
Biome biome = Bukkit.getUnsafe().get(RegistryKey.BIOME, key);
185187
Preconditions.checkArgument(biome != null, "No biome found with the name %s", name);
186188
return biome;
187189
}

paper-api/src/main/java/org/bukkit/block/banner/PatternType.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ private static PatternType getType(@NotNull String key) {
174174
@NotNull
175175
@Deprecated(since = "1.21", forRemoval = true) @org.jetbrains.annotations.ApiStatus.ScheduledForRemoval(inVersion = "1.22") // Paper - will be removed via asm-utils
176176
static PatternType valueOf(@NotNull String name) {
177-
PatternType type = Registry.BANNER_PATTERN.get(NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
177+
final NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
178+
Preconditions.checkArgument(key != null, "Invalid name %s", name);
179+
PatternType type = Registry.BANNER_PATTERN.get(key);
178180
Preconditions.checkArgument(type != null, "No pattern type found with the name %s", name);
179181
return type;
180182
}

paper-api/src/main/java/org/bukkit/entity/Cat.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ private static Type getType(@NotNull String key) {
9191
@NotNull
9292
@Deprecated(since = "1.21", forRemoval = true) @org.jetbrains.annotations.ApiStatus.ScheduledForRemoval(inVersion = "1.22") // Paper - will be removed via asm-utils
9393
static Type valueOf(@NotNull String name) {
94-
Type type = RegistryAccess.registryAccess().getRegistry(RegistryKey.CAT_VARIANT).get(NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
94+
final NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
95+
Preconditions.checkArgument(key != null, "Invalid name %s", name);
96+
Type type = RegistryAccess.registryAccess().getRegistry(RegistryKey.CAT_VARIANT).get(key);
9597
Preconditions.checkArgument(type != null, "No cat type found with the name %s", name);
9698
return type;
9799
}

paper-api/src/main/java/org/bukkit/entity/Frog.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ private static Variant getVariant(@NotNull String key) {
7373
@NotNull
7474
@Deprecated(since = "1.21", forRemoval = true) @org.jetbrains.annotations.ApiStatus.ScheduledForRemoval(inVersion = "1.22") // Paper - will be removed via asm-utils
7575
static Variant valueOf(@NotNull String name) {
76-
Variant variant = RegistryAccess.registryAccess().getRegistry(RegistryKey.FROG_VARIANT).get(NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
76+
final NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
77+
Preconditions.checkArgument(key != null, "Invalid name %s", name);
78+
Variant variant = RegistryAccess.registryAccess().getRegistry(RegistryKey.FROG_VARIANT).get(key);
7779
Preconditions.checkArgument(variant != null, "No frog variant found with the name %s", name);
7880
return variant;
7981
}

paper-api/src/main/java/org/bukkit/entity/Villager.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ private static Type getType(@NotNull String key) {
200200
@NotNull
201201
@Deprecated(since = "1.21", forRemoval = true) @org.jetbrains.annotations.ApiStatus.ScheduledForRemoval(inVersion = "1.22") // Paper - will be removed via asm-utils
202202
static Type valueOf(@NotNull String name) {
203-
Type type = Registry.VILLAGER_TYPE.get(NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
203+
final NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
204+
Preconditions.checkArgument(key != null, "Invalid name %s", name);
205+
Type type = Registry.VILLAGER_TYPE.get(key);
204206
Preconditions.checkArgument(type != null, "No villager type found with the name %s", name);
205207
return type;
206208
}
@@ -323,7 +325,9 @@ private static Profession getProfession(@NotNull String key) {
323325
@NotNull
324326
@Deprecated(since = "1.21", forRemoval = true) @org.jetbrains.annotations.ApiStatus.ScheduledForRemoval(inVersion = "1.22") // Paper - will be removed via asm-utils
325327
static Profession valueOf(@NotNull String name) {
326-
Profession profession = Registry.VILLAGER_PROFESSION.get(NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
328+
final NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
329+
Preconditions.checkArgument(key != null, "Invalid name %s", name);
330+
Profession profession = Registry.VILLAGER_PROFESSION.get(key);
327331
Preconditions.checkArgument(profession != null, "No villager profession found with the name %s", name);
328332
return profession;
329333
}

paper-api/src/main/java/org/bukkit/map/MapCursor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,9 @@ static Type byValue(byte value) {
398398
@NotNull
399399
@Deprecated(since = "1.21", forRemoval = true) @ApiStatus.ScheduledForRemoval(inVersion = "1.22") // Paper - will be removed via asm-utils
400400
static Type valueOf(@NotNull String name) {
401-
Type type = Registry.MAP_DECORATION_TYPE.get(NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
401+
final NamespacedKey key = NamespacedKey.fromString(name.toLowerCase(Locale.ROOT));
402+
Preconditions.checkArgument(key != null, "Invalid name %s", name);
403+
Type type = Registry.MAP_DECORATION_TYPE.get(key);
402404
Preconditions.checkArgument(type != null, "No Type found with the name %s", name);
403405
return type;
404406
}

0 commit comments

Comments
 (0)