2021-06-11 14:02:28 +02:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Aikar <aikar@aikar.co>
|
|
|
|
Date: Mon, 6 Nov 2017 21:08:22 -0500
|
|
|
|
Subject: [PATCH] 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
|
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java
|
2024-08-10 12:24:38 +02:00
|
|
|
index d6ad310d3b472c40c128cfb459171d9f48e50915..48bee70ba4188a4a55beb6584224b0f23784dd88 100644
|
2021-06-11 14:02:28 +02:00
|
|
|
--- a/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java
|
|
|
|
+++ b/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java
|
2024-08-10 12:24:38 +02:00
|
|
|
@@ -59,6 +59,7 @@ public abstract class BlockEntity {
|
2021-06-12 17:06:20 +02:00
|
|
|
this.worldPosition = pos.immutable();
|
2024-08-10 12:24:38 +02:00
|
|
|
this.validateBlockState(state);
|
2021-06-12 17:06:20 +02:00
|
|
|
this.blockState = state;
|
2021-10-02 19:21:49 +02:00
|
|
|
+ this.persistentDataContainer = new CraftPersistentDataContainer(DATA_TYPE_REGISTRY); // Paper - always init
|
2021-06-11 14:02:28 +02:00
|
|
|
}
|
|
|
|
|
2024-08-10 12:24:38 +02:00
|
|
|
private void validateBlockState(BlockState state) {
|
|
|
|
@@ -92,7 +93,7 @@ public abstract class BlockEntity {
|
2021-06-11 14:02:28 +02:00
|
|
|
|
2021-06-12 17:06:20 +02:00
|
|
|
// CraftBukkit start - read container
|
2024-04-24 03:25:14 +02:00
|
|
|
protected void loadAdditional(CompoundTag nbt, HolderLookup.Provider registryLookup) {
|
2021-06-12 17:06:20 +02:00
|
|
|
- this.persistentDataContainer = new CraftPersistentDataContainer(BlockEntity.DATA_TYPE_REGISTRY);
|
2021-10-02 19:21:49 +02:00
|
|
|
+ this.persistentDataContainer.clear(); // Paper - clear instead of init
|
2021-06-12 17:06:20 +02:00
|
|
|
|
|
|
|
net.minecraft.nbt.Tag persistentDataTag = nbt.get("PublicBukkitValues");
|
2021-06-11 14:02:28 +02:00
|
|
|
if (persistentDataTag instanceof CompoundTag) {
|
2024-08-10 12:24:38 +02:00
|
|
|
@@ -379,8 +380,15 @@ public abstract class BlockEntity {
|
2021-06-11 14:02:28 +02:00
|
|
|
|
|
|
|
// CraftBukkit start - add method
|
|
|
|
public InventoryHolder getOwner() {
|
2021-10-02 19:21:49 +02:00
|
|
|
+ // Paper start
|
2021-06-11 14:02:28 +02:00
|
|
|
+ return getOwner(true);
|
|
|
|
+ }
|
|
|
|
+ public InventoryHolder getOwner(boolean useSnapshot) {
|
|
|
|
+ // Paper end
|
2021-10-02 19:21:49 +02:00
|
|
|
if (this.level == null) return null;
|
2021-11-29 13:15:54 +01:00
|
|
|
- org.bukkit.block.BlockState state = this.level.getWorld().getBlockAt(this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ()).getState();
|
|
|
|
+ org.bukkit.block.Block block = this.level.getWorld().getBlockAt(this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ());
|
|
|
|
+ if (block.getType() == org.bukkit.Material.AIR) return null;
|
2021-06-11 14:02:28 +02:00
|
|
|
+ org.bukkit.block.BlockState state = block.getState(useSnapshot); // Paper
|
|
|
|
if (state instanceof InventoryHolder) return (InventoryHolder) state;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
|
2024-01-14 10:46:04 +01:00
|
|
|
index a1c1a101aa424e74309f6f4c0a53a6a8db5df441..013298c424025cd88f15d61e50d196f70fa4c58b 100644
|
2021-06-11 14:02:28 +02:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
|
2023-12-05 23:12:48 +01:00
|
|
|
@@ -328,6 +328,13 @@ public class CraftBlock implements Block {
|
2021-10-02 19:21:49 +02:00
|
|
|
return CraftBlockStates.getBlockState(this);
|
|
|
|
}
|
2021-06-11 14:02:28 +02:00
|
|
|
|
2022-01-13 20:31:48 +01:00
|
|
|
+ // Paper start
|
|
|
|
+ @Override
|
|
|
|
+ public BlockState getState(boolean useSnapshot) {
|
|
|
|
+ return CraftBlockStates.getBlockState(this, useSnapshot);
|
|
|
|
+ }
|
|
|
|
+ // Paper end
|
|
|
|
+
|
2021-10-02 19:21:49 +02:00
|
|
|
@Override
|
2022-01-13 20:31:48 +01:00
|
|
|
public Biome getBiome() {
|
|
|
|
return this.getWorld().getBiome(this.getX(), this.getY(), this.getZ());
|
2021-06-11 14:02:28 +02:00
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockEntityState.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockEntityState.java
|
2024-05-05 18:08:55 +02:00
|
|
|
index f8b5595dc92036cc1889c0835f69389c93501fa7..a7bb7d263cb981fcf6dd35a3b32270e89e96945a 100644
|
2021-06-11 14:02:28 +02:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockEntityState.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockEntityState.java
|
2024-05-05 18:08:55 +02:00
|
|
|
@@ -24,15 +24,26 @@ public class CraftBlockEntityState<T extends BlockEntity> extends CraftBlockStat
|
2021-06-11 14:02:28 +02:00
|
|
|
|
2021-10-02 19:21:49 +02:00
|
|
|
private final T tileEntity;
|
|
|
|
private final T snapshot;
|
2023-09-29 02:28:26 +02:00
|
|
|
+ public boolean snapshotDisabled; // Paper
|
2021-06-11 14:02:28 +02:00
|
|
|
+ public static boolean DISABLE_SNAPSHOT = false; // Paper
|
|
|
|
|
2021-10-02 19:21:49 +02:00
|
|
|
public CraftBlockEntityState(World world, T tileEntity) {
|
|
|
|
super(world, tileEntity.getBlockPos(), tileEntity.getBlockState());
|
|
|
|
|
2021-06-11 14:02:28 +02:00
|
|
|
this.tileEntity = tileEntity;
|
2021-10-02 19:21:49 +02:00
|
|
|
|
2021-06-11 14:02:28 +02:00
|
|
|
+ // Paper start
|
|
|
|
+ this.snapshotDisabled = DISABLE_SNAPSHOT;
|
|
|
|
+ if (DISABLE_SNAPSHOT) {
|
|
|
|
+ this.snapshot = this.tileEntity;
|
|
|
|
+ } else {
|
2021-10-02 19:21:49 +02:00
|
|
|
+ this.snapshot = this.createSnapshot(tileEntity);
|
2021-06-11 14:02:28 +02:00
|
|
|
+ }
|
|
|
|
// copy tile entity data:
|
|
|
|
- this.snapshot = this.createSnapshot(tileEntity);
|
2023-10-27 01:34:58 +02:00
|
|
|
- this.load(this.snapshot);
|
2021-10-02 19:21:49 +02:00
|
|
|
+ if (this.snapshot != null) {
|
2021-06-11 14:02:28 +02:00
|
|
|
+ this.load(this.snapshot);
|
|
|
|
+ }
|
|
|
|
+ // Paper end
|
|
|
|
}
|
|
|
|
|
2024-04-24 03:25:14 +02:00
|
|
|
protected CraftBlockEntityState(CraftBlockEntityState<T> state, Location location) {
|
2024-05-05 18:08:55 +02:00
|
|
|
@@ -169,4 +180,11 @@ public class CraftBlockEntityState<T extends BlockEntity> extends CraftBlockStat
|
2024-04-24 03:25:14 +02:00
|
|
|
public CraftBlockEntityState<T> copy(Location location) {
|
|
|
|
return new CraftBlockEntityState<>(this, location);
|
2022-03-17 02:28:06 +01:00
|
|
|
}
|
|
|
|
+
|
|
|
|
+ // Paper start
|
|
|
|
+ @Override
|
|
|
|
+ public boolean isSnapshot() {
|
|
|
|
+ return !this.snapshotDisabled;
|
|
|
|
+ }
|
|
|
|
+ // Paper end
|
|
|
|
}
|
2021-11-29 13:15:54 +01:00
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockStates.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockStates.java
|
2024-06-13 17:45:43 +02:00
|
|
|
index 0173a8eac2153d889536e48970f967040490e4b7..83ac5fc6cbbd249b5865ab203b150f53f01c9f05 100644
|
2021-11-29 13:15:54 +01:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockStates.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockStates.java
|
2024-04-24 03:25:14 +02:00
|
|
|
@@ -387,15 +387,30 @@ public final class CraftBlockStates {
|
2022-01-13 20:31:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-04-24 03:25:14 +02:00
|
|
|
@Deprecated
|
2021-06-11 14:02:28 +02:00
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/persistence/CraftPersistentDataContainer.java b/src/main/java/org/bukkit/craftbukkit/persistence/CraftPersistentDataContainer.java
|
2024-01-14 10:46:04 +01:00
|
|
|
index dbb9a5b2f8d3bc0b13e0f56f6e8a0d9a7b761327..3001bb0e3d4af9b16645a0136093db594b89ab01 100644
|
2021-06-11 14:02:28 +02:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/persistence/CraftPersistentDataContainer.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/persistence/CraftPersistentDataContainer.java
|
2024-01-14 10:46:04 +01:00
|
|
|
@@ -177,4 +177,10 @@ public class CraftPersistentDataContainer implements PersistentDataContainer {
|
2023-09-24 09:16:58 +02:00
|
|
|
public String serialize() {
|
|
|
|
return CraftNBTTagConfigSerializer.serialize(this.toTagCompound());
|
2021-06-11 14:02:28 +02:00
|
|
|
}
|
2021-12-05 08:11:59 +01:00
|
|
|
+
|
2021-06-11 14:02:28 +02:00
|
|
|
+ // Paper start
|
|
|
|
+ public void clear() {
|
|
|
|
+ this.customDataTags.clear();
|
|
|
|
+ }
|
|
|
|
+ // Paper end
|
2021-12-05 08:11:59 +01:00
|
|
|
}
|