area teleport

Discussion in 'Plugin Development' started by MattTheBeast, Feb 14, 2017.

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

    MattTheBeast

    Hi, I'm trying to get a player to teleport to a random area when he enters a designed area. For example, lets say i have a 3x3 area, when a player enters that area, he gets teleported to a random location that I set.

    But it isn't working! I'm pretty new to all this coding stuff so dont blame me. I think the mistake is on the area that i need to enter to get teleported.

    here is my code:

    public void onNetherPortalEnter(PlayerMoveEvent e)
    {
    Player p = e.getPlayer();
    Random random = new Random();

    int NPx = random.nextInt(422) + 418;
    int NPy = random.nextInt(135) + 134;
    int NPz = random.nextInt(284) + 280;

    if(p.getLocation() == new Location(p.getWorld(), NPx, NPy, NPz))
    {
    Location teleportLocation = null;

    int NPTx = random.nextInt(529) + 467;
    int NPTy = 53;
    int NPTz = random.nextInt(320) + 202;

    boolean isOnLand = false;

    while (isOnLand == false) {

    teleportLocation = new Location(p.getWorld(), NPTx, NPTy, NPTz);

    if (teleportLocation.getBlock().getType() != Material.AIR) {
    isOnLand = true;
    } else NPTy--;

    }

    p.teleport(new Location(p.getWorld(), teleportLocation.getX(), teleportLocation.getY() + 1, teleportLocation.getZ()));

    }


    I used the PlayerMoveEvent cuz I thought it to be the best option, but if anybody has any other suggestions please mention.
     
  2. Offline

    mine-care

    Before i start, i want to point out that:
    Don't compare Object pointers with ==, use .equals() instead.

    Anyways your code doesn't really seem to be doing what you expect it to...
    Err what are the chances! You create a random location with:
    x between 418 and 839
    y between 134 and 268
    z between 280 and 563
    and then attempt to check if the player is in that location ('attempt' goes for reasons explained above)
    The chances of the player actually hitting a constantly changing location are pretty low and because the move event fires with the slightest movement (even player rotation or head movement) this is generating hundreds of Random object instances, locations and performs tons of checks very frequently. Imagine if you have 100 players online, how many times a second this will fire!


    Anyway, a possible way to go about doing what you want is:
    1. Check if the player is between two diagonal locations (player x,y,z has to be numerically between the two locations' x,y,z For isntance, minLocationX < playerX < maxLocationX)
    2. If and only if they are within the location THEN create the random location (i sugest you keep the Random object as a class variable and you don't create one each time the move event fires) Hint: you may use this method to determine the first on-ground block.

    Voila! Try that out and let us know if you have any difficulties.
     
    Last edited: Feb 15, 2017
  3. Offline

    MattTheBeast

    I thought it worked like this int x = (Max) + min
    Since it calculates the random int starting from the + min all the way to the designed max

    Like lets say I have (100) it take a random from 0 to 100 so if I have + 5 its from 5 to 100, isn't that how it works?

    Btw do you think I should use worldguard api for selecting a area?
    And if the playermoveevent is bad what should I use?

    Sry for all the questions im new
     
    Last edited: Feb 15, 2017
  4. Offline

    mine-care

    no, if you have a random between ( 0 and 100 ) + 5 that is a random between 5 and 104 because if we suppose that the random between 0 and 100 turns out to be 99 then 99+5= 104 not 100.
    Also nextInt(int max) is exclusive of max, therefore if you want an int between 0 and 5 inclusive of 5 you need to do nextInt(6);

    The problem in your code is that you are creating a random location within certain boundaries and then you attempt to check if the player is ON that perticular location. A location is a single block not a region of blocks though! So the chances of the random location to be the same as the location of the player are EXTREMELY slim.
     
  5. Offline

    MattTheBeast

    @mine-care Ok, I just realised how retarded I am, I got mixed up a bit. I'm going to use the world guard and edit api to select a region and when a region is selected I will teleport them to a random location within the intergers.

    Thanks for helping, also I'm kinda new to coding so :p
     
  6. Offline

    mine-care

    @MattTheBeast Haha nah you're not! We all make mistakes ;)
    Well if you dont want to depend onWorldGuard and overcomplicate things you can use something like the following code (I wrote it in Pseudocode off the top of my head i hope its correct):
    Code:
    Location CORNER_ONE;
    Location CORNER_TWO;
    Location PLAYER;
    
    if PLAYER.x <= max(CORNER_ONE.x , CORNER_TWO.x) and PLAYER.x >= min(CORNER_ONE.x , CORNER_TWO.x) then:
       if PLAYER.y <= max(CORNER_ONE.y , CORNER_TWO.y) and PLAYER.y >= min(CORNER_ONE.y , CORNER_TWO.y) then:
           if PLAYER.z <= max(CORNER_ONE.z , CORNER_TWO.z) and PLAYER.y >= min(CORNER_ONE.z , CORNER_TWO.z) then:
                       //Player is between the two locations
                endif
        endif 
    endif
    
     
  7. Offline

    MattTheBeast


    Thanks dude! Il take that into consideration, I dont like having a dependancy to other plugins xD Even tho worldguard and worldedit is kinda a execption since its one of those staples.
     
  8. Offline

    mine-care

    @MattTheBeast No problem! :)
    And yeah you are right to say that worldguard and wordledit are in the section of essentials but eh why limit your plugin when you can make it absolutely standalone :D
     
Thread Status:
Not open for further replies.

Share This Page