Player's syncRepeatingTask

Discussion in 'Plugin Development' started by Staartvin, May 10, 2012.

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

    Staartvin

    Hello Everyone,

    I have a question,
    I want to create a syncrepeatingtask when a player joins. So for example:

    - Player1 joins
    - Creates syncrepeatingtask with name: "Player1repeatingtask"
    - Player2 joins
    - Creates syncrepeatingtask with name: "Player2repeatingtask"
    - Staartvin joins
    - Creates syncrepeatingtask with name: "Staartvinrepeatingtask"

    Something like that. Is this possible? If so, could you be so kind to tell me? ;)

    Thank you in advance,
    Staartvin
     
  2. Offline

    Lolmewn

    Of course it is. Just start a new one ;) I'm just not sure how you want to name the threads, because the only thing you can get back is an int, representing the Bukkit Thread ID for that thread.
     
  3. Offline

    Staartvin

    That's the problem. :p I know Java can do this, but I don't know how. I thought it was called Dynamic Programming where the value of an object is the name you can create things with. The problem is I can't find any tutorials about it, nor do I understand them :p So I hope for more luck!
     
  4. You can't use names, so save the int (id of the task) for your specific player using a HashMap.
     
  5. Code:java
    1. LookItsATask task = new LookItsATask(plugin, player.getName());
    2. task.setPid(plugin.getServer().getScheduler().scheduleSyncRepeatingTask(task, 20L, 20L));

    Code:java
    1. public class LookItsATask implements Runnable
    2. {
    3. private final YourPlugin plugin;
    4. private final String name;
    5. private int pid;
    6. private count = 0;
    7.  
    8. public LookItsATask(YourPlugin plugin, String name)
    9. {
    10. this.plugin = plugin;
    11. this.name = name;
    12. }
    13.  
    14. public void setPid(int pid)
    15. {
    16. this.pid = pid;
    17. }
    18.  
    19. public Player getPlayer() // Just in case you want to use the class out of the BukkitScheduler, too, and need to get the name...
    20. {
    21. return plugin.getServer().getPlayerExact(name);
    22. }
    23.  
    24. public void run()
    25. {
    26. Player player = getPlayer();
    27. if(player == null)
    28. plugin.getServer().getScheduler().cancelTask(pid);
    29. else
    30. player.sendMessage("You're online for "+(++count)+" seconds now!");
    31. }
    32. }

    Where's the problem...? :confused:
     
  6. Offline

    Staartvin

    Yeah.. That doesn't work. I get error in Eclipse about here:

    LookItsATask task = new LookItsATask(plugin, player.getName());
    task.setPid(plugin.getServer().getScheduler().scheduleSyncRepeatingTask(task, 20L, 20L));
     
  7. What error and where exactly? <.<
     
  8. Offline

    Staartvin

    Whoops forgot to mention error, Sorry!


    Error:

    The method scheduleSyncRepeatingTask(Plugin, Runnable, long, long) in the type BukkitScheduler is not applicable for the arguments (Broadcaster.LookItsATask, long, long)
    at:

    task.setPid(plugin.getServer().getScheduler().scheduleSyncRepeatingTask(task, 20L, 20L));
     
  9. so change it to
    task.setPid(plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, task, 20L, 20L));
    ...
     
  10. Offline

    Staartvin

    It gives me a nullpointer.. on this line:
    task.setPid(plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, task, 20L, 20L));

    What could be null?

    Whole code here:

    Code:java
    1.  
    2. package me.staartvin.SimpleBroadcaster;
    3.  
    4.  
    5. import me.staartvin.SimpleBroadcaster.Broadcaster;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerJoinEvent;
    14. import org.bukkit.plugin.PluginManager;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class Broadcaster extends JavaPlugin implements Listener
    18. {
    19. private Broadcaster plugin;
    20. String playername1 = "Staartvin";
    21. String playername2 = "Carlosj";
    22. int broadcaster = 0;
    23. int broadcaster2 = 0;
    24. int time = 10;
    25. int time2 = 10;
    26. boolean broadcastison;
    27. PluginManager pm;
    28.  
    29. public void Players2(Broadcaster plugin)
    30. {
    31. this.plugin = plugin;
    32. }
    33.  
    34. public void onEnable() {
    35. System.out.println(getDescription().getName() + " version " + getDescription().getVersion() + " has been enabled!");
    36. this.pm = getServer().getPluginManager();
    37.  
    38. this.pm.registerEvents(this, this);
    39. }
    40.  
    41. public void onDisable() {
    42. System.out.println(getDescription().getName() + " version " + getDescription().getVersion() + " has been disabled!");
    43. }
    44.  
    45. public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args) {
    46.  
    47. if (cmd.getName().equalsIgnoreCase("teston")) {
    48. if (broadcaster == 0) {
    49. broadcaster = 1;
    50. broadcaster = getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    51. public void run() {
    52. System.out.println("Time left: " + time);
    53. if (time == 0) {
    54. getServer().broadcastMessage(ChatColor.BLUE + "Time is over!");
    55. getServer().getScheduler().cancelTask(broadcaster);
    56. time = 10;
    57. broadcastison = false;
    58. time = 10;
    59. }
    60. else {
    61. time--;
    62. }
    63.  
    64. }
    65. }, 0L, 20L);
    66. sender.sendMessage("The broadcaster has been turned on!");
    67. }
    68. else if (broadcaster2 == 0) {
    69. broadcaster2 = 1;
    70. broadcaster2 = getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    71. public void run() {
    72. System.out.println("Time left: " + time2);
    73. if (time2 == 0) {
    74. getServer().broadcastMessage(ChatColor.BLUE + "Time is over!");
    75. getServer().getScheduler().cancelTask(broadcaster2);
    76. time2 = 10;
    77. broadcastison = false;
    78. time2 = 10;
    79. }
    80. else {
    81. time2--;
    82. }
    83.  
    84. }
    85. }, 0L, 20L);
    86. sender.sendMessage("The broadcaster2 has been turned on!");
    87. }
    88. }
    89. else if (cmd.getName().equalsIgnoreCase("testoff")){
    90. if (!broadcastison) {
    91. sender.sendMessage("The broadcaster is already off!");
    92. return true;
    93. }
    94. else {
    95. broadcastison = false;
    96. getServer().getScheduler().cancelTask(broadcaster);
    97. sender.sendMessage("The broadcaster has been turned off!");
    98.  
    99. }
    100. }
    101. else if (cmd.getName().equalsIgnoreCase("testtime")){
    102. sender.sendMessage("Time is: " + time);
    103. }
    104.  
    105.  
    106. return true;
    107.  
    108. }
    109.  
    110.  
    111.  
    112. public class LookItsATask implements Runnable
    113. {
    114. private final Broadcaster plugin;
    115. private final String name;
    116. private int pid;
    117. private int count = 0;
    118.  
    119. public LookItsATask(Broadcaster plugin, String name)
    120. {
    121. this.plugin = plugin;
    122. this.name = name;
    123. }
    124.  
    125. public void setPid(int pid)
    126. {
    127. this.pid = pid;
    128. }
    129.  
    130. public Player getPlayer() // Just in case you want to use the class out of the BukkitScheduler, too, and need to get the name...
    131. {
    132. return plugin.getServer().getPlayerExact(name);
    133. }
    134.  
    135. public void run()
    136. {
    137. Player player = getPlayer();
    138. if(player == null)
    139. plugin.getServer().getScheduler().cancelTask(pid);
    140. else
    141. player.sendMessage("You're online for "+(++count)+" seconds now!");
    142. }
    143. }
    144.  
    145. @EventHandler
    146. public void onPlayerJoin(PlayerJoinEvent event) {
    147.  
    148. Player player = event.getPlayer();
    149. String playername = player.getName();
    150. LookItsATask task = new LookItsATask(plugin, player.getName());
    151. task.setPid(plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, task, 20L, 20L));
    152. }
    153.  
    154.  
    155. }
    156.  
     
  11. plugin is null.
    Remove this:
    Code:java
    1. private Broadcaster plugin;

    this:
    Code:java
    1. public void Players2(Broadcaster plugin)
    2. {
    3. this.plugin = plugin;
    4. }

    and change every "plugin." to "this."
     
Thread Status:
Not open for further replies.

Share This Page