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

361 lines
12 KiB
Java
Raw Normal View History

2021-06-30 02:18:31 -05:00
package net.nuggetmc.ai.utils;
2021-07-24 23:34:07 -05:00
import net.minecraft.server.v1_16_R3.EntityLiving;
2021-07-21 13:52:21 -05:00
import net.nuggetmc.ai.TerminatorPlus;
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.LegacyAgent;
2021-08-21 13:36:10 -05:00
import net.nuggetmc.ai.bot.agent.legacyagent.ai.IntelligenceAgent;
import net.nuggetmc.ai.command.commands.AICommand;
2021-07-21 13:52:21 -05:00
import org.bukkit.*;
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;
2021-07-24 23:34:07 -05:00
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.ServerOperator;
2021-07-21 13:52:21 -05:00
import org.bukkit.util.Vector;
2021-06-30 02:18:31 -05:00
import java.beans.Statement;
2021-07-23 17:25:10 -05:00
import java.util.*;
2021-07-24 23:34:07 -05:00
import java.util.stream.Collectors;
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
*/
2021-08-21 13:36:10 -05:00
public void colorTest() {
Player player = (Player) sender;
Location loc = player.getLocation();
String[] skin = MojangAPI.getSkin("Kubepig");
TerminatorPlus plugin = TerminatorPlus.getInstance();
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
for (int n = 1; n <= 40; n++) {
int wait = (int) (Math.pow(1.05, 130 - n) + 100);
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
e.printStackTrace();
}
Bukkit.getScheduler().runTask(plugin, () -> Bot.createBot(PlayerUtils.findBottom(loc.clone().add(Math.random() * 20 - 10, 0, Math.random() * 20 - 10)), ChatColor.GREEN + "-$26.95", skin));
player.playSound(player.getLocation(), Sound.ENTITY_ITEM_PICKUP, 1, 1);
}
});
}
public void tpall() {
Player player = (Player) sender;
TerminatorPlus.getInstance().getManager().fetch().stream().filter(EntityLiving::isAlive).forEach(bot -> bot.getBukkitEntity().teleport(player));
}
public void viewsession() {
IntelligenceAgent session = ((AICommand) TerminatorPlus.getInstance().getHandler().getCommand("ai")).getSession();
Bukkit.getOnlinePlayers().stream().filter(ServerOperator::isOp).forEach(session::addUser);
}
2021-07-23 17:25:10 -05:00
public void block() {
TerminatorPlus.getInstance().getManager().fetch().forEach(bot -> bot.block(10, 10));
}
public void offsets(boolean b) {
Agent agent = TerminatorPlus.getInstance().getManager().getAgent();
if (!(agent instanceof LegacyAgent)) {
print("This method currently only supports " + ChatColor.AQUA + "LegacyAgent" + ChatColor.RESET + ".");
return;
}
LegacyAgent legacyAgent = (LegacyAgent) agent;
legacyAgent.offsets = b;
print("Bot target offsets are now "
+ (legacyAgent.offsets ? ChatColor.GREEN + "ENABLED" : ChatColor.RED + "DISABLED")
+ ChatColor.RESET + ".");
}
2021-07-21 13:52:21 -05:00
public void confuse(int n) {
if (!(sender instanceof Player)) return;
Player player = (Player) sender;
Location loc = player.getLocation();
double f = n < 100 ? .004 * n : .4;
for (int i = 0; i < n; i++) {
Player target = Bukkit.getOnlinePlayers().stream().skip((int) (Bukkit.getOnlinePlayers().size() * Math.random())).findFirst().orElse(null);
String name = target == null ? "Steve" : target.getName();
Bot bot = Bot.createBot(loc, name);
bot.setVelocity(new Vector(Math.random() - 0.5, 0.5, Math.random() - 0.5).normalize().multiply(f));
bot.faceLocation(bot.getLocation().add(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5));
}
player.getWorld().spawnParticle(Particle.CLOUD, loc, 100, 1, 1, 1, 0.5);
}
public void dreamsmp() {
2021-07-23 17:25:10 -05:00
spawnBots(Arrays.asList(
2021-07-21 13:52:21 -05:00
"Dream", "GeorgeNotFound", "Callahan", "Sapnap", "awesamdude", "Ponk", "BadBoyHalo", "TommyInnit", "Tubbo_", "ItsFundy", "Punz",
"Purpled", "WilburSoot", "Jschlatt", "Skeppy", "The_Eret", "JackManifoldTV", "Nihachu", "Quackity", "KarlJacobs", "HBomb94",
"Technoblade", "Antfrost", "Ph1LzA", "ConnorEatsPants", "CaptainPuffy", "Vikkstar123", "LazarCodeLazar", "Ranboo", "FoolishG",
"hannahxxrose", "Slimecicle", "Michaelmcchill"
2021-07-23 17:25:10 -05:00
));
}
private void spawnBots(List<String> players) {
if (!(sender instanceof Player)) return;
2021-07-21 13:52:21 -05:00
2021-07-23 17:25:10 -05:00
print("Processing request asynchronously...");
2021-07-21 13:52:21 -05:00
2021-07-23 17:25:10 -05:00
Bukkit.getScheduler().runTaskAsynchronously(TerminatorPlus.getInstance(), () -> {
try {
print("Fetching skin data from the Mojang API for:");
2021-07-21 13:52:21 -05:00
2021-07-23 17:25:10 -05:00
Player player = (Player) sender;
Location loc = player.getLocation();
Collections.shuffle(players);
Map<String, String[]> skinCache = new HashMap<>();
int size = players.size();
int i = 1;
for (String name : players) {
print(name, ChatColor.GRAY + "(" + ChatColor.GREEN + i + ChatColor.GRAY + "/" + size + ")");
String[] skin = MojangAPI.getSkin(name);
skinCache.put(name, skin);
i++;
}
print("Creating bots...");
double f = .004 * players.size();
Bukkit.getScheduler().runTask(TerminatorPlus.getInstance(), () -> {
skinCache.forEach((name, skin) -> {
Bot bot = Bot.createBot(loc, name, skin);
bot.setVelocity(new Vector(Math.random() - 0.5, 0.5, Math.random() - 0.5).normalize().multiply(f));
bot.faceLocation(bot.getLocation().add(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5));
});
player.getWorld().spawnParticle(Particle.CLOUD, loc, 100, 1, 1, 1, 0.5);
print("Done.");
});
}
catch (Exception e) {
print(e);
}
});
2021-07-21 13:52:21 -05:00
}
public void item() {
2021-07-24 23:34:07 -05:00
TerminatorPlus.getInstance().getManager().fetch().forEach(b -> b.setDefaultItem(new ItemStack(Material.IRON_SWORD)));
2021-07-21 13:52:21 -05:00
}
public void j(boolean b) {
TerminatorPlus.getInstance().getManager().joinMessages = b;
}
public void epic(int n) {
if (!(sender instanceof Player)) return;
2021-07-23 17:25:10 -05:00
print("Fetching names asynchronously...");
2021-07-21 13:52:21 -05:00
2021-07-23 17:25:10 -05:00
List<String> players = new ArrayList<>();
2021-07-21 13:52:21 -05:00
for (int i = 0; i < n; i++) {
String name = PlayerUtils.randomName();
2021-07-23 17:25:10 -05:00
players.add(name);
print(name);
2021-07-21 13:52:21 -05:00
}
2021-07-23 17:25:10 -05:00
spawnBots(players);
2021-07-21 13:52:21 -05:00
}
2021-07-19 17:35:28 -05:00
public void tp() {
2021-07-24 23:34:07 -05:00
Bot bot = MathUtils.getRandomSetElement(TerminatorPlus.getInstance().getManager().fetch().stream().filter(EntityLiving::isAlive).collect(Collectors.toSet()));
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) {
2021-08-21 13:36:10 -05:00
print("This has been established as a feature as \"" + ChatColor.AQUA + "/bot settings setgoal" + ChatColor.RESET + "\"!");
2021-07-19 17:35:28 -05:00
}
2021-07-16 03:41:21 -05:00
2021-07-19 17:35:28 -05:00
public void fire(boolean b) {
2021-07-21 13:52:21 -05:00
TerminatorPlus.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;
2021-07-21 13:52:21 -05:00
Bukkit.getScheduler().scheduleSyncRepeatingTask(TerminatorPlus.getInstance(), () -> {
print(player.getVelocity().getY());
}, 0, 1);
}
public void hideNametags() { // this works for some reason
2021-07-21 13:52:21 -05:00
Set<Bot> bots = TerminatorPlus.getInstance().getManager().fetch();
2021-07-09 15:49:10 -05:00
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-21 13:52:21 -05:00
Set<Bot> bots = TerminatorPlus.getInstance().getManager().fetch();
2021-07-09 15:49:10 -05:00
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;
2021-07-21 13:52:21 -05:00
for (Bot bot : TerminatorPlus.getInstance().getManager().fetch()) {
2021-07-09 15:49:10 -05:00
bot.faceLocation(player.getEyeLocation());
}
}
public void toggleAgent() {
2021-07-21 13:52:21 -05:00
Agent agent = TerminatorPlus.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 + ".");
}
}