From 22cbd283793ff1b8df135bd7a30dfc728687b09c Mon Sep 17 00:00:00 2001 From: Chip <65827213+ChipmunkMC@users.noreply.github.com> Date: Tue, 7 Mar 2023 20:37:24 -0500 Subject: [PATCH] Better config? --- .../chipmunk/chipmunkbot/ChipmunkBot.java | 9 +++-- .../land/chipmunk/chipmunkbot/Client.java | 31 +++++++++++------ .../chipmunk/chipmunkbot/ClientOptions.java | 34 ------------------- .../java/land/chipmunk/chipmunkbot/Main.java | 33 +++++++----------- .../land/chipmunk/chipmunkbot/Options.java | 15 ++++++++ .../plugins/ChatCommandHandler.java | 6 ++-- 6 files changed, 57 insertions(+), 71 deletions(-) delete mode 100644 src/main/java/land/chipmunk/chipmunkbot/ClientOptions.java create mode 100644 src/main/java/land/chipmunk/chipmunkbot/Options.java diff --git a/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java b/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java index d554a80..03186a3 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java +++ b/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java @@ -1,6 +1,5 @@ package land.chipmunk.chipmunkbot; -import com.github.steveice10.mc.auth.service.AuthenticationService; import com.github.steveice10.mc.protocol.MinecraftProtocol; import com.github.steveice10.packetlib.ProxyInfo; import com.github.steveice10.packetlib.Session; @@ -12,17 +11,21 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import lombok.Getter; import land.chipmunk.chipmunkbot.plugins.*; +import java.util.List; public class ChipmunkBot extends Client { @Getter public final ChatPlugin chat = new ChatPlugin(this); @Getter public final QueryPlugin query = new QueryPlugin(this); @Getter public final PlayerListPlugin playerList = new PlayerListPlugin(this); @Getter public final CommandManager commandManager = new CommandManager(this); - @Getter public final ChatCommandHandler chatCommandHandler = new ChatCommandHandler(this); + @Getter public final ChatCommandHandler chatCommandHandler; @Getter public final PositionManager position = new PositionManager(this); @Getter public final CommandCore core = new CommandCore(this); @Getter public final SelfCarePlugin selfCare = new SelfCarePlugin(this); @Getter public final SongPlayer songPlayer = new SongPlayer(this); - public ChipmunkBot (ClientOptions options) { super(options); } + public ChipmunkBot (Options options, List allClients) { + super(options.host, options.port, new MinecraftProtocol(options.username), null, options.reconnectDelay, allClients); + chatCommandHandler = new ChatCommandHandler(this, options.commands); + } } diff --git a/src/main/java/land/chipmunk/chipmunkbot/Client.java b/src/main/java/land/chipmunk/chipmunkbot/Client.java index e0f5a6a..08f0f59 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/Client.java +++ b/src/main/java/land/chipmunk/chipmunkbot/Client.java @@ -1,7 +1,6 @@ package land.chipmunk.chipmunkbot; -import com.github.steveice10.mc.auth.service.AuthenticationService; -import com.github.steveice10.mc.protocol.MinecraftProtocol; +import com.github.steveice10.packetlib.packet.PacketProtocol; import com.github.steveice10.packetlib.ProxyInfo; import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.tcp.TcpClientSession; @@ -12,21 +11,34 @@ import java.util.TimerTask; import java.util.List; import java.util.ArrayList; import lombok.Getter; +import lombok.Setter; public class Client { @Getter private Session session; - @Getter private final ClientOptions options; @Getter private final List listeners = new ArrayList<>(); - public Client (ClientOptions options) { - this.options = new ClientOptions(options.host(), options.port(), options.protocol(), options.proxy(), options.reconnectDelay(), options.allClients()); + private String host; + private int port; + private PacketProtocol protocol; + private ProxyInfo proxy; + + @Getter @Setter private long reconnectDelay; + @Getter @Setter private List allClients; + + public Client (String host, int port, PacketProtocol protocol, ProxyInfo proxy, long reconnectDelay, List allClients) { + this.host = host; + this.port = port; + this.protocol = protocol; + this.proxy = proxy; + this.reconnectDelay = reconnectDelay; + this.allClients = allClients; 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()); + final Session session = new TcpClientSession(host, port, protocol, proxy); this.session = session; session.addListener(new SessionAdapter () { @@ -41,7 +53,7 @@ public class Client { public void disconnected (DisconnectedEvent event) { for (SessionListener listener : listeners) listener.disconnected(event); - if (options.reconnectDelay() < 0) return; + if (reconnectDelay < 0) return; TimerTask task = new TimerTask() { @Override @@ -50,13 +62,10 @@ public class Client { } }; - new Timer().schedule(task, options.reconnectDelay()); + new Timer().schedule(task, reconnectDelay); } }); session.connect(); } - - public long reconnectDelay () { return options.reconnectDelay(); } - public List allClients () { return options.allClients(); } } diff --git a/src/main/java/land/chipmunk/chipmunkbot/ClientOptions.java b/src/main/java/land/chipmunk/chipmunkbot/ClientOptions.java deleted file mode 100644 index 74426df..0000000 --- a/src/main/java/land/chipmunk/chipmunkbot/ClientOptions.java +++ /dev/null @@ -1,34 +0,0 @@ -package land.chipmunk.chipmunkbot; - -import com.github.steveice10.mc.auth.service.AuthenticationService; -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 lombok.Data; -import lombok.AllArgsConstructor; -import lombok.NoArgsConstructor; -import java.util.List; -import java.util.ArrayList; - -@Data -@AllArgsConstructor -@NoArgsConstructor -public class ClientOptions { - private String host; - private int port; - private MinecraftProtocol protocol; - private ProxyInfo proxy; - private long reconnectDelay = 1000l; - private List allClients = new ArrayList<>(); - - /* public ClientOptions profile (GameProfile profile) { - protocol(new MinecraftProtocol(profile)); - return this; - } */ - - public ClientOptions username (String username) { - protocol(new MinecraftProtocol(username)); - return this; - } -} diff --git a/src/main/java/land/chipmunk/chipmunkbot/Main.java b/src/main/java/land/chipmunk/chipmunkbot/Main.java index c6ce3d6..ff529c0 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/Main.java +++ b/src/main/java/land/chipmunk/chipmunkbot/Main.java @@ -18,14 +18,12 @@ import java.io.IOException; import java.util.List; import java.util.ArrayList; -import com.google.gson.JsonObject; -import com.google.gson.JsonElement; -import com.google.gson.JsonParser; - import com.google.gson.Gson; public class Main { - private static JsonObject getConfig (File file) throws IOException { + private static Configuration getConfig (File file) throws IOException { + final Gson gson = new Gson(); + if (!file.exists()) { // Read the default config InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("default_config.json"); @@ -45,27 +43,19 @@ public class Main { System.out.println("The config.json file was not found, so a default one was created."); // Return the default config (instead of reading again, for efficiency) - return JsonParser.parseString(defaultConfig).getAsJsonObject(); + return gson.fromJson(defaultConfig, Configuration.class); } InputStream opt = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(opt)); - return JsonParser.parseReader(reader).getAsJsonObject(); - } - - public static ClientOptions parseClientOptions (JsonObject options) { - 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() : "Player") - .reconnectDelay(options.has("reconnectDelay") ? options.get("reconnectDelay").getAsLong() : 0l); + return gson.fromJson(reader, Configuration.class); } public static void main (String[] arguments) { System.out.println("ChipmunkBot is starting..."); - JsonObject config = null; + Configuration config = null; try { config = getConfig(new File(arguments.length > 0 ? arguments[0] : "config.json")); } catch (Exception exception) { @@ -75,12 +65,13 @@ public class Main { List allClients = new ArrayList<>(); - for (JsonElement element : config.get("bots").getAsJsonArray()) { - ClientOptions options = parseClientOptions(element.getAsJsonObject()); - options.allClients(allClients); - - final Client client = new ChipmunkBot(options); // TODO: Maybe create a list of some sort + for (Options options : config.bots) { + final Client client = new ChipmunkBot(options, allClients); allClients.add(client); } } + + private static class Configuration { + public Options[] bots; + } } diff --git a/src/main/java/land/chipmunk/chipmunkbot/Options.java b/src/main/java/land/chipmunk/chipmunkbot/Options.java new file mode 100644 index 0000000..e4713a2 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkbot/Options.java @@ -0,0 +1,15 @@ +package land.chipmunk.chipmunkbot; + +public class Options { + public String host = "0.0.0.0"; + public int port = 25565; + public String username = "Player"; + // public ProxyInfo proxy; + public long reconnectDelay = 1000; + + public Commands commands = new Commands(); + + public class Commands { + public String prefix = "default."; + } +} diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatCommandHandler.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatCommandHandler.java index c453d72..8105ee5 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatCommandHandler.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatCommandHandler.java @@ -1,6 +1,7 @@ package land.chipmunk.chipmunkbot.plugins; import land.chipmunk.chipmunkbot.ChipmunkBot; +import land.chipmunk.chipmunkbot.Options; import land.chipmunk.chipmunkbot.command.CommandSource; import land.chipmunk.chipmunkbot.command.PlayerCommandSource; import land.chipmunk.chipmunkbot.command.ComponentMessage; @@ -18,10 +19,11 @@ import lombok.Setter; public class ChatCommandHandler extends ChatPlugin.Listener { private ChipmunkBot client; - @Getter @Setter private String prefix = "'"; // TODO: Don't hardcode this + @Getter @Setter private String prefix; - public ChatCommandHandler (ChipmunkBot client) { + public ChatCommandHandler (ChipmunkBot client, Options.Commands options) { this.client = client; + this.prefix = options.prefix; client.chat().addListener((ChatPlugin.Listener) this); }