From 97e1bebcbd76927756af3dad4084327bd052805f Mon Sep 17 00:00:00 2001 From: Eclipse Date: Fri, 10 May 2024 21:08:21 +0000 Subject: [PATCH] Implement logic for new vanilla custom item components in custom item registry --- .../api/item/custom/CustomItemData.java | 6 ++++ .../geyser/item/GeyserCustomItemData.java | 4 +-- .../CustomItemRegistryPopulator.java | 33 ++++++++++++++----- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/api/src/main/java/org/geysermc/geyser/api/item/custom/CustomItemData.java b/api/src/main/java/org/geysermc/geyser/api/item/custom/CustomItemData.java index 4d8de7c9a..26a5adc8d 100644 --- a/api/src/main/java/org/geysermc/geyser/api/item/custom/CustomItemData.java +++ b/api/src/main/java/org/geysermc/geyser/api/item/custom/CustomItemData.java @@ -118,6 +118,8 @@ public interface CustomItemData { /** * Gets the stack size of the item. * + * Returns 0 if not set. When not set (or 0), takes the Java item stack count when based of a vanilla item, or uses 64 when porting a modded item. + * * @return the stack size of the item */ @NonNegative @@ -126,6 +128,8 @@ public interface CustomItemData { /** * Gets the max damage of the item. * + * Returns -1 if not set. When not set (or below 0), takes the Java item max damage when based of a vanilla item, or uses 0 when porting a modded item. + * * @return the max damage of the item */ int maxDamage(); @@ -134,6 +138,8 @@ public interface CustomItemData { * Gets the attack damage of the item. * This is purely visual, and only applied to tools * + * Returns 0 if not set. When 0, takes the Java item attack damage when based of a vanilla item, or uses 0 when porting a modded item. + * * @return the attack damage of the item */ int attackDamage(); diff --git a/core/src/main/java/org/geysermc/geyser/item/GeyserCustomItemData.java b/core/src/main/java/org/geysermc/geyser/item/GeyserCustomItemData.java index 3063ef8dc..dd95e3dc8 100644 --- a/core/src/main/java/org/geysermc/geyser/item/GeyserCustomItemData.java +++ b/core/src/main/java/org/geysermc/geyser/item/GeyserCustomItemData.java @@ -205,8 +205,8 @@ public class GeyserCustomItemData implements CustomItemData { protected int textureSize = 16; protected CustomRenderOffsets renderOffsets = null; protected Set tags = new HashSet<>(); - private int stackSize = 64; - private int maxDamage = 0; + private int stackSize = 0; + private int maxDamage = -1; private int attackDamage = 0; private String toolType = null; private String toolTier = null; diff --git a/core/src/main/java/org/geysermc/geyser/registry/populator/CustomItemRegistryPopulator.java b/core/src/main/java/org/geysermc/geyser/registry/populator/CustomItemRegistryPopulator.java index 10d87a8a9..75d7fc2c6 100644 --- a/core/src/main/java/org/geysermc/geyser/registry/populator/CustomItemRegistryPopulator.java +++ b/core/src/main/java/org/geysermc/geyser/registry/populator/CustomItemRegistryPopulator.java @@ -131,8 +131,8 @@ public class CustomItemRegistryPopulator { Set repairMaterials = customItemData.repairMaterials(); Item.Builder itemBuilder = Item.builder() - .stackSize(customItemData.stackSize()) - .maxDamage(customItemData.maxDamage()); + .stackSize(customItemData.stackSize() == 0 ? 64 : customItemData.stackSize()) + .maxDamage(Math.max(customItemData.maxDamage(), 0)); Item item = new Item(customIdentifier, itemBuilder) { @Override public boolean isValidRepairItem(Item other) { @@ -168,12 +168,24 @@ public class CustomItemRegistryPopulator { NbtMapBuilder itemProperties = NbtMap.builder(); NbtMapBuilder componentBuilder = NbtMap.builder(); - setupBasicItemInfo(javaItem.maxDamage(), javaItem.maxStackSize(), mapping.getToolType() != null || customItemData.displayHandheld(), customItemData, itemProperties, componentBuilder, protocolVersion); + setupBasicItemInfo(customItemData.maxDamage() < 0 ? javaItem.maxDamage() : customItemData.maxDamage(), + customItemData.stackSize() == 0 ? javaItem.maxStackSize() : customItemData.stackSize(), + mapping.getToolType() != null || customItemData.displayHandheld(), + customItemData, itemProperties, componentBuilder, protocolVersion); boolean canDestroyInCreative = true; - if (mapping.getToolType() != null) { // This is not using the isTool boolean because it is not just a render type here. - canDestroyInCreative = computeToolProperties(mapping.getToolType(), itemProperties, componentBuilder, javaItem.attackDamage()); + String toolType = null; + if (mapping.getToolType() != null) { + toolType = mapping.getToolType(); + } else if (customItemData.toolType() != null) { + toolType = customItemData.toolType(); } + + if (toolType != null) { + canDestroyInCreative = computeToolProperties(toolType, itemProperties, componentBuilder, + customItemData.attackDamage() == 0 ? javaItem.attackDamage() : customItemData.attackDamage()); + } + itemProperties.putBoolean("can_destroy_in_creative", canDestroyInCreative); if (mapping.getArmorType() != null) { @@ -184,14 +196,18 @@ public class CustomItemRegistryPopulator { computeBlockItemProperties(mapping.getBedrockIdentifier(), componentBuilder); } - if (mapping.isEdible()) { - computeConsumableProperties(itemProperties, componentBuilder, 1, false); + if (mapping.isEdible() || customItemData.isEdible()) { + computeConsumableProperties(itemProperties, componentBuilder, 1, customItemData.canAlwaysEat()); } if (mapping.isEntityPlacer()) { computeEntityPlacerProperties(componentBuilder); } + if (customItemData.isFoil()) { + itemProperties.putBoolean("foil", true); + } + switch (mapping.getBedrockIdentifier()) { case "minecraft:fire_charge", "minecraft:flint_and_steel" -> computeBlockItemProperties("minecraft:fire", componentBuilder); case "minecraft:bow", "minecraft:crossbow", "minecraft:trident" -> computeChargeableProperties(itemProperties, componentBuilder, mapping.getBedrockIdentifier(), protocolVersion); @@ -217,7 +233,8 @@ public class CustomItemRegistryPopulator { NbtMapBuilder itemProperties = NbtMap.builder(); NbtMapBuilder componentBuilder = NbtMap.builder(); - setupBasicItemInfo(customItemData.maxDamage(), customItemData.stackSize(), displayHandheld, customItemData, itemProperties, componentBuilder, protocolVersion); + setupBasicItemInfo(Math.max(customItemData.maxDamage(), 0), customItemData.stackSize() == 0 ? 64 : customItemData.stackSize(), + displayHandheld, customItemData, itemProperties, componentBuilder, protocolVersion); boolean canDestroyInCreative = true; if (customItemData.toolType() != null) { // This is not using the isTool boolean because it is not just a render type here.