API to get a BlockState without a snapshot
This allows you to get a BlockState without creating a snapshot, operating on the real tile entity. This is useful for where performance is needed also Avoid NPE during CraftBlockEntityState load if could not get TE If Tile Entity was null, correct Sign to return empty lines instead of null
This commit is contained in:
@@ -328,6 +328,13 @@ public class CraftBlock implements Block {
|
||||
return CraftBlockStates.getBlockState(this);
|
||||
}
|
||||
|
||||
// Paper start
|
||||
@Override
|
||||
public BlockState getState(boolean useSnapshot) {
|
||||
return CraftBlockStates.getBlockState(this, useSnapshot);
|
||||
}
|
||||
// Paper end
|
||||
|
||||
@Override
|
||||
public Biome getBiome() {
|
||||
return this.getWorld().getBiome(this.getX(), this.getY(), this.getZ());
|
||||
|
||||
@@ -24,15 +24,26 @@ public class CraftBlockEntityState<T extends BlockEntity> extends CraftBlockStat
|
||||
|
||||
private final T tileEntity;
|
||||
private final T snapshot;
|
||||
public boolean snapshotDisabled; // Paper
|
||||
public static boolean DISABLE_SNAPSHOT = false; // Paper
|
||||
|
||||
public CraftBlockEntityState(World world, T tileEntity) {
|
||||
super(world, tileEntity.getBlockPos(), tileEntity.getBlockState());
|
||||
|
||||
this.tileEntity = tileEntity;
|
||||
|
||||
// Paper start
|
||||
this.snapshotDisabled = DISABLE_SNAPSHOT;
|
||||
if (DISABLE_SNAPSHOT) {
|
||||
this.snapshot = this.tileEntity;
|
||||
} else {
|
||||
this.snapshot = this.createSnapshot(tileEntity);
|
||||
}
|
||||
// copy tile entity data:
|
||||
this.snapshot = this.createSnapshot(tileEntity);
|
||||
this.load(this.snapshot);
|
||||
if (this.snapshot != null) {
|
||||
this.load(this.snapshot);
|
||||
}
|
||||
// Paper end
|
||||
}
|
||||
|
||||
protected CraftBlockEntityState(CraftBlockEntityState<T> state, Location location) {
|
||||
@@ -184,4 +195,11 @@ public class CraftBlockEntityState<T extends BlockEntity> extends CraftBlockStat
|
||||
public CraftBlockEntityState<T> copy(Location location) {
|
||||
return new CraftBlockEntityState<>(this, location);
|
||||
}
|
||||
|
||||
// Paper start
|
||||
@Override
|
||||
public boolean isSnapshot() {
|
||||
return !this.snapshotDisabled;
|
||||
}
|
||||
// Paper end
|
||||
}
|
||||
|
||||
@@ -393,15 +393,30 @@ public final class CraftBlockStates {
|
||||
}
|
||||
|
||||
public static BlockState getBlockState(Block block) {
|
||||
// Paper start
|
||||
return CraftBlockStates.getBlockState(block, true);
|
||||
}
|
||||
public static BlockState getBlockState(Block block, boolean useSnapshot) {
|
||||
// Paper end
|
||||
Preconditions.checkNotNull(block, "block is null");
|
||||
CraftBlock craftBlock = (CraftBlock) block;
|
||||
CraftWorld world = (CraftWorld) block.getWorld();
|
||||
BlockPos blockPosition = craftBlock.getPosition();
|
||||
net.minecraft.world.level.block.state.BlockState blockData = craftBlock.getNMS();
|
||||
BlockEntity tileEntity = craftBlock.getHandle().getBlockEntity(blockPosition);
|
||||
// Paper start - block state snapshots
|
||||
boolean prev = CraftBlockEntityState.DISABLE_SNAPSHOT;
|
||||
CraftBlockEntityState.DISABLE_SNAPSHOT = !useSnapshot;
|
||||
try {
|
||||
// Paper end
|
||||
CraftBlockState blockState = CraftBlockStates.getBlockState(world, blockPosition, blockData, tileEntity);
|
||||
blockState.setWorldHandle(craftBlock.getHandle()); // Inject the block's generator access
|
||||
return blockState;
|
||||
// Paper start
|
||||
} finally {
|
||||
CraftBlockEntityState.DISABLE_SNAPSHOT = prev;
|
||||
}
|
||||
// Paper end
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
|
||||
@@ -177,4 +177,10 @@ public class CraftPersistentDataContainer implements PersistentDataContainer {
|
||||
public String serialize() {
|
||||
return CraftNBTTagConfigSerializer.serialize(this.toTagCompound());
|
||||
}
|
||||
|
||||
// Paper start
|
||||
public void clear() {
|
||||
this.customDataTags.clear();
|
||||
}
|
||||
// Paper end
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user