Check if a player is in a specific arena

Discussion in 'Plugin Development' started by Panjab, Nov 28, 2012.

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

    Panjab

    Hey guys!

    I'm writing a plugin to enabling PvP for 2 Players: P1 vs. P2. This works amazing. But there's one problem:

    How can I disable that Non-PvP-Players can't enter the arena? e.g. that there's an invisible wall that stops that they can enter the arena?

    Currently I have this code:

    Code:java
    1.  
    2. double x = plugin.saveFile.getDouble(plugin.arena + ".L1.X");
    3. double x2 = plugin.saveFile.getDouble(plugin.arena + ".L2.X");
    4. double y = plugin.saveFile.getDouble(plugin.arena + "L1.Y");
    5. double y2 = plugin.saveFile.getDouble(plugin.arena + "L1.Y");
    6. double z = plugin.saveFile.getDouble(plugin.arena + ".L1.Z");
    7. double z2 = plugin.saveFile.getDouble(plugin.arena + ".L2.Z");
    8.  
    9. double px = player.getLocation().getX();
    10. double py = player.getLocation().getY();
    11. double pz = player.getLocation().getZ();


    Thanks a lot!
     
  2. you have to get the min and max numbers and then check if the player is between it..
    i do it like that:
    double lowX = Math.min(x1, x2);
    double lowY = Math.min(y1, y2);
    double lowZ = Math.min(z1, z2);
    double highX = Math.max(x1, x2);
    double highY = Math.max(y1, y2);
    double highZ = Math.max(z1, z2);

    double pX = p.getLocation().getX();
    double pY = p.getLocation().getY();
    double pZ = p.getLocation().getZ();

    public boolean isInArea(){
    if (lowX <= pX && pX < highX && lowY <= pY && pY < highY && loxZ <= pZ && pZ < highZ){
    return true;
    } else {
    return false;
    }
    }

    then just call it onPlayerMove oder so :D
     
  3. Offline

    Uniclaw

    pizzafreak08 If he would call it in the MoveEvent, your Code doesn't works ;) You need to check if the location event.getTo() is in the Area.
     
  4. yes of course :D it was just a code snippet :D
     
  5. Offline

    Panjab

    pizzafreak08

    Hm.. I'll try it.

    Uniclaw

    Yes, but this is my problem. I don't know how to check if the event.getTo() is in the arena :eek:
     
  6. Offline

    Uniclaw

  7. Offline

    Panjab

  8. i would do it like this:

    //need to store the arena coords somewhere...
    private int x1 = 0;
    private int x2 = 0;
    private int y1 = 0;
    private int y2 = 0;
    private int z1 = 0;
    private int z2 = 0;

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent){
    Location loc = event.getTo();
    if(isInArea(loc)){
    event.setCancelled(true); //maybe?!
    }
    }

    private boolean isInArea(Location loc){
    double lowX = Math.min(x1, x2);
    double lowY = Math.min(y1, y2);
    double lowZ = Math.min(z1, z2);
    double highX = Math.max(x1, x2);
    double highY = Math.max(y1, y2);
    double highZ = Math.max(z1, z2);

    double pX = loc.getX();
    double pY = loc.getY();
    double pZ = loc.getZ();

    if (lowX <= pX && pX < highX && lowY <= pY && pY < highY && loxZ <= pZ && pZ < highZ){
    return true;
    } else {
    return false;
    }
    }
     
  9. Offline

    coldandtired

    Why do that hundreds of times a second?
     
  10. yeah thats bad. so you should find out min and max on enable i think and save them to a variable or something
     
  11. Offline

    Panjab

    I tried everything out, but I don't get it working. :(
     
  12. Offline

    coldandtired

    If you just want to have it working, you're free to use Extra Events. If you want to see how I did it check out the source.
     
    Panjab likes this.
  13. Offline

    Panjab

    coldandtired

    I try out your method isIn_area, but how it works? I don't know how to use or to edit it for me. :/
     
  14. Offline

    coldandtired

    If you want to just use the plugin then add it to your project as a reference (just like Bukkit) and then listen for the events you want. All users need to install the plugin as well.

    If you want to implement it yourself, here's how it works:

    The main class sets up a repeating timer to check areas every 10 ticks (500ms). The plugin uses both custom areas defined in the config and WorldGuard regions, but these are converted to my areas to keep the code clean.

    Regions have a min and max point, and these are read into the Area class here. Once the Area class has the six points (minx, maxx, etc.) the isIn_area method takes the player's location and makes sure it is within min and max for all three axes.
     
  15. Offline

    Panjab

    coldandtired

    Wow, thanks. But how to do it if you got these information:

    Code:java
    1.  
    2. double x = [..].getDouble(ModPvP.arena.get(player) + ".L1.X");
    3. double y = [..].getDouble(ModPvP.arena.get(player) + ".L1.Y");
    4. double z = [..].getDouble(ModPvP.arena.get(player) + ".L1.Z");
    5.  
    6. double x2 = [..].getDouble(ModPvP.arena.get(player) + ".L2.X");
    7. double y2 = [..].getDouble(ModPvP.arena.get(player) + ".L2.Y");
    8. double z2 = [..].getDouble(ModPvP.arena.get(player) + ".L2.Z");
    9.  
    10. Location player_From = new Location(player.getWorld(), event.getFrom().getBlockX(), event.getFrom().getBlockY(), event.getFrom().getBlockZ());
    11. Location player_to = new Location(player.getWorld(), event.getTo().getBlockX(), event.getTo().getBlockY(), event.getTo().getBlockZ());
    12.  


    ? :) You're welcome!
     
  16. Offline

    coldandtired

    Are these player locations or areas?
     
  17. Offline

    Panjab

    coldandtired

    These are coordinates of opposite points.

    Code:
    L1
             
                  L2
     
  18. Offline

    coldandtired

    All you have to do is check that the player's location is within those two points.

    Code:Java
    1.  
    2. playerx = event.getTo().getX();
    3. //repeat for y and z
    4.  
    5. if (playerx >= x && playerx <= x2 &&
    6. //repeat for y and z
    7. )...
     
Thread Status:
Not open for further replies.

Share This Page