From c37b890c8bb59cdc19043dce915fe3fe2c1db22f Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Thu, 13 Mar 2025 12:27:12 +0100 Subject: [PATCH] More deferred requireNonNull message creation --- .../src/main/java/org/bukkit/NamespacedKey.java | 17 ++++++++--------- .../src/main/java/org/bukkit/Registry.java | 2 +- .../java/org/bukkit/inventory/ItemStack.java | 2 +- .../io/papermc/paper/CraftGameEventTag.java | 2 +- .../papermc/paper/registry/PaperRegistries.java | 4 ++-- .../paper/registry/entry/RegistryEntryMeta.java | 2 +- .../paper/registry/event/RegistryEventMap.java | 2 +- 7 files changed, 15 insertions(+), 16 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/NamespacedKey.java b/paper-api/src/main/java/org/bukkit/NamespacedKey.java index d71531c38..cb5782340 100644 --- a/paper-api/src/main/java/org/bukkit/NamespacedKey.java +++ b/paper-api/src/main/java/org/bukkit/NamespacedKey.java @@ -83,14 +83,12 @@ public final class NamespacedKey implements net.kyori.adventure.key.Key, com.des * @see #NamespacedKey(Plugin, String) */ public NamespacedKey(@NotNull String namespace, @NotNull String key) { - Preconditions.checkArgument(namespace != null && isValidNamespace(namespace), "Invalid namespace. Must be [a-z0-9._-]: %s", namespace); - Preconditions.checkArgument(key != null && isValidKey(key), "Invalid key. Must be [a-z0-9/._-]: %s", key); - + Preconditions.checkArgument(namespace != null, "Namespace cannot be null"); + Preconditions.checkArgument(key != null, "Key cannot be null"); this.namespace = namespace; this.key = key; - String string = toString(); - Preconditions.checkArgument(string.length() <= Short.MAX_VALUE, "NamespacedKey must be less than 32768 characters", string); // Paper - Fix improper length validation + this.validate(); } /** @@ -108,16 +106,17 @@ public final class NamespacedKey implements net.kyori.adventure.key.Key, com.des public NamespacedKey(@NotNull Plugin plugin, @NotNull String key) { Preconditions.checkArgument(plugin != null, "Plugin cannot be null"); Preconditions.checkArgument(key != null, "Key cannot be null"); - this.namespace = plugin.getName().toLowerCase(Locale.ROOT); this.key = key.toLowerCase(Locale.ROOT); // Check validity after normalization + this.validate(); + } + + private void validate() { + Preconditions.checkArgument(this.namespace.length() + 1 + this.key.length() <= Short.MAX_VALUE, "NamespacedKey must be less than 32768 characters"); Preconditions.checkArgument(isValidNamespace(this.namespace), "Invalid namespace. Must be [a-z0-9._-]: %s", this.namespace); Preconditions.checkArgument(isValidKey(this.key), "Invalid key. Must be [a-z0-9/._-]: %s", this.key); - - String string = toString(); - Preconditions.checkArgument(string.length() <= Short.MAX_VALUE, "NamespacedKey must be less than 32768 characters", string); // Paper - Fix improper length validation } @NotNull diff --git a/paper-api/src/main/java/org/bukkit/Registry.java b/paper-api/src/main/java/org/bukkit/Registry.java index 09d999877..75e236ae2 100644 --- a/paper-api/src/main/java/org/bukkit/Registry.java +++ b/paper-api/src/main/java/org/bukkit/Registry.java @@ -64,7 +64,7 @@ public interface Registry extends Iterable { @SuppressWarnings("removal") @Deprecated(forRemoval = true, since = "1.21.4") private static Registry legacyRegistryFor(final Class clazz) { - return Objects.requireNonNull(RegistryAccess.registryAccess().getRegistry(clazz), "No registry present for " + clazz.getSimpleName() + ". This is a bug."); + return Objects.requireNonNull(RegistryAccess.registryAccess().getRegistry(clazz), () -> "No registry present for " + clazz.getSimpleName() + ". This is a bug."); } /** 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 5644b3501..dc50f83e9 100644 --- a/paper-api/src/main/java/org/bukkit/inventory/ItemStack.java +++ b/paper-api/src/main/java/org/bukkit/inventory/ItemStack.java @@ -57,7 +57,7 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat */ @org.jetbrains.annotations.Contract(value = "_, _ -> new", pure = true) public static @NotNull ItemStack of(final @NotNull Material type, final int amount) { - Preconditions.checkArgument(type.asItemType() != null, type + " isn't an item"); + Preconditions.checkArgument(type.asItemType() != null, "%s isn't an item", type); Preconditions.checkArgument(amount > 0, "amount must be greater than 0"); return java.util.Objects.requireNonNull(type.asItemType(), type + " is not an item").createItemStack(amount); // Paper - delegate } diff --git a/paper-server/src/main/java/io/papermc/paper/CraftGameEventTag.java b/paper-server/src/main/java/io/papermc/paper/CraftGameEventTag.java index 874c420e6..8a2662d4b 100644 --- a/paper-server/src/main/java/io/papermc/paper/CraftGameEventTag.java +++ b/paper-server/src/main/java/io/papermc/paper/CraftGameEventTag.java @@ -30,6 +30,6 @@ public class CraftGameEventTag extends CraftTag getValues() { - return getHandle().stream().map((nms) -> Objects.requireNonNull(GameEvent.getByKey(CraftNamespacedKey.fromMinecraft(BuiltInRegistries.GAME_EVENT.getKey(nms.value()))), nms + " is not a recognized game event")).collect(Collectors.toUnmodifiableSet()); + return getHandle().stream().map((nms) -> Objects.requireNonNull(GameEvent.getByKey(CraftNamespacedKey.fromMinecraft(BuiltInRegistries.GAME_EVENT.getKey(nms.value()))), () -> nms + " is not a recognized game event")).collect(Collectors.toUnmodifiableSet()); } } diff --git a/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistries.java b/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistries.java index c0b17315a..d0e5ff93e 100644 --- a/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistries.java +++ b/paper-server/src/main/java/io/papermc/paper/registry/PaperRegistries.java @@ -153,12 +153,12 @@ public final class PaperRegistries { @SuppressWarnings("unchecked") public static RegistryKey registryFromNms(final ResourceKey> registryResourceKey) { - return (RegistryKey) Objects.requireNonNull(BY_RESOURCE_KEY.get(registryResourceKey), registryResourceKey + " doesn't have an api RegistryKey").apiKey(); + return (RegistryKey) Objects.requireNonNull(BY_RESOURCE_KEY.get(registryResourceKey), () -> registryResourceKey + " doesn't have an api RegistryKey").apiKey(); } @SuppressWarnings("unchecked") public static ResourceKey> registryToNms(final RegistryKey registryKey) { - return (ResourceKey>) Objects.requireNonNull(BY_REGISTRY_KEY.get(registryKey), registryKey + " doesn't have an mc registry ResourceKey").mcKey(); + return (ResourceKey>) Objects.requireNonNull(BY_REGISTRY_KEY.get(registryKey), () -> registryKey + " doesn't have an mc registry ResourceKey").mcKey(); } public static TypedKey fromNms(final ResourceKey resourceKey) { diff --git a/paper-server/src/main/java/io/papermc/paper/registry/entry/RegistryEntryMeta.java b/paper-server/src/main/java/io/papermc/paper/registry/entry/RegistryEntryMeta.java index 7cd152734..be3ec5863 100644 --- a/paper-server/src/main/java/io/papermc/paper/registry/entry/RegistryEntryMeta.java +++ b/paper-server/src/main/java/io/papermc/paper/registry/entry/RegistryEntryMeta.java @@ -60,7 +60,7 @@ public sealed interface RegistryEntryMeta permits RegistryEn ) implements ServerSide { // TODO remove Keyed public Craft { - Preconditions.checkArgument(!classToPreload.getPackageName().startsWith("net.minecraft"), classToPreload + " should not be in the net.minecraft package as the class-to-preload"); + Preconditions.checkArgument(!classToPreload.getPackageName().startsWith("net.minecraft"), "%s should not be in the net.minecraft package as the class-to-preload", classToPreload); } } diff --git a/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEventMap.java b/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEventMap.java index bfcd0884d..2c662e895 100644 --- a/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEventMap.java +++ b/paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEventMap.java @@ -34,7 +34,7 @@ public final class RegistryEventMap { @SuppressWarnings("unchecked") public LifecycleEventType getEventType(final RegistryKey registryKey) { - return (LifecycleEventType) Objects.requireNonNull(this.eventTypes.get(registryKey), "No hook for " + registryKey); + return (LifecycleEventType) Objects.requireNonNull(this.eventTypes.get(registryKey), () -> "No hook for " + registryKey); } public boolean hasHandlers(final RegistryKey registryKey) {