Class CustomBlockData
- All Implemented Interfaces:
PersistentDataContainer
PersistentDataContainer
for a specific Block
. Also provides some static utility methods
that can be used on every PersistentDataContainer.
By default, and for backward compatibility reasons, data stored inside blocks is independent of the underlying block.
That means: if you store some data inside a dirt block, and that block is now pushed by a piston, then the information
will still reside in the old block's location. You can of course also make CustomBockData automatically take care of those situations,
so that CustomBlockData will always be updated on certain Bukkit Events like BlockBreakEvent, EntityExplodeEvent, etc.
For more information about this please see registerListener(Plugin)
.-
Constructor Summary
ConstructorDescriptionCustomBlockData
(Block block, String namespace) Deprecated.CustomBlockData
(Block block, Plugin plugin) Gets the PersistentDataContainer associated with the given block and plugin -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all CustomBlockData and disables the protection status (setProtected(boolean)
void
Copies all data to another block.<T,
Z> Z get
(NamespacedKey namespacedKey, PersistentDataType<T, Z> persistentDataType) getBlock()
Gets the Block associated with this CustomBlockData, or null if the world is no longer loaded.getBlocksWithCustomData
(String namespace, Chunk chunk) getBlocksWithCustomData
(Plugin plugin, Chunk chunk) Returns a Set of all blocks in this chunk containing Custom Block Data created by the given pluginPersistentDataType<?,
?> getDataType
(NamespacedKey key) Gets the proper primitivePersistentDataType
for the givenNamespacedKey
static PersistentDataType<?,
?> getDataType
(PersistentDataContainer pdc, NamespacedKey key) Gets the proper primitivePersistentDataType
for the givenNamespacedKey
in the givenPersistentDataContainer
getKeys()
<T,
Z> Z getOrDefault
(NamespacedKey namespacedKey, PersistentDataType<T, Z> persistentDataType, Z z) boolean
has
(NamespacedKey namespacedKey) <T,
Z> boolean has
(NamespacedKey namespacedKey, PersistentDataType<T, Z> persistentDataType) static boolean
hasCustomBlockData
(Block block, Plugin plugin) Get if the given Block has any CustomBockData associated with itboolean
isEmpty()
boolean
Gets whether this CustomBlockData is protected.static boolean
isProtected
(Block block, Plugin plugin) Get if the given Block's CustomBlockData is protected.void
readFromBytes
(byte[] bytes) Deprecated.Paper-onlyvoid
readFromBytes
(byte[] bytes, boolean clear) Deprecated.Paper-onlystatic void
registerListener
(Plugin plugin) Starts to listen and manage block-related events such asBlockBreakEvent
.void
remove
(NamespacedKey namespacedKey) byte[]
Deprecated.Paper-only<T,
Z> void set
(NamespacedKey namespacedKey, PersistentDataType<T, Z> persistentDataType, Z z) void
setProtected
(boolean isProtected) Sets whether this CustomBlockData is protected.
-
Constructor Details
-
CustomBlockData
Gets the PersistentDataContainer associated with the given block and plugin- Parameters:
block
- Blockplugin
- Plugin
-
CustomBlockData
Deprecated.UseCustomBlockData(Block, Plugin)
instead.Gets the PersistentDataContainer associated with the given block and plugin- Parameters:
block
- Blocknamespace
- Namespace
-
-
Method Details
-
getBlock
Gets the Block associated with this CustomBlockData, or null if the world is no longer loaded. -
isProtected
public boolean isProtected()Gets whether this CustomBlockData is protected. Protected CustomBlockData will not be changed by any Bukkit Events- See Also:
-
setProtected
public void setProtected(boolean isProtected) Sets whether this CustomBlockData is protected. Protected CustomBlockData will not be changed by any Bukkit Events- See Also:
-
hasCustomBlockData
Get if the given Block has any CustomBockData associated with it -
isProtected
Get if the given Block's CustomBlockData is protected. Protected CustomBlockData will not be changed by any Bukkit Events- Returns:
- true if the Block's CustomBlockData is protected, false if it doesn't have any CustomBlockData or it's not protected
- See Also:
-
registerListener
Starts to listen and manage block-related events such asBlockBreakEvent
. By default, CustomBlockData is "stateless". That means: when you add data to a block, and now a player breaks the block, the data will still reside at the original block location. This is to ensure that you always have full control about what data is saved at which location.If you do not want to handle this yourself, you can instead let CustomBlockData handle those events by calling this method once. It will then listen to the common events itself, and automatically remove/update CustomBlockData.
Block changes made using the Bukkit API (e.g.
Block.setType(Material)
) or using a plugin like WorldEdit will not be registered by this (but pull requests are welcome, of course)For example, when you call this method in onEnable, CustomBlockData will now get automatically removed from a block when a player breaks this block. It will additionally call custom events like
CustomBlockDataRemoveEvent
. Those events implementCancellable
. If one of the CustomBlockData events is cancelled, it will not alter any CustomBlockData.- Parameters:
plugin
- Your plugin's instance
-
getBlocksWithCustomData
Returns a Set of all blocks in this chunk containing Custom Block Data created by the given plugin- Parameters:
plugin
- Pluginchunk
- Chunk- Returns:
- A Set containing all blocks in this chunk containing Custom Block Data created by the given plugin
-
getBlocksWithCustomData
- Parameters:
namespace
- Namespacechunk
- Chunk- Returns:
- A
Set
containing all blocks in this chunk containing Custom Block Data created by the given plugin
-
clear
public void clear()Removes all CustomBlockData and disables the protection status (setProtected(boolean)
-
copyTo
Copies all data to another block. Data already present in the destination block will keep intact, unless it gets overwritten by identically named keys. Data in the source block won't be changed. -
getDataType
Gets the proper primitivePersistentDataType
for the givenNamespacedKey
in the givenPersistentDataContainer
- Returns:
- The primitive PersistentDataType for the given key, or null if the key doesn't exist
-
set
public <T,Z> void set(@NotNull NamespacedKey namespacedKey, @NotNull PersistentDataType<T, Z> persistentDataType, @NotNull Z z) - Specified by:
set
in interfacePersistentDataContainer
-
has
public <T,Z> boolean has(@NotNull NamespacedKey namespacedKey, @NotNull PersistentDataType<T, Z> persistentDataType) - Specified by:
has
in interfacePersistentDataContainer
-
has
- Specified by:
has
in interfacePersistentDataContainer
-
get
@Nullable public <T,Z> Z get(@NotNull NamespacedKey namespacedKey, @NotNull PersistentDataType<T, Z> persistentDataType) - Specified by:
get
in interfacePersistentDataContainer
-
getOrDefault
@NotNull public <T,Z> Z getOrDefault(@NotNull NamespacedKey namespacedKey, @NotNull PersistentDataType<T, Z> persistentDataType, @NotNull Z z) - Specified by:
getOrDefault
in interfacePersistentDataContainer
-
getKeys
- Specified by:
getKeys
in interfacePersistentDataContainer
-
remove
- Specified by:
remove
in interfacePersistentDataContainer
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfacePersistentDataContainer
-
getAdapterContext
- Specified by:
getAdapterContext
in interfacePersistentDataContainer
-
serializeToBytes
Deprecated.Paper-only- Specified by:
serializeToBytes
in interfacePersistentDataContainer
- Throws:
IOException
- See Also:
-
readFromBytes
Deprecated.Paper-only- Specified by:
readFromBytes
in interfacePersistentDataContainer
- Throws:
IOException
- See Also:
-
readFromBytes
Deprecated.Paper-only- Specified by:
readFromBytes
in interfacePersistentDataContainer
- Throws:
IOException
- See Also:
-
getDataType
Gets the proper primitivePersistentDataType
for the givenNamespacedKey
- Returns:
- The primitive PersistentDataType for the given key, or null if the key doesn't exist
-
CustomBlockData(Block, Plugin)
instead.