Compare commits

..

2 commits

6 changed files with 42 additions and 27 deletions

View file

@ -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;

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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 -> {
final EvalFunction.Output output = function.execute(args);
socket.on(BRIDGE_PREFIX + function.name, args -> new Thread(() -> {
try {
final EvalFunction.Output output = function.execute(args);
if (output == null) return;
if (output == null) return;
socket.emit("functionOutput:" + function.name, output.message, output.parseJSON);
});
socket.emit("functionOutput:" + function.name, output.message, output.parseJSON);
} catch (Exception ignored) {}
}).start());
}
socket.on("codeOutput", (args) -> {

View file

@ -84,14 +84,14 @@ public class FileLoggerUtilities {
if (!Files.exists(logPath)) Files.createFile(logPath);
logWriter = Files.newBufferedWriter(logPath, StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING);
logWriter = Files.newBufferedWriter(logPath, StandardCharsets.UTF_16, StandardOpenOption.TRUNCATE_EXISTING);
logWriter.write(currentLogDate.toString() + '\n');
logWriter.flush();
}
public static void openLogFile() throws IOException {
currentLogDate = LocalDate.parse(getLogDate(logPath));
logWriter = Files.newBufferedWriter(logPath, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
logWriter = Files.newBufferedWriter(logPath, StandardCharsets.UTF_16, StandardOpenOption.APPEND);
}
public static void compressLogFile() throws IOException {