Skip to content

Commit

Permalink
New feature legacyCarpetHandshake
Browse files Browse the repository at this point in the history
Signed-off-by: Hendrix-Shen <[email protected]>
  • Loading branch information
Hendrix-Shen committed Jan 13, 2024
1 parent 8c5adab commit d193e20
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 2 deletions.
6 changes: 4 additions & 2 deletions common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ repositories {

content {
includeGroup("com.github.Nyan-Work")
includeGroup("com.github.gnembon")
}
}

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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<CustomPacketPayload> cir) {
if (Configs.legacyCarpetHandshake && resourceLocation.equals(CarpetClient.CARPET_CHANNEL)) {
try {
cir.setReturnValue(new LegacyCarpetVersionPayload(new FriendlyByteBuf(friendlyByteBuf.copy())));
} catch (Exception ignore) {
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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)
));
}
}
2 changes: 2 additions & 0 deletions src/main/resources/assets/tweakmyclient/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/tweakmyclient/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -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蓝屏模拟",
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/tweakmyclient.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
"patch.endPortalRendererFix.MixinTheEndPortalRenderer",
"patch.forceDebugInfoDetailed.MixinPlayer",
"patch.forcePistonWithoutAffectByTool.MixinMiningToolItem",
"patch.legacyCarpetHandshake.MixinClientboundCustomPayloadPacket",
"patch.legacyCarpetHandshake.MixinClientPacketListener",
"patch.notEnoughCrashesBlueScreenOfDeath.MixinInGameCatcher"
],
"injectors": {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package top.hendrixshen.tweakmyclient.network.legacyCarpetHandshake;

public class LegacyCarpetVersionPayload {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package top.hendrixshen.tweakmyclient.network.legacyCarpetHandshake;

public class LegacyClientNetworkHandler {
}
1 change: 1 addition & 0 deletions versions/1.20.2/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions versions/1.20.4/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d193e20

Please sign in to comment.