Skip to content

Commit

Permalink
Fixed entities being removed from the world when islands are created …
Browse files Browse the repository at this point in the history
…/ disbanded (#1059)
  • Loading branch information
OmerBenGera committed Apr 21, 2022
1 parent 82dffc9 commit 45a13f8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
Expand Up @@ -127,21 +127,20 @@ private static void removeEntities(Chunk chunk) {
ChunkCoordIntPair chunkCoords = chunk.getPos();
WorldServer worldServer = (WorldServer) chunk.getWorld();

int chunkWorldCoordX = chunkCoords.b << 4;
int chunkWorldCoordZ = chunkCoords.c << 4;

AxisAlignedBB chunkBounds = new AxisAlignedBB(
chunkCoords.b << 4, 0, chunkCoords.c << 4,
chunkCoords.b << 4 + 15, chunk.getWorld().getMaxBuildHeight(), chunkCoords.c << 4 + 15
);
chunkWorldCoordX, 0, chunkWorldCoordZ,
chunkWorldCoordX + 15, chunk.getWorld().getMaxBuildHeight(), chunkWorldCoordZ + 15);

Iterator<Entity> chunkEntities;

try {
chunkEntities = chunk.entities.iterator();
} catch (Throwable ex) {
List<Entity> worldEntities = new ArrayList<>();
worldServer.getEntities().a().forEach(entity -> {
if (entity.getBoundingBox().c(chunkBounds))
worldEntities.add(entity);
});
worldServer.getEntities().a(chunkBounds, worldEntities::add);
chunkEntities = worldEntities.iterator();
}

Expand Down
Expand Up @@ -122,22 +122,21 @@ private static void removeEntities(ChunkAccess chunk) {
int minBuildHeight = worldServer.getWorld().getMinHeight();
int maxBuildHeight = worldServer.getWorld().getMaxHeight();

int chunkWorldCoordX = chunkCoords.getX() << 4;
int chunkWorldCoordZ = chunkCoords.getZ() << 4;

net.minecraft.world.phys.AxisAlignedBB chunkBounds = new net.minecraft.world.phys.AxisAlignedBB(
chunkCoords.getX() << 4, minBuildHeight, chunkCoords.getZ() << 4,
chunkCoords.getX() << 4 + 15, maxBuildHeight, chunkCoords.getZ() << 4 + 15
);
chunkWorldCoordX, minBuildHeight, chunkWorldCoordZ,
chunkWorldCoordX + 15, maxBuildHeight, chunkWorldCoordZ + 15);

List<net.minecraft.world.entity.Entity> worldEntities = new ArrayList<>();
worldServer.getEntities().get(chunkBounds, worldEntities::add);

List<Entity> worldEntities = new ArrayList<>();
worldServer.getEntities().getAll().forEach(nmsEntity -> {
worldEntities.forEach(nmsEntity -> {
Entity entity = new Entity(nmsEntity);
if (entity.getBoundingBox().intercepts(chunkBounds))
worldEntities.add(entity);
if (!(entity.getHandle() instanceof EntityHuman))
entity.setRemoved(net.minecraft.world.entity.Entity.RemovalReason.b);
});

for (Entity worldEntity : worldEntities) {
if (!(worldEntity.getHandle() instanceof EntityHuman))
worldEntity.setRemoved(net.minecraft.world.entity.Entity.RemovalReason.b);
}
}

private static void removeBlocks(ChunkAccess chunk) {
Expand Down
Expand Up @@ -2,6 +2,9 @@

import com.bgsoftware.superiorskyblock.nms.v1_18_R1.mapping.MappedObject;
import net.minecraft.world.level.entity.EntityAccess;
import net.minecraft.world.phys.AxisAlignedBB;

import java.util.function.Consumer;

public final class LevelEntityGetter<T extends EntityAccess> extends
MappedObject<net.minecraft.world.level.entity.LevelEntityGetter<T>> {
Expand All @@ -10,8 +13,8 @@ public LevelEntityGetter(net.minecraft.world.level.entity.LevelEntityGetter<T> h
super(handle);
}

public Iterable<T> getAll() {
return handle.a();
public void get(AxisAlignedBB boundingBox, Consumer<T> consumer) {
handle.a(boundingBox, consumer);
}

}
Expand Up @@ -124,22 +124,21 @@ private static void removeEntities(ChunkAccess chunk) {
int minBuildHeight = worldServer.getWorld().getMinHeight();
int maxBuildHeight = worldServer.getWorld().getMaxHeight();

int chunkWorldCoordX = chunkCoords.getX() << 4;
int chunkWorldCoordZ = chunkCoords.getZ() << 4;

net.minecraft.world.phys.AxisAlignedBB chunkBounds = new net.minecraft.world.phys.AxisAlignedBB(
chunkCoords.getX() << 4, minBuildHeight, chunkCoords.getZ() << 4,
chunkCoords.getX() << 4 + 15, maxBuildHeight, chunkCoords.getZ() << 4 + 15
);
chunkWorldCoordX, minBuildHeight, chunkWorldCoordZ,
chunkWorldCoordX + 15, maxBuildHeight, chunkWorldCoordZ + 15);

List<net.minecraft.world.entity.Entity> worldEntities = new ArrayList<>();
worldServer.getEntities().get(chunkBounds, worldEntities::add);

List<Entity> worldEntities = new ArrayList<>();
worldServer.getEntities().getAll().forEach(nmsEntity -> {
worldEntities.forEach(nmsEntity -> {
Entity entity = new Entity(nmsEntity);
if (entity.getBoundingBox().intercepts(chunkBounds))
worldEntities.add(entity);
if (!(entity.getHandle() instanceof EntityHuman))
entity.setRemoved(net.minecraft.world.entity.Entity.RemovalReason.b);
});

for (Entity worldEntity : worldEntities) {
if (!(worldEntity.getHandle() instanceof EntityHuman))
worldEntity.setRemoved(net.minecraft.world.entity.Entity.RemovalReason.b);
}
}

private static void removeBlocks(ChunkAccess chunk) {
Expand Down
Expand Up @@ -2,6 +2,9 @@

import com.bgsoftware.superiorskyblock.nms.v1_18_R2.mapping.MappedObject;
import net.minecraft.world.level.entity.EntityAccess;
import net.minecraft.world.phys.AxisAlignedBB;

import java.util.function.Consumer;

public final class LevelEntityGetter<T extends EntityAccess> extends
MappedObject<net.minecraft.world.level.entity.LevelEntityGetter<T>> {
Expand All @@ -10,8 +13,8 @@ public LevelEntityGetter(net.minecraft.world.level.entity.LevelEntityGetter<T> h
super(handle);
}

public Iterable<T> getAll() {
return handle.a();
public void get(AxisAlignedBB boundingBox, Consumer<T> consumer) {
handle.a(boundingBox, consumer);
}

}

0 comments on commit 45a13f8

Please sign in to comment.