diff --git a/paper-api/src/main/java/org/bukkit/NamespacedKey.java b/paper-api/src/main/java/org/bukkit/NamespacedKey.java index d71531c384..cb57823400 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 09d9998772..75e236ae23 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<T extends Keyed> extends Iterable<T> { @SuppressWarnings("removal") @Deprecated(forRemoval = true, since = "1.21.4") private static <A extends Keyed> Registry<A> legacyRegistryFor(final Class<A> 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 5644b35015..dc50f83e96 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 874c420e60..8a2662d4b1 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<net.minecraft.world.level.gameev @Override public @NotNull Set<GameEvent> 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 c0b17315a3..d0e5ff93e0 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 <M, T> RegistryKey<T> registryFromNms(final ResourceKey<? extends Registry<M>> registryResourceKey) { - return (RegistryKey<T>) Objects.requireNonNull(BY_RESOURCE_KEY.get(registryResourceKey), registryResourceKey + " doesn't have an api RegistryKey").apiKey(); + return (RegistryKey<T>) Objects.requireNonNull(BY_RESOURCE_KEY.get(registryResourceKey), () -> registryResourceKey + " doesn't have an api RegistryKey").apiKey(); } @SuppressWarnings("unchecked") public static <M, T> ResourceKey<? extends Registry<M>> registryToNms(final RegistryKey<T> registryKey) { - return (ResourceKey<? extends Registry<M>>) Objects.requireNonNull(BY_REGISTRY_KEY.get(registryKey), registryKey + " doesn't have an mc registry ResourceKey").mcKey(); + return (ResourceKey<? extends Registry<M>>) Objects.requireNonNull(BY_REGISTRY_KEY.get(registryKey), () -> registryKey + " doesn't have an mc registry ResourceKey").mcKey(); } public static <M, T> TypedKey<T> fromNms(final ResourceKey<M> 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 7cd152734f..be3ec5863f 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<M, A extends Keyed> permits RegistryEn ) implements ServerSide<M, A> { // 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 bfcd0884d7..2c662e8957 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 <T, E extends LifecycleEvent> LifecycleEventType<BootstrapContext, E, ?> getEventType(final RegistryKey<T> registryKey) { - return (LifecycleEventType<BootstrapContext, E, ?>) Objects.requireNonNull(this.eventTypes.get(registryKey), "No hook for " + registryKey); + return (LifecycleEventType<BootstrapContext, E, ?>) Objects.requireNonNull(this.eventTypes.get(registryKey), () -> "No hook for " + registryKey); } public boolean hasHandlers(final RegistryKey<?> registryKey) {