Skip to content

Commit

Permalink
Reverted to old BlockBox#getCollidingBlocks and Improvements
Browse files Browse the repository at this point in the history
Reverted back to old method because the new and "improved" one didn't work very well at all. I also cleaned up the code to fit in with the Java Conventions and improved the performance slitghtly by getting rid of unnecessary code, casting, etc.
  • Loading branch information
funkemunky committed Oct 18, 2019
1 parent f6de0a0 commit 097cec9
Show file tree
Hide file tree
Showing 12 changed files with 667 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public List<BoundingBox> getCollidingBoxes(org.bukkit.World world, BoundingBox b
int minZ = MathUtils.floor(box.minZ);
int maxZ = MathUtils.floor(box.maxZ + 1);

if(!isChunkLoaded(box.getMinimum().toLocation(world))) return Collections.emptyList();

List<Location> locs = new ArrayList<>();

for (int x = minX; x < maxX; x++) {
Expand All @@ -44,43 +42,89 @@ public List<BoundingBox> getCollidingBoxes(org.bukkit.World world, BoundingBox b
}
}

WorldServer vanillaWorld = ((CraftWorld)world).getHandle();
AxisAlignedBB aabb = MinecraftReflection.toAABB(box);

Vector<AxisAlignedBB> vector = new Vector<>();

locs.parallelStream().forEach(loc -> {
BlockPosition pos = new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
IBlockData blockData = vanillaWorld.c(pos);
Block block = blockData.getBlock();
block.a(blockData, vanillaWorld, pos, aabb, vector, null);
});
List<BoundingBox> boxes = Collections.synchronizedList(new ArrayList<>());

boolean chunkLoaded = isChunkLoaded(box.getMinimum().toLocation(world));

if(chunkLoaded) {
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(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);
}
}
}
});
}

return vector.parallelStream().map(MinecraftReflection::fromAABB).collect(Collectors.toList());
return boxes;
}


@Override
public List<BoundingBox> getSpecificBox(Location loc) {
return getCollidingBoxes(loc.getWorld(), new BoundingBox(loc.toVector(), loc.toVector()));
}

@Override
public boolean isChunkLoaded(Location loc) {
net.minecraft.server.v1_10_R1.World world = ((org.bukkit.craftbukkit.v1_10_R1.CraftWorld) loc.getWorld()).getHandle();

return !world.isClientSide && world.isLoaded(new net.minecraft.server.v1_10_R1.BlockPosition(loc.getBlockX(), 0, loc.getBlockZ())) && world.getChunkAtWorldCoords(new net.minecraft.server.v1_10_R1.BlockPosition(loc.getBlockX(), 0, loc.getBlockZ())).p();
net.minecraft.server.v1_10_R1.World world =
((org.bukkit.craftbukkit.v1_10_R1.CraftWorld) loc.getWorld()).getHandle();

return !world.isClientSide
&& world.isLoaded(
new net.minecraft.server.v1_10_R1.BlockPosition(loc.getBlockX(), 0, loc.getBlockZ()))
&& world.getChunkAtWorldCoords(
new net.minecraft.server.v1_10_R1.BlockPosition(loc.getBlockX(), 0, loc.getBlockZ())).p();
}

@Override
public boolean isUsingItem(Player player) {
net.minecraft.server.v1_10_R1.EntityLiving entity = ((org.bukkit.craftbukkit.v1_10_R1.entity.CraftLivingEntity) player).getHandle();
return entity.cA() != null && entity.cA().getItem().f(entity.cA()) != net.minecraft.server.v1_10_R1.EnumAnimation.NONE;
net.minecraft.server.v1_10_R1.EntityLiving entity =
((org.bukkit.craftbukkit.v1_10_R1.entity.CraftLivingEntity) player).getHandle();
return entity.cA() != null
&& entity.cA().getItem()
.f(entity.cA()) != net.minecraft.server.v1_10_R1.EnumAnimation.NONE;
}

@Override
public float getMovementFactor(Player player) {
return (float) ((CraftPlayer) player).getHandle().getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).getValue();
return (float) ((CraftPlayer) player).getHandle()
.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).getValue();
}

@Override
Expand All @@ -91,7 +135,8 @@ public boolean isRiptiding(LivingEntity entity) {
@Override
public int getTrackerId(Player player) {
EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle();
EntityTrackerEntry entry = ((WorldServer) entityPlayer.getWorld()).tracker.trackedEntities.get(entityPlayer.getId());
EntityTrackerEntry entry = ((WorldServer) entityPlayer.getWorld()).tracker.
trackedEntities.get(entityPlayer.getId());
return entry.b().getId();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public List<BoundingBox> getCollidingBoxes(org.bukkit.World world, BoundingBox b
int minZ = MathUtils.floor(box.minZ);
int maxZ = MathUtils.floor(box.maxZ + 1);

if(!isChunkLoaded(box.getMinimum().toLocation(world))) return Collections.emptyList();

List<Location> locs = new ArrayList<>();

for (int x = minX; x < maxX; x++) {
Expand All @@ -45,19 +43,80 @@ public List<BoundingBox> getCollidingBoxes(org.bukkit.World world, BoundingBox b
}
}

WorldServer vanillaWorld = ((CraftWorld)world).getHandle();
AxisAlignedBB aabb = MinecraftReflection.toAABB(box);

Vector<AxisAlignedBB> vector = new Vector<>();

locs.parallelStream().forEach(loc -> {
BlockPosition pos = new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
IBlockData blockData = vanillaWorld.c(pos);
Block block = blockData.getBlock();
block.a(blockData, vanillaWorld, pos, aabb, vector, null, true);
});
List<BoundingBox> boxes = Collections.synchronizedList(new ArrayList<>());

boolean chunkLoaded = isChunkLoaded(box.getMinimum().toLocation(world));

if(chunkLoaded) {
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 (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);

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

return vector.parallelStream().map(MinecraftReflection::fromAABB).collect(Collectors.toList());
return boxes;
}

@Override
Expand All @@ -67,9 +126,14 @@ public List<BoundingBox> getSpecificBox(Location loc) {

@Override
public boolean isChunkLoaded(Location loc) {
net.minecraft.server.v1_11_R1.World world = ((org.bukkit.craftbukkit.v1_11_R1.CraftWorld) loc.getWorld()).getHandle();

return !world.isClientSide && world.isLoaded(new net.minecraft.server.v1_11_R1.BlockPosition(loc.getBlockX(), 0, loc.getBlockZ())) && world.getChunkAtWorldCoords(new net.minecraft.server.v1_11_R1.BlockPosition(loc.getBlockX(), 0, loc.getBlockZ())).p();
net.minecraft.server.v1_11_R1.World world =
((org.bukkit.craftbukkit.v1_11_R1.CraftWorld) loc.getWorld()).getHandle();

return !world.isClientSide
&& world.isLoaded(
new net.minecraft.server.v1_11_R1.BlockPosition(loc.getBlockX(), 0, loc.getBlockZ()))
&& world.getChunkAtWorldCoords(
new net.minecraft.server.v1_11_R1.BlockPosition(loc.getBlockX(), 0, loc.getBlockZ())).p();
}

@Override
Expand All @@ -80,7 +144,8 @@ public boolean isUsingItem(Player player) {

@Override
public float getMovementFactor(Player player) {
return (float) ((CraftPlayer) player).getHandle().getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).getValue();
return (float) ((CraftPlayer) player).getHandle()
.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).getValue();
}

@Override
Expand All @@ -91,7 +156,8 @@ public boolean isRiptiding(LivingEntity entity) {
@Override
public int getTrackerId(Player player) {
EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle();
EntityTrackerEntry entry = ((WorldServer) entityPlayer.getWorld()).tracker.trackedEntities.get(entityPlayer.getId());
EntityTrackerEntry entry = ((WorldServer) entityPlayer.getWorld()).tracker.
trackedEntities.get(entityPlayer.getId());
return entry.b().getId();
}

Expand Down
Loading

0 comments on commit 097cec9

Please sign in to comment.