Solved Pause event for x seconds.

Discussion in 'Spigot Plugin Development' started by Squidgie, Mar 3, 2020.

Thread Status:
Not open for further replies.
  1. Hi,

    How would I pause an event for x amount of seconds.

    e.g Player mines a diamond ore (BlockBreakEvent), the event won't trigger for another 5s which within, the player can mine as many blocks and the event won't be set off.

    I tried to do
    Code (Java):
    TimeUnit.SECONDS.sleep(10);
    inside the event, however it ended up killing/lagging the server.
     
  2. Hello,

    Do not try to sleep the main thread, it will just block your server.

    Your best solution here is to create a cooldown system:
    -> Create a Map<UUID, Long>
    -> When player break something, check if the map contains the player, get and compare the time added in the map (using System.currentTimeMillis())
     
  3. How would I implement this into an event?

     
  4. Create a HashSet of UUIDs, which will store the players who are on cooldown. Now, in your BlockBreakEvent, first check if the HashSet contains the players UUID, and if it does, cancel the event (this means the player was on cooldown). If the player was not on cooldown add the players UUID to the HashSet, create a BukkitRunnable that removes their UUID from the HashSet, and run it 100 ticks later (5 seconds). Voila.
    Apologies if this is a little vague, but I believe that challenging yourself is the best way to learn.
     
    • Like Like x 1
  5. What are you trying to achieve by delaying the event?
    Are you also delaying the block break?
    What you need to consider is the expectation of other plugins on the server and that the BlockBreakEvent is cancelable.
     
  6. Code (Text):
    public class BlockBreak implements Listener {

        Set<UUID> delaySet = new HashSet<UUID>();

        @EventHandler
        void onBlockBreak(BlockBreakEvent event) {
            if (event.isCancelled())
                return;
            final UUID uuid = event.getPlayer().getUniqueId();
            if (delaySet.contains(uuid)) {
                event.setCancelled(true);
                return;
            }
            delaySet.add(uuid);
            new BukkitRunnable() {
                @Override
                public void run() {
                    onDelayExpire(uuid, event.getPlayer());
                }
            }.runTaskLater(Main.getInstance(), 5 * 20);
            // 5 secs * 20 cause this value is in ticks
            // replace Main with the name of your plugins main class
            // create Main getInstance() { return plugin; } in main class where plugin
            // is defined as plugin = this in onEnable
        }

        void onDelayExpire(UUID uuid, Player player) {
            delaySet.remove(uuid);
            // put any code you want to happen after the delay here.
        }

    }
     
     
  7. Thank you, I did end up coding my own timer, using Scheduler and HashSets.
     
    • Like Like x 1
Thread Status:
Not open for further replies.