From 302b0f54830d526b5f79135b77ad0e6febf4ebaf Mon Sep 17 00:00:00 2001 From: batchprogrammer314 Date: Wed, 30 Jun 2021 02:18:31 -0500 Subject: [PATCH] Debugger added. --- src/main/java/net/nuggetmc/ai/PlayerAI.java | 2 +- src/main/java/net/nuggetmc/ai/bot/Bot.java | 11 ++--- .../java/net/nuggetmc/ai/bot/BotManager.java | 12 +++-- .../net/nuggetmc/ai/bot/agent/BotAgent.java | 26 +++++++++- .../nuggetmc/ai/commands/CommandHandler.java | 2 +- .../nuggetmc/ai/commands/CommandInstance.java | 2 +- .../ai/commands/commands/PlayerAICommand.java | 26 +++++----- .../java/net/nuggetmc/ai/utils/Debugger.java | 47 +++++++++++++++++++ 8 files changed, 98 insertions(+), 30 deletions(-) create mode 100644 src/main/java/net/nuggetmc/ai/utils/Debugger.java diff --git a/src/main/java/net/nuggetmc/ai/PlayerAI.java b/src/main/java/net/nuggetmc/ai/PlayerAI.java index 1315884..6331f4d 100644 --- a/src/main/java/net/nuggetmc/ai/PlayerAI.java +++ b/src/main/java/net/nuggetmc/ai/PlayerAI.java @@ -1,7 +1,7 @@ package net.nuggetmc.ai; -import net.nuggetmc.ai.commands.CommandHandler; import net.nuggetmc.ai.bot.BotManager; +import net.nuggetmc.ai.commands.CommandHandler; import org.bukkit.plugin.java.JavaPlugin; public class PlayerAI extends JavaPlugin { diff --git a/src/main/java/net/nuggetmc/ai/bot/Bot.java b/src/main/java/net/nuggetmc/ai/bot/Bot.java index 5083cea..8195a5b 100644 --- a/src/main/java/net/nuggetmc/ai/bot/Bot.java +++ b/src/main/java/net/nuggetmc/ai/bot/Bot.java @@ -151,7 +151,7 @@ public class Bot extends EntityPlayer { double y; - if (isOnGround()) { + if (groundTicks != 0) { velocity.setY(0); addFriction(); y = 0; @@ -289,13 +289,12 @@ public class Bot extends EntityPlayer { } private void kb(Location loc1, Location loc2) { - Vector diff = loc1.toVector().subtract(loc2.toVector()).normalize(); - diff.multiply(0.5); - diff.setY(kbUp); + Vector diff = loc1.toVector().subtract(loc2.toVector()).normalize().setY(kbUp); + Vector vel = velocity.clone().add(diff).multiply(0.5); - Vector vel = velocity.clone().add(diff); if (vel.length() > 1) vel.normalize(); - if (vel.getY() > kbUp) vel.setY(kbUp); + if (groundTicks != 0) vel.multiply(0.8).setY(0.4); + else if (vel.getY() > kbUp) vel.setY(kbUp); velocity = vel; kbTicks = 10; diff --git a/src/main/java/net/nuggetmc/ai/bot/BotManager.java b/src/main/java/net/nuggetmc/ai/bot/BotManager.java index 6031179..8556aed 100644 --- a/src/main/java/net/nuggetmc/ai/bot/BotManager.java +++ b/src/main/java/net/nuggetmc/ai/bot/BotManager.java @@ -27,6 +27,12 @@ public class BotManager implements Listener { private final Set bots = new HashSet<>(); + public BotManager(PlayerAI plugin) { + this.plugin = plugin; + this.agent = new BotAgent(this); + this.numberFormat = NumberFormat.getInstance(Locale.US); + } + public Set fetch() { return bots; } @@ -35,10 +41,8 @@ public class BotManager implements Listener { bots.add(bot); } - public BotManager(PlayerAI plugin) { - this.plugin = plugin; - this.agent = new BotAgent(this); - this.numberFormat = NumberFormat.getInstance(Locale.US); + public BotAgent getAgent() { + return agent; } public void createBots(Player sender, String name, String skin, int n) { diff --git a/src/main/java/net/nuggetmc/ai/bot/agent/BotAgent.java b/src/main/java/net/nuggetmc/ai/bot/agent/BotAgent.java index b910363..cf2a837 100644 --- a/src/main/java/net/nuggetmc/ai/bot/agent/BotAgent.java +++ b/src/main/java/net/nuggetmc/ai/bot/agent/BotAgent.java @@ -7,6 +7,7 @@ import net.nuggetmc.ai.utils.PlayerUtils; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.util.Vector; public class BotAgent { @@ -14,11 +15,32 @@ public class BotAgent { private PlayerAI plugin; private BotManager manager; + private final BukkitScheduler scheduler; + + private boolean enabled; + + private int taskID; + public BotAgent(BotManager manager) { this.plugin = PlayerAI.getInstance(); this.manager = manager; + this.scheduler = Bukkit.getScheduler(); - Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::tick, 0, 1); + setEnabled(true); + } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean b) { + enabled = b; + + if (b) { + taskID = scheduler.scheduleSyncRepeatingTask(plugin, this::tick, 0, 1); + } else { + scheduler.cancelTask(taskID); + } } private void tick() { @@ -61,7 +83,7 @@ public class BotAgent { vel.checkFinite(); vel.add(bot.velocity); } catch (IllegalArgumentException e) { - vel = bot.velocity; + vel = new Vector(0, 0.5, 0); } if (vel.length() > 1) vel.normalize(); diff --git a/src/main/java/net/nuggetmc/ai/commands/CommandHandler.java b/src/main/java/net/nuggetmc/ai/commands/CommandHandler.java index 4e9b483..fef8f25 100644 --- a/src/main/java/net/nuggetmc/ai/commands/CommandHandler.java +++ b/src/main/java/net/nuggetmc/ai/commands/CommandHandler.java @@ -18,7 +18,7 @@ public class CommandHandler { public CommandHandler(PlayerAI plugin) { drink = (DrinkCommandService) Drink.get(plugin); - drink.register(new PlayerAICommand(this), "bot", "playerai", "pai", "ai", "npc", "k"); + drink.register(new PlayerAICommand(this), "bot", "playerai", "pai", "ai", "npc"); drink.registerCommands(); help = new HashMap<>(); diff --git a/src/main/java/net/nuggetmc/ai/commands/CommandInstance.java b/src/main/java/net/nuggetmc/ai/commands/CommandInstance.java index d171c77..8533820 100644 --- a/src/main/java/net/nuggetmc/ai/commands/CommandInstance.java +++ b/src/main/java/net/nuggetmc/ai/commands/CommandInstance.java @@ -2,7 +2,7 @@ package net.nuggetmc.ai.commands; public abstract class CommandInstance { - private final CommandHandler commandHandler; + protected final CommandHandler commandHandler; public CommandInstance(CommandHandler commandHandler) { this.commandHandler = commandHandler; diff --git a/src/main/java/net/nuggetmc/ai/commands/commands/PlayerAICommand.java b/src/main/java/net/nuggetmc/ai/commands/commands/PlayerAICommand.java index f0f5d94..01830e1 100644 --- a/src/main/java/net/nuggetmc/ai/commands/commands/PlayerAICommand.java +++ b/src/main/java/net/nuggetmc/ai/commands/commands/PlayerAICommand.java @@ -5,13 +5,13 @@ import com.jonahseguin.drink.annotation.OptArg; import com.jonahseguin.drink.annotation.Require; import com.jonahseguin.drink.annotation.Sender; import net.nuggetmc.ai.PlayerAI; -import net.nuggetmc.ai.bot.Bot; import net.nuggetmc.ai.bot.BotManager; +import net.nuggetmc.ai.bot.agent.BotAgent; import net.nuggetmc.ai.commands.CommandHandler; import net.nuggetmc.ai.commands.CommandInstance; import net.nuggetmc.ai.utils.ChatUtils; +import net.nuggetmc.ai.utils.Debugger; import org.bukkit.ChatColor; -import org.bukkit.Location; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -20,12 +20,14 @@ import java.util.Locale; public class PlayerAICommand extends CommandInstance { + private PlayerAI plugin; private BotManager manager; public PlayerAICommand(CommandHandler commandHandler) { super(commandHandler); - this.manager = PlayerAI.getInstance().getManager(); + this.plugin = PlayerAI.getInstance(); + this.manager = plugin.getManager(); } @Command(name = "", desc = "The PlayerAI main command.") @@ -34,7 +36,7 @@ public class PlayerAICommand extends CommandInstance { sender.sendMessage(ChatUtils.LINE); sender.sendMessage(ChatColor.GOLD + "PlayerAI" + ChatColor.GRAY + " [" + ChatColor.RED + "v" + PlayerAI.VERSION + ChatColor.GRAY + "]"); - for (String line : getCommandHandler().getHelp(getClass())) { + for (String line : commandHandler.getHelp(getClass())) { sender.sendMessage(line); } @@ -53,22 +55,16 @@ public class PlayerAICommand extends CommandInstance { manager.createBots(sender, name, skin, n); } - @Command(name = "debug", desc = "Debug bot stats.") + @Command(name = "debug", desc = "Debug plugin code.", usage = "") @Require("playerai.manage") - public void debugCommand(@Sender Player sender) { - // This will be used for miscellaneous code for testing as the plugin is worked on - - Location loc = sender.getLocation(); - - for (Bot bot : PlayerAI.getInstance().getManager().fetch()) { - bot.faceLocation(loc); - } + public void debugCommand(@Sender CommandSender sender, String cmd) { + (new Debugger(sender)).execute(cmd); } @Command(name = "info", desc = "Information about loaded bots.") @Require("playerai.manage") - public void infoCommand(@Sender Player player) { - // This will be the future GUI where players can view information about every loaded bot + public void infoCommand(@Sender Player sender) { + sender.sendMessage("Bot GUI coming soon!"); } @Command(name = "reset", desc = "Remove all loaded bots.") diff --git a/src/main/java/net/nuggetmc/ai/utils/Debugger.java b/src/main/java/net/nuggetmc/ai/utils/Debugger.java new file mode 100644 index 0000000..b097428 --- /dev/null +++ b/src/main/java/net/nuggetmc/ai/utils/Debugger.java @@ -0,0 +1,47 @@ +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; + +public class Debugger { + + 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); + } + + 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 + "\"..."); + statement.execute(); + } + + catch (Exception e) { + print(prefix + "Error: the expression \"" + ChatColor.AQUA + cmd + ChatColor.RESET + "\" failed to execute."); + print(prefix + e.toString()); + } + } + + public void toggleAgent() { + BotAgent agent = PlayerAI.getInstance().getManager().getAgent(); + + boolean b = agent.isEnabled(); + agent.setEnabled(!b); + + print(prefix + "The Bot Agent is now " + + (b ? ChatColor.RED + "DISABLED" : ChatColor.GREEN + "ENABLED") + + ChatColor.RESET + "."); + } +}