From 7fabf0c28d2a9808f4db47d631b3f0e9ecc5b308 Mon Sep 17 00:00:00 2001 From: Eclipse Date: Wed, 11 Dec 2024 19:20:42 +0000 Subject: [PATCH] Implement/update register items event for new custom item definitions --- .../GeyserDefineCustomItemsEvent.java | 34 +++++++++++++++-- .../item/custom/v2/CustomItemDefinition.java | 4 +- .../GeyserDefineCustomItemsEventImpl.java | 38 +++---------------- .../CustomItemRegistryPopulator.java | 32 ---------------- .../CustomItemRegistryPopulator_v2.java | 27 +++++++++++++ 5 files changed, 64 insertions(+), 71 deletions(-) diff --git a/api/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserDefineCustomItemsEvent.java b/api/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserDefineCustomItemsEvent.java index bd14aaf43..cacfa8eec 100644 --- a/api/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserDefineCustomItemsEvent.java +++ b/api/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserDefineCustomItemsEvent.java @@ -29,8 +29,10 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.geysermc.event.Event; import org.geysermc.geyser.api.item.custom.CustomItemData; import org.geysermc.geyser.api.item.custom.NonVanillaCustomItemData; +import org.geysermc.geyser.api.item.custom.v2.CustomItemDefinition; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -40,13 +42,27 @@ import java.util.Map; * This event will not be called if the "add non-Bedrock items" setting is disabled in the Geyser config. */ public interface GeyserDefineCustomItemsEvent extends Event { + /** * Gets a multimap of all the already registered custom items indexed by the item's extended java item's identifier. + * This will always return an empty map since the switch to custom item definitions, use {@link GeyserDefineCustomItemsEvent#getExistingCustomItemDefinitions()}. * + * @deprecated use {@link GeyserDefineCustomItemsEvent#getExistingCustomItemDefinitions()} * @return a multimap of all the already registered custom items */ + @Deprecated(forRemoval = true) @NonNull - Map> getExistingCustomItems(); + default Map> getExistingCustomItems() { + return Collections.emptyMap(); + } + + /** + * Gets a multimap of all the already registered custom item definitions indexed by the item's extended java item's identifier. + * + * @return a multimap of all the already registered custom item definitions + */ + @NonNull + Map> getExistingCustomItemDefinitions(); /** * Gets the list of the already registered non-vanilla custom items. @@ -58,14 +74,26 @@ public interface GeyserDefineCustomItemsEvent extends Event { /** * Registers a custom item with a base Java item. This is used to register items with custom textures and properties - * based on NBT data. + * based on NBT data. This method should not be used anymore, {@link CustomItemDefinition}s are preferred now and this method will convert {@code CustomItemData} to {@code CustomItemDefinition} internally. * + * @deprecated use {@link GeyserDefineCustomItemsEvent#register(String, CustomItemDefinition)} * @param identifier the base (java) item * @param customItemData the custom item data to register * @return if the item was registered */ + @Deprecated boolean register(@NonNull String identifier, @NonNull CustomItemData customItemData); + /** + * Registers a custom item with a base Java item. This is used to register items with custom textures and properties + * based on NBT data. + * + * @param identifier the base (java) item + * @param customItemDefinition the custom item definition to register + * @return if the item was registered + */ + boolean register(@NonNull String identifier, @NonNull CustomItemDefinition customItemDefinition); + /** * Registers a custom item with no base item. This is used for mods. * @@ -73,4 +101,4 @@ public interface GeyserDefineCustomItemsEvent extends Event { * @return if the item was registered */ boolean register(@NonNull NonVanillaCustomItemData customItemData); -} \ No newline at end of file +} diff --git a/api/src/main/java/org/geysermc/geyser/api/item/custom/v2/CustomItemDefinition.java b/api/src/main/java/org/geysermc/geyser/api/item/custom/v2/CustomItemDefinition.java index 5e8aa63c0..5a644fda9 100644 --- a/api/src/main/java/org/geysermc/geyser/api/item/custom/v2/CustomItemDefinition.java +++ b/api/src/main/java/org/geysermc/geyser/api/item/custom/v2/CustomItemDefinition.java @@ -41,9 +41,7 @@ public interface CustomItemDefinition { /** * The Bedrock identifier for this custom item. This can't be in the {@code minecraft} namespace. If the {@code minecraft} namespace is given in the builder, the default - * namespace of the implementation is used. - * - * @implNote for Geyser, the default namespace is the {@code geyser_custom} namespace. + * namespace of the implementation is used. For Geyser, the default namespace is the {@code geyser_custom} namespace. */ @NonNull Key bedrockIdentifier(); diff --git a/core/src/main/java/org/geysermc/geyser/event/type/GeyserDefineCustomItemsEventImpl.java b/core/src/main/java/org/geysermc/geyser/event/type/GeyserDefineCustomItemsEventImpl.java index b9a059f19..d7492c761 100644 --- a/core/src/main/java/org/geysermc/geyser/event/type/GeyserDefineCustomItemsEventImpl.java +++ b/core/src/main/java/org/geysermc/geyser/event/type/GeyserDefineCustomItemsEventImpl.java @@ -28,8 +28,8 @@ package org.geysermc.geyser.event.type; import com.google.common.collect.Multimap; import org.checkerframework.checker.nullness.qual.NonNull; import org.geysermc.geyser.api.event.lifecycle.GeyserDefineCustomItemsEvent; -import org.geysermc.geyser.api.item.custom.CustomItemData; import org.geysermc.geyser.api.item.custom.NonVanillaCustomItemData; +import org.geysermc.geyser.api.item.custom.v2.CustomItemDefinition; import java.util.Collection; import java.util.Collections; @@ -37,49 +37,21 @@ import java.util.List; import java.util.Map; public abstract class GeyserDefineCustomItemsEventImpl implements GeyserDefineCustomItemsEvent { - private final Multimap customItems; + private final Multimap customItems; private final List nonVanillaCustomItems; - public GeyserDefineCustomItemsEventImpl(Multimap customItems, List nonVanillaCustomItems) { + public GeyserDefineCustomItemsEventImpl(Multimap customItems, List nonVanillaCustomItems) { this.customItems = customItems; this.nonVanillaCustomItems = nonVanillaCustomItems; } - /** - * Gets a multimap of all the already registered custom items indexed by the item's extended java item's identifier. - * - * @return a multimap of all the already registered custom items - */ @Override - public @NonNull Map> getExistingCustomItems() { - return Collections.unmodifiableMap(this.customItems.asMap()); + public @NonNull Map> getExistingCustomItemDefinitions() { + return Collections.unmodifiableMap(customItems.asMap()); } - /** - * Gets the list of the already registered non-vanilla custom items. - * - * @return the list of the already registered non-vanilla custom items - */ @Override public @NonNull List getExistingNonVanillaCustomItems() { return Collections.unmodifiableList(this.nonVanillaCustomItems); } - - /** - * Registers a custom item with a base Java item. This is used to register items with custom textures and properties - * based on NBT data. - * - * @param identifier the base (java) item - * @param customItemData the custom item data to register - * @return if the item was registered - */ - public abstract boolean register(@NonNull String identifier, @NonNull CustomItemData customItemData); - - /** - * Registers a custom item with no base item. This is used for mods. - * - * @param customItemData the custom item data to register - * @return if the item was registered - */ - public abstract boolean register(@NonNull NonVanillaCustomItemData customItemData); } 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 c4bac70c5..4ae5b407c 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 @@ -26,7 +26,6 @@ package org.geysermc.geyser.registry.populator; import com.google.common.collect.Multimap; -import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.cloudburstmc.nbt.NbtMap; import org.cloudburstmc.nbt.NbtMapBuilder; @@ -39,7 +38,6 @@ import org.geysermc.geyser.api.item.custom.CustomItemData; import org.geysermc.geyser.api.item.custom.CustomRenderOffsets; import org.geysermc.geyser.api.item.custom.NonVanillaCustomItemData; import org.geysermc.geyser.api.util.TriState; -import org.geysermc.geyser.event.type.GeyserDefineCustomItemsEventImpl; import org.geysermc.geyser.item.GeyserCustomMappingData; import org.geysermc.geyser.item.Items; import org.geysermc.geyser.item.components.WearableSlot; @@ -52,7 +50,6 @@ import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponen import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -69,35 +66,6 @@ public class CustomItemRegistryPopulator { //} // TODO }); - GeyserImpl.getInstance().eventBus().fire(new GeyserDefineCustomItemsEventImpl(customItems, nonVanillaCustomItems) { - @Override - public boolean register(@NonNull String identifier, @NonNull CustomItemData customItemData) { - if (CustomItemRegistryPopulator.initialCheck(identifier, customItemData, items)) { - customItems.get(identifier).add(customItemData); - return true; - } - return false; - } - - @Override - public boolean register(@NonNull NonVanillaCustomItemData customItemData) { - if (customItemData.identifier().startsWith("minecraft:")) { - GeyserImpl.getInstance().getLogger().error("The custom item " + customItemData.identifier() + - " is attempting to masquerade as a vanilla Minecraft item!"); - return false; - } - - if (customItemData.javaId() < items.size()) { - // Attempting to overwrite an item that already exists in the protocol - GeyserImpl.getInstance().getLogger().error("The custom item " + customItemData.identifier() + - " is attempting to overwrite a vanilla Minecraft item!"); - return false; - } - - nonVanillaCustomItems.add(customItemData); - return true; - } - }); int customItemCount = customItems.size() + nonVanillaCustomItems.size(); if (customItemCount > 0) { diff --git a/core/src/main/java/org/geysermc/geyser/registry/populator/CustomItemRegistryPopulator_v2.java b/core/src/main/java/org/geysermc/geyser/registry/populator/CustomItemRegistryPopulator_v2.java index 4e8754073..9867474c2 100644 --- a/core/src/main/java/org/geysermc/geyser/registry/populator/CustomItemRegistryPopulator_v2.java +++ b/core/src/main/java/org/geysermc/geyser/registry/populator/CustomItemRegistryPopulator_v2.java @@ -27,6 +27,7 @@ package org.geysermc.geyser.registry.populator; import com.google.common.collect.Multimap; import net.kyori.adventure.key.Key; +import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.cloudburstmc.nbt.NbtMap; import org.cloudburstmc.nbt.NbtMapBuilder; @@ -35,12 +36,14 @@ import org.cloudburstmc.protocol.bedrock.data.definitions.ItemDefinition; import org.cloudburstmc.protocol.bedrock.data.definitions.SimpleItemDefinition; import org.cloudburstmc.protocol.bedrock.data.inventory.ComponentItemData; import org.geysermc.geyser.GeyserImpl; +import org.geysermc.geyser.api.item.custom.CustomItemData; import org.geysermc.geyser.api.item.custom.CustomRenderOffsets; import org.geysermc.geyser.api.item.custom.NonVanillaCustomItemData; import org.geysermc.geyser.api.item.custom.v2.BedrockCreativeTab; import org.geysermc.geyser.api.item.custom.v2.CustomItemBedrockOptions; import org.geysermc.geyser.api.item.custom.v2.CustomItemDefinition; import org.geysermc.geyser.api.item.custom.v2.predicate.CustomItemPredicate; +import org.geysermc.geyser.event.type.GeyserDefineCustomItemsEventImpl; import org.geysermc.geyser.item.GeyserCustomMappingData; import org.geysermc.geyser.item.components.WearableSlot; import org.geysermc.geyser.item.type.Item; @@ -85,6 +88,30 @@ public class CustomItemRegistryPopulator_v2 { } }); + GeyserImpl.getInstance().eventBus().fire(new GeyserDefineCustomItemsEventImpl(customItems, nonVanillaCustomItems) { + + @Override + @Deprecated + public boolean register(@NonNull String identifier, @NonNull CustomItemData customItemData) { + return register(identifier, customItemData.toDefinition(identifier).build()); + } + + @Override + public boolean register(@NonNull String identifier, @NonNull CustomItemDefinition definition) { + if (initialCheck(identifier, definition, customItems, items)) { + customItems.get(identifier).add(definition); + return true; + } + return false; + } + + @Override + public boolean register(@NonNull NonVanillaCustomItemData customItemData) { + // TODO + return false; + } + }); + int customItemCount = customItems.size() + nonVanillaCustomItems.size(); if (customItemCount > 0) { GeyserImpl.getInstance().getLogger().info("Registered " + customItemCount + " custom items");