Running an AsyncRepeatingTask from the main thread?

Discussion in 'Plugin Development' started by Craftiii4, Apr 6, 2012.

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

    Craftiii4

    I am trying to run an repeating task from the main thread, this is the file i am trying to run:

    http://pastebin.com/YVYtrSvw

    How would i run this from the main thread when a is command run?

    -----

    This wants me to change the code in the file above, which causes an error when ever i try to run.

    Code:
    Round1.startRoundOne(player);


    Any help would be appreciated. Thanks.
     
  2. Offline

    kumpelblase2

    SyncRepeatingTask?
     
  3. Offline

    sd5

  4. Offline

    Craftiii4

    The problem is that i cant get the main thread to run the other thread, i have looked at that link sd5 but i cant seem to make it work :(. Thanks anyways.
     
  5. Offline

    sd5

    If you use a sync repeating task you don't have to run another thread...?
     
  6. Offline

    Craftiii4

    I want to though, or else its hard to navigate through the plugin as the main thread is stupidly big. Is this even possible? Thanks for the help so far.
     
  7. Offline

    sd5

    Maybe it's possible, but I think it's difficult...
    Your task is only every 20th server tick, so I think the main thread will manage it easily.
     
  8. Offline

    Craftiii4

    It may be able to handle it, but I don't really want all of the tasks in the same class file. :(
     
  9. Offline

    sd5

    Oh you don't have to.
    Just create a new class like this:
    Code:
    public class MyTask imlpements Runnable {
     
        public void run() {
            //Your task code...
        }
     
    }
    And in your Round1 class:
    Code:
    plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new MyTask(), 60L, 20L);
    EDIT: btw, why does your Round1 class implements Runnable? Your run() is empty...?
     
  10. Offline

    Craftiii4


    The code in the run is very big ^^, thanks for trying to help but I don't think that will solve my problem. I'm trying to make the main class file run that class file above. If I try to do this it forces me to change everything to static and an error happens :(. Thanks so far anyways :).
     
  11. Offline

    billofbong

    Try changing
    Code:java
    1. public static Plugin plugin
    to
    Code:java
    1. private final Plugin plugin
    unless it's in use by other classes, in which case, we probably need to see the code.
     
  12. Offline

    ferrybig

    Code:java
    1. public class Round1 implements Runnable {
    2.  
    3. public static Plugin plugin;
    4.  
    5. public Round1(Plugin instance) {
    6. plugin = instance;
    7. }
    8.  
    9. @Override
    10. public void run() {
    11.  
    12. }
    13.  
    14. private int time = 60;
    15. int round1;
    16.  
    17. public void startRoundOne(final Player player) {
    18. round1 = plugin.getServer().getScheduler()
    19. .scheduleAsyncRepeatingTask(plugin, new Runnable() {
    20. public void run() {
    21.  
    22. // -----------------------//
    23. // CODE //
    24. // CODE //
    25. // CODE //
    26. // -----------------------//
    27.  
    28. if (time == 0) {
    29. stopround1();
    30. }
    31. time--;
    32.  
    33. }
    34.  
    35. }, 60L, 20L);
    36. }
    37.  
    38. public void stopround1() {
    39. plugin.getServer().getScheduler().cancelTask(round1);
    40. Bukkit.getScheduler().cancelTask(round1);
    41. time = 60;
    42. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,new Runnable() {
    43. public void run(){
    44. // Excuted on main server thread
    45. }
    46. });
    47. }
    48.  
    49. }
     
  13. Offline

    Craftiii4

    The problem with removing the statics is that then i can not run this file from the main thread :(. Thanks anyways


    I am trying to run this file from the main thread, when a command is sent.

    Main:

    Code:java
    1.  
    2.  
    3. public boolean onCommand(CommandSender sender, Command command,
    4. String commandLabel, String args[]) {
    5. String commandName = command.getName().toLowerCase();
    6. final Player player;
    7. if (sender instanceof Player) {
    8. player = (Player) sender;
    9. } else {
    10. return true;
    11. }
    12. if (commandName.equals("rounds")) {
    13.  
    14. // startCountdown(player);
    15.  
    16. if (args.length == 1) {
    17.  
    18. final int arenan = 1;
    19.  
    20. String whatisthecommand = args[0].toLowerCase();
    21.  
    22. if (whatisthecommand.equals("test")) {
    23.  
    24. Round1.startRoundOne45(player);
    25.  
    26. }
    27. }
    28. }
    29. }
    30. }
    31.  
    32.  



    This is what i am trying to run

    Code:java
    1. package me.craftiii4.Rounds.Round;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.plugin.Plugin;
    6.  
    7. public class Round1 implements Runnable {
    8.  
    9. public static Plugin plugin;
    10.  
    11. public Round1(Plugin instance) {
    12. plugin = instance;
    13. }
    14.  
    15. @Override
    16. public void run() {
    17.  
    18. }
    19.  
    20.  
    21. private static int time = 60;
    22. static int round1;
    23. public static void startRoundOne45(final Player player) {
    24. System.out.println("[Rounds] TEST0");
    25. round1 = plugin.getServer().getScheduler()
    26. .scheduleAsyncRepeatingTask(plugin, new Runnable() {
    27. public void run() {
    28.  
    29.  
    30. System.out.println("[Rounds] TEST1");
    31.  
    32. if (time == 0) {
    33. stopround1();
    34. System.out.println("[Rounds] TEST2");
    35. }
    36. time--;
    37.  
    38. }
    39.  
    40. }, 60L, 20L);
    41. }
    42.  
    43. public static void stopround1() {
    44. System.out.println("[Rounds] TEST3");
    45. plugin.getServer().getScheduler().cancelTask(round1);
    46. Bukkit.getScheduler().cancelTask(round1);
    47. time = 60;
    48. }
    49.  
    50. }


    Thanks so far.

    EDIT: This is the error i get in the console

    [Rounds] TEST02012-04-08 15:05:20
    [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'rounds' in plugin Rounds v1.0.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:473)
    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:821)
    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
    at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:33)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
    at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
    at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:554)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:452)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:490)
    Caused by: java.lang.NullPointerException
    at me.craftiii4.Rounds.Round.Round1.startRoundOne45(Round1.java:25)
    at me.craftiii4.Rounds.Rounds.onCommand(Rounds.java:129)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    ... 12 more
     
Thread Status:
Not open for further replies.

Share This Page