diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/ChatFunction.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/ChatFunction.java index 1d96ddd..fd5ba79 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/ChatFunction.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/ChatFunction.java @@ -8,9 +8,11 @@ public class ChatFunction extends EvalFunction { } @Override - public void execute(Object... args) { + public Output execute(Object... args) { final String message = (String) args[0]; bot.chat.send(message); + + return null; } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/CoreFunction.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/CoreFunction.java index 72ec82e..a10b052 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/CoreFunction.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/CoreFunction.java @@ -8,9 +8,11 @@ public class CoreFunction extends EvalFunction { } @Override - public void execute(Object... args) { + public Output execute(Object... args) { final String command = (String) args[0]; bot.core.run(command); + + return null; } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/CorePlaceBlockFunction.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/CorePlaceBlockFunction.java index 6d65969..39813c9 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/CorePlaceBlockFunction.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/CorePlaceBlockFunction.java @@ -8,9 +8,11 @@ public class CorePlaceBlockFunction extends EvalFunction { } @Override - public void execute(Object... args) { + public Output execute(Object... args) { final String command = (String) args[0]; bot.core.runPlaceBlock(command); + + return null; } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/EvalFunction.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/EvalFunction.java index cdfdfec..c9b59f7 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/EvalFunction.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/EvalFunction.java @@ -1,7 +1,6 @@ package land.chipmunk.chayapak.chomens_bot.evalFunctions; import land.chipmunk.chayapak.chomens_bot.Bot; -import land.chipmunk.chayapak.chomens_bot.data.EvalOutput; public class EvalFunction { public final String name; @@ -16,5 +15,15 @@ public class EvalFunction { this.bot = bot; } - public void execute (Object ...args) {} + public Output execute (Object ...args) { return null; } + + public static class Output { + public final String message; + public final boolean parseJSON; + + public Output (String message, boolean parseJSON) { + this.message = message; + this.parseJSON = parseJSON; + } + } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/GetPlayerListFunction.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/GetPlayerListFunction.java new file mode 100644 index 0000000..97bf1cf --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/GetPlayerListFunction.java @@ -0,0 +1,34 @@ +package land.chipmunk.chayapak.chomens_bot.evalFunctions; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerEntry; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; + +import java.util.List; + +public class GetPlayerListFunction extends EvalFunction { + public GetPlayerListFunction(Bot bot) { + super("getPlayerList", bot); + } + + @Override + public Output execute(Object... args) { + final List list = bot.players.list; + + final JsonArray array = new JsonArray(); + + for (PlayerEntry entry : list) { + final JsonObject object = new JsonObject(); + + object.addProperty("uuid", entry.profile.getIdAsString()); + object.addProperty("username", entry.profile.getName()); + if (entry.displayName != null) object.addProperty("displayName", GsonComponentSerializer.gson().serialize(entry.displayName)); + + array.add(object); + } + + return new Output(array.toString(), true); + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/EvalPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/EvalPlugin.java index 292a9f1..c54df19 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/EvalPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/EvalPlugin.java @@ -6,12 +6,12 @@ import io.socket.client.IO; import io.socket.client.Socket; import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.data.EvalOutput; -import land.chipmunk.chayapak.chomens_bot.evalFunctions.ChatFunction; -import land.chipmunk.chayapak.chomens_bot.evalFunctions.CoreFunction; -import land.chipmunk.chayapak.chomens_bot.evalFunctions.CorePlaceBlockFunction; -import land.chipmunk.chayapak.chomens_bot.evalFunctions.EvalFunction; +import land.chipmunk.chayapak.chomens_bot.evalFunctions.*; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.concurrent.CompletableFuture; public class EvalPlugin { @@ -33,6 +33,7 @@ public class EvalPlugin { functions.add(new CoreFunction(bot)); functions.add(new CorePlaceBlockFunction(bot)); functions.add(new ChatFunction(bot)); + functions.add(new GetPlayerListFunction(bot)); try { socket = IO.socket(bot.config.eval.address); @@ -55,7 +56,13 @@ public class EvalPlugin { socket.on(Socket.EVENT_DISCONNECT, (args) -> connected = false); socket.on(Socket.EVENT_CONNECT_ERROR, (args) -> connected = false); - for (EvalFunction function : functions) socket.on(BRIDGE_PREFIX + function.name, function::execute); + for (EvalFunction function : functions) { + socket.on(BRIDGE_PREFIX + function.name, args -> { + final EvalFunction.Output output = function.execute(args); + + socket.emit("functionOutput:" + function.name, output.message, output.parseJSON); + }); + } socket.on("codeOutput", (args) -> { final int id = (int) args[0];