Class PacketBlock
java.lang.Object
net.bitbylogic.packetblocks.block.PacketBlock
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
PacketBlock
(@NonNull org.bukkit.Location location, @NonNull org.bukkit.block.data.BlockData blockData) Constructs a new PacketBlock instance associated with a specific location and block data. -
Method Summary
Modifier and TypeMethodDescriptionaddAndUpdateViewer
(@NonNull org.bukkit.entity.Player player) Adds the specified player as a viewer to this Packet Block if they are not already added and updates their block state to reflect the current state of the Packet Block.addAndUpdateViewer
(@NonNull org.bukkit.entity.Player player, @NonNull Supplier<org.bukkit.block.data.BlockData> blockDataSupplier) Adds a viewer for the block data and sends an update to the specified player.void
addMetadata
(@NonNull String key, @NonNull Object object) Adds metadata to the PacketBlock.void
addMetadata
(@NonNull org.bukkit.entity.Player player, @NonNull String key, @NonNull Object object) Adds a metadata key-value pair to the specific player'sPacketBlockPlayerData
, if the player is a viewer.void
addViewCondition
(@NonNull Predicate<org.bukkit.entity.Player> condition) Adds a viewing condition to the list of conditions if it is not already present.addViewer
(@NonNull org.bukkit.entity.Player player) Adds the specified player as a viewer to this Packet Block.addViewer
(@NonNull org.bukkit.entity.Player player, @NonNull Supplier<org.bukkit.block.data.BlockData> blockDataSupplier) Adds a viewer to this Packet Block with a custom BlockData supplier.attemptAddViewer
(@NonNull org.bukkit.entity.Player player, boolean sendUpdate) Attempts to add the specified player as a viewer to this Packet Block if they meet the viewing conditions.boolean
canView
(@NonNull org.bukkit.entity.Player player) Checks if the specified player meets all the conditions required to view this Packet Block.org.bukkit.block.BlockState
getBlockState
(@NonNull org.bukkit.entity.Player player) Retrieves the BlockState associated with a specific player.int
getBreakSpeed
(@NonNull org.bukkit.entity.Player player) Calculates and retrieves the break speed for the given player.getMetadata
(@NonNull String key) Retrieves the metadata value associated with the specified key.getMetadata
(@NonNull String key, @Nullable Object fallback) Retrieves the metadata value associated with the given key.getMetadata
(@NonNull org.bukkit.entity.Player player, @NonNull String key) Retrieves the metadata associated with a specific player and a given key.<T> T
getMetadataAs
(@NonNull String key, @NonNull Class<T> clazz) Retrieves metadata associated with the specified key and casts it to the desired type.getMetadataAs
(@NonNull org.bukkit.entity.Player player, @NonNull String key, @Nullable Object fallback) Retrieves metadata associated with a given key for a specified player.getViewer
(@NonNull org.bukkit.entity.Player player) Retrieves thePacketBlockPlayerData
associated with the given player if they are a viewer of this Packet Block.boolean
hasMetadata
(@NonNull String key) Checks if the metadata contains the specified key.boolean
hasMetadata
(@NonNull org.bukkit.entity.Player player, @NonNull String key) Checks if the specified player has metadata associated with the given key within this Packet Block.boolean
isViewer
(@NonNull org.bukkit.entity.Player player) Checks whether the specified player is a viewer of this Packet Block.void
removeMetadata
(@NonNull String key) Removes the metadata associated with the specified key.void
removeMetadata
(@NonNull org.bukkit.entity.Player player, @NonNull String key) Removes the metadata associated with a specific key for the given player, if the player is a viewer of this PacketBlock.void
removeViewer
(@NonNull org.bukkit.entity.Player player) Removes the specified player from the list of viewers if they are present.void
sendUpdate
(@NonNull org.bukkit.entity.Player player) Sends a block update to the specified player at the current location.void
Sends block updates to all the viewers currently tracking the block at the specified location.void
setBlockData
(@NonNull org.bukkit.entity.Player player, @Nullable org.bukkit.block.data.BlockData blockData) Sets the block data for a specific player.void
setBlockDataAndUpdate
(@NonNull org.bukkit.entity.Player player, @Nullable org.bukkit.block.data.BlockData blockData) Sets the block data for the specified player and sends an update.void
setBlockDataForAll
(@NonNull org.bukkit.block.data.BlockData blockData) Sets the block data for all viewers and updates them accordingly.void
setBlockDataSupplier
(@NonNull org.bukkit.entity.Player player, @NonNull org.bukkit.block.data.BlockData blockData) Sets the BlockData supplier for a specific player, allowing for dynamic control over block data.void
setBlockDataSupplierForAll
(@NonNull org.bukkit.block.data.BlockData blockData) Sets the block data supplier for all viewers and updates the block data for each viewer based on the provided BlockData.void
simulateBreak
(@NonNull org.bukkit.entity.Player player) Simulates the breaking action for the specified player.void
simulateBreak
(@NonNull org.bukkit.entity.Player player, @Nullable org.bukkit.inventory.ItemStack tool) Simulates the block-breaking process, triggering a custom event and handling item drops based on the event state.
-
Constructor Details
-
PacketBlock
protected PacketBlock(@NonNull @NonNull org.bukkit.Location location, @NonNull @NonNull org.bukkit.block.data.BlockData blockData) Constructs a new PacketBlock instance associated with a specific location and block data. The PacketBlock represents a virtual block that interacts with players based on certain conditions, metadata, and viewer states.- Parameters:
location
- The location of the block in the world. Must not be null.blockData
- The visual and physical characteristics of the block. Must not be null.
-
-
Method Details
-
canView
public boolean canView(@NonNull @NonNull org.bukkit.entity.Player player) Checks if the specified player meets all the conditions required to view this Packet Block. The conditions are evaluated using the stream of view requirements associated with this block.- Parameters:
player
- The player whose eligibility to view the block is being checked. Must not be null.- Returns:
- True if the player satisfies all the view conditions, otherwise false.
-
isViewer
public boolean isViewer(@NonNull @NonNull org.bukkit.entity.Player player) Checks whether the specified player is a viewer of this Packet Block.- Parameters:
player
- The player to check. Must not be null.- Returns:
- True if the player is a viewer of this Packet Block, false otherwise.
-
getViewer
Retrieves thePacketBlockPlayerData
associated with the given player if they are a viewer of this Packet Block.- Parameters:
player
- The player whose viewer data is to be retrieved. Must not be null.- Returns:
- An
Optional
containing thePacketBlockPlayerData
associated with the player if they are a viewer, or an empty optional if they are not a viewer.
-
attemptAddViewer
public Optional<PacketBlockPlayerData> attemptAddViewer(@NonNull @NonNull org.bukkit.entity.Player player, boolean sendUpdate) Attempts to add the specified player as a viewer to this Packet Block if they meet the viewing conditions. If the player is successfully added, an optional containing the associatedPacketBlockPlayerData
instance is returned. If the player is already a viewer, their existingPacketBlockPlayerData
is returned. No viewer is added if the player does not meet the viewing conditions.- Parameters:
player
- The player to attempt to add as a viewer.sendUpdate
- Whether to send a block update to the player upon successfully adding them as a viewer.- Returns:
- An
Optional
containing thePacketBlockPlayerData
associated with the player if they meet the conditions and are added as a viewer, or an empty optional otherwise.
-
addViewer
Adds the specified player as a viewer to this Packet Block. This method bypasses any view requirements that are set.- Parameters:
player
- The player to add as a viewer.- Returns:
- The
PacketBlockPlayerData
instance associated with the added player.
-
addAndUpdateViewer
Adds the specified player as a viewer to this Packet Block if they are not already added and updates their block state to reflect the current state of the Packet Block. This method bypasses any view requirements that are set.- Parameters:
player
- The player to add as a viewer and send block updates to- Returns:
- The
PacketBlockPlayerData
instance associated with the added player
-
addViewer
public PacketBlockPlayerData addViewer(@NonNull @NonNull org.bukkit.entity.Player player, @NonNull @NonNull Supplier<org.bukkit.block.data.BlockData> blockDataSupplier) Adds a viewer to this Packet Block with a custom BlockData supplier. This method bypasses any view requirements that are set.- Parameters:
player
- The player to add as a viewer.blockDataSupplier
- A supplier that provides the BlockData for this player.- Returns:
- The
PacketBlockPlayerData
instance associated with the added player.
-
addAndUpdateViewer
public PacketBlockPlayerData addAndUpdateViewer(@NonNull @NonNull org.bukkit.entity.Player player, @NonNull @NonNull Supplier<org.bukkit.block.data.BlockData> blockDataSupplier) Adds a viewer for the block data and sends an update to the specified player.- Parameters:
player
- the player to be added as a viewer, must not be nullblockDataSupplier
- a supplier for providing the block data, must not be null- Returns:
- the PacketBlockPlayerData object associated with the player
-
removeViewer
public void removeViewer(@NonNull @NonNull org.bukkit.entity.Player player) Removes the specified player from the list of viewers if they are present. If the player is successfully removed, it sends a block change notification to the player for a specific location.- Parameters:
player
- the player to be removed from the viewers list; must not be null
-
setBlockDataForAll
public void setBlockDataForAll(@NonNull @NonNull org.bukkit.block.data.BlockData blockData) Sets the block data for all viewers and updates them accordingly.- Parameters:
blockData
- the block data to be set for all viewers
-
setBlockDataSupplierForAll
public void setBlockDataSupplierForAll(@NonNull @NonNull org.bukkit.block.data.BlockData blockData) Sets the block data supplier for all viewers and updates the block data for each viewer based on the provided BlockData.- Parameters:
blockData
- the BlockData object to be supplied to all viewers
-
setBlockData
public void setBlockData(@NonNull @NonNull org.bukkit.entity.Player player, @Nullable @Nullable org.bukkit.block.data.BlockData blockData) Sets the block data for a specific player.- Parameters:
player
- the player for whom the block data is being set; must not be nullblockData
- the block data to associate with the player; can be null to reset or remove the block data
-
setBlockDataSupplier
public void setBlockDataSupplier(@NonNull @NonNull org.bukkit.entity.Player player, @NonNull @NonNull org.bukkit.block.data.BlockData blockData) Sets the BlockData supplier for a specific player, allowing for dynamic control over block data.- Parameters:
player
- the player for whom the BlockData supplier is being set, must not be nullblockData
- the BlockData object to be supplied, must not be null
-
setBlockDataAndUpdate
public void setBlockDataAndUpdate(@NonNull @NonNull org.bukkit.entity.Player player, @Nullable @Nullable org.bukkit.block.data.BlockData blockData) Sets the block data for the specified player and sends an update.- Parameters:
player
- the player for whom the block data is being set, must not be nullblockData
- the block data to be set, can be null
-
getBlockState
public org.bukkit.block.BlockState getBlockState(@NonNull @NonNull org.bukkit.entity.Player player) Retrieves the BlockState associated with a specific player. This method takes into account the player's data and determines the appropriate BlockState, using either their pre-existing block data or a supplier to generate new data. If no player-specific data is available, a default BlockState is created based on the block data and location.- Parameters:
player
- the player for whom the BlockState is being retrieved- Returns:
- the BlockState associated with the given player
-
addMetadata
Adds metadata to the PacketBlock. The metadata is a key-value pair where the key is a unique identifier, and the value is associated data. If the specified key already exists in the metadata, the method will do nothing.- Parameters:
key
- The unique key for the metadata. Must not be null.object
- The value associated with the metadata key. Must not be null.
-
removeMetadata
Removes the metadata associated with the specified key. If the key exists in the metadata map, it will be removed.- Parameters:
key
- the key of the metadata to be removed; must not be null
-
hasMetadata
Checks if the metadata contains the specified key.- Parameters:
key
- the key to check for in the metadata; must not be null- Returns:
- true if the metadata contains the specified key, false otherwise
-
getMetadata
Retrieves the metadata value associated with the specified key. If the key does not exist, a default fallback value is used.- Parameters:
key
- The key for which the metadata value is to be retrieved. Must not be null.- Returns:
- The metadata value associated with the given key, or null if no value exists and no fallback is provided.
-
getMetadataAs
Retrieves metadata associated with the specified key and casts it to the desired type. If the key is not present or the cast fails, exceptions may be thrown.- Type Parameters:
T
- The type to which the metadata will be cast.- Parameters:
key
- The key used to reference the metadata. Must not be null.clazz
- The class type to which the metadata should be cast. Must not be null.- Returns:
- The metadata object corresponding to the specified key, cast to the specified type. Returns null if the key is not present or the metadata value itself is null.
-
getMetadata
Retrieves the metadata value associated with the given key. If the key is not present in the metadata, the fallback value is returned.- Parameters:
key
- The key to look up in the metadata. Must not be null.fallback
- The fallback value to return if the key is not present in the metadata. Can be null.- Returns:
- The metadata value associated with the key, or the provided fallback value if the key is not present.
-
addMetadata
public void addMetadata(@NonNull @NonNull org.bukkit.entity.Player player, @NonNull @NonNull String key, @NonNull @NonNull Object object) Adds a metadata key-value pair to the specific player'sPacketBlockPlayerData
, if the player is a viewer.- Parameters:
player
- The player whose metadata is being updated. Must not be null.key
- The key for the metadata entry. Must not be null.object
- The value associated with the specified key. Must not be null.
-
removeMetadata
public void removeMetadata(@NonNull @NonNull org.bukkit.entity.Player player, @NonNull @NonNull String key) Removes the metadata associated with a specific key for the given player, if the player is a viewer of this PacketBlock. This allows targeted removal of metadata entries tied to individual players.- Parameters:
player
- The player whose associated metadata is to be removed. Must not be null.key
- The key of the metadata to be removed. Must not be null.
-
hasMetadata
public boolean hasMetadata(@NonNull @NonNull org.bukkit.entity.Player player, @NonNull @NonNull String key) Checks if the specified player has metadata associated with the given key within this Packet Block.- Parameters:
player
- The player whose metadata association is to be checked. Must not be null.key
- The key of the metadata to check. Must not be null.- Returns:
- True if the specified player has metadata associated with the given key, otherwise false.
-
getMetadata
public Object getMetadata(@NonNull @NonNull org.bukkit.entity.Player player, @NonNull @NonNull String key) Retrieves the metadata associated with a specific player and a given key.- Parameters:
player
- The player for whom metadata is being retrieved. Must not be null.key
- The key identifying the metadata to be retrieved. Must not be null.- Returns:
- The metadata object associated with the specified player and key, or null if no metadata exists for the key.
-
getMetadataAs
public Object getMetadataAs(@NonNull @NonNull org.bukkit.entity.Player player, @NonNull @NonNull String key, @Nullable @Nullable Object fallback) Retrieves metadata associated with a given key for a specified player. If no metadata is found, a fallback value will be returned.- Parameters:
player
- the player whose metadata is to be retrieved; must not be nullkey
- the key associated with the metadata to retrieve; must not be nullfallback
- the value to return if no associated metadata is found; can be null- Returns:
- the metadata value associated with the specified key, or the fallback value if no metadata is found
-
sendUpdate
public void sendUpdate(@NonNull @NonNull org.bukkit.entity.Player player) Sends a block update to the specified player at the current location.- Parameters:
player
- the player to whom the block update will be sent
-
sendUpdates
public void sendUpdates()Sends block updates to all the viewers currently tracking the block at the specified location. This method iterates through all viewers stored in the `viewers` map and attempts to send a block update to each. If a viewer no longer exists or is offline, they are removed from the `viewers` map. The block state sent to each viewer is dependent on the `getBlockState(Player)` implementation, which determines the block's appearance based on the specific viewer. -
getBreakSpeed
public int getBreakSpeed(@NonNull @NonNull org.bukkit.entity.Player player) Calculates and retrieves the break speed for the given player.- Parameters:
player
- the player whose break speed is being requested, must not be null- Returns:
- the calculated break speed for the player; returns a default value if not available
-
simulateBreak
public void simulateBreak(@NonNull @NonNull org.bukkit.entity.Player player) Simulates the breaking action for the specified player. This method triggers a simulation of a break event for the provided player.- Parameters:
player
- The player for whom the breaking action is being simulated. Must not be null.
-
simulateBreak
public void simulateBreak(@NonNull @NonNull org.bukkit.entity.Player player, @Nullable @Nullable org.bukkit.inventory.ItemStack tool) Simulates the block-breaking process, triggering a custom event and handling item drops based on the event state.- Parameters:
player
- The player performing the block-breaking action. Must not be null.tool
- The tool used by the player to break the block. Can be null if no tool is used.
-
addViewCondition
Adds a viewing condition to the list of conditions if it is not already present.- Parameters:
condition
- the condition to be added, represented as aPredicate<Player>
. This condition evaluates to determine whether a player meets the viewing criteria.
-