scheduleSyncDelayedTask works only 1 time

Discussion in 'Plugin Development' started by DeCyRed, Feb 26, 2014.

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

    DeCyRed

    Hey, Im having a problem with me plugin.

    Code:java
    1. package nl.decyred.scoreboard;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.PlayerJoinEvent;
    9. import org.bukkit.event.player.PlayerLevelChangeEvent;
    10. import org.bukkit.event.player.PlayerQuitEvent;
    11. import org.bukkit.scoreboard.DisplaySlot;
    12. import org.bukkit.scoreboard.Objective;
    13. import org.bukkit.scoreboard.Score;
    14. import org.bukkit.scoreboard.Scoreboard;
    15. import org.bukkit.scoreboard.ScoreboardManager;
    16.  
    17. import nl.decyred.Main;
    18.  
    19. public class scoreboard implements Listener{
    20.  
    21. Main plugin;
    22.  
    23. public scoreboard(Main plugin)
    24. {
    25. this.plugin = plugin;
    26. }
    27. @EventHandler
    28. public void playerjoin(PlayerJoinEvent event) {
    29. Player p = event.getPlayer();
    30. p.performCommand("static 1234");
    31. event.setJoinMessage(ChatColor.DARK_GRAY + "["+ ChatColor.AQUA + "+" + ChatColor.DARK_GRAY + "] " + ChatColor.GREEN + p.getName() + " heeft de server betreden.");
    32. ScoreboardManager manager = Bukkit.getScoreboardManager();
    33. Scoreboard board = manager.getNewScoreboard();
    34. Objective objective = board.registerNewObjective("test2", "dummy");
    35. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    36. objective.setDisplayName( ChatColor.GOLD +"Statistieken");
    37. Score score = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + " Online:"));
    38. score.setScore(1); //Integer only!
    39. Score score2 = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.WHITE +"- " + ChatColor.GRAY + Bukkit.getOnlinePlayers().length));
    40. score2.setScore(0); //Integer only!
    41. p.setScoreboard(board);
    42. }
    43. @EventHandler
    44. public void playerjoin(PlayerQuitEvent event) {
    45. event.setQuitMessage(ChatColor.DARK_GRAY + "["+ ChatColor.AQUA + "-" + ChatColor.DARK_GRAY + "] " + ChatColor.GREEN + p.getName() + " heeft de server verlaten.");
    46. }
    47.  
    48. }
    49.  

    Code:java
    1. package nl.decyred;
    2.  
    3. import nl.decyred.scoreboard.scoreboard;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Damageable;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12. import org.bukkit.scoreboard.DisplaySlot;
    13. import org.bukkit.scoreboard.Objective;
    14. import org.bukkit.scoreboard.Score;
    15. import org.bukkit.scoreboard.Scoreboard;
    16. import org.bukkit.scoreboard.ScoreboardManager;
    17.  
    18. public class Main extends JavaPlugin {
    19.  
    20. public void onEnable() {
    21. getConfig().options().copyDefaults(true);
    22. saveConfig();
    23. Bukkit.getServer().getPluginManager().registerEvents(new scoreboard(this), this);
    24. }
    25.  
    26. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    27. final Player player = (Player) sender;
    28. if(cmd.getName().equalsIgnoreCase("static")) {
    29. if (args.length == 0) {
    30. player.sendMessage(ChatColor.DARK_GRAY + "-------------[" + ChatColor.GOLD + "Static" + ChatColor.DARK_GRAY + "]-------------");
    31. player.sendMessage(ChatColor.WHITE + " Licentie aan: " + ChatColor.AQUA + getConfig().getString("Licentie"));
    32. player.sendMessage(ChatColor.WHITE + " Made by: " + ChatColor.AQUA + " DeCyRed");
    33. return true;
    34. }else {
    35. if(args.length == 1) {
    36. if(args[0].equalsIgnoreCase("1234")) {
    37. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    38. public void run(){
    39. player.sendMessage("Refresh caught");
    40. ScoreboardManager manager = Bukkit.getScoreboardManager();
    41. Scoreboard board = manager.getNewScoreboard();
    42. Objective objective = board.registerNewObjective("test2", "dummy");
    43. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    44. objective.setDisplayName( ChatColor.GOLD +"Statistieken");
    45. Score score = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.GOLD + " Online:"));
    46. score.setScore(1); //Integer only!
    47. Score score2 = objective.getScore(Bukkit.getOfflinePlayer(ChatColor.WHITE +"- " + ChatColor.GRAY + Bukkit.getOnlinePlayers().length));
    48. score2.setScore(0); //Integer only!
    49. player.setScoreboard(board);
    50. }
    51. }, 20 * 5);
    52.  
    53. }
    54. }
    55. }
    56. }
    57. return false;
    58. }
    59. }

    So basicly what it does is when the player joins it gives him a scoreboard and then the player automaticly runs the command /static 1234 which will give it a refresh every 5 second but it only refreshes for 1 time.
     
  2. Offline

    KaiPol

    Code:
        Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run(){
                    // Code to run here //
                }
            }, 0, 100L);
     
    NathanWolf likes this.
  3. Offline

    DeCyRed

    Actual that's what 20*5 means becaue 20x5= 100. At another plugin of mine it does work but i dont know what i did wrong this time.
     
  4. Offline

    NathanWolf

    I think the main fix KaiPol was trying to show you was if you want the task to repeat, use "scheduleSyncRepeatingTask" :)

    "scheduleSyncDelayedTask" only runs once, by design.
     
Thread Status:
Not open for further replies.

Share This Page