Improve exception messages for stackSize and maxDamage builder methods, and add proper method argument annotations for these methods

This commit is contained in:
Eclipse 2024-08-11 10:44:40 +01:00
parent 7394cf5a18
commit 7ebbf538f2
No known key found for this signature in database
GPG key ID: 95E6998F82EC938A
3 changed files with 10 additions and 8 deletions

View file

@ -26,6 +26,7 @@
package org.geysermc.geyser.api.item.custom; package org.geysermc.geyser.api.item.custom;
import org.checkerframework.checker.index.qual.NonNegative; import org.checkerframework.checker.index.qual.NonNegative;
import org.checkerframework.checker.index.qual.Positive;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.api.GeyserApi; import org.geysermc.geyser.api.GeyserApi;
@ -226,9 +227,9 @@ public interface CustomItemData {
Builder tags(@Nullable Set<String> tags); Builder tags(@Nullable Set<String> tags);
Builder stackSize(@NonNegative int stackSize); Builder stackSize(@Positive int stackSize);
Builder maxDamage(int maxDamage); Builder maxDamage(@NonNegative int maxDamage);
Builder attackDamage(int attackDamage); Builder attackDamage(int attackDamage);

View file

@ -26,6 +26,7 @@
package org.geysermc.geyser.api.item.custom; package org.geysermc.geyser.api.item.custom;
import org.checkerframework.checker.index.qual.NonNegative; import org.checkerframework.checker.index.qual.NonNegative;
import org.checkerframework.checker.index.qual.Positive;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.api.GeyserApi; import org.geysermc.geyser.api.GeyserApi;
@ -142,10 +143,10 @@ public interface NonVanillaCustomItemData extends CustomItemData {
Builder tags(@Nullable Set<String> tags); Builder tags(@Nullable Set<String> tags);
@Override @Override
Builder stackSize(@NonNegative int stackSize); Builder stackSize(@Positive int stackSize);
@Override @Override
Builder maxDamage(int maxDamage); Builder maxDamage(@NonNegative int maxDamage);
@Override @Override
Builder attackDamage(int attackDamage); Builder attackDamage(int attackDamage);

View file

@ -276,10 +276,10 @@ public class GeyserCustomItemData implements CustomItemData {
@Override @Override
public Builder stackSize(int stackSize) { public Builder stackSize(int stackSize) {
if (stackSize < 1) { if (stackSize < 1) {
throw new IllegalArgumentException("Stack size cannot be below 1"); throw new IllegalArgumentException("Stack size cannot be below 1 (" + stackSize + " was given)");
} else if (stackSize > 1) { } else if (stackSize > 1) {
if (this.maxDamage > 0) { if (this.maxDamage > 0) {
throw new IllegalStateException("Stack size cannot be above 1 when max damage is above 0"); throw new IllegalArgumentException("Stack size cannot be above 1 when max damage is above 0 (" + stackSize + " was given)");
} }
// Explicitly set max damage to 0 instead of falling back to the Java vanilla item value // Explicitly set max damage to 0 instead of falling back to the Java vanilla item value
this.maxDamage = 0; this.maxDamage = 0;
@ -292,10 +292,10 @@ public class GeyserCustomItemData implements CustomItemData {
@Override @Override
public Builder maxDamage(int maxDamage) { public Builder maxDamage(int maxDamage) {
if (maxDamage < 0) { if (maxDamage < 0) {
throw new IllegalArgumentException("Max damage cannot be below 0"); throw new IllegalArgumentException("Max damage cannot be below 0 (" + maxDamage + " was given)");
} else if (maxDamage > 0) { } else if (maxDamage > 0) {
if (this.stackSize > 1) { if (this.stackSize > 1) {
throw new IllegalStateException("Max damage cannot be above 0 when stack size is above 1"); throw new IllegalArgumentException("Max damage cannot be above 0 when stack size is above 1 (" + maxDamage + " was given)");
} }
// Explicitly set stack size to 1 instead of falling back to the Java vanilla item value // Explicitly set stack size to 1 instead of falling back to the Java vanilla item value
this.stackSize = 1; this.stackSize = 1;