vector exceptions handled

This commit is contained in:
batchprogrammer314
2021-07-09 15:49:10 -05:00
parent 0f86796f96
commit 7bd2e50afd
6 changed files with 123 additions and 14 deletions

View File

@@ -15,6 +15,7 @@ import org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import java.util.Objects;
import java.util.UUID;
public class Bot extends EntityPlayer {
@@ -26,11 +27,7 @@ public class Bot extends EntityPlayer {
private byte jumpTicks;
private byte groundTicks;
private final double regenAmount = 0.05;
private final double frictionMin = 0.01;
private final double kbUp = 0.3;
private Vector offset;
private final Vector offset;
public Bot(MinecraftServer minecraftServer, WorldServer worldServer, GameProfile profile, PlayerInteractManager manager) {
super(minecraftServer, worldServer, profile, manager);
@@ -43,7 +40,7 @@ public class Bot extends EntityPlayer {
public static Bot createBot(Location loc, String name, String skin) {
MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer();
WorldServer nmsWorld = ((CraftWorld) loc.getWorld()).getHandle();
WorldServer nmsWorld = ((CraftWorld) Objects.requireNonNull(loc.getWorld())).getHandle();
UUID uuid = SteveUUID.generate();
@@ -135,6 +132,7 @@ public class Bot extends EntityPlayer {
double health = player.getHealth();
double maxHealth = player.getHealthScale();
double regenAmount = 0.05;
double amount;
if (health < maxHealth - regenAmount) {
@@ -218,6 +216,8 @@ public class Bot extends EntityPlayer {
}
public void addFriction() {
double frictionMin = 0.01;
double x = velocity.getX();
double z = velocity.getZ();
@@ -291,6 +291,7 @@ public class Bot extends EntityPlayer {
private void kb(Location loc1, Location loc2) {
double f = 4;
double kbUp = 0.3;
Vector diff = loc1.toVector().subtract(loc2.toVector()).setY(0).normalize().multiply(f).setY(kbUp);
Vector vel = velocity.clone().add(diff).multiply(0.3 / f);

View File

@@ -71,14 +71,17 @@ public class BotAgent {
// if checkVertical(bot) { break block action add; return; }
// BotSituation situation = BotSituation.create(bot);
// based on the situation, the bot can perform different actions
// there can be priorities assigned
if (bot.tickDelay(3)) attack(bot, player, loc);
move(bot, player, loc, target);
}
private void attack(Bot bot, Player player, Location loc) {
if (!PlayerUtils.isVulnerableGamemode(player.getGameMode())
|| player.getNoDamageTicks() >= 5
|| loc.distance(player.getLocation()) >= 4) return;
if (player.getNoDamageTicks() >= 5 || loc.distance(player.getLocation()) >= 4) return;
bot.attack(player);
}
@@ -92,7 +95,9 @@ public class BotAgent {
vel.checkFinite();
vel.add(bot.velocity);
} catch (IllegalArgumentException e) {
return;
if (!MathUtils.isFinite(vel)) {
MathUtils.clean(vel);
}
}
if (vel.length() > 1) vel.normalize();
@@ -105,7 +110,7 @@ public class BotAgent {
Player result = null;
for (Player player : Bukkit.getOnlinePlayers()) {
if (loc.getWorld() != player.getWorld()) continue;
if (!PlayerUtils.isTargetable(player.getGameMode()) || loc.getWorld() != player.getWorld()) continue;
if (result == null || loc.distance(player.getLocation()) < loc.distance(result.getLocation())) {
result = player;

View File

@@ -1,14 +1,21 @@
package net.nuggetmc.ai.utils;
import net.nuggetmc.ai.PlayerAI;
import net.nuggetmc.ai.bot.Bot;
import net.nuggetmc.ai.bot.agent.BotAgent;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import java.beans.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
public class Debugger {
@@ -58,6 +65,84 @@ public class Debugger {
return list.toArray();
}
public void t(String content) {
Object[] obj = buildObjects(content);
if (obj.length != 1 || obj[0] instanceof Boolean) {
print("Invalid arguments!");
return;
}
PlayerUtils.setAllTargetable(Boolean.parseBoolean((String) obj[0]));
String var = "PlayerUtils.allTargetable";
if (PlayerUtils.getAllTargetable()) {
print(var + " is now " + ChatColor.GREEN + "TRUE" + ChatColor.RESET + ".");
} else {
print(var + " is now " + ChatColor.RED + "FALSE" + ChatColor.RESET + ".");
}
}
public void hideNametags(String content) { // this works for some reason
Set<Bot> bots = PlayerAI.getInstance().getManager().fetch();
for (Bot bot : bots) {
Location loc = bot.getLocation();
World world = loc.getWorld();
if (world == null) continue;
loc.setX(loc.getBlockX());
loc.setY(loc.getBlockY());
loc.setZ(loc.getBlockZ());
loc.add(0.5, 0.5, 0.5);
ArmorStand seat = (ArmorStand) world.spawnEntity(loc, EntityType.ARMOR_STAND);
seat.setVisible(false);
seat.setSmall(true);
bot.getBukkitEntity().setPassenger(seat);
}
}
public void sit(String content) {
Set<Bot> bots = PlayerAI.getInstance().getManager().fetch();
for (Bot bot : bots) {
Location loc = bot.getLocation();
World world = loc.getWorld();
if (world == null) continue;
loc.setX(loc.getBlockX());
loc.setY(loc.getBlockY());
loc.setZ(loc.getBlockZ());
loc.add(0.5, -1.5, 0.5);
ArmorStand seat = (ArmorStand) world.spawnEntity(loc, EntityType.ARMOR_STAND);
seat.setVisible(false);
seat.setGravity(false);
seat.setSmall(true);
seat.addPassenger(bot.getBukkitEntity());
}
}
public void look(String content) {
if (!(sender instanceof Player)) {
print("Unspecified player.");
return;
}
Player player = (Player) sender;
for (Bot bot : PlayerAI.getInstance().getManager().fetch()) {
bot.faceLocation(player.getEyeLocation());
}
}
public void printObj(String content) {
if (content.isEmpty()) {
print("null");

View File

@@ -45,4 +45,10 @@ public class MathUtils {
return false;
}
}
public static void clean(Vector vector) {
if (!NumberConversions.isFinite(vector.getX())) vector.setX(0);
if (!NumberConversions.isFinite(vector.getY())) vector.setY(0);
if (!NumberConversions.isFinite(vector.getZ())) vector.setZ(0);
}
}

View File

@@ -11,10 +11,12 @@ import java.util.Map;
public class MojangAPI {
private static final boolean CACHE_ENABLED = false;
private static final Map<String, String[]> CACHE = new HashMap<>();
public static String[] getSkin(String name) {
if (CACHE.containsKey(name)) {
if (CACHE_ENABLED && CACHE.containsKey(name)) {
return CACHE.get(name);
}

View File

@@ -4,7 +4,17 @@ import org.bukkit.GameMode;
public class PlayerUtils {
public static boolean isVulnerableGamemode(GameMode mode) {
return mode == GameMode.SURVIVAL || mode == GameMode.ADVENTURE;
public static boolean allTargetable;
public static boolean isTargetable(GameMode mode) {
return allTargetable || mode == GameMode.SURVIVAL || mode == GameMode.ADVENTURE;
}
public static void setAllTargetable(boolean b) {
allTargetable = b;
}
public static boolean getAllTargetable() {
return allTargetable;
}
}