use CB BlockState implementations for captured blocks

When modifying the world, CB will store a copy of the affected
blocks in order to restore their state in the case that the event
is cancelled. This change only modifies the collection of blocks
in the world by normal means, e.g. not during tree population,
as the potentially marginal overheads would serve no advantage.

CB was using a CraftBlockState for all blocks, which causes issues
should any block that uses information beyond a data ID would suffer
from missing information, e.g. Skulls.

By using CBs CraftBlock#getState(), we will maintain a proper copy of
the blockstate that will be valid for restoration, as opposed to dropping
information on restoration when the event is cancelled.
This commit is contained in:
Shane Freeder
2017-11-16 12:12:41 +00:00
parent f1fd83ac7a
commit 1d1c5a4493

View File

@@ -84,7 +84,7 @@
+ public boolean preventPoiUpdated = false; // CraftBukkit - SPIGOT-5710 + public boolean preventPoiUpdated = false; // CraftBukkit - SPIGOT-5710
+ public boolean captureBlockStates = false; + public boolean captureBlockStates = false;
+ public boolean captureTreeGeneration = false; + public boolean captureTreeGeneration = false;
+ public Map<BlockPos, CapturedBlockState> capturedBlockStates = new java.util.LinkedHashMap<>(); + public Map<BlockPos, org.bukkit.craftbukkit.block.CraftBlockState> capturedBlockStates = new java.util.LinkedHashMap<>(); // Paper
+ public Map<BlockPos, BlockEntity> capturedTileEntities = new HashMap<>(); + public Map<BlockPos, BlockEntity> capturedTileEntities = new HashMap<>();
+ public List<ItemEntity> captureDrops; + public List<ItemEntity> captureDrops;
+ public final it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap<SpawnCategory> ticksPerSpawnCategory = new it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap<>(); + public final it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap<SpawnCategory> ticksPerSpawnCategory = new it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap<>();
@@ -237,7 +237,7 @@
+ // Paper start - if loaded + // Paper start - if loaded
@Nullable @Nullable
@Override + @Override
+ public final ChunkAccess getChunkIfLoadedImmediately(int x, int z) { + public final ChunkAccess getChunkIfLoadedImmediately(int x, int z) {
+ return ((ServerLevel)this).chunkSource.getChunkAtIfLoadedImmediately(x, z); + return ((ServerLevel)this).chunkSource.getChunkAtIfLoadedImmediately(x, z);
+ } + }
@@ -290,7 +290,7 @@
+ return getWorldBorder().isWithinBounds(blockposition) ? getBlockStateIfLoaded(blockposition) : null; + return getWorldBorder().isWithinBounds(blockposition) ? getBlockStateIfLoaded(blockposition) : null;
+ } + }
+ +
+ @Override @Override
public ChunkAccess getChunk(int chunkX, int chunkZ, ChunkStatus leastStatus, boolean create) { public ChunkAccess getChunk(int chunkX, int chunkZ, ChunkStatus leastStatus, boolean create) {
+ // Paper end + // Paper end
ChunkAccess ichunkaccess = this.getChunkSource().getChunk(chunkX, chunkZ, leastStatus, create); ChunkAccess ichunkaccess = this.getChunkSource().getChunk(chunkX, chunkZ, leastStatus, create);
@@ -302,7 +302,7 @@
public boolean setBlock(BlockPos pos, BlockState state, int flags, int maxUpdateDepth) { public boolean setBlock(BlockPos pos, BlockState state, int flags, int maxUpdateDepth) {
+ // CraftBukkit start - tree generation + // CraftBukkit start - tree generation
+ if (this.captureTreeGeneration) { + if (this.captureTreeGeneration) {
+ CapturedBlockState blockstate = this.capturedBlockStates.get(pos); + CraftBlockState blockstate = this.capturedBlockStates.get(pos);
+ if (blockstate == null) { + if (blockstate == null) {
+ blockstate = CapturedBlockState.getTreeBlockState(this, pos, flags); + blockstate = CapturedBlockState.getTreeBlockState(this, pos, flags);
+ this.capturedBlockStates.put(pos.immutable(), blockstate); + this.capturedBlockStates.put(pos.immutable(), blockstate);
@@ -315,7 +315,7 @@
if (this.isOutsideBuildHeight(pos)) { if (this.isOutsideBuildHeight(pos)) {
return false; return false;
} else if (!this.isClientSide && this.isDebug()) { } else if (!this.isClientSide && this.isDebug()) {
@@ -214,44 +400,123 @@ @@ -214,44 +400,124 @@
} else { } else {
LevelChunk chunk = this.getChunkAt(pos); LevelChunk chunk = this.getChunkAt(pos);
Block block = state.getBlock(); Block block = state.getBlock();
@@ -324,7 +324,8 @@
+ // CraftBukkit start - capture blockstates + // CraftBukkit start - capture blockstates
+ boolean captured = false; + boolean captured = false;
+ if (this.captureBlockStates && !this.capturedBlockStates.containsKey(pos)) { + if (this.captureBlockStates && !this.capturedBlockStates.containsKey(pos)) {
+ CapturedBlockState blockstate = CapturedBlockState.getBlockState(this, pos, flags); + CraftBlockState blockstate = (CraftBlockState) world.getBlockAt(pos.getX(), pos.getY(), pos.getZ()).getState(); // Paper - use CB getState to get a suitable snapshot
+ blockstate.setFlag(flags); // Paper - set flag
+ this.capturedBlockStates.put(pos.immutable(), blockstate); + this.capturedBlockStates.put(pos.immutable(), blockstate);
+ captured = true; + captured = true;
+ } + }
@@ -454,13 +455,13 @@
public void onBlockStateChange(BlockPos pos, BlockState oldBlock, BlockState newBlock) {} public void onBlockStateChange(BlockPos pos, BlockState oldBlock, BlockState newBlock) {}
@@ -340,10 +605,18 @@ @@ -340,10 +606,18 @@
@Override @Override
public BlockState getBlockState(BlockPos pos) { public BlockState getBlockState(BlockPos pos) {
+ // CraftBukkit start - tree generation + // CraftBukkit start - tree generation
+ if (this.captureTreeGeneration) { + if (this.captureTreeGeneration) {
+ CapturedBlockState previous = this.capturedBlockStates.get(pos); + CraftBlockState previous = this.capturedBlockStates.get(pos); // Paper
+ if (previous != null) { + if (previous != null) {
+ return previous.getHandle(); + return previous.getHandle();
+ } + }
@@ -474,7 +475,7 @@
return chunk.getBlockState(pos); return chunk.getBlockState(pos);
} }
@@ -446,32 +719,44 @@ @@ -446,32 +720,44 @@
this.pendingBlockEntityTickers.clear(); this.pendingBlockEntityTickers.clear();
} }
@@ -528,7 +529,7 @@
} }
} }
@@ -510,13 +795,29 @@ @@ -510,13 +796,29 @@
@Nullable @Nullable
@Override @Override
public BlockEntity getBlockEntity(BlockPos pos) { public BlockEntity getBlockEntity(BlockPos pos) {
@@ -559,7 +560,7 @@
this.getChunkAt(blockposition).addAndRegisterBlockEntity(blockEntity); this.getChunkAt(blockposition).addAndRegisterBlockEntity(blockEntity);
} }
} }
@@ -643,7 +944,7 @@ @@ -643,7 +945,7 @@
for (int k = 0; k < j; ++k) { for (int k = 0; k < j; ++k) {
EnderDragonPart entitycomplexpart = aentitycomplexpart[k]; EnderDragonPart entitycomplexpart = aentitycomplexpart[k];
@@ -568,7 +569,7 @@
if (t0 != null && predicate.test(t0)) { if (t0 != null && predicate.test(t0)) {
result.add(t0); result.add(t0);
@@ -912,7 +1213,7 @@ @@ -912,7 +1214,7 @@
public static enum ExplosionInteraction implements StringRepresentable { public static enum ExplosionInteraction implements StringRepresentable {