9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-19 15:09:15 +00:00

修改发包物品实现

This commit is contained in:
XiaoMoMi
2025-10-08 02:47:27 +08:00
parent e4a422ffc0
commit 652a9db91c
13 changed files with 246 additions and 336 deletions

View File

@@ -1,7 +1,6 @@
package net.momirealms.craftengine.bukkit.compatibility.quickshop;
import com.ghostchu.quickshop.api.QuickShopAPI;
import com.ghostchu.quickshop.api.event.QSConfigurationReloadEvent;
import com.ghostchu.quickshop.api.registry.BuiltInRegistry;
import com.ghostchu.quickshop.api.registry.Registry;
import com.ghostchu.quickshop.api.registry.builtin.itemexpression.ItemExpressionHandler;
@@ -9,9 +8,6 @@ import com.ghostchu.quickshop.api.registry.builtin.itemexpression.ItemExpression
import net.momirealms.craftengine.bukkit.api.CraftEngineItems;
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
import net.momirealms.craftengine.core.util.Key;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;

View File

@@ -124,7 +124,7 @@ public class FallingBlockBehavior extends BukkitBlockBehavior {
SoundData fallSound = null;
SoundData destroySound = null;
if (sounds != null) {
fallSound = Optional.ofNullable(sounds.get("fall")).map(obj -> SoundData.create(obj, SoundData.SoundValue.FIXED_1, SoundData.SoundValue.ranged(0.9f, 1f))).orElse(null);
fallSound = Optional.ofNullable(sounds.get("land")).map(obj -> SoundData.create(obj, SoundData.SoundValue.FIXED_1, SoundData.SoundValue.ranged(0.9f, 1f))).orElse(null);
destroySound = Optional.ofNullable(sounds.get("destroy")).map(obj -> SoundData.create(obj, SoundData.SoundValue.FIXED_1, SoundData.SoundValue.ranged(0.9f, 1f))).orElse(null);
}
return new FallingBlockBehavior(block, hurtAmount, hurtMax, fallSound, destroySound);

View File

@@ -22,7 +22,6 @@ import net.momirealms.craftengine.core.item.recipe.UniqueIdItem;
import net.momirealms.craftengine.core.pack.AbstractPackManager;
import net.momirealms.craftengine.core.plugin.config.Config;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.plugin.logger.Debugger;
import net.momirealms.craftengine.core.util.*;
import org.bukkit.Bukkit;
import org.bukkit.event.HandlerList;
@@ -126,35 +125,23 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
@Override
public Item<ItemStack> s2c(Item<ItemStack> item, Player player) {
if (item.isEmpty()) return item;
return this.networkItemHandler.s2c(item, player).orElse(item);
return this.networkItemHandler.s2c(item, player);
}
@Override
public Item<ItemStack> c2s(Item<ItemStack> item) {
if (item.isEmpty()) return item;
return this.networkItemHandler.c2s(item).orElse(item);
return this.networkItemHandler.c2s(item);
}
public Optional<ItemStack> s2c(ItemStack itemStack, Player player) {
try {
Item<ItemStack> wrapped = wrap(itemStack);
if (wrapped.isEmpty()) return Optional.empty();
return this.networkItemHandler.s2c(wrapped, player).map(Item::getItem);
} catch (Throwable e) {
Debugger.ITEM.warn(() -> "Failed to handle s2c items.", e);
return Optional.empty();
}
public ItemStack s2c(ItemStack item, Player player) {
if (item.isEmpty()) return item;
return s2c(wrap(item), player).getItem();
}
public Optional<ItemStack> c2s(ItemStack itemStack) {
try {
Item<ItemStack> wrapped = wrap(itemStack);
if (wrapped.isEmpty()) return Optional.empty();
return this.networkItemHandler.c2s(wrapped).map(Item::getItem);
} catch (Throwable e) {
Debugger.COMMON.warn(() -> "Failed to handle c2s items.", e);
return Optional.empty();
}
public ItemStack c2s(ItemStack item) {
if (item.isEmpty()) return item;
return c2s(wrap(item)).getItem();
}
@Override

View File

@@ -32,85 +32,83 @@ import java.util.function.BiConsumer;
public final class LegacyNetworkItemHandler implements NetworkItemHandler<ItemStack> {
@Override
public Optional<Item<ItemStack>> c2s(Item<ItemStack> wrapped) {
public Item<ItemStack> c2s(Item<ItemStack> wrapped) {
Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem();
boolean hasDifferentMaterial = false;
if (optionalCustomItem.isPresent()) {
BukkitCustomItem customItem = (BukkitCustomItem) optionalCustomItem.get();
if (customItem.item() != FastNMS.INSTANCE.method$ItemStack$getItem(wrapped.getLiteralObject())) {
wrapped = wrapped.unsafeTransmuteCopy(customItem.item(), wrapped.count());
hasDifferentMaterial = true;
}
}
if (!wrapped.hasTag(NETWORK_ITEM_TAG)) {
if (hasDifferentMaterial) {
return Optional.of(wrapped);
}
}
CompoundTag networkData = (CompoundTag) wrapped.getTag(NETWORK_ITEM_TAG);
if (networkData == null) return Optional.empty();
if (networkData == null) return wrapped;
wrapped.removeTag(NETWORK_ITEM_TAG);
for (Map.Entry<String, Tag> entry : networkData.entrySet()) {
if (entry.getValue() instanceof CompoundTag tag) {
NetworkItemHandler.apply(entry.getKey(), tag, wrapped);
}
}
return Optional.of(wrapped);
return wrapped;
}
@Override
public Optional<Item<ItemStack>> s2c(Item<ItemStack> wrapped, Player player) {
public Item<ItemStack> s2c(Item<ItemStack> wrapped, Player player) {
// todo 处理bundle和container
// todo 处理book
Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem();
// 不是自定义物品或修改过的原版物品
if (optionalCustomItem.isEmpty()) {
if (!Config.interceptItem()) return Optional.empty();
return new OtherItem(wrapped, false).process(NetworkTextReplaceContext.of(player));
} else {
BukkitCustomItem customItem = (BukkitCustomItem) optionalCustomItem.get();
Object serverItem = FastNMS.INSTANCE.method$ItemStack$getItem(wrapped.getLiteralObject());
boolean hasDifferentMaterial = serverItem == customItem.item() && serverItem != customItem.clientItem();
if (hasDifferentMaterial) {
wrapped = wrapped.unsafeTransmuteCopy(customItem.clientItem(), wrapped.count());
if (!Config.interceptItem()) return wrapped;
return new OtherItem(wrapped).process(NetworkTextReplaceContext.of(player));
}
// 应用client-bound-material
BukkitCustomItem customItem = (BukkitCustomItem) optionalCustomItem.get();
Object serverItem = FastNMS.INSTANCE.method$ItemStack$getItem(wrapped.getLiteralObject());
boolean hasDifferentMaterial = serverItem == customItem.item() && serverItem != customItem.clientItem();
if (hasDifferentMaterial) {
wrapped = wrapped.unsafeTransmuteCopy(customItem.clientItem(), wrapped.count());
}
// 没有客户端侧组件
if (!customItem.hasClientBoundDataModifier()) {
if (!Config.interceptItem()) return wrapped;
return new OtherItem(wrapped).process(NetworkTextReplaceContext.of(player));
}
// 应用client-bound-data
CompoundTag tag = new CompoundTag();
Tag argumentTag = wrapped.getTag(ArgumentsModifier.ARGUMENTS_TAG);
ItemBuildContext context;
if (argumentTag instanceof CompoundTag arguments) {
ContextHolder.Builder builder = ContextHolder.builder();
for (Map.Entry<String, Tag> entry : arguments.entrySet()) {
builder.withParameter(ContextKey.direct(entry.getKey()), entry.getValue().getAsString());
}
if (!customItem.hasClientBoundDataModifier()) {
if (!Config.interceptItem() && !hasDifferentMaterial) return Optional.empty();
return new OtherItem(wrapped, hasDifferentMaterial).process(NetworkTextReplaceContext.of(player));
} else {
CompoundTag tag = new CompoundTag();
Tag argumentTag = wrapped.getTag(ArgumentsModifier.ARGUMENTS_TAG);
ItemBuildContext context;
if (argumentTag instanceof CompoundTag arguments) {
ContextHolder.Builder builder = ContextHolder.builder();
for (Map.Entry<String, Tag> entry : arguments.entrySet()) {
builder.withParameter(ContextKey.direct(entry.getKey()), entry.getValue().getAsString());
}
context = ItemBuildContext.of(player, builder);
} else {
context = ItemBuildContext.of(player);
}
for (ItemDataModifier<ItemStack> modifier : customItem.clientBoundDataModifiers()) {
modifier.prepareNetworkItem(wrapped, context, tag);
}
for (ItemDataModifier<ItemStack> modifier : customItem.clientBoundDataModifiers()) {
modifier.apply(wrapped, context);
}
if (Config.interceptItem()) {
if (!tag.containsKey("display.Name")) {
processCustomName(wrapped, tag::put, context);
}
if (!tag.containsKey("display.Lore")) {
processLore(wrapped, tag::put, context);
}
}
if (tag.isEmpty()) {
if (hasDifferentMaterial) {
return Optional.of(wrapped);
}
return Optional.empty();
}
wrapped.setTag(tag, NETWORK_ITEM_TAG);
return Optional.of(wrapped);
context = ItemBuildContext.of(player, builder);
} else {
context = ItemBuildContext.of(player);
}
for (ItemDataModifier<ItemStack> modifier : customItem.clientBoundDataModifiers()) {
modifier.prepareNetworkItem(wrapped, context, tag);
}
for (ItemDataModifier<ItemStack> modifier : customItem.clientBoundDataModifiers()) {
modifier.apply(wrapped, context);
}
if (Config.interceptItem()) {
if (!tag.containsKey("display.Name")) {
processCustomName(wrapped, tag::put, context);
}
if (!tag.containsKey("display.Lore")) {
processLore(wrapped, tag::put, context);
}
}
if (!tag.isEmpty()) {
wrapped.setTag(tag, NETWORK_ITEM_TAG);
}
return wrapped;
}
public static boolean processCustomName(Item<ItemStack> item, BiConsumer<String, CompoundTag> callback, Context context) {
@@ -159,14 +157,12 @@ public final class LegacyNetworkItemHandler implements NetworkItemHandler<ItemSt
private final Item<ItemStack> item;
private boolean globalChanged = false;
private CompoundTag networkTag;
private final boolean forceReturn;
public OtherItem(Item<ItemStack> item, boolean forceReturn) {
public OtherItem(Item<ItemStack> item) {
this.item = item;
this.forceReturn = forceReturn;
}
public Optional<Item<ItemStack>> process(Context context) {
public Item<ItemStack> process(Context context) {
if (processLore(this.item, (s, c) -> networkTag().put(s, c), context)) {
this.globalChanged = true;
}
@@ -175,12 +171,8 @@ public final class LegacyNetworkItemHandler implements NetworkItemHandler<ItemSt
}
if (this.globalChanged) {
this.item.setTag(this.networkTag, NETWORK_ITEM_TAG);
return Optional.of(this.item);
} else if (this.forceReturn) {
return Optional.of(this.item);
} else {
return Optional.empty();
}
return this.item;
}
public CompoundTag networkTag() {

View File

@@ -30,96 +30,104 @@ import java.util.function.Supplier;
public final class ModernNetworkItemHandler implements NetworkItemHandler<ItemStack> {
@Override
public Optional<Item<ItemStack>> c2s(Item<ItemStack> wrapped) {
Tag customData = wrapped.getSparrowNBTComponent(ComponentTypes.CUSTOM_DATA);
if (!(customData instanceof CompoundTag compoundTag)) return Optional.empty();
public Item<ItemStack> c2s(Item<ItemStack> wrapped) {
// 先尝试恢复client-bound-material
Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem();
boolean hasDifferentMaterial = false;
if (optionalCustomItem.isPresent()) {
BukkitCustomItem customItem = (BukkitCustomItem) optionalCustomItem.get();
if (customItem.item() != FastNMS.INSTANCE.method$ItemStack$getItem(wrapped.getLiteralObject())) {
wrapped = wrapped.unsafeTransmuteCopy(customItem.item(), wrapped.count());
hasDifferentMaterial = true;
}
}
// 没有 custom data 则忽略
Tag customData = wrapped.getSparrowNBTComponent(ComponentTypes.CUSTOM_DATA);
if (!(customData instanceof CompoundTag compoundTag)) {
return wrapped;
}
// 未曾应用过 client-bound-data
CompoundTag networkData = compoundTag.getCompound(NETWORK_ITEM_TAG);
if (networkData == null) {
if (hasDifferentMaterial) {
return Optional.of(wrapped);
}
return Optional.empty();
return wrapped;
}
// 移除此tag
compoundTag.remove(NETWORK_ITEM_TAG);
for (Map.Entry<String, Tag> entry : networkData.entrySet()) {
if (entry.getValue() instanceof CompoundTag tag) {
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);
return wrapped;
}
@Override
public Optional<Item<ItemStack>> s2c(Item<ItemStack> wrapped, Player player) {
public Item<ItemStack> s2c(Item<ItemStack> wrapped, Player player) {
// todo 处理bundle和container
// todo 处理book
// 不是自定义物品或修改过的原版物品
Item<ItemStack> original = wrapped;
Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem();
if (optionalCustomItem.isEmpty()) {
if (!Config.interceptItem()) return Optional.empty();
return new OtherItem(wrapped, false).process(NetworkTextReplaceContext.of(player));
if (!Config.interceptItem()) return wrapped;
return new OtherItem(wrapped).process(NetworkTextReplaceContext.of(player));
}
// 应用 client-bound-data
BukkitCustomItem customItem = (BukkitCustomItem) optionalCustomItem.get();
Object serverItem = FastNMS.INSTANCE.method$ItemStack$getItem(wrapped.getLiteralObject());
boolean hasDifferentMaterial = serverItem == customItem.item() && serverItem != customItem.clientItem();
if (hasDifferentMaterial) {
wrapped = wrapped.unsafeTransmuteCopy(customItem.clientItem(), wrapped.count());
}
if (!customItem.hasClientBoundDataModifier()) {
if (!Config.interceptItem()) return wrapped;
return new OtherItem(wrapped).process(NetworkTextReplaceContext.of(player));
}
CompoundTag customData = Optional.ofNullable(wrapped.getSparrowNBTComponent(ComponentTypes.CUSTOM_DATA)).map(CompoundTag.class::cast).orElse(new CompoundTag());
CompoundTag arguments = customData.getCompound(ArgumentsModifier.ARGUMENTS_TAG);
ItemBuildContext context;
if (arguments == null) {
context = ItemBuildContext.of(player);
} else {
BukkitCustomItem customItem = (BukkitCustomItem) optionalCustomItem.get();
Object serverItem = FastNMS.INSTANCE.method$ItemStack$getItem(wrapped.getLiteralObject());
boolean hasDifferentMaterial = serverItem == customItem.item() && serverItem != customItem.clientItem();
if (hasDifferentMaterial) {
wrapped = wrapped.unsafeTransmuteCopy(customItem.clientItem(), wrapped.count());
ContextHolder.Builder builder = ContextHolder.builder();
for (Map.Entry<String, Tag> entry : arguments.entrySet()) {
builder.withParameter(ContextKey.direct(entry.getKey()), entry.getValue().getAsString());
}
if (!customItem.hasClientBoundDataModifier()) {
if (!Config.interceptItem() && !hasDifferentMaterial) return Optional.empty();
return new OtherItem(wrapped, hasDifferentMaterial).process(NetworkTextReplaceContext.of(player));
} else {
CompoundTag customData = Optional.ofNullable(wrapped.getSparrowNBTComponent(ComponentTypes.CUSTOM_DATA)).map(CompoundTag.class::cast).orElse(new CompoundTag());
CompoundTag arguments = customData.getCompound(ArgumentsModifier.ARGUMENTS_TAG);
ItemBuildContext context;
if (arguments == null) {
context = ItemBuildContext.of(player);
} else {
ContextHolder.Builder builder = ContextHolder.builder();
for (Map.Entry<String, Tag> entry : arguments.entrySet()) {
builder.withParameter(ContextKey.direct(entry.getKey()), entry.getValue().getAsString());
}
context = ItemBuildContext.of(player, builder);
}
CompoundTag tag = new CompoundTag();
for (ItemDataModifier<ItemStack> modifier : customItem.clientBoundDataModifiers()) {
modifier.prepareNetworkItem(original, context, tag);
}
for (ItemDataModifier<ItemStack> modifier : customItem.clientBoundDataModifiers()) {
modifier.apply(wrapped, context);
}
if (Config.interceptItem()) {
if (!tag.containsKey(ComponentIds.ITEM_NAME)) {
if (VersionHelper.isOrAbove1_21_5()) processModernItemName(wrapped, () -> tag, context);
else processLegacyItemName(wrapped, () -> tag, context);
}
if (!tag.containsKey(ComponentIds.CUSTOM_NAME)) {
if (VersionHelper.isOrAbove1_21_5()) processModernCustomName(wrapped, () -> tag, context);
else processLegacyCustomName(wrapped, () -> tag, context);
}
if (!tag.containsKey(ComponentIds.LORE)) {
if (VersionHelper.isOrAbove1_21_5()) processModernLore(wrapped, () -> tag, context);
else processLegacyLore(wrapped, () -> tag, context);
}
}
if (tag.isEmpty()) {
if (hasDifferentMaterial) return Optional.of(wrapped);
return Optional.empty();
}
customData.put(NETWORK_ITEM_TAG, tag);
wrapped.setNBTComponent(ComponentTypes.CUSTOM_DATA, customData);
return Optional.of(wrapped);
context = ItemBuildContext.of(player, builder);
}
CompoundTag tag = new CompoundTag();
for (ItemDataModifier<ItemStack> modifier : customItem.clientBoundDataModifiers()) {
modifier.prepareNetworkItem(original, context, tag);
}
for (ItemDataModifier<ItemStack> modifier : customItem.clientBoundDataModifiers()) {
modifier.apply(wrapped, context);
}
if (Config.interceptItem()) {
if (!tag.containsKey(ComponentIds.ITEM_NAME)) {
if (VersionHelper.isOrAbove1_21_5()) processModernItemName(wrapped, () -> tag, context);
else processLegacyItemName(wrapped, () -> tag, context);
}
if (!tag.containsKey(ComponentIds.CUSTOM_NAME)) {
if (VersionHelper.isOrAbove1_21_5()) processModernCustomName(wrapped, () -> tag, context);
else processLegacyCustomName(wrapped, () -> tag, context);
}
if (!tag.containsKey(ComponentIds.LORE)) {
if (VersionHelper.isOrAbove1_21_5()) processModernLore(wrapped, () -> tag, context);
else processLegacyLore(wrapped, () -> tag, context);
}
}
if (!tag.isEmpty()) {
customData.put(NETWORK_ITEM_TAG, tag);
wrapped.setNBTComponent(ComponentTypes.CUSTOM_DATA, customData);
}
return wrapped;
}
public static boolean processLegacyLore(Item<ItemStack> item, Supplier<CompoundTag> tag, Context context) {
@@ -231,16 +239,14 @@ public final class ModernNetworkItemHandler implements NetworkItemHandler<ItemSt
static class OtherItem {
private final Item<ItemStack> item;
private final boolean forceReturn;
private boolean globalChanged = false;
private CompoundTag tag;
public OtherItem(Item<ItemStack> item, boolean forceReturn) {
public OtherItem(Item<ItemStack> item) {
this.item = item;
this.forceReturn = forceReturn;
}
public Optional<Item<ItemStack>> process(Context context) {
public Item<ItemStack> process(Context context) {
if (VersionHelper.isOrAbove1_21_5()) {
if (processModernLore(this.item, this::getOrCreateTag, context))
this.globalChanged = true;
@@ -260,12 +266,8 @@ public final class ModernNetworkItemHandler implements NetworkItemHandler<ItemSt
CompoundTag customData = Optional.ofNullable(this.item.getSparrowNBTComponent(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);
} else if (this.forceReturn) {
return Optional.of(this.item);
} else {
return Optional.empty();
}
return this.item;
}
private CompoundTag getOrCreateTag() {

View File

@@ -1,6 +1,5 @@
package net.momirealms.craftengine.bukkit.item.behavior;
import net.momirealms.craftengine.bukkit.block.BukkitBlockManager;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MFluids;
@@ -13,12 +12,10 @@ import net.momirealms.craftengine.core.item.behavior.ItemBehavior;
import net.momirealms.craftengine.core.item.behavior.ItemBehaviorFactory;
import net.momirealms.craftengine.core.item.context.UseOnContext;
import net.momirealms.craftengine.core.pack.Pack;
import net.momirealms.craftengine.core.pack.PendingConfigSection;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Direction;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.world.BlockHitResult;
import net.momirealms.craftengine.core.world.BlockPos;

View File

@@ -3185,26 +3185,12 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
int stateId = buf.readVarInt();
int listSize = buf.readVarInt();
List<ItemStack> items = new ArrayList<>(listSize);
boolean changed = false;
Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
for (int i = 0; i < listSize; i++) {
ItemStack itemStack = FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf);
Optional<ItemStack> optional = BukkitItemManager.instance().s2c(itemStack, serverPlayer);
if (optional.isPresent()) {
items.add(optional.get());
changed = true;
} else {
items.add(itemStack);
}
ItemStack itemStack = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf), serverPlayer);
items.add(itemStack);
}
ItemStack carriedItem = FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf);
ItemStack newCarriedItem = carriedItem;
Optional<ItemStack> optional = BukkitItemManager.instance().s2c(carriedItem, serverPlayer);
if (optional.isPresent()) {
changed = true;
newCarriedItem = optional.get();
}
if (!changed) return;
ItemStack carriedItem = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf), serverPlayer);
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
@@ -3215,7 +3201,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
for (ItemStack itemStack : items) {
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, itemStack);
}
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, newCarriedItem);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, carriedItem);
}
}
@@ -3236,16 +3222,15 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
// 其他插件干的比如某ty*****er不要赖到ce头上
return;
}
BukkitItemManager.instance().s2c(itemStack, serverPlayer).ifPresent((newItemStack) -> {
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeContainerId(containerId);
buf.writeVarInt(stateId);
buf.writeShort(slot);
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, newItemStack);
});
itemStack = BukkitItemManager.instance().s2c(itemStack, serverPlayer);
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeContainerId(containerId);
buf.writeVarInt(stateId);
buf.writeShort(slot);
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, itemStack);
}
}
@@ -3256,14 +3241,12 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
if (!(user instanceof BukkitServerPlayer serverPlayer)) return;
FriendlyByteBuf buf = event.getBuffer();
Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
ItemStack itemStack = FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf);
BukkitItemManager.instance().s2c(itemStack, serverPlayer).ifPresent((newItemStack) -> {
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, newItemStack);
});
ItemStack itemStack = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf), serverPlayer);
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, itemStack);
}
}
@@ -3273,7 +3256,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
public void onPacketSend(NetWorkUser user, ByteBufPacketEvent event) {
if (!(user instanceof BukkitServerPlayer serverPlayer)) return;
FriendlyByteBuf buf = event.getBuffer();
boolean changed = false;
Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
int entity = buf.readVarInt();
List<com.mojang.datafixers.util.Pair<Object, ItemStack>> slots = Lists.newArrayList();
@@ -3281,29 +3264,23 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
do {
slotMask = buf.readByte();
Object equipmentSlot = CoreReflections.instance$EquipmentSlot$values[slotMask & 127];
ItemStack itemStack = FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf);
Optional<ItemStack> optional = BukkitItemManager.instance().s2c(itemStack, serverPlayer);
if (optional.isPresent()) {
changed = true;
itemStack = optional.get();
}
ItemStack itemStack = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf), serverPlayer);
slots.add(com.mojang.datafixers.util.Pair.of(equipmentSlot, itemStack));
} while ((slotMask & -128) != 0);
if (changed) {
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeVarInt(entity);
int i = slots.size();
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
for (int j = 0; j < i; ++j) {
com.mojang.datafixers.util.Pair<Object, ItemStack> pair = slots.get(j);
Enum<?> equipmentSlot = (Enum<?>) pair.getFirst();
boolean bl = j != i - 1;
int k = equipmentSlot.ordinal();
buf.writeByte(bl ? k | -128 : k);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, pair.getSecond());
}
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeVarInt(entity);
int i = slots.size();
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
for (int j = 0; j < i; ++j) {
com.mojang.datafixers.util.Pair<Object, ItemStack> pair = slots.get(j);
Enum<?> equipmentSlot = (Enum<?>) pair.getFirst();
boolean bl = j != i - 1;
int k = equipmentSlot.ordinal();
buf.writeByte(bl ? k | -128 : k);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, pair.getSecond());
}
}
}
@@ -3316,15 +3293,14 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
FriendlyByteBuf buf = event.getBuffer();
int slot = buf.readVarInt();
Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
ItemStack itemStack = FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf);
BukkitItemManager.instance().s2c(itemStack, serverPlayer).ifPresent((newItemStack) -> {
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeVarInt(slot);
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, newItemStack);
});
ItemStack itemStack = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf), serverPlayer);
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeVarInt(slot);
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, itemStack);
}
}
@@ -3344,18 +3320,18 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
} catch (Exception e) {
return;
}
BukkitItemManager.instance().c2s(itemStack).ifPresent((newItemStack) -> {
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeShort(slotNum);
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
if (VersionHelper.isOrAbove1_20_5()) {
FastNMS.INSTANCE.method$FriendlyByteBuf$writeUntrustedItem(newFriendlyBuf, newItemStack);
} else {
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, newItemStack);
}
});
itemStack = BukkitItemManager.instance().s2c(itemStack, serverPlayer);
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeShort(slotNum);
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
if (VersionHelper.isOrAbove1_20_5()) {
FastNMS.INSTANCE.method$FriendlyByteBuf$writeUntrustedItem(newFriendlyBuf, itemStack);
} else {
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, itemStack);
}
}
}
@@ -3364,7 +3340,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
@Override
public void onPacketReceive(NetWorkUser user, ByteBufPacketEvent event) {
FriendlyByteBuf buf = event.getBuffer();
boolean changed = false;
Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
int containerId = buf.readContainerId();
int stateId = buf.readVarInt();
@@ -3375,37 +3351,27 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
Int2ObjectMap<ItemStack> changedSlots = new Int2ObjectOpenHashMap<>(i);
for (int j = 0; j < i; ++j) {
int k = buf.readShort();
ItemStack itemStack = FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf);
Optional<ItemStack> optional = BukkitItemManager.instance().c2s(itemStack);
if (optional.isPresent()) {
changed = true;
itemStack = optional.get();
}
ItemStack itemStack = BukkitItemManager.instance().c2s(FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf));
changedSlots.put(k, itemStack);
}
ItemStack carriedItem = FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf);
Optional<ItemStack> optional = BukkitItemManager.instance().c2s(carriedItem);
if (optional.isPresent()) {
changed = true;
carriedItem = optional.get();
}
if (changed) {
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeContainerId(containerId);
buf.writeVarInt(stateId);
buf.writeShort(slotNum);
buf.writeByte(buttonNum);
buf.writeVarInt(clickType);
buf.writeVarInt(changedSlots.size());
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
changedSlots.forEach((k, v) -> {
buf.writeShort(k);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, v);
});
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, carriedItem);
}
ItemStack carriedItem = BukkitItemManager.instance().c2s(FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf));
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeContainerId(containerId);
buf.writeVarInt(stateId);
buf.writeShort(slotNum);
buf.writeByte(buttonNum);
buf.writeVarInt(clickType);
buf.writeVarInt(changedSlots.size());
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
changedSlots.forEach((k, v) -> {
buf.writeShort(k);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, v);
});
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, carriedItem);
}
}

View File

@@ -13,7 +13,6 @@ import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import org.bukkit.inventory.ItemStack;
import java.util.List;
import java.util.Optional;
public class CommonItemPacketHandler implements EntityPacketHandler {
public static final CommonItemPacketHandler INSTANCE = new CommonItemPacketHandler();
@@ -23,7 +22,7 @@ public class CommonItemPacketHandler implements EntityPacketHandler {
public void handleSetEntityData(Player user, ByteBufPacketEvent event) {
FriendlyByteBuf buf = event.getBuffer();
int id = buf.readVarInt();
boolean isChanged = false;
List<Object> packedItems = FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$unpack(buf);
for (int i = 0; i < packedItems.size(); i++) {
Object packedItem = packedItems.get(i);
@@ -40,23 +39,16 @@ public class CommonItemPacketHandler implements EntityPacketHandler {
}
continue;
}
ItemStack itemStack = FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(nmsItemStack);
Optional<ItemStack> optional = BukkitItemManager.instance().s2c(itemStack, user);
if (optional.isEmpty()) continue;
isChanged = true;
itemStack = optional.get();
ItemStack itemStack = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(nmsItemStack), user);
Object serializer = FastNMS.INSTANCE.field$SynchedEntityData$DataValue$serializer(packedItem);
packedItems.set(i, FastNMS.INSTANCE.constructor$SynchedEntityData$DataValue(
entityDataId, serializer, FastNMS.INSTANCE.method$CraftItemStack$asNMSCopy(itemStack)
));
packedItems.set(i, FastNMS.INSTANCE.constructor$SynchedEntityData$DataValue(entityDataId, serializer, FastNMS.INSTANCE.method$CraftItemStack$asNMSCopy(itemStack)));
break;
}
if (isChanged) {
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeVarInt(id);
FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$pack(packedItems, buf);
}
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeVarInt(id);
FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$pack(packedItems, buf);
}
}

View File

@@ -3,7 +3,6 @@ package net.momirealms.craftengine.bukkit.plugin.network.handler;
import net.momirealms.craftengine.bukkit.entity.data.ItemDisplayEntityData;
import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.plugin.network.ByteBufPacketEvent;
import net.momirealms.craftengine.core.plugin.network.EntityPacketHandler;
@@ -11,7 +10,6 @@ import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import org.bukkit.inventory.ItemStack;
import java.util.List;
import java.util.Optional;
public class ItemDisplayPacketHandler implements EntityPacketHandler {
public static final ItemDisplayPacketHandler INSTANCE = new ItemDisplayPacketHandler();
@@ -20,30 +18,24 @@ public class ItemDisplayPacketHandler implements EntityPacketHandler {
public void handleSetEntityData(Player user, ByteBufPacketEvent event) {
FriendlyByteBuf buf = event.getBuffer();
int id = buf.readVarInt();
boolean isChanged = false;
List<Object> packedItems = FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$unpack(buf);
for (int i = 0; i < packedItems.size(); i++) {
Object packedItem = packedItems.get(i);
int entityDataId = FastNMS.INSTANCE.field$SynchedEntityData$DataValue$id(packedItem);
if (entityDataId != ItemDisplayEntityData.DisplayedItem.id()) continue;
Object nmsItemStack = FastNMS.INSTANCE.field$SynchedEntityData$DataValue$value(packedItem);
ItemStack itemStack = FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(nmsItemStack);
Optional<ItemStack> optional = BukkitItemManager.instance().s2c(itemStack, (BukkitServerPlayer) user);
if (optional.isEmpty()) continue;
isChanged = true;
itemStack = optional.get();
ItemStack itemStack = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(nmsItemStack), user);
Object serializer = FastNMS.INSTANCE.field$SynchedEntityData$DataValue$serializer(packedItem);
packedItems.set(i, FastNMS.INSTANCE.constructor$SynchedEntityData$DataValue(
entityDataId, serializer, FastNMS.INSTANCE.method$CraftItemStack$asNMSCopy(itemStack)
));
break;
}
if (isChanged) {
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeVarInt(id);
FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$pack(packedItems, buf);
}
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeVarInt(id);
FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$pack(packedItems, buf);
}
}

View File

@@ -13,7 +13,6 @@ import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import org.bukkit.inventory.ItemStack;
import java.util.List;
import java.util.Optional;
public class ItemFramePacketHandler implements EntityPacketHandler {
public static final ItemFramePacketHandler INSTANCE = new ItemFramePacketHandler();
@@ -23,7 +22,6 @@ public class ItemFramePacketHandler implements EntityPacketHandler {
public void handleSetEntityData(Player user, ByteBufPacketEvent event) {
FriendlyByteBuf buf = event.getBuffer();
int id = buf.readVarInt();
boolean isChanged = false;
List<Object> packedItems = FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$unpack(buf);
for (int i = 0; i < packedItems.size(); i++) {
Object packedItem = packedItems.get(i);
@@ -40,23 +38,18 @@ public class ItemFramePacketHandler implements EntityPacketHandler {
}
continue;
}
ItemStack itemStack = FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(nmsItemStack);
Optional<ItemStack> optional = BukkitItemManager.instance().s2c(itemStack, user);
if (optional.isEmpty()) continue;
isChanged = true;
itemStack = optional.get();
ItemStack itemStack = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(nmsItemStack), user);
Object serializer = FastNMS.INSTANCE.field$SynchedEntityData$DataValue$serializer(packedItem);
packedItems.set(i, FastNMS.INSTANCE.constructor$SynchedEntityData$DataValue(
entityDataId, serializer, FastNMS.INSTANCE.method$CraftItemStack$asNMSCopy(itemStack)
));
break;
}
if (isChanged) {
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeVarInt(id);
FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$pack(packedItems, buf);
}
event.setChanged(true);
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeVarInt(id);
FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$pack(packedItems, buf);
}
}

View File

@@ -29,14 +29,10 @@ import java.util.UUID;
public class ProjectilePacketHandler implements EntityPacketHandler {
private final int entityId;
private final BukkitCustomProjectile projectile;
private final Object cachedPacket;
private final List<Object> cachedData;
public ProjectilePacketHandler(BukkitCustomProjectile projectile, int entityId) {
this.projectile = projectile;
this.entityId = entityId;
this.cachedData = createCustomProjectileEntityDataValues();
this.cachedPacket = FastNMS.INSTANCE.constructor$ClientboundSetEntityDataPacket(entityId, this.cachedData);
}
@Override
@@ -47,7 +43,7 @@ public class ProjectilePacketHandler implements EntityPacketHandler {
buf.clear();
buf.writeVarInt(event.packetID());
buf.writeVarInt(id);
FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$pack(this.cachedData, buf);
FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$pack(this.createCustomProjectileEntityDataValues(user), buf);
}
@Override
@@ -60,7 +56,7 @@ public class ProjectilePacketHandler implements EntityPacketHandler {
public void handleMoveAndRotate(NetWorkUser user, NMSPacketEvent event, Object packet) {
int entityId = ProtectedFieldVisitor.get().field$ClientboundMoveEntityPacket$entityId(packet);
event.replacePacket(FastNMS.INSTANCE.constructor$ClientboundBundlePacket(List.of(
this.cachedPacket,
FastNMS.INSTANCE.constructor$ClientboundSetEntityDataPacket(entityId, this.createCustomProjectileEntityDataValues((Player) user)),
convertCustomProjectileMovePacket(packet, entityId)
)));
}
@@ -110,14 +106,14 @@ public class ProjectilePacketHandler implements EntityPacketHandler {
return FastNMS.INSTANCE.constructor$ClientboundEntityPositionSyncPacket(entityId, newPositionMoveRotation, onGround);
}
public List<Object> createCustomProjectileEntityDataValues() {
public List<Object> createCustomProjectileEntityDataValues(Player player) {
List<Object> itemDisplayValues = new ArrayList<>();
Optional<CustomItem<ItemStack>> customItem = BukkitItemManager.instance().getCustomItem(this.projectile.metadata().item());
if (customItem.isEmpty()) return itemDisplayValues;
ProjectileMeta meta = this.projectile.metadata();
Item<ItemStack> displayedItem = customItem.get().buildItem(ItemBuildContext.empty());
// 我们应当使用新的展示物品的组件覆盖原物品的组件,以完成附魔,附魔光效等组件的继承
displayedItem = this.projectile.item().mergeCopy(displayedItem);
displayedItem = BukkitItemManager.instance().s2c(this.projectile.item().mergeCopy(displayedItem), player);
ItemDisplayEntityData.InterpolationDelay.addEntityDataIfNotDefaultValue(-1, itemDisplayValues);
ItemDisplayEntityData.Translation.addEntityDataIfNotDefaultValue(meta.translation(), itemDisplayValues);
ItemDisplayEntityData.Scale.addEntityDataIfNotDefaultValue(meta.scale(), itemDisplayValues);
@@ -130,9 +126,7 @@ public class ProjectilePacketHandler implements EntityPacketHandler {
}
Object literalItem = displayedItem.getLiteralObject();
BukkitItemManager.instance().s2c(displayedItem.getItem(), null).ifPresentOrElse(
it -> ItemDisplayEntityData.DisplayedItem.addEntityDataIfNotDefaultValue(FastNMS.INSTANCE.field$CraftItemStack$handle(it), itemDisplayValues),
() -> ItemDisplayEntityData.DisplayedItem.addEntityDataIfNotDefaultValue(literalItem, itemDisplayValues));
ItemDisplayEntityData.DisplayedItem.addEntityDataIfNotDefaultValue(literalItem, itemDisplayValues);
ItemDisplayEntityData.DisplayType.addEntityDataIfNotDefaultValue(meta.displayType().id(), itemDisplayValues);
ItemDisplayEntityData.BillboardConstraints.addEntityDataIfNotDefaultValue(meta.billboard().id(), itemDisplayValues);
return itemDisplayValues;

View File

@@ -10,7 +10,6 @@ import net.momirealms.sparrow.nbt.Tag;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.Optional;
public interface NetworkItemHandler<T> {
Operation[] BY_INDEX = new Operation[] {Operation.ADD, Operation.REMOVE, Operation.RESET};
@@ -18,9 +17,9 @@ public interface NetworkItemHandler<T> {
String NETWORK_OPERATION = "type";
String NETWORK_VALUE = "value";
Optional<Item<T>> s2c(Item<T> itemStack, Player player);
Item<T> s2c(Item<T> itemStack, Player player);
Optional<Item<T>> c2s(Item<T> itemStack);
Item<T> c2s(Item<T> itemStack);
static CompoundTag pack(Operation operation, @Nullable Tag value) {
if (value == null) {

View File

@@ -50,7 +50,7 @@ byte_buddy_version=1.17.5
ahocorasick_version=0.6.3
snake_yaml_version=2.5
anti_grief_version=1.0.2
nms_helper_version=1.0.104
nms_helper_version=1.0.106
evalex_version=3.5.0
reactive_streams_version=1.0.4
amazon_awssdk_version=2.34.5