Reconnecting

Expect to see this hosted 24/7 on the same laptop as the gitea soon!
This commit is contained in:
Chipmunk 2023-01-21 23:18:30 -05:00
parent 68c343e114
commit 0341df11f2
11 changed files with 89 additions and 16 deletions

View file

@ -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<SessionListener> 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());
}
});
}
}

View file

@ -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));

View file

@ -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) {

View file

@ -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<CommandSource> context) {
final CommandSource source = context.getSource();
final ChipmunkBot client = source.client();
source.sendOutput(Component.translatable("Reconnecting!"));
client.session().disconnect("quit");
return 1;
}
}

View file

@ -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));

View file

@ -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

View file

@ -23,7 +23,14 @@ import lombok.Setter;
public class CommandManager {
private ChipmunkBot client;
@Getter @Setter private CommandDispatcher<CommandSource> 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?

View file

@ -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

View file

@ -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

View file

@ -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<CompoundTag> block (Vector3i position) {

View file

@ -3,7 +3,8 @@
{
"host": "localhost",
"port": 25565,
"username": "ChipmunkBot"
"username": "ChipmunkBot",
"reconnectDelay": 1000
}
]
}