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,6 +17,8 @@ public class EntityManager {
public void update() {
for (Instance instance : instanceManager.getInstances()) {
synchronized (instance) {
// Creatures
for (EntityCreature creature : instance.getCreatures()) {
creaturesPool.submit(() -> {
@ -42,4 +44,6 @@ public class EntityManager {
}
}
}

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,9 +31,10 @@ 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
public void process(PlayerConnection connection, ConnectionManager connectionManager) {