Skip to content

Commit c37b890

Browse files
committed
More deferred requireNonNull message creation
1 parent f0388e2 commit c37b890

File tree

7 files changed

+15
-16
lines changed

7 files changed

+15
-16
lines changed

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

+8-9
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,12 @@ private static boolean isValidKey(String key) {
8383
* @see #NamespacedKey(Plugin, String)
8484
*/
8585
public NamespacedKey(@NotNull String namespace, @NotNull String key) {
86-
Preconditions.checkArgument(namespace != null && isValidNamespace(namespace), "Invalid namespace. Must be [a-z0-9._-]: %s", namespace);
87-
Preconditions.checkArgument(key != null && isValidKey(key), "Invalid key. Must be [a-z0-9/._-]: %s", key);
88-
86+
Preconditions.checkArgument(namespace != null, "Namespace cannot be null");
87+
Preconditions.checkArgument(key != null, "Key cannot be null");
8988
this.namespace = namespace;
9089
this.key = key;
9190

92-
String string = toString();
93-
Preconditions.checkArgument(string.length() <= Short.MAX_VALUE, "NamespacedKey must be less than 32768 characters", string); // Paper - Fix improper length validation
91+
this.validate();
9492
}
9593

9694
/**
@@ -108,16 +106,17 @@ public NamespacedKey(@NotNull String namespace, @NotNull String key) {
108106
public NamespacedKey(@NotNull Plugin plugin, @NotNull String key) {
109107
Preconditions.checkArgument(plugin != null, "Plugin cannot be null");
110108
Preconditions.checkArgument(key != null, "Key cannot be null");
111-
112109
this.namespace = plugin.getName().toLowerCase(Locale.ROOT);
113110
this.key = key.toLowerCase(Locale.ROOT);
114111

115112
// Check validity after normalization
113+
this.validate();
114+
}
115+
116+
private void validate() {
117+
Preconditions.checkArgument(this.namespace.length() + 1 + this.key.length() <= Short.MAX_VALUE, "NamespacedKey must be less than 32768 characters");
116118
Preconditions.checkArgument(isValidNamespace(this.namespace), "Invalid namespace. Must be [a-z0-9._-]: %s", this.namespace);
117119
Preconditions.checkArgument(isValidKey(this.key), "Invalid key. Must be [a-z0-9/._-]: %s", this.key);
118-
119-
String string = toString();
120-
Preconditions.checkArgument(string.length() <= Short.MAX_VALUE, "NamespacedKey must be less than 32768 characters", string); // Paper - Fix improper length validation
121120
}
122121

123122
@NotNull

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private static <A extends Keyed> Registry<A> registryFor(final RegistryKey<A> re
6464
@SuppressWarnings("removal")
6565
@Deprecated(forRemoval = true, since = "1.21.4")
6666
private static <A extends Keyed> Registry<A> legacyRegistryFor(final Class<A> clazz) {
67-
return Objects.requireNonNull(RegistryAccess.registryAccess().getRegistry(clazz), "No registry present for " + clazz.getSimpleName() + ". This is a bug.");
67+
return Objects.requireNonNull(RegistryAccess.registryAccess().getRegistry(clazz), () -> "No registry present for " + clazz.getSimpleName() + ". This is a bug.");
6868
}
6969

7070
/**

paper-api/src/main/java/org/bukkit/inventory/ItemStack.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat
5757
*/
5858
@org.jetbrains.annotations.Contract(value = "_, _ -> new", pure = true)
5959
public static @NotNull ItemStack of(final @NotNull Material type, final int amount) {
60-
Preconditions.checkArgument(type.asItemType() != null, type + " isn't an item");
60+
Preconditions.checkArgument(type.asItemType() != null, "%s isn't an item", type);
6161
Preconditions.checkArgument(amount > 0, "amount must be greater than 0");
6262
return java.util.Objects.requireNonNull(type.asItemType(), type + " is not an item").createItemStack(amount); // Paper - delegate
6363
}

paper-server/src/main/java/io/papermc/paper/CraftGameEventTag.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ public boolean isTagged(@NotNull GameEvent gameEvent) {
3030

3131
@Override
3232
public @NotNull Set<GameEvent> getValues() {
33-
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());
33+
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());
3434
}
3535
}

paper-server/src/main/java/io/papermc/paper/registry/PaperRegistries.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ public static <M, T extends Keyed, B extends PaperRegistryBuilder<M, T>> Registr
153153

154154
@SuppressWarnings("unchecked")
155155
public static <M, T> RegistryKey<T> registryFromNms(final ResourceKey<? extends Registry<M>> registryResourceKey) {
156-
return (RegistryKey<T>) Objects.requireNonNull(BY_RESOURCE_KEY.get(registryResourceKey), registryResourceKey + " doesn't have an api RegistryKey").apiKey();
156+
return (RegistryKey<T>) Objects.requireNonNull(BY_RESOURCE_KEY.get(registryResourceKey), () -> registryResourceKey + " doesn't have an api RegistryKey").apiKey();
157157
}
158158

159159
@SuppressWarnings("unchecked")
160160
public static <M, T> ResourceKey<? extends Registry<M>> registryToNms(final RegistryKey<T> registryKey) {
161-
return (ResourceKey<? extends Registry<M>>) Objects.requireNonNull(BY_REGISTRY_KEY.get(registryKey), registryKey + " doesn't have an mc registry ResourceKey").mcKey();
161+
return (ResourceKey<? extends Registry<M>>) Objects.requireNonNull(BY_REGISTRY_KEY.get(registryKey), () -> registryKey + " doesn't have an mc registry ResourceKey").mcKey();
162162
}
163163

164164
public static <M, T> TypedKey<T> fromNms(final ResourceKey<M> resourceKey) {

paper-server/src/main/java/io/papermc/paper/registry/entry/RegistryEntryMeta.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ record Craft<M, A extends Keyed>(
6060
) implements ServerSide<M, A> { // TODO remove Keyed
6161

6262
public Craft {
63-
Preconditions.checkArgument(!classToPreload.getPackageName().startsWith("net.minecraft"), classToPreload + " should not be in the net.minecraft package as the class-to-preload");
63+
Preconditions.checkArgument(!classToPreload.getPackageName().startsWith("net.minecraft"), "%s should not be in the net.minecraft package as the class-to-preload", classToPreload);
6464
}
6565
}
6666

paper-server/src/main/java/io/papermc/paper/registry/event/RegistryEventMap.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public RegistryEventMap(final String name) {
3434

3535
@SuppressWarnings("unchecked")
3636
public <T, E extends LifecycleEvent> LifecycleEventType<BootstrapContext, E, ?> getEventType(final RegistryKey<T> registryKey) {
37-
return (LifecycleEventType<BootstrapContext, E, ?>) Objects.requireNonNull(this.eventTypes.get(registryKey), "No hook for " + registryKey);
37+
return (LifecycleEventType<BootstrapContext, E, ?>) Objects.requireNonNull(this.eventTypes.get(registryKey), () -> "No hook for " + registryKey);
3838
}
3939

4040
public boolean hasHandlers(final RegistryKey<?> registryKey) {

0 commit comments

Comments
 (0)