9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-31 12:56:28 +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

@@ -1,57 +0,0 @@
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.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 java.util.Map;
import java.util.Optional;
public interface NetworkItemHandler {
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<ItemStack> c2s(ItemStack itemStack, ItemBuildContext context);
static CompoundTag pack(Operation operation, Tag value) {
return new CompoundTag(Map.of(NETWORK_OPERATION, operation.tag(), NETWORK_VALUE, value));
}
static void apply(String componentType, CompoundTag networkData, Item<ItemStack> 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);
}
enum Operation {
ADD(0, Item::setNBTComponent),
REMOVE(1, (i, s, t) -> i.removeComponent(s)),
RESET(2, (i, s, t) -> i.resetComponent(s));
private final int id;
private final ByteTag tag;
private final TriConsumer<Item<ItemStack>, String, Tag> consumer;
Operation(int id, TriConsumer<Item<ItemStack>, String, Tag> consumer) {
this.id = id;
this.tag = new ByteTag((byte) id);
this.consumer = consumer;
}
public int id() {
return this.id;
}
public ByteTag tag() {
return tag;
}
}
}