Is there a way that i could run this method asynchronously?

Discussion in 'Spigot Plugin Development' started by PixliePlaysYT, Jan 7, 2020.

Thread Status:
Not open for further replies.
  1. Hey! I've created a 8-neighbour flood-fill method. But it uses alot fo performance. So i would like to ask you guys if you could help me running this asynchronously? Thank you!

    Code removed.
     
    #1 PixliePlaysYT, Jan 7, 2020
    Last edited: Jan 11, 2020
  2. MrGeneralQ

    MrGeneralQ Previously qintinator
    Supporter

    You can use a BukkitRunnable for that. You can refer to this article:
    https://www.spigotmc.org/threads/running-tasks-asynchronously.155658/
     
  3. Sorry i'm new to this, but how exactly do i implement this? Do i do:

    Code (Java):
            new BukkitRunnable() {
                @Override
                public void run() {
                    floodSearch(x, z, toclaim, alreadychecked);
                }.runTaskAsynchronously(instance);
            }
     
  4. You can shorten it to one line
    Code (Java):
    Bukkit.getScheduler().runTaskAsynchronously(() -> floodSearch(x, z, toclaim, alreadychecked));
     
  5. Whoa, be careful there:
    From the Bukkit API-reference:
    There are a lot of methods that use Bukkit API, thou I am pretty sure that you can a thread-safe handle to a chunk.
    I believe also that Bukkit IO-Methods (config) are generally Thread-save (tho you should definitely take a closer look)
     
    • Agree Agree x 1
  6. Oooorr you could use Paper to access chunks asynchronously ;)

    PaperLib has a nice bridge between Spigot and Paper, sync on Spigot and async on Paper.
     
  7. Waaait a second, why am I not using paper?
     
  8. So would you recommend me using Paper as the library for my plugin?
     
  9. FrostedSnowman

    Resource Staff

    instead of revising the collection twice, just visit it once. don’t use

    Code (Java):
    if (toClaim.contains(x)) {
        return;
    }
    toClaim.add(x);
    //code
     
    Code (Java):
    if (toClaim.add(x)) {
        //code
    }
     
    • Useful Useful x 1
  10. I recommend PaperLib. This means your plugin will work on both Spigot and Paper, using PaperLib#getChunkAtAsync
     
  11. Code (Text):
    [23:09:18 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'f' in plugin PixliesFactionSystem v1.0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[patched_1.15.1.jar:git-Paper-23]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159) ~[patched_1.15.1.jar:git-Paper-23]
            at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchCommand(CraftServer.java:740) ~[patched_1.15.1.jar:git-Paper-23]
            at net.minecraft.server.v1_15_R1.PlayerConnection.handleCommand(PlayerConnection.java:1820) ~[patched_1.15.1.jar:git-Paper-23]
            at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1628) ~[patched_1.15.1.jar:git-Paper-23]
            at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[patched_1.15.1.jar:git-Paper-23]
            at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:5) ~[patched_1.15.1.jar:git-Paper-23]
            at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:23) ~[patched_1.15.1.jar:git-Paper-23]
            at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) ~[patched_1.15.1.jar:git-Paper-23]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136) ~[patched_1.15.1.jar:git-Paper-23]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[patched_1.15.1.jar:git-Paper-23]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109) ~[patched_1.15.1.jar:git-Paper-23]
            at net.minecraft.server.v1_15_R1.MinecraftServer.aZ(MinecraftServer.java:1037) ~[patched_1.15.1.jar:git-Paper-23]
            at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:1030) ~[patched_1.15.1.jar:git-Paper-23]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119) ~[patched_1.15.1.jar:git-Paper-23]
            at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:1014) ~[patched_1.15.1.jar:git-Paper-23]
            at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:937) ~[patched_1.15.1.jar:git-Paper-23]
            at java.lang.Thread.run(Thread.java:834) [?:?]
    Caused by: java.lang.NoClassDefFoundError: io/papermc/lib/PaperLib
            at me.mickmmars.factions.factions.FactionManager.floodSearch(FactionManager.java:889) ~[?:?]
            at me.mickmmars.factions.commands.FactionCommand.onCommand(FactionCommand.java:617) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[patched_1.15.1.jar:git-Paper-23]
            ... 17 more
    Caused by: java.lang.ClassNotFoundException: io.papermc.lib.PaperLib
            at java.net.URLClassLoader.findClass(URLClassLoader.java:471) ~[?:?]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:139) ~[patched_1.15.1.jar:git-Paper-23]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:85) ~[patched_1.15.1.jar:git-Paper-23]
            at java.lang.ClassLoader.loadClass(ClassLoader.java:588) ~[?:?]
            at java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[?:?]
            at me.mickmmars.factions.factions.FactionManager.floodSearch(FactionManager.java:889) ~[?:?]
            at me.mickmmars.factions.commands.FactionCommand.onCommand(FactionCommand.java:617) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[patched_1.15.1.jar:git-Paper-23]
            ... 17 more
     
    i keep getting this error when i do /f claim fill

    My method:
    Code (Java):
       // THIS IS A 8-Neighbour FLOODFILL ALGORYTHM THAT SEARCHES FOR WHAT CHUNKS TO CLAIM.
        public void floodSearch(int x, int z, Set<Chunk> toClaim, Set<Chunk> alreadyChecked) throws ExecutionException, InterruptedException {

                    // CHECK IF WE ALREADY CHECKED
                    if (alreadyChecked.contains(PaperLib.getChunkAtAsync(Bukkit.getWorld(Config.FACTION_WORLD.getData().toString()), x, z).get())) return;

                    // MAX REACHED?
                    if (alreadyChecked.size() >= (int) Config.MAX_FILL_SIZE.getData()) return;

                    // ADD TO ALREADY CHECKED SET
                    alreadyChecked.add(PaperLib.getChunkAtAsync(Bukkit.getWorld(Config.FACTION_WORLD.getData().toString()), x, z).get());

                    // ALREADY CONTAINS CHUNK?
                    if (toClaim.contains(PaperLib.getChunkAtAsync(Bukkit.getWorld(Config.FACTION_WORLD.getData().toString()), x, z).get()))
                        return;

                    // ALREADY CLAIMED?
                    if (!(instance.getChunkManager().getFactionDataByChunk(PaperLib.getChunkAtAsync(Bukkit.getWorld(Config.FACTION_WORLD.getData().toString()), x, z).get()) == null))
                        return;

                    toClaim.add(PaperLib.getChunkAtAsync(Bukkit.getWorld(Config.FACTION_WORLD.getData().toString()), x, z).get());

                    // RECOURSE
                    floodSearch(x, z + 1, toClaim, alreadyChecked); // north
                    floodSearch(x, z - 1, toClaim, alreadyChecked); // south
                    floodSearch(x - 1, z, toClaim, alreadyChecked); // west
                    floodSearch(x + 1, z, toClaim, alreadyChecked); // east
                    floodSearch(x - 1, z + 1, toClaim, alreadyChecked); // north-west
                    floodSearch(x + 1, z - 1, toClaim, alreadyChecked); // south-east
                    floodSearch(x - 1, z + 1, toClaim, alreadyChecked); // north-west
                    floodSearch(x + 1, z - 1, toClaim, alreadyChecked); // south-east
        }
     
  12. Because you’re not shading it, and dear god don’t use #get on futures, it destroys the entire point of them as get blocks. Use thenAccept.
     
  13. How can i shade that?
     
  14. You can use .get() if you’re running many future in parallel and are waiting to get all the data and sync it up. But if you’re only using one, there is no point in using a future.
     
  15. Can you help me with shading Paperlib? It always gives this error:

    Code (Text):
    The project main artifact does not exist. This could have the following
    reasons:
    - You have invoked the goal directly from the command line. This is not
      supported. Please add the goal to the default lifecycle via an
      <execution> element in your POM and use "mvn package" to have it run.
    - You have bound the goal to a lifecycle phase before "package". Please
      remove this binding from your POM such that the goal will be run in
      the proper phase.
    - You removed the configuration of the maven-jar-plugin that produces the main artifact.
     
  16. In plugins under build

    Code (Text):
            <plugins>
                <plugin>
                    <artifactId>maven-shade-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
    You can also include / exclude specifics but i normally just abuse the <provided> tag on depends
     
  17. Is there a way to not use Maven, but use the normal Java compiler of IntellIJ with Paperlib? Is there a Paperlib.jar?
     
  18. yeah maven/gradle can suck to learn but it's worth it.
     
  19. Because my whole project has dependencies that are for example not online, but my own plugins. So how can i use local dependencies in Maven?
     
Thread Status:
Not open for further replies.