Change the algorithm order of InstanceContainer#setBlock

This commit is contained in:
themode 2021-01-28 16:19:06 +01:00
parent da2286de74
commit b321a10caa
2 changed files with 22 additions and 17 deletions

View File

@ -152,9 +152,9 @@ public class InstanceContainer extends Instance {
} }
/** /**
* Set a block at the position * Sets a block at the specified position.
* <p> * <p>
* Unsafe because the method is not synchronized and it does not verify if the chunk is loaded or not * Unsafe because the method is not synchronized and it does not verify if the chunk is loaded or not.
* *
* @param chunk the {@link Chunk} which should be loaded * @param chunk the {@link Chunk} which should be loaded
* @param x the block X * @param x the block X
@ -187,15 +187,12 @@ public class InstanceContainer extends Instance {
return; return;
} }
setAlreadyChanged(blockPosition, blockStateId); setAlreadyChanged(blockPosition, blockStateId);
final int index = ChunkUtils.getBlockIndex(x, y, z); final int index = ChunkUtils.getBlockIndex(x, y, z);
final CustomBlock previousBlock = chunk.getCustomBlock(index); final CustomBlock previousBlock = chunk.getCustomBlock(index);
final Data previousBlockData = previousBlock != null ? chunk.getBlockData(index) : null;
if (previousBlock != null) { if (previousBlock != null) {
// Previous block was a custom block
// Call the destroy listener
callBlockDestroy(chunk, index, previousBlock, blockPosition);
// Remove digging information for the previous custom block // Remove digging information for the previous custom block
previousBlock.removeDiggingInformation(this, blockPosition); previousBlock.removeDiggingInformation(this, blockPosition);
} }
@ -221,9 +218,15 @@ public class InstanceContainer extends Instance {
// Refresh player chunk block // Refresh player chunk block
sendBlockChange(chunk, blockPosition, blockStateId); sendBlockChange(chunk, blockPosition, blockStateId);
// Call the place listener for custom block // Call the destroy listener for the previously destroyed block
if (isCustomBlock) if (previousBlock != null) {
callBlockDestroy(previousBlock, previousBlockData, blockPosition);
}
// Call the place listener for newly placed custom block
if (isCustomBlock) {
callBlockPlace(chunk, index, blockPosition); callBlockPlace(chunk, index, blockPosition);
}
} }
} }
@ -263,14 +266,13 @@ public class InstanceContainer extends Instance {
* <p> * <p>
* WARNING {@code chunk} needs to be synchronized. * WARNING {@code chunk} needs to be synchronized.
* *
* @param chunk the chunk where the block is * @param previousBlock the block which has been destroyed
* @param index the index of the block * @param previousBlockData the data of the destroyed block
* @param previousBlock the block which has been destroyed * @param blockPosition the block position
* @param blockPosition the block position
*/ */
private void callBlockDestroy(@NotNull Chunk chunk, int index, @NotNull CustomBlock previousBlock, @NotNull BlockPosition blockPosition) { private void callBlockDestroy(@NotNull CustomBlock previousBlock, @Nullable Data previousBlockData,
final Data previousData = chunk.getBlockData(index); @NotNull BlockPosition blockPosition) {
previousBlock.onDestroy(this, blockPosition, previousData); previousBlock.onDestroy(this, blockPosition, previousBlockData);
} }
/** /**

View File

@ -34,12 +34,15 @@ public class CustomBlockSample extends CustomBlock {
instance.setBlock(above, Block.AIR); instance.setBlock(above, Block.AIR);
instance.setBlock(blockPosition, Block.AIR); // this should NOT create a stack overflow simply because we are trying to remove this same block instance.setBlock(blockPosition, Block.AIR); // this should NOT create a stack overflow simply because we are trying to remove this same block
} }
System.out.println("position "+blockPosition);
instance.setBlock(blockPosition, Block.DIAMOND_BLOCK);
} }
@Override @Override
public void update(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @Nullable Data data) { public void update(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @Nullable Data data) {
final short blockId = instance.getBlockStateId(blockPosition); final short blockId = instance.getBlockStateId(blockPosition);
instance.refreshBlockStateId(blockPosition, (short) (blockId+1)); //instance.refreshBlockStateId(blockPosition, (short) (blockId+1));
} }
@Nullable @Nullable