Implement/update register items event for new custom item definitions

This commit is contained in:
Eclipse 2024-12-11 19:20:42 +00:00
parent fd09a05aaf
commit 7fabf0c28d
No known key found for this signature in database
GPG key ID: 95E6998F82EC938A
5 changed files with 64 additions and 71 deletions

View file

@ -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<String, Collection<CustomItemData>> getExistingCustomItems();
default Map<String, Collection<CustomItemData>> 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<String, Collection<CustomItemDefinition>> 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);
}
}

View file

@ -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();

View file

@ -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<String, CustomItemData> customItems;
private final Multimap<String, CustomItemDefinition> customItems;
private final List<NonVanillaCustomItemData> nonVanillaCustomItems;
public GeyserDefineCustomItemsEventImpl(Multimap<String, CustomItemData> customItems, List<NonVanillaCustomItemData> nonVanillaCustomItems) {
public GeyserDefineCustomItemsEventImpl(Multimap<String, CustomItemDefinition> customItems, List<NonVanillaCustomItemData> 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<String, Collection<CustomItemData>> getExistingCustomItems() {
return Collections.unmodifiableMap(this.customItems.asMap());
public @NonNull Map<String, Collection<CustomItemDefinition>> 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<NonVanillaCustomItemData> 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);
}

View file

@ -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) {

View file

@ -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");