Problem/Questions

Discussion in 'Plugin Development' started by pkt, Jul 3, 2013.

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

    pkt

    I will try to explain what I'm trying to do as best as I can...
    So I'm trying to make something spawn in a random chunk out of all the loaded chunks in the world, but before I spawn the object, I want to get all the blocks in the spot I want to spawn it, so that I can put them back after 5 minutes or so.
    I have fiddled with my code for about 5+ hours.
    My Questions:
    - What causes a chunk to stay loaded and when will bukkit unload it?
    - When I'm randomly picking a chunk, Why does it seem to only pick 1-2 chunks out of all my times testing
    - does the world.loadChunk() method keep a chunk loaded forever until told to unload?
    more questions in code

    Code:
    public void randomSpawn() {
            new BukkitRunnable() {
                public void run() {
                    Random random = new Random();
                    List<Chunk> chunks = new ArrayList<>();
                    World world = Bukkit.getWorld("world");
     
                    for (Chunk c : world.getLoadedChunks())
                        chunks.add(c);
     
                    Chunk chunk = chunks.get(random.nextInt(chunks.size()));
                    Location l= new Location(world, pOrN(chunk.getX()), world.getHighestBlockYAt(chunk.getX(), chunk.getZ()), pOrN(chunk.getZ()));
                    List<Block> blocks = new ArrayList<>(); // list of old blocks
                    int x, y;
                    world.loadChunk(chunk);
     
                    for (x = -2; x <= 2; x++)
                        for (y = 0; y <= 5; y++) { // It seems that when I add the old block in that location.. it adds the new block I want to make instead...
                            blocks.add(world.getBlockAt(portal.clone().add(x, y, 0)));
                            world.getBlockAt(l.clone().add(x, y, 0)).setType(Material.OBSIDIAN);
                        }
     
                    for (x = -1; x <= 1; x++)
                        for (y = 1; y <= 4; y++)
                            world.getBlockAt(l.clone().add(x, y, 0)).setType(Material.PORTAL);
                    delete(l, blocks, chunk);
     
                    for (Player players : Bukkit.getOnlinePlayers())
                        if (players.isOp())
                            players.sendMessage(ChatColor.DARK_PURPLE + "New thing spawned at X: " + l.getX() + " Y: " + l.getY() + " Z: " + l.getZ());
                }
            }.runTaskTimer(this, 0L, 500L);
        }
     
      // this stands for Positive Or Negative, don't think of it the other way  xD
        public int pOrN(int i) {
            if (i >= 0)
                return i + 8;
            else if (i < 0)
                return i - 8;
            return i;
        }
     
        public void delete(final Location l, final List<Block> blocks, final Chunk chunk) {
            new BukkitRunnable() {
                public void run() {
                    for (int i = 0; i <= blocks.size(); i++) {
                        Block block = blocks.get(i);  // WHY do I get an IndexOutOfBoundsException here?
                        l.getWorld().getBlockAt(block.getX(), block.getY(), block.getZ()).setType(block.getType());
                    }
                    l.getWorld().unloadChunk(chunk);
                }
            }.runTaskLater(this, 100L);
        }
    Hopefully I explained it ok, if I didn't, let me know if you need anymore details
     
  2. Offline

    SnowGears

    What causes a chunk to stay loaded and when will bukkit unload it?
    Minecraft unloads chunks when no players are in the area.

    When I'm randomly picking a chunk, why does it seem to only pick 1-2 chunks out of all my times testing?
    Well first of all you call random.nextInt() which could very likely give you an out of bounds exception when trying to get something out of a list with it.
    What I would recommend is putting all of the chunks in a temporary arraylist. Then just call Arrays.shuffle(chunkList). Then you can get the first chunk in the shuffled list like this:
    Chunk c = chunkList.get(0);

    Does the world.loadChunk() method keep a chunk loaded forever until told to unload?
    I believe so but I could be wrong on this one. I think it keeps the chunk loaded into memory but does not keep it visually loaded until told to unload.

    Hope that helped!
     
    pkt likes this.
  3. Offline

    pkt

    Tooner101
    1. I thought that too, but when I printed out the amount of loaded chunks with no players in the server, it was above 1000 on server start, then lowered down to about 300...

    2. I never heard of the shuffle() method, so thanks for telling me about that :) , but the objects still seem to be placed in the same 'area' of chunks, they don't get scattered as I thought they would... :(

    That would not help me...

    I doesn't help me with what Im trying to do...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
Thread Status:
Not open for further replies.

Share This Page