9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-20 15:39:22 +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; package net.momirealms.craftengine.bukkit.compatibility.quickshop;
import com.ghostchu.quickshop.api.QuickShopAPI; 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.BuiltInRegistry;
import com.ghostchu.quickshop.api.registry.Registry; import com.ghostchu.quickshop.api.registry.Registry;
import com.ghostchu.quickshop.api.registry.builtin.itemexpression.ItemExpressionHandler; 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.api.CraftEngineItems;
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine; import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
import net.momirealms.craftengine.core.util.Key; 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.event.Listener;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;

View File

@@ -124,7 +124,7 @@ public class FallingBlockBehavior extends BukkitBlockBehavior {
SoundData fallSound = null; SoundData fallSound = null;
SoundData destroySound = null; SoundData destroySound = null;
if (sounds != 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); 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); 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.pack.AbstractPackManager;
import net.momirealms.craftengine.core.plugin.config.Config; import net.momirealms.craftengine.core.plugin.config.Config;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException; import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.plugin.logger.Debugger;
import net.momirealms.craftengine.core.util.*; import net.momirealms.craftengine.core.util.*;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
@@ -126,35 +125,23 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
@Override @Override
public Item<ItemStack> s2c(Item<ItemStack> item, Player player) { public Item<ItemStack> s2c(Item<ItemStack> item, Player player) {
if (item.isEmpty()) return item; if (item.isEmpty()) return item;
return this.networkItemHandler.s2c(item, player).orElse(item); return this.networkItemHandler.s2c(item, player);
} }
@Override @Override
public Item<ItemStack> c2s(Item<ItemStack> item) { public Item<ItemStack> c2s(Item<ItemStack> item) {
if (item.isEmpty()) return 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) { public ItemStack s2c(ItemStack item, Player player) {
try { if (item.isEmpty()) return item;
Item<ItemStack> wrapped = wrap(itemStack); return s2c(wrap(item), player).getItem();
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 Optional<ItemStack> c2s(ItemStack itemStack) { public ItemStack c2s(ItemStack item) {
try { if (item.isEmpty()) return item;
Item<ItemStack> wrapped = wrap(itemStack); return c2s(wrap(item)).getItem();
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();
}
} }
@Override @Override

View File

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

View File

@@ -30,96 +30,104 @@ import java.util.function.Supplier;
public final class ModernNetworkItemHandler implements NetworkItemHandler<ItemStack> { public final class ModernNetworkItemHandler implements NetworkItemHandler<ItemStack> {
@Override @Override
public Optional<Item<ItemStack>> c2s(Item<ItemStack> wrapped) { public Item<ItemStack> c2s(Item<ItemStack> wrapped) {
Tag customData = wrapped.getSparrowNBTComponent(ComponentTypes.CUSTOM_DATA); // 先尝试恢复client-bound-material
if (!(customData instanceof CompoundTag compoundTag)) return Optional.empty();
Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem(); Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem();
boolean hasDifferentMaterial = false;
if (optionalCustomItem.isPresent()) { if (optionalCustomItem.isPresent()) {
BukkitCustomItem customItem = (BukkitCustomItem) optionalCustomItem.get(); BukkitCustomItem customItem = (BukkitCustomItem) optionalCustomItem.get();
if (customItem.item() != FastNMS.INSTANCE.method$ItemStack$getItem(wrapped.getLiteralObject())) { if (customItem.item() != FastNMS.INSTANCE.method$ItemStack$getItem(wrapped.getLiteralObject())) {
wrapped = wrapped.unsafeTransmuteCopy(customItem.item(), wrapped.count()); 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); CompoundTag networkData = compoundTag.getCompound(NETWORK_ITEM_TAG);
if (networkData == null) { if (networkData == null) {
if (hasDifferentMaterial) { return wrapped;
return Optional.of(wrapped);
}
return Optional.empty();
} }
// 移除此tag
compoundTag.remove(NETWORK_ITEM_TAG); compoundTag.remove(NETWORK_ITEM_TAG);
for (Map.Entry<String, Tag> entry : networkData.entrySet()) { for (Map.Entry<String, Tag> entry : networkData.entrySet()) {
if (entry.getValue() instanceof CompoundTag tag) { if (entry.getValue() instanceof CompoundTag tag) {
NetworkItemHandler.apply(entry.getKey(), tag, wrapped); NetworkItemHandler.apply(entry.getKey(), tag, wrapped);
} }
} }
if (compoundTag.isEmpty()) wrapped.resetComponent(ComponentTypes.CUSTOM_DATA); if (compoundTag.isEmpty()) wrapped.resetComponent(ComponentTypes.CUSTOM_DATA);
else wrapped.setNBTComponent(ComponentTypes.CUSTOM_DATA, compoundTag); else wrapped.setNBTComponent(ComponentTypes.CUSTOM_DATA, compoundTag);
return Optional.of(wrapped); return wrapped;
} }
@Override @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; Item<ItemStack> original = wrapped;
Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem(); Optional<CustomItem<ItemStack>> optionalCustomItem = wrapped.getCustomItem();
if (optionalCustomItem.isEmpty()) { if (optionalCustomItem.isEmpty()) {
if (!Config.interceptItem()) return Optional.empty(); if (!Config.interceptItem()) return wrapped;
return new OtherItem(wrapped, false).process(NetworkTextReplaceContext.of(player)); 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 { } else {
BukkitCustomItem customItem = (BukkitCustomItem) optionalCustomItem.get(); ContextHolder.Builder builder = ContextHolder.builder();
Object serverItem = FastNMS.INSTANCE.method$ItemStack$getItem(wrapped.getLiteralObject()); for (Map.Entry<String, Tag> entry : arguments.entrySet()) {
boolean hasDifferentMaterial = serverItem == customItem.item() && serverItem != customItem.clientItem(); builder.withParameter(ContextKey.direct(entry.getKey()), entry.getValue().getAsString());
if (hasDifferentMaterial) {
wrapped = wrapped.unsafeTransmuteCopy(customItem.clientItem(), wrapped.count());
} }
if (!customItem.hasClientBoundDataModifier()) { context = ItemBuildContext.of(player, builder);
if (!Config.interceptItem() && !hasDifferentMaterial) return Optional.empty(); }
return new OtherItem(wrapped, hasDifferentMaterial).process(NetworkTextReplaceContext.of(player)); CompoundTag tag = new CompoundTag();
} else { for (ItemDataModifier<ItemStack> modifier : customItem.clientBoundDataModifiers()) {
CompoundTag customData = Optional.ofNullable(wrapped.getSparrowNBTComponent(ComponentTypes.CUSTOM_DATA)).map(CompoundTag.class::cast).orElse(new CompoundTag()); modifier.prepareNetworkItem(original, context, tag);
CompoundTag arguments = customData.getCompound(ArgumentsModifier.ARGUMENTS_TAG); }
ItemBuildContext context; for (ItemDataModifier<ItemStack> modifier : customItem.clientBoundDataModifiers()) {
if (arguments == null) { modifier.apply(wrapped, context);
context = ItemBuildContext.of(player); }
} else { if (Config.interceptItem()) {
ContextHolder.Builder builder = ContextHolder.builder(); if (!tag.containsKey(ComponentIds.ITEM_NAME)) {
for (Map.Entry<String, Tag> entry : arguments.entrySet()) { if (VersionHelper.isOrAbove1_21_5()) processModernItemName(wrapped, () -> tag, context);
builder.withParameter(ContextKey.direct(entry.getKey()), entry.getValue().getAsString()); else processLegacyItemName(wrapped, () -> tag, context);
} }
context = ItemBuildContext.of(player, builder); if (!tag.containsKey(ComponentIds.CUSTOM_NAME)) {
} if (VersionHelper.isOrAbove1_21_5()) processModernCustomName(wrapped, () -> tag, context);
CompoundTag tag = new CompoundTag(); else processLegacyCustomName(wrapped, () -> tag, context);
for (ItemDataModifier<ItemStack> modifier : customItem.clientBoundDataModifiers()) { }
modifier.prepareNetworkItem(original, context, tag); if (!tag.containsKey(ComponentIds.LORE)) {
} if (VersionHelper.isOrAbove1_21_5()) processModernLore(wrapped, () -> tag, context);
for (ItemDataModifier<ItemStack> modifier : customItem.clientBoundDataModifiers()) { else processLegacyLore(wrapped, () -> tag, context);
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);
} }
} }
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) { 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 { static class OtherItem {
private final Item<ItemStack> item; private final Item<ItemStack> item;
private final boolean forceReturn;
private boolean globalChanged = false; private boolean globalChanged = false;
private CompoundTag tag; private CompoundTag tag;
public OtherItem(Item<ItemStack> item, boolean forceReturn) { public OtherItem(Item<ItemStack> item) {
this.item = 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 (VersionHelper.isOrAbove1_21_5()) {
if (processModernLore(this.item, this::getOrCreateTag, context)) if (processModernLore(this.item, this::getOrCreateTag, context))
this.globalChanged = true; 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()); CompoundTag customData = Optional.ofNullable(this.item.getSparrowNBTComponent(ComponentTypes.CUSTOM_DATA)).map(CompoundTag.class::cast).orElse(new CompoundTag());
customData.put(NETWORK_ITEM_TAG, getOrCreateTag()); customData.put(NETWORK_ITEM_TAG, getOrCreateTag());
this.item.setNBTComponent(ComponentKeys.CUSTOM_DATA, customData); 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() { private CompoundTag getOrCreateTag() {

View File

@@ -1,6 +1,5 @@
package net.momirealms.craftengine.bukkit.item.behavior; 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.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections; import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MFluids; 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.behavior.ItemBehaviorFactory;
import net.momirealms.craftengine.core.item.context.UseOnContext; import net.momirealms.craftengine.core.item.context.UseOnContext;
import net.momirealms.craftengine.core.pack.Pack; 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.CraftEngine;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException; import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Direction; import net.momirealms.craftengine.core.util.Direction;
import net.momirealms.craftengine.core.util.Key; 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.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.world.BlockHitResult; import net.momirealms.craftengine.core.world.BlockHitResult;
import net.momirealms.craftengine.core.world.BlockPos; 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 stateId = buf.readVarInt();
int listSize = buf.readVarInt(); int listSize = buf.readVarInt();
List<ItemStack> items = new ArrayList<>(listSize); List<ItemStack> items = new ArrayList<>(listSize);
boolean changed = false;
Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf); Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
for (int i = 0; i < listSize; i++) { for (int i = 0; i < listSize; i++) {
ItemStack itemStack = FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf); ItemStack itemStack = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf), serverPlayer);
Optional<ItemStack> optional = BukkitItemManager.instance().s2c(itemStack, serverPlayer); items.add(itemStack);
if (optional.isPresent()) {
items.add(optional.get());
changed = true;
} else {
items.add(itemStack);
}
} }
ItemStack carriedItem = FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf); ItemStack carriedItem = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf), serverPlayer);
ItemStack newCarriedItem = carriedItem;
Optional<ItemStack> optional = BukkitItemManager.instance().s2c(carriedItem, serverPlayer);
if (optional.isPresent()) {
changed = true;
newCarriedItem = optional.get();
}
if (!changed) return;
event.setChanged(true); event.setChanged(true);
buf.clear(); buf.clear();
buf.writeVarInt(event.packetID()); buf.writeVarInt(event.packetID());
@@ -3215,7 +3201,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
for (ItemStack itemStack : items) { for (ItemStack itemStack : items) {
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, itemStack); 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头上 // 其他插件干的比如某ty*****er不要赖到ce头上
return; return;
} }
BukkitItemManager.instance().s2c(itemStack, serverPlayer).ifPresent((newItemStack) -> { itemStack = BukkitItemManager.instance().s2c(itemStack, serverPlayer);
event.setChanged(true); event.setChanged(true);
buf.clear(); buf.clear();
buf.writeVarInt(event.packetID()); buf.writeVarInt(event.packetID());
buf.writeContainerId(containerId); buf.writeContainerId(containerId);
buf.writeVarInt(stateId); buf.writeVarInt(stateId);
buf.writeShort(slot); buf.writeShort(slot);
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf); Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, newItemStack); FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, itemStack);
});
} }
} }
@@ -3256,14 +3241,12 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
if (!(user instanceof BukkitServerPlayer serverPlayer)) return; if (!(user instanceof BukkitServerPlayer serverPlayer)) return;
FriendlyByteBuf buf = event.getBuffer(); FriendlyByteBuf buf = event.getBuffer();
Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf); Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
ItemStack itemStack = FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf); ItemStack itemStack = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf), serverPlayer);
BukkitItemManager.instance().s2c(itemStack, serverPlayer).ifPresent((newItemStack) -> { event.setChanged(true);
event.setChanged(true); buf.clear();
buf.clear(); buf.writeVarInt(event.packetID());
buf.writeVarInt(event.packetID()); Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf); FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, itemStack);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, newItemStack);
});
} }
} }
@@ -3273,7 +3256,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
public void onPacketSend(NetWorkUser user, ByteBufPacketEvent event) { public void onPacketSend(NetWorkUser user, ByteBufPacketEvent event) {
if (!(user instanceof BukkitServerPlayer serverPlayer)) return; if (!(user instanceof BukkitServerPlayer serverPlayer)) return;
FriendlyByteBuf buf = event.getBuffer(); FriendlyByteBuf buf = event.getBuffer();
boolean changed = false;
Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf); Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
int entity = buf.readVarInt(); int entity = buf.readVarInt();
List<com.mojang.datafixers.util.Pair<Object, ItemStack>> slots = Lists.newArrayList(); List<com.mojang.datafixers.util.Pair<Object, ItemStack>> slots = Lists.newArrayList();
@@ -3281,29 +3264,23 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
do { do {
slotMask = buf.readByte(); slotMask = buf.readByte();
Object equipmentSlot = CoreReflections.instance$EquipmentSlot$values[slotMask & 127]; Object equipmentSlot = CoreReflections.instance$EquipmentSlot$values[slotMask & 127];
ItemStack itemStack = FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf); ItemStack itemStack = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf), serverPlayer);
Optional<ItemStack> optional = BukkitItemManager.instance().s2c(itemStack, serverPlayer);
if (optional.isPresent()) {
changed = true;
itemStack = optional.get();
}
slots.add(com.mojang.datafixers.util.Pair.of(equipmentSlot, itemStack)); slots.add(com.mojang.datafixers.util.Pair.of(equipmentSlot, itemStack));
} while ((slotMask & -128) != 0); } while ((slotMask & -128) != 0);
if (changed) {
event.setChanged(true); event.setChanged(true);
buf.clear(); buf.clear();
buf.writeVarInt(event.packetID()); buf.writeVarInt(event.packetID());
buf.writeVarInt(entity); buf.writeVarInt(entity);
int i = slots.size(); int i = slots.size();
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf); Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
for (int j = 0; j < i; ++j) { for (int j = 0; j < i; ++j) {
com.mojang.datafixers.util.Pair<Object, ItemStack> pair = slots.get(j); com.mojang.datafixers.util.Pair<Object, ItemStack> pair = slots.get(j);
Enum<?> equipmentSlot = (Enum<?>) pair.getFirst(); Enum<?> equipmentSlot = (Enum<?>) pair.getFirst();
boolean bl = j != i - 1; boolean bl = j != i - 1;
int k = equipmentSlot.ordinal(); int k = equipmentSlot.ordinal();
buf.writeByte(bl ? k | -128 : k); buf.writeByte(bl ? k | -128 : k);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, pair.getSecond()); FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, pair.getSecond());
}
} }
} }
} }
@@ -3316,15 +3293,14 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
FriendlyByteBuf buf = event.getBuffer(); FriendlyByteBuf buf = event.getBuffer();
int slot = buf.readVarInt(); int slot = buf.readVarInt();
Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf); Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
ItemStack itemStack = FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf); ItemStack itemStack = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf), serverPlayer);
BukkitItemManager.instance().s2c(itemStack, serverPlayer).ifPresent((newItemStack) -> {
event.setChanged(true); event.setChanged(true);
buf.clear(); buf.clear();
buf.writeVarInt(event.packetID()); buf.writeVarInt(event.packetID());
buf.writeVarInt(slot); buf.writeVarInt(slot);
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf); Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, newItemStack); FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, itemStack);
});
} }
} }
@@ -3344,18 +3320,18 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
} catch (Exception e) { } catch (Exception e) {
return; return;
} }
BukkitItemManager.instance().c2s(itemStack).ifPresent((newItemStack) -> { itemStack = BukkitItemManager.instance().s2c(itemStack, serverPlayer);
event.setChanged(true);
buf.clear(); event.setChanged(true);
buf.writeVarInt(event.packetID()); buf.clear();
buf.writeShort(slotNum); buf.writeVarInt(event.packetID());
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf); buf.writeShort(slotNum);
if (VersionHelper.isOrAbove1_20_5()) { Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeUntrustedItem(newFriendlyBuf, newItemStack); if (VersionHelper.isOrAbove1_20_5()) {
} else { FastNMS.INSTANCE.method$FriendlyByteBuf$writeUntrustedItem(newFriendlyBuf, itemStack);
FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, newItemStack); } else {
} FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, itemStack);
}); }
} }
} }
@@ -3364,7 +3340,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
@Override @Override
public void onPacketReceive(NetWorkUser user, ByteBufPacketEvent event) { public void onPacketReceive(NetWorkUser user, ByteBufPacketEvent event) {
FriendlyByteBuf buf = event.getBuffer(); FriendlyByteBuf buf = event.getBuffer();
boolean changed = false;
Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf); Object friendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
int containerId = buf.readContainerId(); int containerId = buf.readContainerId();
int stateId = buf.readVarInt(); int stateId = buf.readVarInt();
@@ -3375,37 +3351,27 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
Int2ObjectMap<ItemStack> changedSlots = new Int2ObjectOpenHashMap<>(i); Int2ObjectMap<ItemStack> changedSlots = new Int2ObjectOpenHashMap<>(i);
for (int j = 0; j < i; ++j) { for (int j = 0; j < i; ++j) {
int k = buf.readShort(); int k = buf.readShort();
ItemStack itemStack = FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf); ItemStack itemStack = BukkitItemManager.instance().c2s(FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf));
Optional<ItemStack> optional = BukkitItemManager.instance().c2s(itemStack);
if (optional.isPresent()) {
changed = true;
itemStack = optional.get();
}
changedSlots.put(k, itemStack); changedSlots.put(k, itemStack);
} }
ItemStack carriedItem = FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf); ItemStack carriedItem = BukkitItemManager.instance().c2s(FastNMS.INSTANCE.method$FriendlyByteBuf$readItem(friendlyBuf));
Optional<ItemStack> optional = BukkitItemManager.instance().c2s(carriedItem);
if (optional.isPresent()) { event.setChanged(true);
changed = true; buf.clear();
carriedItem = optional.get(); buf.writeVarInt(event.packetID());
} buf.writeContainerId(containerId);
if (changed) { buf.writeVarInt(stateId);
event.setChanged(true); buf.writeShort(slotNum);
buf.clear(); buf.writeByte(buttonNum);
buf.writeVarInt(event.packetID()); buf.writeVarInt(clickType);
buf.writeContainerId(containerId); buf.writeVarInt(changedSlots.size());
buf.writeVarInt(stateId); Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf);
buf.writeShort(slotNum); changedSlots.forEach((k, v) -> {
buf.writeByte(buttonNum); buf.writeShort(k);
buf.writeVarInt(clickType); FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, v);
buf.writeVarInt(changedSlots.size()); });
Object newFriendlyBuf = FastNMS.INSTANCE.constructor$FriendlyByteBuf(buf); FastNMS.INSTANCE.method$FriendlyByteBuf$writeItem(newFriendlyBuf, carriedItem);
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 org.bukkit.inventory.ItemStack;
import java.util.List; import java.util.List;
import java.util.Optional;
public class CommonItemPacketHandler implements EntityPacketHandler { public class CommonItemPacketHandler implements EntityPacketHandler {
public static final CommonItemPacketHandler INSTANCE = new CommonItemPacketHandler(); public static final CommonItemPacketHandler INSTANCE = new CommonItemPacketHandler();
@@ -23,7 +22,7 @@ public class CommonItemPacketHandler implements EntityPacketHandler {
public void handleSetEntityData(Player user, ByteBufPacketEvent event) { public void handleSetEntityData(Player user, ByteBufPacketEvent event) {
FriendlyByteBuf buf = event.getBuffer(); FriendlyByteBuf buf = event.getBuffer();
int id = buf.readVarInt(); int id = buf.readVarInt();
boolean isChanged = false;
List<Object> packedItems = FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$unpack(buf); List<Object> packedItems = FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$unpack(buf);
for (int i = 0; i < packedItems.size(); i++) { for (int i = 0; i < packedItems.size(); i++) {
Object packedItem = packedItems.get(i); Object packedItem = packedItems.get(i);
@@ -40,23 +39,16 @@ public class CommonItemPacketHandler implements EntityPacketHandler {
} }
continue; continue;
} }
ItemStack itemStack = FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(nmsItemStack); ItemStack itemStack = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(nmsItemStack), user);
Optional<ItemStack> optional = BukkitItemManager.instance().s2c(itemStack, user);
if (optional.isEmpty()) continue;
isChanged = true;
itemStack = optional.get();
Object serializer = FastNMS.INSTANCE.field$SynchedEntityData$DataValue$serializer(packedItem); Object serializer = FastNMS.INSTANCE.field$SynchedEntityData$DataValue$serializer(packedItem);
packedItems.set(i, FastNMS.INSTANCE.constructor$SynchedEntityData$DataValue( packedItems.set(i, FastNMS.INSTANCE.constructor$SynchedEntityData$DataValue(entityDataId, serializer, FastNMS.INSTANCE.method$CraftItemStack$asNMSCopy(itemStack)));
entityDataId, serializer, FastNMS.INSTANCE.method$CraftItemStack$asNMSCopy(itemStack)
));
break; break;
} }
if (isChanged) {
event.setChanged(true); event.setChanged(true);
buf.clear(); buf.clear();
buf.writeVarInt(event.packetID()); buf.writeVarInt(event.packetID());
buf.writeVarInt(id); buf.writeVarInt(id);
FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$pack(packedItems, buf); 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.entity.data.ItemDisplayEntityData;
import net.momirealms.craftengine.bukkit.item.BukkitItemManager; import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
import net.momirealms.craftengine.bukkit.nms.FastNMS; 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.entity.player.Player;
import net.momirealms.craftengine.core.plugin.network.ByteBufPacketEvent; import net.momirealms.craftengine.core.plugin.network.ByteBufPacketEvent;
import net.momirealms.craftengine.core.plugin.network.EntityPacketHandler; 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 org.bukkit.inventory.ItemStack;
import java.util.List; import java.util.List;
import java.util.Optional;
public class ItemDisplayPacketHandler implements EntityPacketHandler { public class ItemDisplayPacketHandler implements EntityPacketHandler {
public static final ItemDisplayPacketHandler INSTANCE = new ItemDisplayPacketHandler(); public static final ItemDisplayPacketHandler INSTANCE = new ItemDisplayPacketHandler();
@@ -20,30 +18,24 @@ public class ItemDisplayPacketHandler implements EntityPacketHandler {
public void handleSetEntityData(Player user, ByteBufPacketEvent event) { public void handleSetEntityData(Player user, ByteBufPacketEvent event) {
FriendlyByteBuf buf = event.getBuffer(); FriendlyByteBuf buf = event.getBuffer();
int id = buf.readVarInt(); int id = buf.readVarInt();
boolean isChanged = false;
List<Object> packedItems = FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$unpack(buf); List<Object> packedItems = FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$unpack(buf);
for (int i = 0; i < packedItems.size(); i++) { for (int i = 0; i < packedItems.size(); i++) {
Object packedItem = packedItems.get(i); Object packedItem = packedItems.get(i);
int entityDataId = FastNMS.INSTANCE.field$SynchedEntityData$DataValue$id(packedItem); int entityDataId = FastNMS.INSTANCE.field$SynchedEntityData$DataValue$id(packedItem);
if (entityDataId != ItemDisplayEntityData.DisplayedItem.id()) continue; if (entityDataId != ItemDisplayEntityData.DisplayedItem.id()) continue;
Object nmsItemStack = FastNMS.INSTANCE.field$SynchedEntityData$DataValue$value(packedItem); Object nmsItemStack = FastNMS.INSTANCE.field$SynchedEntityData$DataValue$value(packedItem);
ItemStack itemStack = FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(nmsItemStack); ItemStack itemStack = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(nmsItemStack), user);
Optional<ItemStack> optional = BukkitItemManager.instance().s2c(itemStack, (BukkitServerPlayer) user);
if (optional.isEmpty()) continue;
isChanged = true;
itemStack = optional.get();
Object serializer = FastNMS.INSTANCE.field$SynchedEntityData$DataValue$serializer(packedItem); Object serializer = FastNMS.INSTANCE.field$SynchedEntityData$DataValue$serializer(packedItem);
packedItems.set(i, FastNMS.INSTANCE.constructor$SynchedEntityData$DataValue( packedItems.set(i, FastNMS.INSTANCE.constructor$SynchedEntityData$DataValue(
entityDataId, serializer, FastNMS.INSTANCE.method$CraftItemStack$asNMSCopy(itemStack) entityDataId, serializer, FastNMS.INSTANCE.method$CraftItemStack$asNMSCopy(itemStack)
)); ));
break; break;
} }
if (isChanged) {
event.setChanged(true); event.setChanged(true);
buf.clear(); buf.clear();
buf.writeVarInt(event.packetID()); buf.writeVarInt(event.packetID());
buf.writeVarInt(id); buf.writeVarInt(id);
FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$pack(packedItems, buf); 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 org.bukkit.inventory.ItemStack;
import java.util.List; import java.util.List;
import java.util.Optional;
public class ItemFramePacketHandler implements EntityPacketHandler { public class ItemFramePacketHandler implements EntityPacketHandler {
public static final ItemFramePacketHandler INSTANCE = new ItemFramePacketHandler(); public static final ItemFramePacketHandler INSTANCE = new ItemFramePacketHandler();
@@ -23,7 +22,6 @@ public class ItemFramePacketHandler implements EntityPacketHandler {
public void handleSetEntityData(Player user, ByteBufPacketEvent event) { public void handleSetEntityData(Player user, ByteBufPacketEvent event) {
FriendlyByteBuf buf = event.getBuffer(); FriendlyByteBuf buf = event.getBuffer();
int id = buf.readVarInt(); int id = buf.readVarInt();
boolean isChanged = false;
List<Object> packedItems = FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$unpack(buf); List<Object> packedItems = FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$unpack(buf);
for (int i = 0; i < packedItems.size(); i++) { for (int i = 0; i < packedItems.size(); i++) {
Object packedItem = packedItems.get(i); Object packedItem = packedItems.get(i);
@@ -40,23 +38,18 @@ public class ItemFramePacketHandler implements EntityPacketHandler {
} }
continue; continue;
} }
ItemStack itemStack = FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(nmsItemStack); ItemStack itemStack = BukkitItemManager.instance().s2c(FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(nmsItemStack), user);
Optional<ItemStack> optional = BukkitItemManager.instance().s2c(itemStack, user);
if (optional.isEmpty()) continue;
isChanged = true;
itemStack = optional.get();
Object serializer = FastNMS.INSTANCE.field$SynchedEntityData$DataValue$serializer(packedItem); Object serializer = FastNMS.INSTANCE.field$SynchedEntityData$DataValue$serializer(packedItem);
packedItems.set(i, FastNMS.INSTANCE.constructor$SynchedEntityData$DataValue( packedItems.set(i, FastNMS.INSTANCE.constructor$SynchedEntityData$DataValue(
entityDataId, serializer, FastNMS.INSTANCE.method$CraftItemStack$asNMSCopy(itemStack) entityDataId, serializer, FastNMS.INSTANCE.method$CraftItemStack$asNMSCopy(itemStack)
)); ));
break; break;
} }
if (isChanged) {
event.setChanged(true); event.setChanged(true);
buf.clear(); buf.clear();
buf.writeVarInt(event.packetID()); buf.writeVarInt(event.packetID());
buf.writeVarInt(id); buf.writeVarInt(id);
FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$pack(packedItems, buf); FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$pack(packedItems, buf);
}
} }
} }

View File

@@ -29,14 +29,10 @@ import java.util.UUID;
public class ProjectilePacketHandler implements EntityPacketHandler { public class ProjectilePacketHandler implements EntityPacketHandler {
private final int entityId; private final int entityId;
private final BukkitCustomProjectile projectile; private final BukkitCustomProjectile projectile;
private final Object cachedPacket;
private final List<Object> cachedData;
public ProjectilePacketHandler(BukkitCustomProjectile projectile, int entityId) { public ProjectilePacketHandler(BukkitCustomProjectile projectile, int entityId) {
this.projectile = projectile; this.projectile = projectile;
this.entityId = entityId; this.entityId = entityId;
this.cachedData = createCustomProjectileEntityDataValues();
this.cachedPacket = FastNMS.INSTANCE.constructor$ClientboundSetEntityDataPacket(entityId, this.cachedData);
} }
@Override @Override
@@ -47,7 +43,7 @@ public class ProjectilePacketHandler implements EntityPacketHandler {
buf.clear(); buf.clear();
buf.writeVarInt(event.packetID()); buf.writeVarInt(event.packetID());
buf.writeVarInt(id); buf.writeVarInt(id);
FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$pack(this.cachedData, buf); FastNMS.INSTANCE.method$ClientboundSetEntityDataPacket$pack(this.createCustomProjectileEntityDataValues(user), buf);
} }
@Override @Override
@@ -60,7 +56,7 @@ public class ProjectilePacketHandler implements EntityPacketHandler {
public void handleMoveAndRotate(NetWorkUser user, NMSPacketEvent event, Object packet) { public void handleMoveAndRotate(NetWorkUser user, NMSPacketEvent event, Object packet) {
int entityId = ProtectedFieldVisitor.get().field$ClientboundMoveEntityPacket$entityId(packet); int entityId = ProtectedFieldVisitor.get().field$ClientboundMoveEntityPacket$entityId(packet);
event.replacePacket(FastNMS.INSTANCE.constructor$ClientboundBundlePacket(List.of( event.replacePacket(FastNMS.INSTANCE.constructor$ClientboundBundlePacket(List.of(
this.cachedPacket, FastNMS.INSTANCE.constructor$ClientboundSetEntityDataPacket(entityId, this.createCustomProjectileEntityDataValues((Player) user)),
convertCustomProjectileMovePacket(packet, entityId) convertCustomProjectileMovePacket(packet, entityId)
))); )));
} }
@@ -110,14 +106,14 @@ public class ProjectilePacketHandler implements EntityPacketHandler {
return FastNMS.INSTANCE.constructor$ClientboundEntityPositionSyncPacket(entityId, newPositionMoveRotation, onGround); return FastNMS.INSTANCE.constructor$ClientboundEntityPositionSyncPacket(entityId, newPositionMoveRotation, onGround);
} }
public List<Object> createCustomProjectileEntityDataValues() { public List<Object> createCustomProjectileEntityDataValues(Player player) {
List<Object> itemDisplayValues = new ArrayList<>(); List<Object> itemDisplayValues = new ArrayList<>();
Optional<CustomItem<ItemStack>> customItem = BukkitItemManager.instance().getCustomItem(this.projectile.metadata().item()); Optional<CustomItem<ItemStack>> customItem = BukkitItemManager.instance().getCustomItem(this.projectile.metadata().item());
if (customItem.isEmpty()) return itemDisplayValues; if (customItem.isEmpty()) return itemDisplayValues;
ProjectileMeta meta = this.projectile.metadata(); ProjectileMeta meta = this.projectile.metadata();
Item<ItemStack> displayedItem = customItem.get().buildItem(ItemBuildContext.empty()); 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.InterpolationDelay.addEntityDataIfNotDefaultValue(-1, itemDisplayValues);
ItemDisplayEntityData.Translation.addEntityDataIfNotDefaultValue(meta.translation(), itemDisplayValues); ItemDisplayEntityData.Translation.addEntityDataIfNotDefaultValue(meta.translation(), itemDisplayValues);
ItemDisplayEntityData.Scale.addEntityDataIfNotDefaultValue(meta.scale(), itemDisplayValues); ItemDisplayEntityData.Scale.addEntityDataIfNotDefaultValue(meta.scale(), itemDisplayValues);
@@ -130,9 +126,7 @@ public class ProjectilePacketHandler implements EntityPacketHandler {
} }
Object literalItem = displayedItem.getLiteralObject(); Object literalItem = displayedItem.getLiteralObject();
BukkitItemManager.instance().s2c(displayedItem.getItem(), null).ifPresentOrElse( ItemDisplayEntityData.DisplayedItem.addEntityDataIfNotDefaultValue(literalItem, itemDisplayValues);
it -> ItemDisplayEntityData.DisplayedItem.addEntityDataIfNotDefaultValue(FastNMS.INSTANCE.field$CraftItemStack$handle(it), itemDisplayValues),
() -> ItemDisplayEntityData.DisplayedItem.addEntityDataIfNotDefaultValue(literalItem, itemDisplayValues));
ItemDisplayEntityData.DisplayType.addEntityDataIfNotDefaultValue(meta.displayType().id(), itemDisplayValues); ItemDisplayEntityData.DisplayType.addEntityDataIfNotDefaultValue(meta.displayType().id(), itemDisplayValues);
ItemDisplayEntityData.BillboardConstraints.addEntityDataIfNotDefaultValue(meta.billboard().id(), itemDisplayValues); ItemDisplayEntityData.BillboardConstraints.addEntityDataIfNotDefaultValue(meta.billboard().id(), itemDisplayValues);
return itemDisplayValues; return itemDisplayValues;

View File

@@ -10,7 +10,6 @@ import net.momirealms.sparrow.nbt.Tag;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Map; import java.util.Map;
import java.util.Optional;
public interface NetworkItemHandler<T> { public interface NetworkItemHandler<T> {
Operation[] BY_INDEX = new Operation[] {Operation.ADD, Operation.REMOVE, Operation.RESET}; 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_OPERATION = "type";
String NETWORK_VALUE = "value"; 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) { static CompoundTag pack(Operation operation, @Nullable Tag value) {
if (value == null) { if (value == null) {

View File

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