From 94da2ac11bfc8285d1de8e04ae5de16f8945c70f Mon Sep 17 00:00:00 2001 From: batchprogrammer314 Date: Mon, 12 Jul 2021 18:20:17 -0500 Subject: [PATCH] Agent migration from 2.0 (legacy), basic velocities and maneuvering. --- src/main/java/net/nuggetmc/ai/PlayerAI.java | 2 +- src/main/java/net/nuggetmc/ai/bot/Bot.java | 77 +- .../java/net/nuggetmc/ai/bot/BotManager.java | 18 +- .../java/net/nuggetmc/ai/bot/BotUtils.java | 29 + .../java/net/nuggetmc/ai/bot/agent/Agent.java | 55 ++ .../ai/bot/agent/{ => botagent}/BotAgent.java | 47 +- .../agent/{ => botagent}/BotSituation.java | 2 +- .../{ => botagent}/VerticalDisplacement.java | 2 +- .../ai/bot/agent/legacyagent/LegacyAgent.java | 777 ++++++++++++++++++ .../agent/legacyagent/LegacyBlockCheck.java | 185 +++++ .../ai/bot/agent/legacyagent/LegacyItems.java | 9 + .../ai/bot/agent/legacyagent/LegacyLevel.java | 35 + .../ai/bot/agent/legacyagent/LegacyMats.java | 156 ++++ .../ai/bot/agent/legacyagent/LegacyUtils.java | 54 ++ .../agent/legacyagent/LegacyWorldManager.java | 19 + .../java/net/nuggetmc/ai/utils/Debugger.java | 4 +- 16 files changed, 1407 insertions(+), 64 deletions(-) create mode 100644 src/main/java/net/nuggetmc/ai/bot/BotUtils.java create mode 100644 src/main/java/net/nuggetmc/ai/bot/agent/Agent.java rename src/main/java/net/nuggetmc/ai/bot/agent/{ => botagent}/BotAgent.java (83%) rename src/main/java/net/nuggetmc/ai/bot/agent/{ => botagent}/BotSituation.java (91%) rename src/main/java/net/nuggetmc/ai/bot/agent/{ => botagent}/VerticalDisplacement.java (86%) create mode 100644 src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyAgent.java create mode 100644 src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyBlockCheck.java create mode 100644 src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyItems.java create mode 100644 src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyLevel.java create mode 100644 src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyMats.java create mode 100644 src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyUtils.java create mode 100644 src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyWorldManager.java diff --git a/src/main/java/net/nuggetmc/ai/PlayerAI.java b/src/main/java/net/nuggetmc/ai/PlayerAI.java index 7a70a9e..d1eff27 100644 --- a/src/main/java/net/nuggetmc/ai/PlayerAI.java +++ b/src/main/java/net/nuggetmc/ai/PlayerAI.java @@ -30,7 +30,7 @@ public class PlayerAI extends JavaPlugin { instance = this; // Create Instances - this.manager = new BotManager(this); + this.manager = new BotManager(); this.handler = new CommandHandler(this); // Register all the things diff --git a/src/main/java/net/nuggetmc/ai/bot/Bot.java b/src/main/java/net/nuggetmc/ai/bot/Bot.java index 6641231..1d38ee3 100644 --- a/src/main/java/net/nuggetmc/ai/bot/Bot.java +++ b/src/main/java/net/nuggetmc/ai/bot/Bot.java @@ -169,23 +169,48 @@ public class Bot extends EntityPlayer { } private void updateLocation() { - // Eventually there will be a whole algorithm here to slow a player down to a certain velocity depending on the liquid a player is in - double y; - if (groundTicks != 0) { - velocity.setY(0); - addFriction(); - y = 0; - } else { - y = velocity.getY(); + // Check to reset y velocity if staying in the same position + + if (isInWater()) { + y = Math.min(velocity.getY() + 0.1, 0.1); + addFriction(0.8); + velocity.setY(y); } - velocity.setY(Math.max(velocity.getY() - 0.1, -3.5)); + else { + if (groundTicks != 0) { + velocity.setY(0); + addFriction(0.5); + y = 0; + } else { + y = velocity.getY(); + } + + velocity.setY(Math.max(velocity.getY() - 0.1, -3.5)); + } this.move(EnumMoveType.SELF, new Vec3D(velocity.getX(), y, velocity.getZ())); } + @Override + public boolean isInWater() { + Location loc = getLocation(); + + for (int i = 0; i <= 2; i++) { + Material type = loc.getBlock().getType(); + + if (type == Material.WATER || type == Material.LAVA) { + return true; + } + + loc.add(0, 0.9, 0); + } + + return false; + } + public void jump(Vector vel) { if (jumpTicks == 0 && groundTicks > 1) { jumpTicks = 4; @@ -208,7 +233,6 @@ public class Bot extends EntityPlayer { } public boolean checkGround() { - double k = 0.01; double vy = velocity.getY(); if (vy > 0) { @@ -219,18 +243,21 @@ public class Bot extends EntityPlayer { AxisAlignedBB box = getBoundingBox(); double[] xVals = new double[] { - box.minX + k, - box.maxX - k + box.minX, + box.maxX }; double[] zVals = new double[] { - box.minZ + k, - box.maxZ - k + box.minZ, + box.maxZ }; for (double x : xVals) { for (double z : zVals) { - if (world.getBlockAt(new Location(world, x, locY() - 0.01, z)).getType().isSolid()) { + Location loc = new Location(world, x, locY() - 0.01, z); + Block block = world.getBlockAt(loc); + + if (block.getType().isSolid() && BotUtils.solidAt(loc)) { return true; } } @@ -244,14 +271,14 @@ public class Bot extends EntityPlayer { return groundTicks != 0; } - public void addFriction() { + public void addFriction(double factor) { double frictionMin = 0.01; double x = velocity.getX(); double z = velocity.getZ(); - velocity.setX(Math.abs(x) < frictionMin ? 0 : x * 0.5); - velocity.setZ(Math.abs(z) < frictionMin ? 0 : z * 0.5); + velocity.setX(Math.abs(x) < frictionMin ? 0 : x * factor); + velocity.setZ(Math.abs(z) < frictionMin ? 0 : z * factor); } public void despawn() { @@ -352,12 +379,16 @@ public class Bot extends EntityPlayer { float[] vals = MathUtils.fetchYawPitch(dir); yaw = vals[0]; pitch = vals[1]; + + PacketPlayOutEntityHeadRotation packet = new PacketPlayOutEntityHeadRotation(getBukkitEntity().getHandle(), (byte) (yaw * 256 / 360f)); + Bukkit.getOnlinePlayers().forEach(p -> ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet)); } setYawPitch(yaw, pitch); - PacketPlayOutEntityHeadRotation packet = new PacketPlayOutEntityHeadRotation(getBukkitEntity().getHandle(), (byte) (yaw * 256 / 360f)); - Bukkit.getOnlinePlayers().forEach(p -> ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet)); + // this causes a lot of lag lol + //PacketPlayOutEntity.PacketPlayOutEntityLook packet = new PacketPlayOutEntity.PacketPlayOutEntityLook(getId(), (byte) (yaw * 256 / 360f), (byte) (pitch * 256 / 360f), isOnGround()); + //Bukkit.getOnlinePlayers().forEach(p -> ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet)); } public void attemptBlockPlace(Location loc, Material type) { @@ -387,14 +418,20 @@ public class Bot extends EntityPlayer { } public void swim() { + getBukkitEntity().setSwimming(true); registerPose(EntityPose.SWIMMING); } public void sneak() { + getBukkitEntity().setSneaking(true); registerPose(EntityPose.CROUCHING); } public void stand() { + Player player = getBukkitEntity(); + player.setSneaking(false); + player.setSwimming(false); + registerPose(EntityPose.STANDING); } diff --git a/src/main/java/net/nuggetmc/ai/bot/BotManager.java b/src/main/java/net/nuggetmc/ai/bot/BotManager.java index 7ca28a1..e0cd2c9 100644 --- a/src/main/java/net/nuggetmc/ai/bot/BotManager.java +++ b/src/main/java/net/nuggetmc/ai/bot/BotManager.java @@ -1,8 +1,8 @@ package net.nuggetmc.ai.bot; import net.minecraft.server.v1_16_R3.PlayerConnection; -import net.nuggetmc.ai.PlayerAI; -import net.nuggetmc.ai.bot.agent.BotAgent; +import net.nuggetmc.ai.bot.agent.Agent; +import net.nuggetmc.ai.bot.agent.legacyagent.LegacyAgent; import net.nuggetmc.ai.utils.MojangAPI; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -22,15 +22,13 @@ import java.util.Set; public class BotManager implements Listener { - private final PlayerAI plugin; - private final BotAgent agent; + private final Agent agent; private final NumberFormat numberFormat; private final Set bots = new HashSet<>(); - public BotManager(PlayerAI plugin) { - this.plugin = plugin; - this.agent = new BotAgent(this); + public BotManager() { + this.agent = new LegacyAgent(this); this.numberFormat = NumberFormat.getInstance(Locale.US); } @@ -42,7 +40,7 @@ public class BotManager implements Listener { bots.add(bot); } - public BotAgent getAgent() { + public Agent getAgent() { return agent; } @@ -78,6 +76,10 @@ public class BotManager implements Listener { sender.sendMessage("Process completed (" + ChatColor.RED + ((System.currentTimeMillis() - timestamp) / 1000D) + "s" + ChatColor.RESET + ")."); } + public void remove(Bot bot) { + bots.remove(bot); + } + public void reset() { bots.forEach(Bot::remove); bots.clear(); diff --git a/src/main/java/net/nuggetmc/ai/bot/BotUtils.java b/src/main/java/net/nuggetmc/ai/bot/BotUtils.java new file mode 100644 index 0000000..d732c33 --- /dev/null +++ b/src/main/java/net/nuggetmc/ai/bot/BotUtils.java @@ -0,0 +1,29 @@ +package net.nuggetmc.ai.bot; + +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.util.BoundingBox; +import org.bukkit.util.Vector; + +public class BotUtils { + + public static boolean solidAt(Location loc) { // not perfect, still cuts corners of fences + Block block = loc.getBlock(); + BoundingBox box = block.getBoundingBox(); + Vector position = loc.toVector(); + + double x = position.getX(); + double y = position.getY(); + double z = position.getZ(); + + double minX = box.getMinX(); + double minY = box.getMinY(); + double minZ = box.getMinZ(); + + double maxX = box.getMaxX(); + double maxY = box.getMaxY(); + double maxZ = box.getMaxZ(); + + return x > minX && x < maxX && y > minY && y < maxY && z > minZ && z < maxZ; + } +} diff --git a/src/main/java/net/nuggetmc/ai/bot/agent/Agent.java b/src/main/java/net/nuggetmc/ai/bot/agent/Agent.java new file mode 100644 index 0000000..b8fcc26 --- /dev/null +++ b/src/main/java/net/nuggetmc/ai/bot/agent/Agent.java @@ -0,0 +1,55 @@ +package net.nuggetmc.ai.bot.agent; + +import net.nuggetmc.ai.PlayerAI; +import net.nuggetmc.ai.bot.BotManager; +import org.bukkit.Bukkit; +import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.scheduler.BukkitScheduler; + +import java.util.HashSet; +import java.util.Random; +import java.util.Set; + +public abstract class Agent { + + protected final PlayerAI plugin; + protected final BotManager manager; + protected final BukkitScheduler scheduler; + protected final Set taskList; + protected final Random random; + + protected boolean enabled; + protected int taskID; + + public Agent(BotManager manager) { + this.plugin = PlayerAI.getInstance(); + this.manager = manager; + this.scheduler = Bukkit.getScheduler(); + this.taskList = new HashSet<>(); + this.random = new Random(); + + setEnabled(true); + } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean b) { + enabled = b; + + if (b) { + taskID = scheduler.scheduleSyncRepeatingTask(plugin, this::tick, 0, 1); + } else { + scheduler.cancelTask(taskID); + taskList.forEach(t -> { + if (!t.isCancelled()) { + t.cancel(); + } + }); + taskList.clear(); + } + } + + protected abstract void tick(); +} diff --git a/src/main/java/net/nuggetmc/ai/bot/agent/BotAgent.java b/src/main/java/net/nuggetmc/ai/bot/agent/botagent/BotAgent.java similarity index 83% rename from src/main/java/net/nuggetmc/ai/bot/agent/BotAgent.java rename to src/main/java/net/nuggetmc/ai/bot/agent/botagent/BotAgent.java index e73f146..1542222 100644 --- a/src/main/java/net/nuggetmc/ai/bot/agent/BotAgent.java +++ b/src/main/java/net/nuggetmc/ai/bot/agent/botagent/BotAgent.java @@ -1,51 +1,27 @@ -package net.nuggetmc.ai.bot.agent; +package net.nuggetmc.ai.bot.agent.botagent; -import net.nuggetmc.ai.PlayerAI; import net.nuggetmc.ai.bot.Bot; import net.nuggetmc.ai.bot.BotManager; +import net.nuggetmc.ai.bot.agent.Agent; import net.nuggetmc.ai.utils.MathUtils; import net.nuggetmc.ai.utils.PlayerUtils; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.util.Vector; import java.util.Set; -public class BotAgent { +public class BotAgent extends Agent { - private final PlayerAI plugin; - private final BotManager manager; - private final BukkitScheduler scheduler; - - private boolean enabled; - private int taskID; private int count; public BotAgent(BotManager manager) { - this.plugin = PlayerAI.getInstance(); - this.manager = manager; - this.scheduler = Bukkit.getScheduler(); - - setEnabled(true); + super(manager); } - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean b) { - enabled = b; - - if (b) { - taskID = scheduler.scheduleSyncRepeatingTask(plugin, this::tick, 0, 1); - } else { - scheduler.cancelTask(taskID); - } - } - - private void tick() { + @Override + protected void tick() { Set bots = manager.fetch(); count = bots.size(); bots.forEach(this::tickBot); @@ -53,6 +29,8 @@ public class BotAgent { // This is where the code starts to get spicy private void tickBot(Bot bot) { + if (!bot.isAlive()) return; + Location loc = bot.getLocation(); // if bot.hasHoldState() return; << This will be to check if a bot is mining or something similar where it can't move @@ -130,7 +108,14 @@ public class BotAgent { } if (vel.length() > 1) vel.normalize(); - vel.multiply(0.4).setY(0.4); + + if (loc.distance(target) <= 5) { + vel.multiply(0.3); + } else { + vel.multiply(0.4); + } + + vel.setY(0.4); bot.jump(vel); } diff --git a/src/main/java/net/nuggetmc/ai/bot/agent/BotSituation.java b/src/main/java/net/nuggetmc/ai/bot/agent/botagent/BotSituation.java similarity index 91% rename from src/main/java/net/nuggetmc/ai/bot/agent/BotSituation.java rename to src/main/java/net/nuggetmc/ai/bot/agent/botagent/BotSituation.java index 6115df6..96ee75f 100644 --- a/src/main/java/net/nuggetmc/ai/bot/agent/BotSituation.java +++ b/src/main/java/net/nuggetmc/ai/bot/agent/botagent/BotSituation.java @@ -1,4 +1,4 @@ -package net.nuggetmc.ai.bot.agent; +package net.nuggetmc.ai.bot.agent.botagent; import net.nuggetmc.ai.bot.Bot; import org.bukkit.Location; diff --git a/src/main/java/net/nuggetmc/ai/bot/agent/VerticalDisplacement.java b/src/main/java/net/nuggetmc/ai/bot/agent/botagent/VerticalDisplacement.java similarity index 86% rename from src/main/java/net/nuggetmc/ai/bot/agent/VerticalDisplacement.java rename to src/main/java/net/nuggetmc/ai/bot/agent/botagent/VerticalDisplacement.java index 2d2d9b6..26043d1 100644 --- a/src/main/java/net/nuggetmc/ai/bot/agent/VerticalDisplacement.java +++ b/src/main/java/net/nuggetmc/ai/bot/agent/botagent/VerticalDisplacement.java @@ -1,4 +1,4 @@ -package net.nuggetmc.ai.bot.agent; +package net.nuggetmc.ai.bot.agent.botagent; public enum VerticalDisplacement { AT, diff --git a/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyAgent.java b/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyAgent.java new file mode 100644 index 0000000..7f6ecf0 --- /dev/null +++ b/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyAgent.java @@ -0,0 +1,777 @@ +package net.nuggetmc.ai.bot.agent.legacyagent; + +import net.minecraft.server.v1_16_R3.BlockPosition; +import net.minecraft.server.v1_16_R3.PacketPlayOutBlockBreakAnimation; +import net.nuggetmc.ai.bot.Bot; +import net.nuggetmc.ai.bot.BotManager; +import net.nuggetmc.ai.bot.agent.Agent; +import net.nuggetmc.ai.utils.MathUtils; +import net.nuggetmc.ai.utils.PlayerUtils; +import org.bukkit.*; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer; +import org.bukkit.entity.Boat; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.util.Vector; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +// Yes, this code is very unoptimized, I know. +public class LegacyAgent extends Agent { + + private final LegacyBlockCheck blockCheck; + + public LegacyAgent(BotManager manager) { + super(manager); + + this.blockCheck = new LegacyBlockCheck(this); + } + + private final Map miningAnim = new HashMap<>(); + private final Set boats = new HashSet<>(); + + private final Map btList = new HashMap<>(); + private final Map btCheck = new HashMap<>(); + private final Map towerList = new HashMap<>(); + + private final Set boatCooldown = new HashSet<>(); + private final Map crackList = new HashMap<>(); + private final Map mining = new HashMap<>(); + + private final Set fallDamageCooldown = new HashSet<>(); + + public final Set noFace = new HashSet<>(); + public final Set noJump = new HashSet<>(); + + @Override + protected void tick() { + manager.fetch().forEach(this::tickBot); + } + + private void center(Bot bot) { + if (bot == null || !bot.isAlive()) { + return; + } + + final Player playerBot = bot.getBukkitEntity(); + + Location prev = null; + if (btList.containsKey(playerBot)) { + prev = btList.get(playerBot); + } + + Location loc = playerBot.getLocation(); + + if (prev != null) { + if (loc.getBlockX() == prev.getBlockX() && loc.getBlockZ() == prev.getBlockZ()) { + btCheck.put(playerBot, true); + } else { + btCheck.put(playerBot, false); + } + } + + btList.put(playerBot, loc); + } + + private void tickBot(Bot bot) { + if (!bot.isAlive()) return; + + if (bot.tickDelay(20)) { + center(bot); + } + + Location loc = bot.getLocation(); + + Player player = nearestPlayer(loc); + if (player == null) { + // LESSLAG if (bot.tickDelay(20)) + stopMining(bot); + return; + } + + blockCheck.clutch(bot, player); + + Player playerBot = bot.getBukkitEntity(); + Location target = player.getLocation().add(bot.getOffset()); + + if (bot.tickDelay(3) && !miningAnim.containsKey(playerBot)) { + Location a = playerBot.getEyeLocation(); + Location b = player.getEyeLocation(); + Location c1 = player.getLocation(); + + if (!LegacyUtils.checkIfBlocksOnVector(a, b) || !LegacyUtils.checkIfBlocksOnVector(a, c1)) { + attack(bot, player, loc); + } + } + + boolean waterGround = (LegacyMats.WATER.contains(loc.clone().add(0, -0.1, 0).getBlock().getType()) + && !LegacyMats.AIR.contains(loc.clone().add(0, -0.6, 0).getBlock().getType())); + + boolean c = false, lc = false; + + if (btCheck.containsKey(playerBot)) lc = btCheck.get(playerBot); + + if (waterGround || bot.isOnGround() || onBoat(playerBot)) { + byte j = 1; + + if (towerList.containsKey(playerBot)) { + if (loc.getBlockY() > player.getLocation().getBlockY()) { + towerList.remove(playerBot); + resetHand(bot, player, playerBot); + } + } + + Block block = loc.clone().add(0, 1, 0).getBlock(); + + if (Math.abs(loc.getBlockX() - target.getBlockX()) <= 3 && + Math.abs(loc.getBlockZ() - target.getBlockZ()) <= 3) { + c = true; + } + + boolean bc = c || lc; + + // make this not destroy in scenarios where the bot can move out of the place + if (checkAt(bot, block, playerBot)) { + return; + } + + else if (checkFence(bot, loc.getBlock(), playerBot)) { + return; + } + + else if (checkDown(bot, playerBot, player.getLocation(), bc)) { + return; + } + + else if ((c || lc) && checkUp(bot, player, playerBot, target, c)) { + return; + } + + else { + if (bc) j = checkSide(bot, player, playerBot); + + switch (j) { + case 1: + resetHand(bot, player, playerBot); + if (!noJump.contains(playerBot)) { + if (!waterGround) move(bot, player, loc, target); + } + return; + + case 2: + if (!waterGround) move(bot, player, loc, target); + return; + } + } + }/* else if (lc && !bot.isOnGround()) { + moveSmall(bot, loc, target); + }*/ + } + + private void stopMining(Bot bot) { + Player playerNPC = bot.getBukkitEntity(); + if (miningAnim.containsKey(playerNPC)) { + BukkitRunnable task = miningAnim.get(playerNPC); + if (task != null) { + task.cancel(); + miningAnim.remove(playerNPC); + } + } + } + + /*private void moveSmall(Bot bot, Location loc, Location target) { + Vector vel = target.toVector().subtract(loc.toVector()).setY(0).normalize(); + + bot.stand(); // eventually create a memory system so packets do not have to be sent every tick + + try { + Vector newVel = bot.velocity.clone().add(vel); + if (newVel.length() > 1) newVel.normalize(); + bot.addVelocity(newVel.multiply(0.01)); + } catch (IllegalArgumentException ignored) { } + }*/ + + private void move(Bot bot, Player player, Location loc, Location target) { + Vector vel = target.toVector().subtract(loc.toVector()).normalize(); + + if (bot.tickDelay(5)) bot.faceLocation(player.getLocation()); + if (!bot.isOnGround()) return; // calling this a second time later on + + bot.stand(); // eventually create a memory system so packets do not have to be sent every tick + bot.setItem(null); // method to check item in main hand, bot.getItemInHand() + + try { + vel.add(bot.velocity); + } catch (IllegalArgumentException e) { + if (!MathUtils.isFinite(vel)) { + MathUtils.clean(vel); + } + } + + if (vel.length() > 1) vel.normalize(); + + if (loc.distance(target) <= 5) { + vel.multiply(0.3); + } else { + vel.multiply(0.4); + } + + vel.setY(0.4); + + bot.jump(vel); + } + + private byte checkSide(Bot npc, Player player, Player playerNPC) { // make it so they don't jump when checking side + Location a = playerNPC.getEyeLocation(); + Location b = player.getLocation().add(0, 1, 0); + + if (npc.getLocation().distance(player.getLocation()) < 2.9 && !LegacyUtils.checkIfBlocksOnVector(a, b)) { + resetHand(npc, player, playerNPC); + return 1; + } + + LegacyLevel h = checkNearby(player, npc); + + if (h == null) { + resetHand(npc, player, playerNPC); + return 1; + } else if (h.isSide()) { + return 0; + } else { + return 2; + } + } + + private LegacyLevel checkNearby(Player ply, Bot npc) { + Player player = npc.getBukkitEntity(); + + npc.faceLocation(ply.getLocation()); + + BlockFace dir = player.getFacing(); + LegacyLevel level = null; + Block get = null; + + switch (dir) { + case NORTH: + get = player.getLocation().add(0, 1, -1).getBlock(); + if (checkSideBreak(get.getType())) { + level = LegacyLevel.NORTH; + } else if (checkSideBreak(get.getLocation().add(0, -1, 0).getBlock().getType())) { + get = get.getLocation().add(0, -1, 0).getBlock(); + level = LegacyLevel.NORTH_D; + } + break; + case SOUTH: + get = player.getLocation().add(0, 1, 1).getBlock(); + if (checkSideBreak(get.getType())) { + level = LegacyLevel.SOUTH; + } else if (checkSideBreak(get.getLocation().add(0, -1, 0).getBlock().getType())) { + get = get.getLocation().add(0, -1, 0).getBlock(); + level = LegacyLevel.SOUTH_D; + } + break; + case EAST: + get = player.getLocation().add(1, 1, 0).getBlock(); + if (checkSideBreak(get.getType())) { + level = LegacyLevel.EAST; + } else if (checkSideBreak(get.getLocation().add(0, -1, 0).getBlock().getType())) { + get = get.getLocation().add(0, -1, 0).getBlock(); + level = LegacyLevel.EAST_D; + } + break; + case WEST: + get = player.getLocation().add(-1, 1, 0).getBlock(); + if (checkSideBreak(get.getType())) { + level = LegacyLevel.WEST; + } else if (checkSideBreak(get.getLocation().add(0, -1, 0).getBlock().getType())) { + get = get.getLocation().add(0, -1, 0).getBlock(); + level = LegacyLevel.WEST_D; + } + break; + default: + break; + } + + if (level == LegacyLevel.EAST_D || level == LegacyLevel.WEST_D || level == LegacyLevel.NORTH_D || level == LegacyLevel.SOUTH_D) { + if (LegacyMats.AIR.contains(player.getLocation().add(0, 2, 0).getBlock().getType()) + && LegacyMats.AIR.contains(get.getLocation().add(0, 2, 0).getBlock().getType())) { + return null; + } + } + + if (level != null) { + preBreak(npc, player, get, level); + } + + return level; + } + + private static boolean checkSideBreak(Material type) { + return !LegacyMats.BREAK.contains(type) && !LegacyMats.LEAVES.contains(type); + } + + private boolean checkUp(Bot npc, Player player, Player playerNPC, Location loc, boolean c) { + Location a = playerNPC.getLocation(); + Location b = player.getLocation(); + + a.setY(0); + b.setY(0); + + boolean above = LegacyWorldManager.aboveGround(playerNPC.getLocation()); + + BlockFace dir = playerNPC.getFacing(); + Block get; + + switch (dir) { + case NORTH: + get = playerNPC.getLocation().add(0, 1, -1).getBlock(); + break; + case SOUTH: + get = playerNPC.getLocation().add(0, 1, 1).getBlock(); + break; + case EAST: + get = playerNPC.getLocation().add(1, 1, 0).getBlock(); + break; + case WEST: + get = playerNPC.getLocation().add(-1, 1, 0).getBlock(); + break; + default: + get = null; + } + + if (get == null || LegacyMats.BREAK.contains(get.getType())) { + if (a.distance(b) >= 16 && above) return false; + } + + if (playerNPC.getLocation().getBlockY() < player.getLocation().getBlockY() - 1) { + Material m0 = playerNPC.getLocation().getBlock().getType(); + Material m1 = playerNPC.getLocation().add(0, 1, 0).getBlock().getType(); + Material m2 = playerNPC.getLocation().add(0, 2, 0).getBlock().getType(); + + if (LegacyMats.BREAK.contains(m0) && LegacyMats.BREAK.contains(m1) && LegacyMats.BREAK.contains(m2)) { + + npc.setItem(new ItemStack(Material.COBBLESTONE)); + + Block place = playerNPC.getLocation().getBlock(); + + if (miningAnim.containsKey(playerNPC)) { + BukkitRunnable task = miningAnim.get(playerNPC); + if (task != null) { + task.cancel(); + miningAnim.remove(playerNPC); + } + } + + npc.look(BlockFace.DOWN); + + // maybe put this in lower if statement onGround() + Bukkit.getScheduler().runTaskLater(plugin, () -> { + npc.sneak(); + npc.setItem(new ItemStack(Material.COBBLESTONE)); + npc.punch(); + npc.look(BlockFace.DOWN); + + Bukkit.getScheduler().runTaskLater(plugin, () -> { + npc.look(BlockFace.DOWN); + }, 1); + + blockCheck.placeBlock(npc, playerNPC, place); + + if (!towerList.containsKey(playerNPC)) { + if (c) { + towerList.put(playerNPC, playerNPC.getLocation()); + } + } + }, 5); + + if (npc.isOnGround()) { + if (player.getLocation().distance(playerNPC.getLocation()) < 16) { + if (noJump.contains(playerNPC)) { + + Bukkit.getScheduler().runTaskLater(plugin, () -> { + npc.setVelocity(new Vector(0, 0.5, 0)); + }, 1); + + } else { + Vector vector = loc.toVector().subtract(playerNPC.getLocation().toVector()).normalize(); + npc.stand(); + + Vector move = npc.getVelocity().add(vector); + if (move.length() > 1) move = move.normalize(); + move.multiply(0.1); + move.setY(0.5); + + npc.setVelocity(move); + return true; + } + } else { + if (npc.isOnGround()) { + Location locBlock = playerNPC.getLocation(); + locBlock.setX(locBlock.getBlockX() + 0.5); + locBlock.setZ(locBlock.getBlockZ() + 0.5); + + Vector vector = locBlock.toVector().subtract(playerNPC.getLocation().toVector()); + if (vector.length() > 1) vector = vector.normalize(); + vector.multiply(0.1); + vector.setY(0.5); + + npc.setVelocity(npc.getVelocity().add(vector)); + return true; + } + } + } + + return false; + + } else if (LegacyMats.BREAK.contains(m0) && LegacyMats.BREAK.contains(m1) && !LegacyMats.BREAK.contains(m2)) { + Block block = npc.getLocation().add(0, 2, 0).getBlock(); + npc.look(BlockFace.UP); + preBreak(npc, playerNPC, block, LegacyLevel.ABOVE); + + if (npc.isOnGround()) { + Location locBlock = playerNPC.getLocation(); + locBlock.setX(locBlock.getBlockX() + 0.5); + locBlock.setZ(locBlock.getBlockZ() + 0.5); + + Vector vector = locBlock.toVector().subtract(playerNPC.getLocation().toVector()); + if (vector.length() > 1) vector = vector.normalize(); + vector.multiply(0.1); + vector.setY(0); + + npc.setVelocity(npc.getVelocity().add(vector)); + } + + return true; + } + } + + return false; + } + + private boolean checkDown(Bot npc, Player player, Location loc, boolean c) { // possibly a looser check for c + + if (!LegacyUtils.checkIfBlocksOnVector(npc.getLocation(), loc) || !LegacyUtils.checkIfBlocksOnVector(player.getEyeLocation(), loc)) return false; + + if (c && npc.getLocation().getBlockY() > loc.getBlockY() + 1) { + Block block = npc.getLocation().add(0, -1, 0).getBlock(); + npc.look(BlockFace.DOWN); + + downMine(npc, player, block); + preBreak(npc, player, block, LegacyLevel.BELOW); + return true; + } + + else { + Location a = loc.clone(); + Location b = player.getLocation(); + + a.setY(0); + b.setY(0); + + if (npc.getLocation().getBlockY() > loc.getBlockY() + 10 && a.distance(b) < 10) { + Block block = npc.getLocation().add(0, -1, 0).getBlock(); + npc.look(BlockFace.DOWN); + + downMine(npc, player, block); + preBreak(npc, player, block, LegacyLevel.BELOW); + return true; + + } else { + return false; + } + } + } + + private void downMine(Bot npc, Player player, Block block) { + if (!LegacyMats.NO_CRACK.contains(block.getType())) { + Location locBlock = player.getLocation(); + locBlock.setX(locBlock.getBlockX() + 0.5); + locBlock.setZ(locBlock.getBlockZ() + 0.5); + + Vector vector = locBlock.toVector().subtract(player.getLocation().toVector()); + if (vector.length() > 1) vector = vector.normalize(); + vector.setY(0); + vector.multiply(0.1); + npc.setVelocity(vector); + } + + if (npc.isInWater()) { + Location locBlock = player.getLocation(); + locBlock.setX(locBlock.getBlockX() + 0.5); + locBlock.setZ(locBlock.getBlockZ() + 0.5); + + Vector vector = locBlock.toVector().subtract(player.getLocation().toVector()); + if (vector.length() > 1) vector = vector.normalize(); + vector.multiply(0.3); + vector.setY(-1); + + if (!fallDamageCooldown.contains(npc)) { + fallDamageCooldown.add(npc); + + Bukkit.getScheduler().runTaskLater(plugin, () -> { + fallDamageCooldown.remove(npc); + }, 10); + } + + npc.setVelocity(vector); + } + } + + private boolean checkFence(Bot bot, Block block, Player player) { + if (LegacyMats.FENCE.contains(block.getType())) { + preBreak(bot, player, block, LegacyLevel.AT_D); + return true; + } + + return false; + } + + private boolean checkAt(Bot bot, Block block, Player player) { + if (LegacyMats.BREAK.contains(block.getType())) { + return false; + } else { + preBreak(bot, player, block, LegacyLevel.AT); + return true; + } + } + + private void preBreak(Bot bot, Player player, Block block, LegacyLevel level) { + Material item; + Material type = block.getType(); + + if (LegacyMats.SHOVEL.contains(type)) { + item = LegacyItems.SHOVEL; + } else if (LegacyMats.AXE.contains(type)) { + item = LegacyItems.AXE; + } else { + item = LegacyItems.PICKAXE; + } + + bot.setItem(new ItemStack(item)); + + if (level == LegacyLevel.EAST_D || level == LegacyLevel.NORTH_D || level == LegacyLevel.SOUTH_D || level == LegacyLevel.WEST_D) { + bot.pitch = 69; + + Bukkit.getScheduler().runTaskLater(plugin, () -> { + btCheck.put(player, true); + }, 5); + } else if (level == LegacyLevel.AT_D || level == LegacyLevel.AT) { + Location blockLoc = block.getLocation().add(0.5, -1, 0.5); + bot.faceLocation(blockLoc); + } + + if (!miningAnim.containsKey(player)) { + + BukkitRunnable task = new BukkitRunnable() { + + @Override + public void run() { + bot.punch(); + } + }; + + task.runTaskTimer(plugin, 0, 4); + taskList.add(task); + miningAnim.put(player, task); + } + + blockBreakEffect(player, block, level); + } + + private void blockBreakEffect(Player player, Block block, LegacyLevel level) { + + if (LegacyMats.NO_CRACK.contains(block.getType())) return; + + if (!crackList.containsKey(block)) { + BukkitRunnable task = new BukkitRunnable() { + + @Override + public void run() { + byte i = mining.get(this); + + Block cur; + switch (level) { + case ABOVE: + cur = player.getLocation().add(0, 2, 0).getBlock(); + break; + case BELOW: + cur = player.getLocation().add(0, -1, 0).getBlock(); + break; + case NORTH: + cur = player.getLocation().add(0, 1, -1).getBlock(); + break; + case SOUTH: + cur = player.getLocation().add(0, 1, 1).getBlock(); + break; + case EAST: + cur = player.getLocation().add(1, 1, 0).getBlock(); + break; + case WEST: + cur = player.getLocation().add(-1, 1, 0).getBlock(); + break; + case NORTH_D: + cur = player.getLocation().add(0, 0, -1).getBlock(); + break; + case SOUTH_D: + cur = player.getLocation().add(0, 0, 1).getBlock(); + break; + case EAST_D: + cur = player.getLocation().add(1, 0, 0).getBlock(); + break; + case WEST_D: + cur = player.getLocation().add(-1, 0, 0).getBlock(); + break; + case AT_D: + cur = player.getLocation().getBlock(); + break; + default: + cur = player.getLocation().add(0, 1, 0).getBlock(); + } + + // wow this repeated code is so bad lmao + + if (player.isDead()) { + this.cancel(); + + PacketPlayOutBlockBreakAnimation crack = new PacketPlayOutBlockBreakAnimation(crackList.get(block), new BlockPosition(block.getX(), block.getY(), block.getZ()), -1); + for (Player all : Bukkit.getOnlinePlayers()) { + ((CraftPlayer) all).getHandle().playerConnection.sendPacket(crack); + } + + crackList.remove(block); + mining.remove(this); + return; + } + + if (!block.equals(cur) || block.getType() != cur.getType()) { + this.cancel(); + + PacketPlayOutBlockBreakAnimation crack = new PacketPlayOutBlockBreakAnimation(crackList.get(block), new BlockPosition(block.getX(), block.getY(), block.getZ()), -1); + for (Player all : Bukkit.getOnlinePlayers()) { + ((CraftPlayer) all).getHandle().playerConnection.sendPacket(crack); + } + + crackList.remove(block); + mining.remove(this); + return; + } + + Sound sound = LegacyUtils.breakBlockSound(block); + + if (i == 9) { + this.cancel(); + + PacketPlayOutBlockBreakAnimation crack = new PacketPlayOutBlockBreakAnimation(crackList.get(block), new BlockPosition(block.getX(), block.getY(), block.getZ()), -1); + for (Player all : Bukkit.getOnlinePlayers()) { + ((CraftPlayer) all).getHandle().playerConnection.sendPacket(crack); + } + + if (sound != null) { + for (Player all : Bukkit.getOnlinePlayers()) all.playSound(block.getLocation(), sound, SoundCategory.BLOCKS, 1, 1); + } + + block.breakNaturally(); + + if (level == LegacyLevel.ABOVE) { + noJump.add(player); + + Bukkit.getScheduler().runTaskLater(plugin, () -> { + noJump.remove(player); + }, 15); + } + + crackList.remove(block); + mining.remove(this); + return; + } + + if (sound != null) { + 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; + + PacketPlayOutBlockBreakAnimation crack = new PacketPlayOutBlockBreakAnimation(crackList.get(block), new BlockPosition(block.getX(), block.getY(), block.getZ()), i); + for (Player all : Bukkit.getOnlinePlayers()) { + ((CraftPlayer) all).getHandle().playerConnection.sendPacket(crack); + } + + mining.put(this, (byte) (i + 1)); + } + }; + + taskList.add(task); + mining.put(task, (byte) 0); + crackList.put(block, (short) random.nextInt(2000)); + task.runTaskTimer(plugin, 0, 2); + } + } + + private void resetHand(Bot npc, Player player, Player playerNPC) { + if (!noFace.contains(npc)) { // LESSLAG if there is no if statement here + npc.faceLocation(player.getLocation()); + } + + if (miningAnim.containsKey(playerNPC)) { + BukkitRunnable task = miningAnim.get(playerNPC); + if (task != null) { + task.cancel(); + miningAnim.remove(playerNPC); + } + } + + if (boatCooldown.contains(npc)) return; + + npc.setItem(null); + } + + private boolean onBoat(Player player) { + Set cache = new HashSet<>(); + + boolean check = false; + + for (Boat boat : boats) { + if (player.getWorld() != boat.getWorld()) continue; + + if (boat.isDead()) { + cache.add(boat); + continue; + } + + if (player.getLocation().distance(boat.getLocation()) < 1) { + check = true; + break; + } + } + + boats.removeAll(cache); + + return check; + } + + private void attack(Bot bot, Player player, Location loc) { + if (!PlayerUtils.isVulnerableGameMode(player.getGameMode()) || player.getNoDamageTicks() >= 5 || loc.distance(player.getLocation()) >= 4) return; + + bot.attack(player); + } + + private Player nearestPlayer(Location loc) { + Player result = null; + + for (Player player : Bukkit.getOnlinePlayers()) { + if (!PlayerUtils.isTargetable(player.getGameMode()) || loc.getWorld() != player.getWorld()) continue; + + if (result == null || loc.distance(player.getLocation()) < loc.distance(result.getLocation())) { + result = player; + } + } + + return result; + } +} diff --git a/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyBlockCheck.java b/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyBlockCheck.java new file mode 100644 index 0000000..560697f --- /dev/null +++ b/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyBlockCheck.java @@ -0,0 +1,185 @@ +package net.nuggetmc.ai.bot.agent.legacyagent; + +import net.nuggetmc.ai.PlayerAI; +import net.nuggetmc.ai.bot.Bot; +import org.bukkit.*; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class LegacyBlockCheck { + + private final PlayerAI plugin; + private final LegacyAgent agent; + + public LegacyBlockCheck(LegacyAgent agent) { + this.plugin = PlayerAI.getInstance(); + this.agent = agent; + } + + private void placeFinal(Bot bot, Player player, Location loc) { + if (loc.getBlock().getType() != Material.COBBLESTONE) { + for (Player all : Bukkit.getOnlinePlayers()) all.playSound(loc, Sound.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1, 1); + bot.setItem(new ItemStack(Material.COBBLESTONE)); + loc.getBlock().setType(Material.COBBLESTONE); + } + } + + public void placeBlock(Bot bot, Player player, Block block) { + + Location loc = block.getLocation(); + + Block under = loc.clone().add(0, -1, 0).getBlock(); + + if (LegacyMats.SPAWN.contains(under.getType())) { + placeFinal(bot, player, loc.clone().add(0, -1, 0)); + Bukkit.getScheduler().runTaskLater(plugin, () -> { + placeFinal(bot, player, block.getLocation()); + }, 2); + }/* else { + placeFinal(player, block.getLocation()); + return; + }*/ + + Set face = new HashSet<>(Arrays.asList(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())); + + boolean a = false; + for (Block side : face) { + if (!LegacyMats.SPAWN.contains(side.getType())) { + a = true; + } + } + + if (a) { + placeFinal(bot, player, block.getLocation()); + return; + } + + Set edge = new HashSet<>(Arrays.asList(loc.clone().add(1, -1, 0).getBlock(), + loc.clone().add(-1, -1, 0).getBlock(), + loc.clone().add(0, -1, 1).getBlock(), + loc.clone().add(0, -1, -1).getBlock())); + + boolean b = false; + for (Block side : edge) { + if (!LegacyMats.SPAWN.contains(side.getType())) { + b = true; + } + } + + if (b && LegacyMats.SPAWN.contains(under.getType())) { + placeFinal(bot, player, loc.clone().add(0, -1, 0)); + Bukkit.getScheduler().runTaskLater(plugin, () -> { + placeFinal(bot, player, block.getLocation()); + }, 2); + return; + } + + Block c1 = loc.clone().add(1, -1, 1).getBlock(); + Block c2 = loc.clone().add(1, -1, -1).getBlock(); + Block c3 = loc.clone().add(-1, -1, 1).getBlock(); + Block c4 = loc.clone().add(-1, -1, -1).getBlock(); + + boolean t = false; + + if (!LegacyMats.SPAWN.contains(c1.getType()) || !LegacyMats.SPAWN.contains(c2.getType())) { + + Block b1 = loc.clone().add(1, -1, 0).getBlock(); + if (LegacyMats.SPAWN.contains(b1.getType())) { + placeFinal(bot, player, b1.getLocation()); + } + + t = true; + + } else if (!LegacyMats.SPAWN.contains(c3.getType()) || !LegacyMats.SPAWN.contains(c4.getType())) { + + Block b1 = loc.clone().add(-1, -1, 0).getBlock(); + if (LegacyMats.SPAWN.contains(b1.getType())) { + placeFinal(bot, player, b1.getLocation()); + } + + t = true; + } + + if (t) { + Bukkit.getScheduler().runTaskLater(plugin, () -> { + Block b2 = loc.clone().add(0, -1, 0).getBlock(); + if (LegacyMats.SPAWN.contains(b2.getType())) { + for (Player all : Bukkit.getOnlinePlayers()) all.playSound(loc, Sound.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1, 1); + placeFinal(bot, player, b2.getLocation()); + } + }, 1); + + Bukkit.getScheduler().runTaskLater(plugin, () -> { + for (Player all : Bukkit.getOnlinePlayers()) all.playSound(loc, Sound.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1, 1); + placeFinal(bot, player, block.getLocation()); + }, 3); + return; + } + + for (Player all : Bukkit.getOnlinePlayers()) all.playSound(loc, Sound.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1, 1); + placeFinal(bot, player, block.getLocation()); + } + + public void clutch(Bot bot, Player target) { + Player player = bot.getBukkitEntity(); + + Material type = player.getLocation().add(0, -1, 0).getBlock().getType(); + Material type2 = player.getLocation().add(0, -2, 0).getBlock().getType(); + + if (!(LegacyMats.SPAWN.contains(type) && LegacyMats.SPAWN.contains(type2))) return; + + if (target.getLocation().getBlockY() >= player.getLocation().getBlockY()) { + Location loc = player.getLocation().add(0, -1, 0); + + Set face = new HashSet<>(Arrays.asList( + 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() + )); + + Location at = null; + for (Block side : face) { + if (!LegacyMats.SPAWN.contains(side.getType())) { + at = side.getLocation(); + } + } + + if (at != null) { + + //agent.slow.add(player); + agent.noFace.add(bot); + + Bukkit.getScheduler().runTaskLater(plugin, () -> { + bot.stand(); + //agent.slow.remove(player); + }, 12); + + Bukkit.getScheduler().runTaskLater(plugin, () -> { + agent.noFace.remove(bot); + }, 15); + + bot.look(BlockFace.DOWN); + + Bukkit.getScheduler().runTaskLater(plugin, () -> { + bot.look(BlockFace.DOWN); + }, 1); + + bot.punch(); + bot.sneak(); + for (Player all : Bukkit.getOnlinePlayers()) all.playSound(loc, Sound.BLOCK_STONE_PLACE, SoundCategory.BLOCKS, 1, 1); + bot.setItem(new ItemStack(Material.COBBLESTONE)); + loc.getBlock().setType(Material.COBBLESTONE); + } + } + } +} diff --git a/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyItems.java b/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyItems.java new file mode 100644 index 0000000..bd5091d --- /dev/null +++ b/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyItems.java @@ -0,0 +1,9 @@ +package net.nuggetmc.ai.bot.agent.legacyagent; + +import org.bukkit.Material; + +public class LegacyItems { + public static final Material SHOVEL = Material.IRON_SHOVEL; + public static final Material AXE = Material.IRON_AXE; + public static final Material PICKAXE = Material.IRON_PICKAXE; +} diff --git a/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyLevel.java b/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyLevel.java new file mode 100644 index 0000000..5eae55f --- /dev/null +++ b/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyLevel.java @@ -0,0 +1,35 @@ +package net.nuggetmc.ai.bot.agent.legacyagent; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public enum LegacyLevel { + ABOVE, + BELOW, + AT, + AT_D, + NORTH, + SOUTH, + EAST, + WEST, + NORTH_D, + SOUTH_D, + EAST_D, + WEST_D; + + private static final Set SIDE = new HashSet<>(Arrays.asList( + NORTH, + SOUTH, + EAST, + WEST, + NORTH_D, + SOUTH_D, + EAST_D, + WEST_D + )); + + public boolean isSide() { + return SIDE.contains(this); + } +} diff --git a/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyMats.java b/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyMats.java new file mode 100644 index 0000000..f744348 --- /dev/null +++ b/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyMats.java @@ -0,0 +1,156 @@ +package net.nuggetmc.ai.bot.agent.legacyagent; + +import org.bukkit.Material; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class LegacyMats { + + public static final Set AIR = new HashSet<>(Arrays.asList( + Material.WATER, + Material.OAK_TRAPDOOR, + Material.FIRE, + Material.LAVA, + Material.SNOW, + Material.CAVE_AIR, + Material.VINE, + Material.FERN, + Material.LARGE_FERN, + Material.GRASS, + Material.TALL_GRASS, + Material.SEAGRASS, + Material.TALL_SEAGRASS, + Material.KELP, + Material.KELP_PLANT, + Material.AIR, + Material.VOID_AIR + )); + + public static final Set NO_CRACK = new HashSet<>(Arrays.asList( + Material.WATER, + Material.FIRE, + Material.LAVA, + Material.CAVE_AIR + )); + + public static final Set SHOVEL = new HashSet<>(Arrays.asList( + Material.DIRT, + Material.GRAVEL, + Material.SAND, + Material.SNOW + )); + + public static final Set AXE = new HashSet<>(Arrays.asList( + Material.OAK_PLANKS, Material.OAK_DOOR, Material.OAK_FENCE, Material.OAK_FENCE_GATE, Material.OAK_LOG, Material.OAK_PLANKS, + Material.OAK_SIGN, Material.OAK_SLAB, Material.OAK_STAIRS, Material.OAK_TRAPDOOR, Material.OAK_WALL_SIGN, Material.OAK_WOOD, + Material.DARK_OAK_PLANKS, Material.DARK_OAK_DOOR, Material.DARK_OAK_FENCE, Material.DARK_OAK_FENCE_GATE, Material.DARK_OAK_LOG, Material.DARK_OAK_PLANKS, + Material.DARK_OAK_SIGN, Material.DARK_OAK_SLAB, Material.DARK_OAK_STAIRS, Material.DARK_OAK_TRAPDOOR, Material.DARK_OAK_WALL_SIGN, Material.DARK_OAK_WOOD, + Material.ACACIA_PLANKS, Material.ACACIA_DOOR, Material.ACACIA_FENCE, Material.ACACIA_FENCE_GATE, Material.ACACIA_LOG, Material.ACACIA_PLANKS, + Material.ACACIA_SIGN, Material.ACACIA_SLAB, Material.ACACIA_STAIRS, Material.ACACIA_TRAPDOOR, Material.ACACIA_WALL_SIGN, Material.ACACIA_WOOD, + Material.BIRCH_PLANKS, Material.BIRCH_DOOR, Material.BIRCH_FENCE, Material.BIRCH_FENCE_GATE, Material.BIRCH_LOG, Material.BIRCH_PLANKS, + Material.BIRCH_SIGN, Material.BIRCH_SLAB, Material.BIRCH_STAIRS, Material.BIRCH_TRAPDOOR, Material.BIRCH_WALL_SIGN, Material.BIRCH_WOOD, + Material.JUNGLE_PLANKS, Material.JUNGLE_DOOR, Material.JUNGLE_FENCE, Material.JUNGLE_FENCE_GATE, Material.JUNGLE_LOG, Material.JUNGLE_PLANKS, + Material.JUNGLE_SIGN, Material.JUNGLE_SLAB, Material.JUNGLE_STAIRS, Material.JUNGLE_TRAPDOOR, Material.JUNGLE_WALL_SIGN, Material.JUNGLE_WOOD, + Material.SPRUCE_PLANKS, Material.SPRUCE_DOOR, Material.SPRUCE_FENCE, Material.SPRUCE_FENCE_GATE, Material.SPRUCE_LOG, Material.SPRUCE_PLANKS, + Material.SPRUCE_SIGN, Material.SPRUCE_SLAB, Material.SPRUCE_STAIRS, Material.SPRUCE_TRAPDOOR, Material.SPRUCE_WALL_SIGN, Material.SPRUCE_WOOD, + Material.CRIMSON_PLANKS, Material.CRIMSON_DOOR, Material.CRIMSON_FENCE, Material.CRIMSON_FENCE_GATE, Material.CRIMSON_PLANKS, + Material.CRIMSON_SIGN, Material.CRIMSON_SLAB, Material.CRIMSON_STAIRS, Material.CRIMSON_TRAPDOOR, Material.CRIMSON_WALL_SIGN, + Material.WARPED_PLANKS, Material.WARPED_DOOR, Material.WARPED_FENCE, Material.WARPED_FENCE_GATE, Material.WARPED_PLANKS, + Material.WARPED_SIGN, Material.WARPED_SLAB, Material.WARPED_STAIRS, Material.WARPED_TRAPDOOR, Material.WARPED_WALL_SIGN + )); + + public static final Set BREAK = new HashSet<>(Arrays.asList( + Material.AIR, + Material.WATER, + Material.LAVA, + Material.TALL_GRASS, + Material.SNOW, + Material.GRASS_PATH, + Material.CAVE_AIR, + Material.VINE, + Material.FERN, + Material.LARGE_FERN, + Material.SUGAR_CANE, + Material.TWISTING_VINES, + Material.WEEPING_VINES, + Material.SEAGRASS, + Material.TALL_SEAGRASS, + Material.KELP, + Material.KELP_PLANT + )); + + public static final Set WATER = new HashSet<>(Arrays.asList( + Material.SEAGRASS, + Material.TALL_SEAGRASS, + Material.KELP, + Material.KELP_PLANT + )); + + public static final Set SPAWN = new HashSet<>(Arrays.asList( + Material.AIR, + Material.TALL_GRASS, + Material.SNOW, + Material.CAVE_AIR, + Material.VINE, + Material.FERN, + Material.LARGE_FERN, + Material.SUGAR_CANE, + Material.TWISTING_VINES, + Material.WEEPING_VINES, + Material.SEAGRASS, + Material.TALL_SEAGRASS, + Material.KELP, + Material.KELP_PLANT + )); + + public static final Set FALL = new HashSet<>(Arrays.asList( + Material.AIR, + Material.TALL_GRASS, + Material.SNOW, + Material.CAVE_AIR, + Material.VINE, + Material.FERN, + Material.LARGE_FERN, + Material.SUGAR_CANE, + Material.TWISTING_VINES, + Material.WEEPING_VINES, + Material.SEAGRASS, + Material.TALL_SEAGRASS, + Material.KELP, + Material.KELP_PLANT, + Material.WATER + )); + + public static final Set FENCE = new HashSet<>(Arrays.asList( + Material.OAK_FENCE, + Material.ACACIA_FENCE, + Material.BIRCH_FENCE, + Material.CRIMSON_FENCE, + Material.DARK_OAK_FENCE, + Material.JUNGLE_FENCE, + Material.NETHER_BRICK_FENCE, + Material.SPRUCE_FENCE, + Material.WARPED_FENCE, + Material.COBBLESTONE_WALL, + Material.ANDESITE_WALL, + Material.BLACKSTONE_WALL, + Material.BRICK_WALL, + Material.GRANITE_WALL, + Material.DIORITE_WALL, + Material.SANDSTONE_WALL, + Material.RED_SANDSTONE_WALL, + Material.RED_NETHER_BRICK_WALL, + Material.IRON_BARS, + Material.COBWEB + )); + + public static final Set LEAVES = new HashSet<>(Arrays.asList( + Material.BIRCH_LEAVES, + Material.DARK_OAK_LEAVES, + Material.JUNGLE_LEAVES, + Material.OAK_LEAVES, + Material.SPRUCE_LEAVES + )); +} diff --git a/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyUtils.java b/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyUtils.java new file mode 100644 index 0000000..7b17be4 --- /dev/null +++ b/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyUtils.java @@ -0,0 +1,54 @@ +package net.nuggetmc.ai.bot.agent.legacyagent; + +import net.minecraft.server.v1_16_R3.*; +import org.bukkit.Location; +import org.bukkit.Sound; +import org.bukkit.block.Block; +import org.bukkit.craftbukkit.v1_16_R3.CraftWorld; +import org.bukkit.util.Vector; + +import java.lang.reflect.Field; + +public class LegacyUtils { + + public static boolean checkIfBlocksOnVector(Location a, Location b) { + Vector v = b.toVector().subtract(a.toVector()); + + int n = 32; + double m = 1 / (double) n; + + double j = Math.floor(v.length() * n); + v.multiply(m / v.length()); + + for (int i = 0; i <= j; i++) { + + Block block = a.getWorld().getBlockAt((a.toVector().add(v.clone().multiply(i))).toLocation(a.getWorld())); + + if (!LegacyMats.AIR.contains(block.getType())) { + return true; + } + } + + return false; + } + + public static Sound breakBlockSound(Block block) { + try { + World nmsWorld = ((CraftWorld) block.getWorld()).getHandle(); + net.minecraft.server.v1_16_R3.Block nmsBlock = nmsWorld.getType(new BlockPosition(block.getX(), block.getY(), block.getZ())).getBlock(); + SoundEffectType soundEffectType = nmsBlock.getStepSound(nmsBlock.getBlockData()); + + Field breakSound = SoundEffectType.class.getDeclaredField("stepSound"); + breakSound.setAccessible(true); + SoundEffect nmsSound = (SoundEffect) breakSound.get(soundEffectType); + + Field keyField = SoundEffect.class.getDeclaredField("b"); + keyField.setAccessible(true); + MinecraftKey nmsString = (MinecraftKey) keyField.get(nmsSound); + + return Sound.valueOf(nmsString.getKey().replace(".", "_").toUpperCase()); + } catch (IllegalAccessException | NoSuchFieldException ex) { + return null; + } + } +} diff --git a/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyWorldManager.java b/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyWorldManager.java new file mode 100644 index 0000000..36dd5ea --- /dev/null +++ b/src/main/java/net/nuggetmc/ai/bot/agent/legacyagent/LegacyWorldManager.java @@ -0,0 +1,19 @@ +package net.nuggetmc.ai.bot.agent.legacyagent; + +import org.bukkit.Location; +import org.bukkit.Material; + +public class LegacyWorldManager { + + public static boolean aboveGround(Location loc) { + int y = 1; + int y0 = loc.getBlockY(); + while (y + y0 < y0 + 25) { + if (loc.clone().add(0, y, 0).getBlock().getType() != Material.AIR) { + return false; + } + y++; + } + return true; + } +} diff --git a/src/main/java/net/nuggetmc/ai/utils/Debugger.java b/src/main/java/net/nuggetmc/ai/utils/Debugger.java index 1d4669e..0fe9a5e 100644 --- a/src/main/java/net/nuggetmc/ai/utils/Debugger.java +++ b/src/main/java/net/nuggetmc/ai/utils/Debugger.java @@ -2,7 +2,7 @@ package net.nuggetmc.ai.utils; import net.nuggetmc.ai.PlayerAI; import net.nuggetmc.ai.bot.Bot; -import net.nuggetmc.ai.bot.agent.BotAgent; +import net.nuggetmc.ai.bot.agent.Agent; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -177,7 +177,7 @@ public class Debugger { } public void toggleAgent() { - BotAgent agent = PlayerAI.getInstance().getManager().getAgent(); + Agent agent = PlayerAI.getInstance().getManager().getAgent(); boolean b = agent.isEnabled(); agent.setEnabled(!b);