diff --git a/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java b/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java new file mode 100644 index 0000000..24b149e --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java @@ -0,0 +1,22 @@ +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 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 lombok.Getter; +import land.chipmunk.chipmunkbot.plugins.*; + +public class ChipmunkBot extends Client { + @Getter public ChatPlugin chat = new ChatPlugin(this); + @Getter public PlayerListPlugin playerList = new PlayerListPlugin(this); + @Getter public CommandManagerPlugin commandManager = new CommandManagerPlugin(this); + + public ChipmunkBot (ClientOptions options) { super(options); } +} diff --git a/src/main/java/land/chipmunk/chipmunkbot/Client.java b/src/main/java/land/chipmunk/chipmunkbot/Client.java index 4e4ca98..55b14d8 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/Client.java +++ b/src/main/java/land/chipmunk/chipmunkbot/Client.java @@ -14,7 +14,6 @@ import lombok.Getter; public class Client { @Getter private final Session session; - private Map plugins = new HashMap(); public Client (ClientOptions options) { Session session = new TcpClientSession(options.host(), options.port(), options.protocol(), options.proxy()); @@ -22,26 +21,4 @@ public class Client { session.connect(); } - - public Plugin getPlugin (String id) { return plugins.get(id); } - - public void loadPlugin (Class pluginClass) { - try { - Plugin plugin = pluginClass.newInstance(); - plugin.inject(this, null); - if (plugin instanceof SessionListener) session.addListener((SessionListener) plugin); - plugins.put(plugin.id(), plugin); - } catch (Exception ignored) { - } - } - - // TODO: Maybe also add unloading? - - public void inject (Class injectorClass) { - try { - Injector injector = injectorClass.newInstance(); - injector.inject(this); - } catch (Exception ignored) { - } - } } diff --git a/src/main/java/land/chipmunk/chipmunkbot/Injector.java b/src/main/java/land/chipmunk/chipmunkbot/Injector.java deleted file mode 100644 index 4745e8c..0000000 --- a/src/main/java/land/chipmunk/chipmunkbot/Injector.java +++ /dev/null @@ -1,5 +0,0 @@ -package land.chipmunk.chipmunkbot; - -public interface Injector { - public void inject (Client client); -} diff --git a/src/main/java/land/chipmunk/chipmunkbot/Main.java b/src/main/java/land/chipmunk/chipmunkbot/Main.java index dd53277..260f233 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/Main.java +++ b/src/main/java/land/chipmunk/chipmunkbot/Main.java @@ -18,10 +18,6 @@ import com.google.gson.JsonParser; import com.google.gson.Gson; -import land.chipmunk.chipmunkbot.plugins.ChatPlugin; -import land.chipmunk.chipmunkbot.plugins.PlayerListPlugin; -import land.chipmunk.chipmunkbot.plugins.CommandManagerPlugin; - public class Main { private static File CONFIG_FILE = new File("config.json"); @@ -52,10 +48,7 @@ public class Main { for (JsonElement element : config.get("bots").getAsJsonArray()) { ClientOptions options = parseClientOptions(element.getAsJsonObject()); - Client client = new Client(options); // TODO: Maybe create a list of some sort - client.loadPlugin(ChatPlugin.class); - client.loadPlugin(PlayerListPlugin.class); - client.loadPlugin(CommandManagerPlugin.class); + Client client = new ChipmunkBot(options); // TODO: Maybe create a list of some sort } } } diff --git a/src/main/java/land/chipmunk/chipmunkbot/Plugin.java b/src/main/java/land/chipmunk/chipmunkbot/Plugin.java deleted file mode 100644 index 7de4f74..0000000 --- a/src/main/java/land/chipmunk/chipmunkbot/Plugin.java +++ /dev/null @@ -1,8 +0,0 @@ -package land.chipmunk.chipmunkbot; - -import com.google.gson.JsonObject; - -public interface Plugin { - String id (); - void inject (Client client, JsonObject options); -} diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatPlugin.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatPlugin.java index 7714a17..c6da7de 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatPlugin.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/ChatPlugin.java @@ -1,6 +1,5 @@ package land.chipmunk.chipmunkbot.plugins; -import land.chipmunk.chipmunkbot.Plugin; import land.chipmunk.chipmunkbot.Client; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundPlayerChatPacket; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundSystemChatPacket; @@ -9,20 +8,14 @@ import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundCh import com.github.steveice10.packetlib.packet.Packet; import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.event.session.SessionAdapter; -import com.google.gson.JsonObject; import java.util.BitSet; import java.util.ArrayList; import java.util.List; import java.time.Instant; -public class ChatPlugin implements Plugin { +public class ChatPlugin { private Client client; - - @Override public String id () { return "chat"; } - - public void inject (Client client, JsonObject options) { - this.client = client; - } + public ChatPlugin (Client client) { this.client = client; } public void message (String message) { final ServerboundChatPacket packet = new ServerboundChatPacket(message, Instant.now().toEpochMilli(), 0, new byte[0], false, new ArrayList<>(), null); diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManagerPlugin.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManagerPlugin.java index 209face..9fd271c 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManagerPlugin.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/CommandManagerPlugin.java @@ -2,20 +2,13 @@ package land.chipmunk.chipmunkbot.plugins; import com.mojang.brigadier.CommandDispatcher; import land.chipmunk.chipmunkbot.Client; -import land.chipmunk.chipmunkbot.Plugin; import land.chipmunk.chipmunkbot.command.CommandSource; -import com.google.gson.JsonObject; import lombok.Getter; import lombok.Setter; -public class CommandManagerPlugin implements Plugin { +public class CommandManagerPlugin { private Client client; @Getter @Setter private CommandDispatcher dispatcher = new CommandDispatcher<>(); - @Override public String id () { return "command_manager"; } - - @Override - public void inject (Client client, JsonObject options) { - this.client = client; - } + public CommandManagerPlugin (Client client) { this.client = client; } } diff --git a/src/main/java/land/chipmunk/chipmunkbot/plugins/PlayerListPlugin.java b/src/main/java/land/chipmunk/chipmunkbot/plugins/PlayerListPlugin.java index 4181a62..0b5bb6f 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/plugins/PlayerListPlugin.java +++ b/src/main/java/land/chipmunk/chipmunkbot/plugins/PlayerListPlugin.java @@ -1,6 +1,5 @@ package land.chipmunk.chipmunkbot.plugins; -import land.chipmunk.chipmunkbot.Plugin; import land.chipmunk.chipmunkbot.Client; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundPlayerInfoPacket; import com.github.steveice10.packetlib.packet.Packet; @@ -9,37 +8,31 @@ import com.github.steveice10.packetlib.event.session.SessionAdapter; import com.github.steveice10.mc.protocol.data.game.PlayerListEntry; import com.github.steveice10.mc.protocol.data.game.PlayerListEntryAction; import land.chipmunk.chipmunkbot.data.MutablePlayerListEntry; -import com.google.gson.JsonObject; import java.util.List; import java.util.ArrayList; import java.util.UUID; -public class PlayerListPlugin extends SessionAdapter implements Plugin { +public class PlayerListPlugin extends SessionAdapter { private Client client; public List list = new ArrayList(); - @Override public String id () { return "player_list"; } - - @Override - public void inject (Client client, JsonObject options) { - this.client = client; - } + public PlayerListPlugin (Client client) { this.client = client; } @Override public void packetReceived (Session session, Packet packet) { - if (packet instanceof ClientboundPlayerInfoPacket) { - ClientboundPlayerInfoPacket _packet = (ClientboundPlayerInfoPacket) packet; + if (packet instanceof ClientboundPlayerInfoPacket) packetReceived(session, (ClientboundPlayerInfoPacket) packet); + } - PlayerListEntryAction action = _packet.getAction(); + public void packetReceived (Session session, ClientboundPlayerInfoPacket packet) { + PlayerListEntryAction action = packet.getAction(); - for (PlayerListEntry entry : _packet.getEntries()) { - if (action == PlayerListEntryAction.ADD_PLAYER) addPlayer(entry); - else if (action == PlayerListEntryAction.UPDATE_GAMEMODE) updateGamemode(entry); - else if (action == PlayerListEntryAction.UPDATE_LATENCY) updateLatency(entry); - else if (action == PlayerListEntryAction.UPDATE_DISPLAY_NAME) updateDisplayName(entry); - else if (action == PlayerListEntryAction.REMOVE_PLAYER) removePlayer(entry); - } + for (PlayerListEntry entry : packet.getEntries()) { + if (action == PlayerListEntryAction.ADD_PLAYER) addPlayer(entry); + else if (action == PlayerListEntryAction.UPDATE_GAMEMODE) updateGamemode(entry); + else if (action == PlayerListEntryAction.UPDATE_LATENCY) updateLatency(entry); + else if (action == PlayerListEntryAction.UPDATE_DISPLAY_NAME) updateDisplayName(entry); + else if (action == PlayerListEntryAction.REMOVE_PLAYER) removePlayer(entry); } }