mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-31 04:46:37 +00:00
feat(config): 改进编码和解码
This commit is contained in:
@@ -3,34 +3,62 @@ package net.momirealms.craftengine.bukkit.plugin.network;
|
||||
|
||||
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
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
|
||||
);
|
||||
public class NetWorkDataTypes<T> {
|
||||
private static final Map<Integer, NetWorkDataTypes<?>> id2NetWorkDataTypes = new HashMap<>();
|
||||
|
||||
private final Function<FriendlyByteBuf, ?> decoder;
|
||||
private final BiConsumer<FriendlyByteBuf, Object> encoder;
|
||||
public static final NetWorkDataTypes<Integer> CLIENT_CUSTOM_BLOCK =
|
||||
new NetWorkDataTypes<>(0, FriendlyByteBuf::readInt, FriendlyByteBuf::writeInt);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
<T> NetWorkDataTypes(Function<FriendlyByteBuf, T> decoder, BiConsumer<FriendlyByteBuf, T> encoder) {
|
||||
public static final NetWorkDataTypes<Boolean> CANCEL_BLOCK_UPDATE =
|
||||
new NetWorkDataTypes<>(1, FriendlyByteBuf::readBoolean, FriendlyByteBuf::writeBoolean);
|
||||
|
||||
static {
|
||||
register(CLIENT_CUSTOM_BLOCK);
|
||||
register(CANCEL_BLOCK_UPDATE);
|
||||
}
|
||||
|
||||
private static void register(NetWorkDataTypes<?> type) {
|
||||
id2NetWorkDataTypes.put(type.id, type);
|
||||
}
|
||||
|
||||
private final int id;
|
||||
private final Function<FriendlyByteBuf, T> decoder;
|
||||
private final BiConsumer<FriendlyByteBuf, T> encoder;
|
||||
|
||||
public NetWorkDataTypes(int id, Function<FriendlyByteBuf, T> decoder, BiConsumer<FriendlyByteBuf, T> encoder) {
|
||||
this.id = id;
|
||||
this.decoder = decoder;
|
||||
this.encoder = (buf, data) -> encoder.accept(buf, (T) data);
|
||||
this.encoder = encoder;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T decode(FriendlyByteBuf buf) {
|
||||
return (T) decoder.apply(buf);
|
||||
public T decode(FriendlyByteBuf buf) {
|
||||
return decoder.apply(buf);
|
||||
}
|
||||
|
||||
public <T> void encode(FriendlyByteBuf buf, T data) {
|
||||
public void encode(FriendlyByteBuf buf, T data) {
|
||||
encoder.accept(buf, data);
|
||||
}
|
||||
}
|
||||
|
||||
public int id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void writeType(FriendlyByteBuf buf) {
|
||||
buf.writeVarInt(id);
|
||||
}
|
||||
|
||||
public static NetWorkDataTypes<?> readType(FriendlyByteBuf buf) {
|
||||
int id = buf.readVarInt();
|
||||
return id2NetWorkDataTypes.get(id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <R> NetWorkDataTypes<R> as(Class<R> clazz) {
|
||||
return (NetWorkDataTypes<R>) this;
|
||||
}
|
||||
}
|
||||
@@ -1962,9 +1962,9 @@ public class PacketConsumers {
|
||||
data = (byte[]) Reflections.method$DiscardedPayload$dataByteArray.invoke(payload);
|
||||
}
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.wrappedBuffer(data));
|
||||
NetWorkDataTypes dataType = buf.readEnumConstant(NetWorkDataTypes.class);
|
||||
NetWorkDataTypes<?> dataType = NetWorkDataTypes.readType(buf);
|
||||
if (dataType == NetWorkDataTypes.CLIENT_CUSTOM_BLOCK) {
|
||||
int clientBlockRegistrySize = dataType.decode(buf);
|
||||
int clientBlockRegistrySize = dataType.as(Integer.class).decode(buf);
|
||||
int serverBlockRegistrySize = RegistryUtils.currentBlockRegistrySize();
|
||||
if (clientBlockRegistrySize != serverBlockRegistrySize) {
|
||||
Object kickPacket = Reflections.constructor$ClientboundDisconnectPacket.newInstance(
|
||||
@@ -1983,10 +1983,10 @@ public class PacketConsumers {
|
||||
user.setClientModState(true);
|
||||
} else if (dataType == NetWorkDataTypes.CANCEL_BLOCK_UPDATE) {
|
||||
if (!VersionHelper.isOrAbove1_20_2()) return;
|
||||
if (dataType.decode(buf)) {
|
||||
if (dataType.as(Boolean.class).decode(buf)) {
|
||||
FriendlyByteBuf bufPayload = new FriendlyByteBuf(Unpooled.buffer());
|
||||
bufPayload.writeEnumConstant(dataType);
|
||||
dataType.encode(bufPayload, true);
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user