Making this code happen every 1.5 seconds

Discussion in 'Plugin Development' started by Konkz, Jan 18, 2014.

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

    Konkz

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onMove(PlayerMoveEvent e) {
    4. Player p = e.getPlayer();
    5. Block passblock = p.getWorld().getBlockAt(p.getLocation());
    6. if (passblock.getTypeId() == 8 || passblock.getTypeId() == 9) {
    7. p.damage(1);
    8. p.sendMessage(ChatColor.DARK_RED + "[IMPORTANT]" + ChatColor.AQUA
    9. + " Get out of water!");
    10. }
    11. }


    I'm very confused how to make this damage the player every 1.5'th second
     
  2. Offline

    Gater12

    Konkz I guess you can have a repeating task happen every 1.5 seconds hurting the player.
     
  3. Offline

    Konkz

    But how would I make that happen...
     
  4. Offline

    Maulss

    Konkz
    Create a new class and let it implement BukkitRunnable:
    Code:java
    1. public class updater implements BukkitRunnable {
    2. public void run() {
    3. // Your code here!
    4. }
    5. }


    Then go to your main class and add this to the top and onEnable():
    Code:java
    1. updater u;
    2.  
    3. public void onEnable() {
    4. u = new updater();
    5. u.runTaskTimer(this, 30, 30);
    6. }
     
  5. Offline

    Konkz


    I tried doing that and failed. :|
     
  6. Offline

    Niknea

    Konkz Do you want the code to execute EVERY 1.5 seconds, not just a certain amount of times? If you want that then create a infinite loop that contains the bukkit runnable.
     
  7. Offline

    Konkz


    I want it to repeat every 1.5 seconds, until player gets out of water. And I am not too sure how to create an infinite loop.
     
  8. Offline

    Niknea

    Konkz Possibly use the PlayerMoveEvent and check if they are in water, and if they are perform the BukkitRunnable.

    Also an infinite loop is "for( ;; ) { \\ Code }"
     
  9. Offline

    Konkz


    Could you possibly use the code I provided and embed it into that please? :)
     
  10. Offline

    xMrPoi

    Put a scheduler in the on enable that goes every 1.5 seconds and loops through all the players in the server; checking if they're in water. And if they are, damage them.
     
  11. Offline

    Niknea

    Konkz does everything currently work? Just the only thing that dosent is you want the code to execute every 1.5 seconds?
     
  12. Offline

    Konkz


    The plugin works 100% fine, yes, I just need the event to be delayed by 1.5 seconds.
     
  13. Offline

    Niknea

    Konkz then after the if statement add a Bukkit runnable
     
  14. Offline

    Konkz

    I'm not too sure how. :/

    I'm quit noobish. :p
     
  15. Offline

    sgavster

  16. Offline

    Konkz

    I noticed after I asked :p I got it to work now, thanks everyone!
     
  17. Offline

    Niknea

    Konkz here is a basic Bukkit runnable

    Code:java
    1. Bukkit.getScheduler.scheduleSyncDelayedTask(this, new Runnable() {
    2.  
    3. @Override
    4. public void run() {
    5. // Your code
    6. }, 1 * 30)); // Executes every 1.5 seconds
    7. }
    8.  
     
Thread Status:
Not open for further replies.

Share This Page