Skip to content

Commit

Permalink
Fixed bugs with boxes and added code improvements
Browse files Browse the repository at this point in the history
- Removed useless instances of some utils in which are static but I made an instance for a long time ago because I sucked at java.
- Added Block.collisionBoxes to boundingBox checks because of problems with certain block boundingboxes getting called but theyre always the same boundingBox type no matter orientation.
  • Loading branch information
funkemunky committed Oct 18, 2019
1 parent 097cec9 commit b27197f
Show file tree
Hide file tree
Showing 16 changed files with 427 additions and 400 deletions.
4 changes: 2 additions & 2 deletions AtlasParent/Atlas/buildNumber.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#maven.buildNumber.plugin properties file
#Wed Oct 16 12:09:32 EDT 2019
buildNumber=427
#Fri Oct 18 15:34:54 EDT 2019
buildNumber=432
4 changes: 0 additions & 4 deletions AtlasParent/Atlas/src/main/java/cc/funkemunky/api/Atlas.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ public void onEnable() {
blockBoxManager = new BlockBoxManager();
commandManager = new CommandManager(this);
funkeCommandManager = new FunkeCommandManager();
new BlockUtils();
new ReflectionsUtil();
new Color();
new MiscUtils();

updater = new Updater();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@

public class BlockUtils {

public static Map<Material, BoundingBox> collisionBoundingBoxes;
public static Map<Material, BoundingBox> collisionBoundingBoxes = new HashMap<>();;
public static Map<Integer, BlockBounds> blockBounds = new HashMap<>();

public BlockUtils() {
collisionBoundingBoxes = new HashMap<>();

setupCollisionBB();
}

public static Block getBlock(Location location) {
if (Atlas.getInstance().getBlockBoxManager().getBlockBox().isChunkLoaded(location)) {
return location.getBlock();
Expand Down Expand Up @@ -344,7 +338,7 @@ public static Location findGround(World world, Location point) {
return point;
}

private void setupCollisionBB() {
private static void setupCollisionBB() {
if(ProtocolVersion.getGameVersion().isBelow(ProtocolVersion.V1_13)) {
collisionBoundingBoxes.put(Material.getMaterial("FIRE"), new BoundingBox((float) 0.0, (float) 0.0, (float) 0.0, (float) 1.0, (float) 1.0, (float) 1.0));
collisionBoundingBoxes.put(Material.getMaterial("STONE_PLATE"), new BoundingBox((float) 0.0625, (float) 0.0, (float) 0.0625, (float) 0.9375, (float) 0.0625, (float) 0.9375));
Expand Down Expand Up @@ -441,5 +435,9 @@ private void setupCollisionBB() {
collisionBoundingBoxes.put(Material.getMaterial("STATIONARY_LAVA"), new BoundingBox(0, 0, 0, 0.9f, 0.9f, 0.9f));
}
}

static {
setupCollisionBB();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ public class Color {
public static final String Purple = ChatColor.DARK_PURPLE.toString();
public static final String Black = ChatColor.BLACK.toString();
public static final String Underline = ChatColor.UNDERLINE.toString();
private static Color instance;

public Color() {
instance = this;
}

public static String translate(String string) {
return ChatColor.translateAlternateColorCodes('&', string);
Expand All @@ -44,7 +39,7 @@ public static String getColorFromString(String string) {
if (string.contains("&")) {
return ChatColor.translateAlternateColorCodes('&', string);
} else {
String color = (String) ReflectionsUtil.getFieldValue(ReflectionsUtil.getFieldByName(instance.getClass(), string), instance);
String color = (String) ReflectionsUtil.getFieldValue(ReflectionsUtil.getFieldByName(null, string), null);

if (color == null) {
Bukkit.getLogger().log(Level.WARNING, "The color '" + string + "' does not exist.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,7 @@

public class MiscUtils {

public static Map<EntityType, Vector> entityDimensions;

public MiscUtils() {
entityDimensions = new HashMap<>();

entityDimensions.put(EntityType.WOLF, new Vector(0.31, 0.8, 0.31));
entityDimensions.put(EntityType.SHEEP, new Vector(0.45, 1.3, 0.45));
entityDimensions.put(EntityType.COW, new Vector(0.45, 1.3, 0.45));
entityDimensions.put(EntityType.PIG, new Vector(0.45, 0.9, 0.45));
entityDimensions.put(EntityType.MUSHROOM_COW, new Vector(0.45, 1.3, 0.45));
entityDimensions.put(EntityType.WITCH, new Vector(0.31, 1.95, 0.31));
entityDimensions.put(EntityType.BLAZE, new Vector(0.31, 1.8, 0.31));
entityDimensions.put(EntityType.PLAYER, new Vector(0.3, 1.8, 0.3));
entityDimensions.put(EntityType.VILLAGER, new Vector(0.31, 1.8, 0.31));
entityDimensions.put(EntityType.CREEPER, new Vector(0.31, 1.8, 0.31));
entityDimensions.put(EntityType.GIANT, new Vector(1.8, 10.8, 1.8));
entityDimensions.put(EntityType.SKELETON, new Vector(0.31, 1.8, 0.31));
entityDimensions.put(EntityType.ZOMBIE, new Vector(0.31, 1.8, 0.31));
entityDimensions.put(EntityType.SNOWMAN, new Vector(0.35, 1.9, 0.35));
entityDimensions.put(EntityType.HORSE, new Vector(0.7, 1.6, 0.7));
entityDimensions.put(EntityType.ENDER_DRAGON, new Vector(1.5, 1.5, 1.5));
entityDimensions.put(EntityType.ENDERMAN, new Vector(0.31, 2.9, 0.31));
entityDimensions.put(EntityType.CHICKEN, new Vector(0.2, 0.7, 0.2));
entityDimensions.put(EntityType.OCELOT, new Vector(0.31, 0.7, 0.31));
entityDimensions.put(EntityType.SPIDER, new Vector(0.7, 0.9, 0.7));
entityDimensions.put(EntityType.WITHER, new Vector(0.45, 3.5, 0.45));
entityDimensions.put(EntityType.IRON_GOLEM, new Vector(0.7, 2.9, 0.7));
entityDimensions.put(EntityType.GHAST, new Vector(2, 4, 2));
}
public static Map<EntityType, Vector> entityDimensions = new HashMap<>();;

public static boolean containsIgnoreCase(String toCheck, String contains) {
return toCheck.toLowerCase().contains(contains.toLowerCase());
Expand Down Expand Up @@ -306,5 +278,31 @@ private static Plugin getPlugin(final String p) {
public static void printToConsole(String string) {
Atlas.getInstance().getConsoleSender().sendMessage(Color.translate(string));
}

static {
entityDimensions.put(EntityType.WOLF, new Vector(0.31, 0.8, 0.31));
entityDimensions.put(EntityType.SHEEP, new Vector(0.45, 1.3, 0.45));
entityDimensions.put(EntityType.COW, new Vector(0.45, 1.3, 0.45));
entityDimensions.put(EntityType.PIG, new Vector(0.45, 0.9, 0.45));
entityDimensions.put(EntityType.MUSHROOM_COW, new Vector(0.45, 1.3, 0.45));
entityDimensions.put(EntityType.WITCH, new Vector(0.31, 1.95, 0.31));
entityDimensions.put(EntityType.BLAZE, new Vector(0.31, 1.8, 0.31));
entityDimensions.put(EntityType.PLAYER, new Vector(0.3, 1.8, 0.3));
entityDimensions.put(EntityType.VILLAGER, new Vector(0.31, 1.8, 0.31));
entityDimensions.put(EntityType.CREEPER, new Vector(0.31, 1.8, 0.31));
entityDimensions.put(EntityType.GIANT, new Vector(1.8, 10.8, 1.8));
entityDimensions.put(EntityType.SKELETON, new Vector(0.31, 1.8, 0.31));
entityDimensions.put(EntityType.ZOMBIE, new Vector(0.31, 1.8, 0.31));
entityDimensions.put(EntityType.SNOWMAN, new Vector(0.35, 1.9, 0.35));
entityDimensions.put(EntityType.HORSE, new Vector(0.7, 1.6, 0.7));
entityDimensions.put(EntityType.ENDER_DRAGON, new Vector(1.5, 1.5, 1.5));
entityDimensions.put(EntityType.ENDERMAN, new Vector(0.31, 2.9, 0.31));
entityDimensions.put(EntityType.CHICKEN, new Vector(0.2, 0.7, 0.2));
entityDimensions.put(EntityType.OCELOT, new Vector(0.31, 0.7, 0.31));
entityDimensions.put(EntityType.SPIDER, new Vector(0.7, 0.9, 0.7));
entityDimensions.put(EntityType.WITHER, new Vector(0.45, 3.5, 0.45));
entityDimensions.put(EntityType.IRON_GOLEM, new Vector(0.7, 2.9, 0.7));
entityDimensions.put(EntityType.GHAST, new Vector(2, 4, 2));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ public class ReflectionsUtil {
private static Method getCubes = getMethod(World, "a", getNMSClass("AxisAlignedBB"));
private static Method getCubes1_12 = getMethod(World, "getCubes", getNMSClass("Entity"), getNMSClass("AxisAlignedBB"));

public ReflectionsUtil() {
if (ProtocolVersion.getGameVersion().isOrAbove(ProtocolVersion.V1_8)) {
iBlockData = getNMSClass("IBlockData");
blockPosition = getNMSClass("BlockPosition");
iBlockAccess = getNMSClass("IBlockAccess");
}
}

public static Object getEntityPlayer(Player player) {
return getMethodValue(getMethod(CraftPlayer, "getHandle"), player);
}
Expand Down Expand Up @@ -579,4 +571,12 @@ public static Object newInstance(Class<?> objectClass, Object... args) {
public static Class<?> getNMSClass(String string) {
return getClass("net.minecraft.server." + version + "." + string);
}

static {
if (ProtocolVersion.getGameVersion().isOrAbove(ProtocolVersion.V1_8)) {
iBlockData = getNMSClass("IBlockData");
blockPosition = getNMSClass("BlockPosition");
iBlockAccess = getNMSClass("IBlockAccess");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,43 +50,48 @@ public List<BoundingBox> getCollidingBoxes(org.bukkit.World world, BoundingBox b
locs.parallelStream().forEach(loc -> {
org.bukkit.block.Block block = loc.getBlock();
if (block != null && !block.getType().equals(Material.AIR)) {
int x = block.getX(), y = block.getY(), z = block.getZ();

BlockPosition pos = new BlockPosition(x, y, z);
World nmsWorld = ((CraftWorld) world).getHandle();
IBlockData nmsiBlockData = ((CraftWorld) world).getHandle().getType(pos);
Block nmsBlock = nmsiBlockData.getBlock();
List<AxisAlignedBB> preBoxes = new ArrayList<>();

nmsBlock.updateState(nmsiBlockData, nmsWorld, pos);
nmsBlock.a(nmsiBlockData,
nmsWorld,
pos,
(AxisAlignedBB) box.toAxisAlignedBB(),
preBoxes,
null);

if (preBoxes.size() > 0) {
for (AxisAlignedBB aabb : preBoxes) {
BoundingBox bb = new BoundingBox(
(float)aabb.a,
(float)aabb.b,
(float)aabb.c,
(float)aabb.d,
(float)aabb.e,
(float)aabb.f);
if(BlockUtils.collisionBoundingBoxes.containsKey(block.getType())) {
BoundingBox box2 = BlockUtils.collisionBoundingBoxes.get(block.getType()).add(block.getLocation().toVector());
boxes.add(box2);
} else {
int x = block.getX(), y = block.getY(), z = block.getZ();

BlockPosition pos = new BlockPosition(x, y, z);
World nmsWorld = ((CraftWorld) world).getHandle();
IBlockData nmsiBlockData = ((CraftWorld) world).getHandle().getType(pos);
Block nmsBlock = nmsiBlockData.getBlock();
List<AxisAlignedBB> preBoxes = new ArrayList<>();

nmsBlock.updateState(nmsiBlockData, nmsWorld, pos);
nmsBlock.a(nmsiBlockData,
nmsWorld,
pos,
(AxisAlignedBB) box.toAxisAlignedBB(),
preBoxes,
null);

if (preBoxes.size() > 0) {
for (AxisAlignedBB aabb : preBoxes) {
BoundingBox bb = new BoundingBox(
(float)aabb.a,
(float)aabb.b,
(float)aabb.c,
(float)aabb.d,
(float)aabb.e,
(float)aabb.f);

if(bb.collides(box)) {
boxes.add(bb);
}
}
} else {
BoundingBox bb = ReflectionsUtil.toBoundingBox(nmsBlock.a(nmsiBlockData, nmsWorld, pos))
.add(x, y, z, x, y, z);

if(bb.collides(box)) {
boxes.add(bb);
}
}
} else {
BoundingBox bb = ReflectionsUtil.toBoundingBox(nmsBlock.a(nmsiBlockData, nmsWorld, pos))
.add(x, y, z, x, y, z);

if(bb.collides(box)) {
boxes.add(bb);
}
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,65 +51,70 @@ public List<BoundingBox> getCollidingBoxes(org.bukkit.World world, BoundingBox b
locs.parallelStream().forEach(loc -> {
org.bukkit.block.Block block = loc.getBlock();
if (block != null && !block.getType().equals(Material.AIR)) {
int x = block.getX(), y = block.getY(), z = block.getZ();

BlockPosition pos = new BlockPosition(x, y, z);
World nmsWorld = ((CraftWorld) world).getHandle();
IBlockData nmsiBlockData = ((CraftWorld) world).getHandle().getType(pos);
Block nmsBlock = nmsiBlockData.getBlock();
List<AxisAlignedBB> preBoxes = new ArrayList<>();

nmsBlock.updateState(nmsiBlockData, nmsWorld, pos);
nmsBlock.a(nmsiBlockData,
nmsWorld,
pos,
(AxisAlignedBB) box.toAxisAlignedBB(),
preBoxes,
null,
true);

if (preBoxes.size() > 0) {
for (AxisAlignedBB aabb : preBoxes) {
BoundingBox bb = new BoundingBox(
(float)aabb.a,
(float)aabb.b,
(float)aabb.c,
(float)aabb.d,
(float)aabb.e,
(float)aabb.f);

if(bb.collides(box)) {
boxes.add(bb);
if(BlockUtils.collisionBoundingBoxes.containsKey(block.getType())) {
BoundingBox box2 = BlockUtils.collisionBoundingBoxes.get(block.getType()).add(block.getLocation().toVector());
boxes.add(box2);
} else {
int x = block.getX(), y = block.getY(), z = block.getZ();

BlockPosition pos = new BlockPosition(x, y, z);
World nmsWorld = ((CraftWorld) world).getHandle();
IBlockData nmsiBlockData = ((CraftWorld) world).getHandle().getType(pos);
Block nmsBlock = nmsiBlockData.getBlock();
List<AxisAlignedBB> preBoxes = new ArrayList<>();

nmsBlock.updateState(nmsiBlockData, nmsWorld, pos);
nmsBlock.a(nmsiBlockData,
nmsWorld,
pos,
(AxisAlignedBB) box.toAxisAlignedBB(),
preBoxes,
null,
false);

if (preBoxes.size() > 0) {
for (AxisAlignedBB aabb : preBoxes) {
BoundingBox bb = new BoundingBox(
(float)aabb.a,
(float)aabb.b,
(float)aabb.c,
(float)aabb.d,
(float)aabb.e,
(float)aabb.f);

if(bb.collides(box)) {
boxes.add(bb);
}
}
}

if (nmsBlock instanceof BlockShulkerBox) {
TileEntity tileentity = nmsWorld.getTileEntity(pos);
BlockShulkerBox shulker = (BlockShulkerBox) nmsBlock;

if (tileentity instanceof TileEntityShulkerBox) {
TileEntityShulkerBox entity = (TileEntityShulkerBox) tileentity;
//Bukkit.broadcastMessage("entity");
boxes.add(ReflectionsUtil.toBoundingBox(entity.a(nmsiBlockData)));

if (entity.p().toString().contains("OPEN")
|| entity.p().toString().contains("CLOSING")) {
boxes.add(new BoundingBox(
block.getX(),
block.getY(),
block.getZ(),
block.getX() + 1,
block.getY() + 1.5f,
block.getZ() + 1));
if (nmsBlock instanceof BlockShulkerBox) {
TileEntity tileentity = nmsWorld.getTileEntity(pos);
BlockShulkerBox shulker = (BlockShulkerBox) nmsBlock;

if (tileentity instanceof TileEntityShulkerBox) {
TileEntityShulkerBox entity = (TileEntityShulkerBox) tileentity;
//Bukkit.broadcastMessage("entity");
boxes.add(ReflectionsUtil.toBoundingBox(entity.a(nmsiBlockData)));

if (entity.p().toString().contains("OPEN")
|| entity.p().toString().contains("CLOSING")) {
boxes.add(new BoundingBox(
block.getX(),
block.getY(),
block.getZ(),
block.getX() + 1,
block.getY() + 1.5f,
block.getZ() + 1));
}
}
}
}
} else {
BoundingBox bb = ReflectionsUtil.toBoundingBox(nmsBlock.a(nmsiBlockData, nmsWorld, pos))
.add(x, y, z, x, y, z);
} else {
BoundingBox bb = ReflectionsUtil.toBoundingBox(nmsBlock.a(nmsiBlockData, nmsWorld, pos))
.add(x, y, z, x, y, z);

if(bb.collides(box)) {
boxes.add(bb);
if(bb.collides(box)) {
boxes.add(bb);
}
}
}
}
Expand Down
Loading

0 comments on commit b27197f

Please sign in to comment.