2 sequential runnable

Discussion in 'Plugin Development' started by Quantico, Feb 14, 2022.

Thread Status:
Not open for further replies.
  1. Offline

    Quantico

    Good time of day, guys. I need help. I'm writing a plugin. Its essence lies in the periodic establishment and destruction of the block. I am creating 2 Bukkit Runnables. This works, but only if you specify the delay and execution time as 2 to 1. I require them to be executed sequentially.
    The block is installed, then 3 minutes pass and the block breaks. After the next 7 minutes, the block again installed and again after 3 minutes block breaks. And so on indefinitely.

    Thank you in advance. I appreciate your time.
     
  2. Offline

    Strahan

    I'd make an object to hold data on the block (location, material, last operation, last operation time) then make a collection of said objects and then a single runnable that runs every 20 ticks (but remember, the 20 ticks = 1 second is only under ideal conditions, it isn't 100%). Then in my runnable, I'd loop the collection and check if the object is ready to change state then act on it.

    EDIT: I'm in a really boring meeting right now so I fired up Minecraft and figured I'd write up and test an example, lol. Here is the idea.

    Code:
    Main class {
      Create List of DynamicBlocks
    
      onEnable {
        Run timer method
      }
    
      timer method {
        runnable {
          Create iterator
          Loop iterator {
            Is obj not ready for change?  continue
    
            If obj is visible (unbroken) {
              Set the location's block to air
              Set visible to false since we broke it
              continue
            }
    
            Set the location's block to the right material
            Set visible to true
          }
        } Run task timer for 20 ticks
      }
    }
    
    BlockBreakEvent {
      Create a new DynamicBlock
      Is the DynamicBlock already in the collection?  return
    
      Set the visible to false, since this is BlockBreakEvent
      Add to the collection
    }
    
    DynamicBlock {
      private MyPlugin plugin = null;
      private Location loc = null;
      private long lastchanged = 0;
      private boolean isVisible = true;
      private Material mat = null;
    
      public DynamicBlock(MyPlugin plugin, Location loc, Material mat) {
        Set the passed vars and set lastchanged to now
      }
    
      Override equals to return true if the locations are the same
    
      public void setVisible(boolean isVisible) {
        Set isVisible and update lastchanged
      }
    
      public boolean isReadyToChange() {
        get int adjustment from config, using isVisible to determine the appropriate path
        Multiply adjustment by 1000
        Return current time greater than or equal to last changed plus adjustment
      }
    }
    Only problem I can see with this, now that I think about it, is by only using material stuff like stairs and whatnot would lose their orientation data. So I guess one should add blockdata to the dynamicblock object so that can be restored too.
     
    Last edited: Feb 14, 2022
  3. Offline

    Tim_M

    Why not just put a scheduled task inside of a scheduled task, that way you can reuse the same variables.
     
Thread Status:
Not open for further replies.

Share This Page