mirror of https://github.com/Minestom/Minestom.git
chore: generalize ItemComponent to DataComponent (item component defs are still in ItemComponent)
This commit is contained in:
parent
4257632c6c
commit
a7b8de0a1e
|
@ -6,8 +6,9 @@ import net.kyori.adventure.nbt.TagStringIOExt;
|
|||
import net.minestom.server.command.CommandSender;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||
import net.minestom.server.component.DataComponent;
|
||||
import net.minestom.server.component.DataComponentMap;
|
||||
import net.minestom.server.item.ItemComponent;
|
||||
import net.minestom.server.item.ItemComponentMap;
|
||||
import net.minestom.server.item.ItemStack;
|
||||
import net.minestom.server.item.Material;
|
||||
import net.minestom.server.item.component.CustomData;
|
||||
|
@ -59,14 +60,14 @@ public class ArgumentItemStack extends Argument<ItemStack> {
|
|||
return ItemStack.of(material); // Nothing else, we have our item
|
||||
}
|
||||
|
||||
ItemComponentMap.Builder components = ItemComponentMap.builder();
|
||||
DataComponentMap.Builder components = DataComponentMap.builder();
|
||||
|
||||
// Parse the declared components
|
||||
if (reader.peek() == '[') {
|
||||
reader.consume('[');
|
||||
do {
|
||||
final NamespaceID componentId = reader.readNamespaceId();
|
||||
final ItemComponent<?> component = ItemComponent.fromNamespaceId(componentId);
|
||||
final DataComponent<?> component = DataComponent.fromNamespaceId(componentId);
|
||||
if (component == null)
|
||||
throw new ArgumentSyntaxException("Unknown item component", input, INVALID_COMPONENT);
|
||||
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
package net.minestom.server.component;
|
||||
|
||||
import net.kyori.adventure.nbt.BinaryTag;
|
||||
import net.minestom.server.network.NetworkBuffer;
|
||||
import net.minestom.server.registry.StaticProtocolObject;
|
||||
import net.minestom.server.utils.NamespaceID;
|
||||
import net.minestom.server.utils.nbt.BinaryTagSerializer;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public sealed interface DataComponent<T> extends StaticProtocolObject permits DataComponentImpl {
|
||||
|
||||
@NotNull T read(@NotNull BinaryTag tag);
|
||||
@NotNull BinaryTag write(@NotNull T value);
|
||||
|
||||
@NotNull T read(@NotNull NetworkBuffer reader);
|
||||
void write(@NotNull NetworkBuffer writer, @NotNull T value);
|
||||
|
||||
|
||||
static @Nullable DataComponent<?> fromNamespaceId(@NotNull String namespaceId) {
|
||||
return DataComponentImpl.NAMESPACES.get(namespaceId);
|
||||
}
|
||||
|
||||
static @Nullable DataComponent<?> fromNamespaceId(@NotNull NamespaceID namespaceId) {
|
||||
return fromNamespaceId(namespaceId.asString());
|
||||
}
|
||||
|
||||
static @Nullable DataComponent<?> fromId(int id) {
|
||||
return DataComponentImpl.IDS.get(id);
|
||||
}
|
||||
|
||||
static @NotNull Collection<DataComponent<?>> values() {
|
||||
return DataComponentImpl.NAMESPACES.values();
|
||||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
static <T> DataComponent<T> register(@NotNull String name, @Nullable NetworkBuffer.Type<T> network, @Nullable BinaryTagSerializer<T> nbt) {
|
||||
DataComponent<T> impl = new DataComponentImpl<>(DataComponentImpl.NAMESPACES.size(), NamespaceID.from(name), network, nbt);
|
||||
DataComponentImpl.NAMESPACES.put(impl.name(), impl);
|
||||
DataComponentImpl.IDS.set(impl.id(), impl);
|
||||
return impl;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package net.minestom.server.item;
|
||||
package net.minestom.server.component;
|
||||
|
||||
import net.kyori.adventure.nbt.BinaryTag;
|
||||
import net.minestom.server.network.NetworkBuffer;
|
||||
|
@ -12,21 +12,15 @@ import org.jetbrains.annotations.Nullable;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
record ItemComponentImpl<T>(
|
||||
record DataComponentImpl<T>(
|
||||
int id,
|
||||
@NotNull NamespaceID namespace,
|
||||
@Nullable NetworkBuffer.Type<T> network,
|
||||
@Nullable BinaryTagSerializer<T> nbt
|
||||
) implements ItemComponent<T> {
|
||||
static final Map<String, ItemComponent<?>> NAMESPACES = new HashMap<>(32);
|
||||
static final ObjectArray<ItemComponent<?>> IDS = ObjectArray.singleThread(32);
|
||||
) implements DataComponent<T> {
|
||||
static final Map<String, DataComponent<?>> NAMESPACES = new HashMap<>(32);
|
||||
static final ObjectArray<DataComponent<?>> IDS = ObjectArray.singleThread(32);
|
||||
|
||||
static <T> ItemComponent<T> declare(@NotNull String name, @Nullable NetworkBuffer.Type<T> network, @Nullable BinaryTagSerializer<T> nbt) {
|
||||
ItemComponent<T> impl = new ItemComponentImpl<>(NAMESPACES.size(), NamespaceID.from(name), network, nbt);
|
||||
NAMESPACES.put(impl.name(), impl);
|
||||
IDS.set(impl.id(), impl);
|
||||
return impl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull T read(@NotNull BinaryTag tag) {
|
|
@ -0,0 +1,31 @@
|
|||
package net.minestom.server.component;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface DataComponentMap {
|
||||
|
||||
static @NotNull DataComponentMap.Builder builder() {
|
||||
return new DataComponentMapImpl.BuilderImpl(new Int2ObjectArrayMap<>());
|
||||
}
|
||||
|
||||
boolean has(@NotNull DataComponent<?> component);
|
||||
|
||||
<T> @Nullable T get(@NotNull DataComponent<T> component);
|
||||
|
||||
default <T> @NotNull T get(@NotNull DataComponent<T> component, @NotNull T defaultValue) {
|
||||
T value = get(component);
|
||||
return value != null ? value : defaultValue;
|
||||
}
|
||||
|
||||
interface Builder extends DataComponentMap {
|
||||
|
||||
@NotNull Builder set(@NotNull DataComponent<?> component, @Nullable Object value);
|
||||
|
||||
@NotNull Builder remove(@NotNull DataComponent<?> component);
|
||||
|
||||
@NotNull DataComponentMap build();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,47 +1,47 @@
|
|||
package net.minestom.server.item;
|
||||
package net.minestom.server.component;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public record ItemComponentMapImpl(@NotNull Int2ObjectMap<Object> components) implements ItemComponentMap {
|
||||
public record DataComponentMapImpl(@NotNull Int2ObjectMap<Object> components) implements DataComponentMap {
|
||||
@Override
|
||||
public boolean has(@NotNull ItemComponent<?> component) {
|
||||
public boolean has(@NotNull DataComponent<?> component) {
|
||||
return components.get(component.id()) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> @Nullable T get(@NotNull ItemComponent<T> component) {
|
||||
public <T> @Nullable T get(@NotNull DataComponent<T> component) {
|
||||
return (T) components.get(component.id());
|
||||
}
|
||||
|
||||
public record BuilderImpl(@NotNull Int2ObjectMap<Object> components) implements ItemComponentMap.Builder {
|
||||
public record BuilderImpl(@NotNull Int2ObjectMap<Object> components) implements DataComponentMap.Builder {
|
||||
|
||||
@Override
|
||||
public boolean has(@NotNull ItemComponent<?> component) {
|
||||
public boolean has(@NotNull DataComponent<?> component) {
|
||||
return components.get(component.id()) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> @Nullable T get(@NotNull ItemComponent<T> component) {
|
||||
public <T> @Nullable T get(@NotNull DataComponent<T> component) {
|
||||
return (T) components.get(component.id());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Builder set(@NotNull ItemComponent<?> component, @Nullable Object value) {
|
||||
public @NotNull Builder set(@NotNull DataComponent<?> component, @Nullable Object value) {
|
||||
components.put(component.id(), value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Builder remove(@NotNull ItemComponent<?> component) {
|
||||
public @NotNull Builder remove(@NotNull DataComponent<?> component) {
|
||||
components.remove(component.id());
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ItemComponentMap build() {
|
||||
return new ItemComponentMapImpl(components);
|
||||
public @NotNull DataComponentMap build() {
|
||||
return new DataComponentMapImpl(components);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package net.minestom.server.item;
|
||||
package net.minestom.server.component;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
|
@ -19,14 +19,14 @@ import java.util.Map;
|
|||
*
|
||||
* @param patch
|
||||
*/
|
||||
record ItemComponentPatch(@NotNull Int2ObjectMap<Object> patch) {
|
||||
public record DataComponentPatch(@NotNull Int2ObjectMap<Object> patch) {
|
||||
private static final char REMOVAL_PREFIX = '!';
|
||||
|
||||
public static final ItemComponentPatch EMPTY = new ItemComponentPatch(new Int2ObjectArrayMap<>(0));
|
||||
public static final DataComponentPatch EMPTY = new DataComponentPatch(new Int2ObjectArrayMap<>(0));
|
||||
|
||||
public static final @NotNull NetworkBuffer.Type<ItemComponentPatch> NETWORK_TYPE = new NetworkBuffer.Type<>() {
|
||||
public static final @NotNull NetworkBuffer.Type<DataComponentPatch> NETWORK_TYPE = new NetworkBuffer.Type<>() {
|
||||
@Override
|
||||
public void write(@NotNull NetworkBuffer buffer, ItemComponentPatch value) {
|
||||
public void write(@NotNull NetworkBuffer buffer, DataComponentPatch value) {
|
||||
int added = 0;
|
||||
for (Object o : value.patch.values()) {
|
||||
if (o != null) added++;
|
||||
|
@ -38,7 +38,7 @@ record ItemComponentPatch(@NotNull Int2ObjectMap<Object> patch) {
|
|||
if (entry.getValue() != null) {
|
||||
buffer.write(NetworkBuffer.VAR_INT, entry.getIntKey());
|
||||
//noinspection unchecked
|
||||
ItemComponent<Object> type = (ItemComponent<Object>) ItemComponent.fromId(entry.getIntKey());
|
||||
DataComponent<Object> type = (DataComponent<Object>) DataComponent.fromId(entry.getIntKey());
|
||||
assert type != null;
|
||||
type.write(buffer, entry.getValue());
|
||||
}
|
||||
|
@ -51,15 +51,15 @@ record ItemComponentPatch(@NotNull Int2ObjectMap<Object> patch) {
|
|||
}
|
||||
|
||||
@Override
|
||||
public ItemComponentPatch read(@NotNull NetworkBuffer buffer) {
|
||||
public DataComponentPatch read(@NotNull NetworkBuffer buffer) {
|
||||
int added = buffer.read(NetworkBuffer.VAR_INT);
|
||||
int removed = buffer.read(NetworkBuffer.VAR_INT);
|
||||
Check.stateCondition(added + removed > ItemComponent.values().size() * 2, "Item component patch too large: {0}", added + removed);
|
||||
Check.stateCondition(added + removed > DataComponent.values().size() * 2, "Item component patch too large: {0}", added + removed);
|
||||
Int2ObjectMap<Object> patch = new Int2ObjectArrayMap<>(added + removed);
|
||||
for (int i = 0; i < added; i++) {
|
||||
int id = buffer.read(NetworkBuffer.VAR_INT);
|
||||
//noinspection unchecked
|
||||
ItemComponent<Object> type = (ItemComponent<Object>) ItemComponent.fromId(id);
|
||||
DataComponent<Object> type = (DataComponent<Object>) DataComponent.fromId(id);
|
||||
Check.notNull(type, "Unknown item component id: {0}", id);
|
||||
patch.put(id, type.read(buffer));
|
||||
}
|
||||
|
@ -67,10 +67,10 @@ record ItemComponentPatch(@NotNull Int2ObjectMap<Object> patch) {
|
|||
int id = buffer.read(NetworkBuffer.VAR_INT);
|
||||
patch.put(id, null);
|
||||
}
|
||||
return new ItemComponentPatch(patch);
|
||||
return new DataComponentPatch(patch);
|
||||
}
|
||||
};
|
||||
public static final @NotNull BinaryTagSerializer<ItemComponentPatch> NBT_TYPE = BinaryTagSerializer.COMPOUND.map(
|
||||
public static final @NotNull BinaryTagSerializer<DataComponentPatch> NBT_TYPE = BinaryTagSerializer.COMPOUND.map(
|
||||
tag -> {
|
||||
if (tag.size() == 0) return EMPTY;
|
||||
Int2ObjectMap<Object> patch = new Int2ObjectArrayMap<>(tag.size());
|
||||
|
@ -81,7 +81,7 @@ record ItemComponentPatch(@NotNull Int2ObjectMap<Object> patch) {
|
|||
key = key.substring(1);
|
||||
remove = true;
|
||||
}
|
||||
ItemComponent<?> type = ItemComponent.fromNamespaceId(key);
|
||||
DataComponent<?> type = DataComponent.fromNamespaceId(key);
|
||||
Check.notNull(type, "Unknown item component: {0}", key);
|
||||
if (remove) {
|
||||
patch.put(type.id(), null);
|
||||
|
@ -90,14 +90,14 @@ record ItemComponentPatch(@NotNull Int2ObjectMap<Object> patch) {
|
|||
patch.put(type.id(), value);
|
||||
}
|
||||
}
|
||||
return new ItemComponentPatch(patch);
|
||||
return new DataComponentPatch(patch);
|
||||
},
|
||||
patch -> {
|
||||
if (patch.patch.isEmpty()) return CompoundBinaryTag.empty();
|
||||
CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder();
|
||||
for (Int2ObjectMap.Entry<Object> entry : patch.patch.int2ObjectEntrySet()) {
|
||||
//noinspection unchecked
|
||||
ItemComponent<Object> type = (ItemComponent<Object>) ItemComponent.fromId(entry.getIntKey());
|
||||
DataComponent<Object> type = (DataComponent<Object>) DataComponent.fromId(entry.getIntKey());
|
||||
Check.notNull(type, "Unknown item component id: {0}", entry.getIntKey());
|
||||
if (entry.getValue() == null) {
|
||||
builder.put(REMOVAL_PREFIX + type.name(), CompoundBinaryTag.empty());
|
||||
|
@ -109,11 +109,11 @@ record ItemComponentPatch(@NotNull Int2ObjectMap<Object> patch) {
|
|||
}
|
||||
);
|
||||
|
||||
public static @NotNull ItemComponentPatch from(@NotNull ItemComponentMap prototype, @NotNull ItemComponentMap components) {
|
||||
return new ItemComponentPatch(((ItemComponentMapImpl) components).components());
|
||||
public static @NotNull DataComponentPatch from(@NotNull DataComponentMap prototype, @NotNull DataComponentMap components) {
|
||||
return new DataComponentPatch(((DataComponentMapImpl) components).components());
|
||||
}
|
||||
|
||||
public boolean has(@NotNull ItemComponentMap prototype, @NotNull ItemComponent<?> component) {
|
||||
public boolean has(@NotNull DataComponentMap prototype, @NotNull DataComponent<?> component) {
|
||||
if (patch.containsKey(component.id())) {
|
||||
return patch.get(component.id()) != null;
|
||||
} else {
|
||||
|
@ -121,7 +121,7 @@ record ItemComponentPatch(@NotNull Int2ObjectMap<Object> patch) {
|
|||
}
|
||||
}
|
||||
|
||||
public <T> @Nullable T get(@NotNull ItemComponentMap prototype, @NotNull ItemComponent<T> component) {
|
||||
public <T> @Nullable T get(@NotNull DataComponentMap prototype, @NotNull DataComponent<T> component) {
|
||||
if (patch.containsKey(component.id())) {
|
||||
return (T) patch.get(component.id());
|
||||
} else {
|
||||
|
@ -129,46 +129,46 @@ record ItemComponentPatch(@NotNull Int2ObjectMap<Object> patch) {
|
|||
}
|
||||
}
|
||||
|
||||
public <T> @NotNull ItemComponentPatch with(@NotNull ItemComponent<T> component, @NotNull T value) {
|
||||
public <T> @NotNull DataComponentPatch with(@NotNull DataComponent<T> component, @NotNull T value) {
|
||||
Int2ObjectMap<Object> newPatch = new Int2ObjectArrayMap<>(patch);
|
||||
newPatch.put(component.id(), value);
|
||||
return new ItemComponentPatch(newPatch);
|
||||
return new DataComponentPatch(newPatch);
|
||||
}
|
||||
|
||||
public <T> @NotNull ItemComponentPatch without(@NotNull ItemComponent<T> component) {
|
||||
public <T> @NotNull DataComponentPatch without(@NotNull DataComponent<T> component) {
|
||||
Int2ObjectMap<Object> newPatch = new Int2ObjectArrayMap<>(patch);
|
||||
newPatch.put(component.id(), null);
|
||||
return new ItemComponentPatch(newPatch);
|
||||
return new DataComponentPatch(newPatch);
|
||||
}
|
||||
|
||||
public @NotNull Builder builder() {
|
||||
return new Builder(new Int2ObjectArrayMap<>(patch));
|
||||
}
|
||||
|
||||
record Builder(@NotNull Int2ObjectMap<Object> patch) implements ItemComponentMap {
|
||||
public record Builder(@NotNull Int2ObjectMap<Object> patch) implements DataComponentMap {
|
||||
|
||||
@Override
|
||||
public boolean has(@NotNull ItemComponent<?> component) {
|
||||
public boolean has(@NotNull DataComponent<?> component) {
|
||||
return patch.get(component.id()) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> @Nullable T get(@NotNull ItemComponent<T> component) {
|
||||
public <T> @Nullable T get(@NotNull DataComponent<T> component) {
|
||||
return (T) patch.get(component.id());
|
||||
}
|
||||
|
||||
public <T> ItemComponentPatch.@NotNull Builder set(@NotNull ItemComponent<T> component, @NotNull T value) {
|
||||
public <T> DataComponentPatch.@NotNull Builder set(@NotNull DataComponent<T> component, @NotNull T value) {
|
||||
patch.put(component.id(), value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ItemComponentPatch.@NotNull Builder remove(@NotNull ItemComponent<?> component) {
|
||||
public DataComponentPatch.@NotNull Builder remove(@NotNull DataComponent<?> component) {
|
||||
patch.put(component.id(), null);
|
||||
return this;
|
||||
}
|
||||
|
||||
public @NotNull ItemComponentPatch build() {
|
||||
return new ItemComponentPatch(new Int2ObjectArrayMap<>(this.patch));
|
||||
public @NotNull DataComponentPatch build() {
|
||||
return new DataComponentPatch(new Int2ObjectArrayMap<>(this.patch));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,103 +1,76 @@
|
|||
package net.minestom.server.item;
|
||||
|
||||
import net.kyori.adventure.nbt.BinaryTag;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.minestom.server.color.Color;
|
||||
import net.minestom.server.color.DyeColor;
|
||||
import net.minestom.server.component.DataComponent;
|
||||
import net.minestom.server.item.component.*;
|
||||
import net.minestom.server.network.NetworkBuffer;
|
||||
import net.minestom.server.registry.StaticProtocolObject;
|
||||
import net.minestom.server.utils.NamespaceID;
|
||||
import net.minestom.server.utils.nbt.BinaryTagSerializer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static net.minestom.server.item.ItemComponentImpl.declare;
|
||||
|
||||
public sealed interface ItemComponent<T> extends StaticProtocolObject permits ItemComponentImpl {
|
||||
// Note that even non-networked components are declared here as they still contribute to the component ID counter.
|
||||
public final class ItemComponent {
|
||||
// Note that even non-networked components are registered here as they still contribute to the component ID counter.
|
||||
// The order in this file determines the component protocol IDs, so it is important to match the client.
|
||||
|
||||
ItemComponent<CustomData> CUSTOM_DATA = declare("custom_data", CustomData.NETWORK_TYPE, CustomData.NBT_TYPE);
|
||||
ItemComponent<Integer> MAX_STACK_SIZE = declare("max_stack_size", NetworkBuffer.VAR_INT, BinaryTagSerializer.INT);
|
||||
ItemComponent<Integer> MAX_DAMAGE = declare("max_damage", NetworkBuffer.VAR_INT, BinaryTagSerializer.INT);
|
||||
ItemComponent<Integer> DAMAGE = declare("damage", NetworkBuffer.VAR_INT, BinaryTagSerializer.INT);
|
||||
ItemComponent<Unbreakable> UNBREAKABLE = declare("unbreakable", Unbreakable.NETWORK_TYPE, Unbreakable.NBT_TYPE);
|
||||
ItemComponent<Component> CUSTOM_NAME = declare("custom_name", NetworkBuffer.COMPONENT, BinaryTagSerializer.JSON_COMPONENT);
|
||||
ItemComponent<Component> ITEM_NAME = declare("item_name", NetworkBuffer.COMPONENT, BinaryTagSerializer.JSON_COMPONENT);
|
||||
ItemComponent<List<Component>> LORE = declare("lore", NetworkBuffer.COMPONENT.list(256), BinaryTagSerializer.JSON_COMPONENT.list());
|
||||
ItemComponent<ItemRarity> RARITY = declare("rarity", ItemRarity.NETWORK_TYPE, ItemRarity.NBT_TYPE);
|
||||
ItemComponent<EnchantmentList> ENCHANTMENTS = declare("enchantments", EnchantmentList.NETWORK_TYPE, EnchantmentList.NBT_TYPE);
|
||||
ItemComponent<BlockPredicates> CAN_PLACE_ON = declare("can_place_on", BlockPredicates.NETWORK_TYPE, BlockPredicates.NBT_TYPE);
|
||||
ItemComponent<BlockPredicates> CAN_BREAK = declare("can_break", BlockPredicates.NETWORK_TYPE, BlockPredicates.NBT_TYPE);
|
||||
ItemComponent<AttributeList> ATTRIBUTE_MODIFIERS = declare("attribute_modifiers", AttributeList.NETWORK_TYPE, AttributeList.NBT_TYPE);
|
||||
ItemComponent<Integer> CUSTOM_MODEL_DATA = declare("custom_model_data", NetworkBuffer.VAR_INT, BinaryTagSerializer.INT);
|
||||
ItemComponent<Void> HIDE_ADDITIONAL_TOOLTIP = declare("hide_additional_tooltip", NetworkBuffer.NOTHING, BinaryTagSerializer.NOTHING);
|
||||
ItemComponent<Void> HIDE_TOOLTIP = declare("hide_tooltip", NetworkBuffer.NOTHING, BinaryTagSerializer.NOTHING);
|
||||
ItemComponent<Integer> REPAIR_COST = declare("repair_cost", NetworkBuffer.VAR_INT, BinaryTagSerializer.INT);
|
||||
ItemComponent<Void> CREATIVE_SLOT_LOCK = declare("creative_slot_lock", NetworkBuffer.NOTHING, null);
|
||||
ItemComponent<Boolean> ENCHANTMENT_GLINT_OVERRIDE = declare("enchantment_glint_override", NetworkBuffer.BOOLEAN, BinaryTagSerializer.BOOLEAN);
|
||||
ItemComponent<Void> INTANGIBLE_PROJECTILE = declare("intangible_projectile", null, BinaryTagSerializer.NOTHING);
|
||||
ItemComponent<Food> FOOD = declare("food", Food.NETWORK_TYPE, Food.NBT_TYPE);
|
||||
ItemComponent<Void> FIRE_RESISTANT = declare("fire_resistant", NetworkBuffer.NOTHING, BinaryTagSerializer.NOTHING);
|
||||
ItemComponent<Tool> TOOL = declare("tool", Tool.NETWORK_TYPE, Tool.NBT_TYPE);
|
||||
ItemComponent<EnchantmentList> STORED_ENCHANTMENTS = declare("stored_enchantments", EnchantmentList.NETWORK_TYPE, EnchantmentList.NBT_TYPE);
|
||||
ItemComponent<DyedItemColor> DYED_COLOR = declare("dyed_color", DyedItemColor.NETWORK_TYPE, DyedItemColor.NBT_TYPE);
|
||||
ItemComponent<Color> MAP_COLOR = declare("map_color", NetworkBuffer.COLOR, BinaryTagSerializer.INT.map(Color::new, Color::asRGB));
|
||||
ItemComponent<Integer> MAP_ID = declare("map_id", NetworkBuffer.VAR_INT, BinaryTagSerializer.INT);
|
||||
ItemComponent<MapDecorations> MAP_DECORATIONS = declare("map_decorations", null, MapDecorations.NBT_TYPE);
|
||||
ItemComponent<MapPostProcessing> MAP_POST_PROCESSING = declare("map_post_processing", MapPostProcessing.NETWORK_TYPE, null);
|
||||
ItemComponent<List<ItemStack>> CHARGED_PROJECTILES = declare("charged_projectiles", ItemStack.NETWORK_TYPE.list(Short.MAX_VALUE), BinaryTagSerializer.ITEM.list());
|
||||
ItemComponent<List<ItemStack>> BUNDLE_CONTENTS = declare("bundle_contents", ItemStack.NETWORK_TYPE.list(Short.MAX_VALUE), BinaryTagSerializer.ITEM.list());
|
||||
ItemComponent<PotionContents> POTION_CONTENTS = declare("potion_contents", PotionContents.NETWORK_TYPE, PotionContents.NBT_TYPE);
|
||||
ItemComponent<SuspiciousStewEffects> SUSPICIOUS_STEW_EFFECTS = declare("suspicious_stew_effects", SuspiciousStewEffects.NETWORK_TYPE, SuspiciousStewEffects.NBT_TYPE);
|
||||
ItemComponent<WritableBookContent> WRITABLE_BOOK_CONTENT = declare("writable_book_content", WritableBookContent.NETWORK_TYPE, WritableBookContent.NBT_TYPE);
|
||||
ItemComponent<WrittenBookContent> WRITTEN_BOOK_CONTENT = declare("written_book_content", WrittenBookContent.NETWORK_TYPE, WrittenBookContent.NBT_TYPE);
|
||||
ItemComponent<ArmorTrim> TRIM = declare("trim", ArmorTrim.NETWORK_TYPE, ArmorTrim.NBT_TYPE);
|
||||
ItemComponent<DebugStickState> DEBUG_STICK_STATE = declare("debug_stick_state", null, DebugStickState.NBT_TYPE);
|
||||
ItemComponent<CustomData> ENTITY_DATA = declare("entity_data", CustomData.NETWORK_TYPE, CustomData.NBT_TYPE);
|
||||
ItemComponent<CustomData> BUCKET_ENTITY_DATA = declare("bucket_entity_data", CustomData.NETWORK_TYPE, CustomData.NBT_TYPE);
|
||||
ItemComponent<CustomData> BLOCK_ENTITY_DATA = declare("block_entity_data", CustomData.NETWORK_TYPE, CustomData.NBT_TYPE);
|
||||
ItemComponent<String> INSTRUMENT = declare("instrument", NetworkBuffer.STRING, BinaryTagSerializer.STRING);
|
||||
ItemComponent<Integer> OMINOUS_BOTTLE_AMPLIFIER = declare("ominous_bottle_amplifier", NetworkBuffer.VAR_INT, BinaryTagSerializer.INT);
|
||||
ItemComponent<List<String>> RECIPES = declare("recipes", NetworkBuffer.STRING.list(Short.MAX_VALUE), BinaryTagSerializer.STRING.list());
|
||||
ItemComponent<LodestoneTracker> LODESTONE_TRACKER = declare("lodestone_tracker", LodestoneTracker.NETWORK_TYPE, LodestoneTracker.NBT_TYPE);
|
||||
ItemComponent<FireworkExplosion> FIREWORK_EXPLOSION = declare("firework_explosion", FireworkExplosion.NETWORK_TYPE, FireworkExplosion.NBT_TYPE);
|
||||
ItemComponent<FireworkList> FIREWORKS = declare("fireworks", FireworkList.NETWORK_TYPE, FireworkList.NBT_TYPE);
|
||||
ItemComponent<HeadProfile> PROFILE = declare("profile", HeadProfile.NETWORK_TYPE, HeadProfile.NBT_TYPE);
|
||||
ItemComponent<String> NOTE_BLOCK_SOUND = declare("note_block_sound", NetworkBuffer.STRING, BinaryTagSerializer.STRING);
|
||||
ItemComponent<BannerPatterns> BANNER_PATTERNS = declare("banner_patterns", BannerPatterns.NETWORK_TYPE, BannerPatterns.NBT_TYPE);
|
||||
ItemComponent<DyeColor> BASE_COLOR = declare("base_color", DyeColor.NETWORK_TYPE, DyeColor.NBT_TYPE);
|
||||
ItemComponent<PotDecorations> POT_DECORATIONS = declare("pot_decorations", PotDecorations.NETWORK_TYPE, PotDecorations.NBT_TYPE);
|
||||
ItemComponent<List<ItemStack>> CONTAINER = declare("container", ItemStack.NETWORK_TYPE.list(256), BinaryTagSerializer.ITEM.list());
|
||||
ItemComponent<ItemBlockState> BLOCK_STATE = declare("block_state", ItemBlockState.NETWORK_TYPE, ItemBlockState.NBT_TYPE);
|
||||
ItemComponent<List<Bee>> BEES = declare("bees", Bee.NETWORK_TYPE.list(Short.MAX_VALUE), Bee.NBT_TYPE.list());
|
||||
ItemComponent<String> LOCK = declare("lock", null, BinaryTagSerializer.STRING);
|
||||
ItemComponent<SeededContainerLoot> CONTAINER_LOOT = declare("container_loot", null, SeededContainerLoot.NBT_TYPE);
|
||||
public static final DataComponent<CustomData> CUSTOM_DATA = DataComponent.register("custom_data", CustomData.NETWORK_TYPE, CustomData.NBT_TYPE);
|
||||
public static final DataComponent<Integer> MAX_STACK_SIZE = DataComponent.register("max_stack_size", NetworkBuffer.VAR_INT, BinaryTagSerializer.INT);
|
||||
public static final DataComponent<Integer> MAX_DAMAGE = DataComponent.register("max_damage", NetworkBuffer.VAR_INT, BinaryTagSerializer.INT);
|
||||
public static final DataComponent<Integer> DAMAGE = DataComponent.register("damage", NetworkBuffer.VAR_INT, BinaryTagSerializer.INT);
|
||||
public static final DataComponent<Unbreakable> UNBREAKABLE = DataComponent.register("unbreakable", Unbreakable.NETWORK_TYPE, Unbreakable.NBT_TYPE);
|
||||
public static final DataComponent<Component> CUSTOM_NAME = DataComponent.register("custom_name", NetworkBuffer.COMPONENT, BinaryTagSerializer.JSON_COMPONENT);
|
||||
public static final DataComponent<Component> ITEM_NAME = DataComponent.register("item_name", NetworkBuffer.COMPONENT, BinaryTagSerializer.JSON_COMPONENT);
|
||||
public static final DataComponent<List<Component>> LORE = DataComponent.register("lore", NetworkBuffer.COMPONENT.list(256), BinaryTagSerializer.JSON_COMPONENT.list());
|
||||
public static final DataComponent<ItemRarity> RARITY = DataComponent.register("rarity", ItemRarity.NETWORK_TYPE, ItemRarity.NBT_TYPE);
|
||||
public static final DataComponent<EnchantmentList> ENCHANTMENTS = DataComponent.register("enchantments", EnchantmentList.NETWORK_TYPE, EnchantmentList.NBT_TYPE);
|
||||
public static final DataComponent<BlockPredicates> CAN_PLACE_ON = DataComponent.register("can_place_on", BlockPredicates.NETWORK_TYPE, BlockPredicates.NBT_TYPE);
|
||||
public static final DataComponent<BlockPredicates> CAN_BREAK = DataComponent.register("can_break", BlockPredicates.NETWORK_TYPE, BlockPredicates.NBT_TYPE);
|
||||
public static final DataComponent<AttributeList> ATTRIBUTE_MODIFIERS = DataComponent.register("attribute_modifiers", AttributeList.NETWORK_TYPE, AttributeList.NBT_TYPE);
|
||||
public static final DataComponent<Integer> CUSTOM_MODEL_DATA = DataComponent.register("custom_model_data", NetworkBuffer.VAR_INT, BinaryTagSerializer.INT);
|
||||
public static final DataComponent<Void> HIDE_ADDITIONAL_TOOLTIP = DataComponent.register("hide_additional_tooltip", NetworkBuffer.NOTHING, BinaryTagSerializer.NOTHING);
|
||||
public static final DataComponent<Void> HIDE_TOOLTIP = DataComponent.register("hide_tooltip", NetworkBuffer.NOTHING, BinaryTagSerializer.NOTHING);
|
||||
public static final DataComponent<Integer> REPAIR_COST = DataComponent.register("repair_cost", NetworkBuffer.VAR_INT, BinaryTagSerializer.INT);
|
||||
public static final DataComponent<Void> CREATIVE_SLOT_LOCK = DataComponent.register("creative_slot_lock", NetworkBuffer.NOTHING, null);
|
||||
public static final DataComponent<Boolean> ENCHANTMENT_GLINT_OVERRIDE = DataComponent.register("enchantment_glint_override", NetworkBuffer.BOOLEAN, BinaryTagSerializer.BOOLEAN);
|
||||
public static final DataComponent<Void> INTANGIBLE_PROJECTILE = DataComponent.register("intangible_projectile", null, BinaryTagSerializer.NOTHING);
|
||||
public static final DataComponent<Food> FOOD = DataComponent.register("food", Food.NETWORK_TYPE, Food.NBT_TYPE);
|
||||
public static final DataComponent<Void> FIRE_RESISTANT = DataComponent.register("fire_resistant", NetworkBuffer.NOTHING, BinaryTagSerializer.NOTHING);
|
||||
public static final DataComponent<Tool> TOOL = DataComponent.register("tool", Tool.NETWORK_TYPE, Tool.NBT_TYPE);
|
||||
public static final DataComponent<EnchantmentList> STORED_ENCHANTMENTS = DataComponent.register("stored_enchantments", EnchantmentList.NETWORK_TYPE, EnchantmentList.NBT_TYPE);
|
||||
public static final DataComponent<DyedItemColor> DYED_COLOR = DataComponent.register("dyed_color", DyedItemColor.NETWORK_TYPE, DyedItemColor.NBT_TYPE);
|
||||
public static final DataComponent<Color> MAP_COLOR = DataComponent.register("map_color", NetworkBuffer.COLOR, BinaryTagSerializer.INT.map(Color::new, Color::asRGB));
|
||||
public static final DataComponent<Integer> MAP_ID = DataComponent.register("map_id", NetworkBuffer.VAR_INT, BinaryTagSerializer.INT);
|
||||
public static final DataComponent<MapDecorations> MAP_DECORATIONS = DataComponent.register("map_decorations", null, MapDecorations.NBT_TYPE);
|
||||
public static final DataComponent<MapPostProcessing> MAP_POST_PROCESSING = DataComponent.register("map_post_processing", MapPostProcessing.NETWORK_TYPE, null);
|
||||
public static final DataComponent<List<ItemStack>> CHARGED_PROJECTILES = DataComponent.register("charged_projectiles", ItemStack.NETWORK_TYPE.list(Short.MAX_VALUE), BinaryTagSerializer.ITEM.list());
|
||||
public static final DataComponent<List<ItemStack>> BUNDLE_CONTENTS = DataComponent.register("bundle_contents", ItemStack.NETWORK_TYPE.list(Short.MAX_VALUE), BinaryTagSerializer.ITEM.list());
|
||||
public static final DataComponent<PotionContents> POTION_CONTENTS = DataComponent.register("potion_contents", PotionContents.NETWORK_TYPE, PotionContents.NBT_TYPE);
|
||||
public static final DataComponent<SuspiciousStewEffects> SUSPICIOUS_STEW_EFFECTS = DataComponent.register("suspicious_stew_effects", SuspiciousStewEffects.NETWORK_TYPE, SuspiciousStewEffects.NBT_TYPE);
|
||||
public static final DataComponent<WritableBookContent> WRITABLE_BOOK_CONTENT = DataComponent.register("writable_book_content", WritableBookContent.NETWORK_TYPE, WritableBookContent.NBT_TYPE);
|
||||
public static final DataComponent<WrittenBookContent> WRITTEN_BOOK_CONTENT = DataComponent.register("written_book_content", WrittenBookContent.NETWORK_TYPE, WrittenBookContent.NBT_TYPE);
|
||||
public static final DataComponent<ArmorTrim> TRIM = DataComponent.register("trim", ArmorTrim.NETWORK_TYPE, ArmorTrim.NBT_TYPE);
|
||||
public static final DataComponent<DebugStickState> DEBUG_STICK_STATE = DataComponent.register("debug_stick_state", null, DebugStickState.NBT_TYPE);
|
||||
public static final DataComponent<CustomData> ENTITY_DATA = DataComponent.register("entity_data", CustomData.NETWORK_TYPE, CustomData.NBT_TYPE);
|
||||
public static final DataComponent<CustomData> BUCKET_ENTITY_DATA = DataComponent.register("bucket_entity_data", CustomData.NETWORK_TYPE, CustomData.NBT_TYPE);
|
||||
public static final DataComponent<CustomData> BLOCK_ENTITY_DATA = DataComponent.register("block_entity_data", CustomData.NETWORK_TYPE, CustomData.NBT_TYPE);
|
||||
public static final DataComponent<String> INSTRUMENT = DataComponent.register("instrument", NetworkBuffer.STRING, BinaryTagSerializer.STRING);
|
||||
public static final DataComponent<Integer> OMINOUS_BOTTLE_AMPLIFIER = DataComponent.register("ominous_bottle_amplifier", NetworkBuffer.VAR_INT, BinaryTagSerializer.INT);
|
||||
public static final DataComponent<List<String>> RECIPES = DataComponent.register("recipes", NetworkBuffer.STRING.list(Short.MAX_VALUE), BinaryTagSerializer.STRING.list());
|
||||
public static final DataComponent<LodestoneTracker> LODESTONE_TRACKER = DataComponent.register("lodestone_tracker", LodestoneTracker.NETWORK_TYPE, LodestoneTracker.NBT_TYPE);
|
||||
public static final DataComponent<FireworkExplosion> FIREWORK_EXPLOSION = DataComponent.register("firework_explosion", FireworkExplosion.NETWORK_TYPE, FireworkExplosion.NBT_TYPE);
|
||||
public static final DataComponent<FireworkList> FIREWORKS = DataComponent.register("fireworks", FireworkList.NETWORK_TYPE, FireworkList.NBT_TYPE);
|
||||
public static final DataComponent<HeadProfile> PROFILE = DataComponent.register("profile", HeadProfile.NETWORK_TYPE, HeadProfile.NBT_TYPE);
|
||||
public static final DataComponent<String> NOTE_BLOCK_SOUND = DataComponent.register("note_block_sound", NetworkBuffer.STRING, BinaryTagSerializer.STRING);
|
||||
public static final DataComponent<BannerPatterns> BANNER_PATTERNS = DataComponent.register("banner_patterns", BannerPatterns.NETWORK_TYPE, BannerPatterns.NBT_TYPE);
|
||||
public static final DataComponent<DyeColor> BASE_COLOR = DataComponent.register("base_color", DyeColor.NETWORK_TYPE, DyeColor.NBT_TYPE);
|
||||
public static final DataComponent<PotDecorations> POT_DECORATIONS = DataComponent.register("pot_decorations", PotDecorations.NETWORK_TYPE, PotDecorations.NBT_TYPE);
|
||||
public static final DataComponent<List<ItemStack>> CONTAINER = DataComponent.register("container", ItemStack.NETWORK_TYPE.list(256), BinaryTagSerializer.ITEM.list());
|
||||
public static final DataComponent<ItemBlockState> BLOCK_STATE = DataComponent.register("block_state", ItemBlockState.NETWORK_TYPE, ItemBlockState.NBT_TYPE);
|
||||
public static final DataComponent<List<Bee>> BEES = DataComponent.register("bees", Bee.NETWORK_TYPE.list(Short.MAX_VALUE), Bee.NBT_TYPE.list());
|
||||
public static final DataComponent<String> LOCK = DataComponent.register("lock", null, BinaryTagSerializer.STRING);
|
||||
public static final DataComponent<SeededContainerLoot> CONTAINER_LOOT = DataComponent.register("container_loot", null, SeededContainerLoot.NBT_TYPE);
|
||||
|
||||
@NotNull T read(@NotNull BinaryTag tag);
|
||||
@NotNull BinaryTag write(@NotNull T value);
|
||||
|
||||
@NotNull T read(@NotNull NetworkBuffer reader);
|
||||
void write(@NotNull NetworkBuffer writer, @NotNull T value);
|
||||
|
||||
|
||||
static @Nullable ItemComponent<?> fromNamespaceId(@NotNull String namespaceId) {
|
||||
return ItemComponentImpl.NAMESPACES.get(namespaceId);
|
||||
}
|
||||
|
||||
static @Nullable ItemComponent<?> fromNamespaceId(@NotNull NamespaceID namespaceId) {
|
||||
return fromNamespaceId(namespaceId.asString());
|
||||
}
|
||||
|
||||
static @Nullable ItemComponent<?> fromId(int id) {
|
||||
return ItemComponentImpl.IDS.get(id);
|
||||
}
|
||||
|
||||
static @NotNull Collection<ItemComponent<?>> values() {
|
||||
return ItemComponentImpl.NAMESPACES.values();
|
||||
private ItemComponent() {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
package net.minestom.server.item;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface ItemComponentMap {
|
||||
|
||||
static @NotNull ItemComponentMap.Builder builder() {
|
||||
return new ItemComponentMapImpl.BuilderImpl(new Int2ObjectArrayMap<>());
|
||||
}
|
||||
|
||||
boolean has(@NotNull ItemComponent<?> component);
|
||||
|
||||
<T> @Nullable T get(@NotNull ItemComponent<T> component);
|
||||
|
||||
default <T> @NotNull T get(@NotNull ItemComponent<T> component, @NotNull T defaultValue) {
|
||||
T value = get(component);
|
||||
return value != null ? value : defaultValue;
|
||||
}
|
||||
|
||||
interface Builder extends ItemComponentMap {
|
||||
|
||||
@NotNull Builder set(@NotNull ItemComponent<?> component, @Nullable Object value);
|
||||
|
||||
@NotNull Builder remove(@NotNull ItemComponent<?> component);
|
||||
|
||||
@NotNull ItemComponentMap build();
|
||||
}
|
||||
|
||||
}
|
|
@ -5,6 +5,9 @@ import net.kyori.adventure.nbt.api.BinaryTagHolder;
|
|||
import net.kyori.adventure.text.event.HoverEvent;
|
||||
import net.kyori.adventure.text.event.HoverEventSource;
|
||||
import net.minestom.server.adventure.MinestomAdventure;
|
||||
import net.minestom.server.component.DataComponent;
|
||||
import net.minestom.server.component.DataComponentMap;
|
||||
import net.minestom.server.component.DataComponentPatch;
|
||||
import net.minestom.server.inventory.ContainerInventory;
|
||||
import net.minestom.server.item.component.CustomData;
|
||||
import net.minestom.server.network.NetworkBuffer;
|
||||
|
@ -28,7 +31,7 @@ import java.util.function.UnaryOperator;
|
|||
* <p>
|
||||
* An item stack cannot be null, {@link ItemStack#AIR} should be used instead.
|
||||
*/
|
||||
public sealed interface ItemStack extends TagReadable, ItemComponentMap, HoverEventSource<HoverEvent.ShowItem>
|
||||
public sealed interface ItemStack extends TagReadable, DataComponentMap, HoverEventSource<HoverEvent.ShowItem>
|
||||
permits ItemStackImpl {
|
||||
|
||||
/**
|
||||
|
@ -56,13 +59,13 @@ public sealed interface ItemStack extends TagReadable, ItemComponentMap, HoverEv
|
|||
}
|
||||
|
||||
@Contract(value = "_ ,_ -> new", pure = true)
|
||||
static @NotNull ItemStack of(@NotNull Material material, @NotNull ItemComponentMap components) {
|
||||
return ItemStackImpl.create(material, 1, ItemComponentPatch.from(material.registry().prototype(), components));
|
||||
static @NotNull ItemStack of(@NotNull Material material, @NotNull DataComponentMap components) {
|
||||
return ItemStackImpl.create(material, 1, DataComponentPatch.from(material.registry().prototype(), components));
|
||||
}
|
||||
|
||||
@Contract(value = "_ ,_, _ -> new", pure = true)
|
||||
static @NotNull ItemStack of(@NotNull Material material, int amount, @NotNull ItemComponentMap components) {
|
||||
return ItemStackImpl.create(material, amount, ItemComponentPatch.from(material.registry().prototype(), components));
|
||||
static @NotNull ItemStack of(@NotNull Material material, int amount, @NotNull DataComponentMap components) {
|
||||
return ItemStackImpl.create(material, amount, DataComponentPatch.from(material.registry().prototype(), components));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -95,16 +98,16 @@ public sealed interface ItemStack extends TagReadable, ItemComponentMap, HoverEv
|
|||
}
|
||||
|
||||
@Contract(value = "_, _ -> new", pure = true)
|
||||
<T> @NotNull ItemStack with(@NotNull ItemComponent<T> component, T value);
|
||||
<T> @NotNull ItemStack with(@NotNull DataComponent<T> component, T value);
|
||||
|
||||
default <T> @NotNull ItemStack with(@NotNull ItemComponent<T> component, @NotNull UnaryOperator<T> operator) {
|
||||
default <T> @NotNull ItemStack with(@NotNull DataComponent<T> component, @NotNull UnaryOperator<T> operator) {
|
||||
T value = get(component);
|
||||
if (value == null) return this;
|
||||
return with(component, operator.apply(value));
|
||||
}
|
||||
|
||||
@Contract(value = "_, -> new", pure = true)
|
||||
@NotNull ItemStack without(@NotNull ItemComponent<?> component);
|
||||
@NotNull ItemStack without(@NotNull DataComponent<?> component);
|
||||
|
||||
@Contract(value = "_, _ -> new", pure = true)
|
||||
default <T> @NotNull ItemStack withTag(@NotNull Tag<T> tag, @Nullable T value) {
|
||||
|
@ -157,10 +160,10 @@ public sealed interface ItemStack extends TagReadable, ItemComponentMap, HoverEv
|
|||
@NotNull Builder amount(int amount);
|
||||
|
||||
@Contract(value = "_, _ -> this")
|
||||
<T> @NotNull Builder set(@NotNull ItemComponent<T> component, T value);
|
||||
<T> @NotNull Builder set(@NotNull DataComponent<T> component, T value);
|
||||
|
||||
@Contract(value = "_ -> this")
|
||||
@NotNull Builder remove(@NotNull ItemComponent<?> component);
|
||||
@NotNull Builder remove(@NotNull DataComponent<?> component);
|
||||
|
||||
@Contract(value = "_, _ -> this")
|
||||
default <T> @NotNull Builder set(@NotNull Tag<T> tag, @Nullable T value) {
|
||||
|
|
|
@ -2,6 +2,8 @@ package net.minestom.server.item;
|
|||
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
|
||||
import net.kyori.adventure.nbt.CompoundBinaryTag;
|
||||
import net.minestom.server.component.DataComponent;
|
||||
import net.minestom.server.component.DataComponentPatch;
|
||||
import net.minestom.server.item.component.CustomData;
|
||||
import net.minestom.server.network.NetworkBuffer;
|
||||
import net.minestom.server.tag.Tag;
|
||||
|
@ -13,7 +15,7 @@ import org.jetbrains.annotations.Nullable;
|
|||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
record ItemStackImpl(Material material, int amount, ItemComponentPatch components) implements ItemStack {
|
||||
record ItemStackImpl(Material material, int amount, DataComponentPatch components) implements ItemStack {
|
||||
|
||||
static final NetworkBuffer.Type<ItemStack> NETWORK_TYPE = new NetworkBuffer.Type<>() {
|
||||
@Override
|
||||
|
@ -25,7 +27,7 @@ record ItemStackImpl(Material material, int amount, ItemComponentPatch component
|
|||
|
||||
buffer.write(NetworkBuffer.VAR_INT, value.amount());
|
||||
buffer.write(NetworkBuffer.VAR_INT, value.material().id());
|
||||
buffer.write(ItemComponentPatch.NETWORK_TYPE, ((ItemStackImpl) value).components);
|
||||
buffer.write(DataComponentPatch.NETWORK_TYPE, ((ItemStackImpl) value).components);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -33,7 +35,7 @@ record ItemStackImpl(Material material, int amount, ItemComponentPatch component
|
|||
int amount = buffer.read(NetworkBuffer.VAR_INT);
|
||||
if (amount <= 0) return ItemStack.AIR;
|
||||
Material material = Material.fromId(buffer.read(NetworkBuffer.VAR_INT));
|
||||
ItemComponentPatch components = buffer.read(ItemComponentPatch.NETWORK_TYPE);
|
||||
DataComponentPatch components = buffer.read(DataComponentPatch.NETWORK_TYPE);
|
||||
return new ItemStackImpl(material, amount, components);
|
||||
}
|
||||
};
|
||||
|
@ -46,13 +48,13 @@ record ItemStackImpl(Material material, int amount, ItemComponentPatch component
|
|||
});
|
||||
static final BinaryTagSerializer<ItemStack> NBT_TYPE = BinaryTagSerializer.COMPOUND.map(ItemStackImpl::fromCompound, ItemStackImpl::toCompound);
|
||||
|
||||
static ItemStack create(Material material, int amount, ItemComponentPatch components) {
|
||||
static ItemStack create(Material material, int amount, DataComponentPatch components) {
|
||||
if (amount <= 0) return AIR;
|
||||
return new ItemStackImpl(material, amount, components);
|
||||
}
|
||||
|
||||
static ItemStack create(Material material, int amount) {
|
||||
return create(material, amount, ItemComponentPatch.EMPTY);
|
||||
return create(material, amount, DataComponentPatch.EMPTY);
|
||||
}
|
||||
|
||||
public ItemStackImpl {
|
||||
|
@ -60,12 +62,12 @@ record ItemStackImpl(Material material, int amount, ItemComponentPatch component
|
|||
}
|
||||
|
||||
@Override
|
||||
public <T> @Nullable T get(@NotNull ItemComponent<T> component) {
|
||||
public <T> @Nullable T get(@NotNull DataComponent<T> component) {
|
||||
return components.get(material.prototype(), component);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean has(@NotNull ItemComponent<?> component) {
|
||||
public boolean has(@NotNull DataComponent<?> component) {
|
||||
return components.has(material.prototype(), component);
|
||||
}
|
||||
|
||||
|
@ -88,12 +90,12 @@ record ItemStackImpl(Material material, int amount, ItemComponentPatch component
|
|||
}
|
||||
|
||||
@Override
|
||||
public @NotNull <T> ItemStack with(@NotNull ItemComponent<T> component, T value) {
|
||||
public @NotNull <T> ItemStack with(@NotNull DataComponent<T> component, T value) {
|
||||
return new ItemStackImpl(material, amount, components.with(component, value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ItemStack without(@NotNull ItemComponent<?> component) {
|
||||
public @NotNull ItemStack without(@NotNull DataComponent<?> component) {
|
||||
return new ItemStackImpl(material, amount, components.without(component));
|
||||
}
|
||||
|
||||
|
@ -122,7 +124,7 @@ record ItemStackImpl(Material material, int amount, ItemComponentPatch component
|
|||
Material material = Material.fromNamespaceId(id);
|
||||
Check.notNull(material, "Unknown material: {0}", id);
|
||||
int count = tag.getInt("count", 1);
|
||||
ItemComponentPatch patch = ItemComponentPatch.NBT_TYPE.read(tag.getCompound("components"));
|
||||
DataComponentPatch patch = DataComponentPatch.NBT_TYPE.read(tag.getCompound("components"));
|
||||
return new ItemStackImpl(material, count, patch);
|
||||
}
|
||||
|
||||
|
@ -131,7 +133,7 @@ record ItemStackImpl(Material material, int amount, ItemComponentPatch component
|
|||
tag.putString("id", itemStack.material().name());
|
||||
tag.putInt("count", itemStack.amount());
|
||||
|
||||
CompoundBinaryTag components = (CompoundBinaryTag) ItemComponentPatch.NBT_TYPE.write(((ItemStackImpl) itemStack).components);
|
||||
CompoundBinaryTag components = (CompoundBinaryTag) DataComponentPatch.NBT_TYPE.write(((ItemStackImpl) itemStack).components);
|
||||
if (components.size() > 0) tag.put("components", components);
|
||||
|
||||
return tag.build();
|
||||
|
@ -140,9 +142,9 @@ record ItemStackImpl(Material material, int amount, ItemComponentPatch component
|
|||
static final class Builder implements ItemStack.Builder {
|
||||
final Material material;
|
||||
int amount;
|
||||
ItemComponentPatch.Builder components;
|
||||
DataComponentPatch.Builder components;
|
||||
|
||||
Builder(Material material, int amount, ItemComponentPatch.Builder components) {
|
||||
Builder(Material material, int amount, DataComponentPatch.Builder components) {
|
||||
this.material = material;
|
||||
this.amount = amount;
|
||||
this.components = components;
|
||||
|
@ -151,7 +153,7 @@ record ItemStackImpl(Material material, int amount, ItemComponentPatch component
|
|||
Builder(Material material, int amount) {
|
||||
this.material = material;
|
||||
this.amount = amount;
|
||||
this.components = new ItemComponentPatch.Builder(new Int2ObjectArrayMap<>());
|
||||
this.components = new DataComponentPatch.Builder(new Int2ObjectArrayMap<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -161,13 +163,13 @@ record ItemStackImpl(Material material, int amount, ItemComponentPatch component
|
|||
}
|
||||
|
||||
@Override
|
||||
public <T> ItemStack.@NotNull Builder set(@NotNull ItemComponent<T> component, T value) {
|
||||
public <T> ItemStack.@NotNull Builder set(@NotNull DataComponent<T> component, T value) {
|
||||
components.set(component, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack.@NotNull Builder remove(@NotNull ItemComponent<?> component) {
|
||||
public ItemStack.@NotNull Builder remove(@NotNull DataComponent<?> component) {
|
||||
components.remove(component);
|
||||
return this;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package net.minestom.server.item;
|
||||
|
||||
import net.minestom.server.component.DataComponentMap;
|
||||
import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.network.NetworkBuffer;
|
||||
import net.minestom.server.registry.Registry;
|
||||
|
@ -76,7 +77,7 @@ public sealed interface Material extends StaticProtocolObject, Materials permits
|
|||
return registry().block();
|
||||
}
|
||||
|
||||
default @NotNull ItemComponentMap prototype() {
|
||||
default @NotNull DataComponentMap prototype() {
|
||||
return registry().prototype();
|
||||
}
|
||||
|
||||
|
|
|
@ -10,12 +10,12 @@ import net.minestom.server.MinecraftServer;
|
|||
import net.minestom.server.collision.BoundingBox;
|
||||
import net.minestom.server.collision.CollisionUtils;
|
||||
import net.minestom.server.collision.Shape;
|
||||
import net.minestom.server.component.DataComponent;
|
||||
import net.minestom.server.component.DataComponentMap;
|
||||
import net.minestom.server.entity.EntitySpawnType;
|
||||
import net.minestom.server.entity.EntityType;
|
||||
import net.minestom.server.entity.EquipmentSlot;
|
||||
import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.item.ItemComponent;
|
||||
import net.minestom.server.item.ItemComponentMap;
|
||||
import net.minestom.server.item.Material;
|
||||
import net.minestom.server.utils.NamespaceID;
|
||||
import net.minestom.server.utils.collection.ObjectArray;
|
||||
|
@ -481,7 +481,7 @@ public final class Registry {
|
|||
private final int id;
|
||||
private final String translationKey;
|
||||
private final Supplier<Block> blockSupplier;
|
||||
private ItemComponentMap prototype;
|
||||
private DataComponentMap prototype;
|
||||
|
||||
private final EquipmentSlot equipmentSlot;
|
||||
private final EntityType entityType;
|
||||
|
@ -537,13 +537,13 @@ public final class Registry {
|
|||
return blockSupplier.get();
|
||||
}
|
||||
|
||||
public @NotNull ItemComponentMap prototype() {
|
||||
public @NotNull DataComponentMap prototype() {
|
||||
if (prototype == null) {
|
||||
try {
|
||||
ItemComponentMap.Builder builder = ItemComponentMap.builder();
|
||||
DataComponentMap.Builder builder = DataComponentMap.builder();
|
||||
for (Map.Entry<String, Object> entry : main.section("components")) {
|
||||
//noinspection unchecked
|
||||
ItemComponent<Object> component = (ItemComponent<Object>) ItemComponent.fromNamespaceId(entry.getKey());
|
||||
DataComponent<Object> component = (DataComponent<Object>) DataComponent.fromNamespaceId(entry.getKey());
|
||||
Check.notNull(component, "Unknown component {0} in {1}", entry.getKey(), namespace);
|
||||
|
||||
BinaryTag tag = TagStringIOExt.readTag((String) entry.getValue());
|
||||
|
|
|
@ -3,6 +3,7 @@ package net.minestom.server.item;
|
|||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.component.DataComponent;
|
||||
import net.minestom.server.entity.EntityType;
|
||||
import net.minestom.server.item.component.EnchantmentList;
|
||||
import net.minestom.server.item.enchant.Enchantment;
|
||||
|
@ -27,7 +28,7 @@ public class ItemTest {
|
|||
|
||||
// Should have the exact same components as the material prototype
|
||||
var prototype = Material.DIAMOND_SWORD.registry().prototype();
|
||||
for (ItemComponent<?> component : ItemComponent.values()) {
|
||||
for (DataComponent<?> component : DataComponent.values()) {
|
||||
var proto = prototype.get(component);
|
||||
if (proto == null) {
|
||||
assertFalse(item.has(component), "Item should not have component " + component);
|
||||
|
@ -52,7 +53,7 @@ public class ItemTest {
|
|||
|
||||
// Should have the exact same components as the material prototype
|
||||
var prototype = Material.DIAMOND_SWORD.registry().prototype();
|
||||
for (ItemComponent<?> component : ItemComponent.values()) {
|
||||
for (DataComponent<?> component : DataComponent.values()) {
|
||||
var proto = prototype.get(component);
|
||||
if (proto == null) {
|
||||
assertFalse(item.has(component), "Item should not have component " + component);
|
||||
|
|
Loading…
Reference in New Issue