Schedule a message every x ticks?

Discussion in 'Plugin Development' started by x86cam, Jan 21, 2014.

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

    x86cam

    I've been working on a plugin for my server. It is supposed to find my radio station's stream info, which it does correctly. I want it to broadcast the info every 10 minutes. I've had no luck so far.

    Out of all the things I've tried, my outcome is always "Plugin attempted to register task while disabled."

    I mean everywhere it says to put the scheduler method is in the onEnable, and I get the above message every time.

    Code without the scheduler (and yes, only one file):
    Code:java
    1. package com.x86cam.MineRadio;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. import java.net.URI;
    11. import java.net.URISyntaxException;
    12. import java.util.List;
    13.  
    14. import net.moraleboost.streamscraper.ScrapeException;
    15. import net.moraleboost.streamscraper.Stream;
    16. import net.moraleboost.streamscraper.Scraper;
    17. import net.moraleboost.streamscraper.scraper.IceCastScraper;
    18.  
    19. public class MineRadio extends JavaPlugin {
    20.  
    21. static String pName = "MineRadio";
    22. String v = "1.0";
    23. static String prefix = "[" + pName + "] ";
    24. static MineRadio plugin;
    25.  
    26. @Override
    27. public void onEnable(){
    28. this.saveDefaultConfig();
    29. getLogger().info(prefix + pName + " version " + v + " has been enabled.");
    30. plugin = new MineRadio();
    31. }
    32.  
    33. @Override
    34. public void onDisable() {
    35. getLogger().info(prefix + pName + " version " + v + " has been disabled.");
    36. }
    37.  
    38. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    39. if(cmd.getName().equalsIgnoreCase("radio")) {
    40. if (args.length > 0) {
    41. sender.sendMessage(ChatColor.RED + "Too many arguments");
    42. return false;
    43. } else {
    44. if (!(sender instanceof Player)) {
    45. String host = getConfig().getString("host");
    46. String port = getConfig().getString("port");
    47. String mount = getConfig().getString("mount");
    48. String radioName = getConfig().getString("radio-name");
    49. try {
    50. Scraper scraper = new IceCastScraper();
    51. List<Stream> streams = scraper.scrape(new URI("http://"+host+":"+port+"/"+mount));
    52. Bukkit.broadcastMessage(ChatColor.GRAY + prefix + "Information on " + radioName);
    53. Bukkit.broadcastMessage(ChatColor.GRAY + "====================================");
    54. for (Stream stream: streams) {
    55. Bukkit.broadcastMessage(ChatColor.YELLOW + "Song: " + stream.getCurrentSong());
    56. Bukkit.broadcastMessage(ChatColor.GREEN + "Listeners: " + stream.getCurrentListenerCount());
    57. }
    58. String listenLink = getConfig().getString("listen-link");
    59. Bukkit.broadcastMessage(ChatColor.AQUA + "To listen, click the link: " + listenLink);
    60. } catch (ScrapeException e) {
    61. e.printStackTrace();
    62. } catch (URISyntaxException e) {
    63. e.printStackTrace();
    64. }
    65. return true;
    66. } else {
    67. Player player = (Player) sender;
    68. //logger.info(prefix + player.getName() + " has executed '/playing'");
    69. String host = getConfig().getString("host");
    70. String port = getConfig().getString("port");
    71. String mount = getConfig().getString("mount");
    72. String radioName = getConfig().getString("radio-name");
    73. try {
    74. Scraper scraper = new IceCastScraper();
    75. List<Stream> streams = scraper.scrape(new URI("http://"+host+":"+port+"/"+mount));
    76. player.sendMessage(ChatColor.GRAY + prefix + "Information on " + radioName);
    77. player.sendMessage(ChatColor.GRAY + "====================================");
    78. for (Stream stream: streams) {
    79. player.sendMessage(ChatColor.YELLOW + "Song: " + stream.getCurrentSong());
    80. player.sendMessage(ChatColor.GREEN + "Listeners: " + stream.getCurrentListenerCount());
    81. }
    82. } catch (ScrapeException e) {
    83. e.printStackTrace();
    84. } catch (URISyntaxException e) {
    85. e.printStackTrace();
    86. }
    87. String listenLink = getConfig().getString("listen-link");
    88. player.sendMessage(ChatColor.AQUA + "To listen, click the link: " + listenLink);
    89. return true;
    90. }
    91. }
    92. }//If this has happened the function will return true.
    93. // If this hasn't happened the a value of false will be returned.
    94. return false;
    95. }
    96.  
    97. }
    98.  


    Where should I put the scheduler, because I want to execute it every 10 minutes after the plugin starts. What do?
     
  2. Offline

    1Rogue

    First off, get rid of static.

    As for the task, try registering it to run after 1 server tick (rather than instantly).
     
  3. Offline

    x86cam

    This is what I get:
    Code:java
    1. [19:06:15 INFO]: Preparing start region for level 1 (Seed: 93573100628603986)
    2. [19:06:15 INFO]: Preparing start region for level 2 (Seed: 93573100628603986)
    3. [19:06:15 INFO]: [MineRadio] Enabling MineRadio v1.0
    4. [19:06:15 INFO]: [MineRadio] [MineRadio] MineRadio version 1.0 has been enabled.
    5.  
    6. [19:06:15 ERROR]: Error occurred while enabling MineRadio v1.0 (Is it up to date
    7. ?)
    8. org.bukkit.plugin.IllegalPluginAccessException: Plugin attempted to register tas
    9. k while disabled
    10. at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.validate(Craf
    11. tScheduler.java:394) ~[bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    12. at org.bukkit.craftbukkit.v1_7_R1.scheduler.CraftScheduler.runTaskTimerA
    13. synchronously(CraftScheduler.java:137) ~[bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974j
    14. nks]
    15. at com.x86cam.MineRadio.MineRadio.runScheduler(MineRadio.java:101) ~[?:?
    16. ]
    17. at com.x86cam.MineRadio.MineRadio.onEnable(MineRadio.java:31) ~[?:?]
    18. at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:218) ~[b
    19. ukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    20. at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    21. .java:457) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    22. at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    23. r.java:384) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    24. at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugin(CraftServer.jav
    25. a:298) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    26. at org.bukkit.craftbukkit.v1_7_R1.CraftServer.enablePlugins(CraftServer.
    27. java:280) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    28. at net.minecraft.server.v1_7_R1.MinecraftServer.m(MinecraftServer.java:3
    29. 42) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    30. at net.minecraft.server.v1_7_R1.MinecraftServer.g(MinecraftServer.java:3
    31. 19) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    32. at net.minecraft.server.v1_7_R1.MinecraftServer.a(MinecraftServer.java:2
    33. 75) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    34. at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.jav
    35. a:175) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    36. at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    37. :424) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    38. at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    39. 17) [bukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    40. [19:06:15 INFO]: Server permissions file permissions.yml is empty, ignoring it
    41. [19:06:15 INFO]: Done (1.892s)! For help, type "help" or "?"
    42. >


    Here's my code with the Scheduler in it:
    http://pastebin.com/KTKCV3xh
     
  4. Offline

    xTrollxDudex

    x86cam
    Don't use the static plugin field, that is unnecessary. You are doing it wrong anyways. Use "this" in the place of plugin.
     
  5. Offline

    x86cam

    Oh wow, simply changing plugin to this got it working. Huh...
     
Thread Status:
Not open for further replies.

Share This Page