9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-30 20:39:10 +00:00
This commit is contained in:
XiaoMoMi
2025-05-29 21:29:01 +08:00
parent b459dfec1c
commit fed73b746b
7 changed files with 30 additions and 52 deletions

View File

@@ -2,6 +2,7 @@ package net.momirealms.craftengine.bukkit.item;
import com.saicone.rtag.RtagItem;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.core.item.ItemWrapper;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.util.VersionHelper;
@@ -32,13 +33,7 @@ public class LegacyItemWrapper implements ItemWrapper<ItemStack> {
public boolean setTag(Object value, Object... path) {
if (value instanceof Tag tag) {
try {
Object nmsTag = FastNMS.INSTANCE.method$NbtIo$fromBytes(NBT.toBytes(tag, true));
return this.rtagItem.set(nmsTag, path);
} catch (IOException e) {
CraftEngine.instance().logger().warn("Failed to set NBT tag " + Arrays.toString(path), e);
return false;
}
return this.rtagItem.set(Reflections.instance$SPARROW_NBT_OPS.convertTo(Reflections.instance$NBT_OPS, tag), path);
} else {
return this.rtagItem.set(value, path);
}
@@ -46,15 +41,7 @@ public class LegacyItemWrapper implements ItemWrapper<ItemStack> {
public boolean add(Object value, Object... path) {
if (value instanceof Tag tag) {
try {
// Incompatible DFU version
// return this.rtagItem.add(Reflections.instance$SPARROW_NBT_OPS.convertTo(Reflections.instance$NBT_OPS, tag), path);
Object nmsTag = FastNMS.INSTANCE.method$NbtIo$fromBytes(NBT.toBytes(tag, true));
return this.rtagItem.add(nmsTag, path);
} catch (IOException e) {
CraftEngine.instance().logger().warn("Failed to add NBT tag " + Arrays.toString(path), e);
return false;
}
return this.rtagItem.add(Reflections.instance$SPARROW_NBT_OPS.convertTo(Reflections.instance$NBT_OPS, tag), path);
} else {
return this.rtagItem.add(value, path);
}
@@ -67,12 +54,7 @@ public class LegacyItemWrapper implements ItemWrapper<ItemStack> {
public Tag getNBTTag(Object... path) {
Object tag = getExactTag(path);
if (tag == null) return null;
try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(FastNMS.INSTANCE.method$NbtIo$toBytes(tag)))) {
return NBT.readUnnamedTag(dis, !VersionHelper.isOrAbove1_20_3());
} catch (IOException e) {
CraftEngine.instance().logger().warn("Failed to read NBT tag " + Arrays.toString(path), e);
return null;
}
return Reflections.instance$NBT_OPS.convertTo(Reflections.instance$SPARROW_NBT_OPS, tag);
}
public int count() {

View File

@@ -565,17 +565,25 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
}
private boolean handleCompression(ChannelHandlerContext ctx, ByteBuf buffer) {
if (handledCompression) return false;
if (this.handledCompression) return false;
int compressIndex = ctx.pipeline().names().indexOf("compress");
if (compressIndex == -1) return false;
handledCompression = true;
this.handledCompression = true;
int encoderIndex = ctx.pipeline().names().indexOf(PACKET_ENCODER);
if (encoderIndex == -1) return false;
if (compressIndex > encoderIndex) {
decompress(ctx, buffer, buffer);
PluginChannelDecoder decoder = (PluginChannelDecoder) ctx.pipeline().get(PACKET_DECODER);
if (decoder != null) {
if (decoder.relocated) return true;
decoder.relocated = true;
}
PluginChannelEncoder encoder = (PluginChannelEncoder) ctx.pipeline().remove(PACKET_ENCODER);
String encoderName = ctx.pipeline().names().contains("outbound_config") ? "outbound_config" : "encoder";
ctx.pipeline().addBefore(encoderName, PACKET_ENCODER, new PluginChannelEncoder(encoder));
decoder = (PluginChannelDecoder) ctx.pipeline().remove(PACKET_DECODER);
String decoderName = ctx.pipeline().names().contains("inbound_config") ? "inbound_config" : "decoder";
ctx.pipeline().addBefore(decoderName, PACKET_DECODER, new PluginChannelDecoder(decoder));
return true;
}
return false;
@@ -608,7 +616,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
public class PluginChannelDecoder extends MessageToMessageDecoder<ByteBuf> {
private final NetWorkUser player;
private boolean handledCompression = false;
public boolean relocated = false;
public PluginChannelDecoder(NetWorkUser player) {
this.player = player;
@@ -616,38 +624,17 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
public PluginChannelDecoder(PluginChannelDecoder decoder) {
this.player = decoder.player;
this.handledCompression = decoder.handledCompression;
this.relocated = decoder.relocated;
}
@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
boolean needCompression = !handledCompression && handleCompression(channelHandlerContext, byteBuf);
this.onByteBufReceive(byteBuf);
if (needCompression) {
compress(channelHandlerContext, byteBuf);
}
if (byteBuf.isReadable()) {
list.add(byteBuf.retain());
}
}
private boolean handleCompression(ChannelHandlerContext ctx, ByteBuf buffer) {
if (handledCompression) return false;
int compressIndex = ctx.pipeline().names().indexOf("compress");
if (compressIndex == -1) return false;
handledCompression = true;
int decoderIndex = ctx.pipeline().names().indexOf(PACKET_DECODER);
if (decoderIndex == -1) return false;
if (compressIndex > decoderIndex) {
decompress(ctx, buffer, buffer);
PluginChannelDecoder encoder = (PluginChannelDecoder) ctx.pipeline().remove(PACKET_DECODER);
String decoderName = ctx.pipeline().names().contains("inbound_config") ? "inbound_config" : "decoder";
ctx.pipeline().addBefore(decoderName, PACKET_DECODER, new PluginChannelDecoder(encoder));
return true;
}
return false;
}
private void onByteBufReceive(ByteBuf buffer) {
// I don't care packets before PLAY for the moment
if (player.decoderState() != ConnectionState.PLAY) return;

View File

@@ -17,6 +17,7 @@ import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.core.util.ReflectionUtils;
import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.sparrow.nbt.Tag;
import net.momirealms.sparrow.nbt.codec.LegacyNBTOps;
import net.momirealms.sparrow.nbt.codec.NBTOps;
import org.bukkit.NamespacedKey;
import org.bukkit.block.BlockState;
@@ -6969,7 +6970,7 @@ public class Reflections {
instance$JAVA_OPS = null;
}
instance$JSON_OPS = (DynamicOps<JsonElement>) method$RegistryOps$create.invoke(null, JsonOps.INSTANCE, instance$MinecraftRegistry);
instance$SPARROW_NBT_OPS = (DynamicOps<Tag>) method$RegistryOps$create.invoke(null, NBTOps.INSTANCE, instance$MinecraftRegistry);
instance$SPARROW_NBT_OPS = (DynamicOps<Tag>) method$RegistryOps$create.invoke(null, VersionHelper.isOrAbove1_20_5() ? NBTOps.INSTANCE : LegacyNBTOps.INSTANCE, instance$MinecraftRegistry);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}