refactor (eval): getBotUsername -> getBotInfo & make core() tracked in eval
This commit is contained in:
parent
4b16f12623
commit
f561a9c810
5 changed files with 40 additions and 25 deletions
|
@ -15,7 +15,7 @@ public class EvalFunction {
|
|||
this.bot = bot;
|
||||
}
|
||||
|
||||
public Output execute (Object ...args) { return null; }
|
||||
public Output execute (Object ...args) throws Exception { return null; }
|
||||
|
||||
public static class Output {
|
||||
public final String message;
|
||||
|
|
|
@ -2,6 +2,11 @@ package me.chayapak1.chomens_bot.evalFunctions;
|
|||
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import me.chayapak1.chomens_bot.data.eval.EvalFunction;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class CoreFunction extends EvalFunction {
|
||||
public CoreFunction (Bot bot) {
|
||||
|
@ -9,13 +14,13 @@ public class CoreFunction extends EvalFunction {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Output execute(Object... args) {
|
||||
public Output execute(Object... args) throws Exception {
|
||||
if (args.length == 0) return null;
|
||||
|
||||
final String command = (String) args[0];
|
||||
|
||||
bot.core.run(command);
|
||||
final CompletableFuture<Component> future = bot.core.runTracked(command);
|
||||
|
||||
return null;
|
||||
return new Output(GsonComponentSerializer.gson().serialize(future.get(1, TimeUnit.SECONDS)), true);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package me.chayapak1.chomens_bot.evalFunctions;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import me.chayapak1.chomens_bot.data.eval.EvalFunction;
|
||||
|
||||
public class GetBotInfoFunction extends EvalFunction {
|
||||
public GetBotInfoFunction(Bot bot) {
|
||||
super("getBotInfo", bot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Output execute(Object... args) {
|
||||
final JsonObject object = new JsonObject();
|
||||
|
||||
object.addProperty("usernane", bot.username);
|
||||
object.addProperty("host", bot.host);
|
||||
object.addProperty("port", bot.port);
|
||||
object.addProperty("loggedIn", bot.loggedIn);
|
||||
|
||||
return new Output(object.toString(), true);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package me.chayapak1.chomens_bot.evalFunctions;
|
||||
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import me.chayapak1.chomens_bot.data.eval.EvalFunction;
|
||||
|
||||
public class GetBotUsernameFunction extends EvalFunction {
|
||||
public GetBotUsernameFunction (Bot bot) {
|
||||
super("getBotUsername", bot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Output execute(Object... args) {
|
||||
return new Output(bot.username, false);
|
||||
}
|
||||
}
|
|
@ -35,7 +35,7 @@ public class EvalPlugin {
|
|||
functions.add(new CorePlaceBlockFunction(bot));
|
||||
functions.add(new ChatFunction(bot));
|
||||
functions.add(new GetPlayerListFunction(bot));
|
||||
functions.add(new GetBotUsernameFunction(bot));
|
||||
functions.add(new GetBotInfoFunction(bot));
|
||||
functions.add(new GetLatestChatMessageFunction(bot));
|
||||
|
||||
try {
|
||||
|
@ -60,13 +60,15 @@ public class EvalPlugin {
|
|||
socket.on(Socket.EVENT_CONNECT_ERROR, (args) -> connected = false);
|
||||
|
||||
for (EvalFunction function : functions) {
|
||||
socket.on(BRIDGE_PREFIX + function.name, args -> {
|
||||
socket.on(BRIDGE_PREFIX + function.name, args -> new Thread(() -> {
|
||||
try {
|
||||
final EvalFunction.Output output = function.execute(args);
|
||||
|
||||
if (output == null) return;
|
||||
|
||||
socket.emit("functionOutput:" + function.name, output.message, output.parseJSON);
|
||||
});
|
||||
} catch (Exception ignored) {}
|
||||
}).start());
|
||||
}
|
||||
|
||||
socket.on("codeOutput", (args) -> {
|
||||
|
|
Loading…
Reference in a new issue