optimizations
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
|||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>me.horsenuggets</groupId>
|
<groupId>net.nuggetmc</groupId>
|
||||||
<artifactId>PlayerAI</artifactId>
|
<artifactId>PlayerAI</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<name>PlayerAI</name>
|
<name>PlayerAI</name>
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
package net.nuggetmc.ai;
|
package net.nuggetmc.ai;
|
||||||
|
|
||||||
import net.nuggetmc.ai.bot.BotManager;
|
import net.nuggetmc.ai.bot.BotManager;
|
||||||
import net.nuggetmc.ai.commands.CommandHandler;
|
import net.nuggetmc.ai.command.CommandHandler;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
public class PlayerAI extends JavaPlugin {
|
public class PlayerAI extends JavaPlugin {
|
||||||
|
|
||||||
public static final double VERSION = 1.0;
|
public static final double VERSION = 3.0;
|
||||||
|
|
||||||
private static PlayerAI instance;
|
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) {
|
public void attack(org.bukkit.entity.Entity entity) {
|
||||||
faceLocation(entity.getLocation());
|
faceLocation(entity.getLocation());
|
||||||
punch();
|
punch();
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import org.bukkit.entity.Player;
|
|||||||
import org.bukkit.scheduler.BukkitScheduler;
|
import org.bukkit.scheduler.BukkitScheduler;
|
||||||
import org.bukkit.util.Vector;
|
import org.bukkit.util.Vector;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class BotAgent {
|
public class BotAgent {
|
||||||
|
|
||||||
private PlayerAI plugin;
|
private PlayerAI plugin;
|
||||||
@@ -21,6 +23,8 @@ public class BotAgent {
|
|||||||
|
|
||||||
private int taskID;
|
private int taskID;
|
||||||
|
|
||||||
|
private int count;
|
||||||
|
|
||||||
public BotAgent(BotManager manager) {
|
public BotAgent(BotManager manager) {
|
||||||
this.plugin = PlayerAI.getInstance();
|
this.plugin = PlayerAI.getInstance();
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
@@ -44,24 +48,28 @@ public class BotAgent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void tick() {
|
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) {
|
private void tickBot(Bot bot) {
|
||||||
Location loc = bot.getLocation();
|
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);
|
Player player = nearestPlayer(loc);
|
||||||
if (player == null) return;
|
if (player == null) return;
|
||||||
|
|
||||||
Location target = player.getLocation();
|
Location target = player.getLocation();
|
||||||
|
|
||||||
if (manager.fetch().size() > 1) {
|
if (count > 1) target.add(bot.getOffset());
|
||||||
target.add(bot.getOffset());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make the XZ offsets stored in the bot object (so they don't form a straight line),
|
// 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
|
// 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);
|
if (bot.tickDelay(3)) attack(bot, player, loc);
|
||||||
move(bot, player, loc, target);
|
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.Drink;
|
||||||
import com.jonahseguin.drink.annotation.Command;
|
import com.jonahseguin.drink.annotation.Command;
|
||||||
import com.jonahseguin.drink.command.DrinkCommandService;
|
import com.jonahseguin.drink.command.DrinkCommandService;
|
||||||
|
import com.jonahseguin.drink.utils.ChatUtils;
|
||||||
import net.nuggetmc.ai.PlayerAI;
|
import net.nuggetmc.ai.PlayerAI;
|
||||||
import net.nuggetmc.ai.commands.commands.PlayerAICommand;
|
import net.nuggetmc.ai.command.commands.PlayerAICommand;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
@@ -22,38 +23,34 @@ public class CommandHandler {
|
|||||||
drink.registerCommands();
|
drink.registerCommands();
|
||||||
|
|
||||||
help = new HashMap<>();
|
help = new HashMap<>();
|
||||||
|
setHelps(PlayerAICommand.class);
|
||||||
setHelps(new Class[] {
|
|
||||||
PlayerAICommand.class
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setHelps(Class<? extends CommandInstance>[] cls) {
|
@SafeVarargs
|
||||||
for (Class<? extends CommandInstance> clazz : cls) {
|
private final void setHelps(Class<? extends CommandInstance>... cls) {
|
||||||
help.put(clazz, getUsage(clazz));
|
Arrays.stream(cls).forEach(c -> help.put(c, getUsage(c)));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getHelp(Class<? extends CommandInstance> clazz) {
|
public List<String> getHelp(Class<? extends CommandInstance> cls) {
|
||||||
return help.get(clazz);
|
return help.get(cls);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> getUsage(Class<? extends CommandInstance> clazz) {
|
private List<String> getUsage(Class<? extends CommandInstance> cls) {
|
||||||
String rootName = getRootName(clazz);
|
String rootName = getRootName(cls);
|
||||||
|
|
||||||
return getSubCommands(clazz).stream().map(c -> {
|
return getSubCommands(cls).stream().map(c -> {
|
||||||
Command command = c.getAnnotation(Command.class);
|
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();
|
+ ChatColor.RESET + command.desc();
|
||||||
}).sorted().collect(Collectors.toList());
|
}).sorted().collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getRootName(Class<? extends CommandInstance> clazz) {
|
private String getRootName(Class<? extends CommandInstance> cls) {
|
||||||
return drink.getCommands().entrySet().stream()
|
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) {
|
private List<Method> getSubCommands(Class<? extends CommandInstance> cls) {
|
||||||
return Arrays.stream(clazz.getDeclaredMethods()).filter(m -> m.isAnnotationPresent(Command.class) && !m.getAnnotation(Command.class).name().isEmpty()).collect(Collectors.toList());
|
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 {
|
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.*;
|
||||||
import com.jonahseguin.drink.annotation.OptArg;
|
import com.jonahseguin.drink.utils.ChatUtils;
|
||||||
import com.jonahseguin.drink.annotation.Require;
|
|
||||||
import com.jonahseguin.drink.annotation.Sender;
|
|
||||||
import net.nuggetmc.ai.PlayerAI;
|
import net.nuggetmc.ai.PlayerAI;
|
||||||
import net.nuggetmc.ai.bot.BotManager;
|
import net.nuggetmc.ai.bot.BotManager;
|
||||||
import net.nuggetmc.ai.bot.agent.BotAgent;
|
import net.nuggetmc.ai.command.CommandHandler;
|
||||||
import net.nuggetmc.ai.commands.CommandHandler;
|
import net.nuggetmc.ai.command.CommandInstance;
|
||||||
import net.nuggetmc.ai.commands.CommandInstance;
|
|
||||||
import net.nuggetmc.ai.utils.ChatUtils;
|
|
||||||
import net.nuggetmc.ai.utils.Debugger;
|
import net.nuggetmc.ai.utils.Debugger;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@@ -35,11 +31,7 @@ public class PlayerAICommand extends CommandInstance {
|
|||||||
public void rootCommand(@Sender Player sender) {
|
public void rootCommand(@Sender Player sender) {
|
||||||
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 + "]");
|
||||||
|
commandHandler.getHelp(getClass()).forEach(sender::sendMessage);
|
||||||
for (String line : commandHandler.getHelp(getClass())) {
|
|
||||||
sender.sendMessage(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
sender.sendMessage(ChatUtils.LINE);
|
sender.sendMessage(ChatUtils.LINE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,8 +49,8 @@ public class PlayerAICommand extends CommandInstance {
|
|||||||
|
|
||||||
@Command(name = "debug", desc = "Debug plugin code.", usage = "<expression>")
|
@Command(name = "debug", desc = "Debug plugin code.", usage = "<expression>")
|
||||||
@Require("playerai.manage")
|
@Require("playerai.manage")
|
||||||
public void debugCommand(@Sender CommandSender sender, String cmd) {
|
public void debugCommand(@Sender CommandSender sender, @Text String cmd) {
|
||||||
(new Debugger(sender)).execute(cmd);
|
new Debugger(sender).execute(cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Command(name = "info", desc = "Information about loaded bots.")
|
@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 org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
import java.beans.Statement;
|
import java.beans.Statement;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Debugger {
|
public class Debugger {
|
||||||
|
|
||||||
|
private static final String PREFIX = ChatColor.YELLOW + "[DEBUG] " + ChatColor.RESET;
|
||||||
|
|
||||||
private CommandSender sender;
|
private CommandSender sender;
|
||||||
private String prefix;
|
|
||||||
|
|
||||||
public Debugger(CommandSender sender) {
|
public Debugger(CommandSender sender) {
|
||||||
this.sender = sender;
|
this.sender = sender;
|
||||||
this.prefix = ChatColor.YELLOW + "[DEBUG] " + ChatColor.RESET;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void print(String msg) {
|
private void print(Object... objects) {
|
||||||
sender.sendMessage(msg);
|
String[] values = Arrays.stream(objects).map(Object::toString).toArray(String[]::new);
|
||||||
|
sender.sendMessage(PREFIX + String.join(" ", values));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(String cmd) {
|
public void execute(String cmd) {
|
||||||
try {
|
try {
|
||||||
Statement statement = new Statement(this, cmd.replace("()", ""), new Object[]{});
|
int[] pts = {cmd.indexOf('('), cmd.indexOf(')')};
|
||||||
print(prefix + "Running the expression \"" + ChatColor.AQUA + cmd + ChatColor.RESET + "\"...");
|
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();
|
statement.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
print(prefix + "Error: the expression \"" + ChatColor.AQUA + cmd + ChatColor.RESET + "\" failed to execute.");
|
print("Error: the expression \"" + ChatColor.AQUA + cmd + ChatColor.RESET + "\" failed to execute.");
|
||||||
print(prefix + e.toString());
|
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();
|
BotAgent agent = PlayerAI.getInstance().getManager().getAgent();
|
||||||
|
|
||||||
boolean b = agent.isEnabled();
|
boolean b = agent.isEnabled();
|
||||||
agent.setEnabled(!b);
|
agent.setEnabled(!b);
|
||||||
|
|
||||||
print(prefix + "The Bot Agent is now "
|
print("The Bot Agent is now "
|
||||||
+ (b ? ChatColor.RED + "DISABLED" : ChatColor.GREEN + "ENABLED")
|
+ (b ? ChatColor.RED + "DISABLED" : ChatColor.GREEN + "ENABLED")
|
||||||
+ ChatColor.RESET + ".");
|
+ ChatColor.RESET + ".");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user