Solved Storing runnables

Discussion in 'Plugin Development' started by Betagear, Sep 30, 2015.

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

    Betagear

    Hi.
    I tried to find it by myself but I didn't fount, how can we store Runnables inside an array or a collection ? This is how I would use it :
    • Schedule a task to run for later and store it
    • Then, once the task is ran, delete it from the collection
    • If the plugin deactivates, run everything that's in the collection
    For running the runnable, I use
    Code:
    new BukkitRunnable() {
                       
                        @Override
                        public void run() {
                            //Thing to run
                           
                        }
                    }.runTaskLater(plugin, delay);
    Thanks for helping if you know how.
     
  2. Offline

    teej107

    @Betagear Just make a collection that'll store the BukkitRunnables. I think it'll be best to make a custom class for the Bukkit Runnable where when the task is started, you can tell it to be removed from the collection. When the plugin gets disabled, just loop through the remaining BukkitRunnables in the collection and call the run method.
     
  3. Offline

    Betagear

    Can you give me a code for the declaration of the collection ? That's what I'm looking for.
    And if also you could give me the code for adding and removing it from the collection this would be nice.
     
  4. Offline

    bwfcwalshy Retired Staff

  5. Offline

    Betagear

  6. Offline

    mythbusterma

    @Betagear

    No, you may not. Do you understand what "new BukkitRunnable()" is? It's a constructor. It returns a BukkitRunnable. Store it.
     
  7. Offline

    frrancis909

    Code:
    List<BukkitRunnable> collection = new ArrayList<>();
    collection.add(new BukkitRunnable()
    {
        @Override
         public void run()
        {
        //Thing to run
        }
    }.runTaskLater(plugin, delay));
     
  8. Offline

    Betagear

    Thanks a lot, but i've got a tiny error : The method add(BukkitRunnable) in the type List<BukkitRunnable> is not applicable for the arguments (BukkitTask).
    Where can it come from ?
     
  9. Offline

    mythbusterma

    @Betagear

    Because he was wrong. Don't use the return value of "runTaskLater," just store the Object itself.
     
  10. Offline

    Betagear

    So, to where do I have to move the parenthesis ?
     
  11. Offline

    timtower Moderator Moderator

    @Betagear Create the object, store it, then call runTaskLater on it.
     
  12. Offline

    Betagear

    I've stored it, but how can I refer to the thing that I have stored in my collection ?
    Should I store it first in a temp variable then run the RunTaskLater and then store it inside the Collection ?

    Edit : I did it, and, on paper, it should work but I can't remove it from the collection after the task is done.
    Code :
    Code:
    List<BukkitRunnable> collection = new ArrayList<>();
    final BukkitRunnable temprun = (new BukkitRunnable()
    {
        @Override
         public void run()
        {
        collection.remove(temprun); //The local variable temprun may not have been initialised
        }
    });
    temprun.runTaskLater(plugin, delay);
    collection.add(temprun);
     
    Last edited: Oct 1, 2015
  13. Offline

    timtower Moderator Moderator

    @Betagear What are you using this for, because you might be overcomplicating stuff.
     
  14. Offline

    Betagear

    @timtower For changing a block for a period of time then reverting it back to normal, and if then server reloads or stops, then the block is reverted to normal.
     
  15. Offline

    timtower Moderator Moderator

    @Betagear Why not make a class that stores the data and location, use that is a hashmap where the class is the key, and a long value ( time to revert it back)
    Then have 1 runnable handle them all.
    Can explain it better in 30-60 minutes.
     
  16. Offline

    Betagear

    @timtower I think i'll wait 30-60 minutes :)
     
    timtower likes this.
  17. Offline

    timtower Moderator Moderator

    @Betagear Got a decent keyboard now to work with :p
    Runnables can be used in many ways. It depends on what they need to do what method to use.
    For this I suggest 1 runnable that handles all blocks managed.

    Create a field HashMap<SomeStorageClass,long>
    The SomeStorageClass only does 1 thing: keep hold of a location and material+data.
    Then we get to the runnable:
    1. Loop through the hashmap
    2. Check if the currenttime>long
    3. If so: revert the block on location to material + data, remove set from hashmap (be aware of concurrentmodification)
    4. If not: continue with the next set
    Let that run every ... time.
     
  18. Offline

    Betagear

    Hum... The only problem is that, in my case, it's much more than just a block. I simplified it a lot.
    I only need to know how I can fix the error I get.
    Thanks anyways for this answer.
     
  19. Offline

    timtower Moderator Moderator

    @Betagear Then I suggest a hashmap<number that isn't in the map already, runnable>
     
  20. Offline

    Betagear

    I was talking of this error :

     
  21. Offline

    timtower Moderator Moderator

    Use the keyword this
     
  22. Offline

    Betagear

    I am getting a small error when using :
    Code:
            for (BukkitRunnable bukkitRunnable : runnablescoll) {
                bukkitRunnable.run();
            }
    The error kind in the console is : Is it up to date ?

    Any idea of what it could mean ?
     
  23. Offline

    timtower Moderator Moderator

    @Betagear Full error? As there could be many errors in there.
     
  24. Offline

    Betagear

    @timtower I don't think so. The error is said to be only on that line.
    One thing strange, is that if there is only one runnable in the List, it works but outputs the error, and if there's more it doesnt works.

    Edit : Maybe I should change the List to a Collection or a HashMap.
     
  25. Offline

    timtower Moderator Moderator

    @Betagear It can't be just that line, then it wouldn't be an error.
     
  26. Offline

    mine-care

    @Betagear is it an exception or a compilation warning?
     
    Last edited by a moderator: Oct 1, 2015
  27. Offline

    Betagear

    @mine-care It's an error in the server console when I /reload

    @timtower In the console, it's pointing a lot of things but the only one from my plugin is that line (and the one where I cast this thing)
     
  28. Offline

    timtower Moderator Moderator

  29. Offline

    Betagear

  30. Offline

    timtower Moderator Moderator

    @Betagear If the plugin that you are developing is called stomparena:
    Code:
    [21:54:39] [Server thread/ERROR]: Error occurred while disabling StompArena v2.5 (Is it up to date?)
    java.util.ConcurrentModificationException
            at java.util.ArrayList$Itr.checkForComodification(Unknown Source) ~[?:1.8.0_60]
            at java.util.ArrayList$Itr.next(Unknown Source) ~[?:1.8.0_60]
            at dev.bukkit.moddingear.PlayerListener.disactivate(PlayerListener.java:77) ~[?:?]
            at dev.bukkit.moddingear.Main.onDisable(Main.java:55) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:323) ~[spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:364) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:424) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:417) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at org.bukkit.plugin.SimplePluginManager.clearPlugins(SimplePluginManager.java:458) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:708) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_60]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_60]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot-1.8.8-R0.1-SNAPSHOT.jar:git-Spigot-0359393-9cd1111]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_60]
    That is your full error. You are getting concurrentmodificationexceptions.
     
Thread Status:
Not open for further replies.

Share This Page