optimizations

This commit is contained in:
batchprogrammer314
2021-07-01 01:12:18 -05:00
parent 302b0f5483
commit e9aa334aee
9 changed files with 88 additions and 61 deletions

View File

@@ -6,41 +6,74 @@ import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import java.beans.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Debugger {
private static final String PREFIX = ChatColor.YELLOW + "[DEBUG] " + ChatColor.RESET;
private CommandSender sender;
private String prefix;
public Debugger(CommandSender sender) {
this.sender = sender;
this.prefix = ChatColor.YELLOW + "[DEBUG] " + ChatColor.RESET;
}
private void print(String msg) {
sender.sendMessage(msg);
private void print(Object... objects) {
String[] values = Arrays.stream(objects).map(Object::toString).toArray(String[]::new);
sender.sendMessage(PREFIX + String.join(" ", values));
}
public void execute(String cmd) {
try {
Statement statement = new Statement(this, cmd.replace("()", ""), new Object[]{});
print(prefix + "Running the expression \"" + ChatColor.AQUA + cmd + ChatColor.RESET + "\"...");
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 + "\"...");
statement.execute();
}
catch (Exception e) {
print(prefix + "Error: the expression \"" + ChatColor.AQUA + cmd + ChatColor.RESET + "\" failed to execute.");
print(prefix + e.toString());
print("Error: the expression \"" + ChatColor.AQUA + cmd + ChatColor.RESET + "\" failed to execute.");
print(e.toString());
}
}
public void toggleAgent() {
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) {
BotAgent agent = PlayerAI.getInstance().getManager().getAgent();
boolean b = agent.isEnabled();
agent.setEnabled(!b);
print(prefix + "The Bot Agent is now "
print("The Bot Agent is now "
+ (b ? ChatColor.RED + "DISABLED" : ChatColor.GREEN + "ENABLED")
+ ChatColor.RESET + ".");
}