9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2026-01-04 15:41:38 +00:00

feat(config): 改进编码和解码

This commit is contained in:
jhqwqmc
2025-05-12 16:58:41 +08:00
parent bac058a926
commit 82e8fc5263
5 changed files with 117 additions and 48 deletions

View File

@@ -0,0 +1,36 @@
package net.momirealms.craftengine.bukkit.plugin.network;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
import java.util.function.BiConsumer;
import java.util.function.Function;
public enum NetWorkDataTypes {
CLIENT_CUSTOM_BLOCK(
FriendlyByteBuf::readInt,
FriendlyByteBuf::writeInt
),
CANCEL_BLOCK_UPDATE(
FriendlyByteBuf::readBoolean,
FriendlyByteBuf::writeBoolean
);
private final Function<FriendlyByteBuf, ?> decoder;
private final BiConsumer<FriendlyByteBuf, Object> encoder;
@SuppressWarnings("unchecked")
<T> NetWorkDataTypes(Function<FriendlyByteBuf, T> decoder, BiConsumer<FriendlyByteBuf, T> encoder) {
this.decoder = decoder;
this.encoder = (buf, data) -> encoder.accept(buf, (T) data);
}
@SuppressWarnings("unchecked")
public <T> T decode(FriendlyByteBuf buf) {
return (T) decoder.apply(buf);
}
public <T> void encode(FriendlyByteBuf buf, T data) {
encoder.accept(buf, data);
}
}

View File

@@ -1961,14 +1961,10 @@ public class PacketConsumers {
} else {
data = (byte[]) Reflections.method$DiscardedPayload$dataByteArray.invoke(payload);
}
String decodeData = new String(data, StandardCharsets.UTF_8);
if (decodeData.endsWith("init")) {
int firstColon = decodeData.indexOf(':');
if (firstColon == -1) return;
int secondColon = decodeData.indexOf(':', firstColon + 1);
if (secondColon == -1) return;
String payloadData = decodeData.substring(firstColon + 1, secondColon);
int clientBlockRegistrySize = Integer.parseInt(payloadData);
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.wrappedBuffer(data));
NetWorkDataTypes dataType = buf.readEnumConstant(NetWorkDataTypes.class);
if (dataType == NetWorkDataTypes.CLIENT_CUSTOM_BLOCK) {
int clientBlockRegistrySize = dataType.decode(buf);
int serverBlockRegistrySize = RegistryUtils.currentBlockRegistrySize();
if (clientBlockRegistrySize != serverBlockRegistrySize) {
Object kickPacket = Reflections.constructor$ClientboundDisconnectPacket.newInstance(
@@ -1985,23 +1981,16 @@ public class PacketConsumers {
return;
}
user.setClientModState(true);
} else if (decodeData.endsWith("cancel")) {
} else if (dataType == NetWorkDataTypes.CANCEL_BLOCK_UPDATE) {
if (!VersionHelper.isOrAbove1_20_2()) return;
int firstColon = decodeData.indexOf(':');
if (firstColon == -1) return;
int secondColon = decodeData.indexOf(':', firstColon + 1);
if (secondColon == -1) return;
String payloadData = decodeData.substring(firstColon + 1, secondColon);
boolean cancel = Boolean.parseBoolean(payloadData);
if (cancel) {
user.nettyChannel().writeAndFlush(
Reflections.constructor$ClientboundCustomPayloadPacket.newInstance(
Reflections.constructor$DiscardedPayload.newInstance(
KeyUtils.toResourceLocation(Key.of(NetworkManager.MOD_CHANNEL)),
(":true:cancel").getBytes(StandardCharsets.UTF_8)
)
)
);
if (dataType.decode(buf)) {
FriendlyByteBuf bufPayload = new FriendlyByteBuf(Unpooled.buffer());
bufPayload.writeEnumConstant(dataType);
dataType.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);
}
}
}