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
ConstructorsConstructorDescriptionCustomBlockData(Block block, String namespace) Deprecated.CustomBlockData(Block block, Plugin plugin) Gets the PersistentDataContainer associated with the given block and plugin -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all CustomBlockData and disables the protection status (setProtected(boolean)voidCopies 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 primitivePersistentDataTypefor the givenNamespacedKeystatic PersistentDataType<?,?> getDataType(PersistentDataContainer pdc, NamespacedKey key) Gets the proper primitivePersistentDataTypefor the givenNamespacedKeyin the givenPersistentDataContainergetKeys()<T,Z> Z getOrDefault(NamespacedKey namespacedKey, PersistentDataType<T, Z> persistentDataType, Z z) booleanhas(NamespacedKey namespacedKey) <T,Z> boolean has(NamespacedKey namespacedKey, PersistentDataType<T, Z> persistentDataType) static booleanhasCustomBlockData(Block block, Plugin plugin) Get if the given Block has any CustomBockData associated with itbooleanisEmpty()booleanGets whether this CustomBlockData is protected.static booleanisProtected(Block block, Plugin plugin) Get if the given Block's CustomBlockData is protected.voidreadFromBytes(byte[] bytes) Deprecated.Paper-onlyvoidreadFromBytes(byte[] bytes, boolean clear) Deprecated.Paper-onlystatic voidregisterListener(Plugin plugin) Starts to listen and manage block-related events such asBlockBreakEvent.voidremove(NamespacedKey namespacedKey) byte[]Deprecated.Paper-only<T,Z> void set(NamespacedKey namespacedKey, PersistentDataType<T, Z> persistentDataType, Z z) voidsetProtected(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
-
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
Setcontaining all blocks in this chunk containing Custom Block Data created by the given plugin
-
getDataType
Gets the proper primitivePersistentDataTypefor the givenNamespacedKeyin the givenPersistentDataContainer- Returns:
- The primitive PersistentDataType for the given key, or null if the key doesn't exist
-
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:
-
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. -
set
public <T,Z> void set(@NotNull NamespacedKey namespacedKey, @NotNull PersistentDataType<T, Z> persistentDataType, @NotNull Z z) - Specified by:
setin interfacePersistentDataContainer
-
has
public <T,Z> boolean has(@NotNull NamespacedKey namespacedKey, @NotNull PersistentDataType<T, Z> persistentDataType) - Specified by:
hasin interfacePersistentDataContainer
-
has
- Specified by:
hasin interfacePersistentDataContainer
-
get
@Nullable public <T,Z> Z get(@NotNull NamespacedKey namespacedKey, @NotNull PersistentDataType<T, Z> persistentDataType) - Specified by:
getin interfacePersistentDataContainer
-
getOrDefault
@NotNull public <T,Z> Z getOrDefault(@NotNull NamespacedKey namespacedKey, @NotNull PersistentDataType<T, Z> persistentDataType, @NotNull Z z) - Specified by:
getOrDefaultin interfacePersistentDataContainer
-
getKeys
- Specified by:
getKeysin interfacePersistentDataContainer
-
remove
- Specified by:
removein interfacePersistentDataContainer
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfacePersistentDataContainer
-
getAdapterContext
- Specified by:
getAdapterContextin interfacePersistentDataContainer
-
serializeToBytes
Deprecated.Paper-only- Specified by:
serializeToBytesin interfacePersistentDataContainer- Throws:
IOException- See Also:
-
readFromBytes
Deprecated.Paper-only- Specified by:
readFromBytesin interfacePersistentDataContainer- Throws:
IOException- See Also:
-
readFromBytes
Deprecated.Paper-only- Specified by:
readFromBytesin interfacePersistentDataContainer- Throws:
IOException- See Also:
-
getDataType
Gets the proper primitivePersistentDataTypefor the givenNamespacedKey- Returns:
- The primitive PersistentDataType for the given key, or null if the key doesn't exist
-
CustomBlockData(Block, Plugin)instead.