Minestom/src/main/java/fr/themode/minestom/instance/ChunkBatch.java

83 lines
2.1 KiB
Java
Raw Normal View History

2019-08-19 17:04:19 +02:00
package fr.themode.minestom.instance;
2019-08-23 15:37:38 +02:00
import fr.themode.minestom.Main;
2019-08-19 17:04:19 +02:00
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
2019-08-19 17:04:19 +02:00
2019-08-27 05:23:25 +02:00
/**
* Use chunk coordinate (0-16) instead of world's
*/
2019-08-19 17:04:19 +02:00
public class ChunkBatch implements BlockModifier {
2019-08-23 15:37:38 +02:00
private static volatile ExecutorService batchesPool = Executors.newFixedThreadPool(Main.THREAD_COUNT_CHUNK_BATCH);
2019-08-19 17:04:19 +02:00
2019-08-24 20:34:01 +02:00
private InstanceContainer instance;
2019-08-19 17:04:19 +02:00
private Chunk chunk;
private List<BlockData> dataList = new ArrayList<>();
2019-08-24 20:34:01 +02:00
public ChunkBatch(InstanceContainer instance, Chunk chunk) {
2019-08-19 17:04:19 +02:00
this.instance = instance;
this.chunk = chunk;
}
@Override
public void setBlock(int x, int y, int z, short blockId) {
BlockData data = new BlockData();
data.x = (byte) x;
data.y = (byte) y;
data.z = (byte) z;
data.blockId = blockId;
this.dataList.add(data);
}
@Override
public void setBlock(int x, int y, int z, String blockId) {
BlockData data = new BlockData();
data.x = (byte) x;
data.y = (byte) y;
data.z = (byte) z;
data.blockIdentifier = blockId;
this.dataList.add(data);
}
public void flush(Consumer<Chunk> callback) {
2019-08-19 17:04:19 +02:00
synchronized (chunk) {
2019-08-24 20:34:01 +02:00
batchesPool.execute(() -> {
2019-08-19 17:04:19 +02:00
for (BlockData data : dataList) {
data.apply(chunk);
}
2019-08-27 05:23:25 +02:00
2019-08-27 20:49:11 +02:00
// dataList.clear();
2019-08-27 05:23:25 +02:00
chunk.refreshDataPacket();
instance.sendChunkUpdate(chunk);
if (callback != null)
callback.accept(chunk);
2019-08-19 17:04:19 +02:00
});
}
}
private class BlockData {
private byte x, y, z;
private short blockId;
private String blockIdentifier;
public void apply(Chunk chunk) {
if (blockIdentifier == null) {
chunk.setBlock(x, y, z, blockId);
} else {
2019-08-25 20:03:43 +02:00
chunk.setCustomBlock(x, y, z, blockIdentifier);
2019-08-19 17:04:19 +02:00
}
}
}
}