9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2026-01-04 15:41:38 +00:00
This commit is contained in:
XiaoMoMi
2025-11-04 19:50:23 +08:00
7 changed files with 117 additions and 22 deletions

View File

@@ -4132,16 +4132,22 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
CompoundTag tag = (CompoundTag) buf.readNbt(named);
// todo 刷怪笼里的物品?
// 展示架
if (VersionHelper.isOrAbove1_21_9() && tag != null && tag.containsKey("Items")) {
// 通用方块实体存储的物品
if (tag != null && tag.containsKey("Items")) {
BukkitItemManager itemManager = BukkitItemManager.instance();
ListTag itemsTag = tag.getList("Items");
List<Pair<Byte, ItemStack>> items = new ArrayList<>();
for (Tag itemTag : itemsTag) {
if (itemTag instanceof CompoundTag itemCompoundTag) {
byte slot = itemCompoundTag.getByte("Slot");
Object nmsStack = CoreReflections.instance$ItemStack$CODEC.parse(MRegistryOps.SPARROW_NBT, itemCompoundTag)
.resultOrPartial((error) -> CraftEngine.instance().logger().severe("Tried to parse invalid item: '" + error + "'")).orElse(null);
Object nmsStack;
if (VersionHelper.isOrAbove1_20_5()) {
nmsStack = CoreReflections.instance$ItemStack$CODEC.parse(MRegistryOps.SPARROW_NBT, itemCompoundTag)
.resultOrPartial((error) -> CraftEngine.instance().logger().severe("Tried to parse invalid item: '" + error + "'")).orElse(null);
} else {
Object nmsTag = MRegistryOps.SPARROW_NBT.convertTo(MRegistryOps.NBT, itemTag);
nmsStack = FastNMS.INSTANCE.method$ItemStack$of(nmsTag);
}
ItemStack bukkitStack = FastNMS.INSTANCE.method$CraftItemStack$asCraftMirror(nmsStack);
Optional<ItemStack> optional = itemManager.s2c(bukkitStack, (BukkitServerPlayer) user);
if (optional.isPresent()) {
@@ -4155,8 +4161,14 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
if (changed) {
ListTag newItemsTag = new ListTag();
for (Pair<Byte, ItemStack> pair : items) {
CompoundTag newItemCompoundTag = (CompoundTag) CoreReflections.instance$ItemStack$CODEC.encodeStart(MRegistryOps.SPARROW_NBT, FastNMS.INSTANCE.field$CraftItemStack$handle(pair.right()))
.resultOrPartial((error) -> CraftEngine.instance().logger().severe("Tried to encode invalid item: '" + error + "'")).orElse(null);
CompoundTag newItemCompoundTag;
if (VersionHelper.isOrAbove1_20_5()) {
newItemCompoundTag = (CompoundTag) CoreReflections.instance$ItemStack$CODEC.encodeStart(MRegistryOps.SPARROW_NBT, FastNMS.INSTANCE.field$CraftItemStack$handle(pair.right()))
.resultOrPartial((error) -> CraftEngine.instance().logger().severe("Tried to encode invalid item: '" + error + "'")).orElse(null);
} else {
Object nmsTag = FastNMS.INSTANCE.method$itemStack$save(FastNMS.INSTANCE.field$CraftItemStack$handle(pair.right()), FastNMS.INSTANCE.constructor$CompoundTag());
newItemCompoundTag = (CompoundTag) MRegistryOps.NBT.convertTo(MRegistryOps.SPARROW_NBT, nmsTag);
}
if (newItemCompoundTag != null) {
newItemCompoundTag.putByte("Slot", pair.left());
newItemsTag.add(newItemCompoundTag);