From 568aeb2c31b8f7e09eec23b596e96ca4e6700d6e Mon Sep 17 00:00:00 2001 From: Just Date: Fri, 3 Feb 2023 16:02:21 +0700 Subject: [PATCH] rework + upd --- README.md | 2 +- gradle.properties | 8 +- .../java/net/just_s/ctpmod/CMDHandler.java | 29 ----- src/main/java/net/just_s/ctpmod/CTPMod.java | 122 +++--------------- .../just_s/ctpmod/commands/AddCommand.java | 43 ++++++ .../just_s/ctpmod/commands/DelCommand.java | 40 ++++++ .../just_s/ctpmod/commands/ListCommand.java | 28 ++++ .../net/just_s/ctpmod/commands/RcCommand.java | 38 ++++++ .../net/just_s/ctpmod/commands/TpCommand.java | 65 ++++++++++ .../java/net/just_s/ctpmod/config/Point.java | 4 +- .../net/just_s/ctpmod/mixin/CTPMixin.java | 25 ---- .../just_s/ctpmod/util/CommandManager.java | 18 +++ .../just_s/ctpmod/util/ReconnectThread.java | 24 +--- src/main/resources/ctpmod.mixins.json | 6 +- src/main/resources/fabric.mod.json | 2 +- 15 files changed, 266 insertions(+), 188 deletions(-) delete mode 100644 src/main/java/net/just_s/ctpmod/CMDHandler.java create mode 100644 src/main/java/net/just_s/ctpmod/commands/AddCommand.java create mode 100644 src/main/java/net/just_s/ctpmod/commands/DelCommand.java create mode 100644 src/main/java/net/just_s/ctpmod/commands/ListCommand.java create mode 100644 src/main/java/net/just_s/ctpmod/commands/RcCommand.java create mode 100644 src/main/java/net/just_s/ctpmod/commands/TpCommand.java delete mode 100644 src/main/java/net/just_s/ctpmod/mixin/CTPMixin.java create mode 100644 src/main/java/net/just_s/ctpmod/util/CommandManager.java diff --git a/README.md b/README.md index b22beba..f3b034f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # CatTeleportMod-Fabric -Little mod to help you count seconds on cat-powered teleport \ No newline at end of file +A little mod to help you count seconds on cat-powered teleports \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 024470c..761e765 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,9 +3,9 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties # check these on https://fabricmc.net/versions.html - minecraft_version=1.19 - yarn_mappings=1.19+build.1 - loader_version=0.14.6 + minecraft_version=1.19.2 + yarn_mappings=1.19.2+build.28 + loader_version=0.14.13 # Mod Properties mod_version = 1.19-1.0.0 @@ -13,4 +13,4 @@ org.gradle.jvmargs=-Xmx1G archives_base_name = ctp-mod # Dependencies - fabric_version=0.55.1+1.19 + fabric_version=0.73.2+1.19.2 diff --git a/src/main/java/net/just_s/ctpmod/CMDHandler.java b/src/main/java/net/just_s/ctpmod/CMDHandler.java deleted file mode 100644 index fc3e67a..0000000 --- a/src/main/java/net/just_s/ctpmod/CMDHandler.java +++ /dev/null @@ -1,29 +0,0 @@ -package net.just_s.ctpmod; - -public class CMDHandler { - public static final CTPMod mod = new CTPMod(); - - public static void cmdHandler(String[] args) { - String message = ""; - boolean error = false; - try { - String command = args[0]; - switch (command) { - case "add" -> mod.addPoint(args); - case "del" -> mod.delPoint(args); - case "tp" -> mod.teleport(args); - case "list" -> mod.listPoints(); - case "rc" -> mod.rawTp(args); - } - error = true; - } - catch (NumberFormatException e) { message = "bad Period argument. §c" + e;} - catch (ArrayIndexOutOfBoundsException e) { message = "wrong number of arguments. §c" + e;} - catch (ClassCastException e) { message = "wrong type of argument. §c" + e;} - catch (IllegalArgumentException e) { message = "bad writing. §c" + e;} - - if (!error) { - CTPMod.printInGame(message); - } - } -} diff --git a/src/main/java/net/just_s/ctpmod/CTPMod.java b/src/main/java/net/just_s/ctpmod/CTPMod.java index f3b3b39..b732388 100644 --- a/src/main/java/net/just_s/ctpmod/CTPMod.java +++ b/src/main/java/net/just_s/ctpmod/CTPMod.java @@ -3,8 +3,10 @@ import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; import net.just_s.ctpmod.config.ConfigParser; import net.just_s.ctpmod.config.Point; +import net.just_s.ctpmod.util.CommandManager; import net.just_s.ctpmod.util.ReconnectThread; import net.minecraft.client.gui.screen.*; import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen; @@ -22,130 +24,40 @@ @Environment(EnvType.CLIENT) public class CTPMod implements ClientModInitializer { public static final String MOD_ID = "ctpmod"; + public static final String MOD_CMD = "ctp"; public static final MinecraftClient MC = MinecraftClient.getInstance(); public static final Logger LOGGER = LogManager.getLogger(MOD_ID); public static Point[] points = new Point[0]; public static int delta = 0; public static final ConfigParser config = ConfigParser.INSTANCE; - public static ServerInfo server = null; + public static ServerInfo currentServer = null; public static CTPMod INSTANCE = new CTPMod(); - private ReconnectThread reconnectThread; + private static ReconnectThread reconnectThread; @Override public void onInitializeClient() { + CommandManager.registerCommands(); + ClientPlayConnectionEvents.JOIN.register((networkHandler, packetSender, client) -> currentServer = client.getCurrentServerEntry()); } - public void listPoints() { - String[] pointRepr = new String[points.length]; - for (int i = 0; i < points.length; i++) {pointRepr[i] = points[i].toString();} - String message = "list of loaded Points:\n" + String.join("\n", pointRepr); - printInGame(message); - } - - public void addPoint(String[] args) { - String message; - try { - if (args.length != 3) {throw new ArrayIndexOutOfBoundsException("Too much or not enough arguments.");} - String waypointName = args[1]; - String[] numbers = args[2].split("-"); - int startPeriod = Integer.parseInt(numbers[0]); - int endPeriod = Integer.parseInt(numbers[1]); - message = "Point §f" + waypointName + " §aadded§2 with period: §f" + startPeriod + "-" + endPeriod + "§2."; - - config.addPoint(new Point(waypointName, startPeriod, endPeriod)); - } - catch (NumberFormatException e) { message = "bad Period argument. §c" + e;} - catch (ArrayIndexOutOfBoundsException e) { message = "wrong number of arguments. §c" + e;} - catch (ClassCastException e) { message = "wrong type of argument. §c" + e;} - catch (IllegalArgumentException e) { message = "bad writing. §c" + e;} - - - printInGame(message); - } - - public void delPoint(String[] args) { - String message; - try { - if (args.length != 2) {throw new ArrayIndexOutOfBoundsException("Too much or not enough arguments.");} - String waypointName = args[1]; - if (config.deletePoint(waypointName)) { - message = "Point §f" + waypointName + " §cdeleted§2."; - } else { - message = "Point §f" + waypointName + " §cwas not deleted due to some issues§2."; - } - } - catch (NumberFormatException e) { message = "bad Period argument. §c" + e;} - catch (ArrayIndexOutOfBoundsException e) { message = "wrong number of arguments. §c" + e;} - catch (ClassCastException e) { message = "wrong type of argument. §c" + e;} - catch (IllegalArgumentException e) { message = "bad writing. §c" + e;} - - printInGame(message); - } - - public void teleport(String[] args) { - //Doesn't work in singleplayer, getCurrentServerEntry() returns null - if (!MC.isInSingleplayer()) { - if (args.length != 2) {throw new IllegalArgumentException("Too much or not enough arguments.");} - Point curPoint = null; - for (Point point : points) { - if (Objects.equals(point.getName(), args[1])) { - curPoint = point; - break; - } - } - if (curPoint == null) { - printInGame("§cThere is no §fPoint §cwith name \"§f" + args[1] + "§c\"."); - return; - } - if (curPoint.getStartPeriod() < 0) { - printInGame("§cCan't teleport to §fPoint §cwith negative §fstartPeriodTime§c."); - return; - } - - //if everything is okay, only then start reconnect cycle: - startReconnect(curPoint); - } else { - printInGame("§cCTPMod doesn't work in §4SinglePlayer§c. You can use §dNether Portals§c instead of rejoining."); - } - } - - public void rawTp(String[] args) { - if (!MC.isInSingleplayer()) { - if (args.length != 2) {throw new IllegalArgumentException("Too much or not enough arguments.");} - int time = Integer.parseInt(args[1]); - - if (time < 0) { - printInGame("§cCan't reconnect with negative §fstartPeriodTime§c."); - return; - } - - //if everything is okay, only then start reconnect cycle: - startReconnect(new Point("null", time, time)); - } else { - printInGame("§cCTPMod doesn't work in §4SinglePlayer§c. You can use §dNether Portals§c instead of rejoining."); - } - } - - public void startReconnect(Point point) { + public static void startReconnect(Point point) { Screen newScr = new DisconnectedScreen( new MultiplayerScreen(new TitleScreen()), Text.of("§8[§6CatTeleport§8]"), Text.of("startReconnect")); - server = MC.getCurrentServerEntry(); - ClientConnection con = Objects.requireNonNull(MC.getNetworkHandler()).getConnection(); - //con.disconnect(new LiteralText("§8[§6CatTeleport§8]")); + Objects.requireNonNull(CTPMod.MC.getNetworkHandler()).getConnection().disconnect(Text.translatable("reconnecting")); + MC.disconnect(); - reconnectThread = new ReconnectThread(server, point.getStartPeriod(), point.getEndPeriod()); + reconnectThread = new ReconnectThread(point.getStartPeriod(), point.getEndPeriod()); reconnectThread.start(); - newScr.init(MC, 0, 0); - MC.disconnect(newScr); + MC.setScreen(newScr); } public void finishReconnect() { - connectToServer(server); + connectToServer(currentServer); } public void cancelReconnect() { @@ -161,7 +73,7 @@ public void connectToServer(ServerInfo targetInfo) { ConnectScreen.connect(new MultiplayerScreen(new TitleScreen()), MC, ServerAddress.parse(targetInfo.address), targetInfo); } - public static void printInGame(String... msgargs) { + public static Text generateFeedback(String message, Object... args) { //Send message in chat that only user can see //§0 black §8 dark_gray §g minecoin_gold //§1 dark_blue §9 blue §f white @@ -169,7 +81,9 @@ public static void printInGame(String... msgargs) { //§3 dark_aqua §b aqua §e yellow //§4 dark_red §c red §6 gold //§5 dark_purple §d light_purple - String msg = String.join(", ", msgargs); - MC.inGameHud.getChatHud().addMessage(Text.of("§8[§6CatTeleport§8]§2 " + msg)); + for (int i = 0; i < args.length; i++) { + message = message.replace("{" + i + "}", args[i].toString()); + } + return Text.of("§8[§6CatTeleport§8]§2 " + message); } } diff --git a/src/main/java/net/just_s/ctpmod/commands/AddCommand.java b/src/main/java/net/just_s/ctpmod/commands/AddCommand.java new file mode 100644 index 0000000..1c473aa --- /dev/null +++ b/src/main/java/net/just_s/ctpmod/commands/AddCommand.java @@ -0,0 +1,43 @@ +package net.just_s.ctpmod.commands; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.context.CommandContext; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.just_s.ctpmod.CTPMod; +import net.just_s.ctpmod.config.Point; +import net.minecraft.command.CommandRegistryAccess; +import net.minecraft.text.Text; + +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; + +public class AddCommand { + public static void register(CommandDispatcher fabricClientCommandSourceCommandDispatcher, CommandRegistryAccess commandRegistryAccess) { + fabricClientCommandSourceCommandDispatcher.register( + literal(CTPMod.MOD_CMD).then( + literal("add").then( + argument("name", StringArgumentType.word()).then( + argument("startPeriod", IntegerArgumentType.integer(0)).then( + argument("endPeriod", IntegerArgumentType.integer(1)). + executes(AddCommand::run) + ) + ) + ) + ) + ); + } + + private static int run(CommandContext ctx) { + String waypointName = ctx.getArgument("name", String.class); + int startPeriod = ctx.getArgument("startPeriod", int.class); + int endPeriod = ctx.getArgument("endPeriod", int.class); + CTPMod.config.addPoint(new Point(waypointName, startPeriod, endPeriod)); + ctx.getSource().sendFeedback(CTPMod.generateFeedback( + "Point §f{0} §aadded§2 with period: §f{1}-{2}§2.", + waypointName, startPeriod, endPeriod + )); + return 1; + } +} diff --git a/src/main/java/net/just_s/ctpmod/commands/DelCommand.java b/src/main/java/net/just_s/ctpmod/commands/DelCommand.java new file mode 100644 index 0000000..84c96a0 --- /dev/null +++ b/src/main/java/net/just_s/ctpmod/commands/DelCommand.java @@ -0,0 +1,40 @@ +package net.just_s.ctpmod.commands; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.context.CommandContext; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.just_s.ctpmod.CTPMod; +import net.minecraft.command.CommandRegistryAccess; + +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; + +public class DelCommand { + public static void register(CommandDispatcher fabricClientCommandSourceCommandDispatcher, CommandRegistryAccess commandRegistryAccess) { + fabricClientCommandSourceCommandDispatcher.register( + literal(CTPMod.MOD_CMD).then( + literal("delete").then( + argument("name", StringArgumentType.word()). + executes(DelCommand::run) + ) + ) + ); + } + + private static int run(CommandContext ctx) { + String waypointName = ctx.getArgument("name", String.class); + if (CTPMod.config.deletePoint(waypointName)) { + ctx.getSource().sendFeedback(CTPMod.generateFeedback( + "Point §f{0} §cdeleted§2.", + waypointName + )); + } else { + ctx.getSource().sendFeedback(CTPMod.generateFeedback( + "Point §f{0} §cwas not deleted due to some issues§2.", + waypointName + )); + } + return 1; + } +} diff --git a/src/main/java/net/just_s/ctpmod/commands/ListCommand.java b/src/main/java/net/just_s/ctpmod/commands/ListCommand.java new file mode 100644 index 0000000..ea56444 --- /dev/null +++ b/src/main/java/net/just_s/ctpmod/commands/ListCommand.java @@ -0,0 +1,28 @@ +package net.just_s.ctpmod.commands; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.context.CommandContext; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.just_s.ctpmod.CTPMod; +import net.minecraft.command.CommandRegistryAccess; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; + +public class ListCommand { + public static void register(CommandDispatcher fabricClientCommandSourceCommandDispatcher, CommandRegistryAccess commandRegistryAccess) { + fabricClientCommandSourceCommandDispatcher.register( + literal(CTPMod.MOD_CMD).then( + literal("list").executes(ListCommand::run) + ) + ); + } + + private static int run(CommandContext ctx) { + String[] pointRepr = new String[CTPMod.points.length]; + for (int i = 0; i < CTPMod.points.length; i++) {pointRepr[i] = CTPMod.points[i].toString();} + String message = "list of loaded Points:\n" + String.join("\n", pointRepr); + ctx.getSource().sendFeedback(CTPMod.generateFeedback( + message + )); + return 1; + } +} diff --git a/src/main/java/net/just_s/ctpmod/commands/RcCommand.java b/src/main/java/net/just_s/ctpmod/commands/RcCommand.java new file mode 100644 index 0000000..029cc72 --- /dev/null +++ b/src/main/java/net/just_s/ctpmod/commands/RcCommand.java @@ -0,0 +1,38 @@ +package net.just_s.ctpmod.commands; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.context.CommandContext; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.just_s.ctpmod.CTPMod; +import net.just_s.ctpmod.config.Point; +import net.minecraft.command.CommandRegistryAccess; + +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; + +public class RcCommand { + public static void register(CommandDispatcher fabricClientCommandSourceCommandDispatcher, CommandRegistryAccess commandRegistryAccess) { + fabricClientCommandSourceCommandDispatcher.register( + literal(CTPMod.MOD_CMD).then( + literal("rc").then( + argument("seconds", IntegerArgumentType.integer(1)). + executes(RcCommand::run) + ) + ) + ); + } + + private static int run(CommandContext ctx) { + int seconds = ctx.getArgument("seconds", int.class); + if (CTPMod.MC.isInSingleplayer()) { + ctx.getSource().sendFeedback(CTPMod.generateFeedback( + "§cCTPMod doesn't work in §4SinglePlayer§c. You can use §dNether Portals§c instead of rejoining." + )); + return 1; + } + CTPMod.startReconnect(new Point(null, seconds, seconds)); + return 1; + } +} diff --git a/src/main/java/net/just_s/ctpmod/commands/TpCommand.java b/src/main/java/net/just_s/ctpmod/commands/TpCommand.java new file mode 100644 index 0000000..4af4226 --- /dev/null +++ b/src/main/java/net/just_s/ctpmod/commands/TpCommand.java @@ -0,0 +1,65 @@ +package net.just_s.ctpmod.commands; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.just_s.ctpmod.CTPMod; +import net.just_s.ctpmod.config.Point; +import net.minecraft.command.CommandRegistryAccess; + +import java.util.Objects; +import java.util.concurrent.CompletableFuture; + +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.argument; +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; + +public class TpCommand { + public static void register(CommandDispatcher fabricClientCommandSourceCommandDispatcher, CommandRegistryAccess commandRegistryAccess) { + fabricClientCommandSourceCommandDispatcher.register( + literal(CTPMod.MOD_CMD).then( + literal("tp").then( + argument("name", StringArgumentType.word()). + suggests(TpCommand::suggest).executes(TpCommand::run) + ) + ) + ); + } + + private static CompletableFuture suggest(CommandContext ctx, SuggestionsBuilder builder) { + for (Point point : CTPMod.points) { + builder.suggest(point.getName()); + } + return builder.buildFuture(); + } + + private static int run(CommandContext ctx) { + if (CTPMod.MC.isInSingleplayer()) { + ctx.getSource().sendFeedback(CTPMod.generateFeedback( + "§cCTPMod doesn't work in §4SinglePlayer§c. You can use §dNether Portals§c instead of rejoining." + )); + return 1; + } + String waypointName = ctx.getArgument("name", String.class); + Point curPoint = null; + for (Point point : CTPMod.points) { + if (Objects.equals(point.getName(), waypointName)) { + curPoint = point; + break; + } + } + if (curPoint == null) { + ctx.getSource().sendFeedback(CTPMod.generateFeedback( + "§cThere is no §fPoint §cwith name \"§f{0}§c\".", + waypointName + )); + return 0; + } + + //if everything is okay, only then start reconnect cycle: + CTPMod.startReconnect(curPoint); + return 1; + } +} diff --git a/src/main/java/net/just_s/ctpmod/config/Point.java b/src/main/java/net/just_s/ctpmod/config/Point.java index 47d26ef..6086967 100644 --- a/src/main/java/net/just_s/ctpmod/config/Point.java +++ b/src/main/java/net/just_s/ctpmod/config/Point.java @@ -1,6 +1,8 @@ package net.just_s.ctpmod.config; import com.google.gson.Gson; + +import javax.annotation.Nullable; import java.util.HashMap; import java.util.Map; @@ -9,7 +11,7 @@ public class Point { private int startPeriod; private int endPeriod; - public Point(String name, int startPeriod, int endPeriod) { + public Point(@Nullable String name, int startPeriod, int endPeriod) { this.name = name; this.startPeriod = startPeriod; this.endPeriod = endPeriod; diff --git a/src/main/java/net/just_s/ctpmod/mixin/CTPMixin.java b/src/main/java/net/just_s/ctpmod/mixin/CTPMixin.java deleted file mode 100644 index 6d57417..0000000 --- a/src/main/java/net/just_s/ctpmod/mixin/CTPMixin.java +++ /dev/null @@ -1,25 +0,0 @@ -package net.just_s.ctpmod.mixin; - -import net.just_s.ctpmod.CMDHandler; - -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.minecraft.client.network.ClientPlayerEntity; -import net.minecraft.network.message.ChatMessageSigner; -import net.minecraft.text.Text; -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; - -@Environment(EnvType.CLIENT) -@Mixin(ClientPlayerEntity.class) -public class CTPMixin { - @Inject(at = @At("HEAD"), method = "sendChatMessagePacket", cancellable = true) - private void sendCustomMessage(ChatMessageSigner signer, String message, Text preview, CallbackInfo ci) { - if (message.charAt(0) == '&') { - CMDHandler.cmdHandler(message.substring(1).split(" ")); - ci.cancel(); - } - } -} diff --git a/src/main/java/net/just_s/ctpmod/util/CommandManager.java b/src/main/java/net/just_s/ctpmod/util/CommandManager.java new file mode 100644 index 0000000..b5965ff --- /dev/null +++ b/src/main/java/net/just_s/ctpmod/util/CommandManager.java @@ -0,0 +1,18 @@ +package net.just_s.ctpmod.util; + +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.just_s.ctpmod.commands.AddCommand; +import net.just_s.ctpmod.commands.DelCommand; +import net.just_s.ctpmod.commands.RcCommand; +import net.just_s.ctpmod.commands.TpCommand; +import net.just_s.ctpmod.commands.ListCommand; + +public class CommandManager { + public static void registerCommands() { + ClientCommandRegistrationCallback.EVENT.register(AddCommand::register); + ClientCommandRegistrationCallback.EVENT.register(DelCommand::register); + ClientCommandRegistrationCallback.EVENT.register(TpCommand::register); + ClientCommandRegistrationCallback.EVENT.register(ListCommand::register); + ClientCommandRegistrationCallback.EVENT.register(RcCommand::register); + } +} diff --git a/src/main/java/net/just_s/ctpmod/util/ReconnectThread.java b/src/main/java/net/just_s/ctpmod/util/ReconnectThread.java index 23b8e8c..46d2730 100644 --- a/src/main/java/net/just_s/ctpmod/util/ReconnectThread.java +++ b/src/main/java/net/just_s/ctpmod/util/ReconnectThread.java @@ -16,12 +16,10 @@ @Environment(EnvType.CLIENT) public class ReconnectThread extends Thread { private final int secondsToReconnect; - private final ServerAddress serverAddress; - public ReconnectThread(ServerInfo serverInfo, int start, int end) { + public ReconnectThread(int start, int end) { super(); this.secondsToReconnect = (end - start) / 2 + start; - this.serverAddress = ServerAddress.parse(serverInfo.address); } /** @@ -34,26 +32,14 @@ public void run() { CTPMod.LOGGER.info("reconnect in " + Collections.max(Arrays.asList(ArrayUtils.toObject(s))) + " sec"); Thread.sleep((Collections.max(Arrays.asList(ArrayUtils.toObject(s)))) * 1000L); - for (int i1 = 0; i1 < 10; i1++) { - pingServer(); - } synchronized (this) { CTPMod.LOGGER.info("Reconnecting to server."); - MinecraftClient.getInstance().execute(() -> CTPMod.INSTANCE.finishReconnect()); + MinecraftClient.getInstance().execute(CTPMod.INSTANCE::finishReconnect); } return; - } catch (IOException | InterruptedException e) { - CTPMod.LOGGER.error("Reconnection failed. Reason: " + e.getMessage()); + } catch (/*IOException* |*/ InterruptedException e) { + CTPMod.LOGGER.error("Reconnection failed. Reason: " + e); } - MinecraftClient.getInstance().execute(() -> CTPMod.INSTANCE.cancelReconnect()); - } - - private void pingServer() throws IOException, InterruptedException { - long startTime = System.nanoTime(); - Socket connectionSocket = new Socket(serverAddress.getAddress(), serverAddress.getPort()); - connectionSocket.close(); - long endTime = System.nanoTime(); - if (endTime - startTime > 2 * 1e+9) - throw new IOException("Ping was greater than five seconds, being " + (endTime - startTime) * 1e-9); + MinecraftClient.getInstance().execute(CTPMod.INSTANCE::cancelReconnect); } } diff --git a/src/main/resources/ctpmod.mixins.json b/src/main/resources/ctpmod.mixins.json index 20d4bab..d2ec8aa 100644 --- a/src/main/resources/ctpmod.mixins.json +++ b/src/main/resources/ctpmod.mixins.json @@ -2,11 +2,9 @@ "required": true, "minVersion": "0.8", "package": "net.just_s.ctpmod.mixin", - "compatibilityLevel": "JAVA_16", + "compatibilityLevel": "JAVA_17", "mixins": [], - "client": [ - "CTPMixin" - ], + "client": [], "injectors": { "defaultRequire": 1 } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 3866bb0..fafb504 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -29,7 +29,7 @@ "depends": { "fabricloader": ">=0.11.3", "fabric": "*", - "minecraft": "1.19.x", + "minecraft": "~1.19", "java": ">=17" }, "suggests": {