mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-26 10:29:20 +00:00
feat(config): 改进编码和解码
This commit is contained in:
@@ -71,16 +71,15 @@ public class CraftEngineFabricModClient implements ClientModInitializer {
|
||||
return;
|
||||
}
|
||||
|
||||
NetWorkDataTypes type = ModConfig.enableNetwork
|
||||
? NetWorkDataTypes.CLIENT_CUSTOM_BLOCK
|
||||
: NetWorkDataTypes.CANCEL_BLOCK_UPDATE;
|
||||
|
||||
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
|
||||
buf.writeEnumConstant(type);
|
||||
|
||||
if (ModConfig.enableNetwork) {
|
||||
NetWorkDataTypes<Integer> type = NetWorkDataTypes.CLIENT_CUSTOM_BLOCK;
|
||||
type.writeType(buf);
|
||||
type.encode(buf, Block.STATE_IDS.size());
|
||||
} else if (ModConfig.enableCancelBlockUpdate) {
|
||||
NetWorkDataTypes<Boolean> type = NetWorkDataTypes.CANCEL_BLOCK_UPDATE;
|
||||
type.writeType(buf);
|
||||
type.encode(buf, true);
|
||||
}
|
||||
|
||||
@@ -90,9 +89,9 @@ public class CraftEngineFabricModClient implements ClientModInitializer {
|
||||
private static void handleReceiver(CraftEnginePayload payload, ClientConfigurationNetworking.Context context) {
|
||||
byte[] data = payload.data();
|
||||
PacketByteBuf buf = new PacketByteBuf(Unpooled.wrappedBuffer(data));
|
||||
NetWorkDataTypes type = buf.readEnumConstant(NetWorkDataTypes.class);
|
||||
NetWorkDataTypes<?> type = NetWorkDataTypes.readType(buf);
|
||||
if (type == NetWorkDataTypes.CANCEL_BLOCK_UPDATE) {
|
||||
serverInstalled = type.decode(buf);
|
||||
serverInstalled = type.as(Boolean.class).decode(buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,34 +2,62 @@ package net.momirealms.craftengine.fabric.client.util;
|
||||
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
public enum NetWorkDataTypes {
|
||||
CLIENT_CUSTOM_BLOCK(
|
||||
PacketByteBuf::readInt,
|
||||
PacketByteBuf::writeInt
|
||||
),
|
||||
CANCEL_BLOCK_UPDATE(
|
||||
PacketByteBuf::readBoolean,
|
||||
PacketByteBuf::writeBoolean
|
||||
);
|
||||
public class NetWorkDataTypes<T> {
|
||||
private static final Map<Integer, NetWorkDataTypes<?>> id2NetWorkDataTypes = new HashMap<>();
|
||||
|
||||
private final Function<PacketByteBuf, ?> decoder;
|
||||
private final BiConsumer<PacketByteBuf, Object> encoder;
|
||||
public static final NetWorkDataTypes<Integer> CLIENT_CUSTOM_BLOCK =
|
||||
new NetWorkDataTypes<>(0, PacketByteBuf::readInt, PacketByteBuf::writeInt);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
<T> NetWorkDataTypes(Function<PacketByteBuf, T> decoder, BiConsumer<PacketByteBuf, T> encoder) {
|
||||
public static final NetWorkDataTypes<Boolean> CANCEL_BLOCK_UPDATE =
|
||||
new NetWorkDataTypes<>(1, PacketByteBuf::readBoolean, PacketByteBuf::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<PacketByteBuf, T> decoder;
|
||||
private final BiConsumer<PacketByteBuf, T> encoder;
|
||||
|
||||
public NetWorkDataTypes(int id, Function<PacketByteBuf, T> decoder, BiConsumer<PacketByteBuf, 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(PacketByteBuf buf) {
|
||||
return (T) decoder.apply(buf);
|
||||
public T decode(PacketByteBuf buf) {
|
||||
return decoder.apply(buf);
|
||||
}
|
||||
|
||||
public <T> void encode(PacketByteBuf buf, T data) {
|
||||
public void encode(PacketByteBuf buf, T data) {
|
||||
encoder.accept(buf, data);
|
||||
}
|
||||
}
|
||||
|
||||
public int id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void writeType(PacketByteBuf buf) {
|
||||
buf.writeVarInt(id);
|
||||
}
|
||||
|
||||
public static NetWorkDataTypes<?> readType(PacketByteBuf buf) {
|
||||
int id = buf.readVarInt();
|
||||
return id2NetWorkDataTypes.get(id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <R> NetWorkDataTypes<R> as(Class<R> clazz) {
|
||||
return (NetWorkDataTypes<R>) this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user