Return -1 if not set for attack damage and protection value and implement proper fallbacks

This commit is contained in:
Eclipse 2024-08-13 21:10:34 +01:00
parent db92184d42
commit 5b864ab88e
No known key found for this signature in database
GPG key ID: 95E6998F82EC938A
3 changed files with 13 additions and 10 deletions

View file

@ -142,11 +142,11 @@ public interface CustomItemData {
* Gets the attack damage of the item. * Gets the attack damage of the item.
* This is purely visual, and only applied to tools * This is purely visual, and only applied to tools
* *
* <p>Returns 0 if not set. When 0, Geyser takes the Java item attack damage when based on a vanilla item, or uses 0 when porting a modded item.</p> * <p>Returns -1 if not set. When not set, Geyser takes the Java item attack damage when based on a vanilla item, or uses 0 when porting a modded item.</p>
* *
* @return the attack damage of the item * @return the attack damage of the item
*/ */
@NonNegative int attackDamage(); int attackDamage();
/** /**
* Gets the armor type of the item. * Gets the armor type of the item.
@ -161,11 +161,13 @@ public interface CustomItemData {
/** /**
* Gets the armor protection value of the item. * Gets the armor protection value of the item.
* *
* <p>Only has a function when {@link CustomItemData#armorType} is set.</p> * <p>Only has a function when {@link CustomItemData#armorType} is set, or when the Java item is an armor item (when based on a vanilla item).</p>
*
* <p>Returns -1 if not set. When not set, Geyser takes the Java item protection value when based on a vanilla item, or uses 0 when porting a modded item.</p>
* *
* @return the armor protection value of the item * @return the armor protection value of the item
*/ */
@NonNegative int protectionValue(); 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

View file

@ -204,9 +204,9 @@ public class GeyserCustomItemData implements CustomItemData {
protected Set<String> tags = new HashSet<>(); protected Set<String> tags = new HashSet<>();
private int stackSize = 0; private int stackSize = 0;
private int maxDamage = -1; private int maxDamage = -1;
private int attackDamage = 0; private int attackDamage = -1;
private String armorType = null; private String armorType = null;
private int protectionValue = 0; private int protectionValue = -1;
private boolean hat = false; private boolean hat = false;
private boolean foil = false; private boolean foil = false;
private boolean edible = false; private boolean edible = false;

View file

@ -182,10 +182,11 @@ public class CustomItemRegistryPopulator {
int protectionValue = 0; int protectionValue = 0;
if (mapping.getArmorType() != null) { if (mapping.getArmorType() != null) {
armorType = mapping.getArmorType(); armorType = mapping.getArmorType();
protectionValue = mapping.getProtectionValue(); protectionValue = customItemData.protectionValue() == -1 ? mapping.getProtectionValue() : customItemData.protectionValue();
} else if (customItemData.armorType() != null) { } else if (customItemData.armorType() != null) {
armorType = customItemData.armorType(); armorType = customItemData.armorType();
protectionValue = customItemData.protectionValue(); // Using 0 as fallback here because the Java item doesn't have an armor type - so its protection value would be 0
protectionValue = customItemData.protectionValue() == -1 ? 0 : customItemData.protectionValue();
} }
if (armorType != null) { if (armorType != null) {
@ -244,13 +245,13 @@ public class CustomItemRegistryPopulator {
boolean canDestroyInCreative = true; boolean canDestroyInCreative = true;
if (customItemData.toolType() != null) { // This is not using the isTool boolean because it is not just a render type here. if (customItemData.toolType() != null) { // This is not using the isTool boolean because it is not just a render type here.
canDestroyInCreative = computeToolProperties(Objects.requireNonNull(customItemData.toolType()), itemProperties, componentBuilder, customItemData.attackDamage()); canDestroyInCreative = computeToolProperties(Objects.requireNonNull(customItemData.toolType()), itemProperties, componentBuilder, Math.max(0, customItemData.attackDamage()));
} }
itemProperties.putBoolean("can_destroy_in_creative", canDestroyInCreative); itemProperties.putBoolean("can_destroy_in_creative", canDestroyInCreative);
String armorType = customItemData.armorType(); String armorType = customItemData.armorType();
if (armorType != null) { if (armorType != null) {
computeArmorProperties(armorType, customItemData.protectionValue(), itemProperties, componentBuilder); computeArmorProperties(armorType, Math.max(0, customItemData.protectionValue()), itemProperties, componentBuilder);
} }
if (customItemData.isEdible()) { if (customItemData.isEdible()) {