From 0341df11f2b5f78cbc68c63b252d9d72db03d354 Mon Sep 17 00:00:00 2001 From: Chip <65827213+ChipmunkMC@users.noreply.github.com> Date: Sat, 21 Jan 2023 23:18:30 -0500 Subject: [PATCH] Reconnecting Expect to see this hosted 24/7 on the same laptop as the gitea soon! --- .../land/chipmunk/chipmunkbot/Client.java | 49 ++++++++++++++++--- .../chipmunk/chipmunkbot/ClientOptions.java | 1 + .../java/land/chipmunk/chipmunkbot/Main.java | 3 +- .../commands/ReconnectCommand.java | 28 +++++++++++ .../chipmunkbot/plugins/ChatPlugin.java | 2 +- .../chipmunkbot/plugins/CommandCore.java | 2 +- .../chipmunkbot/plugins/CommandManager.java | 9 +++- .../chipmunkbot/plugins/PlayerListPlugin.java | 2 +- .../chipmunkbot/plugins/PositionManager.java | 2 +- .../chipmunkbot/plugins/QueryPlugin.java | 2 +- src/main/resources/default_config.json | 5 +- 11 files changed, 89 insertions(+), 16 deletions(-) create mode 100644 src/main/java/land/chipmunk/chipmunkbot/commands/ReconnectCommand.java diff --git a/src/main/java/land/chipmunk/chipmunkbot/Client.java b/src/main/java/land/chipmunk/chipmunkbot/Client.java index 55b14d8..9946ef1 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/Client.java +++ b/src/main/java/land/chipmunk/chipmunkbot/Client.java @@ -5,20 +5,55 @@ import com.github.steveice10.mc.protocol.MinecraftProtocol; import com.github.steveice10.packetlib.ProxyInfo; import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.tcp.TcpClientSession; -import com.github.steveice10.packetlib.event.session.SessionListener; -import java.util.Map; -import java.util.HashMap; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; +import com.github.steveice10.packetlib.packet.Packet; +import com.github.steveice10.packetlib.event.session.*; +import java.util.Timer; +import java.util.TimerTask; +import java.util.List; +import java.util.ArrayList; import lombok.Getter; public class Client { - @Getter private final Session session; + @Getter private Session session; + @Getter private final ClientOptions options; + @Getter private final List listeners = new ArrayList<>(); public Client (ClientOptions options) { - Session session = new TcpClientSession(options.host(), options.port(), options.protocol(), options.proxy()); + this.options = new ClientOptions(options.host(), options.port(), options.protocol(), options.proxy(), options.reconnectDelay()); + reconnect(); + } + + public void addListener (SessionListener listener) { listeners.add(listener); } + + public void reconnect () { // ? Should this be public? + final Session session = new TcpClientSession(options.host(), options.port(), options.protocol(), options.proxy()); this.session = session; session.connect(); + + session.addListener(new SessionAdapter () { + @Override public void packetReceived(Session session, Packet packet) { for (SessionListener listener : listeners) listener.packetReceived(session, packet); } + @Override public void packetSending (PacketSendingEvent event) { for (SessionListener listener : listeners) listener.packetSending(event); } + @Override public void packetSent (Session session, Packet packet) { for (SessionListener listener : listeners) listener.packetSent(session, packet); } + @Override public void packetError (PacketErrorEvent event) { for (SessionListener listener : listeners) listener.packetError(event); } + @Override public void connected (ConnectedEvent event) { for (SessionListener listener : listeners) listener.connected(event); } + @Override public void disconnecting (DisconnectingEvent event) { for (SessionListener listener : listeners) listener.disconnecting(event); } + + @Override + public void disconnected (DisconnectedEvent event) { + for (SessionListener listener : listeners) listener.disconnected(event); + + if (options.reconnectDelay() < 0) return; + + TimerTask task = new TimerTask() { + @Override + public void run () { + reconnect(); + } + }; + + new Timer().schedule(task, options.reconnectDelay()); + } + }); } } diff --git a/src/main/java/land/chipmunk/chipmunkbot/ClientOptions.java b/src/main/java/land/chipmunk/chipmunkbot/ClientOptions.java index d422757..8455a82 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/ClientOptions.java +++ b/src/main/java/land/chipmunk/chipmunkbot/ClientOptions.java @@ -17,6 +17,7 @@ public class ClientOptions { private int port; private MinecraftProtocol protocol; private ProxyInfo proxy; + private long reconnectDelay; /* public ClientOptions profile (GameProfile profile) { protocol(new MinecraftProtocol(profile)); diff --git a/src/main/java/land/chipmunk/chipmunkbot/Main.java b/src/main/java/land/chipmunk/chipmunkbot/Main.java index 3288a2b..7b2b57a 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/Main.java +++ b/src/main/java/land/chipmunk/chipmunkbot/Main.java @@ -55,7 +55,8 @@ public class Main { return new ClientOptions() .host(options.has("host") ? options.get("host").getAsString() : "0.0.0.0") .port(options.has("port") ? options.get("port").getAsInt() : 25565) - .username(options.has("username") ? options.get("username").getAsString() : "username"); + .username(options.has("username") ? options.get("username").getAsString() : "Player") + .reconnectDelay(options.has("reconnectDelay") ? options.get("reconnectDelay").getAsLong() : 0l); } public static void main (String[] arguments) { diff --git a/src/main/java/land/chipmunk/chipmunkbot/commands/ReconnectCommand.java b/src/main/java/land/chipmunk/chipmunkbot/commands/ReconnectCommand.java new file mode 100644 index 0000000..e1600eb --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkbot/commands/ReconnectCommand.java @@ -0,0 +1,28 @@ +package land.chipmunk.chipmunkbot.commands; + +import land.chipmunk.chipmunkbot.ChipmunkBot; +import land.chipmunk.chipmunkbot.command.*; +import static land.chipmunk.chipmunkbot.plugins.CommandManager.literal; +import com.mojang.brigadier.context.CommandContext; +import net.kyori.adventure.text.Component; + +public class ReconnectCommand extends Command { + public ReconnectCommand () { + super(); + + this.node( + literal("reconnect") + .executes(this::reconnect) + ); + } + + public int reconnect (CommandContext context) { + final CommandSource source = context.getSource(); + final ChipmunkBot client = source.client(); + + source.sendOutput(Component.translatable("Reconnecting!")); + client.session().disconnect("quit"); + + return 1; + } +} diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatPlugin.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatPlugin.java index 7dbe00b..e020741 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatPlugin.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatPlugin.java @@ -30,7 +30,7 @@ public class ChatPlugin extends SessionAdapter { public ChatPlugin (ChipmunkBot client) { this.client = client; - client.session().addListener((SessionListener) this); + client.addListener((SessionListener) this); systemChatParsers = new ArrayList<>(); systemChatParsers.add(new MinecraftChatParser(client)); diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandCore.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandCore.java index 997ed33..8c08c71 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandCore.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandCore.java @@ -38,7 +38,7 @@ public class CommandCore extends SessionAdapter { public CommandCore (ChipmunkBot client) { this.client = client; - client.session().addListener((SessionListener) this); + client.addListener((SessionListener) this); } @Override diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManager.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManager.java index 5b6dacb..af416a9 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManager.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManager.java @@ -23,7 +23,14 @@ import lombok.Setter; public class CommandManager { private ChipmunkBot client; @Getter @Setter private CommandDispatcher dispatcher = new CommandDispatcher<>(); - private final Command[] commands = {new TestCommand(), new HelpCommand(), new RunCommand(), new EchoCommand(), new InfoCommand()}; + private final Command[] commands = { + new TestCommand(), + new HelpCommand(), + new RunCommand(), + new EchoCommand(), + new InfoCommand(), + new ReconnectCommand() + }; static { // ? Is messing with static properties a good idea? diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/PlayerListPlugin.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/PlayerListPlugin.java index 4aeb22d..c38b3b6 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/PlayerListPlugin.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/PlayerListPlugin.java @@ -21,7 +21,7 @@ public class PlayerListPlugin extends SessionAdapter { public PlayerListPlugin (Client client) { this.client = client; - client.session().addListener((SessionListener) this); + client.addListener((SessionListener) this); } @Override diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/PositionManager.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/PositionManager.java index fd02937..2910801 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/PositionManager.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/PositionManager.java @@ -24,7 +24,7 @@ public class PositionManager extends SessionAdapter { public PositionManager (Client client) { this.client = client; - client.session().addListener((SessionListener) this); + client.addListener((SessionListener) this); } @Override diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/QueryPlugin.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/QueryPlugin.java index c489f41..3dbf7de 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/QueryPlugin.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/QueryPlugin.java @@ -21,7 +21,7 @@ public class QueryPlugin extends SessionAdapter { public QueryPlugin (Client client) { this.client = client; - client.session().addListener((SessionListener) this); + client.addListener((SessionListener) this); } public CompletableFuture block (Vector3i position) { diff --git a/src/main/resources/default_config.json b/src/main/resources/default_config.json index a736854..501b5e5 100644 --- a/src/main/resources/default_config.json +++ b/src/main/resources/default_config.json @@ -3,7 +3,8 @@ { "host": "localhost", "port": 25565, - "username": "ChipmunkBot" + "username": "ChipmunkBot", + "reconnectDelay": 1000 } ] -} \ No newline at end of file +}