Files
Tplus/src/main/java/net/nuggetmc/ai/utils/Debugger.java

81 lines
2.3 KiB
Java
Raw Normal View History

2021-06-30 02:18:31 -05:00
package net.nuggetmc.ai.utils;
import net.nuggetmc.ai.PlayerAI;
import net.nuggetmc.ai.bot.agent.BotAgent;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import java.beans.Statement;
2021-07-01 01:12:18 -05:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
2021-06-30 02:18:31 -05:00
public class Debugger {
2021-07-01 01:12:18 -05:00
private static final String PREFIX = ChatColor.YELLOW + "[DEBUG] " + ChatColor.RESET;
2021-06-30 02:18:31 -05:00
private CommandSender sender;
public Debugger(CommandSender sender) {
this.sender = sender;
}
2021-07-01 01:12:18 -05:00
private void print(Object... objects) {
String[] values = Arrays.stream(objects).map(Object::toString).toArray(String[]::new);
sender.sendMessage(PREFIX + String.join(" ", values));
2021-06-30 02:18:31 -05:00
}
public void execute(String cmd) {
try {
2021-07-01 01:12:18 -05:00
int[] pts = {cmd.indexOf('('), cmd.indexOf(')')};
if (pts[0] == -1 || pts[1] == -1) throw new IllegalArgumentException();
String name = cmd.substring(0, pts[0]);
String content = cmd.substring(pts[0] + 1, pts[1]);
Statement statement = new Statement(this, name, new Object[]{content});
print("Running the expression \"" + ChatColor.AQUA + cmd + ChatColor.RESET + "\"...");
2021-06-30 02:18:31 -05:00
statement.execute();
}
catch (Exception e) {
2021-07-01 01:12:18 -05:00
print("Error: the expression \"" + ChatColor.AQUA + cmd + ChatColor.RESET + "\" failed to execute.");
print(e.toString());
2021-06-30 02:18:31 -05:00
}
}
2021-07-01 01:12:18 -05:00
public Object[] buildObjects(String content) {
List<Object> list = new ArrayList<>();
if (!content.isEmpty()) {
String[] values = content.split(",");
for (String str : values) {
list.add(str.startsWith(" ") ? str.substring(1) : str);
}
}
return list.toArray();
}
public void printObj(String content) {
if (content.isEmpty()) {
print("null");
return;
}
Arrays.stream(buildObjects(content)).forEach(this::print);
}
public void toggleAgent(String content) {
2021-06-30 02:18:31 -05:00
BotAgent agent = PlayerAI.getInstance().getManager().getAgent();
boolean b = agent.isEnabled();
agent.setEnabled(!b);
2021-07-01 01:12:18 -05:00
print("The Bot Agent is now "
2021-06-30 02:18:31 -05:00
+ (b ? ChatColor.RED + "DISABLED" : ChatColor.GREEN + "ENABLED")
+ ChatColor.RESET + ".");
}
}