Debugger added.
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -27,6 +27,12 @@ public class BotManager implements Listener {
|
||||
|
||||
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() {
|
||||
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) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<>();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 = "<expression>")
|
||||
@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.")
|
||||
|
||||
47
src/main/java/net/nuggetmc/ai/utils/Debugger.java
Normal file
47
src/main/java/net/nuggetmc/ai/utils/Debugger.java
Normal 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 + ".");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user