9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-24 17:39:30 +00:00

完善发包组件

This commit is contained in:
XiaoMoMi
2025-05-28 02:17:29 +08:00
parent 4e81a63d03
commit 6ff6fb19c8
24 changed files with 198 additions and 155 deletions

View File

@@ -24,6 +24,7 @@ import net.momirealms.craftengine.core.registry.WritableRegistry;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.util.ResourceKey;
import net.momirealms.craftengine.core.util.VersionHelper;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
@@ -48,7 +49,7 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
private final ItemEventListener itemEventListener;
private final DebugStickListener debugStickListener;
private final ArmorEventListener armorEventListener;
private final NetworkItemHandler networkItemHandler;
private final NetworkItemHandler<ItemStack> networkItemHandler;
public BukkitItemManager(BukkitCraftEngine plugin) {
super(plugin);
@@ -58,7 +59,7 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
this.itemEventListener = new ItemEventListener(plugin);
this.debugStickListener = new DebugStickListener(plugin);
this.armorEventListener = new ArmorEventListener();
this.networkItemHandler = new ModernNetworkItemHandler(this);
this.networkItemHandler = VersionHelper.isOrAbove1_20_5() ? new ModernNetworkItemHandler(this) : new LegacyNetworkItemHandler(this);
this.registerAllVanillaItems();
}
@@ -75,7 +76,9 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
public Optional<ItemStack> s2c(ItemStack itemStack, ItemBuildContext context) {
try {
return this.networkItemHandler.s2c(itemStack, context);
Item<ItemStack> wrapped = wrap(itemStack);
if (wrapped == null) return Optional.empty();
return this.networkItemHandler.s2c(wrapped, context).map(Item::load);
} catch (Throwable e) {
if (Config.debug()) {
this.plugin.logger().warn("Failed to handle s2c items.", e);
@@ -86,7 +89,9 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
public Optional<ItemStack> c2s(ItemStack itemStack, ItemBuildContext context) {
try {
return this.networkItemHandler.c2s(itemStack, context);
Item<ItemStack> wrapped = wrap(itemStack);
if (wrapped == null) return Optional.empty();
return this.networkItemHandler.c2s(wrapped, context).map(Item::load);
} catch (Throwable e) {
if (Config.debug()) {
this.plugin.logger().warn("Failed to handle c2s items.", e);

View File

@@ -0,0 +1,26 @@
package net.momirealms.craftengine.bukkit.item;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.ItemBuildContext;
import net.momirealms.craftengine.core.item.NetworkItemHandler;
import org.bukkit.inventory.ItemStack;
import java.util.Optional;
public class LegacyNetworkItemHandler implements NetworkItemHandler<ItemStack> {
private final BukkitItemManager itemManager;
public LegacyNetworkItemHandler(BukkitItemManager itemManager) {
this.itemManager = itemManager;
}
@Override
public Optional<Item<ItemStack>> c2s(Item<ItemStack> itemStack, ItemBuildContext context) {
return Optional.empty();
}
@Override
public Optional<Item<ItemStack>> s2c(Item<ItemStack> itemStack, ItemBuildContext context) {
return Optional.empty();
}
}

View File

@@ -1,10 +1,8 @@
package net.momirealms.craftengine.bukkit.item;
import net.kyori.adventure.text.Component;
import net.momirealms.craftengine.core.item.ComponentKeys;
import net.momirealms.craftengine.core.item.CustomItem;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.ItemBuildContext;
import net.momirealms.craftengine.core.item.*;
import net.momirealms.craftengine.core.item.modifier.ItemDataModifier;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.config.Config;
import net.momirealms.craftengine.core.util.AdventureHelper;
@@ -20,7 +18,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
public class ModernNetworkItemHandler implements NetworkItemHandler {
public class ModernNetworkItemHandler implements NetworkItemHandler<ItemStack> {
private final BukkitItemManager itemManager;
public ModernNetworkItemHandler(BukkitItemManager itemManager) {
@@ -28,9 +26,7 @@ public class ModernNetworkItemHandler implements NetworkItemHandler {
}
@Override
public Optional<ItemStack> c2s(ItemStack itemStack, ItemBuildContext context) {
Item<ItemStack> wrapped = this.itemManager.wrap(itemStack);
if (wrapped == null) return Optional.empty();
public Optional<Item<ItemStack>> c2s(Item<ItemStack> wrapped, ItemBuildContext context) {
Tag customData = wrapped.getNBTComponent(ComponentTypes.CUSTOM_DATA);
if (!(customData instanceof CompoundTag compoundTag)) return Optional.empty();
CompoundTag networkData = compoundTag.getCompound(NETWORK_ITEM_TAG);
@@ -41,24 +37,31 @@ public class ModernNetworkItemHandler implements NetworkItemHandler {
NetworkItemHandler.apply(entry.getKey(), tag, wrapped);
}
}
if (compoundTag.isEmpty()) {
wrapped.resetComponent(ComponentTypes.CUSTOM_DATA);
} else {
wrapped.setNBTComponent(ComponentTypes.CUSTOM_DATA, compoundTag);
}
return Optional.of(wrapped.load());
// todo 可能会是 !custom_data吗不可能绝对不可能
if (compoundTag.isEmpty()) wrapped.resetComponent(ComponentTypes.CUSTOM_DATA);
else wrapped.setNBTComponent(ComponentTypes.CUSTOM_DATA, compoundTag);
return Optional.of(wrapped);
}
@Override
public Optional<ItemStack> s2c(ItemStack itemStack, ItemBuildContext context) {
Item<ItemStack> wrapped = this.itemManager.wrap(itemStack);
if (wrapped == null) return Optional.empty();
public Optional<Item<ItemStack>> s2c(Item<ItemStack> wrapped, ItemBuildContext context) {
Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem();
if (optionalCustomItem.isEmpty()) {
if (!Config.interceptItem()) return Optional.empty();
return new OtherItem(wrapped).process();
} else {
return Optional.empty();
CustomItem<ItemStack> customItem = optionalCustomItem.get();
if (!customItem.hasClientBoundDataModifier()) return Optional.empty();
CompoundTag customData = Optional.ofNullable(wrapped.getNBTComponent(ComponentTypes.CUSTOM_DATA)).map(CompoundTag.class::cast).orElse(new CompoundTag());
CompoundTag tag = new CompoundTag();
for (ItemDataModifier<ItemStack> modifier : customItem.clientBoundDataModifiers()) {
modifier.prepareNetworkItem(wrapped, context, tag);
modifier.apply(wrapped, context);
}
if (tag.isEmpty()) return Optional.empty();
customData.put(NETWORK_ITEM_TAG, tag);
wrapped.setNBTComponent(ComponentTypes.CUSTOM_DATA, customData);
return Optional.of(wrapped);
}
}
@@ -71,7 +74,7 @@ public class ModernNetworkItemHandler implements NetworkItemHandler {
this.item = item;
}
public Optional<ItemStack> process() {
public Optional<Item<ItemStack>> process() {
if (VersionHelper.isOrAbove1_21_5()) {
processModernLore();
processModernCustomName();
@@ -85,7 +88,7 @@ public class ModernNetworkItemHandler implements NetworkItemHandler {
CompoundTag customData = Optional.ofNullable(this.item.getNBTComponent(ComponentTypes.CUSTOM_DATA)).map(CompoundTag.class::cast).orElse(new CompoundTag());
customData.put(NETWORK_ITEM_TAG, getOrCreateTag());
this.item.setNBTComponent(ComponentKeys.CUSTOM_DATA, customData);
return Optional.of(this.item.load());
return Optional.of(this.item);
} else {
return Optional.empty();
}

View File

@@ -20,11 +20,9 @@ public abstract class AbstractCustomItem<I> implements CustomItem<I> {
protected final Map<String, ItemDataModifier<I>> modifierMap;
protected final ItemDataModifier<I>[] clientBoundModifiers;
protected final Map<String, ItemDataModifier<I>> clientBoundModifierMap;
protected final NetworkItemDataProcessor<I>[] networkItemDataProcessors;
protected final List<ItemBehavior> behaviors;
protected final ItemSettings settings;
protected final Map<EventTrigger, List<Function<PlayerOptionalContext>>> events;
protected final Item<I> base;
@SuppressWarnings("unchecked")
public AbstractCustomItem(Holder<Key> id, Key material,
@@ -48,20 +46,8 @@ public abstract class AbstractCustomItem<I> implements CustomItem<I> {
}
this.modifierMap = modifierMapBuilder.build();
ImmutableMap.Builder<String, ItemDataModifier<I>> clientSideModifierMapBuilder = ImmutableMap.builder();
List<NetworkItemDataProcessor<I>> networkItemDataProcessors = new ArrayList<>();
for (ItemDataModifier<I> modifier : clientBoundModifiers) {
String name = modifier.name();
clientSideModifierMapBuilder.put(name, modifier);
if (this.modifierMap.containsKey(name)) {
networkItemDataProcessors.add(NetworkItemDataProcessor.both(this.modifierMap.get(name), modifier));
} else {
networkItemDataProcessors.add(NetworkItemDataProcessor.clientOnly(modifier));
}
}
this.clientBoundModifierMap = clientSideModifierMapBuilder.build();
// unchecked cast
this.networkItemDataProcessors = networkItemDataProcessors.toArray(new NetworkItemDataProcessor[0]);
this.base = (Item<I>) CraftEngine.instance().itemManager().wrap(CraftEngine.instance().itemManager().getVanillaItem(material).get().buildItemStack());
}
@Override
@@ -86,11 +72,6 @@ public abstract class AbstractCustomItem<I> implements CustomItem<I> {
return this.material;
}
@Override
public NetworkItemDataProcessor<I>[] networkItemDataProcessors() {
return this.networkItemDataProcessors;
}
@Override
public ItemDataModifier<I>[] dataModifiers() {
return this.modifiers;

View File

@@ -21,8 +21,6 @@ public interface CustomItem<I> extends BuildableItem<I> {
Key material();
NetworkItemDataProcessor<I>[] networkItemDataProcessors();
ItemDataModifier<I>[] dataModifiers();
Map<String, ItemDataModifier<I>> dataModifierMap();

View File

@@ -1,27 +0,0 @@
package net.momirealms.craftengine.core.item;
import net.momirealms.craftengine.core.item.modifier.ItemDataModifier;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public record NetworkItemDataProcessor<I>(@Nullable ItemDataModifier<I> server, @NotNull ItemDataModifier<I> client) {
public static <I> NetworkItemDataProcessor<I> clientOnly(ItemDataModifier<I> client) {
return new NetworkItemDataProcessor<>(null, client);
}
public static <I> NetworkItemDataProcessor<I> both(ItemDataModifier<I> server, ItemDataModifier<I> client) {
return new NetworkItemDataProcessor<>(server, client);
}
public void toClient(Item<I> item, ItemBuildContext context) {
this.client.apply(item, context);
}
public void toServer(Item<I> item, ItemBuildContext context) {
this.client.remove(item);
if (this.server != null) {
this.server.apply(item, context);
}
}
}

View File

@@ -1,31 +1,37 @@
package net.momirealms.craftengine.bukkit.item;
package net.momirealms.craftengine.core.item;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.ItemBuildContext;
import net.momirealms.craftengine.core.util.TriConsumer;
import net.momirealms.sparrow.nbt.ByteTag;
import net.momirealms.sparrow.nbt.CompoundTag;
import net.momirealms.sparrow.nbt.Tag;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.Optional;
public interface NetworkItemHandler {
public interface NetworkItemHandler<T> {
Operation[] BY_INDEX = new Operation[] {Operation.ADD, Operation.REMOVE, Operation.RESET};
String NETWORK_ITEM_TAG = "craftengine:network";
String NETWORK_OPERATION = "type";
String NETWORK_VALUE = "value";
Optional<ItemStack> s2c(ItemStack itemStack, ItemBuildContext context);
Optional<Item<T>> s2c(Item<T> itemStack, ItemBuildContext context);
Optional<ItemStack> c2s(ItemStack itemStack, ItemBuildContext context);
Optional<Item<T>> c2s(Item<T> itemStack, ItemBuildContext context);
static CompoundTag pack(Operation operation, Tag value) {
return new CompoundTag(Map.of(NETWORK_OPERATION, operation.tag(), NETWORK_VALUE, value));
static CompoundTag pack(Operation operation, @Nullable Tag value) {
if (value == null) {
return new CompoundTag(Map.of(NETWORK_OPERATION, operation.tag()));
} else {
return new CompoundTag(Map.of(NETWORK_OPERATION, operation.tag(), NETWORK_VALUE, value));
}
}
static void apply(String componentType, CompoundTag networkData, Item<ItemStack> item) {
static CompoundTag pack(Operation operation) {
return new CompoundTag(Map.of(NETWORK_OPERATION, operation.tag()));
}
static <T> void apply(String componentType, CompoundTag networkData, Item<T> item) {
byte index = networkData.getByte(NETWORK_OPERATION);
Operation operation = BY_INDEX[index];
operation.consumer.accept(item, componentType, operation == Operation.ADD ? networkData.get(NETWORK_VALUE) : null);
@@ -38,9 +44,9 @@ public interface NetworkItemHandler {
private final int id;
private final ByteTag tag;
private final TriConsumer<Item<ItemStack>, String, Tag> consumer;
private final TriConsumer<Item<?>, String, Tag> consumer;
Operation(int id, TriConsumer<Item<ItemStack>, String, Tag> consumer) {
Operation(int id, TriConsumer<Item<?>, String, Tag> consumer) {
this.id = id;
this.tag = new ByteTag((byte) id);
this.consumer = consumer;

View File

@@ -5,9 +5,12 @@ import com.google.gson.JsonObject;
import net.momirealms.craftengine.core.item.ComponentKeys;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.ItemBuildContext;
import net.momirealms.craftengine.core.item.NetworkItemHandler;
import net.momirealms.craftengine.core.util.GsonHelper;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.Pair;
import net.momirealms.sparrow.nbt.CompoundTag;
import net.momirealms.sparrow.nbt.Tag;
import java.util.ArrayList;
import java.util.List;
@@ -75,18 +78,13 @@ public class ComponentModifier<I> implements ItemDataModifier<I> {
}
@Override
public void remove(Item<I> item) {
public void prepareNetworkItem(Item<I> item, ItemBuildContext context, CompoundTag networkData) {
for (Pair<Key, Object> entry : this.arguments) {
item.resetComponent(entry.left());
}
if (this.customData != null) {
JsonObject tag = (JsonObject) item.getJavaComponent(ComponentKeys.CUSTOM_DATA);
if (tag != null) {
// crude method
for (String key : this.customData.keySet()) {
tag.remove(key);
}
item.setComponent(ComponentKeys.CUSTOM_DATA, tag);
Tag previous = item.getNBTComponent(entry.left());
if (previous != null) {
networkData.put(entry.left().asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.ADD, previous));
} else {
networkData.put(entry.left().asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.REMOVE));
}
}
}

View File

@@ -1,7 +1,12 @@
package net.momirealms.craftengine.core.item.modifier;
import net.momirealms.craftengine.core.item.ComponentKeys;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.ItemBuildContext;
import net.momirealms.craftengine.core.item.NetworkItemHandler;
import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.sparrow.nbt.CompoundTag;
import net.momirealms.sparrow.nbt.Tag;
public class CustomModelDataModifier<I> implements ItemDataModifier<I> {
private final int argument;
@@ -21,7 +26,14 @@ public class CustomModelDataModifier<I> implements ItemDataModifier<I> {
}
@Override
public void remove(Item<I> item) {
item.customModelData(null);
public void prepareNetworkItem(Item<I> item, ItemBuildContext context, CompoundTag networkData) {
if (VersionHelper.isOrAbove1_20_5()) {
Tag previous = item.getNBTComponent(ComponentKeys.CUSTOM_MODEL_DATA);
if (previous != null) {
networkData.put(ComponentKeys.CUSTOM_MODEL_DATA.asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.ADD, previous));
} else {
networkData.put(ComponentKeys.CUSTOM_MODEL_DATA.asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.REMOVE));
}
}
}
}

View File

@@ -1,9 +1,14 @@
package net.momirealms.craftengine.core.item.modifier;
import net.momirealms.craftengine.core.item.ComponentKeys;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.ItemBuildContext;
import net.momirealms.craftengine.core.item.NetworkItemHandler;
import net.momirealms.craftengine.core.plugin.config.Config;
import net.momirealms.craftengine.core.util.AdventureHelper;
import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.sparrow.nbt.CompoundTag;
import net.momirealms.sparrow.nbt.Tag;
public class CustomNameModifier<I> implements ItemDataModifier<I> {
private final String argument;
@@ -23,7 +28,14 @@ public class CustomNameModifier<I> implements ItemDataModifier<I> {
}
@Override
public void remove(Item<I> item) {
item.customNameJson(null);
public void prepareNetworkItem(Item<I> item, ItemBuildContext context, CompoundTag networkData) {
if (VersionHelper.isOrAbove1_20_5()) {
Tag previous = item.getNBTComponent(ComponentKeys.CUSTOM_NAME);
if (previous != null) {
networkData.put(ComponentKeys.CUSTOM_NAME.asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.ADD, previous));
} else {
networkData.put(ComponentKeys.CUSTOM_NAME.asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.REMOVE));
}
}
}
}

View File

@@ -21,13 +21,10 @@ public class EnchantmentModifier<I> implements ItemDataModifier<I> {
@Override
public void apply(Item<I> item, ItemBuildContext context) {
if (item.vanillaId().equals(ItemKeys.ENCHANTED_BOOK)) item.setStoredEnchantments(enchantments);
else item.setEnchantments(enchantments);
}
@Override
public void remove(Item<I> item) {
if (item.vanillaId().equals(ItemKeys.ENCHANTED_BOOK)) item.setStoredEnchantments(null);
else item.setEnchantments(null);
if (item.vanillaId().equals(ItemKeys.ENCHANTED_BOOK)) {
item.setStoredEnchantments(this.enchantments);
} else {
item.setEnchantments(this.enchantments);
}
}
}

View File

@@ -20,9 +20,4 @@ public class EquippableModifier<I> implements ItemDataModifier<I> {
public void apply(Item<I> item, ItemBuildContext context) {
item.equippable(this.data);
}
@Override
public void remove(Item<I> item) {
item.equippable(null);
}
}

View File

@@ -30,9 +30,4 @@ public class ExternalModifier<I> implements ItemDataModifier<I> {
Item<I> anotherWrapped = (Item<I>) CraftEngine.instance().itemManager().wrap(another);
item.merge(anotherWrapped);
}
@Override
public void remove(Item<I> item) {
// cannot remove
}
}

View File

@@ -21,9 +21,4 @@ public class IdModifier<I> implements ItemDataModifier<I> {
public void apply(Item<I> item, ItemBuildContext context) {
item.customId(this.argument);
}
@Override
public void remove(Item<I> item) {
// WHY DO YOU WANT TO REMOVE CRAFTENGINE ID?
}
}

View File

@@ -2,6 +2,7 @@ package net.momirealms.craftengine.core.item.modifier;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.ItemBuildContext;
import net.momirealms.sparrow.nbt.CompoundTag;
public interface ItemDataModifier<I> {
@@ -9,5 +10,6 @@ public interface ItemDataModifier<I> {
void apply(Item<I> item, ItemBuildContext context);
void remove(Item<I> item);
default void prepareNetworkItem(Item<I> item, ItemBuildContext context, CompoundTag networkData) {
}
}

View File

@@ -1,8 +1,13 @@
package net.momirealms.craftengine.core.item.modifier;
import net.momirealms.craftengine.core.item.ComponentKeys;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.ItemBuildContext;
import net.momirealms.craftengine.core.item.NetworkItemHandler;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.sparrow.nbt.CompoundTag;
import net.momirealms.sparrow.nbt.Tag;
public class ItemModelModifier<I> implements ItemDataModifier<I> {
private final Key data;
@@ -22,7 +27,12 @@ public class ItemModelModifier<I> implements ItemDataModifier<I> {
}
@Override
public void remove(Item<I> item) {
item.itemModel(null);
public void prepareNetworkItem(Item<I> item, ItemBuildContext context, CompoundTag networkData) {
Tag previous = item.getNBTComponent(ComponentKeys.ITEM_MODEL);
if (previous != null) {
networkData.put(ComponentKeys.ITEM_MODEL.asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.ADD, previous));
} else {
networkData.put(ComponentKeys.ITEM_MODEL.asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.REMOVE));
}
}
}

View File

@@ -1,9 +1,14 @@
package net.momirealms.craftengine.core.item.modifier;
import net.momirealms.craftengine.core.item.ComponentKeys;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.ItemBuildContext;
import net.momirealms.craftengine.core.item.NetworkItemHandler;
import net.momirealms.craftengine.core.plugin.config.Config;
import net.momirealms.craftengine.core.util.AdventureHelper;
import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.sparrow.nbt.CompoundTag;
import net.momirealms.sparrow.nbt.Tag;
public class ItemNameModifier<I> implements ItemDataModifier<I> {
private final String argument;
@@ -23,7 +28,14 @@ public class ItemNameModifier<I> implements ItemDataModifier<I> {
}
@Override
public void remove(Item<I> item) {
item.itemNameJson(null);
public void prepareNetworkItem(Item<I> item, ItemBuildContext context, CompoundTag networkData) {
if (VersionHelper.isOrAbove1_20_5()) {
Tag previous = item.getNBTComponent(ComponentKeys.ITEM_NAME);
if (previous != null) {
networkData.put(ComponentKeys.ITEM_NAME.asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.ADD, previous));
} else {
networkData.put(ComponentKeys.ITEM_NAME.asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.REMOVE));
}
}
}
}

View File

@@ -20,9 +20,4 @@ public class JukeboxSongModifier<I> implements ItemDataModifier<I> {
public void apply(Item<I> item, ItemBuildContext context) {
item.jukeboxSong(this.song);
}
@Override
public void remove(Item<I> item) {
item.jukeboxSong(null);
}
}

View File

@@ -1,9 +1,14 @@
package net.momirealms.craftengine.core.item.modifier;
import net.momirealms.craftengine.core.item.ComponentKeys;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.ItemBuildContext;
import net.momirealms.craftengine.core.item.NetworkItemHandler;
import net.momirealms.craftengine.core.plugin.config.Config;
import net.momirealms.craftengine.core.util.AdventureHelper;
import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.sparrow.nbt.CompoundTag;
import net.momirealms.sparrow.nbt.Tag;
import java.util.List;
@@ -26,7 +31,14 @@ public class LoreModifier<I> implements ItemDataModifier<I> {
}
@Override
public void remove(Item<I> item) {
item.loreJson(null);
public void prepareNetworkItem(Item<I> item, ItemBuildContext context, CompoundTag networkData) {
if (VersionHelper.isOrAbove1_20_5()) {
Tag previous = item.getNBTComponent(ComponentKeys.LORE);
if (previous != null) {
networkData.put(ComponentKeys.LORE.asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.ADD, previous));
} else {
networkData.put(ComponentKeys.LORE.asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.REMOVE));
}
}
}
}

View File

@@ -2,6 +2,9 @@ package net.momirealms.craftengine.core.item.modifier;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.ItemBuildContext;
import net.momirealms.craftengine.core.item.NetworkItemHandler;
import net.momirealms.sparrow.nbt.CompoundTag;
import net.momirealms.sparrow.nbt.Tag;
import java.util.Collections;
import java.util.List;
@@ -24,13 +27,18 @@ public class RemoveComponentModifier<I> implements ItemDataModifier<I> {
@Override
public void apply(Item<I> item, ItemBuildContext context) {
for (String argument : arguments) {
for (String argument : this.arguments) {
item.removeComponent(argument);
}
}
@Override
public void remove(Item<I> item) {
// I can't guess
public void prepareNetworkItem(Item<I> item, ItemBuildContext context, CompoundTag networkData) {
for (String component : this.arguments) {
Tag previous = item.getNBTComponent(component);
if (previous != null) {
networkData.put(component, NetworkItemHandler.pack(NetworkItemHandler.Operation.ADD, previous));
}
}
}
}

View File

@@ -33,13 +33,6 @@ public class TagsModifier<I> implements ItemDataModifier<I> {
}
}
@Override
public void remove(Item<I> item) {
for (Map.Entry<String, Object> entry : arguments.entrySet()) {
item.removeTag(entry.getKey());
}
}
private static Map<String, Object> mapToMap(final Map<String, Object> source) {
Map<String, Object> resultMap = new LinkedHashMap<>();
recursiveMapProcessing(source, resultMap);

View File

@@ -1,8 +1,13 @@
package net.momirealms.craftengine.core.item.modifier;
import net.momirealms.craftengine.core.item.ComponentKeys;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.ItemBuildContext;
import net.momirealms.craftengine.core.item.NetworkItemHandler;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.sparrow.nbt.CompoundTag;
import net.momirealms.sparrow.nbt.Tag;
public class TooltipStyleModifier<I> implements ItemDataModifier<I> {
private final Key argument;
@@ -22,7 +27,12 @@ public class TooltipStyleModifier<I> implements ItemDataModifier<I> {
}
@Override
public void remove(Item<I> item) {
item.tooltipStyle(null);
public void prepareNetworkItem(Item<I> item, ItemBuildContext context, CompoundTag networkData) {
Tag previous = item.getNBTComponent(ComponentKeys.TOOLTIP_STYLE);
if (previous != null) {
networkData.put(ComponentKeys.TOOLTIP_STYLE.asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.ADD, previous));
} else {
networkData.put(ComponentKeys.TOOLTIP_STYLE.asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.REMOVE));
}
}
}

View File

@@ -22,9 +22,4 @@ public class TrimModifier<I> implements ItemDataModifier<I> {
public void apply(Item<I> item, ItemBuildContext context) {
item.trim(new Trim(this.material, this.pattern));
}
@Override
public void remove(Item<I> item) {
item.trim(null);
}
}

View File

@@ -1,7 +1,12 @@
package net.momirealms.craftengine.core.item.modifier;
import net.momirealms.craftengine.core.item.ComponentKeys;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.ItemBuildContext;
import net.momirealms.craftengine.core.item.NetworkItemHandler;
import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.sparrow.nbt.CompoundTag;
import net.momirealms.sparrow.nbt.Tag;
public class UnbreakableModifier<I> implements ItemDataModifier<I> {
private final boolean argument;
@@ -21,9 +26,14 @@ public class UnbreakableModifier<I> implements ItemDataModifier<I> {
}
@Override
public void remove(Item<I> item) {
if (this.argument) {
item.unbreakable(false);
public void prepareNetworkItem(Item<I> item, ItemBuildContext context, CompoundTag networkData) {
if (VersionHelper.isOrAbove1_20_5()) {
Tag previous = item.getNBTComponent(ComponentKeys.UNBREAKABLE);
if (previous != null) {
networkData.put(ComponentKeys.UNBREAKABLE.asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.ADD, previous));
} else {
networkData.put(ComponentKeys.UNBREAKABLE.asString(), NetworkItemHandler.pack(NetworkItemHandler.Operation.REMOVE));
}
}
}
}