Solved Having problems deleting a created world

Discussion in 'Plugin Development' started by Ak1ra, Sep 30, 2023.

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

    Ak1ra

    I create a world using these 2 lines:

    Code:
    WorldCreator wc = new WorldCreator("minigame");
    World game = wc.createWorld();
    
    I send players into that world and I want the world to be deleted after so I tried this (for loop cuz before I used a while "minigame" isnt null loop and froze my server lmao)

    Code:
        public static void GameCancel(){
    //----------getting players out of there --------------------
            gamePlayers[0].teleport(server.getWorlds().get(0).getSpawnLocation());
            gamePlayers[1].teleport(server.getWorlds().get(0).getSpawnLocation());
            gamePlayers[0] = null;
            gamePlayers[1] = null;
    //----------getting players out of there --------------------
    
            Bukkit.unloadWorld(Bukkit.getWorld("minigame"), false); // unloading the world
    
            new BukkitRunnable(){
                @Override
                public void run() {
                    if (Bukkit.getWorld("minigame") == null) return;
                    File folder = Bukkit.getWorld("minigame").getWorldFolder(); //storing the minigame world folder
                    for (int i = 0; i < 50; i++) {
                        folder.delete(); // deleting the folder (never works so I try 50 times for good measure :D )
                        if (Bukkit.getWorld("minigame") == null) return;
                    }
    
                }
            }.runTaskAsynchronously(plugin);
        }
    
    I was never successful in deleting the world, anybody knows a better way? Ive looked over at world creator plugins but never understood the code well enough to implement it
     
  2. Offline

    Smeary_Subset

    This is how I do it. Please know I'm only giving you this code to show you the proper way to unload a world, not because I want to give you the answer. Also please note that, from my understanding, you cannot unload/delete the level world (this is the default world generated on enable).

    Code:
    // teleport players out of world to be unloaded
    for(Player player : minigamePlayers) {
       player.teleport(loc); // put in appropriate loc
    }
    
    // unload world
    boolean shouldBeSaved = false; // this means the world will not be saved
    Bukkit.getServer().unloadWorld(worldName, shouldBeSaved);
    
    // remove from world list
    Bukkit.getServer().getWorlds().remove(worldName);
    
    // delete world folder. The FileUtils package is fantastic and I recommend
    FileUtils.deleteDirectory(workingWorld); 
    Also, why are you running that task Asynch? There's no need to do that.
     
    Ak1ra likes this.
Thread Status:
Not open for further replies.

Share This Page