Is this right?

Discussion in 'Spigot Plugin Development' started by suitinacow, Jul 5, 2015.

  1. Code (Text):
                if(player instanceof Player){
                    WorldBorder wB = Bukkit.getServer().getWorld(args[0]).getWorldBorder();
                    WorldBorder center = (WorldBorder) wB.getCenter();
                    int one = r1.nextInt((int) wB.getSize());
                    int two = (r1.nextInt((int) wB.getSize()))*-1;
                    int chance = r1.nextInt(5);
                    int y = 0;
                    if(chance == 0){
                        ((Location) center).subtract(one,y,one);
                    if(chance == 1){
                        ((Location) center).add(one,y,one);
                    if(chance == 2){
                        ((Location) center).subtract(two,y,two);
                    if(chance == 3){
                        ((Location) center).add(two,y,two);
                    player.teleport((Location) center);
                    if(chance == 4){
                        ((Location) center).add(one,y,two);
                    if(chance == 5){
                        ((Location) center).add(two,y,one);
                return true;
    what im trying to do is basicly random teleport someone to a location in a world.. based on the world border im trying to randomly teleport anywhere inside the border
  2. Why are you casting a location as WorldBorder and then casting it back to Location? You should use
    Location center = wB.getCenter();
    Then you can drop the (Location) casts.

    Also, getSize() returns the total size of the border, not the distance from the center to the border. The way you are doing it now could put you up to 2x further from the center than the border. A better way would be to take your random numbers "one" and "two" (without the * -1) and subtract getSize()/2 from them. This would give you two random numbers that are between -border size to +border size. Then just add those to your center location's x and z. This way there is no need for all the if statements.

    Then I would add a check to make sure the player is not teleporting into solid material. You can use World#getHighestBlockAt(Location) to get a safe location to teleport them to.

    Finally, obviously you need to add a line at the end that actually teleports the player.
    #2 St3venAU, Jul 5, 2015
    Last edited: Jul 5, 2015
    • Like Like x 1
  3. Maybe it's better to stick to one thread, and not make multiple threads, because you can just bump. and you also just need to have some patience.
    • Agree Agree x 2
  4. thanks for the help! but im confused about one thing.. how can subtracting getsize()/2 to one and two give me a -border and +border? i need a number before 0,0 and after 0,0
  5. you're right i need to be abit more patient lol
  6. Let's say your border is 100 x 100 (so your border is 50 blocks from the center)

    int size = (int) wB.getSize();
    ^ this will be 100

    int x = r1.nextInt(size);
    ^ this will be a random number between 0 and 99

    x -= size/ 2;
    ^ by subtracting size/2, we turn it into a random number between -50 and +49

    Do this again for z and then add those to your center location:

    Location l = wb.getCenter().add(x, 0 , z);

    Then i'd get the highest block at that location so that you ensure you aren't teleporting the player under ground:

    l = l.getWorld().getHighestBlockAt(l).getRelative(BlockFace.UP).getLocation();
    • Like Like x 1
    • Agree Agree x 1
  7. Thanks for explaining, that helps
  8. This is what i came up with:
    Code (Text):
                if(player instanceof Player){
                    WorldBorder wB = Bukkit.getServer().getWorld(args[0]).getWorldBorder();
                    Location center = wB.getCenter();
                    int one = (int) ((r1.nextInt((int) wB.getSize())) - (wB.getSize()/2));
                    int two = (int) ((r1.nextInt((int) wB.getSize())) - (wB.getSize()/2));
                    Location location = center.add(one, 0, two);
                    location = location.getWorld().getHighestBlockAt(location).getRelative(BlockFace.UP).getLocation();
                    player.teleport((Location) location);
                return true;
  9. This doesn't seem to work since I'm using the world border plugin and i think it may not be checking on the plugin data but the default..
  10. sothatsit


    If your not using vanilla world boarders you will need to hook into the plugin which handles the world boarders. The plugin should have some documentation on how to do this.
  11. I'm already using a world border plugin if that's what you mean... I think the problem is coming from the code trying to find the vanilla world border which is non-existant instead of the plugin world border.
  12. But I'm looking at the plugin documentation and I don't see anything related...
  13. sothatsit


    Message the plugin's author
  14. Oooor just
    1. Make a WorldBorder plugin and hook into it yourself.
    2. Use VANILLA borders, and tell that to the users.
  15. Could've thought of that.. thanks lol