Rewrite the plugin system for the better(or worse)

Also includes in incomplete command manager!
This commit is contained in:
Chipmunk 2022-12-23 16:23:15 -05:00
parent 3960bb64df
commit 6e8cf3f936
13 changed files with 201 additions and 19 deletions

View file

@ -1,6 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="RemoteRepositoriesConfiguration"> <component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="minecraft-libraries" />
<option name="name" value="Minecraft Libraries" />
<option name="url" value="https://libraries.minecraft.net" />
</remote-repository>
<remote-repository> <remote-repository>
<option name="id" value="central" /> <option name="id" value="central" />
<option name="name" value="Central Repository" /> <option name="name" value="Central Repository" />

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

13
pom.xml
View file

@ -17,6 +17,12 @@
<id>opencollab</id> <id>opencollab</id>
<url>https://repo.opencollab.dev/maven-releases/</url> <url>https://repo.opencollab.dev/maven-releases/</url>
</repository> </repository>
<repository>
<id>minecraft-libraries</id>
<name>Minecraft Libraries</name>
<url>https://libraries.minecraft.net</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
@ -38,6 +44,12 @@
<version>1.18.24</version> <version>1.18.24</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>com.mojang</groupId>
<artifactId>brigadier</artifactId>
<version>1.0.18</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -71,5 +83,4 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View file

@ -26,16 +26,21 @@ public class Client {
public void loadPlugin (Class<? extends Plugin> pluginClass) { public void loadPlugin (Class<? extends Plugin> pluginClass) {
try { try {
Constructor<? extends Plugin> constructor = pluginClass.getConstructor(Client.class); Plugin plugin = pluginClass.newInstance();
Plugin plugin = constructor.newInstance(this); plugin.inject(this, null);
plugins.put(plugin.id, plugin); plugins.put(plugin.id(), plugin);
System.out.println("loaded " + plugin.id());
} catch (Exception ignored) { } catch (Exception ignored) {
} }
} }
// TODO: Maybe also add unloading? // TODO: Maybe also add unloading?
public void inject (Injector injector) { public void inject (Class<? extends Injector> injectorClass) {
injector.inject(this); try {
Injector injector = injectorClass.newInstance();
injector.inject(this);
} catch (Exception ignored) {
}
} }
} }

View file

@ -20,6 +20,7 @@ import com.google.gson.Gson;
import land.chipmunk.chipmunkbot.plugins.ChatPlugin; import land.chipmunk.chipmunkbot.plugins.ChatPlugin;
import land.chipmunk.chipmunkbot.plugins.PlayerListPlugin; import land.chipmunk.chipmunkbot.plugins.PlayerListPlugin;
import land.chipmunk.chipmunkbot.plugins.CommandManagerPlugin;
public class Main { public class Main {
private static File CONFIG_FILE = new File("config.json"); private static File CONFIG_FILE = new File("config.json");
@ -54,6 +55,7 @@ public class Main {
Client client = new Client(options); // TODO: Maybe create a list of some sort Client client = new Client(options); // TODO: Maybe create a list of some sort
client.loadPlugin(ChatPlugin.class); client.loadPlugin(ChatPlugin.class);
client.loadPlugin(PlayerListPlugin.class); client.loadPlugin(PlayerListPlugin.class);
client.loadPlugin(CommandManagerPlugin.class);
} }
} }
} }

View file

@ -1,11 +1,8 @@
package land.chipmunk.chipmunkbot; package land.chipmunk.chipmunkbot;
public class Plugin { import com.google.gson.JsonObject;
public String id;
public Client client;
public Plugin (Client client, String id) { public interface Plugin {
this.client = client; String id ();
this.id = id; void inject (Client client, JsonObject options);
}
} }

View file

@ -0,0 +1,12 @@
package land.chipmunk.chipmunkbot.command;
import com.mojang.brigadier.Message;
import net.kyori.adventure.text.Component;
public interface CommandSource {
// ? Should I support message objects?
void sendOutput (Component message, boolean broadcast);
void sendOutput (Component message);
Component displayName ();
}

View file

@ -0,0 +1,66 @@
package land.chipmunk.chipmunkbot.command;
import com.mojang.brigadier.Message;
import com.mojang.brigadier.exceptions.BuiltInExceptionProvider;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.CommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.TextComponent;
import java.util.Map;
import java.util.HashMap;
public class CommandSyntaxErrorTypeMap {
private CommandSyntaxErrorTypeMap () {
}
// Just some methods for stuff repeated a lot
private static Message message (final String key, final ComponentLike... args) { return ComponentMessage.wrap(Component.translatable(key, args)); }
private static TextComponent text (final Object text) { return Component.text(String.valueOf(text)); }
private static TextComponent text (final int text) { return Component.text(text); }
private static TextComponent text (final char text) { return Component.text(String.valueOf(text)); }
public static Map<CommandExceptionType, CommandExceptionType> map = new HashMap<>();
static {
final BuiltInExceptionProvider builtIn = CommandSyntaxException.BUILT_IN_EXCEPTIONS;
map.put(builtIn.doubleTooLow(), new Dynamic2CommandExceptionType((found, min) -> message("argument.double.low", text(min), text(found))));
map.put(builtIn.doubleTooHigh(), new Dynamic2CommandExceptionType((found, max) -> message("argument.double.big", text(max), text(found))));
map.put(builtIn.floatTooLow(), new Dynamic2CommandExceptionType((found, min) -> message("argument.float.low", text(min), text(found))));
map.put(builtIn.floatTooHigh(), new Dynamic2CommandExceptionType((found, max) -> message("argument.float.big", text(max), text(found))));
map.put(builtIn.integerTooLow(), new Dynamic2CommandExceptionType((found, min) -> message("argument.integer.low", text(min), text(found))));
map.put(builtIn.integerTooHigh(), new Dynamic2CommandExceptionType((found, max) -> message("argument.integer.big", text(max), text(found))));
map.put(builtIn.longTooLow(), new Dynamic2CommandExceptionType((found, min) -> message("argument.long.low", text(min), text(found))));
map.put(builtIn.longTooHigh(), new Dynamic2CommandExceptionType((found, max) -> message("argument.long.big", text(max), text(found))));
map.put(builtIn.literalIncorrect(), new DynamicCommandExceptionType(expected -> message("argument.literal.incorrect", text(expected))));
map.put(builtIn.readerExpectedStartOfQuote(), new SimpleCommandExceptionType(message("parsing.quote.expected.start")));
map.put(builtIn.readerExpectedEndOfQuote(), new SimpleCommandExceptionType(message("parsing.quote.expected.end")));
map.put(builtIn.readerInvalidEscape(), new DynamicCommandExceptionType(character -> message("parsing.quote.escape", text(character))));
map.put(builtIn.readerInvalidBool(), new DynamicCommandExceptionType(value -> message("parsing.bool.invalid", text(value))));
map.put(builtIn.readerInvalidInt(), new DynamicCommandExceptionType(value -> message("parsing.int.invalid", text(value))));
map.put(builtIn.readerExpectedInt(), new SimpleCommandExceptionType(message("parsing.int.expected")));
map.put(builtIn.readerInvalidLong(), new DynamicCommandExceptionType(value -> message("parsing.long.invalid", text(value))));
map.put(builtIn.readerExpectedLong(), new SimpleCommandExceptionType(message("parsing.long.expected")));
map.put(builtIn.readerInvalidDouble(), new DynamicCommandExceptionType(value -> message("parsing.double.invalid", text(value))));
map.put(builtIn.readerExpectedDouble(), new SimpleCommandExceptionType(message("parsing.double.expected")));
map.put(builtIn.readerInvalidFloat(), new DynamicCommandExceptionType(value -> message("parsing.float.invalid", text(value))));
map.put(builtIn.readerExpectedFloat(), new SimpleCommandExceptionType(message("parsing.float.expected")));
map.put(builtIn.readerExpectedBool(), new SimpleCommandExceptionType(message("parsing.bool.expected")));
map.put(builtIn.readerExpectedSymbol(), new DynamicCommandExceptionType(symbol -> message("parsing.expected", text(symbol))));
map.put(builtIn.dispatcherUnknownCommand(), new SimpleCommandExceptionType(message("command.unknown.command")));
map.put(builtIn.dispatcherUnknownArgument(), new SimpleCommandExceptionType(message("command.unknown.argument")));
map.put(builtIn.dispatcherExpectedArgumentSeparator(), new SimpleCommandExceptionType(message("command.expected.separator")));
map.put(builtIn.dispatcherParseException(), new SimpleCommandExceptionType(message("command.exception")));
}
}

View file

@ -0,0 +1,25 @@
package land.chipmunk.chipmunkbot.command;
import net.kyori.adventure.text.Component;
import com.mojang.brigadier.Message;
import lombok.Getter;
public class ComponentMessage implements Message {
@Getter private final Component component;
private ComponentMessage (Component component) {
this.component = component;
}
public static ComponentMessage wrap (Component component) {
return new ComponentMessage(component);
}
public String getString () {
return component.toString(); // ? Is this the best way to get the string?
}
public String toString () {
return component.toString();
}
}

View file

@ -0,0 +1,22 @@
package land.chipmunk.chipmunkbot.command;
import com.mojang.brigadier.Message;
import net.kyori.adventure.text.Component;
public class ConsoleCommandSource implements CommandSource {
@Override
public void sendOutput (Component message, boolean broadcast) {
System.out.println(message);
// TODO: broadcast
}
@Override
public void sendOutput (Component message) {
sendOutput(message, true);
}
@Override
public Component displayName () {
return Component.text("Console");
}
}

View file

@ -9,14 +9,19 @@ import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundCh
import com.github.steveice10.packetlib.packet.Packet; import com.github.steveice10.packetlib.packet.Packet;
import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.event.session.SessionAdapter; import com.github.steveice10.packetlib.event.session.SessionAdapter;
import com.google.gson.JsonObject;
import java.util.BitSet; import java.util.BitSet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.time.Instant; import java.time.Instant;
public class ChatPlugin extends Plugin { public class ChatPlugin implements Plugin {
public ChatPlugin (Client client) { private Client client;
super(client, "chat");
@Override public String id () { return "chat"; }
public void inject (Client client, JsonObject options) {
this.client = client;
} }
public void message (String message) { public void message (String message) {

View file

@ -0,0 +1,21 @@
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 {
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;
}
}

View file

@ -9,16 +9,21 @@ 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.PlayerListEntry;
import com.github.steveice10.mc.protocol.data.game.PlayerListEntryAction; import com.github.steveice10.mc.protocol.data.game.PlayerListEntryAction;
import land.chipmunk.chipmunkbot.data.MutablePlayerListEntry; import land.chipmunk.chipmunkbot.data.MutablePlayerListEntry;
import com.google.gson.JsonObject;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.UUID; import java.util.UUID;
public class PlayerListPlugin extends Plugin { public class PlayerListPlugin implements Plugin {
private Client client;
public List<MutablePlayerListEntry> list = new ArrayList<MutablePlayerListEntry>(); public List<MutablePlayerListEntry> list = new ArrayList<MutablePlayerListEntry>();
public PlayerListPlugin (Client client) { @Override public String id () { return "player_list"; }
super(client, "player_list");
@Override
public void inject (Client client, JsonObject options) {
this.client = client;
client.session().addListener(new SessionAdapter() { client.session().addListener(new SessionAdapter() {
@Override @Override