Getting target name

Discussion in 'Plugin Development' started by DamnHippo, Sep 17, 2014.

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

    DamnHippo

    I don't really know how to describe this. Pretty much I'm trying to get my /msg plugin to get the players name this is the code that I'm using to get the player's name "
    Player target = Bukkit.getPlayer(pName);" Now the thing that I'm trying to get is that if I type /msg Hippo its going to send a message to DamnHippo if he is online if no one elese's name contains hippo. So lets say my name is HippoBeans and I type /msg Beans it would send the message to HippoBeans. The current way that it's set up I can type /msg Damn and it sends the message to DamnHippo.
     
  2. Offline

    Unica

    Code:java
    1.  
    2.  
    3. private String getSomeName(String argument){
    4. String name = null;
    5. for(Player p : Bukkit.getOnlinePlayers()){
    6. if(p.getName().contains(actionName)({
    7. name = p.getName();
    8. }
    9. }
    10. return name;
    11. }
    12.  


    But there must be an efficient way
     
  3. DamnHippo I'm not sure what your question means. Just call getName() on the Player object returned?
     
  4. Offline

    fireblast709

  5. Offline

    DamnHippo

  6. DamnHippo Put some dots and parentheses there and you have yourself an actual statement.
     
  7. Offline

    fireblast709

    DamnHippo which is exactly what the method checks for...
    What Bukkit.matchPlayer(String) does (open)
    Code:java
    1. @Deprecated
    2. public List<Player> matchPlayer(String partialName)
    3. {
    4. Validate.notNull(partialName, "PartialName cannot be null");
    5.  
    6. List<Player> matchedPlayers = new ArrayList();
    7. for (Player iterPlayer : getOnlinePlayers())
    8. {
    9. String iterPlayerName = iterPlayer.getName();
    10. if (partialName.equalsIgnoreCase(iterPlayerName))
    11. {
    12. matchedPlayers.clear();
    13. matchedPlayers.add(iterPlayer);
    14. break;
    15. }
    16. if (iterPlayerName.toLowerCase().contains(partialName.toLowerCase())) {
    17. matchedPlayers.add(iterPlayer);
    18. }
    19. }
    20. return matchedPlayers;
    21. }
    Note: the @Deprecated is solely to raise awareness for the UUID change.
     
  8. DamnHippo this is a step by step list you got to do:
    1. create a method to loop through a player[] and check every players name ->
    Code:java
    1. private Player searchPlayer(Player[] players, String namePart){
    2. int foundAmount = 0;
    3. Player foundPlayer = null;
    4. for(Player p : players)
    5. if(p.getName.contains(namePart){
    6. foundAmount = foundAmount + 1;
    7. foundPlayer = p;
    8. }
    9. if(foundAmount <2)
    10. return foundPlayer;
    11. return null;
    12. }

    1. check if there is only one player with this name -> Player p = searchPlayer(Bukkit.getOnlinePlayers(), "Your name to search for"); if(p!=null) -> player has been found
    3. send the found player a message -> p.sendMessage("Your message to send");

    This is how you could do it
     
  9. Offline

    ChipDev

    You are talking depreciated, Don't care of that, Unless your storing the player etc.
    (Its because of UUID, Universal unique ID, 1.7+ uses it for changing names, which Microsoft may change/handle ): )
     
Thread Status:
Not open for further replies.

Share This Page