Fixes + improve standingon

-Blocks standing on is now a list
-Fixed a stuck issue when clipping into walls
This commit is contained in:
ThisTestUser
2022-09-30 17:51:02 -04:00
parent de17a89235
commit a736e59262
5 changed files with 122 additions and 17 deletions

View File

@@ -12,6 +12,7 @@ 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;
import java.util.List;
import java.util.UUID; import java.util.UUID;
public interface Terminator { public interface Terminator {
@@ -52,7 +53,7 @@ public interface Terminator {
boolean isBotOnGround(); boolean isBotOnGround();
Block getStandingOn(); List<Block> getStandingOn();
void setBotPitch(float pitch); void setBotPitch(float pitch);

View File

@@ -11,6 +11,7 @@ import net.nuggetmc.tplus.api.event.BotDamageByPlayerEvent;
import net.nuggetmc.tplus.api.event.BotDeathEvent; import net.nuggetmc.tplus.api.event.BotDeathEvent;
import net.nuggetmc.tplus.api.event.BotFallDamageEvent; import net.nuggetmc.tplus.api.event.BotFallDamageEvent;
import net.nuggetmc.tplus.api.event.TerminatorLocateTargetEvent; import net.nuggetmc.tplus.api.event.TerminatorLocateTargetEvent;
import net.nuggetmc.tplus.api.utils.BotUtils;
import net.nuggetmc.tplus.api.utils.MathUtils; import net.nuggetmc.tplus.api.utils.MathUtils;
import net.nuggetmc.tplus.api.utils.PlayerUtils; import net.nuggetmc.tplus.api.utils.PlayerUtils;
import org.bukkit.*; import org.bukkit.*;
@@ -445,6 +446,71 @@ public class LegacyAgent extends Agent {
LegacyLevel level = null; LegacyLevel level = null;
Block get = null; Block get = null;
BoundingBox box = player.getBoundingBox();
double[] xVals = new double[]{
box.getMinX(),
box.getMaxX() - 0.01
};
double[] zVals = new double[]{
box.getMinZ(),
box.getMaxZ() - 0.01
};
List<Location> locStanding = new ArrayList<>();
for (double x : xVals) {
for (double z : zVals) {
Location loc = new Location(player.getWorld(), Math.floor(x), npc.getLocation().getBlockY(), Math.floor(z));
if (!locStanding.contains(loc))
locStanding.add(loc);
}
}
Collections.sort(locStanding, (a, b) ->
Double.compare(BotUtils.getHorizSqDist(a, player.getLocation()), BotUtils.getHorizSqDist(b, player.getLocation())));
//Break potential obstructing walls
for (Location loc : locStanding) {
boolean up = false;
get = loc.getBlock();
if (!LegacyMats.FENCE.contains(get.getType())) {
up = true;
get = loc.add(0, 1, 0).getBlock();
if (!LegacyMats.FENCE.contains(get.getType())) {
get = null;
}
}
if (get != null) {
int distanceX = get.getLocation().getBlockX() - player.getLocation().getBlockX();
int distanceZ = get.getLocation().getBlockZ() - player.getLocation().getBlockZ();
if (distanceX == 1 && distanceZ == 0) {
if (dir == BlockFace.NORTH || dir == BlockFace.SOUTH) {
npc.faceLocation(get.getLocation());
level = up ? LegacyLevel.EAST : LegacyLevel.EAST_D;
}
} else if (distanceX == -1 && distanceZ == 0) {
if (dir == BlockFace.NORTH || dir == BlockFace.SOUTH) {
npc.faceLocation(get.getLocation());
level = up ? LegacyLevel.WEST : LegacyLevel.WEST_D;
}
} else if (distanceX == 0 && distanceZ == 1) {
if (dir == BlockFace.EAST || dir == BlockFace.WEST) {
npc.faceLocation(get.getLocation());
level = up ? LegacyLevel.SOUTH : LegacyLevel.SOUTH_D;
}
} else if (distanceX == 0 && distanceZ == -1) {
if (dir == BlockFace.EAST || dir == BlockFace.WEST) {
npc.faceLocation(get.getLocation());
level = up ? LegacyLevel.NORTH : LegacyLevel.NORTH_D;
}
}
if (level != null) {
preBreak(npc, player, get, level);
return level;
}
}
}
switch (dir) { switch (dir) {
case NORTH: case NORTH:
get = player.getLocation().add(0, 1, -1).getBlock(); get = player.getLocation().add(0, 1, -1).getBlock();
@@ -457,7 +523,7 @@ public class LegacyAgent extends Agent {
get = get.getLocation().add(0, -2, 0).getBlock(); get = get.getLocation().add(0, -2, 0).getBlock();
level = LegacyLevel.NORTH_D_2; level = LegacyLevel.NORTH_D_2;
} else { } else {
Block standing = npc.getStandingOn(); Block standing = npc.getStandingOn().isEmpty() ? null : npc.getStandingOn().get(0);
if(standing == null) if(standing == null)
break; break;
boolean obstructed = standing.getLocation().getBlockY() == player.getLocation().getBlockY() boolean obstructed = standing.getLocation().getBlockY() == player.getLocation().getBlockY()
@@ -500,7 +566,7 @@ public class LegacyAgent extends Agent {
get = get.getLocation().add(0, -2, 0).getBlock(); get = get.getLocation().add(0, -2, 0).getBlock();
level = LegacyLevel.SOUTH_D_2; level = LegacyLevel.SOUTH_D_2;
} else { } else {
Block standing = npc.getStandingOn(); Block standing = npc.getStandingOn().isEmpty() ? null : npc.getStandingOn().get(0);
if(standing == null) if(standing == null)
break; break;
boolean obstructed = standing.getLocation().getBlockY() == player.getLocation().getBlockY() boolean obstructed = standing.getLocation().getBlockY() == player.getLocation().getBlockY()
@@ -543,7 +609,7 @@ public class LegacyAgent extends Agent {
get = get.getLocation().add(0, -2, 0).getBlock(); get = get.getLocation().add(0, -2, 0).getBlock();
level = LegacyLevel.EAST_D_2; level = LegacyLevel.EAST_D_2;
} else { } else {
Block standing = npc.getStandingOn(); Block standing = npc.getStandingOn().isEmpty() ? null : npc.getStandingOn().get(0);
if(standing == null) if(standing == null)
break; break;
boolean obstructed = standing.getLocation().getBlockY() == player.getLocation().getBlockY() boolean obstructed = standing.getLocation().getBlockY() == player.getLocation().getBlockY()
@@ -586,7 +652,7 @@ public class LegacyAgent extends Agent {
get = get.getLocation().add(0, -2, 0).getBlock(); get = get.getLocation().add(0, -2, 0).getBlock();
level = LegacyLevel.WEST_D_2; level = LegacyLevel.WEST_D_2;
} else { } else {
Block standing = npc.getStandingOn(); Block standing = npc.getStandingOn().isEmpty() ? null : npc.getStandingOn().get(0);
if(standing == null) if(standing == null)
break; break;
boolean obstructed = standing.getLocation().getBlockY() == player.getLocation().getBlockY() boolean obstructed = standing.getLocation().getBlockY() == player.getLocation().getBlockY()
@@ -782,7 +848,7 @@ public class LegacyAgent extends Agent {
return false; return false;
if (c && npc.getLocation().getBlockY() > loc.getBlockY() + 1) { if (c && npc.getLocation().getBlockY() > loc.getBlockY() + 1) {
Block block = npc.getStandingOn(); Block block = npc.getStandingOn().isEmpty() ? null : npc.getStandingOn().get(0);
if (block == null) if (block == null)
return false; return false;
npc.look(BlockFace.DOWN); npc.look(BlockFace.DOWN);
@@ -798,7 +864,7 @@ public class LegacyAgent extends Agent {
b.setY(0); b.setY(0);
if (npc.getLocation().getBlockY() > loc.getBlockY() + 10 && a.distance(b) < 10) { if (npc.getLocation().getBlockY() > loc.getBlockY() + 10 && a.distance(b) < 10) {
Block block = npc.getStandingOn(); Block block = npc.getStandingOn().isEmpty() ? null : npc.getStandingOn().get(0);
if (block == null) if (block == null)
return false; return false;
npc.look(BlockFace.DOWN); npc.look(BlockFace.DOWN);
@@ -938,7 +1004,7 @@ public class LegacyAgent extends Agent {
cur = player.getLocation().add(0, 2, 0).getBlock(); cur = player.getLocation().add(0, 2, 0).getBlock();
break; break;
case BELOW: case BELOW:
cur = bot.getStandingOn(); cur = bot.getStandingOn().isEmpty() ? null : bot.getStandingOn().get(0);
break; break;
case NORTH_U: case NORTH_U:
cur = player.getLocation().add(0, 2, -1).getBlock(); cur = player.getLocation().add(0, 2, -1).getBlock();

View File

@@ -1,21 +1,31 @@
package net.nuggetmc.tplus.api.event; package net.nuggetmc.tplus.api.event;
import java.util.List;
import org.bukkit.block.Block;
import net.nuggetmc.tplus.api.Terminator; import net.nuggetmc.tplus.api.Terminator;
public class BotFallDamageEvent { public class BotFallDamageEvent {
private final Terminator bot; private final Terminator bot;
private final List<Block> standingOn;
private boolean cancelled; private boolean cancelled;
public BotFallDamageEvent(Terminator bot) { public BotFallDamageEvent(Terminator bot, List<Block> standingOn) {
this.bot = bot; this.bot = bot;
this.standingOn = standingOn;
} }
public Terminator getBot() { public Terminator getBot() {
return bot; return bot;
} }
public List<Block> getStandingOn() {
return standingOn;
}
public boolean isCancelled() { public boolean isCancelled() {
return cancelled; return cancelled;
} }

View File

@@ -1,7 +1,10 @@
package net.nuggetmc.tplus.api.utils; package net.nuggetmc.tplus.api.utils;
import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.util.BoundingBox; import org.bukkit.util.BoundingBox;
import org.bukkit.util.NumberConversions;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@@ -13,7 +16,9 @@ public class BotUtils {
Material.WATER, Material.WATER,
Material.LAVA, Material.LAVA,
Material.TWISTING_VINES, Material.TWISTING_VINES,
Material.VINE Material.VINE,
Material.LADDER,
Material.COBWEB
)); ));
public static UUID randomSteveUUID() { public static UUID randomSteveUUID() {
@@ -29,4 +34,8 @@ public class BotUtils {
public static boolean solidAt(BoundingBox playerBox, BoundingBox blockBox) { public static boolean solidAt(BoundingBox playerBox, BoundingBox blockBox) {
return playerBox.overlaps(blockBox); return playerBox.overlaps(blockBox);
} }
public static double getHorizSqDist(Location blockLoc, Location pLoc) {
return NumberConversions.square(blockLoc.getX() + 0.5 - pLoc.getX()) + NumberConversions.square(blockLoc.getZ() + 0.5 - pLoc.getZ());
}
} }

View File

@@ -51,6 +51,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.UUID; import java.util.UUID;
@@ -74,6 +75,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 List<Block> standingOn = new ArrayList<>();
private boolean ignoredByMobs = true; private boolean ignoredByMobs = true;
private UUID targetPlayer = null; private UUID targetPlayer = null;
private Bot(MinecraftServer minecraftServer, ServerLevel worldServer, GameProfile profile) { private Bot(MinecraftServer minecraftServer, ServerLevel worldServer, GameProfile profile) {
@@ -392,7 +394,7 @@ public class Bot extends ServerPlayer implements Terminator {
private void fallDamageCheck() { // TODO create a better bot event system in the future, also have bot.getAgent() private void fallDamageCheck() { // TODO create a better bot event system in the future, also have bot.getAgent()
if (groundTicks != 0 && noFallTicks == 0 && !(oldVelocity.getY() >= -0.8) && !BotUtils.NO_FALL.contains(getLocation().getBlock().getType())) { if (groundTicks != 0 && noFallTicks == 0 && !(oldVelocity.getY() >= -0.8) && !BotUtils.NO_FALL.contains(getLocation().getBlock().getType())) {
BotFallDamageEvent event = new BotFallDamageEvent(this); BotFallDamageEvent event = new BotFallDamageEvent(this, new ArrayList<>(getStandingOn()));
plugin.getManager().getAgent().onFallDamage(event); plugin.getManager().getAgent().onFallDamage(event);
@@ -526,11 +528,10 @@ public class Bot extends ServerPlayer implements Terminator {
return false; return false;
} }
return getStandingOn() != null; return checkStandingOn();
} }
@Override public boolean checkStandingOn() {
public Block getStandingOn() {
World world = getBukkitEntity().getWorld(); World world = getBukkitEntity().getWorld();
AABB box = getBoundingBox(); AABB box = getBoundingBox();
@@ -545,6 +546,8 @@ public class Bot extends ServerPlayer implements Terminator {
}; };
BoundingBox playerBox = new BoundingBox(box.minX, position().y - 0.01, box.minZ, BoundingBox playerBox = new BoundingBox(box.minX, position().y - 0.01, box.minZ,
box.maxX, position().y + getBbHeight(), box.maxZ); box.maxX, position().y + getBbHeight(), box.maxZ);
List<Block> standingOn = new ArrayList<>();
List<Location> locations = new ArrayList<>();
for (double x : xVals) { for (double x : xVals) {
for (double z : zVals) { for (double z : zVals) {
@@ -552,7 +555,10 @@ public class Bot extends ServerPlayer implements Terminator {
Block block = world.getBlockAt(loc); Block block = world.getBlockAt(loc);
if ((block.getType().isSolid() || canStandOn(block.getType())) && BotUtils.solidAt(playerBox, block.getBoundingBox())) { if ((block.getType().isSolid() || canStandOn(block.getType())) && BotUtils.solidAt(playerBox, block.getBoundingBox())) {
return block; if (!locations.contains(block.getLocation())) {
standingOn.add(block);
locations.add(block.getLocation());
}
} }
} }
} }
@@ -568,12 +574,25 @@ public class Bot extends ServerPlayer implements Terminator {
if ((LegacyMats.FENCE.contains(block.getType()) || LegacyMats.GATES.contains(block.getType())) if ((LegacyMats.FENCE.contains(block.getType()) || LegacyMats.GATES.contains(block.getType()))
&& block.getType().isSolid() && BotUtils.solidAt(playerBox, modifiedBox)) { && block.getType().isSolid() && BotUtils.solidAt(playerBox, modifiedBox)) {
return block; if (!locations.contains(block.getLocation())) {
standingOn.add(block);
locations.add(block.getLocation());
}
} }
} }
} }
return null; //Closest block comes first
Collections.sort(standingOn, (a, b) ->
Double.compare(BotUtils.getHorizSqDist(a.getLocation(), getLocation()), BotUtils.getHorizSqDist(b.getLocation(), getLocation())));
this.standingOn = standingOn;
return !standingOn.isEmpty();
}
@Override
public List<Block> getStandingOn() {
return standingOn;
} }
/** /**