Add more annotations and proper validating of attack damage and protection value values

This commit is contained in:
Eclipse 2024-08-13 21:01:22 +01:00
parent 7ebbf538f2
commit c890f064ae
No known key found for this signature in database
GPG key ID: 95E6998F82EC938A
4 changed files with 25 additions and 16 deletions

View file

@ -125,8 +125,7 @@ public interface CustomItemData {
* *
* @return the stack size of the item * @return the stack size of the item
*/ */
@NonNegative @NonNegative int stackSize();
int stackSize();
/** /**
* Gets the max damage of the item. * Gets the max damage of the item.
@ -147,7 +146,7 @@ public interface CustomItemData {
* *
* @return the attack damage of the item * @return the attack damage of the item
*/ */
int attackDamage(); @NonNegative int attackDamage();
/** /**
* Gets the armor type of the item. * Gets the armor type of the item.
@ -166,7 +165,7 @@ public interface CustomItemData {
* *
* @return the armor protection value of the item * @return the armor protection value of the item
*/ */
int protectionValue(); @NonNegative int protectionValue();
/** /**
* Gets if the item is a hat. This is used to determine if the item should be rendered on the player's head, and * Gets if the item is a hat. This is used to determine if the item should be rendered on the player's head, and
@ -231,11 +230,11 @@ public interface CustomItemData {
Builder maxDamage(@NonNegative int maxDamage); Builder maxDamage(@NonNegative int maxDamage);
Builder attackDamage(int attackDamage); Builder attackDamage(@NonNegative int attackDamage);
Builder armorType(@Nullable String armorType); Builder armorType(@Nullable String armorType);
Builder protectionValue(int protectionValue); Builder protectionValue(@NonNegative int protectionValue);
Builder hat(boolean isHat); Builder hat(boolean isHat);

View file

@ -149,13 +149,13 @@ public interface NonVanillaCustomItemData extends CustomItemData {
Builder maxDamage(@NonNegative int maxDamage); Builder maxDamage(@NonNegative int maxDamage);
@Override @Override
Builder attackDamage(int attackDamage); Builder attackDamage(@NonNegative int attackDamage);
@Override @Override
Builder armorType(@Nullable String armorType); Builder armorType(@Nullable String armorType);
@Override @Override
Builder protectionValue(int protectionValue); Builder protectionValue(@NonNegative int protectionValue);
@Override @Override
Builder hat(boolean isHat); Builder hat(boolean isHat);

View file

@ -27,6 +27,8 @@ package org.geysermc.geyser.item;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.ToString; import lombok.ToString;
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.item.custom.CustomItemData; import org.geysermc.geyser.api.item.custom.CustomItemData;
@ -274,7 +276,7 @@ public class GeyserCustomItemData implements CustomItemData {
} }
@Override @Override
public Builder stackSize(int stackSize) { public Builder stackSize(@Positive int stackSize) {
if (stackSize < 1) { if (stackSize < 1) {
throw new IllegalArgumentException("Stack size cannot be below 1 (" + stackSize + " was given)"); throw new IllegalArgumentException("Stack size cannot be below 1 (" + stackSize + " was given)");
} else if (stackSize > 1) { } else if (stackSize > 1) {
@ -290,7 +292,7 @@ public class GeyserCustomItemData implements CustomItemData {
} }
@Override @Override
public Builder maxDamage(int maxDamage) { public Builder maxDamage(@NonNegative int maxDamage) {
if (maxDamage < 0) { if (maxDamage < 0) {
throw new IllegalArgumentException("Max damage cannot be below 0 (" + maxDamage + " was given)"); throw new IllegalArgumentException("Max damage cannot be below 0 (" + maxDamage + " was given)");
} else if (maxDamage > 0) { } else if (maxDamage > 0) {
@ -306,7 +308,10 @@ public class GeyserCustomItemData implements CustomItemData {
} }
@Override @Override
public Builder attackDamage(int attackDamage) { public Builder attackDamage(@NonNegative int attackDamage) {
if (attackDamage < 0) {
throw new IllegalArgumentException("Protection value cannot be below 0 (" + attackDamage + " was given)");
}
this.attackDamage = attackDamage; this.attackDamage = attackDamage;
return this; return this;
} }
@ -318,7 +323,10 @@ public class GeyserCustomItemData implements CustomItemData {
} }
@Override @Override
public Builder protectionValue(int protectionValue) { public Builder protectionValue(@NonNegative int protectionValue) {
if (protectionValue < 0) {
throw new IllegalArgumentException("Protection value cannot be below 0 (" + protectionValue + " was given)");
}
this.protectionValue = protectionValue; this.protectionValue = protectionValue;
return this; return this;
} }

View file

@ -27,6 +27,8 @@ package org.geysermc.geyser.item;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.ToString; import lombok.ToString;
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.item.custom.CustomItemOptions; import org.geysermc.geyser.api.item.custom.CustomItemOptions;
@ -167,17 +169,17 @@ public final class GeyserNonVanillaCustomItemData extends GeyserCustomItemData i
} }
@Override @Override
public Builder stackSize(int stackSize) { public Builder stackSize(@Positive int stackSize) {
return (Builder) super.stackSize(stackSize); return (Builder) super.stackSize(stackSize);
} }
@Override @Override
public Builder maxDamage(int maxDamage) { public Builder maxDamage(@NonNegative int maxDamage) {
return (Builder) super.maxDamage(maxDamage); return (Builder) super.maxDamage(maxDamage);
} }
@Override @Override
public Builder attackDamage(int attackDamage) { public Builder attackDamage(@NonNegative int attackDamage) {
return (Builder) super.attackDamage(attackDamage); return (Builder) super.attackDamage(attackDamage);
} }
@ -187,7 +189,7 @@ public final class GeyserNonVanillaCustomItemData extends GeyserCustomItemData i
} }
@Override @Override
public Builder protectionValue(int protectionValue) { public Builder protectionValue(@NonNegative int protectionValue) {
return (Builder) super.protectionValue(protectionValue); return (Builder) super.protectionValue(protectionValue);
} }