Skip to content

Commit

Permalink
Implement/update register items event for new custom item definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
eclipseisoffline committed Dec 12, 2024
1 parent fd09a05 commit 7fabf0c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
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;

Expand All @@ -40,13 +42,27 @@
* 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
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<CustomItemData>> getExistingCustomItems();
Map<String, Collection<CustomItemDefinition>> getExistingCustomItemDefinitions();

/**
* Gets the list of the already registered non-vanilla custom items.
Expand All @@ -58,19 +74,31 @@ 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.
*
* @param customItemData the custom item data to register
* @return if the item was registered
*/
boolean register(@NonNull NonVanillaCustomItemData customItemData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,58 +28,30 @@
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;
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +38,6 @@
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;
Expand All @@ -52,7 +50,6 @@
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;
Expand All @@ -69,35 +66,6 @@ public static void populate(Map<String, GeyserMappingItem> items, Multimap<Strin
//} // 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

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;
Expand All @@ -35,12 +36,14 @@
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;
Expand Down Expand Up @@ -85,6 +88,30 @@ public static void populate(Map<String, GeyserMappingItem> items, Multimap<Strin
}
});

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");
Expand Down

0 comments on commit 7fabf0c

Please sign in to comment.