Identifying Players In A Certain Region

Discussion in 'Plugin Development' started by Jason Malouin, Jun 21, 2015.

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

    Jason Malouin

    Hey guys, how would I go about checking if a certain player , was in a certain area ?
    Heres what I have sofar. The code I am showing you , does not work

    Code:
    [LIST=1]
    [*]            if(player.getLocation().getX() > -451.0 && player.getName() == "tigergirl1500"){
    [*]                          if(player.getLocation().getX() < -442.0){
    [*]                              if(player.getLocation().getY() <= 65.0){
    [*]                                  if(player.getLocation().getY() >= 62.0){
    [*]                                      if(player.getLocation().getZ() < 347.0){
    [*]                                          if(player.getLocation().getZ() > 341.7){                                            
    [*]                                              if(Race.containsKey(1)){
    [*]                                                  if(LapTimer2.timer2 > 3){
    [*]                                                      if(lap2 > 0){
    [*]                                              Bukkit.broadcastMessage(ChatColor.RED + "Lap: " + lap2 + " "  + ChatColor.YELLOW + "Tobysmith10" + " : " + LapTimer2.timer2);
    [*]                                                  }
    [*]                                                      lap2++;
    [*]                                                      LapTimer1.timer1 = 0;
    [*]                                                  }
    [*]                                              }
    [*]                                              }
    [*]
    
    [*]                                              }
    [*]
    
    [*]                                  }
    [*]                              }
    [*]                         
    [*]                          }
    [*]                      }
    [*]                  }
    [*]                         
    [*]               }, 0L, 6L)
    [*][code]
    [/LIST]
     
  2. Offline

    CD3

    Just saying this code would be a LOT more readable using the logical AND Operator..
    it could be simplified like this:

    Code:
    if((player.getLocation().getX() > -451.0) &&( player.getName() == "tigergirl1500" )
    &&(player.getLocation().getX() < -442.0)&&(player.getLocation().getY() <= 65.0)
    &&(player.getLocation().getY() >= 62.0) &&(player.getLocation().getZ() < 347.0)
    &&(player.getLocation().getZ() > 341.7)&&(Race.containsKey(1))&& (LapTimer2.timer2 > 3)
    && (lap2 > 0)){
            Bukkit.broadcastMessage(ChatColor.RED + "Lap: " + lap2 + " "  + ChatColor.YELLOW + "Tobysmith10" + " : " + LapTimer2.timer2);
    }
    // more code
    
    Also you haven't given any information on your issue. for example, what does your console error say?

    Things I Noticed:
    1. lack of logical ANDs
    2. unbalanced brackets
    4. One of your Ifs has this on the closing bracket "}, 0L, 6L)", which would throw an error
    3. No information on issue whatsoever
    Suggestions:
    1. Replace all the if statements with a boolean method:
    Code:
    if (replaceIfs(parameters)){
    //code goes here
    }
    
    public static boolean replaceIfs(parameters){
    if((player.getLocation().getX() > -451.0) &&( player.getName() == "tigergirl1500" )
    &&(player.getLocation().getX() < -442.0)&&(player.getLocation().getY() <= 65.0)
    &&(player.getLocation().getY() >= 62.0) &&(player.getLocation().getZ() < 347.0)
    &&(player.getLocation().getZ() > 341.7)&&(Race.containsKey(1))&& (LapTimer2.timer2 > 3)
    && (lap2 > 0)){
            return true;
    }
    return false;
    }
    
    2. Balance brackets
    3.fix that one closing bracket
    4. add more information to your post so people may actually try to help you.
     
  3. Offline

    Jason Malouin

    That wasnt really The point of my post anyways. All I really need to know. Is ... If i was using player.getName();
    how would a specify a certain player name. The code used above was just my situation as an example.
    So what I want this to say is:


    If "specified player in code" is within these boundaries, then do the following.


    What I Did:

    Code:
    if(player.getName() == "TobySmith10"){
    
    // Do Something
    
    }
    
     
  4. Offline

    Boomer

    well, you simply... provide a variable name for the playername
    Where do you get the playername? From a command issued earlier, from the player being added to a set of names from some action taken? From a configuration file?

    It is what it is - if player.getName() == somenamestring { //do something } indeed
    If you're not hardcoding it, you are coding the name somewhere , based on how you determien what name you want
    If you want to have a list of names that could be checked, you can do it by testing if the playername is in the list/set of names , and IF it is, then keep processing for the region.
     
  5. Offline

    Jason Malouin

    May i have a quick example of this ?
     
  6. Offline

    Boomer

    if (player.getName().equalsIgnoreCase(somePlayerNameVariable) {
    //do stuff
    }
    thats the only way to answer 'provide a variablename' provided you have created and initialized the value of somePlayerNameVariable prior to the execution of that line

    You can use sets that you add or remove player names to, to come up with a small subset of users to act on, then you loop through each user in that set and carry out the checks and functions on that user

    for (Player p: mySetOfUsers) {
    doThingsOn(p);
    }

    is the easiest, simplest, cleanest, clearest way to iterate through a collection, list, map, set etc -- for (Objecttype individualObject: myCollectionOfObjects) { } form.

    If you dont have a way to predefine a smaller subset of users to act on, and need to test each player on the server...
    there is a bukkit collection for online-players that lists everyone currently logged in, you can iterate through that collection, and the doThingsOn(p) part is where you make a function that does all the required tests on player p.
     
Thread Status:
Not open for further replies.

Share This Page