Delete and create a new world on command

Discussion in 'Plugin Development' started by MiBB3L, Jun 7, 2020.

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

    MiBB3L

    I want to delete a world and create a new world with the same name. Before that all players get teleported to a location on a other world. This is not working any ideas?
    Code:
    public class ResetCommand implements CommandExecutor{
    
        private main plugin;
      
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player) sender;
          
          
    
                FileConfiguration config = main.getPlugin().getConfig();
                World worldtp = Bukkit.getWorld(config.getString("Spawn.World"));
                double x = config.getDouble("Spawn.X");
                double y = config.getDouble("Spawn.Y");
                double z = config.getDouble("Spawn.Z");
                float yaw = (float) config.getDouble("Spawn.Yaw");
                float pitch = (float) config.getDouble("Spawn.Pitch");
                Location location = new Location(worldtp, x, y, z, yaw, pitch);
                World world = p.getWorld();
                for(Player player : Bukkit.getOnlinePlayers()){
                    player.teleport(location);
                }
              
                String worldName = world.getName();
               
                if (!Bukkit.unloadWorld(world, false)) return false;
               
                File worldFolder = new File(plugin.getDataFolder().getParentFile().getParentFile(), worldName);
                worldFolder.delete();
               
                Bukkit.createWorld(new WorldCreator(worldName));
          
            return false;
        }
      
      
    }
     
  2. Offline

    KarimAKL

    @MiBB3L Try debugging, where does it stop?
     
  3. Offline

    MiBB3L

    It stops after the players got teleported.
     
  4. Offline

    KarimAKL

    @MiBB3L Then the Bukkit#unloadWorld(...) call is returning false.
     
  5. Offline

    MiBB3L

    But why? I use the world, where the player stands. It can´t returning false

    Okay now I done it like that
    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player) sender;
           
            System.out.println("1");
    
                FileConfiguration config = main.getPlugin().getConfig();
                World worldtp = Bukkit.getWorld(config.getString("Spawn.World"));
                double x = config.getDouble("Spawn.X");
                double y = config.getDouble("Spawn.Y");
                double z = config.getDouble("Spawn.Z");
                float yaw = (float) config.getDouble("Spawn.Yaw");
                float pitch = (float) config.getDouble("Spawn.Pitch");
                Location location = new Location(worldtp, x, y, z, yaw, pitch);
                World world = p.getWorld();
                for(Player player : Bukkit.getOnlinePlayers()){
                    player.teleport(location);
                    System.out.println("2");
                }
               
                String worldName = world.getName();
                
                if (!Bukkit.unloadWorld(world, false)) {
                    System.out.println("2,5");
                    return false; 
                }
                System.out.println("3");
               
                File worldFolder = new File(plugin.getDataFolder().getParentFile().getParentFile(), worldName);
                System.out.println("4");
                worldFolder.delete();
                System.out.println("5");
                
                Bukkit.createWorld(new WorldCreator(worldName));
                System.out.println("6");
           
            return false;
        }
       After "3" I get this error:
       
    }

    Code:
    null
    org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin MiBB3L_Challenges v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_241]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_241]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    Caused by: java.lang.NullPointerException
        at me.MiBB3L.challenges.ResetCommand.onCommand(ResetCommand.java:51) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2020
  6. Offline

    KarimAKL

    @MiBB3L What is line 51 in your ResetCommand class?
    EDIT: Nevermind, 'plugin' is probably null, use your 'main' variable from above.
     
  7. Offline

    MiBB3L

    I use my "main" variable.
    Code:
    private main plugin;
     
  8. Offline

    caderapee

    @MiBB3L
    Did you initalize your main variable ?
    What is the line 51 ?
     
  9. Offline

    MiBB3L

    This is in Line 51
    Code:
     File worldFolder = new File(plugin.getDataFolder().getParentFile().getParentFile(), worldName); 
    I think I initliazed it, but could you look up in my code I´m not 100% sure.
     
  10. Offline

    caderapee

    @MiBB3L show the full class so, but it look like u haven't initialized it.
    Create a constructor and pass an instance of your main class into it, then u can set ur varaible.

    edit: nvm, i saw ur full class on the first post. Yes u dun initialize it
     
  11. Offline

    KarimAKL

    @MiBB3L I told you in this thread to fix that. Your problem is that you're using 'plugin' instead of 'main#getPlugin()'
     
  12. Offline

    MiBB3L

    Okay now I don´t get an error anymore, but the world gets not deleted. It gets unloaded and when I do a reload it gets back loaded. But a new world gets created too. (I use another worldname for the new worlds now)
    Code:
    public class ResetCommand implements CommandExecutor{
        public ResetCommand(main plugin) {
            this.plugin = plugin;
        }
        private main plugin;
        Random rand = new Random();
    
    
        int randomNum = rand.nextInt((100 - 2) + 1) + 2;
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player) sender;
           
    
                FileConfiguration config = main.getPlugin().getConfig();
                World worldtp = Bukkit.getWorld(config.getString("Spawn.World"));
                double x = config.getDouble("Spawn.X");
                double y = config.getDouble("Spawn.Y");
                double z = config.getDouble("Spawn.Z");
                float yaw = (float) config.getDouble("Spawn.Yaw");
                float pitch = (float) config.getDouble("Spawn.Pitch");
                Location location = new Location(worldtp, x, y, z, yaw, pitch);
                World world = p.getWorld();
                for(Player player : Bukkit.getOnlinePlayers()){
                    player.teleport(location);
                }
               
                String worldName = world.getName();
                
                if (!Bukkit.unloadWorld(world, false)) {
                    return false; 
                }
               
                File worldFolder = new File(plugin.getDataFolder().getParentFile().getParentFile(), worldName);
                worldFolder.delete();
                String newWorldName = worldName + randomNum;
                
                Bukkit.createWorld(new WorldCreator(newWorldName));
           
            return false;
        }
       
       
    }
    @KarimAKL I didn´t understand what you mean, sorry.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2020
  13. Offline

    KarimAKL

    @MiBB3L You still haven't fixed your main class name.

    Is the world folder still there after you run the command?
     
Thread Status:
Not open for further replies.

Share This Page