refactored almost everything into the API module, and left NMS stuff in plugin module, and performance improvements

This commit is contained in:
Badbird-5907
2022-06-17 19:27:24 -04:00
parent b052a04ddb
commit b6313f6622
43 changed files with 583 additions and 449 deletions

View File

@@ -0,0 +1,5 @@
package net.nuggetmc.tplus.api;
public interface AIManager {
void clearSession();
}

View File

@@ -0,0 +1,50 @@
package net.nuggetmc.tplus.api;
import net.nuggetmc.tplus.api.agent.Agent;
import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.Set;
import java.util.UUID;
public interface BotManager {
Set<Terminator> fetch();
Agent getAgent();
void add(Terminator bot);
Terminator getFirst(String name);
List<String> fetchNames();
void createBots(Player sender, String name, String skinName, int n);
void createBots(Player sender, String name, String skinName, int n, NeuralNetwork network);
Set<Terminator> createBots(Location loc, String name, String[] skin, List<NeuralNetwork> networks);
Set<Terminator> createBots(Location loc, String name, String[] skin, int n, NeuralNetwork network);
void remove(Terminator bot);
void reset();
/**
* Get a bot from a Player object
*
* @param player
* @return
* @deprecated Use {@link #getBot(UUID)} instead as this may no longer work
*/
@Deprecated
Terminator getBot(Player player);
Terminator getBot(UUID uuid);
Terminator getBot(int entityId);
}

View File

@@ -0,0 +1,13 @@
package net.nuggetmc.tplus.api;
import org.bukkit.Sound;
import org.bukkit.block.Block;
/**
* This class serves as a bridge between the API and internal code that interacts with NMS.
*/
public interface InternalBridge {
void sendBlockDestructionPacket(short entityId, int x, int y, int z, int progress);
Sound breakBlockSound(Block block);
}

View File

@@ -1,12 +1,12 @@
package net.nuggetmc.tplus.api; package net.nuggetmc.tplus.api;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import net.kyori.adventure.text.Component;
import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork; import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
@@ -15,8 +15,12 @@ public interface Terminator {
String getBotName(); String getBotName();
int getId();
GameProfile getGameProfile(); GameProfile getGameProfile();
LivingEntity getBukkitEntity();
NeuralNetwork getNeuralNetwork(); NeuralNetwork getNeuralNetwork();
void setNeuralNetwork(NeuralNetwork neuralNetwork); void setNeuralNetwork(NeuralNetwork neuralNetwork);
@@ -25,6 +29,8 @@ public interface Terminator {
Location getLocation(); Location getLocation();
boolean isAlive();
float getHealth(); float getHealth();
float getMaxHealth(); float getMaxHealth();
@@ -41,6 +47,10 @@ public interface Terminator {
boolean isInWater(); boolean isInWater();
boolean isOnGround();
void setXRot(float pitch);
void jump(Vector velocity); void jump(Vector velocity);
void jump(); void jump();
@@ -49,6 +59,8 @@ public interface Terminator {
void look(BlockFace face); void look(BlockFace face);
void faceLocation(Location location);
void attack(Entity target); void attack(Entity target);
void attemptBlockPlace(Location loc, Material type, boolean down); void attemptBlockPlace(Location loc, Material type, boolean down);
@@ -88,4 +100,8 @@ public interface Terminator {
int getAliveTicks(); int getAliveTicks();
boolean tickDelay(int ticks); boolean tickDelay(int ticks);
void renderBot(Object packetListener, boolean login);
void setOnFirePackets(boolean onFire);
} }

View File

@@ -0,0 +1,22 @@
package net.nuggetmc.tplus.api;
public class TerminatorPlusAPI {
private static InternalBridge internalBridge;
private static BotManager botManager;
public static InternalBridge getInternalBridge() {
return internalBridge;
}
public static void setInternalBridge(InternalBridge internalBridge) {
TerminatorPlusAPI.internalBridge = internalBridge;
}
public static BotManager getBotManager() {
return botManager;
}
public static void setBotManager(BotManager botManager) {
TerminatorPlusAPI.botManager = botManager;
}
}

View File

@@ -1,14 +1,14 @@
package net.nuggetmc.tplus.bot.agent; package net.nuggetmc.tplus.api.agent;
import net.nuggetmc.tplus.TerminatorPlus; import net.nuggetmc.tplus.api.BotManager;
import net.nuggetmc.tplus.bot.Bot; import net.nuggetmc.tplus.api.Terminator;
import net.nuggetmc.tplus.bot.BotManager; import net.nuggetmc.tplus.api.event.BotDamageByPlayerEvent;
import net.nuggetmc.tplus.bot.event.BotDamageByPlayerEvent; import net.nuggetmc.tplus.api.event.BotDeathEvent;
import net.nuggetmc.tplus.bot.event.BotDeathEvent; import net.nuggetmc.tplus.api.event.BotFallDamageEvent;
import net.nuggetmc.tplus.bot.event.BotFallDamageEvent; import net.nuggetmc.tplus.api.event.BotKilledByPlayerEvent;
import net.nuggetmc.tplus.bot.event.BotKilledByPlayerEvent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
@@ -18,7 +18,7 @@ import java.util.Set;
public abstract class Agent { public abstract class Agent {
protected final TerminatorPlus plugin; protected final Plugin plugin;
protected final BotManager manager; protected final BotManager manager;
protected final BukkitScheduler scheduler; protected final BukkitScheduler scheduler;
protected final Set<BukkitRunnable> taskList; protected final Set<BukkitRunnable> taskList;
@@ -29,8 +29,8 @@ public abstract class Agent {
protected boolean drops; protected boolean drops;
public Agent(BotManager manager) { public Agent(BotManager manager, Plugin plugin) {
this.plugin = TerminatorPlus.getInstance(); this.plugin = plugin;
this.manager = manager; this.manager = manager;
this.scheduler = Bukkit.getScheduler(); this.scheduler = Bukkit.getScheduler();
this.taskList = new HashSet<>(); this.taskList = new HashSet<>();
@@ -67,17 +67,20 @@ public abstract class Agent {
protected abstract void tick(); protected abstract void tick();
public void onFallDamage(BotFallDamageEvent event) { } public void onFallDamage(BotFallDamageEvent event) {
}
public void onPlayerDamage(BotDamageByPlayerEvent event) { } public void onPlayerDamage(BotDamageByPlayerEvent event) {
}
public void onBotDeath(BotDeathEvent event) { } public void onBotDeath(BotDeathEvent event) {
}
public void onBotKilledByPlayer(BotKilledByPlayerEvent event) { public void onBotKilledByPlayer(BotKilledByPlayerEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
scheduler.runTaskAsynchronously(plugin, () -> { scheduler.runTaskAsynchronously(plugin, () -> {
Bot bot = manager.getBot(player); Terminator bot = manager.getBot(player);
if (bot != null) { if (bot != null) {
bot.incrementKills(); bot.incrementKills();

View File

@@ -1,13 +1,14 @@
package net.nuggetmc.tplus.bot.agent.botagent; package net.nuggetmc.tplus.api.agent.botagent;
import net.nuggetmc.tplus.bot.Bot; import net.nuggetmc.tplus.api.BotManager;
import net.nuggetmc.tplus.bot.BotManager; import net.nuggetmc.tplus.api.Terminator;
import net.nuggetmc.tplus.bot.agent.Agent; import net.nuggetmc.tplus.api.agent.Agent;
import net.nuggetmc.tplus.utils.MathUtils; import net.nuggetmc.tplus.api.utils.MathUtils;
import net.nuggetmc.tplus.utils.PlayerUtils; import net.nuggetmc.tplus.api.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.plugin.Plugin;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import java.util.Set; import java.util.Set;
@@ -22,19 +23,19 @@ public class BotAgent extends Agent {
private int count; private int count;
public BotAgent(BotManager manager) { public BotAgent(BotManager manager, Plugin plugin) {
super(manager); super(manager, plugin);
} }
@Override @Override
protected void tick() { protected void tick() {
Set<Bot> bots = manager.fetch(); Set<Terminator> bots = manager.fetch();
count = bots.size(); count = bots.size();
bots.forEach(this::tickBot); bots.forEach(this::tickBot);
} }
// This is where the code starts to get spicy // This is where the code starts to get spicy
private void tickBot(Bot bot) { private void tickBot(Terminator bot) {
if (!bot.isAlive()) return; if (!bot.isAlive()) return;
Location loc = bot.getLocation(); Location loc = bot.getLocation();
@@ -90,13 +91,14 @@ public class BotAgent extends Agent {
if (bot.tickDelay(3)) attack(bot, player, loc); if (bot.tickDelay(3)) attack(bot, player, loc);
} }
private void attack(Bot bot, Player player, Location loc) { private void attack(Terminator bot, Player player, Location loc) {
if (PlayerUtils.isInvincible(player.getGameMode()) || player.getNoDamageTicks() >= 5 || loc.distance(player.getLocation()) >= 4) return; if (PlayerUtils.isInvincible(player.getGameMode()) || player.getNoDamageTicks() >= 5 || loc.distance(player.getLocation()) >= 4)
return;
bot.attack(player); bot.attack(player);
} }
private void move(Bot bot, Player player, Location loc, Location target) { private void move(Terminator bot, Player player, Location loc, Location target) {
Vector vel = target.toVector().subtract(loc.toVector()).normalize(); Vector vel = target.toVector().subtract(loc.toVector()).normalize();
if (bot.tickDelay(5)) bot.faceLocation(player.getLocation()); if (bot.tickDelay(5)) bot.faceLocation(player.getLocation());

View File

@@ -1,6 +1,6 @@
package net.nuggetmc.tplus.bot.agent.botagent; package net.nuggetmc.tplus.api.agent.botagent;
import net.nuggetmc.tplus.bot.Bot; import net.nuggetmc.tplus.api.Terminator;
import org.bukkit.Location; import org.bukkit.Location;
public class BotSituation { public class BotSituation {
@@ -11,7 +11,7 @@ public class BotSituation {
* aboveGround * aboveGround
*/ */
public BotSituation(Bot bot, Location target) { public BotSituation(Terminator bot, Location target) {
Location loc = bot.getLocation(); Location loc = bot.getLocation();
this.disp = VerticalDisplacement.fetch(loc.getBlockY(), target.getBlockY()); this.disp = VerticalDisplacement.fetch(loc.getBlockY(), target.getBlockY());

View File

@@ -1,4 +1,4 @@
package net.nuggetmc.tplus.bot.agent.botagent; package net.nuggetmc.tplus.api.agent.botagent;
public enum VerticalDisplacement { public enum VerticalDisplacement {
AT, AT,

View File

@@ -1,4 +1,4 @@
package net.nuggetmc.tplus.bot.agent.legacyagent; package net.nuggetmc.tplus.api.agent.legacyagent;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;

View File

@@ -1,29 +1,23 @@
package net.nuggetmc.tplus.bot.agent.legacyagent; package net.nuggetmc.tplus.api.agent.legacyagent;
import net.minecraft.core.BlockPos; import net.nuggetmc.tplus.api.BotManager;
import net.minecraft.network.protocol.game.ClientboundBlockDestructionPacket; import net.nuggetmc.tplus.api.Terminator;
import net.nuggetmc.tplus.bot.Bot; import net.nuggetmc.tplus.api.TerminatorPlusAPI;
import net.nuggetmc.tplus.bot.BotManager; import net.nuggetmc.tplus.api.agent.Agent;
import net.nuggetmc.tplus.bot.agent.Agent;
import net.nuggetmc.tplus.api.agent.legacyagent.ai.BotData; import net.nuggetmc.tplus.api.agent.legacyagent.ai.BotData;
import net.nuggetmc.tplus.api.agent.legacyagent.ai.BotNode; import net.nuggetmc.tplus.api.agent.legacyagent.ai.BotNode;
import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork; import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork;
import net.nuggetmc.tplus.bot.event.BotDamageByPlayerEvent; import net.nuggetmc.tplus.api.event.BotDamageByPlayerEvent;
import net.nuggetmc.tplus.bot.event.BotDeathEvent; import net.nuggetmc.tplus.api.event.BotDeathEvent;
import net.nuggetmc.tplus.bot.event.BotFallDamageEvent; import net.nuggetmc.tplus.api.event.BotFallDamageEvent;
import net.nuggetmc.tplus.utils.MathUtils; import net.nuggetmc.tplus.api.utils.MathUtils;
import net.nuggetmc.tplus.utils.PlayerUtils; import net.nuggetmc.tplus.api.utils.PlayerUtils;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer; import org.bukkit.entity.*;
import org.bukkit.entity.Boat;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Mob;
import org.bukkit.entity.Monster;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
@@ -31,72 +25,70 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern;
// Yes, this code is very unoptimized, I know. // Yes, this code is very unoptimized, I know.
public class LegacyAgent extends Agent { public class LegacyAgent extends Agent {
private static final Pattern NAME_PATTERN = Pattern.compile("[^A-Za-z]+");
public final Set<Terminator> noFace = new HashSet<>();
public final Set<LivingEntity> noJump = new HashSet<>();
public final Set<Terminator> slow = new HashSet<>();
private final LegacyBlockCheck blockCheck; private final LegacyBlockCheck blockCheck;
private final Map<LivingEntity, BukkitRunnable> miningAnim = new HashMap<>();
private EnumTargetGoal goal;
public boolean offsets = true;
public LegacyAgent(BotManager manager) {
super(manager);
this.goal = EnumTargetGoal.NEAREST_VULNERABLE_PLAYER;
this.blockCheck = new LegacyBlockCheck(this);
}
private final Map<Player, BukkitRunnable> miningAnim = new HashMap<>();
private final Set<Boat> boats = new HashSet<>(); private final Set<Boat> boats = new HashSet<>();
private final Map<LivingEntity, Location> btList = new HashMap<>();
private final Map<Player, Location> btList = new HashMap<>(); private final Map<LivingEntity, Boolean> btCheck = new HashMap<>();
private final Map<Player, Boolean> btCheck = new HashMap<>(); private final Map<LivingEntity, Location> towerList = new HashMap<>();
private final Map<Player, Location> towerList = new HashMap<>(); private final Set<Terminator> boatCooldown = new HashSet<>();
private final Set<Bot> boatCooldown = new HashSet<>();
private final Map<Block, Short> crackList = new HashMap<>(); private final Map<Block, Short> crackList = new HashMap<>();
private final Map<BukkitRunnable, Byte> mining = new HashMap<>(); private final Map<BukkitRunnable, Byte> mining = new HashMap<>();
private final Set<Terminator> fallDamageCooldown = new HashSet<>();
public boolean offsets = true;
private EnumTargetGoal goal;
private final Set<Bot> fallDamageCooldown = new HashSet<>(); public LegacyAgent(BotManager manager, Plugin plugin) {
super(manager, plugin);
public final Set<Bot> noFace = new HashSet<>(); this.goal = EnumTargetGoal.NEAREST_VULNERABLE_PLAYER;
public final Set<Player> noJump = new HashSet<>(); this.blockCheck = new LegacyBlockCheck(this, plugin);
}
public final Set<Bot> slow = new HashSet<>(); private static boolean checkSideBreak(Material type) {
return !LegacyMats.BREAK.contains(type);// && !LegacyMats.LEAVES.contains(type);
}
@Override @Override
protected void tick() { protected void tick() {
manager.fetch().forEach(this::tickBot); manager.fetch().forEach(this::tickBot);
} }
private void center(Bot bot) { private void center(Terminator bot) {
if (bot == null || !bot.isAlive()) { if (bot == null || !bot.isAlive()) {
return; return;
} }
final Player botPlayer = bot.getBukkitEntity(); final LivingEntity botEntity = bot.getBukkitEntity();
Location prev = null; Location prev = null;
if (btList.containsKey(botPlayer)) { if (btList.containsKey(botEntity)) {
prev = btList.get(botPlayer); prev = btList.get(botEntity);
} }
Location loc = botPlayer.getLocation(); Location loc = botEntity.getLocation();
if (prev != null) { if (prev != null) {
if (loc.getBlockX() == prev.getBlockX() && loc.getBlockZ() == prev.getBlockZ()) { if (loc.getBlockX() == prev.getBlockX() && loc.getBlockZ() == prev.getBlockZ()) {
btCheck.put(botPlayer, true); btCheck.put(botEntity, true);
} else { } else {
btCheck.put(botPlayer, false); btCheck.put(botEntity, false);
} }
} }
btList.put(botPlayer, loc); btList.put(botEntity, loc);
} }
private void tickBot(Bot bot) { private void tickBot(Terminator bot) {
if (!bot.isAlive()) { if (!bot.isAlive()) {
return; return;
} }
@@ -118,7 +110,7 @@ public class LegacyAgent extends Agent {
fallDamageCheck(bot); fallDamageCheck(bot);
miscellaneousChecks(bot, livingTarget); miscellaneousChecks(bot, livingTarget);
Player botPlayer = bot.getBukkitEntity(); LivingEntity botPlayer = bot.getBukkitEntity();
Location target = offsets ? livingTarget.getLocation().add(bot.getOffset()) : livingTarget.getLocation(); Location target = offsets ? livingTarget.getLocation().add(bot.getOffset()) : livingTarget.getLocation();
boolean ai = bot.hasNeuralNetwork(); boolean ai = bot.hasNeuralNetwork();
@@ -190,14 +182,12 @@ public class LegacyAgent extends Agent {
case 2: case 2:
if (!waterGround) move(bot, livingTarget, loc, target, ai); if (!waterGround) move(bot, livingTarget, loc, target, ai);
} }
} } else if (LegacyMats.WATER.contains(loc.getBlock().getType())) {
else if (LegacyMats.WATER.contains(loc.getBlock().getType())) {
swim(bot, target, botPlayer, livingTarget, LegacyMats.WATER.contains(loc.clone().add(0, -1, 0).getBlock().getType())); swim(bot, target, botPlayer, livingTarget, LegacyMats.WATER.contains(loc.clone().add(0, -1, 0).getBlock().getType()));
} }
} }
private void move(Bot bot, LivingEntity livingTarget, Location loc, Location target, boolean ai) { private void move(Terminator bot, LivingEntity livingTarget, Location loc, Location target, boolean ai) {
Vector position = loc.toVector(); Vector position = loc.toVector();
Vector vel = target.toVector().subtract(position).normalize(); Vector vel = target.toVector().subtract(position).normalize();
@@ -259,9 +249,7 @@ public class LegacyAgent extends Agent {
return; return;
} }
} } else {
else {
boolean left = network.check(BotNode.LEFT); boolean left = network.check(BotNode.LEFT);
boolean right = network.check(BotNode.RIGHT); boolean right = network.check(BotNode.RIGHT);
@@ -294,7 +282,7 @@ public class LegacyAgent extends Agent {
bot.jump(vel); bot.jump(vel);
} }
private void fallDamageCheck(Bot bot) { private void fallDamageCheck(Terminator bot) {
if (bot.isFalling()) { if (bot.isFalling()) {
bot.look(BlockFace.DOWN); bot.look(BlockFace.DOWN);
@@ -319,7 +307,7 @@ public class LegacyAgent extends Agent {
@Override @Override
public void onPlayerDamage(BotDamageByPlayerEvent event) { public void onPlayerDamage(BotDamageByPlayerEvent event) {
Bot bot = event.getBot(); Terminator bot = event.getBot();
Location loc = bot.getLocation(); Location loc = bot.getLocation();
Player player = event.getPlayer(); Player player = event.getPlayer();
@@ -333,7 +321,7 @@ public class LegacyAgent extends Agent {
@Override @Override
public void onFallDamage(BotFallDamageEvent event) { public void onFallDamage(BotFallDamageEvent event) {
Bot bot = event.getBot(); Terminator bot = event.getBot();
World world = bot.getBukkitEntity().getWorld(); World world = bot.getBukkitEntity().getWorld();
bot.look(BlockFace.DOWN); bot.look(BlockFace.DOWN);
@@ -380,8 +368,10 @@ public class LegacyAgent extends Agent {
} }
} }
private void swim(Bot bot, Location loc, Player playerNPC, LivingEntity target, boolean anim) { private void swim(Terminator bot, Location loc, LivingEntity playerNPC, LivingEntity target, boolean anim) {
playerNPC.setSneaking(false); if (playerNPC instanceof Player) {
((Player) playerNPC).setSneaking(false);
}
Location at = bot.getLocation(); Location at = bot.getLocation();
@@ -412,8 +402,8 @@ public class LegacyAgent extends Agent {
bot.addVelocity(vector); bot.addVelocity(vector);
} }
private void stopMining(Bot bot) { private void stopMining(Terminator bot) {
Player playerNPC = bot.getBukkitEntity(); LivingEntity playerNPC = bot.getBukkitEntity();
if (miningAnim.containsKey(playerNPC)) { if (miningAnim.containsKey(playerNPC)) {
BukkitRunnable task = miningAnim.get(playerNPC); BukkitRunnable task = miningAnim.get(playerNPC);
if (task != null) { if (task != null) {
@@ -423,7 +413,7 @@ public class LegacyAgent extends Agent {
} }
} }
private byte checkSide(Bot npc, LivingEntity target, Player playerNPC) { // make it so they don't jump when checking side private byte checkSide(Terminator npc, LivingEntity target, LivingEntity playerNPC) { // make it so they don't jump when checking side
Location a = playerNPC.getEyeLocation(); Location a = playerNPC.getEyeLocation();
Location b = target.getLocation().add(0, 1, 0); Location b = target.getLocation().add(0, 1, 0);
@@ -444,8 +434,8 @@ public class LegacyAgent extends Agent {
} }
} }
private LegacyLevel checkNearby(LivingEntity target, Bot npc) { private LegacyLevel checkNearby(LivingEntity target, Terminator npc) {
Player player = npc.getBukkitEntity(); LivingEntity player = npc.getBukkitEntity();
npc.faceLocation(target.getLocation()); npc.faceLocation(target.getLocation());
@@ -508,11 +498,7 @@ public class LegacyAgent extends Agent {
return level; return level;
} }
private static boolean checkSideBreak(Material type) { private boolean checkUp(Terminator npc, LivingEntity target, LivingEntity playerNPC, Location loc, boolean c) {
return !LegacyMats.BREAK.contains(type);// && !LegacyMats.LEAVES.contains(type);
}
private boolean checkUp(Bot npc, LivingEntity target, Player playerNPC, Location loc, boolean c) {
Location a = playerNPC.getLocation(); Location a = playerNPC.getLocation();
Location b = target.getLocation(); Location b = target.getLocation();
@@ -650,9 +636,10 @@ public class LegacyAgent extends Agent {
return false; return false;
} }
private boolean checkDown(Bot npc, Player player, Location loc, boolean c) { // possibly a looser check for c private boolean checkDown(Terminator npc, LivingEntity player, Location loc, boolean c) { // possibly a looser check for c
if (LegacyUtils.checkFreeSpace(npc.getLocation(), loc) || LegacyUtils.checkFreeSpace(player.getEyeLocation(), loc)) return false; if (LegacyUtils.checkFreeSpace(npc.getLocation(), loc) || LegacyUtils.checkFreeSpace(player.getEyeLocation(), loc))
return false;
if (c && npc.getLocation().getBlockY() > loc.getBlockY() + 1) { if (c && npc.getLocation().getBlockY() > loc.getBlockY() + 1) {
Block block = npc.getLocation().add(0, -1, 0).getBlock(); Block block = npc.getLocation().add(0, -1, 0).getBlock();
@@ -661,9 +648,7 @@ public class LegacyAgent extends Agent {
downMine(npc, player, block); downMine(npc, player, block);
preBreak(npc, player, block, LegacyLevel.BELOW); preBreak(npc, player, block, LegacyLevel.BELOW);
return true; return true;
} } else {
else {
Location a = loc.clone(); Location a = loc.clone();
Location b = player.getLocation(); Location b = player.getLocation();
@@ -684,7 +669,7 @@ public class LegacyAgent extends Agent {
} }
} }
private void downMine(Bot npc, Player player, Block block) { private void downMine(Terminator npc, LivingEntity player, Block block) {
if (!LegacyMats.NO_CRACK.contains(block.getType())) { if (!LegacyMats.NO_CRACK.contains(block.getType())) {
Location locBlock = player.getLocation(); Location locBlock = player.getLocation();
locBlock.setX(locBlock.getBlockX() + 0.5); locBlock.setX(locBlock.getBlockX() + 0.5);
@@ -719,7 +704,7 @@ public class LegacyAgent extends Agent {
} }
} }
private boolean checkFence(Bot bot, Block block, Player player) { private boolean checkFence(Terminator bot, Block block, LivingEntity player) {
if (LegacyMats.FENCE.contains(block.getType())) { if (LegacyMats.FENCE.contains(block.getType())) {
preBreak(bot, player, block, LegacyLevel.AT_D); preBreak(bot, player, block, LegacyLevel.AT_D);
return true; return true;
@@ -728,7 +713,7 @@ public class LegacyAgent extends Agent {
return false; return false;
} }
private boolean checkAt(Bot bot, Block block, Player player) { private boolean checkAt(Terminator bot, Block block, LivingEntity player) {
if (LegacyMats.BREAK.contains(block.getType())) { if (LegacyMats.BREAK.contains(block.getType())) {
return false; return false;
} else { } else {
@@ -737,7 +722,7 @@ public class LegacyAgent extends Agent {
} }
} }
private void preBreak(Bot bot, Player player, Block block, LegacyLevel level) { private void preBreak(Terminator bot, LivingEntity player, Block block, LegacyLevel level) {
Material item; Material item;
Material type = block.getType(); Material type = block.getType();
@@ -768,7 +753,7 @@ public class LegacyAgent extends Agent {
@Override @Override
public void run() { public void run() {
bot.punch(); bot.punch();
} }
}; };
@@ -780,7 +765,7 @@ public class LegacyAgent extends Agent {
blockBreakEffect(player, block, level); blockBreakEffect(player, block, level);
} }
private void blockBreakEffect(Player player, Block block, LegacyLevel level) { private void blockBreakEffect(LivingEntity player, Block block, LegacyLevel level) {
if (LegacyMats.NO_CRACK.contains(block.getType())) return; if (LegacyMats.NO_CRACK.contains(block.getType())) return;
@@ -832,26 +817,10 @@ public class LegacyAgent extends Agent {
// wow this repeated code is so bad lmao // wow this repeated code is so bad lmao
if (player.isDead()) { if (player.isDead() || (!block.equals(cur) || block.getType() != cur.getType())) {
this.cancel(); this.cancel();
ClientboundBlockDestructionPacket crack = new ClientboundBlockDestructionPacket(crackList.get(block), new BlockPos(block.getX(), block.getY(), block.getZ()), -1); TerminatorPlusAPI.getInternalBridge().sendBlockDestructionPacket(crackList.get(block), block.getX(), block.getY(), block.getZ(), -1);
for (Player all : Bukkit.getOnlinePlayers()) {
((CraftPlayer) all).getHandle().connection.send(crack);
}
crackList.remove(block);
mining.remove(this);
return;
}
if (!block.equals(cur) || block.getType() != cur.getType()) {
this.cancel();
ClientboundBlockDestructionPacket crack = new ClientboundBlockDestructionPacket(crackList.get(block), new BlockPos(block.getX(), block.getY(), block.getZ()), -1);
for (Player all : Bukkit.getOnlinePlayers()) {
((CraftPlayer) all).getHandle().connection.send(crack);
}
crackList.remove(block); crackList.remove(block);
mining.remove(this); mining.remove(this);
@@ -863,13 +832,12 @@ public class LegacyAgent extends Agent {
if (i == 9) { if (i == 9) {
this.cancel(); this.cancel();
ClientboundBlockDestructionPacket crack = new ClientboundBlockDestructionPacket(crackList.get(block), new BlockPos(block.getX(), block.getY(), block.getZ()), -1); TerminatorPlusAPI.getInternalBridge().sendBlockDestructionPacket(crackList.get(block), block.getX(), block.getY(), block.getZ(), -1);
for (Player all : Bukkit.getOnlinePlayers()) {
((CraftPlayer) all).getHandle().connection.send(crack);
}
if (sound != null) { if (sound != null) {
for (Player all : Bukkit.getOnlinePlayers()) all.playSound(block.getLocation(), sound, SoundCategory.BLOCKS, 1, 1); for (Player all : Bukkit.getOnlinePlayers())
all.playSound(block.getLocation(), sound, SoundCategory.BLOCKS, 1, 1);
} }
block.breakNaturally(); block.breakNaturally();
@@ -888,15 +856,14 @@ public class LegacyAgent extends Agent {
} }
if (sound != null) { if (sound != null) {
for (Player all : Bukkit.getOnlinePlayers()) all.playSound(block.getLocation(), sound, SoundCategory.BLOCKS, (float) 0.3, 1); for (Player all : Bukkit.getOnlinePlayers())
all.playSound(block.getLocation(), sound, SoundCategory.BLOCKS, (float) 0.3, 1);
} }
if (block.getType() == Material.BARRIER || block.getType() == Material.BEDROCK || block.getType() == Material.END_PORTAL_FRAME) return; if (block.getType() == Material.BARRIER || block.getType() == Material.BEDROCK || block.getType() == Material.END_PORTAL_FRAME)
return;
ClientboundBlockDestructionPacket crack = new ClientboundBlockDestructionPacket(crackList.get(block), new BlockPos(block.getX(), block.getY(), block.getZ()), i); TerminatorPlusAPI.getInternalBridge().sendBlockDestructionPacket(crackList.get(block), block.getX(), block.getY(), block.getZ(), i);
for (Player all : Bukkit.getOnlinePlayers()) {
((CraftPlayer) all).getHandle().connection.send(crack);
}
mining.put(this, (byte) (i + 1)); mining.put(this, (byte) (i + 1));
} }
@@ -909,7 +876,7 @@ public class LegacyAgent extends Agent {
} }
} }
private void placeWaterDown(Bot bot, World world, Location loc) { private void placeWaterDown(Terminator bot, World world, Location loc) {
if (loc.getBlock().getType() == Material.WATER) return; if (loc.getBlock().getType() == Material.WATER) return;
bot.look(BlockFace.DOWN); bot.look(BlockFace.DOWN);
@@ -930,8 +897,8 @@ public class LegacyAgent extends Agent {
}, 5); }, 5);
} }
private void miscellaneousChecks(Bot bot, LivingEntity target) { private void miscellaneousChecks(Terminator bot, LivingEntity target) {
Player botPlayer = bot.getBukkitEntity(); LivingEntity botPlayer = bot.getBukkitEntity();
World world = botPlayer.getWorld(); World world = botPlayer.getWorld();
String worldName = world.getName(); String worldName = world.getName();
Location loc = bot.getLocation(); Location loc = bot.getLocation();
@@ -1076,7 +1043,7 @@ public class LegacyAgent extends Agent {
} }
} }
private void resetHand(Bot npc, LivingEntity target, Player playerNPC) { private void resetHand(Terminator npc, LivingEntity target, LivingEntity playerNPC) {
if (!noFace.contains(npc)) { // LESSLAG if there is no if statement here if (!noFace.contains(npc)) { // LESSLAG if there is no if statement here
npc.faceLocation(target.getLocation()); npc.faceLocation(target.getLocation());
} }
@@ -1094,7 +1061,7 @@ public class LegacyAgent extends Agent {
npc.setItem(null); npc.setItem(null);
} }
private boolean onBoat(Player player) { private boolean onBoat(LivingEntity player) {
Set<Boat> cache = new HashSet<>(); Set<Boat> cache = new HashSet<>();
boolean check = false; boolean check = false;
@@ -1118,8 +1085,9 @@ public class LegacyAgent extends Agent {
return check; return check;
} }
private void attack(Bot bot, LivingEntity target, Location loc) { private void attack(Terminator bot, LivingEntity target, Location loc) {
if ((target instanceof Player && PlayerUtils.isInvincible(((Player) target).getGameMode())) || target.getNoDamageTicks() >= 5 || loc.distance(target.getLocation()) >= 4) return; if ((target instanceof Player && PlayerUtils.isInvincible(((Player) target).getGameMode())) || target.getNoDamageTicks() >= 5 || loc.distance(target.getLocation()) >= 4)
return;
bot.attack(target); bot.attack(target);
} }
@@ -1128,7 +1096,7 @@ public class LegacyAgent extends Agent {
this.goal = goal; this.goal = goal;
} }
public LivingEntity locateTarget(Bot bot, Location loc) { public LivingEntity locateTarget(Terminator bot, Location loc) {
LivingEntity result = null; LivingEntity result = null;
switch (goal) { switch (goal) {
@@ -1168,7 +1136,7 @@ public class LegacyAgent extends Agent {
case NEAREST_MOB: { case NEAREST_MOB: {
for (LivingEntity entity : bot.getBukkitEntity().getWorld().getLivingEntities()) { for (LivingEntity entity : bot.getBukkitEntity().getWorld().getLivingEntities()) {
if (entity instanceof Mob && validateCloserEntity(entity, loc, result)) { if (entity instanceof Mob && validateCloserEntity(entity, loc, result)) {
result = entity; result = entity;
} }
} }
@@ -1176,9 +1144,9 @@ public class LegacyAgent extends Agent {
} }
case NEAREST_BOT: { case NEAREST_BOT: {
for (Bot otherBot : manager.fetch()) { for (Terminator otherBot : manager.fetch()) {
if (bot != otherBot) { if (bot != otherBot) {
Player player = otherBot.getBukkitEntity(); LivingEntity player = otherBot.getBukkitEntity();
if (validateCloserEntity(player, loc, result)) { if (validateCloserEntity(player, loc, result)) {
result = player; result = player;
@@ -1190,13 +1158,13 @@ public class LegacyAgent extends Agent {
} }
case NEAREST_BOT_DIFFER: { case NEAREST_BOT_DIFFER: {
String name = bot.getName().getString(); String name = bot.getBotName();
for (Bot otherBot : manager.fetch()) { for (Terminator otherBot : manager.fetch()) {
if (bot != otherBot) { if (bot != otherBot) {
Player player = otherBot.getBukkitEntity(); LivingEntity player = otherBot.getBukkitEntity();
if (!name.equals(otherBot.getName()) && validateCloserEntity(player, loc, result)) { if (!name.equals(otherBot.getBotName()) && validateCloserEntity(player, loc, result)) {
result = player; result = player;
} }
} }
@@ -1206,13 +1174,13 @@ public class LegacyAgent extends Agent {
} }
case NEAREST_BOT_DIFFER_ALPHA: { case NEAREST_BOT_DIFFER_ALPHA: {
String name = bot.getName().getString().replaceAll("[^A-Za-z]+", ""); String name = NAME_PATTERN.matcher(bot.getBotName()).replaceAll("");
for (Bot otherBot : manager.fetch()) { for (Terminator otherBot : manager.fetch()) {
if (bot != otherBot) { if (bot != otherBot) {
Player player = otherBot.getBukkitEntity(); LivingEntity player = otherBot.getBukkitEntity();
if (!name.equals(otherBot.getName().getString().replaceAll("[^A-Za-z]+", "")) && validateCloserEntity(player, loc, result)) { if (!name.equals(NAME_PATTERN.matcher(otherBot.getBotName()).replaceAll("")) && validateCloserEntity(player, loc, result)) {
result = player; result = player;
} }
} }

View File

@@ -1,13 +1,13 @@
package net.nuggetmc.tplus.bot.agent.legacyagent; package net.nuggetmc.tplus.api.agent.legacyagent;
import net.nuggetmc.tplus.TerminatorPlus; import net.nuggetmc.tplus.api.Terminator;
import net.nuggetmc.tplus.bot.Bot;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
@@ -15,17 +15,18 @@ import java.util.Set;
public class LegacyBlockCheck { public class LegacyBlockCheck {
private final TerminatorPlus plugin; private final Plugin plugin;
private final LegacyAgent agent; private final LegacyAgent agent;
public LegacyBlockCheck(LegacyAgent agent) { public LegacyBlockCheck(LegacyAgent agent, Plugin plugin) {
this.plugin = TerminatorPlus.getInstance(); this.plugin = plugin;
this.agent = agent; this.agent = agent;
} }
private void placeFinal(Bot bot, Player player, Location loc) { private void placeFinal(Terminator bot, LivingEntity player, Location loc) {
if (loc.getBlock().getType() != Material.COBBLESTONE) { if (loc.getBlock().getType() != Material.COBBLESTONE) {
for (Player all : Bukkit.getOnlinePlayers()) all.playSound(loc, Sound.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1, 1); for (Player all : Bukkit.getOnlinePlayers())
all.playSound(loc, Sound.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1, 1);
bot.setItem(new ItemStack(Material.COBBLESTONE)); bot.setItem(new ItemStack(Material.COBBLESTONE));
loc.getBlock().setType(Material.COBBLESTONE); loc.getBlock().setType(Material.COBBLESTONE);
@@ -36,7 +37,7 @@ public class LegacyBlockCheck {
} }
} }
public void placeBlock(Bot bot, Player player, Block block) { public void placeBlock(Terminator bot, LivingEntity player, Block block) {
Location loc = block.getLocation(); Location loc = block.getLocation();
@@ -116,23 +117,26 @@ public class LegacyBlockCheck {
Bukkit.getScheduler().runTaskLater(plugin, () -> { Bukkit.getScheduler().runTaskLater(plugin, () -> {
Block b2 = loc.clone().add(0, -1, 0).getBlock(); Block b2 = loc.clone().add(0, -1, 0).getBlock();
if (LegacyMats.SPAWN.contains(b2.getType())) { if (LegacyMats.SPAWN.contains(b2.getType())) {
for (Player all : Bukkit.getOnlinePlayers()) all.playSound(loc, Sound.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1, 1); for (Player all : Bukkit.getOnlinePlayers())
all.playSound(loc, Sound.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1, 1);
placeFinal(bot, player, b2.getLocation()); placeFinal(bot, player, b2.getLocation());
} }
}, 1); }, 1);
Bukkit.getScheduler().runTaskLater(plugin, () -> { Bukkit.getScheduler().runTaskLater(plugin, () -> {
for (Player all : Bukkit.getOnlinePlayers()) all.playSound(loc, Sound.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1, 1); for (Player all : Bukkit.getOnlinePlayers())
all.playSound(loc, Sound.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1, 1);
placeFinal(bot, player, block.getLocation()); placeFinal(bot, player, block.getLocation());
}, 3); }, 3);
return; return;
} }
for (Player all : Bukkit.getOnlinePlayers()) all.playSound(loc, Sound.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1, 1); for (Player all : Bukkit.getOnlinePlayers())
all.playSound(loc, Sound.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1, 1);
placeFinal(bot, player, block.getLocation()); placeFinal(bot, player, block.getLocation());
} }
public void clutch(Bot bot, LivingEntity target) { public void clutch(Terminator bot, LivingEntity target) {
Location botLoc = bot.getLocation(); Location botLoc = bot.getLocation();
Material type = botLoc.clone().add(0, -1, 0).getBlock().getType(); Material type = botLoc.clone().add(0, -1, 0).getBlock().getType();
@@ -144,10 +148,10 @@ public class LegacyBlockCheck {
Location loc = botLoc.clone().add(0, -1, 0); Location loc = botLoc.clone().add(0, -1, 0);
Set<Block> face = new HashSet<>(Arrays.asList( Set<Block> face = new HashSet<>(Arrays.asList(
loc.clone().add(1, 0, 0).getBlock(), loc.clone().add(1, 0, 0).getBlock(),
loc.clone().add(-1, 0, 0).getBlock(), loc.clone().add(-1, 0, 0).getBlock(),
loc.clone().add(0, 0, 1).getBlock(), loc.clone().add(0, 0, 1).getBlock(),
loc.clone().add(0, 0, -1).getBlock() loc.clone().add(0, 0, -1).getBlock()
)); ));
Location at = null; Location at = null;
@@ -181,7 +185,8 @@ public class LegacyBlockCheck {
bot.punch(); bot.punch();
bot.sneak(); bot.sneak();
for (Player all : Bukkit.getOnlinePlayers()) all.playSound(loc, Sound.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1, 1); for (Player all : Bukkit.getOnlinePlayers())
all.playSound(loc, Sound.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1, 1);
bot.setItem(new ItemStack(Material.COBBLESTONE)); bot.setItem(new ItemStack(Material.COBBLESTONE));
loc.getBlock().setType(Material.COBBLESTONE); loc.getBlock().setType(Material.COBBLESTONE);
} }

View File

@@ -1,4 +1,4 @@
package net.nuggetmc.tplus.bot.agent.legacyagent; package net.nuggetmc.tplus.api.agent.legacyagent;
import org.bukkit.Material; import org.bukkit.Material;

View File

@@ -1,4 +1,4 @@
package net.nuggetmc.tplus.bot.agent.legacyagent; package net.nuggetmc.tplus.api.agent.legacyagent;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;

View File

@@ -1,4 +1,4 @@
package net.nuggetmc.tplus.bot.agent.legacyagent; package net.nuggetmc.tplus.api.agent.legacyagent;
import org.bukkit.Material; import org.bukkit.Material;

View File

@@ -1,13 +1,9 @@
package net.nuggetmc.tplus.bot.agent.legacyagent; package net.nuggetmc.tplus.api.agent.legacyagent;
import net.minecraft.core.BlockPos; import net.nuggetmc.tplus.api.TerminatorPlusAPI;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockState;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_18_R1.CraftWorld;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
public class LegacyUtils { public class LegacyUtils {
@@ -36,12 +32,6 @@ public class LegacyUtils {
} }
public static Sound breakBlockSound(Block block) { public static Sound breakBlockSound(Block block) {
Level nmsWorld = ((CraftWorld) block.getWorld()).getHandle(); return TerminatorPlusAPI.getInternalBridge().breakBlockSound(block);
BlockState blockState = nmsWorld.getBlockState(new BlockPos(block.getX(), block.getY(), block.getZ()));
net.minecraft.world.level.block.Block nmsBlock = blockState.getBlock();
SoundType soundEffectType = nmsBlock.getSoundType(blockState);
return Sound.valueOf( soundEffectType.getBreakSound().getLocation().getPath().replace(".", "_").toUpperCase());
} }
} }

View File

@@ -1,4 +1,4 @@
package net.nuggetmc.tplus.bot.agent.legacyagent; package net.nuggetmc.tplus.api.agent.legacyagent;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;

View File

@@ -1,7 +1,7 @@
package net.nuggetmc.tplus.api.agent.legacyagent.ai; package net.nuggetmc.tplus.api.agent.legacyagent.ai;
import net.nuggetmc.tplus.api.Terminator; import net.nuggetmc.tplus.api.Terminator;
import net.nuggetmc.tplus.utils.MathUtils; import net.nuggetmc.tplus.api.utils.MathUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;

View File

@@ -1,25 +1,20 @@
package net.nuggetmc.tplus.bot.agent.legacyagent.ai; package net.nuggetmc.tplus.api.agent.legacyagent.ai;
import net.minecraft.world.entity.LivingEntity; import net.nuggetmc.tplus.api.AIManager;
import net.nuggetmc.tplus.TerminatorPlus; import net.nuggetmc.tplus.api.BotManager;
import net.nuggetmc.tplus.api.Terminator; import net.nuggetmc.tplus.api.Terminator;
import net.nuggetmc.tplus.api.agent.legacyagent.ai.BotDataType; import net.nuggetmc.tplus.api.agent.legacyagent.EnumTargetGoal;
import net.nuggetmc.tplus.api.agent.legacyagent.ai.BotNode; import net.nuggetmc.tplus.api.agent.legacyagent.LegacyAgent;
import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork; import net.nuggetmc.tplus.api.utils.ChatUtils;
import net.nuggetmc.tplus.bot.Bot; import net.nuggetmc.tplus.api.utils.MathUtils;
import net.nuggetmc.tplus.bot.BotManager; import net.nuggetmc.tplus.api.utils.MojangAPI;
import net.nuggetmc.tplus.bot.agent.legacyagent.EnumTargetGoal; import net.nuggetmc.tplus.api.utils.PlayerUtils;
import net.nuggetmc.tplus.bot.agent.legacyagent.LegacyAgent;
import net.nuggetmc.tplus.command.commands.AICommand;
import net.nuggetmc.tplus.utils.ChatUtils;
import net.nuggetmc.tplus.utils.MathUtils;
import net.nuggetmc.tplus.utils.MojangAPI;
import net.nuggetmc.tplus.utils.PlayerUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import java.text.NumberFormat; import java.text.NumberFormat;
@@ -36,9 +31,9 @@ public class IntelligenceAgent {
* default anchor location, /ai relocateanchor * default anchor location, /ai relocateanchor
*/ */
private final TerminatorPlus plugin; private final Plugin plugin;
private final BotManager manager; private final BotManager manager;
private final AICommand aiManager; private final AIManager aiManager;
private final BukkitScheduler scheduler; private final BukkitScheduler scheduler;
private LegacyAgent agent; private LegacyAgent agent;
@@ -51,7 +46,7 @@ public class IntelligenceAgent {
private final String botSkin; private final String botSkin;
private final int cutoff; private final int cutoff;
private final Map<String, Bot> bots; private final Map<String, Terminator> bots;
private int populationSize; private int populationSize;
private int generation; private int generation;
@@ -61,9 +56,9 @@ public class IntelligenceAgent {
private final Set<CommandSender> users; private final Set<CommandSender> users;
private final Map<Integer, Set<Map<BotNode, Map<BotDataType, Double>>>> genProfiles; private final Map<Integer, Set<Map<BotNode, Map<BotDataType, Double>>>> genProfiles;
public IntelligenceAgent(AICommand aiManager, int populationSize, String name, String skin) { public IntelligenceAgent(AIManager aiManager, int populationSize, String name, String skin, Plugin plugin, BotManager manager) {
this.plugin = TerminatorPlus.getInstance(); this.plugin = plugin;
this.manager = plugin.getManager(); this.manager = manager;
this.aiManager = aiManager; this.aiManager = aiManager;
this.scheduler = Bukkit.getScheduler(); this.scheduler = Bukkit.getScheduler();
this.name = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(Calendar.getInstance().getTime()); this.name = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(Calendar.getInstance().getTime());
@@ -126,7 +121,7 @@ public class IntelligenceAgent {
Location loc = PlayerUtils.findAbove(primary.getLocation(), 20); Location loc = PlayerUtils.findAbove(primary.getLocation(), 20);
scheduler.runTask(plugin, () -> { scheduler.runTask(plugin, () -> {
Set<Bot> bots; Set<Terminator> bots;
if (loadedProfiles == null) { if (loadedProfiles == null) {
bots = manager.createBots(loc, botName, skinData, populationSize, NeuralNetwork.RANDOM); bots = manager.createBots(loc, botName, skinData, populationSize, NeuralNetwork.RANDOM);
@@ -145,7 +140,7 @@ public class IntelligenceAgent {
} }
bots.forEach(bot -> { bots.forEach(bot -> {
String name = bot.getName().getString(); String name = bot.getBotName();
while (this.bots.containsKey(name)) { while (this.bots.containsKey(name)) {
name += "_"; name += "_";
@@ -249,7 +244,7 @@ public class IntelligenceAgent {
} }
private int aliveCount() { private int aliveCount() {
return (int) bots.values().stream().filter(LivingEntity::isAlive).count(); return (int) bots.values().stream().filter(Terminator::isAlive).count();
} }
private void close() { private void close() {
@@ -331,7 +326,7 @@ public class IntelligenceAgent {
if (!bots.isEmpty()) { if (!bots.isEmpty()) {
print("Removing all cached bots..."); print("Removing all cached bots...");
bots.values().forEach(Bot::removeVisually); bots.values().forEach(Terminator::removeVisually);
bots.clear(); bots.clear();
} }

View File

@@ -1,8 +1,8 @@
package net.nuggetmc.tplus.api.agent.legacyagent.ai; package net.nuggetmc.tplus.api.agent.legacyagent.ai;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.nuggetmc.tplus.utils.MathUtils; import net.nuggetmc.tplus.api.utils.ChatUtils;
import net.nuggetmc.tplus.utils.ChatUtils; import net.nuggetmc.tplus.api.utils.MathUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import java.util.*; import java.util.*;

View File

@@ -1,24 +1,24 @@
package net.nuggetmc.tplus.bot.event; package net.nuggetmc.tplus.api.event;
import net.nuggetmc.tplus.bot.Bot; import net.nuggetmc.tplus.api.Terminator;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class BotDamageByPlayerEvent { public class BotDamageByPlayerEvent {
private final Bot bot; private final Terminator bot;
private final Player player; private final Player player;
private float damage; private float damage;
private boolean cancelled; private boolean cancelled;
public BotDamageByPlayerEvent(Bot bot, Player player, float damage) { public BotDamageByPlayerEvent(Terminator bot, Player player, float damage) {
this.bot = bot; this.bot = bot;
this.player = player; this.player = player;
this.damage = damage; this.damage = damage;
} }
public Bot getBot() { public Terminator getBot() {
return bot; return bot;
} }

View File

@@ -1,18 +1,18 @@
package net.nuggetmc.tplus.bot.event; package net.nuggetmc.tplus.api.event;
import net.nuggetmc.tplus.bot.Bot; import net.nuggetmc.tplus.api.Terminator;
import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.entity.EntityDeathEvent;
public class BotDeathEvent extends EntityDeathEvent { public class BotDeathEvent extends EntityDeathEvent {
private final Bot bot; private final Terminator bot;
public BotDeathEvent(EntityDeathEvent event, Bot bot) { public BotDeathEvent(EntityDeathEvent event, Terminator bot) {
super(event.getEntity(), event.getDrops(), event.getDroppedExp()); super(event.getEntity(), event.getDrops(), event.getDroppedExp());
this.bot = bot; this.bot = bot;
} }
public Bot getBot() { public Terminator getBot() {
return bot; return bot;
} }
} }

View File

@@ -1,26 +1,26 @@
package net.nuggetmc.tplus.bot.event; package net.nuggetmc.tplus.api.event;
import net.nuggetmc.tplus.bot.Bot; import net.nuggetmc.tplus.api.Terminator;
public class BotFallDamageEvent { public class BotFallDamageEvent {
private final Bot bot; private final Terminator bot;
private boolean cancelled; private boolean cancelled;
public BotFallDamageEvent(Bot bot) { public BotFallDamageEvent(Terminator bot) {
this.bot = bot; this.bot = bot;
} }
public Bot getBot() { public Terminator getBot() {
return bot; return bot;
} }
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
public boolean isCancelled() { public boolean isCancelled() {
return cancelled; return cancelled;
} }
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
} }

View File

@@ -1,6 +1,6 @@
package net.nuggetmc.tplus.bot.event; package net.nuggetmc.tplus.api.event;
import net.nuggetmc.tplus.bot.Bot; import net.nuggetmc.tplus.api.Terminator;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class BotKilledByPlayerEvent { public class BotKilledByPlayerEvent {
@@ -8,15 +8,15 @@ public class BotKilledByPlayerEvent {
// eventually also call this event for deaths from other damage causes within combat time // eventually also call this event for deaths from other damage causes within combat time
// (like hitting the ground too hard) // (like hitting the ground too hard)
private final Bot bot; private final Terminator bot;
private final Player player; private final Player player;
public BotKilledByPlayerEvent(Bot bot, Player player) { public BotKilledByPlayerEvent(Terminator bot, Player player) {
this.bot = bot; this.bot = bot;
this.player = player; this.player = player;
} }
public Bot getBot() { public Terminator getBot() {
return bot; return bot;
} }

View File

@@ -1,4 +1,4 @@
package net.nuggetmc.tplus.utils; package net.nuggetmc.tplus.api.utils;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;

View File

@@ -1,4 +1,4 @@
package net.nuggetmc.tplus.utils; package net.nuggetmc.tplus.api.utils;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;

View File

@@ -1,8 +1,7 @@
package net.nuggetmc.tplus.bot; package net.nuggetmc.tplus.api.utils;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.Property;
import net.nuggetmc.tplus.utils.MojangAPI;
import java.util.UUID; import java.util.UUID;

View File

@@ -0,0 +1,23 @@
package net.nuggetmc.tplus.api.utils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.permissions.ServerOperator;
import java.util.Arrays;
public class DebugLogUtils {
public static final String PREFIX = ChatColor.YELLOW + "[DEBUG] " + ChatColor.RESET;
public static void log(Object... objects) {
String[] values = fromStringArray(objects);
String message = PREFIX + String.join(" ", values);
Bukkit.getConsoleSender().sendMessage(message);
Bukkit.getOnlinePlayers().stream().filter(ServerOperator::isOp).forEach(p -> p.sendMessage(message));
}
public static String[] fromStringArray(Object[] objects) {
return Arrays.stream(objects).map(String::valueOf).toArray(String[]::new);
}
}

View File

@@ -1,4 +1,4 @@
package net.nuggetmc.tplus.utils; package net.nuggetmc.tplus.api.utils;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;

View File

@@ -1,4 +1,4 @@
package net.nuggetmc.tplus.utils; package net.nuggetmc.tplus.api.utils;
import net.nuggetmc.tplus.api.Terminator; import net.nuggetmc.tplus.api.Terminator;
import org.bukkit.util.NumberConversions; import org.bukkit.util.NumberConversions;

View File

@@ -1,4 +1,4 @@
package net.nuggetmc.tplus.utils; package net.nuggetmc.tplus.api.utils;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;

View File

@@ -1,6 +1,6 @@
package net.nuggetmc.tplus.utils; package net.nuggetmc.tplus.api.utils;
import net.nuggetmc.tplus.TerminatorPlus; import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.json.simple.JSONArray; import org.json.simple.JSONArray;
@@ -15,12 +15,12 @@ import java.util.Set;
public class PlayerUtils { public class PlayerUtils {
private static final Set<String> USERNAME_CACHE = new HashSet<>();
public static boolean isInvincible(GameMode mode) { public static boolean isInvincible(GameMode mode) {
return mode != GameMode.SURVIVAL && mode != GameMode.ADVENTURE && mode != null; return mode != GameMode.SURVIVAL && mode != GameMode.ADVENTURE && mode != null;
} }
private static final Set<String> USERNAME_CACHE = new HashSet<>();
public static String randomName() { public static String randomName() {
if (USERNAME_CACHE.isEmpty()) { if (USERNAME_CACHE.isEmpty()) {
fillUsernameCache(); fillUsernameCache();
@@ -30,7 +30,7 @@ public class PlayerUtils {
} }
public static void fillUsernameCache() { public static void fillUsernameCache() {
String file = TerminatorPlus.getInstance().getServer().getWorldContainer().getAbsolutePath(); String file = Bukkit.getServer().getWorldContainer().getAbsolutePath();
file = file.substring(0, file.length() - 1) + "usercache.json"; file = file.substring(0, file.length() - 1) + "usercache.json";
JSONParser parser = new JSONParser(); JSONParser parser = new JSONParser();
@@ -44,10 +44,8 @@ public class PlayerUtils {
USERNAME_CACHE.add(username); USERNAME_CACHE.add(username);
} }
} } catch (IOException | ParseException e) {
DebugLogUtils.log("Failed to fetch from the usercache.");
catch (IOException | ParseException e) {
Debugger.log("Failed to fetch from the usercache.");
} }
} }

View File

@@ -1,4 +1,4 @@
package net.nuggetmc.tplus.utils; package net.nuggetmc.tplus.api.utils;
public class Singularity { public class Singularity {

View File

@@ -1,6 +1,8 @@
package net.nuggetmc.tplus; package net.nuggetmc.tplus;
import net.nuggetmc.tplus.bot.BotManager; import net.nuggetmc.tplus.api.TerminatorPlusAPI;
import net.nuggetmc.tplus.bot.BotManagerImpl;
import net.nuggetmc.tplus.bridge.InternalBridgeImpl;
import net.nuggetmc.tplus.command.CommandHandler; import net.nuggetmc.tplus.command.CommandHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@@ -12,7 +14,7 @@ public class TerminatorPlus extends JavaPlugin {
private static TerminatorPlus instance; private static TerminatorPlus instance;
private static String version; private static String version;
private BotManager manager; private BotManagerImpl manager;
private CommandHandler handler; private CommandHandler handler;
public static TerminatorPlus getInstance() { public static TerminatorPlus getInstance() {
@@ -23,7 +25,7 @@ public class TerminatorPlus extends JavaPlugin {
return version; return version;
} }
public BotManager getManager() { public BotManagerImpl getManager() {
return manager; return manager;
} }
@@ -37,9 +39,12 @@ public class TerminatorPlus extends JavaPlugin {
version = getDescription().getVersion(); version = getDescription().getVersion();
// Create Instances // Create Instances
this.manager = new BotManager(); this.manager = new BotManagerImpl();
this.handler = new CommandHandler(this); this.handler = new CommandHandler(this);
TerminatorPlusAPI.setBotManager(manager);
TerminatorPlusAPI.setInternalBridge(new InternalBridgeImpl());
// Register event listeners // Register event listeners
this.registerEvents(manager); this.registerEvents(manager);
} }

View File

@@ -25,12 +25,12 @@ import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
import net.nuggetmc.tplus.TerminatorPlus; import net.nuggetmc.tplus.TerminatorPlus;
import net.nuggetmc.tplus.api.Terminator; import net.nuggetmc.tplus.api.Terminator;
import net.nuggetmc.tplus.api.agent.Agent;
import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork; import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork;
import net.nuggetmc.tplus.bot.agent.Agent; import net.nuggetmc.tplus.api.event.BotDamageByPlayerEvent;
import net.nuggetmc.tplus.bot.event.BotDamageByPlayerEvent; import net.nuggetmc.tplus.api.event.BotFallDamageEvent;
import net.nuggetmc.tplus.bot.event.BotFallDamageEvent; import net.nuggetmc.tplus.api.event.BotKilledByPlayerEvent;
import net.nuggetmc.tplus.bot.event.BotKilledByPlayerEvent; import net.nuggetmc.tplus.api.utils.*;
import net.nuggetmc.tplus.utils.*;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
@@ -71,6 +71,7 @@ public class Bot extends ServerPlayer implements Terminator {
private byte groundTicks; private byte groundTicks;
private byte jumpTicks; private byte jumpTicks;
private byte noFallTicks; private byte noFallTicks;
private Bot(MinecraftServer minecraftServer, ServerLevel worldServer, GameProfile profile) { private Bot(MinecraftServer minecraftServer, ServerLevel worldServer, GameProfile profile) {
super(minecraftServer, worldServer, profile); super(minecraftServer, worldServer, profile);
@@ -175,6 +176,14 @@ public class Bot extends ServerPlayer implements Terminator {
render(connection, getRenderPackets(), login); render(connection, getRenderPackets(), login);
} }
@Override
public void renderBot(Object packetListener, boolean login) {
if (!(packetListener instanceof ServerGamePacketListenerImpl)) {
throw new IllegalArgumentException("packetListener must be a instance of ServerGamePacketListenerImpl");
}
render((ServerGamePacketListenerImpl) packetListener, login);
}
private Packet<?>[] getRenderPackets() { private Packet<?>[] getRenderPackets() {
return new Packet[]{ return new Packet[]{
new ClientboundPlayerInfoPacket(ClientboundPlayerInfoPacket.Action.ADD_PLAYER, this), new ClientboundPlayerInfoPacket(ClientboundPlayerInfoPacket.Action.ADD_PLAYER, this),
@@ -336,6 +345,7 @@ public class Bot extends ServerPlayer implements Terminator {
fireTicks = 100; fireTicks = 100;
} }
@Override
public void setOnFirePackets(boolean onFire) { public void setOnFirePackets(boolean onFire) {
//entityData.set(new EntityDataAccessor<>(0, EntityDataSerializers.BYTE), onFire ? (byte) 1 : (byte) 0); //entityData.set(new EntityDataAccessor<>(0, EntityDataSerializers.BYTE), onFire ? (byte) 1 : (byte) 0);
//sendPacket(new ClientboundSetEntityDataPacket(getId(), entityData, false)); //sendPacket(new ClientboundSetEntityDataPacket(getId(), entityData, false));
@@ -667,6 +677,7 @@ public class Bot extends ServerPlayer implements Terminator {
return getBukkitEntity().getLocation(); return getBukkitEntity().getLocation();
} }
@Override
public void faceLocation(Location loc) { public void faceLocation(Location loc) {
look(loc.toVector().subtract(getLocation().toVector()), false); look(loc.toVector().subtract(getLocation().toVector()), false);
} }

View File

@@ -1,11 +1,14 @@
package net.nuggetmc.tplus.bot; package net.nuggetmc.tplus.bot;
import net.minecraft.server.network.ServerGamePacketListenerImpl; import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.nuggetmc.tplus.bot.agent.Agent; import net.nuggetmc.tplus.TerminatorPlus;
import net.nuggetmc.tplus.bot.agent.legacyagent.LegacyAgent; import net.nuggetmc.tplus.api.BotManager;
import net.nuggetmc.tplus.api.Terminator;
import net.nuggetmc.tplus.api.agent.Agent;
import net.nuggetmc.tplus.api.agent.legacyagent.LegacyAgent;
import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork; import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork;
import net.nuggetmc.tplus.bot.event.BotDeathEvent; import net.nuggetmc.tplus.api.event.BotDeathEvent;
import net.nuggetmc.tplus.utils.MojangAPI; import net.nuggetmc.tplus.api.utils.MojangAPI;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@@ -23,35 +26,38 @@ import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class BotManager implements Listener { public class BotManagerImpl implements BotManager, Listener {
private final Agent agent; private final Agent agent;
private final Set<Bot> bots; private final Set<Terminator> bots;
private final NumberFormat numberFormat; private final NumberFormat numberFormat;
public boolean joinMessages = false; public boolean joinMessages = false;
public BotManager() { public BotManagerImpl() {
this.agent = new LegacyAgent(this); this.agent = new LegacyAgent(this, TerminatorPlus.getInstance());
this.bots = ConcurrentHashMap.newKeySet(); //should fix concurrentmodificationexception this.bots = ConcurrentHashMap.newKeySet(); //should fix concurrentmodificationexception
this.numberFormat = NumberFormat.getInstance(Locale.US); this.numberFormat = NumberFormat.getInstance(Locale.US);
} }
public Set<Bot> fetch() { @Override
public Set<Terminator> fetch() {
return bots; return bots;
} }
public void add(Bot bot) { @Override
public void add(Terminator bot) {
if (joinMessages) { if (joinMessages) {
Bukkit.broadcastMessage(ChatColor.YELLOW + (bot.getName() + " joined the game")); Bukkit.broadcastMessage(ChatColor.YELLOW + (bot.getBotName() + " joined the game"));
} }
bots.add(bot); bots.add(bot);
} }
public Bot getFirst(String name) { @Override
for (Bot bot : bots) { public Terminator getFirst(String name) {
if (name.equals(bot.getName())) { for (Terminator bot : bots) {
if (name.equals(bot.getBotName())) {
return bot; return bot;
} }
} }
@@ -59,18 +65,26 @@ public class BotManager implements Listener {
return null; return null;
} }
@Override
public List<String> fetchNames() { public List<String> fetchNames() {
return bots.stream().map(Bot::getName).map(component -> component.getString()).collect(Collectors.toList()); //return bots.stream().map(Bot::getBotName).map(component -> component.getString()).collect(Collectors.toList());
return bots.stream().map(terminator -> {
if (terminator instanceof Bot bot) return bot.getName().getString();
else return terminator.getBotName();
}).collect(Collectors.toList());
} }
@Override
public Agent getAgent() { public Agent getAgent() {
return agent; return agent;
} }
@Override
public void createBots(Player sender, String name, String skinName, int n) { public void createBots(Player sender, String name, String skinName, int n) {
createBots(sender, name, skinName, n, null); createBots(sender, name, skinName, n, null);
} }
@Override
public void createBots(Player sender, String name, String skinName, int n, NeuralNetwork network) { public void createBots(Player sender, String name, String skinName, int n, NeuralNetwork network) {
long timestamp = System.currentTimeMillis(); long timestamp = System.currentTimeMillis();
@@ -88,7 +102,8 @@ public class BotManager implements Listener {
sender.sendMessage("Process completed (" + ChatColor.RED + ((System.currentTimeMillis() - timestamp) / 1000D) + "s" + ChatColor.RESET + ")."); sender.sendMessage("Process completed (" + ChatColor.RED + ((System.currentTimeMillis() - timestamp) / 1000D) + "s" + ChatColor.RESET + ").");
} }
public Set<Bot> createBots(Location loc, String name, String[] skin, int n, NeuralNetwork network) { @Override
public Set<Terminator> createBots(Location loc, String name, String[] skin, int n, NeuralNetwork network) {
List<NeuralNetwork> networks = new ArrayList<>(); List<NeuralNetwork> networks = new ArrayList<>();
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
@@ -98,8 +113,9 @@ public class BotManager implements Listener {
return createBots(loc, name, skin, networks); return createBots(loc, name, skin, networks);
} }
public Set<Bot> createBots(Location loc, String name, String[] skin, List<NeuralNetwork> networks) { @Override
Set<Bot> bots = new HashSet<>(); public Set<Terminator> createBots(Location loc, String name, String[] skin, List<NeuralNetwork> networks) {
Set<Terminator> bots = new HashSet<>();
World world = loc.getWorld(); World world = loc.getWorld();
int n = networks.size(); int n = networks.size();
@@ -138,39 +154,38 @@ public class BotManager implements Listener {
return new Vector(Math.random() - 0.5, 0.5, Math.random() - 0.5).normalize(); return new Vector(Math.random() - 0.5, 0.5, Math.random() - 0.5).normalize();
} }
public void remove(Bot bot) { @Override
public void remove(Terminator bot) {
bots.remove(bot); bots.remove(bot);
} }
@Override
public void reset() { public void reset() {
if (!bots.isEmpty()) { if (!bots.isEmpty()) {
bots.forEach(Bot::removeVisually); bots.forEach(Terminator::removeVisually);
bots.clear(); // Not always necessary, but a good security measure bots.clear(); // Not always necessary, but a good security measure
} }
agent.stopAllTasks(); agent.stopAllTasks();
} }
/**
* Get a bot from a Player object @Override
* @param player public Terminator getBot(Player player) { // potentially memory intensive
* @deprecated Use {@link #getBot(UUID)} instead as this may no longer work
* @return
*/
@Deprecated
public Bot getBot(Player player) { // potentially memory intensive
int id = player.getEntityId(); int id = player.getEntityId();
return getBot(id); return getBot(id);
} }
public Bot getBot(UUID uuid) { @Override
public Terminator getBot(UUID uuid) {
Entity entity = Bukkit.getEntity(uuid); Entity entity = Bukkit.getEntity(uuid);
if (entity == null) return null; if (entity == null) return null;
return getBot(entity.getEntityId()); return getBot(entity.getEntityId());
} }
public Bot getBot(int entityId) { @Override
for (Bot bot : bots) { public Terminator getBot(int entityId) {
for (Terminator bot : bots) {
if (bot.getId() == entityId) { if (bot.getId() == entityId) {
return bot; return bot;
} }
@@ -181,13 +196,13 @@ public class BotManager implements Listener {
@EventHandler @EventHandler
public void onJoin(PlayerJoinEvent event) { public void onJoin(PlayerJoinEvent event) {
ServerGamePacketListenerImpl connection = ((CraftPlayer) event.getPlayer()).getHandle().connection; ServerGamePacketListenerImpl connection = ((CraftPlayer) event.getPlayer()).getHandle().connection;
bots.forEach(bot -> bot.render(connection, true)); bots.forEach(bot -> bot.renderBot(connection, true));
} }
@EventHandler @EventHandler
public void onDeath(EntityDeathEvent event) { public void onDeath(EntityDeathEvent event) {
LivingEntity bukkitEntity = event.getEntity(); LivingEntity bukkitEntity = event.getEntity();
Bot bot = getBot(bukkitEntity.getEntityId()); Terminator bot = getBot(bukkitEntity.getEntityId());
if (bot != null) { if (bot != null) {
agent.onBotDeath(new BotDeathEvent(event, bot)); agent.onBotDeath(new BotDeathEvent(event, bot));
} }

View File

@@ -0,0 +1,35 @@
package net.nuggetmc.tplus.bridge;
import net.minecraft.core.BlockPos;
import net.minecraft.network.protocol.game.ClientboundBlockDestructionPacket;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockState;
import net.nuggetmc.tplus.api.InternalBridge;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.craftbukkit.v1_18_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
public class InternalBridgeImpl implements InternalBridge {
@Override
public void sendBlockDestructionPacket(short entityId, int x, int y, int z, int progress) {
ClientboundBlockDestructionPacket crack = new ClientboundBlockDestructionPacket(entityId, new BlockPos(x, y, z), progress);
for (Player all : Bukkit.getOnlinePlayers()) {
((CraftPlayer) all).getHandle().connection.send(crack);
}
}
@Override
public Sound breakBlockSound(Block block) {
Level nmsWorld = ((CraftWorld) block.getWorld()).getHandle();
BlockState blockState = nmsWorld.getBlockState(new BlockPos(block.getX(), block.getY(), block.getZ()));
net.minecraft.world.level.block.Block nmsBlock = blockState.getBlock();
SoundType soundEffectType = nmsBlock.getSoundType(blockState);
return Sound.valueOf(soundEffectType.getBreakSound().getLocation().getPath().replace(".", "_").toUpperCase());
}
}

View File

@@ -2,13 +2,13 @@ package net.nuggetmc.tplus.command;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import net.nuggetmc.tplus.TerminatorPlus; import net.nuggetmc.tplus.TerminatorPlus;
import net.nuggetmc.tplus.api.utils.ChatUtils;
import net.nuggetmc.tplus.api.utils.DebugLogUtils;
import net.nuggetmc.tplus.command.annotation.Command; import net.nuggetmc.tplus.command.annotation.Command;
import net.nuggetmc.tplus.command.annotation.Require; import net.nuggetmc.tplus.command.annotation.Require;
import net.nuggetmc.tplus.command.commands.AICommand; import net.nuggetmc.tplus.command.commands.AICommand;
import net.nuggetmc.tplus.command.commands.BotCommand; import net.nuggetmc.tplus.command.commands.BotCommand;
import net.nuggetmc.tplus.command.commands.MainCommand; import net.nuggetmc.tplus.command.commands.MainCommand;
import net.nuggetmc.tplus.utils.ChatUtils;
import net.nuggetmc.tplus.utils.Debugger;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.SimpleCommandMap; import org.bukkit.command.SimpleCommandMap;
@@ -62,7 +62,7 @@ public class CommandHandler {
try { try {
method.setAccessible(true); method.setAccessible(true);
} catch (SecurityException e) { } catch (SecurityException e) {
Debugger.log("Failed to access method " + method.getName() + "."); DebugLogUtils.log("Failed to access method " + method.getName() + ".");
continue; continue;
} }

View File

@@ -1,13 +1,13 @@
package net.nuggetmc.tplus.command; package net.nuggetmc.tplus.command;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.nuggetmc.tplus.api.utils.ChatUtils;
import net.nuggetmc.tplus.command.annotation.Arg; import net.nuggetmc.tplus.command.annotation.Arg;
import net.nuggetmc.tplus.command.annotation.OptArg; import net.nuggetmc.tplus.command.annotation.OptArg;
import net.nuggetmc.tplus.command.annotation.TextArg; import net.nuggetmc.tplus.command.annotation.TextArg;
import net.nuggetmc.tplus.command.exception.ArgCountException; import net.nuggetmc.tplus.command.exception.ArgCountException;
import net.nuggetmc.tplus.command.exception.ArgParseException; import net.nuggetmc.tplus.command.exception.ArgParseException;
import net.nuggetmc.tplus.command.exception.NonPlayerException; import net.nuggetmc.tplus.command.exception.NonPlayerException;
import net.nuggetmc.tplus.utils.ChatUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand; import org.bukkit.command.defaults.BukkitCommand;

View File

@@ -1,18 +1,19 @@
package net.nuggetmc.tplus.command.commands; package net.nuggetmc.tplus.command.commands;
import net.nuggetmc.tplus.TerminatorPlus; import net.nuggetmc.tplus.TerminatorPlus;
import net.nuggetmc.tplus.bot.Bot; import net.nuggetmc.tplus.api.AIManager;
import net.nuggetmc.tplus.bot.BotManager; import net.nuggetmc.tplus.api.Terminator;
import net.nuggetmc.tplus.bot.agent.legacyagent.ai.IntelligenceAgent; import net.nuggetmc.tplus.api.agent.legacyagent.ai.IntelligenceAgent;
import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork; import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork;
import net.nuggetmc.tplus.api.utils.ChatUtils;
import net.nuggetmc.tplus.api.utils.MathUtils;
import net.nuggetmc.tplus.bot.BotManagerImpl;
import net.nuggetmc.tplus.command.CommandHandler; import net.nuggetmc.tplus.command.CommandHandler;
import net.nuggetmc.tplus.command.CommandInstance; import net.nuggetmc.tplus.command.CommandInstance;
import net.nuggetmc.tplus.command.annotation.Arg; import net.nuggetmc.tplus.command.annotation.Arg;
import net.nuggetmc.tplus.command.annotation.Autofill; import net.nuggetmc.tplus.command.annotation.Autofill;
import net.nuggetmc.tplus.command.annotation.Command; import net.nuggetmc.tplus.command.annotation.Command;
import net.nuggetmc.tplus.command.annotation.OptArg; import net.nuggetmc.tplus.command.annotation.OptArg;
import net.nuggetmc.tplus.utils.ChatUtils;
import net.nuggetmc.tplus.utils.MathUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@@ -22,7 +23,7 @@ import org.bukkit.scheduler.BukkitScheduler;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class AICommand extends CommandInstance { public class AICommand extends CommandInstance implements AIManager {
/* /*
* ideas * ideas
@@ -31,7 +32,7 @@ public class AICommand extends CommandInstance {
*/ */
private final TerminatorPlus plugin; private final TerminatorPlus plugin;
private final BotManager manager; private final BotManagerImpl manager;
private final BukkitScheduler scheduler; private final BukkitScheduler scheduler;
private IntelligenceAgent agent; private IntelligenceAgent agent;
@@ -69,7 +70,7 @@ public class AICommand extends CommandInstance {
sender.sendMessage("Starting a new session..."); sender.sendMessage("Starting a new session...");
agent = new IntelligenceAgent(this, populationSize, name, skin); agent = new IntelligenceAgent(this, populationSize, name, skin, plugin, plugin.getManager());
agent.addUser(sender); agent.addUser(sender);
} }
@@ -94,6 +95,7 @@ public class AICommand extends CommandInstance {
scheduler.runTaskLater(plugin, () -> sender.sendMessage("The session " + ChatColor.YELLOW + name + ChatColor.RESET + " has been closed."), 10); scheduler.runTaskLater(plugin, () -> sender.sendMessage("The session " + ChatColor.YELLOW + name + ChatColor.RESET + " has been closed."), 10);
} }
@Override
public void clearSession() { public void clearSession() {
if (agent != null) { if (agent != null) {
agent.stop(); agent.stop();
@@ -115,7 +117,7 @@ public class AICommand extends CommandInstance {
scheduler.runTaskAsynchronously(plugin, () -> { scheduler.runTaskAsynchronously(plugin, () -> {
try { try {
Bot bot = manager.getFirst(name); Terminator bot = manager.getFirst(name);
if (bot == null) { if (bot == null) {
sender.sendMessage("Could not find bot " + ChatColor.GREEN + name + ChatColor.RESET + "!"); sender.sendMessage("Could not find bot " + ChatColor.GREEN + name + ChatColor.RESET + "!");

View File

@@ -1,18 +1,17 @@
package net.nuggetmc.tplus.command.commands; package net.nuggetmc.tplus.command.commands;
import net.minecraft.world.entity.EquipmentSlot;
import net.nuggetmc.tplus.TerminatorPlus; import net.nuggetmc.tplus.TerminatorPlus;
import net.nuggetmc.tplus.bot.Bot; import net.nuggetmc.tplus.api.Terminator;
import net.nuggetmc.tplus.bot.BotManager; import net.nuggetmc.tplus.api.agent.legacyagent.EnumTargetGoal;
import net.nuggetmc.tplus.bot.agent.legacyagent.EnumTargetGoal; import net.nuggetmc.tplus.api.agent.legacyagent.LegacyAgent;
import net.nuggetmc.tplus.bot.agent.legacyagent.LegacyAgent; import net.nuggetmc.tplus.api.utils.ChatUtils;
import net.nuggetmc.tplus.bot.BotManagerImpl;
import net.nuggetmc.tplus.command.CommandHandler; import net.nuggetmc.tplus.command.CommandHandler;
import net.nuggetmc.tplus.command.CommandInstance; import net.nuggetmc.tplus.command.CommandInstance;
import net.nuggetmc.tplus.command.annotation.Arg; import net.nuggetmc.tplus.command.annotation.Arg;
import net.nuggetmc.tplus.command.annotation.Autofill; import net.nuggetmc.tplus.command.annotation.Autofill;
import net.nuggetmc.tplus.command.annotation.Command; import net.nuggetmc.tplus.command.annotation.Command;
import net.nuggetmc.tplus.command.annotation.OptArg; import net.nuggetmc.tplus.command.annotation.OptArg;
import net.nuggetmc.tplus.utils.ChatUtils;
import net.nuggetmc.tplus.utils.Debugger; import net.nuggetmc.tplus.utils.Debugger;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@@ -20,6 +19,7 @@ import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
@@ -31,11 +31,11 @@ public class BotCommand extends CommandInstance {
private final TerminatorPlus plugin; private final TerminatorPlus plugin;
private final CommandHandler handler; private final CommandHandler handler;
private final BotManager manager; private final BotManagerImpl manager;
private final LegacyAgent agent; private final LegacyAgent agent;
private final BukkitScheduler scheduler; private final BukkitScheduler scheduler;
private final DecimalFormat formatter; private final DecimalFormat formatter;
private final Map<String, ItemStack[]> armorTiers;
private AICommand aiManager; private AICommand aiManager;
public BotCommand(CommandHandler handler, String name, String description, String... aliases) { public BotCommand(CommandHandler handler, String name, String description, String... aliases) {
@@ -58,24 +58,24 @@ public class BotCommand extends CommandInstance {
} }
@Command( @Command(
name = "create", name = "create",
desc = "Create a bot." desc = "Create a bot."
) )
public void create(Player sender, @Arg("name") String name, @OptArg("skin") String skin) { public void create(Player sender, @Arg("name") String name, @OptArg("skin") String skin) {
manager.createBots(sender, name, skin, 1); manager.createBots(sender, name, skin, 1);
} }
@Command( @Command(
name = "multi", name = "multi",
desc = "Create multiple bots at once." desc = "Create multiple bots at once."
) )
public void multi(Player sender, @Arg("amount") int amount, @Arg("name") String name, @OptArg("skin") String skin) { public void multi(Player sender, @Arg("amount") int amount, @Arg("name") String name, @OptArg("skin") String skin) {
manager.createBots(sender, name, skin, amount); manager.createBots(sender, name, skin, amount);
} }
@Command( @Command(
name = "give", name = "give",
desc = "Gives a specified item to all bots." desc = "Gives a specified item to all bots."
) )
public void give(CommandSender sender, @Arg("item-name") String itemName) { public void give(CommandSender sender, @Arg("item-name") String itemName) {
Material type = Material.matchMaterial(itemName); Material type = Material.matchMaterial(itemName);
@@ -92,56 +92,54 @@ public class BotCommand extends CommandInstance {
sender.sendMessage("Successfully set the default item to " + ChatColor.YELLOW + item.getType() + ChatColor.RESET + " for all current bots."); sender.sendMessage("Successfully set the default item to " + ChatColor.YELLOW + item.getType() + ChatColor.RESET + " for all current bots.");
} }
private final Map<String, ItemStack[]> armorTiers;
private void armorTierSetup() { private void armorTierSetup() {
armorTiers.put("leather", new ItemStack[] { armorTiers.put("leather", new ItemStack[]{
new ItemStack(Material.LEATHER_BOOTS), new ItemStack(Material.LEATHER_BOOTS),
new ItemStack(Material.LEATHER_LEGGINGS), new ItemStack(Material.LEATHER_LEGGINGS),
new ItemStack(Material.LEATHER_CHESTPLATE), new ItemStack(Material.LEATHER_CHESTPLATE),
new ItemStack(Material.LEATHER_HELMET), new ItemStack(Material.LEATHER_HELMET),
}); });
armorTiers.put("chain", new ItemStack[] { armorTiers.put("chain", new ItemStack[]{
new ItemStack(Material.CHAINMAIL_BOOTS), new ItemStack(Material.CHAINMAIL_BOOTS),
new ItemStack(Material.CHAINMAIL_LEGGINGS), new ItemStack(Material.CHAINMAIL_LEGGINGS),
new ItemStack(Material.CHAINMAIL_CHESTPLATE), new ItemStack(Material.CHAINMAIL_CHESTPLATE),
new ItemStack(Material.CHAINMAIL_HELMET), new ItemStack(Material.CHAINMAIL_HELMET),
}); });
armorTiers.put("gold", new ItemStack[] { armorTiers.put("gold", new ItemStack[]{
new ItemStack(Material.GOLDEN_BOOTS), new ItemStack(Material.GOLDEN_BOOTS),
new ItemStack(Material.GOLDEN_LEGGINGS), new ItemStack(Material.GOLDEN_LEGGINGS),
new ItemStack(Material.GOLDEN_CHESTPLATE), new ItemStack(Material.GOLDEN_CHESTPLATE),
new ItemStack(Material.GOLDEN_HELMET), new ItemStack(Material.GOLDEN_HELMET),
}); });
armorTiers.put("iron", new ItemStack[] { armorTiers.put("iron", new ItemStack[]{
new ItemStack(Material.IRON_BOOTS), new ItemStack(Material.IRON_BOOTS),
new ItemStack(Material.IRON_LEGGINGS), new ItemStack(Material.IRON_LEGGINGS),
new ItemStack(Material.IRON_CHESTPLATE), new ItemStack(Material.IRON_CHESTPLATE),
new ItemStack(Material.IRON_HELMET), new ItemStack(Material.IRON_HELMET),
}); });
armorTiers.put("diamond", new ItemStack[] { armorTiers.put("diamond", new ItemStack[]{
new ItemStack(Material.DIAMOND_BOOTS), new ItemStack(Material.DIAMOND_BOOTS),
new ItemStack(Material.DIAMOND_LEGGINGS), new ItemStack(Material.DIAMOND_LEGGINGS),
new ItemStack(Material.DIAMOND_CHESTPLATE), new ItemStack(Material.DIAMOND_CHESTPLATE),
new ItemStack(Material.DIAMOND_HELMET), new ItemStack(Material.DIAMOND_HELMET),
}); });
armorTiers.put("netherite", new ItemStack[] { armorTiers.put("netherite", new ItemStack[]{
new ItemStack(Material.NETHERITE_BOOTS), new ItemStack(Material.NETHERITE_BOOTS),
new ItemStack(Material.NETHERITE_LEGGINGS), new ItemStack(Material.NETHERITE_LEGGINGS),
new ItemStack(Material.NETHERITE_CHESTPLATE), new ItemStack(Material.NETHERITE_CHESTPLATE),
new ItemStack(Material.NETHERITE_HELMET), new ItemStack(Material.NETHERITE_HELMET),
}); });
} }
@Command( @Command(
name = "armor", name = "armor",
desc = "Gives all bots an armor set.", desc = "Gives all bots an armor set.",
autofill = "armorAutofill" autofill = "armorAutofill"
) )
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void armor(CommandSender sender, @Arg("armor-tier") String armorTier) { public void armor(CommandSender sender, @Arg("armor-tier") String armorTier) {
@@ -156,14 +154,17 @@ public class BotCommand extends CommandInstance {
ItemStack[] armor = armorTiers.get(tier); ItemStack[] armor = armorTiers.get(tier);
manager.fetch().forEach(bot -> { manager.fetch().forEach(bot -> {
bot.getBukkitEntity().getInventory().setArmorContents(armor); if (bot.getBukkitEntity() instanceof Player) {
bot.getBukkitEntity().updateInventory(); Player botPlayer = (Player) bot.getBukkitEntity();
botPlayer.getInventory().setArmorContents(armor);
botPlayer.updateInventory();
// packet sending to ensure // packet sending to ensure
bot.setItem(armor[0], EquipmentSlot.FEET); bot.setItem(armor[0], EquipmentSlot.FEET);
bot.setItem(armor[1], EquipmentSlot.LEGS); bot.setItem(armor[1], EquipmentSlot.LEGS);
bot.setItem(armor[2], EquipmentSlot.CHEST); bot.setItem(armor[2], EquipmentSlot.CHEST);
bot.setItem(armor[3], EquipmentSlot.HEAD); bot.setItem(armor[3], EquipmentSlot.HEAD);
}
}); });
sender.sendMessage("Successfully set the armor tier to " + ChatColor.YELLOW + tier + ChatColor.RESET + " for all current bots."); sender.sendMessage("Successfully set the armor tier to " + ChatColor.YELLOW + tier + ChatColor.RESET + " for all current bots.");
@@ -175,9 +176,9 @@ public class BotCommand extends CommandInstance {
} }
@Command( @Command(
name = "info", name = "info",
desc = "Information about loaded bots.", desc = "Information about loaded bots.",
autofill = "infoAutofill" autofill = "infoAutofill"
) )
public void info(CommandSender sender, @Arg("bot-name") String name) { public void info(CommandSender sender, @Arg("bot-name") String name) {
if (name == null) { if (name == null) {
@@ -189,7 +190,7 @@ public class BotCommand extends CommandInstance {
scheduler.runTaskAsynchronously(plugin, () -> { scheduler.runTaskAsynchronously(plugin, () -> {
try { try {
Bot bot = manager.getFirst(name); Terminator bot = manager.getFirst(name);
if (bot == null) { if (bot == null) {
sender.sendMessage("Could not find bot " + ChatColor.GREEN + name + ChatColor.RESET + "!"); sender.sendMessage("Could not find bot " + ChatColor.GREEN + name + ChatColor.RESET + "!");
@@ -207,7 +208,7 @@ public class BotCommand extends CommandInstance {
* neural network values (network name if loaded, otherwise RANDOM) * neural network values (network name if loaded, otherwise RANDOM)
*/ */
String botName = bot.getName().getString(); String botName = bot.getBotName();
String world = ChatColor.YELLOW + bot.getBukkitEntity().getWorld().getName(); String world = ChatColor.YELLOW + bot.getBukkitEntity().getWorld().getName();
Location loc = bot.getLocation(); Location loc = bot.getLocation();
String strLoc = ChatColor.YELLOW + formatter.format(loc.getBlockX()) + ", " + formatter.format(loc.getBlockY()) + ", " + formatter.format(loc.getBlockZ()); String strLoc = ChatColor.YELLOW + formatter.format(loc.getBlockX()) + ", " + formatter.format(loc.getBlockY()) + ", " + formatter.format(loc.getBlockZ());
@@ -220,9 +221,7 @@ public class BotCommand extends CommandInstance {
sender.sendMessage(ChatUtils.BULLET_FORMATTED + "Position: " + strLoc); sender.sendMessage(ChatUtils.BULLET_FORMATTED + "Position: " + strLoc);
sender.sendMessage(ChatUtils.BULLET_FORMATTED + "Velocity: " + strVel); sender.sendMessage(ChatUtils.BULLET_FORMATTED + "Velocity: " + strVel);
sender.sendMessage(ChatUtils.LINE); sender.sendMessage(ChatUtils.LINE);
} } catch (Exception e) {
catch (Exception e) {
sender.sendMessage(ChatUtils.EXCEPTION_MESSAGE); sender.sendMessage(ChatUtils.EXCEPTION_MESSAGE);
} }
}); });
@@ -234,8 +233,8 @@ public class BotCommand extends CommandInstance {
} }
@Command( @Command(
name = "reset", name = "reset",
desc = "Remove all loaded bots." desc = "Remove all loaded bots."
) )
public void reset(CommandSender sender) { public void reset(CommandSender sender) {
sender.sendMessage("Removing every bot..."); sender.sendMessage("Removing every bot...");
@@ -257,10 +256,10 @@ public class BotCommand extends CommandInstance {
* basically, in the @Command annotation, you can include a "parent" for the command, so it will be a subcommand under the specified parent * basically, in the @Command annotation, you can include a "parent" for the command, so it will be a subcommand under the specified parent
*/ */
@Command( @Command(
name = "settings", name = "settings",
desc = "Make changes to the global configuration file and bot-specific settings.", desc = "Make changes to the global configuration file and bot-specific settings.",
aliases = "options", aliases = "options",
autofill = "settingsAutofill" autofill = "settingsAutofill"
) )
public void settings(CommandSender sender, List<String> args) { public void settings(CommandSender sender, List<String> args) {
String arg1 = args.isEmpty() ? null : args.get(0); String arg1 = args.isEmpty() ? null : args.get(0);
@@ -307,9 +306,7 @@ public class BotCommand extends CommandInstance {
if (args.length == 2) { if (args.length == 2) {
output.add("setgoal"); output.add("setgoal");
} } else if (args.length == 3) {
else if (args.length == 3) {
if (args[1].equalsIgnoreCase("setgoal")) { if (args[1].equalsIgnoreCase("setgoal")) {
Arrays.stream(EnumTargetGoal.values()).forEach(goal -> output.add(goal.name().replace("_", "").toLowerCase())); Arrays.stream(EnumTargetGoal.values()).forEach(goal -> output.add(goal.name().replace("_", "").toLowerCase()));
} }
@@ -319,9 +316,9 @@ public class BotCommand extends CommandInstance {
} }
@Command( @Command(
name = "debug", name = "debug",
desc = "Debug plugin code.", desc = "Debug plugin code.",
visible = false visible = false
) )
public void debug(CommandSender sender, @Arg("expression") String expression) { public void debug(CommandSender sender, @Arg("expression") String expression) {
new Debugger(sender).execute(expression); new Debugger(sender).execute(expression);

View File

@@ -6,10 +6,10 @@ import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.hover.content.Text; import net.md_5.bungee.api.chat.hover.content.Text;
import net.nuggetmc.tplus.TerminatorPlus; import net.nuggetmc.tplus.TerminatorPlus;
import net.nuggetmc.tplus.api.utils.ChatUtils;
import net.nuggetmc.tplus.command.CommandHandler; import net.nuggetmc.tplus.command.CommandHandler;
import net.nuggetmc.tplus.command.CommandInstance; import net.nuggetmc.tplus.command.CommandInstance;
import net.nuggetmc.tplus.command.annotation.Command; import net.nuggetmc.tplus.command.annotation.Command;
import net.nuggetmc.tplus.utils.ChatUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;

View File

@@ -1,12 +1,16 @@
package net.nuggetmc.tplus.utils; package net.nuggetmc.tplus.utils;
import net.minecraft.world.entity.LivingEntity;
import net.nuggetmc.tplus.TerminatorPlus; import net.nuggetmc.tplus.TerminatorPlus;
import net.nuggetmc.tplus.bot.Bot; import net.nuggetmc.tplus.api.Terminator;
import net.nuggetmc.tplus.bot.agent.Agent; import net.nuggetmc.tplus.api.agent.Agent;
import net.nuggetmc.tplus.bot.agent.legacyagent.LegacyAgent; import net.nuggetmc.tplus.api.agent.legacyagent.LegacyAgent;
import net.nuggetmc.tplus.bot.agent.legacyagent.ai.IntelligenceAgent; import net.nuggetmc.tplus.api.agent.legacyagent.ai.IntelligenceAgent;
import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork; import net.nuggetmc.tplus.api.agent.legacyagent.ai.NeuralNetwork;
import net.nuggetmc.tplus.api.utils.DebugLogUtils;
import net.nuggetmc.tplus.api.utils.MathUtils;
import net.nuggetmc.tplus.api.utils.MojangAPI;
import net.nuggetmc.tplus.api.utils.PlayerUtils;
import net.nuggetmc.tplus.bot.Bot;
import net.nuggetmc.tplus.command.commands.AICommand; import net.nuggetmc.tplus.command.commands.AICommand;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@@ -23,28 +27,15 @@ import java.util.stream.Collectors;
public class Debugger { public class Debugger {
private static final String PREFIX = ChatColor.YELLOW + "[DEBUG] " + ChatColor.RESET;
private final CommandSender sender; private final CommandSender sender;
public Debugger(CommandSender sender) { public Debugger(CommandSender sender) {
this.sender = sender; this.sender = sender;
} }
public static void log(Object... objects) {
String[] values = formStringArray(objects);
String message = PREFIX + String.join(" ", values);
Bukkit.getConsoleSender().sendMessage(message);
Bukkit.getOnlinePlayers().stream().filter(ServerOperator::isOp).forEach(p -> p.sendMessage(message));
}
private static String[] formStringArray(Object[] objects) {
return Arrays.stream(objects).map(String::valueOf).toArray(String[]::new);
}
private void print(Object... objects) { private void print(Object... objects) {
sender.sendMessage(PREFIX + String.join(" ", formStringArray(objects))); sender.sendMessage(DebugLogUtils.PREFIX + String.join(" ", DebugLogUtils.fromStringArray(objects)));
} }
public void execute(String cmd) { public void execute(String cmd) {
@@ -60,9 +51,7 @@ public class Debugger {
Statement statement = new Statement(this, name, args); Statement statement = new Statement(this, name, args);
print("Running the expression \"" + ChatColor.AQUA + cmd + ChatColor.RESET + "\"..."); print("Running the expression \"" + ChatColor.AQUA + cmd + ChatColor.RESET + "\"...");
statement.execute(); statement.execute();
} } catch (Exception e) {
catch (Exception e) {
print("Error: the expression \"" + ChatColor.AQUA + cmd + ChatColor.RESET + "\" failed to execute."); print("Error: the expression \"" + ChatColor.AQUA + cmd + ChatColor.RESET + "\" failed to execute.");
print(e.toString()); print(e.toString());
} }
@@ -80,11 +69,13 @@ public class Debugger {
try { try {
obj = Double.parseDouble(value); obj = Double.parseDouble(value);
} catch (NumberFormatException ignored) { } } catch (NumberFormatException ignored) {
}
try { try {
obj = Integer.parseInt(value); obj = Integer.parseInt(value);
} catch (NumberFormatException ignored) { } } catch (NumberFormatException ignored) {
}
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) { if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
obj = Boolean.parseBoolean(value); obj = Boolean.parseBoolean(value);
@@ -222,7 +213,7 @@ public class Debugger {
public void tpall() { public void tpall() {
Player player = (Player) sender; Player player = (Player) sender;
TerminatorPlus.getInstance().getManager().fetch().stream().filter(LivingEntity::isAlive).forEach(bot -> bot.getBukkitEntity().teleport(player)); TerminatorPlus.getInstance().getManager().fetch().stream().filter(Terminator::isAlive).forEach(bot -> bot.getBukkitEntity().teleport(player));
} }
public void viewsession() { public void viewsession() {
@@ -244,9 +235,7 @@ public class Debugger {
LegacyAgent legacyAgent = (LegacyAgent) agent; LegacyAgent legacyAgent = (LegacyAgent) agent;
legacyAgent.offsets = b; legacyAgent.offsets = b;
print("Bot target offsets are now " print("Bot target offsets are now " + (legacyAgent.offsets ? ChatColor.GREEN + "ENABLED" : ChatColor.RED + "DISABLED") + ChatColor.RESET + ".");
+ (legacyAgent.offsets ? ChatColor.GREEN + "ENABLED" : ChatColor.RED + "DISABLED")
+ ChatColor.RESET + ".");
} }
public void confuse(int n) { public void confuse(int n) {
@@ -269,12 +258,7 @@ public class Debugger {
} }
public void dreamsmp() { public void dreamsmp() {
spawnBots(Arrays.asList( spawnBots(Arrays.asList("Dream", "GeorgeNotFound", "Callahan", "Sapnap", "awesamdude", "Ponk", "BadBoyHalo", "TommyInnit", "Tubbo_", "ItsFundy", "Punz", "Purpled", "WilburSoot", "Jschlatt", "Skeppy", "The_Eret", "JackManifoldTV", "Nihachu", "Quackity", "KarlJacobs", "HBomb94", "Technoblade", "Antfrost", "Ph1LzA", "ConnorEatsPants", "CaptainPuffy", "Vikkstar123", "LazarCodeLazar", "Ranboo", "FoolishG", "hannahxxrose", "Slimecicle", "Michaelmcchill"));
"Dream", "GeorgeNotFound", "Callahan", "Sapnap", "awesamdude", "Ponk", "BadBoyHalo", "TommyInnit", "Tubbo_", "ItsFundy", "Punz",
"Purpled", "WilburSoot", "Jschlatt", "Skeppy", "The_Eret", "JackManifoldTV", "Nihachu", "Quackity", "KarlJacobs", "HBomb94",
"Technoblade", "Antfrost", "Ph1LzA", "ConnorEatsPants", "CaptainPuffy", "Vikkstar123", "LazarCodeLazar", "Ranboo", "FoolishG",
"hannahxxrose", "Slimecicle", "Michaelmcchill"
));
} }
private void spawnBots(List<String> players) { private void spawnBots(List<String> players) {
@@ -319,9 +303,7 @@ public class Debugger {
print("Done."); print("Done.");
}); });
} } catch (Exception e) {
catch (Exception e) {
print(e); print(e);
} }
}); });
@@ -352,14 +334,14 @@ public class Debugger {
} }
public void tp() { public void tp() {
Bot bot = MathUtils.getRandomSetElement(TerminatorPlus.getInstance().getManager().fetch().stream().filter(LivingEntity::isAlive).collect(Collectors.toSet())); Terminator bot = MathUtils.getRandomSetElement(TerminatorPlus.getInstance().getManager().fetch().stream().filter(Terminator::isAlive).collect(Collectors.toSet()));
if (bot == null) { if (bot == null) {
print("Failed to locate a bot."); print("Failed to locate a bot.");
return; return;
} }
print("Located bot", (ChatColor.GREEN.toString() + bot.getName() + ChatColor.RESET.toString() + ".")); print("Located bot", (ChatColor.GREEN + bot.getBotName() + ChatColor.RESET + "."));
if (sender instanceof Player) { if (sender instanceof Player) {
print("Teleporting..."); print("Teleporting...");
@@ -386,9 +368,9 @@ public class Debugger {
} }
public void hideNametags() { // this works for some reason public void hideNametags() { // this works for some reason
Set<Bot> bots = TerminatorPlus.getInstance().getManager().fetch(); Set<Terminator> bots = TerminatorPlus.getInstance().getManager().fetch();
for (Bot bot : bots) { for (Terminator bot : bots) {
Location loc = bot.getLocation(); Location loc = bot.getLocation();
World world = loc.getWorld(); World world = loc.getWorld();
@@ -409,9 +391,9 @@ public class Debugger {
} }
public void sit() { public void sit() {
Set<Bot> bots = TerminatorPlus.getInstance().getManager().fetch(); Set<Terminator> bots = TerminatorPlus.getInstance().getManager().fetch();
for (Bot bot : bots) { for (Terminator bot : bots) {
Location loc = bot.getLocation(); Location loc = bot.getLocation();
World world = loc.getWorld(); World world = loc.getWorld();
@@ -440,7 +422,7 @@ public class Debugger {
Player player = (Player) sender; Player player = (Player) sender;
for (Bot bot : TerminatorPlus.getInstance().getManager().fetch()) { for (Terminator bot : TerminatorPlus.getInstance().getManager().fetch()) {
bot.faceLocation(player.getEyeLocation()); bot.faceLocation(player.getEyeLocation());
} }
} }
@@ -451,8 +433,6 @@ public class Debugger {
boolean b = agent.isEnabled(); boolean b = agent.isEnabled();
agent.setEnabled(!b); agent.setEnabled(!b);
print("The Bot Agent is now " print("The Bot Agent is now " + (b ? ChatColor.RED + "DISABLED" : ChatColor.GREEN + "ENABLED") + ChatColor.RESET + ".");
+ (b ? ChatColor.RED + "DISABLED" : ChatColor.GREEN + "ENABLED")
+ ChatColor.RESET + ".");
} }
} }