From 178fb2136f46e1ad25d37067c35ceca39c211850 Mon Sep 17 00:00:00 2001 From: rtm516 Date: Sun, 21 May 2023 13:22:15 +0100 Subject: [PATCH] Allow custom bows and food to be registered using the API (#3782) --- .../item/custom/NonVanillaCustomItemData.java | 27 ++++++++++++ .../item/GeyserNonVanillaCustomItemData.java | 42 +++++++++++++++++++ .../CustomItemRegistryPopulator.java | 8 ++++ 3 files changed, 77 insertions(+) diff --git a/api/src/main/java/org/geysermc/geyser/api/item/custom/NonVanillaCustomItemData.java b/api/src/main/java/org/geysermc/geyser/api/item/custom/NonVanillaCustomItemData.java index 13b37096d..0a09f6958 100644 --- a/api/src/main/java/org/geysermc/geyser/api/item/custom/NonVanillaCustomItemData.java +++ b/api/src/main/java/org/geysermc/geyser/api/item/custom/NonVanillaCustomItemData.java @@ -136,6 +136,27 @@ public interface NonVanillaCustomItemData extends CustomItemData { */ boolean isFoil(); + /** + * Gets if the item is edible. + * + * @return if the item is edible + */ + boolean isEdible(); + + /** + * Gets if the food item can always be eaten. + * + * @return if the item is allowed to be eaten all the time + */ + boolean canAlwaysEat(); + + /** + * Gets if the item is chargable, like a bow. + * + * @return if the item should act like a chargable item + */ + boolean isChargeable(); + /** * @deprecated Use {@link #displayHandheld()} instead. * Gets if the item is a tool. This is used to set the render type of the item, if the item is handheld. @@ -183,6 +204,12 @@ public interface NonVanillaCustomItemData extends CustomItemData { Builder foil(boolean isFoil); + Builder edible(boolean isEdible); + + Builder canAlwaysEat(boolean canAlwaysEat); + + Builder chargeable(boolean isChargeable); + /** * @deprecated Use {@link #displayHandheld(boolean)} instead. */ diff --git a/core/src/main/java/org/geysermc/geyser/item/GeyserNonVanillaCustomItemData.java b/core/src/main/java/org/geysermc/geyser/item/GeyserNonVanillaCustomItemData.java index 569ab2300..d6731e3b8 100644 --- a/core/src/main/java/org/geysermc/geyser/item/GeyserNonVanillaCustomItemData.java +++ b/core/src/main/java/org/geysermc/geyser/item/GeyserNonVanillaCustomItemData.java @@ -55,6 +55,9 @@ public final class GeyserNonVanillaCustomItemData extends GeyserCustomItemData i private final boolean isHat; private final boolean isFoil; private final boolean isTool; + private final boolean isEdible; + private final boolean canAlwaysEat; + private final boolean isChargeable; public GeyserNonVanillaCustomItemData(NonVanillaCustomItemDataBuilder builder) { super(builder.name, builder.customItemOptions, builder.displayName, builder.icon, builder.allowOffhand, @@ -75,6 +78,9 @@ public final class GeyserNonVanillaCustomItemData extends GeyserCustomItemData i this.isHat = builder.hat; this.isFoil = builder.foil; this.isTool = builder.tool; + this.isEdible = builder.edible; + this.canAlwaysEat = builder.canAlwaysEat; + this.isChargeable = builder.chargeable; } @Override @@ -147,6 +153,21 @@ public final class GeyserNonVanillaCustomItemData extends GeyserCustomItemData i return isFoil; } + @Override + public boolean isEdible() { + return isEdible; + } + + @Override + public boolean canAlwaysEat() { + return canAlwaysEat; + } + + @Override + public boolean isChargeable() { + return isChargeable; + } + public static class NonVanillaCustomItemDataBuilder extends GeyserCustomItemData.CustomItemDataBuilder implements NonVanillaCustomItemData.Builder { private String identifier = null; private int javaId = -1; @@ -171,6 +192,9 @@ public final class GeyserNonVanillaCustomItemData extends GeyserCustomItemData i private boolean hat = false; private boolean foil = false; private boolean tool = false; + private boolean edible = false; + private boolean canAlwaysEat = false; + private boolean chargeable = false; @Override public NonVanillaCustomItemData.Builder name(@NonNull String name) { @@ -297,6 +321,24 @@ public final class GeyserNonVanillaCustomItemData extends GeyserCustomItemData i return this; } + @Override + public NonVanillaCustomItemData.Builder edible(boolean isEdible) { + this.edible = isEdible; + return this; + } + + @Override + public NonVanillaCustomItemData.Builder canAlwaysEat(boolean canAlwaysEat) { + this.canAlwaysEat = canAlwaysEat; + return this; + } + + @Override + public NonVanillaCustomItemData.Builder chargeable(boolean isChargeable) { + this.chargeable = isChargeable; + return this; + } + @Override public NonVanillaCustomItemData build() { if (identifier == null || javaId == -1) { 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 ca0d7aad1..ddf07c7d6 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 @@ -238,6 +238,14 @@ public class CustomItemRegistryPopulator { computeArmorProperties(armorType, customItemData.protectionValue(), componentBuilder); } + if (customItemData.isEdible()) { + computeConsumableProperties(itemProperties, componentBuilder, 1, customItemData.canAlwaysEat()); + } + + if (customItemData.isChargeable()) { + computeChargeableProperties(itemProperties, componentBuilder); + } + computeRenderOffsets(isHat, customItemData, componentBuilder); if (creativeGroup != null) {