Remove plugins
I may revert this decision, but in its current state this project is not really a library, so I see no true need to make it dynamically plugable. The plugin system was kinda bad anyway.
This commit is contained in:
parent
6feabfc10a
commit
2d5793ed27
8 changed files with 39 additions and 81 deletions
22
src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java
Normal file
22
src/main/java/land/chipmunk/chipmunkbot/ChipmunkBot.java
Normal file
|
@ -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); }
|
||||
}
|
|
@ -14,7 +14,6 @@ import lombok.Getter;
|
|||
|
||||
public class Client {
|
||||
@Getter private final Session session;
|
||||
private Map<String, Plugin> 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<? extends Plugin> 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<? extends Injector> injectorClass) {
|
||||
try {
|
||||
Injector injector = injectorClass.newInstance();
|
||||
injector.inject(this);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
package land.chipmunk.chipmunkbot;
|
||||
|
||||
public interface Injector {
|
||||
public void inject (Client client);
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
package land.chipmunk.chipmunkbot;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public interface Plugin {
|
||||
String id ();
|
||||
void inject (Client client, JsonObject options);
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -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<CommandSource> 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; }
|
||||
}
|
||||
|
|
|
@ -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,31 +8,26 @@ 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<MutablePlayerListEntry> list = new ArrayList<MutablePlayerListEntry>();
|
||||
|
||||
@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()) {
|
||||
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);
|
||||
|
@ -41,7 +35,6 @@ public class PlayerListPlugin extends SessionAdapter implements Plugin {
|
|||
else if (action == PlayerListEntryAction.REMOVE_PLAYER) removePlayer(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final MutablePlayerListEntry getEntry (UUID uuid) {
|
||||
for (MutablePlayerListEntry candidate : list) {
|
||||
|
|
Loading…
Reference in a new issue