From 4ee357b7ab33eaca811b73d439960b426e1aad36 Mon Sep 17 00:00:00 2001 From: Gugle Date: Sun, 12 Jul 2026 22:26:21 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat(network):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=E6=A8=A1=E5=9D=97=E5=B9=B6=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=8C=85=E5=8E=8B=E7=BC=A9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 anvillib-network 模块,提供网络API支持 - 实现多种压缩算法(ZSTD、LZ4、GZIP)用于网络数据包 - 添加网络数据包压缩和解压缩功能 - 配置默认压缩算法、压缩阈值和最大解压大小设置 - 更新RPC和同步模块以使用新的压缩流编解码器 - 修改网络版本号从1升级到2 - 添加多语言支持(中文和英文) - 更新模块依赖关系配置 --- .github/modules.json | 8 +- module.network/build.gradle | 7 + module.network/gradle.properties | 3 + .../assets/anvillib_network/lang/en_us.json | 11 ++ .../lib/v2/network/AnvilLibNetwork.java | 13 ++ .../network/AnvilLibNetworkServerConfig.java | 36 ++++ .../compression/CompressionAlgorithm.java | 28 +++ .../compression/PayloadCompression.java | 187 ++++++++++++++++++ .../v2/network/compression/package-info.java | 4 + .../lib/v2/network/data/AnvilLibDatagen.java | 26 +++ .../lib/v2/network/data/package-info.java | 4 + .../data/provider/ModLanguageProvider.java | 18 ++ .../network/data/provider/package-info.java | 4 + .../assets/anvillib_network/lang/zh_cn.json | 11 ++ .../lib/v2/rpc/AnvilLibRpcNetwork.java | 2 +- .../dev/anvilcraft/lib/v2/rpc/RpcPayload.java | 4 +- .../lib/v2/rpc/RpcRequestPayload.java | 4 +- .../lib/v2/rpc/RpcResponsePayload.java | 4 +- .../v2/sync/network/AnvilLibSyncNetwork.java | 2 +- .../sync/network/payload/LazySyncPayload.java | 4 +- .../v2/sync/network/payload/SyncPayload.java | 4 +- 21 files changed, 368 insertions(+), 16 deletions(-) create mode 100644 module.network/src/generated/resources/assets/anvillib_network/lang/en_us.json create mode 100644 module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetwork.java create mode 100644 module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetworkServerConfig.java create mode 100644 module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/CompressionAlgorithm.java create mode 100644 module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/PayloadCompression.java create mode 100644 module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/package-info.java create mode 100644 module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/AnvilLibDatagen.java create mode 100644 module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/package-info.java create mode 100644 module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/provider/ModLanguageProvider.java create mode 100644 module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/provider/package-info.java create mode 100644 module.network/src/main/resources/assets/anvillib_network/lang/zh_cn.json diff --git a/.github/modules.json b/.github/modules.json index 6d9b6dea..51f53fac 100644 --- a/.github/modules.json +++ b/.github/modules.json @@ -4,15 +4,15 @@ { "module": "config", "needs": [] }, { "module": "integration", "needs": [] }, { "module": "moveable-entity-block", "needs": [] }, - { "module": "network", "needs": [] }, { "module": "rendering", "needs": [] }, { "module": "space-select", "needs": [] }, - { "module": "font", "needs": ["rendering"] }, - { "module": "util", "needs": ["codec"] }, + { "module": "network", "needs": ["config"] }, + { "module": "font", "needs": ["rendering"] }, + { "module": "util", "needs": ["codec"] }, { "module": "explosion", "needs": ["config"] }, - { "module": "rpc", "needs": ["network"] }, + { "module": "rpc", "needs": ["network"] }, { "module": "multiblock", "needs": ["codec", "config", "network", "util"] }, { "module": "recipe", "needs": ["codec", "config", "util"] }, { "module": "registrum", "needs": ["util"] }, diff --git a/module.network/build.gradle b/module.network/build.gradle index 7d82dc72..0e53add5 100644 --- a/module.network/build.gradle +++ b/module.network/build.gradle @@ -1,2 +1,9 @@ dependencies { + if (System.getenv("NOT_DEV") == 'true') { + jarJar(api("dev.anvilcraft.lib:anvillib-config-neoforge-26.1:latest.release")) + } else { + jarJar(implementation project(":anvillib-config-neoforge-26.1")) + } + jarJar(implementation("at.yawk.lz4:lz4-java:1.10.1")) + jarJar(implementation("com.github.luben:zstd-jni:1.5.7-7")) } diff --git a/module.network/gradle.properties b/module.network/gradle.properties index 842f5bd0..06299884 100644 --- a/module.network/gradle.properties +++ b/module.network/gradle.properties @@ -2,3 +2,6 @@ mod_id=anvillib_network mod_name=AnvilLib-Network mod_description=Network API with direction-specific packets and automatic registry + +anvillib.needRunConfig=true +anvillib.needRunConfig.data=true diff --git a/module.network/src/generated/resources/assets/anvillib_network/lang/en_us.json b/module.network/src/generated/resources/assets/anvillib_network/lang/en_us.json new file mode 100644 index 00000000..42f7a27d --- /dev/null +++ b/module.network/src/generated/resources/assets/anvillib_network/lang/en_us.json @@ -0,0 +1,11 @@ +{ + "anvillib_network.configuration.default_compression_algorithm": "Default Compression Algorithm", + "anvillib_network.configuration.default_compression_algorithm.tooltip": "The default compression algorithm to use for network packets.\nOptions: NONE, ZSTD, LZ4, GZIP\nDefault: ZSTD\n- NONE No compression\n- ZSTD Excellent balance between compression ratio, compression speed, and decompression speed\n- LZ4 Extremely fast compression and decompression speed\n- GZIP Maximum compatibility\n", + "anvillib_network.configuration.max_decompressed_size": "Max Decompressed Size", + "anvillib_network.configuration.max_decompressed_size.tooltip": "The maximum decompressed size in bytes.\nDefault: 16777216\n", + "anvillib_network.configuration.section.anvillib.network.server.toml": "Anvillib Network Server Configuration", + "anvillib_network.configuration.section.anvillib.network.server.toml.title": "Anvillib Network Server Configuration", + "anvillib_network.configuration.threshold": "Threshold", + "anvillib_network.configuration.threshold.tooltip": "The threshold for compression in bytes.\nDefault: 128\n", + "anvillib_network.configuration.title": "Anvillib Network Configuration" +} \ No newline at end of file diff --git a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetwork.java b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetwork.java new file mode 100644 index 00000000..7b20430f --- /dev/null +++ b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetwork.java @@ -0,0 +1,13 @@ +package dev.anvilcraft.lib.v2.network; + +import dev.anvilcraft.lib.v2.config.ConfigManager; +import net.neoforged.fml.common.Mod; + +@Mod(AnvilLibNetwork.MOD_ID) +public class AnvilLibNetwork { + public static final String MOD_ID = "anvillib_network"; + public static final AnvilLibNetworkServerConfig CONFIG = ConfigManager.register( + AnvilLibNetwork.MOD_ID, + AnvilLibNetworkServerConfig::new + ); +} diff --git a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetworkServerConfig.java b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetworkServerConfig.java new file mode 100644 index 00000000..d58b3d67 --- /dev/null +++ b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetworkServerConfig.java @@ -0,0 +1,36 @@ +package dev.anvilcraft.lib.v2.network; + +import dev.anvilcraft.lib.v2.config.Comment; +import dev.anvilcraft.lib.v2.config.Config; +import dev.anvilcraft.lib.v2.network.compression.CompressionAlgorithm; +import net.neoforged.fml.config.ModConfig; + +@Config(name = "anvillib_network", group = "anvillib", type = ModConfig.Type.SERVER) +public class AnvilLibNetworkServerConfig { + @Comment( + """ + The default compression algorithm to use for network packets. + Options: NONE, ZSTD, LZ4, GZIP + Default: ZSTD + - NONE No compression + - ZSTD Excellent balance between compression ratio, compression speed, and decompression speed + - LZ4 Extremely fast compression and decompression speed + - GZIP Maximum compatibility + """ + ) + public volatile CompressionAlgorithm defaultCompressionAlgorithm = CompressionAlgorithm.ZSTD; + @Comment( + """ + The threshold for compression in bytes. + Default: 128 + """ + ) + public volatile int threshold = 128; + @Comment( + """ + The maximum decompressed size in bytes. + Default: 16777216 + """ + ) + public volatile int maxDecompressedSize = 16777216; +} diff --git a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/CompressionAlgorithm.java b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/CompressionAlgorithm.java new file mode 100644 index 00000000..47ee4028 --- /dev/null +++ b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/CompressionAlgorithm.java @@ -0,0 +1,28 @@ +package dev.anvilcraft.lib.v2.network.compression; + +/** + * Compression algorithms supported by the AnvilLib byte payload wire format. + */ +public enum CompressionAlgorithm { + NONE(0), + ZSTD(1), + LZ4(2), + GZIP(3); + + private final int id; + + CompressionAlgorithm(int id) { + this.id = id; + } + + public int id() { + return this.id; + } + + static CompressionAlgorithm byId(int id) { + for (CompressionAlgorithm algorithm : values()) { + if (algorithm.id == id) return algorithm; + } + throw new IllegalArgumentException("Unknown payload compression algorithm id: " + id); + } +} diff --git a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/PayloadCompression.java b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/PayloadCompression.java new file mode 100644 index 00000000..0fe5b222 --- /dev/null +++ b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/PayloadCompression.java @@ -0,0 +1,187 @@ +package dev.anvilcraft.lib.v2.network.compression; + +import com.github.luben.zstd.Zstd; +import dev.anvilcraft.lib.v2.network.AnvilLibNetwork; +import io.netty.buffer.ByteBuf; +import net.jpountz.lz4.LZ4Factory; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Arrays; +import java.util.Objects; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +/** + * Shared compression envelope for opaque network payloads. + * + *

The wire format is an algorithm byte followed by the raw bytes for + * {@link CompressionAlgorithm#NONE}, or a VarInt uncompressed length and the + * compressed bytes for all other algorithms.

+ */ +public final class PayloadCompression { + public static final StreamCodec STREAM_CODEC = ByteBufCodecs.BYTE_ARRAY.map( + PayloadCompression::decode, + PayloadCompression::encode + ); + + private PayloadCompression() { + } + + public static CompressionAlgorithm algorithm() { + return AnvilLibNetwork.CONFIG.defaultCompressionAlgorithm; + } + + public static int threshold() { + return AnvilLibNetwork.CONFIG.threshold; + } + + public static int maxDecompressedSize() { + return AnvilLibNetwork.CONFIG.maxDecompressedSize; + } + + public static byte[] encode(byte[] data) { + Objects.requireNonNull(data, "data"); + if (data.length > AnvilLibNetwork.CONFIG.maxDecompressedSize) { + throw new IllegalArgumentException("Payload exceeds maximum uncompressed size: " + data.length); + } + + CompressionAlgorithm selected = data.length >= AnvilLibNetwork.CONFIG.threshold + ? AnvilLibNetwork.CONFIG.defaultCompressionAlgorithm + : CompressionAlgorithm.NONE; + if (selected == CompressionAlgorithm.NONE) return uncompressed(data); + + byte[] compressed = compress(selected, data); + int headerLength = 1 + varIntSize(data.length); + if (compressed.length + headerLength >= data.length + 1) return uncompressed(data); + + ByteArrayOutputStream output = new ByteArrayOutputStream(headerLength + compressed.length); + output.write(selected.id()); + writeVarInt(output, data.length); + output.writeBytes(compressed); + return output.toByteArray(); + } + + public static byte[] decode(byte[] envelope) { + Objects.requireNonNull(envelope, "envelope"); + if (envelope.length == 0) throw new IllegalArgumentException("Compressed payload envelope is empty"); + + CompressionAlgorithm selected = CompressionAlgorithm.byId(Byte.toUnsignedInt(envelope[0])); + if (selected == CompressionAlgorithm.NONE) { + int size = envelope.length - 1; + if (size > AnvilLibNetwork.CONFIG.maxDecompressedSize) { + throw new IllegalArgumentException("Payload exceeds maximum uncompressed size: " + size); + } + return Arrays.copyOfRange(envelope, 1, envelope.length); + } + + VarInt length = readVarInt(envelope, 1); + if (length.value() < 0 || length.value() > AnvilLibNetwork.CONFIG.maxDecompressedSize) { + throw new IllegalArgumentException("Invalid decompressed payload size: " + length.value()); + } + if (length.nextOffset() >= envelope.length) { + throw new IllegalArgumentException("Compressed payload has no body"); + } + + byte[] compressed = Arrays.copyOfRange(envelope, length.nextOffset(), envelope.length); + byte[] data = decompress(selected, compressed, length.value()); + if (data.length != length.value()) { + throw new IllegalArgumentException( + "Decompressed payload size mismatch: expected " + length.value() + ", got " + data.length + ); + } + return data; + } + + private static byte[] uncompressed(byte[] data) { + byte[] envelope = new byte[data.length + 1]; + envelope[0] = (byte) CompressionAlgorithm.NONE.id(); + System.arraycopy(data, 0, envelope, 1, data.length); + return envelope; + } + + private static byte[] compress(CompressionAlgorithm selected, byte[] data) { + return switch (selected) { + case ZSTD -> Zstd.compress(data); + case LZ4 -> LZ4Factory.fastestInstance().fastCompressor().compress(data); + case GZIP -> gzipCompress(data); + case NONE -> throw new IllegalArgumentException("NONE does not compress data"); + }; + } + + private static byte[] decompress(CompressionAlgorithm selected, byte[] data, int originalLength) { + return switch (selected) { + case ZSTD -> Zstd.decompress(data, originalLength); + case LZ4 -> LZ4Factory.fastestInstance().safeDecompressor().decompress(data, originalLength); + case GZIP -> gzipDecompress(data, originalLength); + case NONE -> throw new IllegalArgumentException("NONE does not decompress data"); + }; + } + + private static byte[] gzipCompress(byte[] data) { + try { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + try (GZIPOutputStream gzip = new GZIPOutputStream(output)) { + gzip.write(data); + } + return output.toByteArray(); + } catch (IOException e) { + throw new IllegalStateException("Failed to GZIP-compress payload", e); + } + } + + private static byte[] gzipDecompress(byte[] data, int originalLength) { + try (GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(data))) { + ByteArrayOutputStream output = new ByteArrayOutputStream(originalLength); + byte[] buffer = new byte[8192]; + int total = 0; + int read; + while ((read = gzip.read(buffer)) >= 0) { + total += read; + if (total > originalLength) { + throw new IllegalArgumentException("GZIP payload exceeds declared decompressed size"); + } + output.write(buffer, 0, read); + } + return output.toByteArray(); + } catch (IOException e) { + throw new IllegalArgumentException("Failed to GZIP-decompress payload", e); + } + } + + private static void writeVarInt(ByteArrayOutputStream output, int value) { + while ((value & -128) != 0) { + output.write(value & 127 | 128); + value >>>= 7; + } + output.write(value); + } + + private static VarInt readVarInt(byte[] data, int offset) { + int value = 0; + int position = 0; + while (position < 32) { + if (offset >= data.length) throw new IllegalArgumentException("Truncated payload length VarInt"); + byte current = data[offset++]; + value |= (current & 127) << position; + if ((current & 128) == 0) return new VarInt(value, offset); + position += 7; + } + throw new IllegalArgumentException("Payload length VarInt is too large"); + } + + private static int varIntSize(int value) { + int size = 1; + while ((value & -128) != 0) { + size++; + value >>>= 7; + } + return size; + } + + private record VarInt(int value, int nextOffset) { + } +} diff --git a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/package-info.java b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/package-info.java new file mode 100644 index 00000000..02042a54 --- /dev/null +++ b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/package-info.java @@ -0,0 +1,4 @@ +@NullMarked +package dev.anvilcraft.lib.v2.network.compression; + +import org.jspecify.annotations.NullMarked; diff --git a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/AnvilLibDatagen.java b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/AnvilLibDatagen.java new file mode 100644 index 00000000..72edd1f9 --- /dev/null +++ b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/AnvilLibDatagen.java @@ -0,0 +1,26 @@ +package dev.anvilcraft.lib.v2.network.data; + +import dev.anvilcraft.lib.v2.network.AnvilLibNetwork; +import dev.anvilcraft.lib.v2.network.data.provider.ModLanguageProvider; +import net.minecraft.data.DataGenerator; +import net.minecraft.data.PackOutput; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.fml.common.EventBusSubscriber; +import net.neoforged.neoforge.data.event.GatherDataEvent; + +@EventBusSubscriber(modid = AnvilLibNetwork.MOD_ID) +public class AnvilLibDatagen { + @SubscribeEvent + public static void gatherData(GatherDataEvent.Client event) { + DataGenerator generator = event.getGenerator(); + PackOutput packOutput = generator.getPackOutput(); + generator.addProvider(true, new ModLanguageProvider(packOutput)); + } + + @SubscribeEvent + public static void gatherData(GatherDataEvent.Server event) { + DataGenerator generator = event.getGenerator(); + PackOutput packOutput = generator.getPackOutput(); + generator.addProvider(false, new ModLanguageProvider(packOutput)); + } +} diff --git a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/package-info.java b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/package-info.java new file mode 100644 index 00000000..efd2f6bb --- /dev/null +++ b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/package-info.java @@ -0,0 +1,4 @@ +@NullMarked +package dev.anvilcraft.lib.v2.network.data; + +import org.jspecify.annotations.NullMarked; diff --git a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/provider/ModLanguageProvider.java b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/provider/ModLanguageProvider.java new file mode 100644 index 00000000..5dc512e8 --- /dev/null +++ b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/provider/ModLanguageProvider.java @@ -0,0 +1,18 @@ +package dev.anvilcraft.lib.v2.network.data.provider; + +import dev.anvilcraft.lib.v2.config.ConfigData; +import dev.anvilcraft.lib.v2.network.AnvilLibNetwork; +import dev.anvilcraft.lib.v2.network.AnvilLibNetworkServerConfig; +import net.minecraft.data.PackOutput; +import net.neoforged.neoforge.common.data.LanguageProvider; + +public class ModLanguageProvider extends LanguageProvider { + public ModLanguageProvider(PackOutput output) { + super(output, AnvilLibNetwork.MOD_ID, "en_us"); + } + + @Override + protected void addTranslations() { + ConfigData.readConfigClass(this, AnvilLibNetworkServerConfig.class); + } +} diff --git a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/provider/package-info.java b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/provider/package-info.java new file mode 100644 index 00000000..8f3117a1 --- /dev/null +++ b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/data/provider/package-info.java @@ -0,0 +1,4 @@ +@NullMarked +package dev.anvilcraft.lib.v2.network.data.provider; + +import org.jspecify.annotations.NullMarked; diff --git a/module.network/src/main/resources/assets/anvillib_network/lang/zh_cn.json b/module.network/src/main/resources/assets/anvillib_network/lang/zh_cn.json new file mode 100644 index 00000000..5046cd84 --- /dev/null +++ b/module.network/src/main/resources/assets/anvillib_network/lang/zh_cn.json @@ -0,0 +1,11 @@ +{ + "anvillib_network.configuration.default_compression_algorithm": "默认压缩算法", + "anvillib_network.configuration.default_compression_algorithm.tooltip": "用于网络数据包的默认压缩算法。\n选项:NONE、ZSTD、LZ4、GZIP\n默认值:ZSTD\n- NONE 无压缩\n- ZSTD 在压缩率、压缩速度和解压速度之间取得优秀平衡\n- LZ4 极快的压缩和解压速度\n- GZIP 最大兼容性", + "anvillib_network.configuration.max_decompressed_size": "最大解压大小", + "anvillib_network.configuration.max_decompressed_size.tooltip": "最大解压大小(以字节为单位)。\n默认值:16777216", + "anvillib_network.configuration.section.anvillib.network.server.toml": "AnvilLib 网络 服务器 配置", + "anvillib_network.configuration.section.anvillib.network.server.toml.title": "AnvilLib 网络 服务器 配置", + "anvillib_network.configuration.threshold": "阈值", + "anvillib_network.configuration.threshold.tooltip": "压缩阈值(以字节为单位)。\n默认值:128", + "anvillib_network.configuration.title": "AnvilLib 网络 配置" +} diff --git a/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/AnvilLibRpcNetwork.java b/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/AnvilLibRpcNetwork.java index a3323d74..8e912bd2 100644 --- a/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/AnvilLibRpcNetwork.java +++ b/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/AnvilLibRpcNetwork.java @@ -11,7 +11,7 @@ */ @EventBusSubscriber(modid = AnvilLibRpc.MOD_ID) public class AnvilLibRpcNetwork { - public static final String NETWORK_VERSION = "1"; + public static final String NETWORK_VERSION = "2"; @SubscribeEvent public static void register(RegisterPayloadHandlersEvent event) { diff --git a/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/RpcPayload.java b/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/RpcPayload.java index 80a2d3f4..e6f5953a 100644 --- a/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/RpcPayload.java +++ b/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/RpcPayload.java @@ -2,12 +2,12 @@ import dev.anvilcraft.lib.v2.network.packet.IInsensitiveBiPacket; import dev.anvilcraft.lib.v2.network.packet.IPacket; +import dev.anvilcraft.lib.v2.network.compression.PayloadCompression; import dev.anvilcraft.lib.v2.rpc.client.AnvilLibRpcClient; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import net.minecraft.core.RegistryAccess; import net.minecraft.network.RegistryFriendlyByteBuf; -import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.world.entity.player.Player; @@ -34,7 +34,7 @@ public class RpcPayload implements IRpcPayload, IInsensitiveBiPacket { public static final Type TYPE = IPacket.type(AnvilLibRpc.mod("rpc")); public static final StreamCodec STREAM_CODEC = StreamCodec.composite( - ByteBufCodecs.BYTE_ARRAY, + PayloadCompression.STREAM_CODEC, RpcPayload::data, RpcPayload::new ); diff --git a/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/RpcRequestPayload.java b/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/RpcRequestPayload.java index 54419504..0ecf3a46 100644 --- a/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/RpcRequestPayload.java +++ b/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/RpcRequestPayload.java @@ -2,12 +2,12 @@ import dev.anvilcraft.lib.v2.network.packet.IInsensitiveBiPacket; import dev.anvilcraft.lib.v2.network.packet.IPacket; +import dev.anvilcraft.lib.v2.network.compression.PayloadCompression; import dev.anvilcraft.lib.v2.rpc.client.AnvilLibRpcClient; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import net.minecraft.core.RegistryAccess; import net.minecraft.network.RegistryFriendlyByteBuf; -import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.world.entity.player.Player; @@ -26,7 +26,7 @@ public class RpcRequestPayload implements IRpcPayload, IInsensitiveBiPacket { public static final Type TYPE = IPacket.type(AnvilLibRpc.mod("rpc_request")); public static final StreamCodec STREAM_CODEC = StreamCodec.composite( - ByteBufCodecs.BYTE_ARRAY, + PayloadCompression.STREAM_CODEC, RpcRequestPayload::data, RpcRequestPayload::new ); diff --git a/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/RpcResponsePayload.java b/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/RpcResponsePayload.java index 88a61e1e..e07ee2cd 100644 --- a/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/RpcResponsePayload.java +++ b/module.rpc/src/main/java/dev/anvilcraft/lib/v2/rpc/RpcResponsePayload.java @@ -2,12 +2,12 @@ import dev.anvilcraft.lib.v2.network.packet.IInsensitiveBiPacket; import dev.anvilcraft.lib.v2.network.packet.IPacket; +import dev.anvilcraft.lib.v2.network.compression.PayloadCompression; import dev.anvilcraft.lib.v2.rpc.client.AnvilLibRpcClient; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import net.minecraft.core.RegistryAccess; import net.minecraft.network.RegistryFriendlyByteBuf; -import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.world.entity.player.Player; @@ -28,7 +28,7 @@ public class RpcResponsePayload implements IInsensitiveBiPacket { public static final Type TYPE = IPacket.type(AnvilLibRpc.mod("rpc_response")); public static final StreamCodec STREAM_CODEC = StreamCodec.composite( - ByteBufCodecs.BYTE_ARRAY, + PayloadCompression.STREAM_CODEC, RpcResponsePayload::data, RpcResponsePayload::new ); diff --git a/module.sync/src/main/java/dev/anvilcraft/lib/v2/sync/network/AnvilLibSyncNetwork.java b/module.sync/src/main/java/dev/anvilcraft/lib/v2/sync/network/AnvilLibSyncNetwork.java index ac083108..3fe90ef2 100644 --- a/module.sync/src/main/java/dev/anvilcraft/lib/v2/sync/network/AnvilLibSyncNetwork.java +++ b/module.sync/src/main/java/dev/anvilcraft/lib/v2/sync/network/AnvilLibSyncNetwork.java @@ -9,7 +9,7 @@ @EventBusSubscriber(modid = AnvilLibSync.MOD_ID) public class AnvilLibSyncNetwork { - public static final String NETWORK_VERSION = "1"; + public static final String NETWORK_VERSION = "2"; @SubscribeEvent public static void register(RegisterPayloadHandlersEvent event) { diff --git a/module.sync/src/main/java/dev/anvilcraft/lib/v2/sync/network/payload/LazySyncPayload.java b/module.sync/src/main/java/dev/anvilcraft/lib/v2/sync/network/payload/LazySyncPayload.java index f2a45fb9..612fef09 100644 --- a/module.sync/src/main/java/dev/anvilcraft/lib/v2/sync/network/payload/LazySyncPayload.java +++ b/module.sync/src/main/java/dev/anvilcraft/lib/v2/sync/network/payload/LazySyncPayload.java @@ -2,6 +2,7 @@ import dev.anvilcraft.lib.v2.network.packet.IInsensitiveBiPacket; import dev.anvilcraft.lib.v2.network.packet.IPacket; +import dev.anvilcraft.lib.v2.network.compression.PayloadCompression; import dev.anvilcraft.lib.v2.sync.AnvilLibSync; import dev.anvilcraft.lib.v2.sync.client.AnvilLibSyncClient; import dev.anvilcraft.lib.v2.sync.management.LazySyncManager; @@ -12,7 +13,6 @@ import io.netty.buffer.Unpooled; import lombok.extern.slf4j.Slf4j; import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.PacketFlow; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; @@ -45,7 +45,7 @@ public record LazySyncPayload( ) implements IInsensitiveBiPacket { public static final Type TYPE = IPacket.type(AnvilLibSync.of("lazy_sync")); public static final StreamCodec STREAM_CODEC = StreamCodec.composite( - ByteBufCodecs.BYTE_ARRAY, + PayloadCompression.STREAM_CODEC, LazySyncPayload::array, LazySyncPayload::new ); diff --git a/module.sync/src/main/java/dev/anvilcraft/lib/v2/sync/network/payload/SyncPayload.java b/module.sync/src/main/java/dev/anvilcraft/lib/v2/sync/network/payload/SyncPayload.java index 4f32d539..1cdcd090 100644 --- a/module.sync/src/main/java/dev/anvilcraft/lib/v2/sync/network/payload/SyncPayload.java +++ b/module.sync/src/main/java/dev/anvilcraft/lib/v2/sync/network/payload/SyncPayload.java @@ -2,6 +2,7 @@ import dev.anvilcraft.lib.v2.network.packet.IInsensitiveBiPacket; import dev.anvilcraft.lib.v2.network.packet.IPacket; +import dev.anvilcraft.lib.v2.network.compression.PayloadCompression; import dev.anvilcraft.lib.v2.sync.AnvilLibSync; import dev.anvilcraft.lib.v2.sync.client.AnvilLibSyncClient; import dev.anvilcraft.lib.v2.sync.management.SyncProxy; @@ -12,7 +13,6 @@ import io.netty.buffer.Unpooled; import lombok.SneakyThrows; import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.minecraft.network.protocol.PacketFlow; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; @@ -27,7 +27,7 @@ public record SyncPayload( ) implements IInsensitiveBiPacket { public static final Type TYPE = IPacket.type(AnvilLibSync.of("sync")); public static final StreamCodec STREAM_CODEC = StreamCodec.composite( - ByteBufCodecs.BYTE_ARRAY, + PayloadCompression.STREAM_CODEC, SyncPayload::array, SyncPayload::new ); From 467ce0b0b8fb7455599460237884448376822bd1 Mon Sep 17 00:00:00 2001 From: Gugle Date: Sun, 12 Jul 2026 22:40:27 +0800 Subject: [PATCH 2/4] =?UTF-8?q?refactor(network):=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=E9=85=8D=E7=BD=AE=E5=92=8C=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 AnvilLibNetworkServerConfig 中添加 BoundedDiscrete 注解限制阈值范围 - 修复 AnvilLibWheel 中的命名空间标识符使用问题 - 将配置相关的语言文件从其他模块迁移到 recipe 模块 - 更新各模块的 gradle.properties 文件中的配置标志 - 修正多个模块中 "Anvillib" 的命名显示为 "AnvilLib" --- .../resources/assets/anvillib_explosion/lang/zh_cn.json | 6 +++--- .../main/resources/assets/anvillib_font/lang/zh_cn.json | 2 +- .../resources/assets/anvillib_multiblock/lang/zh_cn.json | 6 +++--- .../lib/v2/network/AnvilLibNetworkServerConfig.java | 3 +++ module.recipe/gradle.properties | 3 +++ .../resources/assets/anvillib_recipe/lang/en_us.json | 7 +++++++ .../main/resources/assets/anvillib_recipe/lang/zh_cn.json | 7 +++++++ module.registrum/gradle.properties | 3 --- .../generated/resources/assets/anvillib/lang/en_us.json | 7 ------- module.wheel/gradle.properties | 3 --- .../generated/resources/assets/anvillib/lang/en_us.json | 7 ------- .../java/dev/anvilcraft/lib/v2/wheel/AnvilLibWheel.java | 3 +-- .../shaders/core/annular_sector.fsh | 0 .../{anvillib => anvillib_wheel}/shaders/core/ring.fsh | 0 .../shaders/core/selection.fsh | 0 15 files changed, 28 insertions(+), 29 deletions(-) create mode 100644 module.recipe/src/generated/resources/assets/anvillib_recipe/lang/en_us.json create mode 100644 module.recipe/src/main/resources/assets/anvillib_recipe/lang/zh_cn.json delete mode 100644 module.registrum/src/generated/resources/assets/anvillib/lang/en_us.json delete mode 100644 module.wheel/src/generated/resources/assets/anvillib/lang/en_us.json rename module.wheel/src/main/resources/assets/{anvillib => anvillib_wheel}/shaders/core/annular_sector.fsh (100%) rename module.wheel/src/main/resources/assets/{anvillib => anvillib_wheel}/shaders/core/ring.fsh (100%) rename module.wheel/src/main/resources/assets/{anvillib => anvillib_wheel}/shaders/core/selection.fsh (100%) diff --git a/module.explosion/src/main/resources/assets/anvillib_explosion/lang/zh_cn.json b/module.explosion/src/main/resources/assets/anvillib_explosion/lang/zh_cn.json index e9edafbb..031ba4b1 100644 --- a/module.explosion/src/main/resources/assets/anvillib_explosion/lang/zh_cn.json +++ b/module.explosion/src/main/resources/assets/anvillib_explosion/lang/zh_cn.json @@ -3,7 +3,7 @@ "anvillib_explosion.configuration.default_remove_blocks_per_tick.tooltip": "每 tick 内可以删除的方块的默认数量", "anvillib_explosion.configuration.max_remove_blocks_per_tick": "最大每 tick 移除的方块数", "anvillib_explosion.configuration.max_remove_blocks_per_tick.tooltip": "每 tick 内可以删除的方块的最大数量", - "anvillib_explosion.configuration.section.anvillib.explosion.common.toml": "Anvillib Explosion 通用 配置", - "anvillib_explosion.configuration.section.anvillib.explosion.common.toml.title": "Anvillib Explosion 通用 配置", - "anvillib_explosion.configuration.title": "Anvillib Explosion 配置" + "anvillib_explosion.configuration.section.anvillib.explosion.common.toml": "AnvilLib Explosion 通用 配置", + "anvillib_explosion.configuration.section.anvillib.explosion.common.toml.title": "AnvilLib Explosion 通用 配置", + "anvillib_explosion.configuration.title": "AnvilLib Explosion 配置" } \ No newline at end of file diff --git a/module.font/src/main/resources/assets/anvillib_font/lang/zh_cn.json b/module.font/src/main/resources/assets/anvillib_font/lang/zh_cn.json index 0e37a159..89f260fe 100644 --- a/module.font/src/main/resources/assets/anvillib_font/lang/zh_cn.json +++ b/module.font/src/main/resources/assets/anvillib_font/lang/zh_cn.json @@ -1,7 +1,7 @@ { "narration.anvillib_font.dropdown.collapsed": "折叠", "narration.anvillib_font.dropdown.expanded": "展开", - "screen.anvillib_font.config": "铁砧库 文字渲染 配置", + "screen.anvillib_font.config": "AnvilLib 文字渲染 配置", "screen.anvillib_font.config.family": "字体系列", "screen.anvillib_font.config.font": "字体", "screen.anvillib_font.config.test": "文字渲染测试" diff --git a/module.multiblock/src/main/resources/assets/anvillib_multiblock/lang/zh_cn.json b/module.multiblock/src/main/resources/assets/anvillib_multiblock/lang/zh_cn.json index 2c745d1c..0620d532 100644 --- a/module.multiblock/src/main/resources/assets/anvillib_multiblock/lang/zh_cn.json +++ b/module.multiblock/src/main/resources/assets/anvillib_multiblock/lang/zh_cn.json @@ -5,9 +5,9 @@ "anvillib_multiblock.configuration.formed_multiblock_check_interval.tooltip": "已成型的多方块的检查周期(以tick为单位)", "anvillib_multiblock.configuration.max_checks_per_tick": "每 tick 最大检查数量", "anvillib_multiblock.configuration.max_checks_per_tick.tooltip": "每 tick 提交多方块检查的最大数量", - "anvillib_multiblock.configuration.section.anvillib_multiblock.common.toml": "铁砧库-多方块 通用配置", - "anvillib_multiblock.configuration.section.anvillib_multiblock.common.toml.title": "铁砧库-多方块 通用配置", - "anvillib_multiblock.configuration.title": "铁砧库-多方块 配置", + "anvillib_multiblock.configuration.section.anvillib_multiblock.common.toml": "AnvilLib 多方块 通用 配置", + "anvillib_multiblock.configuration.section.anvillib_multiblock.common.toml.title": "AnvilLib 多方块 通用 配置", + "anvillib_multiblock.configuration.title": "AnvilLib 多方块 配置", "anvillib_multiblock.configuration.unformed_multiblock_check_interval": "未成型多方块检查周期", "anvillib_multiblock.configuration.unformed_multiblock_check_interval.tooltip": "未成型的多方块的检查周期(以tick为单位)" } \ No newline at end of file diff --git a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetworkServerConfig.java b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetworkServerConfig.java index d58b3d67..f42672eb 100644 --- a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetworkServerConfig.java +++ b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetworkServerConfig.java @@ -1,5 +1,6 @@ package dev.anvilcraft.lib.v2.network; +import dev.anvilcraft.lib.v2.config.BoundedDiscrete; import dev.anvilcraft.lib.v2.config.Comment; import dev.anvilcraft.lib.v2.config.Config; import dev.anvilcraft.lib.v2.network.compression.CompressionAlgorithm; @@ -25,6 +26,7 @@ public class AnvilLibNetworkServerConfig { Default: 128 """ ) + @BoundedDiscrete(min = 1, max = 1024) public volatile int threshold = 128; @Comment( """ @@ -32,5 +34,6 @@ public class AnvilLibNetworkServerConfig { Default: 16777216 """ ) + @BoundedDiscrete(min = 1048576, max = 268435456) public volatile int maxDecompressedSize = 16777216; } diff --git a/module.recipe/gradle.properties b/module.recipe/gradle.properties index b4f3f842..8b99e320 100644 --- a/module.recipe/gradle.properties +++ b/module.recipe/gradle.properties @@ -4,3 +4,6 @@ interface_injection=true mod_id=anvillib_recipe mod_name=AnvilLib-Recipe mod_description=In-world recipe system with custom triggers, predicates and outcomes + +anvillib.needRunConfig=true +anvillib.needRunConfig.data=true diff --git a/module.recipe/src/generated/resources/assets/anvillib_recipe/lang/en_us.json b/module.recipe/src/generated/resources/assets/anvillib_recipe/lang/en_us.json new file mode 100644 index 00000000..f6ccf4f5 --- /dev/null +++ b/module.recipe/src/generated/resources/assets/anvillib_recipe/lang/en_us.json @@ -0,0 +1,7 @@ +{ + "anvillib_recipe.configuration.in_world_recipe_max_efficiency": "In World Recipe Max Efficiency", + "anvillib_recipe.configuration.in_world_recipe_max_efficiency.tooltip": "Maximum efficiency of in world recipes", + "anvillib_recipe.configuration.section.anvillib.recipe.common.toml": "Anvillib Recipe Common Configuration", + "anvillib_recipe.configuration.section.anvillib.recipe.common.toml.title": "Anvillib Recipe Common Configuration", + "anvillib_recipe.configuration.title": "Anvillib Recipe Configuration" +} \ No newline at end of file diff --git a/module.recipe/src/main/resources/assets/anvillib_recipe/lang/zh_cn.json b/module.recipe/src/main/resources/assets/anvillib_recipe/lang/zh_cn.json new file mode 100644 index 00000000..3403caa2 --- /dev/null +++ b/module.recipe/src/main/resources/assets/anvillib_recipe/lang/zh_cn.json @@ -0,0 +1,7 @@ +{ + "anvillib_recipe.configuration.in_world_recipe_max_efficiency": "世界配方最大效率", + "anvillib_recipe.configuration.in_world_recipe_max_efficiency.tooltip": "世界配方最大效率值", + "anvillib_recipe.configuration.section.anvillib.recipe.common.toml": "AnvilLib 配方 通用 配置", + "anvillib_recipe.configuration.section.anvillib.recipe.common.toml.title": "AnvilLib 配方 通用 配置", + "anvillib_recipe.configuration.title": "AnvilLib 配方 配置" +} \ No newline at end of file diff --git a/module.registrum/gradle.properties b/module.registrum/gradle.properties index f77737db..0c1f4698 100644 --- a/module.registrum/gradle.properties +++ b/module.registrum/gradle.properties @@ -2,6 +2,3 @@ mod_id=anvillib_registrum mod_name=AnvilLib-Registrum mod_description=Your mod's best friend - keep your registry objects simple and organized - -anvillib.needRunConfig=true -anvillib.needRunConfig.data=true \ No newline at end of file diff --git a/module.registrum/src/generated/resources/assets/anvillib/lang/en_us.json b/module.registrum/src/generated/resources/assets/anvillib/lang/en_us.json deleted file mode 100644 index 30018052..00000000 --- a/module.registrum/src/generated/resources/assets/anvillib/lang/en_us.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "anvillib.configuration.in_world_recipe_max_efficiency": "In World Recipe Max Efficiency", - "anvillib.configuration.in_world_recipe_max_efficiency.tooltip": "Maximum efficiency of in world recipes", - "anvillib.configuration.section.anvillib.common.toml": "Anvillib Common Configuration", - "anvillib.configuration.section.anvillib.common.toml.title": "Anvillib Common Configuration", - "anvillib.configuration.title": "Anvillib Configuration" -} \ No newline at end of file diff --git a/module.wheel/gradle.properties b/module.wheel/gradle.properties index 66c7eb2e..a0b4ee75 100644 --- a/module.wheel/gradle.properties +++ b/module.wheel/gradle.properties @@ -2,6 +2,3 @@ mod_id=anvillib_wheel mod_name=AnvilLib-Wheel mod_description=Provides the AnvilLib Wheel radial menu API. - -anvillib.needRunConfig=true -anvillib.needRunConfig.data=true diff --git a/module.wheel/src/generated/resources/assets/anvillib/lang/en_us.json b/module.wheel/src/generated/resources/assets/anvillib/lang/en_us.json deleted file mode 100644 index 30018052..00000000 --- a/module.wheel/src/generated/resources/assets/anvillib/lang/en_us.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "anvillib.configuration.in_world_recipe_max_efficiency": "In World Recipe Max Efficiency", - "anvillib.configuration.in_world_recipe_max_efficiency.tooltip": "Maximum efficiency of in world recipes", - "anvillib.configuration.section.anvillib.common.toml": "Anvillib Common Configuration", - "anvillib.configuration.section.anvillib.common.toml.title": "Anvillib Common Configuration", - "anvillib.configuration.title": "Anvillib Configuration" -} \ No newline at end of file diff --git a/module.wheel/src/main/java/dev/anvilcraft/lib/v2/wheel/AnvilLibWheel.java b/module.wheel/src/main/java/dev/anvilcraft/lib/v2/wheel/AnvilLibWheel.java index 5838e423..3af20981 100644 --- a/module.wheel/src/main/java/dev/anvilcraft/lib/v2/wheel/AnvilLibWheel.java +++ b/module.wheel/src/main/java/dev/anvilcraft/lib/v2/wheel/AnvilLibWheel.java @@ -11,7 +11,6 @@ @EventBusSubscriber(modid = AnvilLibWheel.MOD_ID, value = Dist.CLIENT) public class AnvilLibWheel { public static final String MOD_ID = "anvillib_wheel"; - public static final String MAIN_ID = "anvillib"; @Getter private static LibDynamicUniforms libDynamicUniforms; @@ -21,6 +20,6 @@ public static void init(ConfigureMainRenderTargetEvent event) { } public static Identifier of(String path) { - return Identifier.fromNamespaceAndPath(AnvilLibWheel.MAIN_ID, path); + return Identifier.fromNamespaceAndPath(AnvilLibWheel.MOD_ID, path); } } diff --git a/module.wheel/src/main/resources/assets/anvillib/shaders/core/annular_sector.fsh b/module.wheel/src/main/resources/assets/anvillib_wheel/shaders/core/annular_sector.fsh similarity index 100% rename from module.wheel/src/main/resources/assets/anvillib/shaders/core/annular_sector.fsh rename to module.wheel/src/main/resources/assets/anvillib_wheel/shaders/core/annular_sector.fsh diff --git a/module.wheel/src/main/resources/assets/anvillib/shaders/core/ring.fsh b/module.wheel/src/main/resources/assets/anvillib_wheel/shaders/core/ring.fsh similarity index 100% rename from module.wheel/src/main/resources/assets/anvillib/shaders/core/ring.fsh rename to module.wheel/src/main/resources/assets/anvillib_wheel/shaders/core/ring.fsh diff --git a/module.wheel/src/main/resources/assets/anvillib/shaders/core/selection.fsh b/module.wheel/src/main/resources/assets/anvillib_wheel/shaders/core/selection.fsh similarity index 100% rename from module.wheel/src/main/resources/assets/anvillib/shaders/core/selection.fsh rename to module.wheel/src/main/resources/assets/anvillib_wheel/shaders/core/selection.fsh From 27f0ea37518779c71b3571616cdf70666f72b0d9 Mon Sep 17 00:00:00 2001 From: Gugle Date: Sun, 12 Jul 2026 22:58:06 +0800 Subject: [PATCH 3/4] =?UTF-8?q?refactor(network):=20=E7=A7=BB=E9=99=A4ZSTD?= =?UTF-8?q?=E5=8E=8B=E7=BC=A9=E7=AE=97=E6=B3=95=E6=94=AF=E6=8C=81=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8E=8B=E7=BC=A9=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 从CompressionAlgorithm枚举中移除ZSTD算法选项 - 将默认压缩算法从ZSTD更改为LZ4 - 从build.gradle中移除zstd-jni依赖 - 更新配置文件注释以反映新的默认算法 - 修改PayloadCompression类中的压缩/解压方法以移除ZSTD相关代码 - 重构编码和解码逻辑以使用配置获取参数 - 更新中英文语言文件以同步压缩算法选项变化 --- module.network/build.gradle | 1 - .../assets/anvillib_network/lang/en_us.json | 2 +- .../network/AnvilLibNetworkServerConfig.java | 6 ++---- .../compression/CompressionAlgorithm.java | 5 ++--- .../compression/PayloadCompression.java | 21 ++++++++----------- .../assets/anvillib_network/lang/zh_cn.json | 2 +- 6 files changed, 15 insertions(+), 22 deletions(-) diff --git a/module.network/build.gradle b/module.network/build.gradle index 0e53add5..5f097527 100644 --- a/module.network/build.gradle +++ b/module.network/build.gradle @@ -5,5 +5,4 @@ dependencies { jarJar(implementation project(":anvillib-config-neoforge-26.1")) } jarJar(implementation("at.yawk.lz4:lz4-java:1.10.1")) - jarJar(implementation("com.github.luben:zstd-jni:1.5.7-7")) } diff --git a/module.network/src/generated/resources/assets/anvillib_network/lang/en_us.json b/module.network/src/generated/resources/assets/anvillib_network/lang/en_us.json index 42f7a27d..47651f87 100644 --- a/module.network/src/generated/resources/assets/anvillib_network/lang/en_us.json +++ b/module.network/src/generated/resources/assets/anvillib_network/lang/en_us.json @@ -1,6 +1,6 @@ { "anvillib_network.configuration.default_compression_algorithm": "Default Compression Algorithm", - "anvillib_network.configuration.default_compression_algorithm.tooltip": "The default compression algorithm to use for network packets.\nOptions: NONE, ZSTD, LZ4, GZIP\nDefault: ZSTD\n- NONE No compression\n- ZSTD Excellent balance between compression ratio, compression speed, and decompression speed\n- LZ4 Extremely fast compression and decompression speed\n- GZIP Maximum compatibility\n", + "anvillib_network.configuration.default_compression_algorithm.tooltip": "The default compression algorithm to use for network packets.\nDefault: LZ4\n- NONE No compression\n- LZ4 Extremely fast compression and decompression speed\n- GZIP Maximum compatibility\n", "anvillib_network.configuration.max_decompressed_size": "Max Decompressed Size", "anvillib_network.configuration.max_decompressed_size.tooltip": "The maximum decompressed size in bytes.\nDefault: 16777216\n", "anvillib_network.configuration.section.anvillib.network.server.toml": "Anvillib Network Server Configuration", diff --git a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetworkServerConfig.java b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetworkServerConfig.java index f42672eb..3d3d9a9c 100644 --- a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetworkServerConfig.java +++ b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/AnvilLibNetworkServerConfig.java @@ -11,15 +11,13 @@ public class AnvilLibNetworkServerConfig { @Comment( """ The default compression algorithm to use for network packets. - Options: NONE, ZSTD, LZ4, GZIP - Default: ZSTD + Default: LZ4 - NONE No compression - - ZSTD Excellent balance between compression ratio, compression speed, and decompression speed - LZ4 Extremely fast compression and decompression speed - GZIP Maximum compatibility """ ) - public volatile CompressionAlgorithm defaultCompressionAlgorithm = CompressionAlgorithm.ZSTD; + public volatile CompressionAlgorithm defaultCompressionAlgorithm = CompressionAlgorithm.LZ4; @Comment( """ The threshold for compression in bytes. diff --git a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/CompressionAlgorithm.java b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/CompressionAlgorithm.java index 47ee4028..d5181498 100644 --- a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/CompressionAlgorithm.java +++ b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/CompressionAlgorithm.java @@ -5,9 +5,8 @@ */ public enum CompressionAlgorithm { NONE(0), - ZSTD(1), - LZ4(2), - GZIP(3); + LZ4(1), + GZIP(2); private final int id; diff --git a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/PayloadCompression.java b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/PayloadCompression.java index 0fe5b222..a6feee13 100644 --- a/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/PayloadCompression.java +++ b/module.network/src/main/java/dev/anvilcraft/lib/v2/network/compression/PayloadCompression.java @@ -1,6 +1,5 @@ package dev.anvilcraft.lib.v2.network.compression; -import com.github.luben.zstd.Zstd; import dev.anvilcraft.lib.v2.network.AnvilLibNetwork; import io.netty.buffer.ByteBuf; import net.jpountz.lz4.LZ4Factory; @@ -45,13 +44,14 @@ public static int maxDecompressedSize() { public static byte[] encode(byte[] data) { Objects.requireNonNull(data, "data"); - if (data.length > AnvilLibNetwork.CONFIG.maxDecompressedSize) { + int threshold = threshold(); + int maxSize = maxDecompressedSize(); + CompressionAlgorithm algo = algorithm(); + if (data.length > maxSize) { throw new IllegalArgumentException("Payload exceeds maximum uncompressed size: " + data.length); } - CompressionAlgorithm selected = data.length >= AnvilLibNetwork.CONFIG.threshold - ? AnvilLibNetwork.CONFIG.defaultCompressionAlgorithm - : CompressionAlgorithm.NONE; + CompressionAlgorithm selected = data.length >= threshold ? algo : CompressionAlgorithm.NONE; if (selected == CompressionAlgorithm.NONE) return uncompressed(data); byte[] compressed = compress(selected, data); @@ -67,19 +67,20 @@ public static byte[] encode(byte[] data) { public static byte[] decode(byte[] envelope) { Objects.requireNonNull(envelope, "envelope"); + int maxSize = maxDecompressedSize(); if (envelope.length == 0) throw new IllegalArgumentException("Compressed payload envelope is empty"); CompressionAlgorithm selected = CompressionAlgorithm.byId(Byte.toUnsignedInt(envelope[0])); if (selected == CompressionAlgorithm.NONE) { int size = envelope.length - 1; - if (size > AnvilLibNetwork.CONFIG.maxDecompressedSize) { + if (size > maxSize) { throw new IllegalArgumentException("Payload exceeds maximum uncompressed size: " + size); } return Arrays.copyOfRange(envelope, 1, envelope.length); } VarInt length = readVarInt(envelope, 1); - if (length.value() < 0 || length.value() > AnvilLibNetwork.CONFIG.maxDecompressedSize) { + if (length.value() < 0 || length.value() > maxSize) { throw new IllegalArgumentException("Invalid decompressed payload size: " + length.value()); } if (length.nextOffset() >= envelope.length) { @@ -89,9 +90,7 @@ public static byte[] decode(byte[] envelope) { byte[] compressed = Arrays.copyOfRange(envelope, length.nextOffset(), envelope.length); byte[] data = decompress(selected, compressed, length.value()); if (data.length != length.value()) { - throw new IllegalArgumentException( - "Decompressed payload size mismatch: expected " + length.value() + ", got " + data.length - ); + throw new IllegalArgumentException("Decompressed payload size mismatch: expected " + length.value() + ", got " + data.length); } return data; } @@ -105,7 +104,6 @@ private static byte[] uncompressed(byte[] data) { private static byte[] compress(CompressionAlgorithm selected, byte[] data) { return switch (selected) { - case ZSTD -> Zstd.compress(data); case LZ4 -> LZ4Factory.fastestInstance().fastCompressor().compress(data); case GZIP -> gzipCompress(data); case NONE -> throw new IllegalArgumentException("NONE does not compress data"); @@ -114,7 +112,6 @@ private static byte[] compress(CompressionAlgorithm selected, byte[] data) { private static byte[] decompress(CompressionAlgorithm selected, byte[] data, int originalLength) { return switch (selected) { - case ZSTD -> Zstd.decompress(data, originalLength); case LZ4 -> LZ4Factory.fastestInstance().safeDecompressor().decompress(data, originalLength); case GZIP -> gzipDecompress(data, originalLength); case NONE -> throw new IllegalArgumentException("NONE does not decompress data"); diff --git a/module.network/src/main/resources/assets/anvillib_network/lang/zh_cn.json b/module.network/src/main/resources/assets/anvillib_network/lang/zh_cn.json index 5046cd84..f0a31e77 100644 --- a/module.network/src/main/resources/assets/anvillib_network/lang/zh_cn.json +++ b/module.network/src/main/resources/assets/anvillib_network/lang/zh_cn.json @@ -1,6 +1,6 @@ { "anvillib_network.configuration.default_compression_algorithm": "默认压缩算法", - "anvillib_network.configuration.default_compression_algorithm.tooltip": "用于网络数据包的默认压缩算法。\n选项:NONE、ZSTD、LZ4、GZIP\n默认值:ZSTD\n- NONE 无压缩\n- ZSTD 在压缩率、压缩速度和解压速度之间取得优秀平衡\n- LZ4 极快的压缩和解压速度\n- GZIP 最大兼容性", + "anvillib_network.configuration.default_compression_algorithm.tooltip": "用于网络数据包的默认压缩算法。\n默认值:ZSTD\n- NONE 无压缩\n- LZ4 极快的压缩和解压速度\n- GZIP 最大兼容性", "anvillib_network.configuration.max_decompressed_size": "最大解压大小", "anvillib_network.configuration.max_decompressed_size.tooltip": "最大解压大小(以字节为单位)。\n默认值:16777216", "anvillib_network.configuration.section.anvillib.network.server.toml": "AnvilLib 网络 服务器 配置", From a738e836682fc145c81846c2c8e32381294c0c6d Mon Sep 17 00:00:00 2001 From: Gugle Date: Sun, 12 Jul 2026 23:04:52 +0800 Subject: [PATCH 4/4] =?UTF-8?q?fix(config):=20=E6=9B=B4=E6=96=B0=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=8E=8B=E7=BC=A9=E7=AE=97=E6=B3=95=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复了默认压缩算法的提示文本,将默认值从 ZSTD 更正为 LZ4 --- .../src/main/resources/assets/anvillib_network/lang/zh_cn.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module.network/src/main/resources/assets/anvillib_network/lang/zh_cn.json b/module.network/src/main/resources/assets/anvillib_network/lang/zh_cn.json index f0a31e77..59645596 100644 --- a/module.network/src/main/resources/assets/anvillib_network/lang/zh_cn.json +++ b/module.network/src/main/resources/assets/anvillib_network/lang/zh_cn.json @@ -1,6 +1,6 @@ { "anvillib_network.configuration.default_compression_algorithm": "默认压缩算法", - "anvillib_network.configuration.default_compression_algorithm.tooltip": "用于网络数据包的默认压缩算法。\n默认值:ZSTD\n- NONE 无压缩\n- LZ4 极快的压缩和解压速度\n- GZIP 最大兼容性", + "anvillib_network.configuration.default_compression_algorithm.tooltip": "用于网络数据包的默认压缩算法。\n默认值:LZ4\n- NONE 无压缩\n- LZ4 极快的压缩和解压速度\n- GZIP 最大兼容性", "anvillib_network.configuration.max_decompressed_size": "最大解压大小", "anvillib_network.configuration.max_decompressed_size.tooltip": "最大解压大小(以字节为单位)。\n默认值:16777216", "anvillib_network.configuration.section.anvillib.network.server.toml": "AnvilLib 网络 服务器 配置",