Implement stack size and max damage restrictions

This commit is contained in:
Eclipse 2024-08-01 10:15:46 +00:00
parent 7881c46411
commit 7d49af1653
No known key found for this signature in database
GPG key ID: 95E6998F82EC938A
3 changed files with 21 additions and 1 deletions

View file

@ -120,6 +120,8 @@ public interface CustomItemData {
*
* Returns 0 if not set. When not set (or 0), it defaults to the stack count of the Java item when based of a vanilla item, or 64 when registering a non-vanilla item.
*
* Note that, to copy Java behaviour, setting the stack size of an item to a value above 1 will set the max damage to 0. If a max damage value above 0 was explicitly set, an exception will be thrown.
*
* @return the stack size of the item
*/
@NonNegative
@ -130,6 +132,8 @@ public interface CustomItemData {
*
* Returns -1 if not set. When not set (or below 0), it defaults to the maximum damage of the Java item when based of a vanilla item, or uses 0 when registering a non-vanilla item.
*
* Note that, to copy Java behaviour, setting the max damage value of an item to a value above 0 will set the stack size to 1. If a stack size above 1 was explicitly set, an exception will be thrown.
*
* @return the max damage of the item
*/
int maxDamage();

View file

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

View file

@ -222,7 +222,7 @@ public class MappingsReader_v1 extends MappingsReader {
}
if (node.has("attack_damage")) {
customItemData.maxDamage(node.get("attack_damage").asInt());
customItemData.attackDamage(node.get("attack_damage").asInt());
}
if (node.has("armor_type")) {