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

fix(network): 修复

This commit is contained in:
jhqwqmc
2025-05-27 00:01:15 +08:00
parent 5e35590205
commit cfc25df325
3 changed files with 42 additions and 18 deletions

View File

@@ -21,6 +21,7 @@ import net.momirealms.craftengine.bukkit.plugin.injector.BukkitInjector;
import net.momirealms.craftengine.bukkit.plugin.network.handler.*;
import net.momirealms.craftengine.bukkit.plugin.network.payload.DiscardedPayload;
import net.momirealms.craftengine.bukkit.plugin.network.payload.NetWorkDataTypes;
import net.momirealms.craftengine.bukkit.plugin.network.payload.Payload;
import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer;
import net.momirealms.craftengine.bukkit.util.*;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
@@ -1970,8 +1971,9 @@ public class PacketConsumers {
try {
if (!VersionHelper.isOrAbove1_20_5()) return;
Object payload = Reflections.field$ServerboundCustomPayloadPacket$payload.get(packet);
if (payload.getClass().equals(Reflections.clazz$DiscardedPayload)) {
DiscardedPayload discardedPayload = DiscardedPayload.decode(payload);
if (Reflections.clazz$DiscardedPayload.isInstance(payload)) {
Payload discardedPayload = DiscardedPayload.from(payload);
if (discardedPayload == null || !discardedPayload.channel().equals(NetworkManager.MOD_CHANNEL_KEY)) return;
FriendlyByteBuf buf = discardedPayload.toBuffer();
NetWorkDataTypes<?> dataType = NetWorkDataTypes.readType(buf);
if (dataType == NetWorkDataTypes.CLIENT_CUSTOM_BLOCK) {

View File

@@ -3,30 +3,42 @@ package net.momirealms.craftengine.bukkit.plugin.network.payload;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import net.momirealms.craftengine.core.util.Key;
import java.lang.reflect.InvocationTargetException;
public record DiscardedPayload(Key id, byte[] data) {
public record DiscardedPayload(Key channel, Object rawPayload) implements Payload {
public static final boolean useNewMethod = Reflections.method$DiscardedPayload$data == null;
public static DiscardedPayload decode(Object payload) throws InvocationTargetException, IllegalAccessException {
public static DiscardedPayload from(Object payload) {
try {
Object type = Reflections.method$CustomPacketPayload$type.invoke(payload);
Object id = Reflections.method$CustomPacketPayload$Type$id.invoke(type);
Key channel = Key.of(id.toString());
byte[] data;
if (useNewMethod) {
data = (byte[]) Reflections.method$DiscardedPayload$dataByteArray.invoke(payload);
} else {
ByteBuf buf = (ByteBuf) Reflections.method$DiscardedPayload$data.invoke(payload);
data = new byte[buf.readableBytes()];
buf.readBytes(data);
return new DiscardedPayload(channel, payload);
} catch (Exception e) {
CraftEngine.instance().logger().warn("Failed to create DiscardedPayload", e);
return null;
}
}
public byte[] getData() {
try {
if (useNewMethod) {
return (byte[]) Reflections.method$DiscardedPayload$dataByteArray.invoke(this.rawPayload());
} else {
ByteBuf buf = (ByteBuf) Reflections.method$DiscardedPayload$data.invoke(this.rawPayload());
byte[] data = new byte[buf.readableBytes()];
buf.readBytes(data);
return data;
}
} catch (Exception e) {
CraftEngine.instance().logger().warn("Failed to get data from DiscardedPayload", e);
return new byte[0];
}
return new DiscardedPayload(channel, data);
}
public FriendlyByteBuf toBuffer() {
return new FriendlyByteBuf(Unpooled.wrappedBuffer(this.data()));
return new FriendlyByteBuf(Unpooled.wrappedBuffer(this.getData()));
}
}

View File

@@ -0,0 +1,10 @@
package net.momirealms.craftengine.bukkit.plugin.network.payload;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import net.momirealms.craftengine.core.util.Key;
public interface Payload {
FriendlyByteBuf toBuffer();
Key channel();
}