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:
@@ -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.handler.*;
|
||||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.DiscardedPayload;
|
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.NetWorkDataTypes;
|
||||||
|
import net.momirealms.craftengine.bukkit.plugin.network.payload.Payload;
|
||||||
import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer;
|
import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer;
|
||||||
import net.momirealms.craftengine.bukkit.util.*;
|
import net.momirealms.craftengine.bukkit.util.*;
|
||||||
import net.momirealms.craftengine.core.block.ImmutableBlockState;
|
import net.momirealms.craftengine.core.block.ImmutableBlockState;
|
||||||
@@ -1970,8 +1971,9 @@ public class PacketConsumers {
|
|||||||
try {
|
try {
|
||||||
if (!VersionHelper.isOrAbove1_20_5()) return;
|
if (!VersionHelper.isOrAbove1_20_5()) return;
|
||||||
Object payload = Reflections.field$ServerboundCustomPayloadPacket$payload.get(packet);
|
Object payload = Reflections.field$ServerboundCustomPayloadPacket$payload.get(packet);
|
||||||
if (payload.getClass().equals(Reflections.clazz$DiscardedPayload)) {
|
if (Reflections.clazz$DiscardedPayload.isInstance(payload)) {
|
||||||
DiscardedPayload discardedPayload = DiscardedPayload.decode(payload);
|
Payload discardedPayload = DiscardedPayload.from(payload);
|
||||||
|
if (discardedPayload == null || !discardedPayload.channel().equals(NetworkManager.MOD_CHANNEL_KEY)) return;
|
||||||
FriendlyByteBuf buf = discardedPayload.toBuffer();
|
FriendlyByteBuf buf = discardedPayload.toBuffer();
|
||||||
NetWorkDataTypes<?> dataType = NetWorkDataTypes.readType(buf);
|
NetWorkDataTypes<?> dataType = NetWorkDataTypes.readType(buf);
|
||||||
if (dataType == NetWorkDataTypes.CLIENT_CUSTOM_BLOCK) {
|
if (dataType == NetWorkDataTypes.CLIENT_CUSTOM_BLOCK) {
|
||||||
|
|||||||
@@ -3,30 +3,42 @@ package net.momirealms.craftengine.bukkit.plugin.network.payload;
|
|||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
import net.momirealms.craftengine.bukkit.util.Reflections;
|
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.FriendlyByteBuf;
|
||||||
import net.momirealms.craftengine.core.util.Key;
|
import net.momirealms.craftengine.core.util.Key;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
public record DiscardedPayload(Key channel, Object rawPayload) implements Payload {
|
||||||
|
|
||||||
public record DiscardedPayload(Key id, byte[] data) {
|
|
||||||
public static final boolean useNewMethod = Reflections.method$DiscardedPayload$data == null;
|
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 type = Reflections.method$CustomPacketPayload$type.invoke(payload);
|
||||||
Object id = Reflections.method$CustomPacketPayload$Type$id.invoke(type);
|
Object id = Reflections.method$CustomPacketPayload$Type$id.invoke(type);
|
||||||
Key channel = Key.of(id.toString());
|
Key channel = Key.of(id.toString());
|
||||||
byte[] data;
|
return new DiscardedPayload(channel, payload);
|
||||||
if (useNewMethod) {
|
} catch (Exception e) {
|
||||||
data = (byte[]) Reflections.method$DiscardedPayload$dataByteArray.invoke(payload);
|
CraftEngine.instance().logger().warn("Failed to create DiscardedPayload", e);
|
||||||
} else {
|
return null;
|
||||||
ByteBuf buf = (ByteBuf) Reflections.method$DiscardedPayload$data.invoke(payload);
|
}
|
||||||
data = new byte[buf.readableBytes()];
|
}
|
||||||
buf.readBytes(data);
|
|
||||||
|
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() {
|
public FriendlyByteBuf toBuffer() {
|
||||||
return new FriendlyByteBuf(Unpooled.wrappedBuffer(this.data()));
|
return new FriendlyByteBuf(Unpooled.wrappedBuffer(this.getData()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user