Solved Get a player name through a variable

Discussion in 'Plugin Development' started by Genius4G, Apr 23, 2014.

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

    Genius4G

    Hi, I'm trying to add to a plugin that I have the source code for. The plugin is useful in all ways except I cannot display a player name on the GUI that the plugin creates.

    I went to the variable files in the source code and found this.

    Code:java
    1. if ("%online%".equals(variable))
    2. {
    3. return getOnlinePlayers(player);
    4. }


    The main point of this code is to replace the '%online%' variable in the config file with the amount of players online.

    The amount of players online would be displayed on the ingame GUI.
    I was looking for help as how to use the variable '%player%', to display the player's own name on the gui.

    I hope I have explained this well.

    Many thanks for any support.

    Genius4G
     
  2. Offline

    Serializator

    If you want so the string %online% would be replaced with the online players you can
    Code:java
    1. strVariable.replaceAll(%online%, Bukkit.getOnlinePlayers().length);
     
  3. Offline

    Genius4G

    what I was aiming at was to edit the previous code into this

    Code:java
    1. if ("%playername%".equals(variable))
    2. {
    3. return getPlayerName(player);
    4. }


    i need to assign a private int with the code to replace all %playername% with the name of the player that is using the plugin or viewing the gui.

    the private int would start something like this.

    Code:java
    1. private int getPlayerName(Player player) {
    2. //Gets a Player's Name
    3. the code to get the viewing players name would need to go here
    4. return 0;
    5. }


    any help with what code to put in the blank area to get the players name would be appreciated

    thanx for the support
     
  4. Serializator replaceAll() = regex
    replace() = String

    Genius4G You really have no idea what you're doing, do you? Please learn Java first, otherwise you'll be impossible to help.
     
  5. Offline

    Genius4G

    AdamQpzm i dont want unhelpful comments, and, i did a two year course on java, so chew your words pls
     
  6. Genius4G 2 years? Then apply that knowledge. If you still don't know, I think you didn't learn much, have forgotten it, or are lying.

    Try and explain why a method called getPlayerName which takes a Player as an argument returns an int, what's that supposed to do? I say that you don't know what you're doing, but maybe you just have trouble explaining it or I just don't understand. Because I'm not actually sure what it is you're stuck on or what you're trying to do exactly.
     
  7. Offline

    1Rogue


    He isn't wrong about this being basic though.

    When you do a string comparison, it is a literal comparison. Doing:

    Code:java
    1. if ("someString".equals(/* an object to compare */)) {...}


    Will compare if the passed object literally equals "someString". If you compare it to "%myDynamicVariable%", it will only match other strings that literally match "%myDynamicVariable%" character for character.

    If you want to replace that text with some value, you should use .replaceAll:

    Code:java
    1. Player p = /* the player executing the command */;
    2. String overall = /* config string */;
    3. overall = overall.replaceAll("%online%", getServer().getOnlinePlayers().length);
    4. overall = overall.replaceAll("%playername%", Matcher.quoteReplacement(p.getDisplayName())); //protect from bad display names
     
  8. 1Rogue Without the all of course ;)
     
  9. Offline

    1Rogue


    If it's a variable that should be replaced, then no; You would include the "All"
     
  10. Offline

    Genius4G


    you obviously didnt read what i had put, the point of the code is that %playername% is replaced with the name of the ingame player that is looking at her/his own gui. since getPlayerName isnt defined yet, i need to tell eclipse what that getPlayerName is going to do.
     
  11. Offline

    1Rogue


    You don't need a method for getting a name from a Player object, it already exists:

    Code:java
    1. Player p; //some player
    2. p.getName(); //their literal name
    3. p.getDisplayName(); //their name with prefixes/chat adjustments


    http://jd.bukkit.org/rb/apidocs/org/bukkit/entity/Player.html
     
  12. 1Rogue Why? From what I understand replace() and replaceAll() do exactly the same thing, except that replaceAll() uses regex.

    Genius4G Yes, but I'm still not sure what you mean. E.g. "i need to assign a private int with the code to replace all %playername% with the name of the player that is using the plugin or viewing the gui." What's the supposed to mean? I assume you just mean that you want to replace %playername% with the actual player's name, but that wouldn't explain the whole assigning a private int thing.
     
  13. Offline

    1Rogue

    Because .replaceAll doesn't stop after the first match
     
  14. Offline

    Genius4G

    rogue what is your skype i need to speak to you
     
  15. 1Rogue Neither does replace()
     
  16. Offline

    1Rogue

    Yeah, just looked at the javadocs. I think it used to be that way, but not entirely certain where I'm deriving it from.


    My skype is private. You're perfectly open to receiving help on this forum I would imagine.
     
    AdamQpzm likes this.
  17. Offline

    Genius4G

    1Rogue can i speak to you over skype about this please


    ok sorry

    here is a picture of my eclipse, maybe you could give me advice from there


    as you can see the method needs to go below in the highlighted section


    the url to the picture is here


    to view the picture just click on the picture at the website

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  18. Genius4G What are you trying to get the player name from and what are you trying to do with it afterwards?
     
  19. Offline

    Genius4G

    I have an ingame gui to the right of the screen and i cant seem to correctly code the variable %playername% so that when i specify that i want %playername%, being the connected player's personal miecraft name, to display the name on the top of the gui

    if you have alook on the picture i posted above you will see exactly what i mean

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  20. Genius4G So you have a GUI and you just want to set the title of that GUI to a player's name?
     
  21. Offline

    Genius4G

  22. Genius4G Then when you're creating the inventory, you need to specify the title in there.
     
  23. Offline

    Genius4G

    but i want the title of that GUI to be the specific name of the player that is looking at it, so my gui would say 'Genius4G' wheras some one elses may say 'CreeperExpletive', if that is their minecraft name.
     
  24. Genius4G Yes I understand that. Show me the full code for where the inventory is initialised please.
     
  25. Offline

    Genius4G

    the inventory is nothing to do with this part i am stuck on, that is a different variable i was adding
     
  26. Genius4G So then what is the GUI made from?
     
  27. Offline

    Genius4G

    this is to do with part of the gui that i am working on
     
  28. Genius4G Show me the code for the gui please
     
  29. Offline

    Genius4G

    the gui doesnt make any difference, the piece of code that im trying to solve is, when i put %playername% in the plugin config file, it will recieve the method and display the player's own name at the top of the gui
     
  30. Genius4G Which part are you stuck on, the replacing the %playername% with the player's name or the displaying on the gui or something else?
     
Thread Status:
Not open for further replies.

Share This Page