Fixed chunk

This commit is contained in:
TheMode 2019-08-11 08:57:23 +02:00
parent d1967186e5
commit 2afb170ff0
4 changed files with 32 additions and 29 deletions

View File

@ -16,8 +16,6 @@ public class Entity {
private boolean isActive; // False if entity has only been instanced without being added somewhere
private boolean shouldRemove;
private Object monitor = new Object();
public Entity() {
this.id = generateId();
this.uuid = UUID.randomUUID();
@ -68,7 +66,7 @@ public class Entity {
if (instance != null) {
Chunk lastChunk = instance.getChunkAt(lastX, lastZ);
Chunk newChunk = instance.getChunkAt(x, z);
if (newChunk != null && lastChunk != newChunk) {
if (lastChunk != null && newChunk != null && lastChunk != newChunk) {
synchronized (lastChunk) {
synchronized (newChunk) {
lastChunk.removeEntity(this);

View File

@ -17,27 +17,31 @@ public class EntityManager {
public void update() {
for (Instance instance : instanceManager.getInstances()) {
// Creatures
for (EntityCreature creature : instance.getCreatures()) {
creaturesPool.submit(() -> {
creature.update();
boolean shouldRemove = creature.shouldRemove();
if (shouldRemove) {
instance.removeEntity(creature);
}
});
synchronized (instance) {
// Creatures
for (EntityCreature creature : instance.getCreatures()) {
creaturesPool.submit(() -> {
creature.update();
boolean shouldRemove = creature.shouldRemove();
if (shouldRemove) {
instance.removeEntity(creature);
}
});
}
// Players
for (Player player : instance.getPlayers()) {
playersPool.submit(() -> {
player.update();
boolean shouldRemove = player.shouldRemove();
if (shouldRemove) {
instance.removeEntity(player);
}
});
}
}
// Players
for (Player player : instance.getPlayers()) {
playersPool.submit(() -> {
player.update();
boolean shouldRemove = player.shouldRemove();
if (shouldRemove) {
instance.removeEntity(player);
}
});
}
}
}

View File

@ -20,11 +20,11 @@ public class Instance {
}
// TODO BlockBatch with pool
public void setBlock(int x, int y, int z, Block block) {
Chunk chunk = getChunkAt(x, z);
public synchronized void setBlock(int x, int y, int z, Block block) {
int chunkX = Math.floorDiv(x, 16);
int chunkZ = Math.floorDiv(z, 16);
Chunk chunk = getChunk(chunkX, chunkZ);
if (chunk == null) {
int chunkX = x / 16;
int chunkZ = z / 16;
chunk = new Chunk(Biome.VOID, chunkX, chunkZ);
this.chunksSet.add(chunk);
}
@ -42,8 +42,8 @@ public class Instance {
}
public Chunk getChunkAt(double x, double z) {
int chunkX = (int) (x / 16);
int chunkZ = (int) (z / 16);
int chunkX = Math.floorDiv((int) x, 16);
int chunkZ = Math.floorDiv((int) z, 16);
return getChunk(chunkX, chunkZ);
}

View File

@ -31,8 +31,9 @@ public class LoginStartPacket implements ClientPreplayPacket {
static {
instance = Main.getInstanceManager().createInstance();
for (int x = -64; x < 64; x++)
for (int z = -64; z < 64; z++)
for (int z = -64; z < 64; z++) {
instance.setBlock(x, 4, z, new Block(1));
}
}
@Override