Timers that perform actions on players

Discussion in 'Plugin Development' started by eyedjellyfish78, Sep 13, 2013.

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

    eyedjellyfish78

    Hi, I'm pretty new to java and really new to developing plugins, and I have a really simple question. I'm making a test plugin, and all I want to do is have a timer that performs an action on all players every 2 or so seconds such as killing them or anything like that. I want it to happen to every player online without anyone executing any commands; it just keeps happening over and over. I know this is really simple, but I honestly searched around for like 50 minutes straight and couldn't find an answer. Thanks in advance for any help :)
     
  2. Offline

    Quantix

    Look at this tutorial to learn how create scheduled tasks. This code below is an example and kills all players every two seconds like you mentioned.
    Code:java
    1. //This code will only work in your main plugin class that extends JavaPlugin
    2. //The 'this' is a reference the main plugin class
    3. //If you're scheduling the task in another class you need to reference your main plugin class somehow
    4.  
    5. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    6. public void run() {
    7. for (Player player : Bukkit.getOnlinePlayers()) {
    8. if (!player.isDead()) {
    9. player.setHealth(0);
    10. }
    11. }
    12. }
    13. }, 0, 40); //Every second is equal to 20 ticks so this would fire every 2 seconds after a 0 tick delay
     
  3. Offline

    eyedjellyfish78

    Thank you so much! This seems to be exactly what I'm looking for.

    I'm having strange issues with this; it's giving me weird squiggly brace syntax errors. Here's how the whole class looks:

    Code:java
    1. package com.gmail.eyedjellyfish.test;
    2. import org.bukkit.entity.Player;
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6. public class test extends JavaPlugin{
    7.  
    8. public void onEnable() {
    9. getLogger().info("Hi.");
    10. }
    11.  
    12. public void onDisable() {
    13. getLogger().info("Bai");
    14. //error on the following brace
    15. }
    16.  
    17. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    18. public void run() {
    19. for (Player player : Bukkit.getOnlinePlayers()) {
    20. if (!player.isDead()) {
    21. player.setHealth(0);
    22. }
    23. }
    24. }
    25. }, 0, 40);
    26.  
    27. //error on the following brace
    28. }
    29.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  4. Offline

    bobacadodl

    eyedjellyfish78
    You need to put the scheduler inside a method...
    Move it inside your onEnable()
     
  5. Offline

    eyedjellyfish78

    Oh wow... epic fail by me. Thanks.
     
Thread Status:
Not open for further replies.

Share This Page