Skip to content

Commit 7fabf0c

Browse files
Implement/update register items event for new custom item definitions
1 parent fd09a05 commit 7fabf0c

File tree

5 files changed

+64
-71
lines changed

5 files changed

+64
-71
lines changed

api/src/main/java/org/geysermc/geyser/api/event/lifecycle/GeyserDefineCustomItemsEvent.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
import org.geysermc.event.Event;
3030
import org.geysermc.geyser.api.item.custom.CustomItemData;
3131
import org.geysermc.geyser.api.item.custom.NonVanillaCustomItemData;
32+
import org.geysermc.geyser.api.item.custom.v2.CustomItemDefinition;
3233

3334
import java.util.Collection;
35+
import java.util.Collections;
3436
import java.util.List;
3537
import java.util.Map;
3638

@@ -40,13 +42,27 @@
4042
* This event will not be called if the "add non-Bedrock items" setting is disabled in the Geyser config.
4143
*/
4244
public interface GeyserDefineCustomItemsEvent extends Event {
45+
4346
/**
4447
* Gets a multimap of all the already registered custom items indexed by the item's extended java item's identifier.
48+
* This will always return an empty map since the switch to custom item definitions, use {@link GeyserDefineCustomItemsEvent#getExistingCustomItemDefinitions()}.
4549
*
50+
* @deprecated use {@link GeyserDefineCustomItemsEvent#getExistingCustomItemDefinitions()}
4651
* @return a multimap of all the already registered custom items
4752
*/
53+
@Deprecated(forRemoval = true)
54+
@NonNull
55+
default Map<String, Collection<CustomItemData>> getExistingCustomItems() {
56+
return Collections.emptyMap();
57+
}
58+
59+
/**
60+
* Gets a multimap of all the already registered custom item definitions indexed by the item's extended java item's identifier.
61+
*
62+
* @return a multimap of all the already registered custom item definitions
63+
*/
4864
@NonNull
49-
Map<String, Collection<CustomItemData>> getExistingCustomItems();
65+
Map<String, Collection<CustomItemDefinition>> getExistingCustomItemDefinitions();
5066

5167
/**
5268
* Gets the list of the already registered non-vanilla custom items.
@@ -58,19 +74,31 @@ public interface GeyserDefineCustomItemsEvent extends Event {
5874

5975
/**
6076
* Registers a custom item with a base Java item. This is used to register items with custom textures and properties
61-
* based on NBT data.
77+
* 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.
6278
*
79+
* @deprecated use {@link GeyserDefineCustomItemsEvent#register(String, CustomItemDefinition)}
6380
* @param identifier the base (java) item
6481
* @param customItemData the custom item data to register
6582
* @return if the item was registered
6683
*/
84+
@Deprecated
6785
boolean register(@NonNull String identifier, @NonNull CustomItemData customItemData);
6886

87+
/**
88+
* Registers a custom item with a base Java item. This is used to register items with custom textures and properties
89+
* based on NBT data.
90+
*
91+
* @param identifier the base (java) item
92+
* @param customItemDefinition the custom item definition to register
93+
* @return if the item was registered
94+
*/
95+
boolean register(@NonNull String identifier, @NonNull CustomItemDefinition customItemDefinition);
96+
6997
/**
7098
* Registers a custom item with no base item. This is used for mods.
7199
*
72100
* @param customItemData the custom item data to register
73101
* @return if the item was registered
74102
*/
75103
boolean register(@NonNull NonVanillaCustomItemData customItemData);
76-
}
104+
}

api/src/main/java/org/geysermc/geyser/api/item/custom/v2/CustomItemDefinition.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ public interface CustomItemDefinition {
4141

4242
/**
4343
* 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
44-
* namespace of the implementation is used.
45-
*
46-
* @implNote for Geyser, the default namespace is the {@code geyser_custom} namespace.
44+
* namespace of the implementation is used. For Geyser, the default namespace is the {@code geyser_custom} namespace.
4745
*/
4846
@NonNull Key bedrockIdentifier();
4947

core/src/main/java/org/geysermc/geyser/event/type/GeyserDefineCustomItemsEventImpl.java

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,58 +28,30 @@
2828
import com.google.common.collect.Multimap;
2929
import org.checkerframework.checker.nullness.qual.NonNull;
3030
import org.geysermc.geyser.api.event.lifecycle.GeyserDefineCustomItemsEvent;
31-
import org.geysermc.geyser.api.item.custom.CustomItemData;
3231
import org.geysermc.geyser.api.item.custom.NonVanillaCustomItemData;
32+
import org.geysermc.geyser.api.item.custom.v2.CustomItemDefinition;
3333

3434
import java.util.Collection;
3535
import java.util.Collections;
3636
import java.util.List;
3737
import java.util.Map;
3838

3939
public abstract class GeyserDefineCustomItemsEventImpl implements GeyserDefineCustomItemsEvent {
40-
private final Multimap<String, CustomItemData> customItems;
40+
private final Multimap<String, CustomItemDefinition> customItems;
4141
private final List<NonVanillaCustomItemData> nonVanillaCustomItems;
4242

43-
public GeyserDefineCustomItemsEventImpl(Multimap<String, CustomItemData> customItems, List<NonVanillaCustomItemData> nonVanillaCustomItems) {
43+
public GeyserDefineCustomItemsEventImpl(Multimap<String, CustomItemDefinition> customItems, List<NonVanillaCustomItemData> nonVanillaCustomItems) {
4444
this.customItems = customItems;
4545
this.nonVanillaCustomItems = nonVanillaCustomItems;
4646
}
4747

48-
/**
49-
* Gets a multimap of all the already registered custom items indexed by the item's extended java item's identifier.
50-
*
51-
* @return a multimap of all the already registered custom items
52-
*/
5348
@Override
54-
public @NonNull Map<String, Collection<CustomItemData>> getExistingCustomItems() {
55-
return Collections.unmodifiableMap(this.customItems.asMap());
49+
public @NonNull Map<String, Collection<CustomItemDefinition>> getExistingCustomItemDefinitions() {
50+
return Collections.unmodifiableMap(customItems.asMap());
5651
}
5752

58-
/**
59-
* Gets the list of the already registered non-vanilla custom items.
60-
*
61-
* @return the list of the already registered non-vanilla custom items
62-
*/
6353
@Override
6454
public @NonNull List<NonVanillaCustomItemData> getExistingNonVanillaCustomItems() {
6555
return Collections.unmodifiableList(this.nonVanillaCustomItems);
6656
}
67-
68-
/**
69-
* Registers a custom item with a base Java item. This is used to register items with custom textures and properties
70-
* based on NBT data.
71-
*
72-
* @param identifier the base (java) item
73-
* @param customItemData the custom item data to register
74-
* @return if the item was registered
75-
*/
76-
public abstract boolean register(@NonNull String identifier, @NonNull CustomItemData customItemData);
77-
78-
/**
79-
* Registers a custom item with no base item. This is used for mods.
80-
*
81-
* @param customItemData the custom item data to register
82-
* @return if the item was registered
83-
*/
84-
public abstract boolean register(@NonNull NonVanillaCustomItemData customItemData);
8557
}

core/src/main/java/org/geysermc/geyser/registry/populator/CustomItemRegistryPopulator.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
package org.geysermc.geyser.registry.populator;
2727

2828
import com.google.common.collect.Multimap;
29-
import org.checkerframework.checker.nullness.qual.NonNull;
3029
import org.checkerframework.checker.nullness.qual.Nullable;
3130
import org.cloudburstmc.nbt.NbtMap;
3231
import org.cloudburstmc.nbt.NbtMapBuilder;
@@ -39,7 +38,6 @@
3938
import org.geysermc.geyser.api.item.custom.CustomRenderOffsets;
4039
import org.geysermc.geyser.api.item.custom.NonVanillaCustomItemData;
4140
import org.geysermc.geyser.api.util.TriState;
42-
import org.geysermc.geyser.event.type.GeyserDefineCustomItemsEventImpl;
4341
import org.geysermc.geyser.item.GeyserCustomMappingData;
4442
import org.geysermc.geyser.item.Items;
4543
import org.geysermc.geyser.item.components.WearableSlot;
@@ -52,7 +50,6 @@
5250
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents;
5351

5452
import java.util.ArrayList;
55-
import java.util.Collections;
5653
import java.util.HashMap;
5754
import java.util.List;
5855
import java.util.Map;
@@ -69,35 +66,6 @@ public static void populate(Map<String, GeyserMappingItem> items, Multimap<Strin
6966
//} // TODO
7067
});
7168

72-
GeyserImpl.getInstance().eventBus().fire(new GeyserDefineCustomItemsEventImpl(customItems, nonVanillaCustomItems) {
73-
@Override
74-
public boolean register(@NonNull String identifier, @NonNull CustomItemData customItemData) {
75-
if (CustomItemRegistryPopulator.initialCheck(identifier, customItemData, items)) {
76-
customItems.get(identifier).add(customItemData);
77-
return true;
78-
}
79-
return false;
80-
}
81-
82-
@Override
83-
public boolean register(@NonNull NonVanillaCustomItemData customItemData) {
84-
if (customItemData.identifier().startsWith("minecraft:")) {
85-
GeyserImpl.getInstance().getLogger().error("The custom item " + customItemData.identifier() +
86-
" is attempting to masquerade as a vanilla Minecraft item!");
87-
return false;
88-
}
89-
90-
if (customItemData.javaId() < items.size()) {
91-
// Attempting to overwrite an item that already exists in the protocol
92-
GeyserImpl.getInstance().getLogger().error("The custom item " + customItemData.identifier() +
93-
" is attempting to overwrite a vanilla Minecraft item!");
94-
return false;
95-
}
96-
97-
nonVanillaCustomItems.add(customItemData);
98-
return true;
99-
}
100-
});
10169

10270
int customItemCount = customItems.size() + nonVanillaCustomItems.size();
10371
if (customItemCount > 0) {

core/src/main/java/org/geysermc/geyser/registry/populator/CustomItemRegistryPopulator_v2.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import com.google.common.collect.Multimap;
2929
import net.kyori.adventure.key.Key;
30+
import org.checkerframework.checker.nullness.qual.NonNull;
3031
import org.checkerframework.checker.nullness.qual.Nullable;
3132
import org.cloudburstmc.nbt.NbtMap;
3233
import org.cloudburstmc.nbt.NbtMapBuilder;
@@ -35,12 +36,14 @@
3536
import org.cloudburstmc.protocol.bedrock.data.definitions.SimpleItemDefinition;
3637
import org.cloudburstmc.protocol.bedrock.data.inventory.ComponentItemData;
3738
import org.geysermc.geyser.GeyserImpl;
39+
import org.geysermc.geyser.api.item.custom.CustomItemData;
3840
import org.geysermc.geyser.api.item.custom.CustomRenderOffsets;
3941
import org.geysermc.geyser.api.item.custom.NonVanillaCustomItemData;
4042
import org.geysermc.geyser.api.item.custom.v2.BedrockCreativeTab;
4143
import org.geysermc.geyser.api.item.custom.v2.CustomItemBedrockOptions;
4244
import org.geysermc.geyser.api.item.custom.v2.CustomItemDefinition;
4345
import org.geysermc.geyser.api.item.custom.v2.predicate.CustomItemPredicate;
46+
import org.geysermc.geyser.event.type.GeyserDefineCustomItemsEventImpl;
4447
import org.geysermc.geyser.item.GeyserCustomMappingData;
4548
import org.geysermc.geyser.item.components.WearableSlot;
4649
import org.geysermc.geyser.item.type.Item;
@@ -85,6 +88,30 @@ public static void populate(Map<String, GeyserMappingItem> items, Multimap<Strin
8588
}
8689
});
8790

91+
GeyserImpl.getInstance().eventBus().fire(new GeyserDefineCustomItemsEventImpl(customItems, nonVanillaCustomItems) {
92+
93+
@Override
94+
@Deprecated
95+
public boolean register(@NonNull String identifier, @NonNull CustomItemData customItemData) {
96+
return register(identifier, customItemData.toDefinition(identifier).build());
97+
}
98+
99+
@Override
100+
public boolean register(@NonNull String identifier, @NonNull CustomItemDefinition definition) {
101+
if (initialCheck(identifier, definition, customItems, items)) {
102+
customItems.get(identifier).add(definition);
103+
return true;
104+
}
105+
return false;
106+
}
107+
108+
@Override
109+
public boolean register(@NonNull NonVanillaCustomItemData customItemData) {
110+
// TODO
111+
return false;
112+
}
113+
});
114+
88115
int customItemCount = customItems.size() + nonVanillaCustomItems.size();
89116
if (customItemCount > 0) {
90117
GeyserImpl.getInstance().getLogger().info("Registered " + customItemCount + " custom items");

0 commit comments

Comments
 (0)