mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-30 04:19:27 +00:00
refactor(network): 重构注册到注册表
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.network.payload;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.codec.NetworkCodec;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.codec.NetworkDecoder;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.codec.NetworkMemberEncoder;
|
||||
import net.momirealms.craftengine.core.plugin.network.NetWorkUser;
|
||||
|
||||
public interface Data {
|
||||
|
||||
default void handle(NetWorkUser user) {
|
||||
}
|
||||
|
||||
static <B extends ByteBuf, T extends Data> NetworkCodec<B, T> codec(NetworkMemberEncoder<B, T> networkMemberEncoder, NetworkDecoder<B, T> networkDecoder) {
|
||||
return NetworkCodec.ofMember(networkMemberEncoder, networkDecoder);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,55 +1,44 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.network.payload;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.codec.NetworkCodec;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.protocol.CancelBlockUpdateData;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.protocol.ClientBlockStateSizeData;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.protocol.ClientCustomBlockData;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.protocol.CancelBlockUpdatePacket;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.protocol.ClientBlockStateSizePacket;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.protocol.ClientCustomBlockPacket;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.plugin.network.ModPacket;
|
||||
import net.momirealms.craftengine.core.plugin.network.NetWorkUser;
|
||||
import net.momirealms.craftengine.core.plugin.network.NetworkManager;
|
||||
import net.momirealms.craftengine.core.plugin.network.codec.NetworkCodec;
|
||||
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
|
||||
import net.momirealms.craftengine.core.registry.Holder;
|
||||
import net.momirealms.craftengine.core.registry.WritableRegistry;
|
||||
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
|
||||
import net.momirealms.craftengine.core.util.ResourceKey;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.Optional;
|
||||
|
||||
public class PayloadHelper {
|
||||
private static final Map<Class<Data>, Byte> classToType = new HashMap<>();
|
||||
private static final Map<Byte, NetworkCodec<FriendlyByteBuf, Data>> typeToCodec = new HashMap<>();
|
||||
private static final AtomicInteger typeCounter = new AtomicInteger(0);
|
||||
|
||||
public static void registerDataTypes() {
|
||||
registerDataType(ClientCustomBlockData.class, ClientCustomBlockData.CODEC);
|
||||
registerDataType(CancelBlockUpdateData.class, CancelBlockUpdateData.CODEC);
|
||||
registerDataType(ClientBlockStateSizeData.class, ClientBlockStateSizeData.CODEC);
|
||||
registerDataType(ClientCustomBlockPacket.TYPE, ClientCustomBlockPacket.CODEC);
|
||||
registerDataType(CancelBlockUpdatePacket.TYPE, CancelBlockUpdatePacket.CODEC);
|
||||
registerDataType(ClientBlockStateSizePacket.TYPE, ClientBlockStateSizePacket.CODEC);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T extends Data> void registerDataType(Class<T> dataClass, NetworkCodec<FriendlyByteBuf, T> codec) {
|
||||
if (classToType.containsKey(dataClass)) {
|
||||
CraftEngine.instance().logger().warn("Duplicate data type class: " + dataClass.getName());
|
||||
return;
|
||||
}
|
||||
int next = typeCounter.getAndIncrement();
|
||||
if (next > 255) {
|
||||
throw new IllegalStateException("Too many data types registered, byte index overflow (max 256)");
|
||||
}
|
||||
byte type = (byte) next;
|
||||
classToType.put((Class<Data>) dataClass, type);
|
||||
typeToCodec.put(type, (NetworkCodec<FriendlyByteBuf, Data>) codec);
|
||||
public static <T extends ModPacket> void registerDataType(ResourceKey<NetworkCodec<FriendlyByteBuf, ? extends ModPacket>> key, NetworkCodec<FriendlyByteBuf, T> codec) {
|
||||
((WritableRegistry<NetworkCodec<FriendlyByteBuf, ? extends ModPacket>>) BuiltInRegistries.MOD_PACKET).register(key, codec);
|
||||
}
|
||||
|
||||
public static void sendData(NetWorkUser user, Data data) {
|
||||
Class<? extends Data> dataClass = data.getClass();
|
||||
Byte type = classToType.get(dataClass);
|
||||
if (type == null) {
|
||||
CraftEngine.instance().logger().warn("Unknown data type class: " + dataClass.getName());
|
||||
public static void sendData(NetWorkUser user, ModPacket data) {
|
||||
Optional<Holder.Reference<NetworkCodec<FriendlyByteBuf, ? extends ModPacket>>> optionalType = BuiltInRegistries.MOD_PACKET.get(data.type());
|
||||
if (optionalType.isEmpty()) {
|
||||
CraftEngine.instance().logger().warn("Unknown data type class: " + data.getClass().getName());
|
||||
return;
|
||||
}
|
||||
NetworkCodec<FriendlyByteBuf, Data> codec = typeToCodec.get(type);
|
||||
@SuppressWarnings("unchecked")
|
||||
NetworkCodec<FriendlyByteBuf, ModPacket> codec = (NetworkCodec<FriendlyByteBuf, ModPacket>) optionalType.get().value();
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
buf.writeByte(type);
|
||||
buf.writeByte(BuiltInRegistries.MOD_PACKET.getId(codec));
|
||||
codec.encode(buf, data);
|
||||
user.sendCustomPayload(NetworkManager.MOD_CHANNEL_KEY, buf.array());
|
||||
}
|
||||
@@ -57,13 +46,14 @@ public class PayloadHelper {
|
||||
public static void handleReceiver(Payload payload, NetWorkUser user) {
|
||||
FriendlyByteBuf buf = payload.toBuffer();
|
||||
byte type = buf.readByte();
|
||||
NetworkCodec<FriendlyByteBuf, Data> codec = typeToCodec.get(type);
|
||||
@SuppressWarnings("unchecked")
|
||||
NetworkCodec<FriendlyByteBuf, ModPacket> codec = (NetworkCodec<FriendlyByteBuf, ModPacket>) BuiltInRegistries.MOD_PACKET.getValue(type);
|
||||
if (codec == null) {
|
||||
CraftEngine.instance().logger().warn("Unknown data type received: " + type);
|
||||
return;
|
||||
}
|
||||
|
||||
Data networkData = codec.decode(buf);
|
||||
ModPacket networkData = codec.decode(buf);
|
||||
networkData.handle(user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.network.payload.codec;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public interface NetworkCodec<B, T> extends NetworkEncoder<B, T>, NetworkDecoder<B, T> {
|
||||
|
||||
default <V> NetworkCodec<B, V> map(Function<? super T, ? extends V> factory, Function<? super V, ? extends T> getter) {
|
||||
return new NetworkCodec<>() {
|
||||
@Override
|
||||
public V decode(B in) {
|
||||
return factory.apply(NetworkCodec.this.decode(in));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(B out, V value) {
|
||||
NetworkCodec.this.encode(out, getter.apply(value));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static <B, V> NetworkCodec<B, V> ofMember(final NetworkMemberEncoder<B, V> networkMemberEncoder, final NetworkDecoder<B, V> networkDecoder) {
|
||||
return new NetworkCodec<>() {
|
||||
public V decode(B in) {
|
||||
return networkDecoder.decode(in);
|
||||
}
|
||||
|
||||
public void encode(B out, V value) {
|
||||
networkMemberEncoder.encode(value, out);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,394 +0,0 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.network.payload.codec;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufInputStream;
|
||||
import io.netty.buffer.ByteBufOutputStream;
|
||||
import io.netty.buffer.ByteBufUtil;
|
||||
import io.netty.handler.codec.DecoderException;
|
||||
import io.netty.handler.codec.EncoderException;
|
||||
import net.momirealms.craftengine.core.util.MCUtils;
|
||||
import net.momirealms.sparrow.nbt.CompoundTag;
|
||||
import net.momirealms.sparrow.nbt.NBT;
|
||||
import net.momirealms.sparrow.nbt.Tag;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
/**
|
||||
* 随便写了点方便后面重构和客户端通讯
|
||||
*/
|
||||
public interface NetworkCodecs {
|
||||
|
||||
NetworkCodec<ByteBuf, Boolean> BOOLEAN = new NetworkCodec<>() {
|
||||
@Override
|
||||
public Boolean decode(ByteBuf in) {
|
||||
return in.readBoolean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, Boolean value) {
|
||||
out.writeBoolean(value);
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, Byte> BYTE = new NetworkCodec<>() {
|
||||
@Override
|
||||
public Byte decode(ByteBuf in) {
|
||||
return in.readByte();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, Byte value) {
|
||||
out.writeByte(value);
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, Float> ROTATION_BYTE = BYTE.map(MCUtils::unpackDegrees, MCUtils::packDegrees);
|
||||
|
||||
NetworkCodec<ByteBuf, Short> SHORT = new NetworkCodec<>() {
|
||||
@Override
|
||||
public Short decode(ByteBuf in) {
|
||||
return in.readShort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, Short value) {
|
||||
out.writeShort(value);
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, Integer> UNSIGNED_SHORT = new NetworkCodec<>() {
|
||||
@Override
|
||||
public Integer decode(ByteBuf in) {
|
||||
return in.readUnsignedShort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, Integer value) {
|
||||
out.writeShort(value);
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, Integer> INTEGER = new NetworkCodec<>() {
|
||||
@Override
|
||||
public Integer decode(ByteBuf in) {
|
||||
return in.readInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, Integer value) {
|
||||
out.writeInt(value);
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, Integer> VAR_INTEGER = new NetworkCodec<>() {
|
||||
@Override
|
||||
public Integer decode(ByteBuf in) {
|
||||
int result = 0;
|
||||
int bytesRead = 0;
|
||||
byte currentByte;
|
||||
do {
|
||||
currentByte = in.readByte();
|
||||
result |= (currentByte & 127) << bytesRead++ * 7;
|
||||
if (bytesRead > 5) {
|
||||
throw new RuntimeException("VarInt too big");
|
||||
}
|
||||
} while ((currentByte & 128) == 128);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, Integer value) {
|
||||
while ((value & -128) != 0) {
|
||||
out.writeByte(value & 127 | 128);
|
||||
value >>>= 7;
|
||||
}
|
||||
out.writeByte(value);
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, OptionalInt> OPTIONAL_VAR_INTEGER = VAR_INTEGER.map(
|
||||
integer -> integer == 0 ? OptionalInt.empty() : OptionalInt.of(integer - 1),
|
||||
optionalInt -> optionalInt.isPresent() ? optionalInt.getAsInt() + 1 : 0
|
||||
);
|
||||
|
||||
NetworkCodec<ByteBuf, Long> LONG = new NetworkCodec<>() {
|
||||
@Override
|
||||
public Long decode(ByteBuf in) {
|
||||
return in.readLong();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, Long value) {
|
||||
out.writeLong(value);
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, Long> VAR_LONG = new NetworkCodec<>() {
|
||||
@Override
|
||||
public Long decode(ByteBuf in) {
|
||||
long result = 0L;
|
||||
int bytesRead = 0;
|
||||
byte currentByte;
|
||||
do {
|
||||
currentByte = in.readByte();
|
||||
result |= (long)(currentByte & 127) << bytesRead++ * 7;
|
||||
if (bytesRead > 10) {
|
||||
throw new RuntimeException("VarLong too big");
|
||||
}
|
||||
} while ((currentByte & 128) == 128);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, Long value) {
|
||||
while ((value & -128L) != 0L) {
|
||||
out.writeByte((int)(value & 127L) | 128);
|
||||
value >>>= 7;
|
||||
}
|
||||
out.writeByte(value.intValue());
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, Float> FLOAT = new NetworkCodec<>() {
|
||||
@Override
|
||||
public Float decode(ByteBuf in) {
|
||||
return in.readFloat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, Float value) {
|
||||
out.writeFloat(value);
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, Double> DOUBLE = new NetworkCodec<>() {
|
||||
@Override
|
||||
public Double decode(ByteBuf in) {
|
||||
return in.readDouble();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, Double value) {
|
||||
out.writeDouble(value);
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, byte[]> BYTE_ARRAY = new NetworkCodec<>() {
|
||||
@Override
|
||||
public byte[] decode(ByteBuf in) {
|
||||
int maxSize = in.readableBytes();
|
||||
int size = VAR_INTEGER.decode(in);
|
||||
if (size > maxSize) {
|
||||
throw new DecoderException("ByteArray with size " + size + " is bigger than allowed " + maxSize);
|
||||
} else {
|
||||
byte[] bytes = new byte[size];
|
||||
in.readBytes(bytes);
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, byte[] value) {
|
||||
VAR_INTEGER.encode(out, value.length);
|
||||
out.writeBytes(value);
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, long[]> LONG_ARRAY = new NetworkCodec<>() {
|
||||
@Override
|
||||
public long[] decode(ByteBuf in) {
|
||||
int arrayLength = VAR_INTEGER.decode(in);
|
||||
int maxPossibleElements = in.readableBytes() / 8;
|
||||
if (arrayLength > maxPossibleElements) {
|
||||
throw new DecoderException("LongArray with size " + arrayLength + " is bigger than allowed " + maxPossibleElements);
|
||||
} else {
|
||||
long[] longArray = new long[arrayLength];
|
||||
for (int i = 0; i < longArray.length; i++) {
|
||||
longArray[i] = in.readLong();
|
||||
}
|
||||
return longArray;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, long[] value) {
|
||||
VAR_INTEGER.encode(out, value.length);
|
||||
for (long element : value) {
|
||||
out.writeLong(element);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, String> STRING_UTF8 = new NetworkCodec<>() {
|
||||
private static final int MAX_STRING_LENGTH = 32767;
|
||||
|
||||
@Override
|
||||
public String decode(ByteBuf in) {
|
||||
int maxEncodedBytes = ByteBufUtil.utf8MaxBytes(MAX_STRING_LENGTH);
|
||||
int encodedLength = VAR_INTEGER.decode(in);
|
||||
if (encodedLength > maxEncodedBytes) {
|
||||
throw new DecoderException("The received encoded string buffer length is longer than maximum allowed (" + encodedLength + " > " + maxEncodedBytes + ")");
|
||||
} else if (encodedLength < 0) {
|
||||
throw new DecoderException("The received encoded string buffer length is less than zero! Weird string!");
|
||||
} else {
|
||||
int availableBytes = in.readableBytes();
|
||||
if (encodedLength > availableBytes) {
|
||||
throw new DecoderException("Not enough bytes in buffer, expected " + encodedLength + ", but got " + availableBytes);
|
||||
} else {
|
||||
String decodedString = in.toString(in.readerIndex(), encodedLength, StandardCharsets.UTF_8);
|
||||
in.readerIndex(in.readerIndex() + encodedLength);
|
||||
if (decodedString.length() > MAX_STRING_LENGTH) {
|
||||
throw new DecoderException("The received string length is longer than maximum allowed (" + decodedString.length() + " > " + MAX_STRING_LENGTH + ")");
|
||||
} else {
|
||||
return decodedString;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, String value) {
|
||||
if (value.length() > MAX_STRING_LENGTH) {
|
||||
throw new EncoderException("String too big (was " + value.length() + " characters, max " + MAX_STRING_LENGTH + ")");
|
||||
} else {
|
||||
int maxPossibleBytes = ByteBufUtil.utf8MaxBytes(value);
|
||||
ByteBuf tempBuffer = out.alloc().buffer(maxPossibleBytes);
|
||||
try {
|
||||
int actualEncodedBytes = ByteBufUtil.writeUtf8(tempBuffer, value);
|
||||
int maxAllowedBytes = ByteBufUtil.utf8MaxBytes(MAX_STRING_LENGTH);
|
||||
if (actualEncodedBytes > maxAllowedBytes) {
|
||||
throw new EncoderException("String too big (was " + actualEncodedBytes + " bytes encoded, max " + maxAllowedBytes + ")");
|
||||
}
|
||||
VAR_INTEGER.encode(out, actualEncodedBytes);
|
||||
out.writeBytes(tempBuffer);
|
||||
} finally {
|
||||
tempBuffer.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, Tag> TAG = new NetworkCodec<>() {
|
||||
@Override
|
||||
public Tag decode(ByteBuf in) {
|
||||
int initialIndex = in.readerIndex();
|
||||
byte marker = in.readByte();
|
||||
if (marker == 0) {
|
||||
return null;
|
||||
} else {
|
||||
in.readerIndex(initialIndex);
|
||||
try {
|
||||
return NBT.readUnnamedTag(new ByteBufInputStream(in), false);
|
||||
} catch (IOException e) {
|
||||
throw new EncoderException("Failed to read NBT compound: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, Tag value) {
|
||||
if (value == null) {
|
||||
out.writeByte(0);
|
||||
} else {
|
||||
try {
|
||||
NBT.writeUnnamedTag(value, new ByteBufOutputStream(out), false);
|
||||
} catch (IOException e) {
|
||||
throw new EncoderException("Failed to write NBT compound: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, CompoundTag> COMPOUND_TAG = TAG.map(tag -> {
|
||||
if (tag instanceof CompoundTag compoundTag) {
|
||||
return compoundTag;
|
||||
} else {
|
||||
throw new DecoderException("Not a compound tag: " + tag);
|
||||
}
|
||||
}, tag -> tag);
|
||||
|
||||
NetworkCodec<ByteBuf, Optional<CompoundTag>> OPTIONAL_COMPOUND_TAG = new NetworkCodec<>() {
|
||||
@Override
|
||||
public Optional<CompoundTag> decode(ByteBuf in) {
|
||||
int initialIndex = in.readerIndex();
|
||||
byte marker = in.readByte();
|
||||
if (marker == 0) {
|
||||
return Optional.empty();
|
||||
} else {
|
||||
in.readerIndex(initialIndex);
|
||||
try {
|
||||
if (NBT.readUnnamedTag(new ByteBufInputStream(in), false) instanceof CompoundTag compoundTag) {
|
||||
return Optional.of(compoundTag);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new EncoderException("Failed to read NBT compound: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, Optional<CompoundTag> value) {
|
||||
CompoundTag compound = value.orElse(null);
|
||||
if (compound == null) {
|
||||
out.writeByte(0);
|
||||
} else {
|
||||
try {
|
||||
NBT.writeUnnamedTag(compound, new ByteBufOutputStream(out), false);
|
||||
} catch (IOException e) {
|
||||
throw new EncoderException("Failed to write NBT compound: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, Vector3f> VECTOR3F = new NetworkCodec<>() {
|
||||
@Override
|
||||
public Vector3f decode(ByteBuf in) {
|
||||
return new Vector3f(in.readFloat(), in.readFloat(), in.readFloat());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, Vector3f value) {
|
||||
out.writeFloat(value.x());
|
||||
out.writeFloat(value.y());
|
||||
out.writeFloat(value.z());
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, Quaternionf> QUATERNIONF = new NetworkCodec<>() {
|
||||
@Override
|
||||
public Quaternionf decode(ByteBuf in) {
|
||||
return new Quaternionf(in.readFloat(), in.readFloat(), in.readFloat(), in.readFloat());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, Quaternionf value) {
|
||||
out.writeFloat(value.x());
|
||||
out.writeFloat(value.y());
|
||||
out.writeFloat(value.z());
|
||||
out.writeFloat(value.w());
|
||||
}
|
||||
};
|
||||
|
||||
NetworkCodec<ByteBuf, Integer> CONTAINER_ID = VAR_INTEGER;
|
||||
|
||||
NetworkCodec<ByteBuf, Integer> RGB_COLOR = new NetworkCodec<>() {
|
||||
@Override
|
||||
public Integer decode(ByteBuf in) {
|
||||
return 255 << 24 | in.readByte() & 0xFF << 16 | in.readByte() & 0xFF << 8 | in.readByte() & 0xFF;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void encode(ByteBuf out, Integer value) {
|
||||
out.writeByte(value >> 16 & 0xFF);
|
||||
out.writeByte(value >> 8 & 0xFF);
|
||||
out.writeByte(value & 0xFF);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.network.payload.codec;
|
||||
|
||||
public interface NetworkDecoder<I, T> {
|
||||
T decode(I in);
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.network.payload.codec;
|
||||
|
||||
public interface NetworkEncoder<O, T> {
|
||||
void encode(O out, T value);
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.network.payload.codec;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface NetworkMemberEncoder<O, T> {
|
||||
void encode(T object, O object2);
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.network.payload.protocol;
|
||||
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.Data;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.PayloadHelper;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.codec.NetworkCodec;
|
||||
import net.momirealms.craftengine.core.plugin.network.NetWorkUser;
|
||||
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
|
||||
|
||||
public record CancelBlockUpdateData(boolean enabled) implements Data {
|
||||
public static final NetworkCodec<FriendlyByteBuf, CancelBlockUpdateData> CODEC = Data.codec(
|
||||
CancelBlockUpdateData::encode,
|
||||
CancelBlockUpdateData::new
|
||||
);
|
||||
|
||||
private CancelBlockUpdateData(FriendlyByteBuf buf) {
|
||||
this(buf.readBoolean());
|
||||
}
|
||||
|
||||
private void encode(FriendlyByteBuf buf) {
|
||||
buf.writeBoolean(this.enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(NetWorkUser user) {
|
||||
if (!this.enabled) return;
|
||||
PayloadHelper.sendData(user, this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.network.payload.protocol;
|
||||
|
||||
import net.momirealms.craftengine.core.plugin.network.ModPacket;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.PayloadHelper;
|
||||
import net.momirealms.craftengine.core.plugin.network.codec.NetworkCodec;
|
||||
import net.momirealms.craftengine.core.plugin.network.NetWorkUser;
|
||||
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
|
||||
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.ResourceKey;
|
||||
|
||||
public record CancelBlockUpdatePacket(boolean enabled) implements ModPacket {
|
||||
public static final ResourceKey<NetworkCodec<FriendlyByteBuf, ? extends ModPacket>> TYPE = ResourceKey.create(
|
||||
BuiltInRegistries.MOD_PACKET.key().location(), Key.of("craftengine", "cancel_block_update")
|
||||
);
|
||||
public static final NetworkCodec<FriendlyByteBuf, CancelBlockUpdatePacket> CODEC = ModPacket.codec(
|
||||
CancelBlockUpdatePacket::encode,
|
||||
CancelBlockUpdatePacket::new
|
||||
);
|
||||
|
||||
private CancelBlockUpdatePacket(FriendlyByteBuf buf) {
|
||||
this(buf.readBoolean());
|
||||
}
|
||||
|
||||
private void encode(FriendlyByteBuf buf) {
|
||||
buf.writeBoolean(this.enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceKey<NetworkCodec<FriendlyByteBuf, ? extends ModPacket>> type() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(NetWorkUser user) {
|
||||
if (!this.enabled) return;
|
||||
PayloadHelper.sendData(user, this);
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.network.payload.protocol;
|
||||
|
||||
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.Data;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.codec.NetworkCodec;
|
||||
import net.momirealms.craftengine.core.plugin.network.NetWorkUser;
|
||||
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
|
||||
import net.momirealms.craftengine.core.util.IntIdentityList;
|
||||
|
||||
public record ClientBlockStateSizeData(int blockStateSize) implements Data {
|
||||
public static final NetworkCodec<FriendlyByteBuf, ClientBlockStateSizeData> CODEC = Data.codec(
|
||||
ClientBlockStateSizeData::encode,
|
||||
ClientBlockStateSizeData::new
|
||||
);
|
||||
|
||||
private ClientBlockStateSizeData(FriendlyByteBuf buf) {
|
||||
this(buf.readInt());
|
||||
}
|
||||
|
||||
private void encode(FriendlyByteBuf buf) {
|
||||
buf.writeInt(this.blockStateSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(NetWorkUser user) {
|
||||
user.setClientBlockList(new IntIdentityList(this.blockStateSize));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.network.payload.protocol;
|
||||
|
||||
|
||||
import net.momirealms.craftengine.core.plugin.network.ModPacket;
|
||||
import net.momirealms.craftengine.core.plugin.network.codec.NetworkCodec;
|
||||
import net.momirealms.craftengine.core.plugin.network.NetWorkUser;
|
||||
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
|
||||
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
|
||||
import net.momirealms.craftengine.core.util.IntIdentityList;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.ResourceKey;
|
||||
|
||||
public record ClientBlockStateSizePacket(int blockStateSize) implements ModPacket {
|
||||
public static final ResourceKey<NetworkCodec<FriendlyByteBuf, ? extends ModPacket>> TYPE = ResourceKey.create(
|
||||
BuiltInRegistries.MOD_PACKET.key().location(), Key.of("craftengine", "client_block_state_size")
|
||||
);
|
||||
public static final NetworkCodec<FriendlyByteBuf, ClientBlockStateSizePacket> CODEC = ModPacket.codec(
|
||||
ClientBlockStateSizePacket::encode,
|
||||
ClientBlockStateSizePacket::new
|
||||
);
|
||||
|
||||
private ClientBlockStateSizePacket(FriendlyByteBuf buf) {
|
||||
this(buf.readInt());
|
||||
}
|
||||
|
||||
private void encode(FriendlyByteBuf buf) {
|
||||
buf.writeInt(this.blockStateSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceKey<NetworkCodec<FriendlyByteBuf, ? extends ModPacket>> type() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(NetWorkUser user) {
|
||||
user.setClientBlockList(new IntIdentityList(this.blockStateSize));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,20 +3,26 @@ package net.momirealms.craftengine.bukkit.plugin.network.payload.protocol;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TranslationArgument;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.Data;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.payload.codec.NetworkCodec;
|
||||
import net.momirealms.craftengine.core.plugin.network.ModPacket;
|
||||
import net.momirealms.craftengine.core.plugin.network.codec.NetworkCodec;
|
||||
import net.momirealms.craftengine.bukkit.util.RegistryUtils;
|
||||
import net.momirealms.craftengine.core.plugin.network.NetWorkUser;
|
||||
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
|
||||
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
|
||||
import net.momirealms.craftengine.core.util.IntIdentityList;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.ResourceKey;
|
||||
|
||||
public record ClientCustomBlockData(int size) implements Data {
|
||||
public static final NetworkCodec<FriendlyByteBuf, ClientCustomBlockData> CODEC = Data.codec(
|
||||
ClientCustomBlockData::encode,
|
||||
ClientCustomBlockData::new
|
||||
public record ClientCustomBlockPacket(int size) implements ModPacket {
|
||||
public static final ResourceKey<NetworkCodec<FriendlyByteBuf, ? extends ModPacket>> TYPE = ResourceKey.create(
|
||||
BuiltInRegistries.MOD_PACKET.key().location(), Key.of("craftengine", "client_custom_block")
|
||||
);
|
||||
public static final NetworkCodec<FriendlyByteBuf, ClientCustomBlockPacket> CODEC = ModPacket.codec(
|
||||
ClientCustomBlockPacket::encode,
|
||||
ClientCustomBlockPacket::new
|
||||
);
|
||||
|
||||
private ClientCustomBlockData(FriendlyByteBuf buf) {
|
||||
private ClientCustomBlockPacket(FriendlyByteBuf buf) {
|
||||
this(buf.readInt());
|
||||
}
|
||||
|
||||
@@ -24,6 +30,11 @@ public record ClientCustomBlockData(int size) implements Data {
|
||||
buf.writeInt(this.size);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceKey<NetworkCodec<FriendlyByteBuf, ? extends ModPacket>> type() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(NetWorkUser user) {
|
||||
int serverBlockRegistrySize = RegistryUtils.currentBlockRegistrySize();
|
||||
Reference in New Issue
Block a user