Bukkit creating World in another thread

Discussion in 'Plugin Development' started by Redstoner26, Jul 5, 2016.

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

    Redstoner26

    Hi guys,

    Just see my code, then i'll tell you what's the problem.

    Main.java
    Code:
    package fr.redstoner26.worldCreator;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.world.WorldLoadEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class Main extends JavaPlugin {
       
        private String worldName;
        private Player p;
       
        public void onEnable() {
           
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            this.p = p;
            if (cmd.getName().equals("worldCreate")) {
                p.sendMessage(ChatColor.DARK_AQUA + "Creating world...");
                String worldName = new String();
                if (args.length >= 1) {
                    worldName = args[0];
                } else {
                    worldName = "default";
                }
                BukkitRunnable br = new WorldCreatorThread(worldName);
                br.runTaskAsynchronously(this);
            }
            return true;
        }
       
        @EventHandler
        public void onWorldLoaded(WorldLoadEvent event) {
            if (this.worldName != null) {
                if (event.getWorld().getName().equals(this.worldName)) {
                    this.p.sendMessage(ChatColor.GREEN + "World created !");
                }
            }
        }
       
        public void onDisable() {
           
        }
       
    }
    
    WorldCreatorThread.java
    Code:
    package fr.redstoner26.worldCreator;
    
    import org.bukkit.Bukkit;
    import org.bukkit.WorldCreator;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class WorldCreatorThread extends BukkitRunnable {
    
        private String worldName;
       
        public WorldCreatorThread(String worldName) {
            this.worldName = worldName;
        }
       
        @Override
        public void run() {
            WorldCreator wc = new WorldCreator(this.worldName);
            wc.createWorld();
            Bukkit.createWorld(wc);
        }
    
    }
    
    The problem is that, i've got any errors... That's okay. But, the server starts to be slow, then, one minute after... It crashes, and everybody get disconnected.

    When the "Bukkit.getScheduler().cancelTask(this.getTaskId());" is at the end of the BukkitRunnable, the exception: "Exception ticking world" get thrown.

    When i try to do this without using threads. Same thing, the server starts to be slow, then crash.

    Note: If it's crash, it's because one server tick is higher than 60s.

    Thanks for any help.
     
  2. Offline

    ArsenArsen

    Short answer: No.
    Long answer: this.
     
    bwfcwalshy likes this.
  3. Offline

    Redstoner26

    First, thanks for the reply :) !

    Second, adapting theses files to my project seems to be impossible.
    The stackoverflow answers are hard to adapt for my project, in 1.10.

    For this : http://pastebin.com/K9CuVMS5
    Somes methods make reference to other java classes, that's make this thing impossible. I tried to search something with the package name, but nothing.

    The things i want to know, is why Bukkit crashes, and how to generate a world without making Bukkit crashing !
    If it's not async but the world is generated normally, without any crashes, it's okay !
     
  4. Offline

    ArsenArsen

    You are generating while there's load on the server. Add load: STARTUP or something like that, not sure I'm on a phone, into plugin.yml and generate in onEnable. Also if it's async it'll be broken. Also if it's a heavy custom generator it might be the issue.
     
  5. Offline

    Redstoner26

    But, i want to generate as much worlds that i want :(
     
Thread Status:
Not open for further replies.

Share This Page