Loop through blocks with delay

Discussion in 'Plugin Development' started by Josh014, Mar 15, 2016.

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

    Josh014

    Hello!

    Been searching all day on how to loop through an area & fill it with air with a delay. The part of looping though an area isn't that hard but the part with the delay starts to get annoying. Been trying to use a Bukkit Runnable and a scheduleSyncDelayedTask, but doesn't really work... I get a huge error in my console.


    Code (Yes I know the part of placing a block needs to be in the scheduler but I can't have a final in a for loop, that's why I tried outside it. It is in a method):
    Code:
    int cx = loc.getBlockX();
                    int cy = loc.getBlockY();
                    int cz = loc.getBlockZ();
                    for (int x = cx - r; x <= cx + r; x++) {
                        for (int z = cz - r; z <= cz + r; z++) {
                            for (int y = (sphere ? cy - r : cy); y < (sphere ? cy + r : cy + h); y++) {
                                double dist = (cx - x) * (cx - x) + (cz - z) * (cz - z) + (sphere ? (cy - y) * (cy - y) : 0);
                                if (dist < r * r && !(hollow && dist < (r - 1) * (r - 1))) {
                                  
                                    main.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
                                        public void run() {
                                          
                                        }
                                    },5);
                                  
                                    Location l = new Location(loc.getWorld(), x, y + plus_y, z);
                                    l.getBlock().setType(Material.AIR);
                                }
                            }
                        }
                    }
    Error:
    Error (open)

    org.bukkit.command.CommandException: Unhandled exception executing command 'gen'
    in plugin BuildingGenerator v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got.jar:git-Spigot-6f291ea-55a8535]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigot.jar:git-Spigot-6f291ea-55a8535]
    at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServe
    r.java:645) ~[spigot.jar:git-Spigot-6f291ea-55a8535]
    at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:1342) [spigot.jar:git-Spigot-6f291ea-55a8535]
    at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java
    :1177) [spigot.jar:git-Spigot-6f291ea-55a8535]
    at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :45) [spigot.jar:git-Spigot-6f291ea-55a8535]
    at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :1) [spigot.jar:git-Spigot-6f291ea-55a8535]
    at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot.jar:git-Spigot-6f291ea-55a8535]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_65]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_65]
    at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot.jar
    :git-Spigot-6f291ea-55a8535]
    at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:7
    21) [spigot.jar:git-Spigot-6f291ea-55a8535]
    at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:4
    00) [spigot.jar:git-Spigot-6f291ea-55a8535]
    at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:6
    60) [spigot.jar:git-Spigot-6f291ea-55a8535]
    at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java
    :559) [spigot.jar:git-Spigot-6f291ea-55a8535]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_65]
    Caused by: java.lang.NullPointerException
    at me.Josh.BuildingGenerator.Generate.Generate_Castle.advanced(Generate_
    Castle.java:61) ~[?:?]
    at me.Josh.BuildingGenerator.Commands.Generate.onCommand(Generate.java:2
    9) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got.jar:git-Spigot-6f291ea-55a8535]
    ... 15 more


    If anyone knows how to solve this problem I'd be amazing :D

    Thanks in advance!

    Edit:
    I want a delayed task to reduce server lagg.
     
  2. Offline

    mythbusterma

    @Josh014

    You really shouldn't be scheduling that many tasks, that's a ton of overhead that's going to give you performance far worse than just doing it outright. Instead, you should make a single task that gets run repeatedly until its complete that maintains its state between runs, and checks to see if it has taken too much time.

    Also, the word "lag" is spelled with one "g," always.
     
  3. Offline

    Josh014

  4. Offline

    Lolmewn

    I'd propose to put everything in a Queue or a HashMap. Then, schedule a task that runs every so many ticks and process an x amount of items from the collection.
     
  5. Offline

    mythbusterma

    @Lolmewn

    Why waste the memory?

    Code:
    Generator : BukkitRunnable {
        x := 0
        y := 0
        z := 0
        count := 0
        r := 0
        location
        LIMIT = 100
    
        Generator(Location loc, int r) {
            r := r
            x := loc.x - r
            y := loc.y - r
            z := loc.z - r
            location := loc
         }
          
         override run() {
              starttime := systemtime
              for (x to location.x +r) {  // note we do not reset the counters here
                  for (y to location.y + r) {
                      for (z  to location.z + r) {
                           // do stuff
                           count++
                           if count % 1000 == 0:
                               currenttime := systemtime
                               if currenttime - starttime > limit:
                                   return
                        }
                    }
                }
            }
    }
     
  6. Offline

    mcdorli

    I would consider adding a little message about this code being a pseudo-code, because I've seen many cases, where people didn't know what that is, and they basically copy-pasted the whole code, especially now, when 1.9 spigot came out and the forum was flooded with newbie coders.
     
  7. Offline

    mythbusterma

    @mcdorli

    I mean, if someone doesn't realise that isn't Java....
     
  8. Offline

    Lolmewn

    @mythbusterma How does that continue where it left off though? I can see this work, but only if x, y and z have been initialized outside the loop.
     
  9. Offline

    mythbusterma

    @Lolmewn

    They are, in the constructor.
     
  10. Offline

    Lolmewn

    @mythbusterma Yeah you're right lol, wasn't paying attention.
     
Thread Status:
Not open for further replies.

Share This Page