From d193e20a099faef892e5a4591ad1c87d506f2830 Mon Sep 17 00:00:00 2001 From: Hendrix-Shen Date: Sat, 13 Jan 2024 20:34:07 +0800 Subject: [PATCH] New feature `legacyCarpetHandshake` Signed-off-by: Hendrix-Shen --- common.gradle | 6 ++- .../tweakmyclient/config/Configs.java | 6 +++ .../MixinClientPacketListener.java | 34 +++++++++++++++ .../MixinClientboundCustomPayloadPacket.java | 42 +++++++++++++++++++ .../LegacyCarpetVersionPayload.java | 28 +++++++++++++ .../LegacyClientNetworkHandler.java | 39 +++++++++++++++++ .../assets/tweakmyclient/lang/en_us.json | 2 + .../assets/tweakmyclient/lang/zh_cn.json | 2 + src/main/resources/tweakmyclient.mixins.json | 2 + .../MixinClientPacketListener.java | 8 ++++ .../MixinClientboundCustomPayloadPacket.java | 8 ++++ .../LegacyCarpetVersionPayload.java | 4 ++ .../LegacyClientNetworkHandler.java | 4 ++ versions/1.20.2/gradle.properties | 1 + versions/1.20.4/gradle.properties | 1 + 15 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientPacketListener.java create mode 100644 src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientboundCustomPayloadPacket.java create mode 100644 src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyCarpetVersionPayload.java create mode 100644 src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyClientNetworkHandler.java create mode 100644 versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientPacketListener.java create mode 100644 versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientboundCustomPayloadPacket.java create mode 100644 versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyCarpetVersionPayload.java create mode 100644 versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyClientNetworkHandler.java diff --git a/common.gradle b/common.gradle index cce7dbc..393f588 100644 --- a/common.gradle +++ b/common.gradle @@ -44,6 +44,7 @@ repositories { content { includeGroup("com.github.Nyan-Work") + includeGroup("com.github.gnembon") } } @@ -67,8 +68,9 @@ configurations { // Module, Property prefix, Resolve condition, Transitive dependencies. def compileOnlyDependencies = [ - ["curse.maven:litematica-308892" , "litematica" , true, false], - ["curse.maven:not-enough-crashes-353890", "not_enough_crashes", true, false], + ["curse.maven:litematica-308892" , "litematica" , true , false], + ["curse.maven:not-enough-crashes-353890", "not_enough_crashes", true , false], + ["com.github.gnembon:fabric-carpet" , "fabric_carpet" , mcVersion > 12001, false], ] dependencies { diff --git a/src/main/java/top/hendrixshen/tweakmyclient/config/Configs.java b/src/main/java/top/hendrixshen/tweakmyclient/config/Configs.java index 5fa0980..2ce428f 100644 --- a/src/main/java/top/hendrixshen/tweakmyclient/config/Configs.java +++ b/src/main/java/top/hendrixshen/tweakmyclient/config/Configs.java @@ -127,6 +127,12 @@ public class Configs { @Config(category = ConfigCategory.PATCH, dependencies = @Dependencies(and = @Dependency(value = "minecraft", versionPredicate = ">=1.16"))) public static boolean forcePistonWithoutAffectByTool = false; + @Config(category = ConfigCategory.PATCH, dependencies = @Dependencies(and = { + @Dependency(value = "carpet", versionPredicate = ">1.4.113"), + @Dependency(value = "minecraft", versionPredicate = ">1.20.1") + })) + public static boolean legacyCarpetHandshake = false; + @Config(category = ConfigCategory.PATCH, dependencies = @Dependencies(and = @Dependency("notenoughcrashes"))) public static boolean notEnoughCrashesBlueScreenOfDeath = false; diff --git a/src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientPacketListener.java b/src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientPacketListener.java new file mode 100644 index 0000000..b60114e --- /dev/null +++ b/src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientPacketListener.java @@ -0,0 +1,34 @@ +package top.hendrixshen.tweakmyclient.mixin.patch.legacyCarpetHandshake; + +import net.minecraft.client.multiplayer.ClientPacketListener; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import top.hendrixshen.magiclib.dependency.api.annotation.Dependencies; +import top.hendrixshen.magiclib.dependency.api.annotation.Dependency; +import top.hendrixshen.tweakmyclient.config.Configs; +import top.hendrixshen.tweakmyclient.network.legacyCarpetHandshake.LegacyCarpetVersionPayload; +import top.hendrixshen.tweakmyclient.network.legacyCarpetHandshake.LegacyClientNetworkHandler; + +@Dependencies(and = { + @Dependency(value = "carpet", versionPredicate = ">1.4.113"), + @Dependency(value = "minecraft", versionPredicate = ">1.20.1") +}) +@Mixin(ClientPacketListener.class) +public class MixinClientPacketListener { + @Inject( + method = "handleUnknownCustomPayload", + at = @At( + value = "HEAD" + ), + cancellable = true + ) + private void onHandleUnknownCustomPayload(CustomPacketPayload packet, CallbackInfo ci) { + if (Configs.legacyCarpetHandshake && packet instanceof LegacyCarpetVersionPayload cpp) { + LegacyClientNetworkHandler.handleData(cpp); + ci.cancel(); + } + } +} diff --git a/src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientboundCustomPayloadPacket.java b/src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientboundCustomPayloadPacket.java new file mode 100644 index 0000000..7d011ff --- /dev/null +++ b/src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientboundCustomPayloadPacket.java @@ -0,0 +1,42 @@ +package top.hendrixshen.tweakmyclient.mixin.patch.legacyCarpetHandshake; + +import carpet.network.CarpetClient; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.NotNull; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import top.hendrixshen.magiclib.dependency.api.annotation.Dependencies; +import top.hendrixshen.magiclib.dependency.api.annotation.Dependency; +import top.hendrixshen.tweakmyclient.TweakMyClientReference; +import top.hendrixshen.tweakmyclient.config.Configs; +import top.hendrixshen.tweakmyclient.network.legacyCarpetHandshake.LegacyCarpetVersionPayload; + +@Dependencies(and = { + @Dependency(value = "carpet", versionPredicate = ">1.4.113"), + @Dependency(value = "minecraft", versionPredicate = ">1.20.1") +}) +@Mixin(value = ClientboundCustomPayloadPacket.class, priority = 990) +public class MixinClientboundCustomPayloadPacket { + @Inject( + method = "readPayload", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/network/protocol/common/ClientboundCustomPayloadPacket;readUnknownPayload(Lnet/minecraft/resources/ResourceLocation;Lnet/minecraft/network/FriendlyByteBuf;)Lnet/minecraft/network/protocol/common/custom/DiscardedPayload;" + ), + cancellable = true + ) + private static void onReadPayload(@NotNull ResourceLocation resourceLocation, FriendlyByteBuf friendlyByteBuf, + @NotNull CallbackInfoReturnable cir) { + if (Configs.legacyCarpetHandshake && resourceLocation.equals(CarpetClient.CARPET_CHANNEL)) { + try { + cir.setReturnValue(new LegacyCarpetVersionPayload(new FriendlyByteBuf(friendlyByteBuf.copy()))); + } catch (Exception ignore) { + } + } + } +} diff --git a/src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyCarpetVersionPayload.java b/src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyCarpetVersionPayload.java new file mode 100644 index 0000000..7c865e2 --- /dev/null +++ b/src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyCarpetVersionPayload.java @@ -0,0 +1,28 @@ +package top.hendrixshen.tweakmyclient.network.legacyCarpetHandshake; + +import carpet.network.CarpetClient; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.NotNull; + +public record LegacyCarpetVersionPayload(int command, String data) implements CustomPacketPayload { + public LegacyCarpetVersionPayload(@NotNull FriendlyByteBuf input) throws RuntimeException { + this(input.readVarInt(), input.readUtf()); + + if (this.command != LegacyClientNetworkHandler.HI && this.command != LegacyClientNetworkHandler.DATA) { + throw new RuntimeException(); + } + } + + @Override + public void write(@NotNull FriendlyByteBuf output) { + output.writeVarInt(command); + output.writeUtf(data); + } + + @Override + public @NotNull ResourceLocation id() { + return CarpetClient.CARPET_CHANNEL; + } +} diff --git a/src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyClientNetworkHandler.java b/src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyClientNetworkHandler.java new file mode 100644 index 0000000..c67b872 --- /dev/null +++ b/src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyClientNetworkHandler.java @@ -0,0 +1,39 @@ +package top.hendrixshen.tweakmyclient.network.legacyCarpetHandshake; + +import carpet.CarpetSettings; +import carpet.network.CarpetClient; +import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; +import org.jetbrains.annotations.NotNull; + +public class LegacyClientNetworkHandler { + public static final int HI = 69; + public static final int HELLO = 420; + public static final int DATA = 1; + + public static void handleData(@NotNull LegacyCarpetVersionPayload data) { + if (data.command() == LegacyClientNetworkHandler.HI) { + LegacyClientNetworkHandler.onHi(data.data()); + } + } + + public static void onHi(@NotNull String version) { + CarpetClient.setCarpet(); + CarpetClient.serverCarpetVersion = version; + + if (CarpetSettings.carpetVersion.equals(CarpetClient.serverCarpetVersion)) { + CarpetSettings.LOG.info("Joined carpet server with matching carpet version"); + } else { + CarpetSettings.LOG.warn("Joined carpet server with another carpet version: " + CarpetClient.serverCarpetVersion); + } + + // We can ensure that this packet is + // processed AFTER the player has joined + LegacyClientNetworkHandler.respondHello(); + } + + public static void respondHello() { + CarpetClient.getPlayer().connection.send(new ServerboundCustomPayloadPacket( + new LegacyCarpetVersionPayload(LegacyClientNetworkHandler.HELLO, CarpetSettings.carpetVersion) + )); + } +} diff --git a/src/main/resources/assets/tweakmyclient/lang/en_us.json b/src/main/resources/assets/tweakmyclient/lang/en_us.json index 680a690..889c8e9 100644 --- a/src/main/resources/assets/tweakmyclient/lang/en_us.json +++ b/src/main/resources/assets/tweakmyclient/lang/en_us.json @@ -199,6 +199,8 @@ "tweakmyclient.config.patch.forceDebugInfoDetailed.comment": "Always show full debug information, even if game rule reducedDebugInfo is true.", "tweakmyclient.config.patch.forcePistonWithoutAffectByTool.name": "forcePistonWithoutAffectByTool", "tweakmyclient.config.patch.forcePistonWithoutAffectByTool.comment": "The pickaxe will no longer be used as an effective mining tool for pistons.", + "tweakmyclient.config.patch.legacyCarpetHandshake.name": "legacyCarpetHandshake", + "tweakmyclient.config.patch.legacyCarpetHandshake.comment": "Handshake protocol compatible with Carpet 1.1.112 and below.", "tweakmyclient.config.patch.litematicaSchematicWailaCompat.name": "litematicaSchematicWailaCompat", "tweakmyclient.config.patch.litematicaSchematicWailaCompat.comment": "Make WAILA compatible to show litematica schematic.\n- For 1.18 and above, Jade is supported. (Recommend)\n- For 1.16 and above, Wthit is supported.\n- For 1.15 and below, Hwyla is supported.", "tweakmyclient.config.patch.notEnoughCrashesBlueScreenOfDeath.name": "notEnoughCrashesBlueScreenOfDeath", diff --git a/src/main/resources/assets/tweakmyclient/lang/zh_cn.json b/src/main/resources/assets/tweakmyclient/lang/zh_cn.json index f9db1b3..18e0d35 100644 --- a/src/main/resources/assets/tweakmyclient/lang/zh_cn.json +++ b/src/main/resources/assets/tweakmyclient/lang/zh_cn.json @@ -199,6 +199,8 @@ "tweakmyclient.config.patch.forceDebugInfoDetailed.comment": "始终展示完整调试信息, 即使游戏规则 reducedDebugInfo 为true.", "tweakmyclient.config.patch.forcePistonWithoutAffectByTool.name": "强制活塞不受镐子影响", "tweakmyclient.config.patch.forcePistonWithoutAffectByTool.comment": "镐子将不再作为活塞的有效开采工具.", + "tweakmyclient.config.patch.legacyCarpetHandshake.name": "旧版 Carpet 握手协议", + "tweakmyclient.config.patch.legacyCarpetHandshake.comment": "兼容 Carpet 1.1.112 及以下版本的握手协议.", "tweakmyclient.config.patch.litematicaSchematicWailaCompat.name": "投影蓝图Waila兼容", "tweakmyclient.config.patch.litematicaSchematicWailaCompat.comment": "使 Waila 兼容展示投影蓝图.\n- 对于 1.18 及以上版本, 支持 Jade. (推荐)\n- 对于 1.16 及以上版本, 支持 Wthit.\n- 对于 1.15 及以下版本, 支持 Hwyla.", "tweakmyclient.config.patch.notEnoughCrashesBlueScreenOfDeath.name": "NotEnoughCrashes蓝屏模拟", diff --git a/src/main/resources/tweakmyclient.mixins.json b/src/main/resources/tweakmyclient.mixins.json index 6c2528a..5afb1d0 100644 --- a/src/main/resources/tweakmyclient.mixins.json +++ b/src/main/resources/tweakmyclient.mixins.json @@ -61,6 +61,8 @@ "patch.endPortalRendererFix.MixinTheEndPortalRenderer", "patch.forceDebugInfoDetailed.MixinPlayer", "patch.forcePistonWithoutAffectByTool.MixinMiningToolItem", + "patch.legacyCarpetHandshake.MixinClientboundCustomPayloadPacket", + "patch.legacyCarpetHandshake.MixinClientPacketListener", "patch.notEnoughCrashesBlueScreenOfDeath.MixinInGameCatcher" ], "injectors": { diff --git a/versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientPacketListener.java b/versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientPacketListener.java new file mode 100644 index 0000000..4112106 --- /dev/null +++ b/versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientPacketListener.java @@ -0,0 +1,8 @@ +package top.hendrixshen.tweakmyclient.mixin.patch.legacyCarpetHandshake; + +import org.spongepowered.asm.mixin.Mixin; +import top.hendrixshen.magiclib.compat.preprocess.api.DummyClass; + +@Mixin(DummyClass.class) +public class MixinClientPacketListener { +} diff --git a/versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientboundCustomPayloadPacket.java b/versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientboundCustomPayloadPacket.java new file mode 100644 index 0000000..81f113e --- /dev/null +++ b/versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/mixin/patch/legacyCarpetHandshake/MixinClientboundCustomPayloadPacket.java @@ -0,0 +1,8 @@ +package top.hendrixshen.tweakmyclient.mixin.patch.legacyCarpetHandshake; + +import org.spongepowered.asm.mixin.Mixin; +import top.hendrixshen.magiclib.compat.preprocess.api.DummyClass; + +@Mixin(DummyClass.class) +public class MixinClientboundCustomPayloadPacket { +} diff --git a/versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyCarpetVersionPayload.java b/versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyCarpetVersionPayload.java new file mode 100644 index 0000000..9da5735 --- /dev/null +++ b/versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyCarpetVersionPayload.java @@ -0,0 +1,4 @@ +package top.hendrixshen.tweakmyclient.network.legacyCarpetHandshake; + +public class LegacyCarpetVersionPayload { +} diff --git a/versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyClientNetworkHandler.java b/versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyClientNetworkHandler.java new file mode 100644 index 0000000..6f1b3f8 --- /dev/null +++ b/versions/1.20.1/src/main/java/top/hendrixshen/tweakmyclient/network/legacyCarpetHandshake/LegacyClientNetworkHandler.java @@ -0,0 +1,4 @@ +package top.hendrixshen.tweakmyclient.network.legacyCarpetHandshake; + +public class LegacyClientNetworkHandler { +} diff --git a/versions/1.20.2/gradle.properties b/versions/1.20.2/gradle.properties index f0632f2..6791a34 100644 --- a/versions/1.20.2/gradle.properties +++ b/versions/1.20.2/gradle.properties @@ -4,6 +4,7 @@ minecraft_version=1.20.2 minecraft_dependency=1.20.2 # Compatible Libraries +fabric_carpet_version=1.4.121 # Litematica - 0.16.0 # https://www.curseforge.com/minecraft/mc-mods/litematica/files/4789765 # litematica-fabric-1.20.2-0.16.0.jar diff --git a/versions/1.20.4/gradle.properties b/versions/1.20.4/gradle.properties index abf98bb..c468d42 100644 --- a/versions/1.20.4/gradle.properties +++ b/versions/1.20.4/gradle.properties @@ -4,6 +4,7 @@ minecraft_version=1.20.4 minecraft_dependency=1.20.4 # Compatible Libraries +fabric_carpet_version=1.4.128 # Litematica - 0.17.0 # https://www.curseforge.com/minecraft/mc-mods/litematica/files/4946471 # litematica-fabric-1.20.4-0.17.0.jar