Solved is world.loadChunk(int x, int z, boolean regen); async? (Make random Teleport 1.14)

Discussion in 'Spigot Plugin Development' started by JoeyPlayzTV, Feb 1, 2020.

  1. Hey, i have only a short question. I will make a random TP for my Server-Ssystem. But the Problem is, randomly players gets teleported in the ground. I think the issue is that, thats the koordinate gets savechecked before the chunk is complete generated. Now my question, is world.loadChunk async? if yes a must create a bukkit task that runs later.


    Here my code

    Code (Java):
    /**
         * Errechnet eine sichere Position.
         *
         * @param player {@link Player}
         * @param world  {@link World}
         * @param max_x  {@link Integer}
         * @param max_z  {@link Integer}
         * @return {@link Location} und null wenn unsicher.
         */

        private Location getGoodLocation(World world, int max_x, int max_z) {
            Location teleportlocation = null;
            Random random = new Random();
            int x = random.nextInt(max_x);
            int y = 170;
            int z = random.nextInt(max_z);
            Location targetlocation = new Location(world, x, y, z);
            targetlocation.getWorld().loadChunk(x, y, true);
            while (targetlocation.getBlockY() > 0) {
                Block block = targetlocation.getBlock();
                if (block.getType() == Material.LAVA || block.getType() == Material.WATER
                        || block.getType() == Material.CACTUS) {
                    break;
                } else if (block.getType() == Material.AIR) {
                    targetlocation.subtract(0.0, 1.0, 0.0);
                } else if (block.getType() != Material.AIR) {
                    teleportlocation = targetlocation;
                    break;
                }
            }
            if (teleportlocation != null) {
                return teleportlocation.add(0.0, 1.0, 0.0);
            } else {
                return null;
            }
        }
    Iam enjoyed for any suggestions or help.
     
  2. Spigot doesn't have an API for loading chunks asynchronous.
     
  3. Ok? Why this code let players spawn into the ground? Have a solution for it?
     
  4. Please use World#getHeighestBlockAt
     
  5. Ok? Also instead of int y = 170; int i = world.getHigestBlockAt().getY; ?
     
    • Agree Agree x 1
  6. Thanks i will try it
     
  7. You found a bug. See your Github ticket. Looks like getHighestBlockAt returns Material of highest-solid+1
     
  8. Not work i get stuck in the ground

    Code (Java):
    private Location getGoodLocation(World world, int max_x, int max_z) {
            Location teleportlocation = null;
            Random random = new Random();
            int x = random.nextInt(max_x);
            int z = random.nextInt(max_z);
            world.loadChunk(x, z, true);
            int y = world.getHighestBlockYAt(x, z);
            Location targetlocation = new Location(world, x, y, z).subtract(0.0, 1.0, 0.0);
            Block block = targetlocation.getBlock();
            if(block.getType() != Material.WATER && block.getType() != Material.LAVA) {
                teleportlocation = targetlocation;
            }
            return teleportlocation;
        }
     
  9. It is like the world gets generated around me
     

  10. Thanks! I have fixed the random Teleport. Thank you so mush.
     
  11. Can you explain how you fixed it? I had a similar issue in the past.
     
  12. Yea. Simple get the highest block in the world int y = world.getHighestBlockYAt(x, z);
    then get the blockface down from the location and check whatever you want. then teleport the player.