Solved Saving/Resetting Chunks Fails?

Discussion in 'Spigot Plugin Development' started by fujiboy4, May 27, 2020.

Thread Status:
Not open for further replies.
  1. Hello! I am working on a game in which the user can set two points on the outer corners of the map. The entire region between those two points will be full of chunks that can be saved and reset. As a note before I get into this, I have been able to disable world saving, so the world is not auto saving on its own.

    My issue was when I copied this code from a previous project I had programmed that was for 1.12. The code worked fine for that game. I would save the default template for the map, and then when the game was over and majorly destroyed, it would reset back to normal. For some odd reason, no results are coming out of the same code for 1.15.

    My current code will go through all the chunks between the regions and then will let me know in the console of the unloading/loading of the chunks was successful. In both cases, the unloading of the chunk fails.

    Here is my code for reference.

    Code (Java):
    public void saveArena(boolean teleport) {
            if (teleport) {
                for (Player pls : Bukkit.getOnlinePlayers()) {
                    pls.teleport(new Location(Bukkit.getWorlds().get(1), 0, 129, 0));
                }
            }

            Chunk chunkCorner1 = getMapCorners().getLoc1().getChunk();
            Chunk chunkCorner2 = getMapCorners().getLoc2().getChunk();
            int minX = Math.min(chunkCorner1.getX(), chunkCorner2.getX());
            int maxX = Math.max(chunkCorner1.getX(), chunkCorner2.getX());
            int minZ = Math.min(chunkCorner1.getZ(), chunkCorner2.getZ());
            int maxZ = Math.max(chunkCorner1.getZ(), chunkCorner2.getZ());

            Bukkit.broadcastMessage("For saving");
            for (int x = minX; x <= maxX; x++) {
                for (int z = minZ; z <= maxZ; z++) {
                    Chunk chunk = chunkCorner1.getWorld().getChunkAt(x, z);
                    Bukkit.getConsoleSender().sendMessage("chunk: " + chunk.getX() + ", " + chunk.getZ() + " in " + chunk.getWorld().getName());
                    Bukkit.getConsoleSender().sendMessage(chunk.unload(true) ? ChatColor.GREEN + "Chunk Unloaded" : ChatColor.RED + "Failed To Unload Chunk");
                    Bukkit.getConsoleSender().sendMessage(chunk.load(false) ? ChatColor.GREEN + "Chunk Loaded" : ChatColor.RED + "Failed To Load Chunk");
                }
            }
            if (teleport) {
                for (Player pls : Bukkit.getOnlinePlayers()) {
                    if (pls != null) {
                        if (Lobby.getLocation() != null) {
                            pls.teleport(Lobby.getLocation());
                        } else if (getCenter() != null){
                            pls.teleport(getCenter());
                        } else {
                            pls.teleport(new Location(chunkCorner1.getWorld(), 0, 256, 0));
                        }
                    }
                }
            }
            this.saved = true;
        }

    Code (Java):
    if (teleport) {
                for (Player pls : Bukkit.getOnlinePlayers()) {
                    if (pls != null) {
                        pls.teleport(new Location(Bukkit.getWorlds().get(1), 0, 129, 0));
                    }
                }
            }
            Chunk chunkCorner1 = getMapCorners().getLoc1().getChunk();
            Chunk chunkCorner2 = getMapCorners().getLoc2().getChunk();
            int minX = Math.min(chunkCorner1.getX(), chunkCorner2.getX());
            int maxX = Math.max(chunkCorner1.getX(), chunkCorner2.getX());
            int minZ = Math.min(chunkCorner1.getZ(), chunkCorner2.getZ());
            int maxZ = Math.max(chunkCorner1.getZ(), chunkCorner2.getZ());

            Bukkit.broadcastMessage("For resetting");
            for (int x = minX; x <= maxX; x++) {
                for (int z = minZ; z <= maxZ; z++) {
                    Chunk chunk = chunkCorner1.getWorld().getChunkAt(x, z);
                    Bukkit.getConsoleSender().sendMessage("chunk: " + chunk.getX() + ", " + chunk.getZ() + " in " + chunk.getWorld().getName());
                    Bukkit.getConsoleSender().sendMessage(chunk.unload(false) ? ChatColor.GREEN + "Chunk Unloaded" : ChatColor.RED + "Failed To Unload Chunk");
                    Bukkit.getConsoleSender().sendMessage(chunk.load(true) ? ChatColor.GREEN + "Chunk Loaded" : ChatColor.RED + "Failed To Load Chunk");
                }
            }
            if (teleport) {
                for (Player pls : Bukkit.getOnlinePlayers()) {
                    if (pls != null) {
                        if (Lobby.getLocation() != null) {
                            pls.teleport(Lobby.getLocation());
                        } else if (getCenter() != null) {
                                pls.teleport(getCenter());
                        } else {
                            pls.teleport(new Location(chunkCorner1.getWorld(), 0, 256, 0));
                        }
                    }
                }
            }
        }

    As you can see, these methods are both very similar. The only difference is with the boolean arguments in the methods for loading/unloading a chunk. I looked up what the booleans actually meant in the documentation and I found this:

    upload_2020-5-27_15-31-3.png

    upload_2020-5-27_15-31-31.png

    So my logic essentially confirms that in order to save a chunk list, I would need to unload each one and save it, so unload(true), and then load it without generation, so load(false). And the opposite for resetting the arena. However, in both cases, my console output shows the following:

    Code (Text):
    chunk: -21, 0 in world
    [15:22:58] [Server thread/INFO]: Failed To Unload Chunk
    [15:22:58] [Server thread/INFO]: Chunk Loaded
    [15:22:58] [Server thread/INFO]: chunk: -21, 1 in world
    [15:22:58] [Server thread/INFO]: Failed To Unload Chunk
    [15:22:58] [Server thread/INFO]: Chunk Loaded
    [15:22:58] [Server thread/INFO]: chunk: -21, 2 in world
    [15:22:58] [Server thread/INFO]: Failed To Unload Chunk
    [15:22:58] [Server thread/INFO]: Chunk Loaded
    ...

    I'm not entirely sure where to go from here as I don't entirely know why it's failing. If someone could shed some light on this, that would be very much appreciated. Thank you!
     
Thread Status:
Not open for further replies.