optimizations
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
package net.nuggetmc.ai;
|
||||
|
||||
import net.nuggetmc.ai.bot.BotManager;
|
||||
import net.nuggetmc.ai.commands.CommandHandler;
|
||||
import net.nuggetmc.ai.command.CommandHandler;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class PlayerAI extends JavaPlugin {
|
||||
|
||||
public static final double VERSION = 1.0;
|
||||
public static final double VERSION = 3.0;
|
||||
|
||||
private static PlayerAI instance;
|
||||
|
||||
|
||||
@@ -171,6 +171,10 @@ public class Bot extends EntityPlayer {
|
||||
}
|
||||
}
|
||||
|
||||
public void jump() {
|
||||
jump(new Vector(0, 0.5, 0));
|
||||
}
|
||||
|
||||
public void attack(org.bukkit.entity.Entity entity) {
|
||||
faceLocation(entity.getLocation());
|
||||
punch();
|
||||
|
||||
@@ -10,6 +10,8 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class BotAgent {
|
||||
|
||||
private PlayerAI plugin;
|
||||
@@ -21,6 +23,8 @@ public class BotAgent {
|
||||
|
||||
private int taskID;
|
||||
|
||||
private int count;
|
||||
|
||||
public BotAgent(BotManager manager) {
|
||||
this.plugin = PlayerAI.getInstance();
|
||||
this.manager = manager;
|
||||
@@ -44,24 +48,28 @@ public class BotAgent {
|
||||
}
|
||||
|
||||
private void tick() {
|
||||
manager.fetch().forEach(this::tickBot);
|
||||
Set<Bot> bots = manager.fetch();
|
||||
count = bots.size();
|
||||
bots.forEach(this::tickBot);
|
||||
}
|
||||
|
||||
private void tickBot(Bot bot) {
|
||||
Location loc = bot.getLocation();
|
||||
|
||||
// if bot.hasHoldState() return; << This will be to check if a bot is mining or something similar where it can't move
|
||||
|
||||
Player player = nearestPlayer(loc);
|
||||
if (player == null) return;
|
||||
|
||||
Location target = player.getLocation();
|
||||
|
||||
if (manager.fetch().size() > 1) {
|
||||
target.add(bot.getOffset());
|
||||
}
|
||||
if (count > 1) target.add(bot.getOffset());
|
||||
|
||||
// Make the XZ offsets stored in the bot object (so they don't form a straight line),
|
||||
// and make it so when mining and stuff, the offset is not taken into account
|
||||
|
||||
// if checkVertical(bot) { break block action add; return; }
|
||||
|
||||
if (bot.tickDelay(3)) attack(bot, player, loc);
|
||||
move(bot, player, loc, target);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package net.nuggetmc.ai.commands;
|
||||
package net.nuggetmc.ai.command;
|
||||
|
||||
import com.jonahseguin.drink.Drink;
|
||||
import com.jonahseguin.drink.annotation.Command;
|
||||
import com.jonahseguin.drink.command.DrinkCommandService;
|
||||
import com.jonahseguin.drink.utils.ChatUtils;
|
||||
import net.nuggetmc.ai.PlayerAI;
|
||||
import net.nuggetmc.ai.commands.commands.PlayerAICommand;
|
||||
import net.nuggetmc.ai.command.commands.PlayerAICommand;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
@@ -22,38 +23,34 @@ public class CommandHandler {
|
||||
drink.registerCommands();
|
||||
|
||||
help = new HashMap<>();
|
||||
|
||||
setHelps(new Class[] {
|
||||
PlayerAICommand.class
|
||||
});
|
||||
setHelps(PlayerAICommand.class);
|
||||
}
|
||||
|
||||
private void setHelps(Class<? extends CommandInstance>[] cls) {
|
||||
for (Class<? extends CommandInstance> clazz : cls) {
|
||||
help.put(clazz, getUsage(clazz));
|
||||
}
|
||||
@SafeVarargs
|
||||
private final void setHelps(Class<? extends CommandInstance>... cls) {
|
||||
Arrays.stream(cls).forEach(c -> help.put(c, getUsage(c)));
|
||||
}
|
||||
|
||||
public List<String> getHelp(Class<? extends CommandInstance> clazz) {
|
||||
return help.get(clazz);
|
||||
public List<String> getHelp(Class<? extends CommandInstance> cls) {
|
||||
return help.get(cls);
|
||||
}
|
||||
|
||||
private List<String> getUsage(Class<? extends CommandInstance> clazz) {
|
||||
String rootName = getRootName(clazz);
|
||||
private List<String> getUsage(Class<? extends CommandInstance> cls) {
|
||||
String rootName = getRootName(cls);
|
||||
|
||||
return getSubCommands(clazz).stream().map(c -> {
|
||||
return getSubCommands(cls).stream().map(c -> {
|
||||
Command command = c.getAnnotation(Command.class);
|
||||
return ChatColor.GRAY + " ▪ " + ChatColor.YELLOW + "/" + rootName + " " + command.name() + ChatColor.GRAY + " ▪ "
|
||||
return ChatUtils.BULLET_FORMATTED + ChatColor.YELLOW + "/" + rootName + " " + command.name() + ChatUtils.BULLET_FORMATTED
|
||||
+ ChatColor.RESET + command.desc();
|
||||
}).sorted().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private String getRootName(Class<? extends CommandInstance> clazz) {
|
||||
private String getRootName(Class<? extends CommandInstance> cls) {
|
||||
return drink.getCommands().entrySet().stream()
|
||||
.filter(c -> c.getValue().getObject().getClass().isAssignableFrom(clazz)).map(Map.Entry::getKey).findFirst().orElse(null);
|
||||
.filter(c -> c.getValue().getObject().getClass().isAssignableFrom(cls)).map(Map.Entry::getKey).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
private List<Method> getSubCommands(Class<? extends CommandInstance> clazz) {
|
||||
return Arrays.stream(clazz.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(Command.class) && !m.getAnnotation(Command.class).name().isEmpty()).collect(Collectors.toList());
|
||||
private List<Method> getSubCommands(Class<? extends CommandInstance> cls) {
|
||||
return Arrays.stream(cls.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(Command.class) && !m.getAnnotation(Command.class).name().isEmpty()).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package net.nuggetmc.ai.commands;
|
||||
package net.nuggetmc.ai.command;
|
||||
|
||||
public abstract class CommandInstance {
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
package net.nuggetmc.ai.commands.commands;
|
||||
package net.nuggetmc.ai.command.commands;
|
||||
|
||||
import com.jonahseguin.drink.annotation.Command;
|
||||
import com.jonahseguin.drink.annotation.OptArg;
|
||||
import com.jonahseguin.drink.annotation.Require;
|
||||
import com.jonahseguin.drink.annotation.Sender;
|
||||
import com.jonahseguin.drink.annotation.*;
|
||||
import com.jonahseguin.drink.utils.ChatUtils;
|
||||
import net.nuggetmc.ai.PlayerAI;
|
||||
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.command.CommandHandler;
|
||||
import net.nuggetmc.ai.command.CommandInstance;
|
||||
import net.nuggetmc.ai.utils.Debugger;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -35,11 +31,7 @@ public class PlayerAICommand extends CommandInstance {
|
||||
public void rootCommand(@Sender Player sender) {
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
sender.sendMessage(ChatColor.GOLD + "PlayerAI" + ChatColor.GRAY + " [" + ChatColor.RED + "v" + PlayerAI.VERSION + ChatColor.GRAY + "]");
|
||||
|
||||
for (String line : commandHandler.getHelp(getClass())) {
|
||||
sender.sendMessage(line);
|
||||
}
|
||||
|
||||
commandHandler.getHelp(getClass()).forEach(sender::sendMessage);
|
||||
sender.sendMessage(ChatUtils.LINE);
|
||||
}
|
||||
|
||||
@@ -57,8 +49,8 @@ public class PlayerAICommand extends CommandInstance {
|
||||
|
||||
@Command(name = "debug", desc = "Debug plugin code.", usage = "<expression>")
|
||||
@Require("playerai.manage")
|
||||
public void debugCommand(@Sender CommandSender sender, String cmd) {
|
||||
(new Debugger(sender)).execute(cmd);
|
||||
public void debugCommand(@Sender CommandSender sender, @Text String cmd) {
|
||||
new Debugger(sender).execute(cmd);
|
||||
}
|
||||
|
||||
@Command(name = "info", desc = "Information about loaded bots.")
|
||||
@@ -1,7 +0,0 @@
|
||||
package net.nuggetmc.ai.utils;
|
||||
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
|
||||
public class ChatUtils {
|
||||
public static String LINE = ChatColor.GRAY + "------------------------------------------------";
|
||||
}
|
||||
@@ -6,41 +6,74 @@ import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.beans.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Debugger {
|
||||
|
||||
private static final String PREFIX = ChatColor.YELLOW + "[DEBUG] " + ChatColor.RESET;
|
||||
|
||||
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);
|
||||
private void print(Object... objects) {
|
||||
String[] values = Arrays.stream(objects).map(Object::toString).toArray(String[]::new);
|
||||
sender.sendMessage(PREFIX + String.join(" ", values));
|
||||
}
|
||||
|
||||
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 + "\"...");
|
||||
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]);
|
||||
|
||||
Statement statement = new Statement(this, name, new Object[]{content});
|
||||
print("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());
|
||||
print("Error: the expression \"" + ChatColor.AQUA + cmd + ChatColor.RESET + "\" failed to execute.");
|
||||
print(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public void toggleAgent() {
|
||||
public Object[] buildObjects(String content) {
|
||||
List<Object> list = new ArrayList<>();
|
||||
|
||||
if (!content.isEmpty()) {
|
||||
String[] values = content.split(",");
|
||||
|
||||
for (String str : values) {
|
||||
list.add(str.startsWith(" ") ? str.substring(1) : str);
|
||||
}
|
||||
}
|
||||
|
||||
return list.toArray();
|
||||
}
|
||||
|
||||
public void printObj(String content) {
|
||||
if (content.isEmpty()) {
|
||||
print("null");
|
||||
return;
|
||||
}
|
||||
|
||||
Arrays.stream(buildObjects(content)).forEach(this::print);
|
||||
}
|
||||
|
||||
public void toggleAgent(String content) {
|
||||
BotAgent agent = PlayerAI.getInstance().getManager().getAgent();
|
||||
|
||||
boolean b = agent.isEnabled();
|
||||
agent.setEnabled(!b);
|
||||
|
||||
print(prefix + "The Bot Agent is now "
|
||||
print("The Bot Agent is now "
|
||||
+ (b ? ChatColor.RED + "DISABLED" : ChatColor.GREEN + "ENABLED")
|
||||
+ ChatColor.RESET + ".");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user