9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-31 12:56:28 +00:00

refactor(network): 重构以提高可维护性

This commit is contained in:
jhqwqmc
2025-05-26 23:13:10 +08:00
parent f08544f82e
commit 5e35590205
6 changed files with 75 additions and 29 deletions

View File

@@ -19,6 +19,8 @@ import net.momirealms.craftengine.bukkit.pack.BukkitPackManager;
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
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.user.BukkitServerPlayer;
import net.momirealms.craftengine.bukkit.util.*;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
@@ -1969,35 +1971,18 @@ public class PacketConsumers {
if (!VersionHelper.isOrAbove1_20_5()) return;
Object payload = Reflections.field$ServerboundCustomPayloadPacket$payload.get(packet);
if (payload.getClass().equals(Reflections.clazz$DiscardedPayload)) {
Object type = Reflections.method$CustomPacketPayload$type.invoke(payload);
Object id = Reflections.method$CustomPacketPayload$Type$id.invoke(type);
String channel = id.toString();
if (!channel.equals(NetworkManager.MOD_CHANNEL)) return;
byte[] data;
if (Reflections.method$DiscardedPayload$data != null) {
ByteBuf buf = (ByteBuf) Reflections.method$DiscardedPayload$data.invoke(payload);
data = new byte[buf.readableBytes()];
buf.readBytes(data);
} else {
data = (byte[]) Reflections.method$DiscardedPayload$dataByteArray.invoke(payload);
}
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.wrappedBuffer(data));
DiscardedPayload discardedPayload = DiscardedPayload.decode(payload);
FriendlyByteBuf buf = discardedPayload.toBuffer();
NetWorkDataTypes<?> dataType = NetWorkDataTypes.readType(buf);
if (dataType == NetWorkDataTypes.CLIENT_CUSTOM_BLOCK) {
int clientBlockRegistrySize = dataType.as(Integer.class).decode(buf);
int serverBlockRegistrySize = RegistryUtils.currentBlockRegistrySize();
if (clientBlockRegistrySize != serverBlockRegistrySize) {
Object kickPacket = Reflections.constructor$ClientboundDisconnectPacket.newInstance(
ComponentUtils.adventureToMinecraft(
Component.translatable(
"disconnect.craftengine.block_registry_mismatch",
TranslationArgument.numeric(clientBlockRegistrySize),
TranslationArgument.numeric(serverBlockRegistrySize)
)
)
);
user.nettyChannel().writeAndFlush(kickPacket);
user.nettyChannel().disconnect();
user.kick(Component.translatable(
"disconnect.craftengine.block_registry_mismatch",
TranslationArgument.numeric(clientBlockRegistrySize),
TranslationArgument.numeric(serverBlockRegistrySize)
));
return;
}
user.setClientModState(true);
@@ -2007,10 +1992,7 @@ public class PacketConsumers {
FriendlyByteBuf bufPayload = new FriendlyByteBuf(Unpooled.buffer());
dataType.writeType(bufPayload);
dataType.as(Boolean.class).encode(bufPayload, true);
Object channelKey = KeyUtils.toResourceLocation(Key.of(NetworkManager.MOD_CHANNEL));
Object dataPayload = Reflections.constructor$DiscardedPayload.newInstance(channelKey, bufPayload.array());
Object responsePacket = Reflections.constructor$ClientboundCustomPayloadPacket.newInstance(dataPayload);
user.nettyChannel().writeAndFlush(responsePacket);
user.sendCustomPayload(NetworkManager.MOD_CHANNEL_KEY, bufPayload.array());
}
}
}

View File

@@ -0,0 +1,32 @@
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.util.FriendlyByteBuf;
import net.momirealms.craftengine.core.util.Key;
import java.lang.reflect.InvocationTargetException;
public record DiscardedPayload(Key id, byte[] data) {
public static final boolean useNewMethod = Reflections.method$DiscardedPayload$data == null;
public static DiscardedPayload decode(Object payload) throws InvocationTargetException, IllegalAccessException {
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, data);
}
public FriendlyByteBuf toBuffer() {
return new FriendlyByteBuf(Unpooled.wrappedBuffer(this.data()));
}
}

View File

@@ -1,4 +1,4 @@
package net.momirealms.craftengine.bukkit.plugin.network;
package net.momirealms.craftengine.bukkit.plugin.network.payload;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;

View File

@@ -287,6 +287,30 @@ public class BukkitServerPlayer extends Player {
this.plugin.networkManager().sendPacket(this, packet, immediately);
}
@Override
public void sendCustomPayload(Key channel, byte[] data) {
try {
Object channelKey = KeyUtils.toResourceLocation(channel);
Object dataPayload = Reflections.constructor$DiscardedPayload.newInstance(channelKey, data);
Object responsePacket = Reflections.constructor$ClientboundCustomPayloadPacket.newInstance(dataPayload);
this.nettyChannel().writeAndFlush(responsePacket);
} catch (Exception e) {
CraftEngine.instance().logger().warn("Failed to send custom payload to " + name(), e);
}
}
@Override
public void kick(Component message) {
try {
Object reason = ComponentUtils.adventureToMinecraft(message);
Object kickPacket = Reflections.constructor$ClientboundDisconnectPacket.newInstance(reason);
this.nettyChannel().writeAndFlush(kickPacket);
this.nettyChannel().disconnect();
} catch (Exception e) {
CraftEngine.instance().logger().warn("Failed to kick " + name(), e);
}
}
@Override
public void sendPackets(List<Object> packet, boolean immediately) {
this.plugin.networkManager().sendPackets(this, packet, immediately);