Added Chunk#setReadOnly and Chunk#isReadOnly

This commit is contained in:
themode 2020-10-24 00:17:23 +02:00
parent 86d6092547
commit fb26ec0f0b
4 changed files with 70 additions and 37 deletions

View File

@ -63,6 +63,7 @@ public abstract class Chunk implements Viewable, DataContainer {
// Options // Options
private final boolean shouldGenerate; private final boolean shouldGenerate;
private boolean readOnly;
// Packet cache // Packet cache
private volatile boolean enableCachePacket; private volatile boolean enableCachePacket;
@ -277,6 +278,24 @@ public abstract class Chunk implements Viewable, DataContainer {
return shouldGenerate; return shouldGenerate;
} }
/**
* Gets if this chunk is read-only.
*
* @return true if the chunk is read-only
*/
public boolean isReadOnly() {
return readOnly;
}
/**
* Changes the read state of the chunk.
*
* @param readOnly true to make the chunk read-only, false otherwise
*/
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}
/** /**
* Gets if this chunk automatically cache the latest {@link ChunkDataPacket} version. * Gets if this chunk automatically cache the latest {@link ChunkDataPacket} version.
* <p> * <p>

View File

@ -160,6 +160,10 @@ public class InstanceContainer extends Instance {
* @param data the {@link Data}, null if none * @param data the {@link Data}, null if none
*/ */
private void UNSAFE_setBlock(Chunk chunk, int x, int y, int z, short blockStateId, CustomBlock customBlock, Data data) { private void UNSAFE_setBlock(Chunk chunk, int x, int y, int z, short blockStateId, CustomBlock customBlock, Data data) {
if (chunk.isReadOnly()) {
return;
}
synchronized (chunk) { synchronized (chunk) {
final boolean isCustomBlock = customBlock != null; final boolean isCustomBlock = customBlock != null;
@ -342,6 +346,11 @@ public class InstanceContainer extends Instance {
final Chunk chunk = getChunkAt(blockPosition); final Chunk chunk = getChunkAt(blockPosition);
// Cancel if the chunk is read-only
if (chunk.isReadOnly()) {
return false;
}
// Chunk unloaded, stop here // Chunk unloaded, stop here
if (!ChunkUtils.isLoaded(chunk)) if (!ChunkUtils.isLoaded(chunk))
return false; return false;

View File

@ -27,6 +27,7 @@ public class StaticChunk extends Chunk {
public StaticChunk(Instance instance, Biome[] biomes, int chunkX, int chunkZ, BlockProvider blockProvider) { public StaticChunk(Instance instance, Biome[] biomes, int chunkX, int chunkZ, BlockProvider blockProvider) {
super(instance, biomes, chunkX, chunkZ, false); super(instance, biomes, chunkX, chunkZ, false);
this.blockProvider = blockProvider; this.blockProvider = blockProvider;
setReadOnly(true);
} }
@Override @Override

View File

@ -80,6 +80,7 @@ public class BlockPlacementListener {
boolean refreshChunk = false; boolean refreshChunk = false;
if (material.isBlock()) { if (material.isBlock()) {
if (!chunk.isReadOnly()) {
final Block block = material.getBlock(); final Block block = material.getBlock();
final Set<Entity> entities = instance.getChunkEntities(chunk); final Set<Entity> entities = instance.getChunkEntities(chunk);
// Check if the player is trying to place a block in an entity // Check if the player is trying to place a block in an entity
@ -127,6 +128,9 @@ public class BlockPlacementListener {
} else { } else {
refreshChunk = true; refreshChunk = true;
} }
} else {
refreshChunk = true;
}
} else { } else {
// Player didn't try to place a block but interacted with one // Player didn't try to place a block but interacted with one
PlayerUseItemOnBlockEvent event = new PlayerUseItemOnBlockEvent(player, hand, usedItem, blockPosition, direction); PlayerUseItemOnBlockEvent event = new PlayerUseItemOnBlockEvent(player, hand, usedItem, blockPosition, direction);