Chunks Unload Even Against Event

Discussion in 'Spigot Plugin Development' started by Bladian, Jun 21, 2015.

  1. I currently have this code.

    Code (Text):
    public void loadLocations() {

            Bukkit.broadcastMessage(scatter + locations.size() + " chunks are being loaded, approximately " + (5*locations.size()) + " seconds");

            final int size = locations.size();


            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Core.getInstance(), new Runnable() {

                int i = 0;

                @Override
                public void run() {

                    if (i < size) {

                        Location location = locations.get(i);
                        Chunk chunk = location.getChunk();

                        if (!chunk.isLoaded()) {
                            chunk.load();

                        }
                    }
                    else {
                        Bukkit.broadcastMessage(scatter + "Chunks have been loaded, teleporting players");
                    }

                }
            }, 0L, 100L);


        }
    Code (Text):
    package me.uhc.scatter;

    import org.bukkit.Bukkit;
    import org.bukkit.Chunk;
    import org.bukkit.Location;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.world.ChunkUnloadEvent;

    import java.util.List;

    /**
    * Created by maxmigliorini on 21/06/15.
    */
    public class EventScatter implements Listener {


        List<Chunk> chunks  = CommandScatter.getInstance().getMethodScatter().chunks;

        @EventHandler
        public void onUnload(ChunkUnloadEvent e) {

            Bukkit.broadcastMessage("It works!");


            if(chunks.contains(e.getChunk())) {
                Bukkit.broadcastMessage("§eIt works!");
                e.setCancelled(true);
            }


        }
    }
     
    I want all the chunks that I have selected to be constantly loaded. The only problem is that it doesn't work. I keep falling down an endless black hole, basically screwing up the method. Any help?
     
  2. I dont see where you are storing the chunks in that List... Its just an empty list all the time, so there are no chunks it wants to keep loaded.
     
  3. It's in another method.

    chunks.add(location.getChunk());

    Code (Text):
    public void chooseLocations(int x, int z, World world) {

            Random random = new Random();

            for (Player all : Bukkit.getOnlinePlayers()) {

                int pX = random.nextInt(x - 0 + 1) + 1;
                int pZ = random.nextInt(z - 0 + 1) + 1;
                int pY = world.getHighestBlockYAt(pX, pZ);

                Location location = new Location(world, pX, pY, pZ);

                locations.add(location);
                chunks.add(location.getChunk());
            }


                Bukkit.broadcastMessage(scatter + "Chunks have been chosen");

        }
     
  4. if(chunks.contains(e.getChunk())) {
    Bukkit.broadcastMessage("§eIt works!");
    e.setCancelled(true);
    }

    Does the broadcastMessage get executed?
     
  5. It does but very rarely. So maybe that isn't the problem.

    But somehow whenever I teleport there it still does that weird thing where it teleports me down and gives a total black screen. It's like a gradual descent to the void.
     
  6. This also screws up the getBlockAt method since it sometimes teleports me inside stone.
     
  7. sothatsit

    Patron

    This could just be server to client lag.
     
  8. Is there any way to fix it?
     
  9. sothatsit

    Patron

    get a better internet connection on the server or your home computer.
     
  10. Okay, also can you explain the getHighestBlockAZ(x,z) teleporting me into the stone.
     
  11. In addition why is this happening? The server is on my computer and has 1ms difference. Are you sure it's not got something to do with the chunk?
     
  12. This is the worst solution I've ever heard. The solution should never ever be "getter a better internet connection".

    On topic:
    Maybe the pointers change, you could try to save the ChunkX/Z coordinate itself instead. That might work. :3
     
  13. How would you propose I save the chunk X/Z coordinate? Using a list? Saving the location?

    I'm not sure how that would really solve it. How long does it take for a chunk to full load? Maybe I can constantly keep it active using the event.
     
  14. ArrayList<String>
    add(chunk.getX()+"/"+chunk.getZ());
    contains()

    I don't know if it would matter, but I feel like it's a better approach, since saving a pointer doesn't always end up with nice results.


    What I also want to mention is that your loadLocations doesn't actually loop over the locations, it just loads 1 location/chunk.
    That might be the problem too ^_^
     
  15. To store the coords you could use a use a hashmap but i wouldn't see a problem with storing the location then getting the chunk from the location.
     
  16. I believe I fixed it:

    How long does it take for a chunk to load? Maybe I can set the timing to the correct amount.
     
  17. Also having my server on 512 MB may have also been a problem...
     
  18. sothatsit

    Patron

    He asked how he would fix a bad connection to the server. That is how you would fix it simply.
     
  19. But it's not bad connection since the server is on my computer, and the connections is 1ms.
     
  20. sothatsit

    Patron

    You never mentioned that, so how was I to know?