Debugger added.

This commit is contained in:
batchprogrammer314
2021-06-30 02:18:31 -05:00
parent 54c7afb957
commit 302b0f5483
8 changed files with 98 additions and 30 deletions

View File

@@ -1,7 +1,7 @@
package net.nuggetmc.ai; package net.nuggetmc.ai;
import net.nuggetmc.ai.commands.CommandHandler;
import net.nuggetmc.ai.bot.BotManager; import net.nuggetmc.ai.bot.BotManager;
import net.nuggetmc.ai.commands.CommandHandler;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class PlayerAI extends JavaPlugin { public class PlayerAI extends JavaPlugin {

View File

@@ -151,7 +151,7 @@ public class Bot extends EntityPlayer {
double y; double y;
if (isOnGround()) { if (groundTicks != 0) {
velocity.setY(0); velocity.setY(0);
addFriction(); addFriction();
y = 0; y = 0;
@@ -289,13 +289,12 @@ public class Bot extends EntityPlayer {
} }
private void kb(Location loc1, Location loc2) { private void kb(Location loc1, Location loc2) {
Vector diff = loc1.toVector().subtract(loc2.toVector()).normalize(); Vector diff = loc1.toVector().subtract(loc2.toVector()).normalize().setY(kbUp);
diff.multiply(0.5); Vector vel = velocity.clone().add(diff).multiply(0.5);
diff.setY(kbUp);
Vector vel = velocity.clone().add(diff);
if (vel.length() > 1) vel.normalize(); 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; velocity = vel;
kbTicks = 10; kbTicks = 10;

View File

@@ -27,6 +27,12 @@ public class BotManager implements Listener {
private final Set<Bot> bots = new HashSet<>(); private final Set<Bot> bots = new HashSet<>();
public BotManager(PlayerAI plugin) {
this.plugin = plugin;
this.agent = new BotAgent(this);
this.numberFormat = NumberFormat.getInstance(Locale.US);
}
public Set<Bot> fetch() { public Set<Bot> fetch() {
return bots; return bots;
} }
@@ -35,10 +41,8 @@ public class BotManager implements Listener {
bots.add(bot); bots.add(bot);
} }
public BotManager(PlayerAI plugin) { public BotAgent getAgent() {
this.plugin = plugin; return agent;
this.agent = new BotAgent(this);
this.numberFormat = NumberFormat.getInstance(Locale.US);
} }
public void createBots(Player sender, String name, String skin, int n) { public void createBots(Player sender, String name, String skin, int n) {

View File

@@ -7,6 +7,7 @@ import net.nuggetmc.ai.utils.PlayerUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
public class BotAgent { public class BotAgent {
@@ -14,11 +15,32 @@ public class BotAgent {
private PlayerAI plugin; private PlayerAI plugin;
private BotManager manager; private BotManager manager;
private final BukkitScheduler scheduler;
private boolean enabled;
private int taskID;
public BotAgent(BotManager manager) { public BotAgent(BotManager manager) {
this.plugin = PlayerAI.getInstance(); this.plugin = PlayerAI.getInstance();
this.manager = manager; 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() { private void tick() {
@@ -61,7 +83,7 @@ public class BotAgent {
vel.checkFinite(); vel.checkFinite();
vel.add(bot.velocity); vel.add(bot.velocity);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
vel = bot.velocity; vel = new Vector(0, 0.5, 0);
} }
if (vel.length() > 1) vel.normalize(); if (vel.length() > 1) vel.normalize();

View File

@@ -18,7 +18,7 @@ public class CommandHandler {
public CommandHandler(PlayerAI plugin) { public CommandHandler(PlayerAI plugin) {
drink = (DrinkCommandService) Drink.get(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(); drink.registerCommands();
help = new HashMap<>(); help = new HashMap<>();

View File

@@ -2,7 +2,7 @@ package net.nuggetmc.ai.commands;
public abstract class CommandInstance { public abstract class CommandInstance {
private final CommandHandler commandHandler; protected final CommandHandler commandHandler;
public CommandInstance(CommandHandler commandHandler) { public CommandInstance(CommandHandler commandHandler) {
this.commandHandler = commandHandler; this.commandHandler = commandHandler;

View File

@@ -5,13 +5,13 @@ import com.jonahseguin.drink.annotation.OptArg;
import com.jonahseguin.drink.annotation.Require; import com.jonahseguin.drink.annotation.Require;
import com.jonahseguin.drink.annotation.Sender; import com.jonahseguin.drink.annotation.Sender;
import net.nuggetmc.ai.PlayerAI; import net.nuggetmc.ai.PlayerAI;
import net.nuggetmc.ai.bot.Bot;
import net.nuggetmc.ai.bot.BotManager; import net.nuggetmc.ai.bot.BotManager;
import net.nuggetmc.ai.bot.agent.BotAgent;
import net.nuggetmc.ai.commands.CommandHandler; import net.nuggetmc.ai.commands.CommandHandler;
import net.nuggetmc.ai.commands.CommandInstance; import net.nuggetmc.ai.commands.CommandInstance;
import net.nuggetmc.ai.utils.ChatUtils; import net.nuggetmc.ai.utils.ChatUtils;
import net.nuggetmc.ai.utils.Debugger;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -20,12 +20,14 @@ import java.util.Locale;
public class PlayerAICommand extends CommandInstance { public class PlayerAICommand extends CommandInstance {
private PlayerAI plugin;
private BotManager manager; private BotManager manager;
public PlayerAICommand(CommandHandler commandHandler) { public PlayerAICommand(CommandHandler commandHandler) {
super(commandHandler); super(commandHandler);
this.manager = PlayerAI.getInstance().getManager(); this.plugin = PlayerAI.getInstance();
this.manager = plugin.getManager();
} }
@Command(name = "", desc = "The PlayerAI main command.") @Command(name = "", desc = "The PlayerAI main command.")
@@ -34,7 +36,7 @@ public class PlayerAICommand extends CommandInstance {
sender.sendMessage(ChatUtils.LINE); sender.sendMessage(ChatUtils.LINE);
sender.sendMessage(ChatColor.GOLD + "PlayerAI" + ChatColor.GRAY + " [" + ChatColor.RED + "v" + PlayerAI.VERSION + ChatColor.GRAY + "]"); 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); sender.sendMessage(line);
} }
@@ -53,22 +55,16 @@ public class PlayerAICommand extends CommandInstance {
manager.createBots(sender, name, skin, n); manager.createBots(sender, name, skin, n);
} }
@Command(name = "debug", desc = "Debug bot stats.") @Command(name = "debug", desc = "Debug plugin code.", usage = "<expression>")
@Require("playerai.manage") @Require("playerai.manage")
public void debugCommand(@Sender Player sender) { public void debugCommand(@Sender CommandSender sender, String cmd) {
// This will be used for miscellaneous code for testing as the plugin is worked on (new Debugger(sender)).execute(cmd);
Location loc = sender.getLocation();
for (Bot bot : PlayerAI.getInstance().getManager().fetch()) {
bot.faceLocation(loc);
}
} }
@Command(name = "info", desc = "Information about loaded bots.") @Command(name = "info", desc = "Information about loaded bots.")
@Require("playerai.manage") @Require("playerai.manage")
public void infoCommand(@Sender Player player) { public void infoCommand(@Sender Player sender) {
// This will be the future GUI where players can view information about every loaded bot sender.sendMessage("Bot GUI coming soon!");
} }
@Command(name = "reset", desc = "Remove all loaded bots.") @Command(name = "reset", desc = "Remove all loaded bots.")

View File

@@ -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 + ".");
}
}