1.15.2 How to add a timer to an event

Discussion in 'Spigot Plugin Development' started by Tatertot.h, Mar 4, 2020.

  1. I have set up the event that I want to happen, but I do not know how to write out a timer that would make it work. I have looked up how to make a timer in java, but every time I attempt to integrate it, some other error pops up. When I fix that, it just creates more errors. Can you all help. I will put the code in text below. Thank you.

    note: I have the respawn event as a placeholder for the timer, just so I could test if the actual heath adjusting worked.

    Code:

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public final class TimerHealth extends JavaPlugin implements Listener {

    @Override
    public void onEnable() {
    System.out.println("The Plugin Has Started");
    getServer().getPluginManager().registerEvents(this, this);
    }
    @EventHandler
    public void afterFiveMinutes(PlayerRespawnEvent event){
    Player player = event.getPlayer();
    double x = player.getMaxHealth();
    player.setMaxHealth(--x);
    player.sendMessage("This is what you get for dying");

    }
    }
     
  2. 2 ways as far I know you can do this

    1st. BukkitRunnables, they will allow you set delays.

    2nd. Take the current system time in ms, and have a simple boolean check to see if enough milliseconds have passed
     
  3. In the future use a service such as Pastebin or Hastebin or use the site's integrated code formatter.

    Insert... -> Code

    Well the only way is to to use a Bukkit Scheduler, as the way Diet_Cola isn't suggested and wouldn't work unless the player continued to respawn.

    Now, here's a helpful site with examples which I suggest reading to learn below:
    https://bukkit.gamepedia.com/Scheduler_Programming
     
    • Like Like x 1