From 9efb855ca1e5b0b15dd9cb884417a0abdbbb774e Mon Sep 17 00:00:00 2001 From: OmeWillem Date: Tue, 12 Nov 2024 20:31:23 +0100 Subject: [PATCH] changes requested by reviewer --- .../item/custom/NonVanillaCustomItemData.java | 14 ++++++++ .../item/GeyserNonVanillaCustomItemData.java | 11 ++++++ .../CustomItemRegistryPopulator.java | 35 ++++++++++--------- 3 files changed, 43 insertions(+), 17 deletions(-) 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 821ca2961..660af3d58 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 @@ -161,6 +161,14 @@ public interface NonVanillaCustomItemData extends CustomItemData { return displayHandheld(); } + /** + * @deprecated Use {@link #blockPlacer()} instead. + * Gets the block the item places. + * + * @return the block the item places + */ + String block(); + /** * Gets the block placer settings, if it's null then the component won't be added. * @@ -208,6 +216,12 @@ public interface NonVanillaCustomItemData extends CustomItemData { Builder chargeable(boolean isChargeable); + /** + * @deprecated Use {@link #blockPlacer(CustomBlockPlacer)} instead. + */ + @Deprecated + Builder block(String block); + Builder blockPlacer(CustomBlockPlacer blockPlacer); /** 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 1593cba42..38af17faa 100644 --- a/core/src/main/java/org/geysermc/geyser/item/GeyserNonVanillaCustomItemData.java +++ b/core/src/main/java/org/geysermc/geyser/item/GeyserNonVanillaCustomItemData.java @@ -163,6 +163,11 @@ public final class GeyserNonVanillaCustomItemData extends GeyserCustomItemData i return isChargeable; } + @Override + public String block() { + return blockPlacer == null ? null : blockPlacer.block(); + } + @Override public CustomBlockPlacer blockPlacer() { return blockPlacer; @@ -348,6 +353,12 @@ public final class GeyserNonVanillaCustomItemData extends GeyserCustomItemData i return this; } + @Override + public Builder block(String block) { + this.blockPlacer = new CustomBlockPlacer(block, false); + return this; + } + @Override public Builder blockPlacer(CustomBlockPlacer blockPlacer) { this.blockPlacer = blockPlacer; 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 bd04d6a42..037a25785 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 @@ -50,12 +50,7 @@ import org.geysermc.geyser.registry.type.GeyserMappingItem; import org.geysermc.geyser.registry.type.ItemMapping; import org.geysermc.geyser.registry.type.NonVanillaItemRegistration; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; +import java.util.*; public class CustomItemRegistryPopulator { public static void populate(Map items, Multimap customItems, List nonVanillaCustomItems) { @@ -173,7 +168,7 @@ 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(javaItem.maxDamage(), javaItem.maxStackSize(), mapping.getToolType() != null || customItemData.displayHandheld(), false, 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. @@ -201,7 +196,8 @@ public class CustomItemRegistryPopulator { case "minecraft:fire_charge", "minecraft:flint_and_steel" -> computeBlockItemProperties(new CustomBlockPlacer("minecraft:fire", false), componentBuilder); case "minecraft:bow", "minecraft:crossbow", "minecraft:trident" -> computeChargeableProperties(itemProperties, componentBuilder, mapping.getBedrockIdentifier(), protocolVersion); case "minecraft:honey_bottle", "minecraft:milk_bucket", "minecraft:potion" -> computeConsumableProperties(itemProperties, componentBuilder, 2, true); - case "minecraft:experience_bottle", "minecraft:egg", "minecraft:ender_pearl", "minecraft:ender_eye", "minecraft:lingering_potion", "minecraft:snowball", "minecraft:splash_potion" -> computeThrowableProperties(componentBuilder); + case "minecraft:experience_bottle", "minecraft:egg", "minecraft:ender_pearl", "minecraft:ender_eye", "minecraft:lingering_potion", "minecraft:snowball", "minecraft:splash_potion" -> + computeThrowableProperties(componentBuilder); } // Hardcoded on Java, and should extend to the custom item @@ -227,7 +223,10 @@ public class CustomItemRegistryPopulator { NbtMapBuilder itemProperties = NbtMap.builder(); NbtMapBuilder componentBuilder = NbtMap.builder(); - setupBasicItemInfo(customItemData.maxDamage(), customItemData.stackSize(), displayHandheld, customItemData, itemProperties, componentBuilder, protocolVersion); + // this can replace minecraft:icon, so we have to add this here. + boolean replaceBlockItem = customItemData.blockPlacer() != null && customItemData.blockPlacer().replaceBlockItem(); + + setupBasicItemInfo(customItemData.maxDamage(), customItemData.stackSize(), displayHandheld, replaceBlockItem, 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. @@ -269,13 +268,15 @@ public class CustomItemRegistryPopulator { return builder; } - private static void setupBasicItemInfo(int maxDamage, int stackSize, boolean displayHandheld, CustomItemData customItemData, NbtMapBuilder itemProperties, NbtMapBuilder componentBuilder, int protocolVersion) { - NbtMap iconMap = NbtMap.builder() - .putCompound("textures", NbtMap.builder() - .putString("default", customItemData.icon()) - .build()) - .build(); - itemProperties.putCompound("minecraft:icon", iconMap); + private static void setupBasicItemInfo(int maxDamage, int stackSize, boolean displayHandheld, boolean replaceBlockItem, CustomItemData customItemData, NbtMapBuilder itemProperties, NbtMapBuilder componentBuilder, int protocolVersion) { + if (!replaceBlockItem) { + NbtMap iconMap = NbtMap.builder() + .putCompound("textures", NbtMap.builder() + .putString("default", customItemData.icon()) + .build()) + .build(); + itemProperties.putCompound("minecraft:icon", iconMap); + } if (customItemData.creativeCategory().isPresent()) { itemProperties.putInt("creative_category", customItemData.creativeCategory().getAsInt()); @@ -499,7 +500,7 @@ public class CustomItemRegistryPopulator { private static void computeEntityPlacerProperties(NbtMapBuilder componentBuilder) { // all items registered that place entities should be given this component to prevent double placement - // it is okay that the entity here does not match the actual one since we control what entity actually spawns + // it is okay that the entity here does not match the actual one since we control what entity actually spawns componentBuilder.putCompound("minecraft:entity_placer", NbtMap.builder().putString("entity", "minecraft:minecart").build()); }