Class BlockDataManager

java.lang.Object
redempt.redlib.blockdata.BlockDataManager
All Implemented Interfaces:
org.bukkit.event.Listener

public class BlockDataManager extends Object implements org.bukkit.event.Listener
Manages DataBlock instances, which allow you to attach persistent metadata to blocks, Keeps track of managed blocks, removing data if a block is destroyed or moving it if a block is pushed by a piston.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected SQLHelper
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a BlockDataManager instance with a save file location, to be saved to and loaded from.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected Set<DataBlock>
    ensureExists(org.bukkit.World world, redempt.redlib.blockdata.BlockDataManager.ChunkPosition pos)
     
    Loads and returns a set of all DataBlocks managed by this BlockDataManager.
     
     
    getDataBlock(org.bukkit.block.Block block)
    Gets a DataBlock from a given Block, creating a new one if that Block had no data attached to it.
    getExisting(org.bukkit.block.Block block)
    Gets an existing DataBlock, returning null if that Block has no data attached to it.
    getLoaded(org.bukkit.Chunk chunk)
    Gets all the loaded DataBlocks in a chunk
    getLoaded(org.bukkit.World world, int cx, int cz)
    Gets all the loaded DataBlocks in a chunk
    getNearby(org.bukkit.Location loc, int radius)
    Gets all the DataBlocks near an approximate location
    boolean
    isChunkLoaded(org.bukkit.Chunk chunk)
     
    boolean
    isChunkLoaded(org.bukkit.World world, int cx, int cz)
     
    load(org.bukkit.Chunk chunk)
    Loads all of the DataBlocks in a given chunk, or retrieves the already-loaded set of DataBlocks
    load(org.bukkit.World world, int cx, int cz)
    Loads all of the DataBlocks in a given chunk, or retrieves the already-loaded set of DataBlocks
    void
    onBlockExplode(org.bukkit.event.block.BlockExplodeEvent e)
     
    void
    onBlockPull(org.bukkit.event.block.BlockPistonRetractEvent e)
     
    void
    onBlockPush(org.bukkit.event.block.BlockPistonExtendEvent e)
     
    void
    onBreakBlock(org.bukkit.event.block.BlockBreakEvent e)
     
    void
    onBucketEmpty(org.bukkit.event.player.PlayerBucketEmptyEvent e)
     
    void
    onBurnBlock(org.bukkit.event.block.BlockBurnEvent e)
     
    void
    onChunkUnload(org.bukkit.event.world.ChunkUnloadEvent e)
     
    void
    onEntityExplode(org.bukkit.event.entity.EntityExplodeEvent e)
     
    void
    onFlowBreakBlock(org.bukkit.event.block.BlockFromToEvent e)
     
    protected void
     
    void
    Removes a DataBlock from this DataBlockManager
    void
    Saves all data to the save file.
    void
    Saves all data to the save file, and closes the SQL connection.
    void
    setAutoSave(boolean autoSave)
    Sets whether this BlockDataManager will automatically save every 5 minutes.
    void
    setAutoUnload(boolean autoUnload)
    Sets whether this BlockDataManager will automatically unload chunks of DataBlocks when a chunk is unloaded
    protected redempt.redlib.blockdata.BlockDataManager.ChunkPosition
    toChunkPosition(org.bukkit.block.Block block)
     
    protected redempt.redlib.blockdata.BlockDataManager.ChunkPosition
    toChunkPosition(org.bukkit.Location loc)
     
    protected Optional<Set<DataBlock>>
    tryExists(org.bukkit.World world, redempt.redlib.blockdata.BlockDataManager.ChunkPosition pos)
     
    void
    unload(org.bukkit.Chunk chunk)
    Saves and unloads all of the DataBlocks in a chunk
    void
    unload(org.bukkit.World world, int cx, int cz)
    Saves and unloads all of the DataBlocks in a chunk
    void
    Saves and unloads all DataBlocks from this BlockDataManager

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • BlockDataManager

      public BlockDataManager(Path saveFile)
      Create a BlockDataManager instance with a save file location, to be saved to and loaded from. This constructor immediately loads from the given file.
      Parameters:
      saveFile - The Path to load from immediately, and save to when save is called
  • Method Details

    • getAllManagers

      public static List<BlockDataManager> getAllManagers()
      Returns:
      The list of all active BlockDataManagers
    • setAutoSave

      public void setAutoSave(boolean autoSave)
      Sets whether this BlockDataManager will automatically save every 5 minutes. Defaults to true.
      Parameters:
      autoSave - Whether to save automatically every 5 minutes
    • setAutoUnload

      public void setAutoUnload(boolean autoUnload)
      Sets whether this BlockDataManager will automatically unload chunks of DataBlocks when a chunk is unloaded
      Parameters:
      autoUnload - Whether to automatically unload DataBlocks
    • save

      public void save()
      Saves all data to the save file.
    • saveAndClose

      public void saveAndClose()
      Saves all data to the save file, and closes the SQL connection. Call this in your onDisable.
    • ensureExists

      protected Set<DataBlock> ensureExists(org.bukkit.World world, redempt.redlib.blockdata.BlockDataManager.ChunkPosition pos)
    • tryExists

      protected Optional<Set<DataBlock>> tryExists(org.bukkit.World world, redempt.redlib.blockdata.BlockDataManager.ChunkPosition pos)
    • toChunkPosition

      protected redempt.redlib.blockdata.BlockDataManager.ChunkPosition toChunkPosition(org.bukkit.Location loc)
    • toChunkPosition

      protected redempt.redlib.blockdata.BlockDataManager.ChunkPosition toChunkPosition(org.bukkit.block.Block block)
    • getExisting

      public DataBlock getExisting(org.bukkit.block.Block block)
      Gets an existing DataBlock, returning null if that Block has no data attached to it.
      Parameters:
      block - The block to check
      Returns:
      A DataBlock, or null
    • getDataBlock

      public DataBlock getDataBlock(org.bukkit.block.Block block)
      Gets a DataBlock from a given Block, creating a new one if that Block had no data attached to it.
      Parameters:
      block - The block to check or create a DataBlock from
      Returns:
      An existing or new DataBlock
    • register

      protected void register(DataBlock db)
    • remove

      public void remove(DataBlock db)
      Removes a DataBlock from this DataBlockManager
      Parameters:
      db - The DataBlock to remove
    • getNearby

      public Set<DataBlock> getNearby(org.bukkit.Location loc, int radius)
      Gets all the DataBlocks near an approximate location
      Parameters:
      loc - The location to check near
      radius - The radius to check in
      Returns:
      The nearby DataBlocks
    • getLoaded

      public Set<DataBlock> getLoaded(org.bukkit.Chunk chunk)
      Gets all the loaded DataBlocks in a chunk
      Parameters:
      chunk - The chunk to get the loaded DataBlocks in
      Returns:
      A set of DataBlocks in the chunk, or null if the chunk is not loaded
    • getLoaded

      public Set<DataBlock> getLoaded(org.bukkit.World world, int cx, int cz)
      Gets all the loaded DataBlocks in a chunk
      Parameters:
      world - The world the chunk is in
      cx - The chunk X
      cz - The chunk Z
      Returns:
      A set of DataBlocks in the chunk, or an empty set if the chunk is not loaded
    • load

      public Set<DataBlock> load(org.bukkit.World world, int cx, int cz)
      Loads all of the DataBlocks in a given chunk, or retrieves the already-loaded set of DataBlocks
      Parameters:
      world - The world the chunk is in
      cx - The chunk X
      cz - The chunk Z
      Returns:
      The set of DataBlocks in the chunk
    • load

      public Set<DataBlock> load(org.bukkit.Chunk chunk)
      Loads all of the DataBlocks in a given chunk, or retrieves the already-loaded set of DataBlocks
      Parameters:
      chunk - The chunk to load DataBlocks in
      Returns:
      The set of DataBlocks in the chunk
    • unload

      public void unload(org.bukkit.World world, int cx, int cz)
      Saves and unloads all of the DataBlocks in a chunk
      Parameters:
      world - The world the chunk is in
      cx - The chunk X
      cz - The chunk Z
    • unload

      public void unload(org.bukkit.Chunk chunk)
      Saves and unloads all of the DataBlocks in a chunk
      Parameters:
      chunk - The chunk to unload the DataBlocks in
    • unloadAll

      public void unloadAll()
      Saves and unloads all DataBlocks from this BlockDataManager
    • isChunkLoaded

      public boolean isChunkLoaded(org.bukkit.Chunk chunk)
      Parameters:
      chunk - The chunk to check
      Returns:
      Whether the DataBlocks in the chunk are loaded in this BlockDataManager
    • isChunkLoaded

      public boolean isChunkLoaded(org.bukkit.World world, int cx, int cz)
      Parameters:
      world - The world the chunk is in
      cx - The X coordinate of the chunk
      cz - The Z coordinate of the chunk
      Returns:
      Whether the DataBlocks in the chunk are loaded in this BlockDataManager
    • getAllLoaded

      public Set<DataBlock> getAllLoaded()
      Returns:
      The set of all loaded DataBlocks
    • getAll

      public Set<DataBlock> getAll()
      Loads and returns a set of all DataBlocks managed by this BlockDataManager. Avoid calling this if possible. Will not return DataBlocks in unloaded worlds.
      Returns:
      The set of all DataBlocks managed by this BlockDataManager
    • onBreakBlock

      public void onBreakBlock(org.bukkit.event.block.BlockBreakEvent e)
    • onBucketEmpty

      public void onBucketEmpty(org.bukkit.event.player.PlayerBucketEmptyEvent e)
    • onFlowBreakBlock

      public void onFlowBreakBlock(org.bukkit.event.block.BlockFromToEvent e)
    • onBurnBlock

      public void onBurnBlock(org.bukkit.event.block.BlockBurnEvent e)
    • onEntityExplode

      public void onEntityExplode(org.bukkit.event.entity.EntityExplodeEvent e)
    • onBlockExplode

      public void onBlockExplode(org.bukkit.event.block.BlockExplodeEvent e)
    • onBlockPush

      public void onBlockPush(org.bukkit.event.block.BlockPistonExtendEvent e)
    • onBlockPull

      public void onBlockPull(org.bukkit.event.block.BlockPistonRetractEvent e)
    • onChunkUnload

      public void onChunkUnload(org.bukkit.event.world.ChunkUnloadEvent e)