Remove world folder

Discussion in 'Plugin Development' started by bennie3211, Aug 4, 2019.

  1. Offline

    bennie3211

    Hey,

    Maybe someone can help me, I'm trying to remove the main 'world' folder from the server when the server closes. But for some reason (which I really can't find the solution for) gives me errors. This is what I'm using right now:

    Code:
    package me.gonnakillyou2.world;
    
    import java.io.File;
    
    import org.bukkit.World;
    import org.bukkit.entity.Player;
    
    import me.gonnakillyou2.HungerGames;
    
    public class WorldManager
    {
        private HungerGames plugin;
    
        public WorldManager(HungerGames p)
        {
            this.plugin = p;
        }
    
        public void removeWorld(String name)
        {
            World w = getPlugin().getServer().getWorld(name);
    
            // If world is not loaded, stop function
            if (w == null)
            {
                getPlugin().getLogging()
                        .logWarning("&cCould not unload world &4" + name.toUpperCase() + " &cdue to not loaded!");
                return;
            }
    
            // Kicking players from the world, if last alive gets kicked congratulate
            // him/her
            for (Player p : w.getPlayers())
            {
                if (w.getPlayers().size() == 1)
                {
                    p.kickPlayer("You have won the " + getPlugin().getGameID() + "st hunger game!");
                } else
                {
                    p.kickPlayer("You have all been rescued by district 13! [Game restart]");
                }
            }
    
            // Unload world
            if (!getPlugin().getServer().unloadWorld(w, false))
            {
                getPlugin().getLogging().logWarning("&cCould not unload world &4" + name.toUpperCase() + "&c!");
            }
    
            // Delete world folder
            if (!deleteDirectory(w.getWorldFolder()))
            {
                getPlugin().getLogging()
                        .logWarning("&cCould not delete world directory for world &4" + name.toUpperCase() + "&c!");
            }
        }
    
        public boolean deleteDirectory(File path)
        {
            if (path.exists())
            {
                File files[] = path.listFiles();
    
                for (int i = 0; i < files.length; i++)
                {
                    if (files[i].isDirectory())
                    {
                        deleteDirectory(files[i]);
                    } else
                    {
                        files[i].delete();
                    }
                }
            }
    
            return (path.delete());
        }
    
        private HungerGames getPlugin()
        {
            return this.plugin;
        }
    }
    and in the onDisable() function I call the following:

    Code:
    public void onDisable()
        {
            getWorldManager().removeWorld("world");
        }
    Which gives me the following error when using the 'stop' command in the command line:

    Error (open)

    >stop
    [23:50:04] [Server thread/INFO]: Stopping the server
    [23:50:05] [Server thread/INFO]: Stopping server
    [23:50:05] [Server thread/INFO]: [HungerGames] Disabling HungerGames v0.1
    [23:50:05] [Server thread/WARN]: ºcCould not unload world º4WORLDºc!
    [23:50:05] [Server thread/WARN]: ºcCould not delete world directory for world º4WORLDºc!
    [23:50:05] [Server thread/INFO]: Saving players
    [23:50:05] [Server thread/INFO]: Saving worlds
    [23:50:05] [Server thread/INFO]: Saving chunks for level 'world'/minecraft:eek:verworld
    [23:50:05] [Server thread/WARN]: Failed to check session lock for world located at .\world, aborting. Stop the server and delete the session.lock in this world to prevent further issues.
    [23:50:05] [Server thread/INFO]: Saving chunks for level 'world_nether'/minecraft:the_nether
    [23:50:06] [Server thread/INFO]: ThreadedAnvilChunkStorage (DIM-1): All chunks are saved
    [23:50:06] [Server thread/INFO]: Saving chunks for level 'world_the_end'/minecraft:the_end
    [23:50:06] [Server thread/INFO]: ThreadedAnvilChunkStorage (DIM1): All chunks are saved
    [23:50:06] [Server thread/ERROR]: Couldn't save chunk; already in use by another instance of Minecraft?
    net.minecraft.server.v1_14_R1.ExceptionWorldConflict: Failed to check session lock for world located at .\world, aborting. Stop the server and delete the session.lock in this world to prevent further issues.
    at net.minecraft.server.v1_14_R1.WorldNBTStorage.checkSession(WorldNBTStorage.java:115) ~[server.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.WorldServer.checkSession(WorldServer.java:1390) ~[server.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.PlayerChunkMap.saveChunk(PlayerChunkMap.java:674) ~[server.jar:git-Spigot-9de398a-9c887d4]
    at java.util.stream.ReferencePipeline$2$1.accept(Unknown Source) [?:1.8.0_192]
    at java.util.stream.ReferencePipeline$2$1.accept(Unknown Source) [?:1.8.0_192]
    at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source) [?:1.8.0_192]
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(Unknown Source) [?:1.8.0_192]
    at java.util.stream.AbstractPipeline.copyInto(Unknown Source) [?:1.8.0_192]
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source) [?:1.8.0_192]
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(Unknown Source) [?:1.8.0_192]
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source) [?:1.8.0_192]
    at java.util.stream.AbstractPipeline.evaluate(Unknown Source) [?:1.8.0_192]
    at java.util.stream.ReferencePipeline.forEach(Unknown Source) [?:1.8.0_192]
    at net.minecraft.server.v1_14_R1.PlayerChunkMap.save(PlayerChunkMap.java:312) [server.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.ChunkProviderServer.save(ChunkProviderServer.java:297) [server.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.ChunkProviderServer.close(ChunkProviderServer.java:302) [server.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.World.close(World.java:1032) [server.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.MinecraftServer.stop(MinecraftServer.java:724) [server.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.DedicatedServer.stop(DedicatedServer.java:686) [server.jar:git-Spigot-9de398a-9c887d4]
    at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:857) [server.jar:git-Spigot-9de398a-9c887d4]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_192]


    Can anyone tell me how to get rid of this error?
     
  2. Offline

    Kars

    Does the deletion go through, though? By the looks of it, it does.
     
  3. Offline

    bennie3211

    @Kars it removes everything except some region files, but when I restart the server and close it again, the error gets spammed multiple times (like 20x) and the next time more and more, so it needs to be solved to work
     
  4. Offline

    Kars

    @bennie3211 yea so, by the looks of it, the deletion happens before some other stuff. That other stuff then throws the error(s).

    What you could do is schedule a task 5 seconds in the future in the onDisable. You might have to use the native Java scheduler to avoid getting more weird exceptions.
     
  5. Offline

    bennie3211

    Hmhm I'm going to try that later this week, but you mean first unload the world, then start a (java)scheduler to remove the folders with I/O functions of java a couple of seconds later in the onDisable() function?
     
  6. Offline

    KarimAKL

    @bennie3211 Out of curiosity, couldn't you just unload the word and then delete the folder using File#delete()?
     
  7. Offline

    Kars

    @bennie3211 that is exactly what i mean yes.

    @KarimAKL whatever works.

    Make sure you use a native Java scheduler. Schedule the task and close the scheduler right away.
     
  8. Offline

    Twisted_Panda

    Last time I checked you can't delete the main world
     

Share This Page