1.16.5 While Loop Crashing The Server

Discussion in 'Spigot Plugin Development' started by Nikhil_11, Jul 9, 2021.

  1. So Recently i made a plugin where every minute a item drops i use thread.sleep() and while loop.
    When I ran it the items started dropping but i am unable to pick it up then i realised the server crashed in a few seconds it stopped. Is this due to the loop. Also this is my bat file for memory allocation

    @Echo off
    java -Xms1G -Xmx1024M -jar spigot.jar -nogui
    pause

    i also tried spigot scheduler but the plugin doesn't work.
    Thnx in advance:)
     
  2. Spigot runs on a single thread. thread.sleep() will stop the ENTIRE server. Can we see what you have done with the scheduler?
     
    • Agree Agree x 1
  3. Instead a while loop, use a BukkitRunnable.runTaskTimerAsynchronously(main, 60 * 20L (60 is seconds and 20 is time in ticks), 60 * 20L);

    Edit:
    new BukkitRunnable(

    @Override
    public void run() {
    //stuff
    }).runTaskTimerAsynchronously(main, 60 * 20L, 60 * 20L);
     
    • Like Like x 1
  4. Yea, Thread#sleep suspends the thread it's called in which in this case is your server's as the above commenter said. I guess you have the right idea, but use this instead:

    Code (Text):
            new BukkitRunnable()
            {
                @Override
                public void run()
                {
                    // Drop item here
                }
            }.runTaskTimer(PLUGIN_INSTANCE, 0, 20 * 60);
    It will accomplish the same thing.

    Why would you run this asynchronously?
     
    • Like Like x 1
  5. what is the difference between
    what is the differnce betwen normal and asynchronously
     
  6. randitem.class

    package mc.plugin;

    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitTask;

    public class randitem extends JavaPlugin {
    public randitem() {
    }

    public void onEnable() {
    BukkitTask task = (new task(this)).runTaskTimer(this, 0L, 100L);
    }

    public void onDisable() {
    }
    }



    task.class

    package mc.plugin;

    import org.bukkit.scheduler.BukkitRunnable;

    public class task extends BukkitRunnable {
    randitem plugins;

    public task(randitem plugin) {
    this.plugins = plugin;
    }

    public void run() {
    System.out.println("Item Dropped"); // Just a test i will add world.dropitemnaturally()
    }
    }

    does this work?
     
  7. also when i use the drop item at players current location in this scheduler it is dropping at one same place.
    i also used oncommand

    and can anyone say what is are definition of parameters of the runTaskTimer
     
  8. will this run without any lag with the according memory?
    java -Xms1G -Xmx1024M -jar spigot.jar -nogui
     
  9. I think, you can try
     
  10. Normal (synchronous) code is running on the server thread (main thread), and asynchronous is running on a separate thread. It's highly advised to avoid using most SpigotAPI functionality on a separate thread as the majority of the SpigotAPI (as far as I know) is not thread-safe. In fact, the SpigotAPI has measures in place to prevent this.

    Now don't get me wrong, there are plenty of things in plugin development that can (and probably should) be done on a separate thread. Database querying, chunk looping for material via chunk snapshot (thread-safe), and various other things that may freeze the main server thread are just a few examples. Use multithreading to your advantage when it's safe.
    https://docs.oracle.com/javase/tutorial/essential/concurrency/index.html

    It should, but it's going to run every 5 seconds rather than 1 minute.
     
  11. thnx but what is the difference between second parameter and third parameter in runtasktimer()
     
  12. 1 is start time and the other one is period