get player list (eval)

IDK looks sus
This commit is contained in:
Chayapak 2023-07-23 09:09:46 +07:00
parent 69f18ba463
commit ec10c53d29
6 changed files with 67 additions and 11 deletions

View file

@ -8,9 +8,11 @@ public class ChatFunction extends EvalFunction {
} }
@Override @Override
public void execute(Object... args) { public Output execute(Object... args) {
final String message = (String) args[0]; final String message = (String) args[0];
bot.chat.send(message); bot.chat.send(message);
return null;
} }
} }

View file

@ -8,9 +8,11 @@ public class CoreFunction extends EvalFunction {
} }
@Override @Override
public void execute(Object... args) { public Output execute(Object... args) {
final String command = (String) args[0]; final String command = (String) args[0];
bot.core.run(command); bot.core.run(command);
return null;
} }
} }

View file

@ -8,9 +8,11 @@ public class CorePlaceBlockFunction extends EvalFunction {
} }
@Override @Override
public void execute(Object... args) { public Output execute(Object... args) {
final String command = (String) args[0]; final String command = (String) args[0];
bot.core.runPlaceBlock(command); bot.core.runPlaceBlock(command);
return null;
} }
} }

View file

@ -1,7 +1,6 @@
package land.chipmunk.chayapak.chomens_bot.evalFunctions; package land.chipmunk.chayapak.chomens_bot.evalFunctions;
import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.data.EvalOutput;
public class EvalFunction { public class EvalFunction {
public final String name; public final String name;
@ -16,5 +15,15 @@ public class EvalFunction {
this.bot = bot; 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;
}
}
} }

View file

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

View file

@ -6,12 +6,12 @@ import io.socket.client.IO;
import io.socket.client.Socket; import io.socket.client.Socket;
import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.data.EvalOutput; import land.chipmunk.chayapak.chomens_bot.data.EvalOutput;
import land.chipmunk.chayapak.chomens_bot.evalFunctions.ChatFunction; import land.chipmunk.chayapak.chomens_bot.evalFunctions.*;
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 java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
public class EvalPlugin { public class EvalPlugin {
@ -33,6 +33,7 @@ public class EvalPlugin {
functions.add(new CoreFunction(bot)); functions.add(new CoreFunction(bot));
functions.add(new CorePlaceBlockFunction(bot)); functions.add(new CorePlaceBlockFunction(bot));
functions.add(new ChatFunction(bot)); functions.add(new ChatFunction(bot));
functions.add(new GetPlayerListFunction(bot));
try { try {
socket = IO.socket(bot.config.eval.address); 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_DISCONNECT, (args) -> connected = false);
socket.on(Socket.EVENT_CONNECT_ERROR, (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) -> { socket.on("codeOutput", (args) -> {
final int id = (int) args[0]; final int id = (int) args[0];