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

217 lines
6.5 KiB
Java
Raw Normal View History

2021-06-30 02:18:31 -05:00
package net.nuggetmc.ai.utils;
import net.nuggetmc.ai.PlayerAI;
2021-07-09 15:49:10 -05:00
import net.nuggetmc.ai.bot.Bot;
import net.nuggetmc.ai.bot.agent.Agent;
2021-07-19 17:35:28 -05:00
import net.nuggetmc.ai.bot.agent.legacyagent.EnumTargetGoal;
import net.nuggetmc.ai.bot.agent.legacyagent.LegacyAgent;
import org.bukkit.Bukkit;
2021-06-30 02:18:31 -05:00
import org.bukkit.ChatColor;
2021-07-09 15:49:10 -05:00
import org.bukkit.Location;
import org.bukkit.World;
2021-06-30 02:18:31 -05:00
import org.bukkit.command.CommandSender;
2021-07-09 15:49:10 -05:00
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.permissions.ServerOperator;
2021-06-30 02:18:31 -05:00
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-07-09 15:49:10 -05:00
import java.util.Set;
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-07-16 03:41:21 -05:00
private final CommandSender sender;
2021-06-30 02:18:31 -05:00
public Debugger(CommandSender sender) {
this.sender = sender;
}
public static void log(Object... objects) {
String[] values = formStringArray(objects);
Bukkit.getOnlinePlayers().stream().filter(ServerOperator::isOp).forEach(p -> p.sendMessage(PREFIX + String.join(" ", values)));
}
private static String[] formStringArray(Object[] objects) {
2021-07-16 03:41:21 -05:00
return Arrays.stream(objects).map(String::valueOf).toArray(String[]::new);
}
2021-07-01 01:12:18 -05:00
private void print(Object... objects) {
sender.sendMessage(PREFIX + String.join(" ", formStringArray(objects)));
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]);
2021-07-19 17:35:28 -05:00
Object[] args = content.isEmpty() ? null : buildObjects(content);
Statement statement = new Statement(this, name, args);
2021-07-01 01:12:18 -05:00
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) {
2021-07-19 17:35:28 -05:00
String value = str.startsWith(" ") ? str.substring(1) : str;
Object obj = value;
try {
obj = Double.parseDouble(value);
} catch (NumberFormatException ignored) { }
try {
obj = Integer.parseInt(value);
} catch (NumberFormatException ignored) { }
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
obj = Boolean.parseBoolean(value);
}
list.add(obj);
2021-07-01 01:12:18 -05:00
}
}
return list.toArray();
}
2021-07-19 17:35:28 -05:00
/*
* DEBUGGER METHODS
*/
public void tp() {
Bot bot = MathUtils.getRandomSetElement(PlayerAI.getInstance().getManager().fetch());
2021-07-16 03:41:21 -05:00
2021-07-19 17:35:28 -05:00
if (bot == null) {
print("Failed to locate a bot.");
2021-07-16 03:41:21 -05:00
return;
}
2021-07-19 17:35:28 -05:00
print("Located bot", ChatColor.GREEN + bot.getName() + ChatColor.RESET + ".");
2021-07-16 03:41:21 -05:00
2021-07-19 17:35:28 -05:00
if (sender instanceof Player) {
print("Teleporting...");
((Player) sender).teleport(bot.getLocation());
}
2021-07-16 03:41:21 -05:00
}
2021-07-19 17:35:28 -05:00
public void setTarget(int n) {
Agent agent = PlayerAI.getInstance().getManager().getAgent();
if (!(agent instanceof LegacyAgent)) {
print("This method currently only supports " + ChatColor.AQUA + "LegacyAgent" + ChatColor.RESET + ".");
2021-07-16 03:41:21 -05:00
return;
}
2021-07-19 17:35:28 -05:00
LegacyAgent legacyAgent = (LegacyAgent) agent;
EnumTargetGoal goal = EnumTargetGoal.of(n);
2021-07-16 03:41:21 -05:00
2021-07-19 17:35:28 -05:00
legacyAgent.setTargetType(goal);
2021-07-16 03:41:21 -05:00
2021-07-19 17:35:28 -05:00
print("The goal has been set to " + ChatColor.BLUE + goal.name() + ChatColor.RESET + ".");
}
2021-07-16 03:41:21 -05:00
2021-07-19 17:35:28 -05:00
public void fire(boolean b) {
PlayerAI.getInstance().getManager().fetch().forEach(bot -> bot.setOnFirePackets(b));
2021-07-16 03:41:21 -05:00
}
public void trackYVel() {
if (!(sender instanceof Player)) return;
Player player = (Player) sender;
Bukkit.getScheduler().scheduleSyncRepeatingTask(PlayerAI.getInstance(), () -> {
print(player.getVelocity().getY());
}, 0, 1);
}
public void hideNametags() { // this works for some reason
2021-07-09 15:49:10 -05:00
Set<Bot> bots = PlayerAI.getInstance().getManager().fetch();
for (Bot bot : bots) {
Location loc = bot.getLocation();
World world = loc.getWorld();
if (world == null) continue;
loc.setX(loc.getBlockX());
loc.setY(loc.getBlockY());
loc.setZ(loc.getBlockZ());
loc.add(0.5, 0.5, 0.5);
ArmorStand seat = (ArmorStand) world.spawnEntity(loc, EntityType.ARMOR_STAND);
seat.setVisible(false);
seat.setSmall(true);
bot.getBukkitEntity().setPassenger(seat);
}
}
public void sit() {
2021-07-09 15:49:10 -05:00
Set<Bot> bots = PlayerAI.getInstance().getManager().fetch();
for (Bot bot : bots) {
Location loc = bot.getLocation();
World world = loc.getWorld();
if (world == null) continue;
loc.setX(loc.getBlockX());
loc.setY(loc.getBlockY());
loc.setZ(loc.getBlockZ());
loc.add(0.5, -1.5, 0.5);
ArmorStand seat = (ArmorStand) world.spawnEntity(loc, EntityType.ARMOR_STAND);
seat.setVisible(false);
seat.setGravity(false);
seat.setSmall(true);
seat.addPassenger(bot.getBukkitEntity());
}
}
public void look() {
2021-07-09 15:49:10 -05:00
if (!(sender instanceof Player)) {
print("Unspecified player.");
return;
}
Player player = (Player) sender;
for (Bot bot : PlayerAI.getInstance().getManager().fetch()) {
bot.faceLocation(player.getEyeLocation());
}
}
public void toggleAgent() {
Agent agent = PlayerAI.getInstance().getManager().getAgent();
2021-06-30 02:18:31 -05:00
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 + ".");
}
}