Checking if player in config file has string value

Discussion in 'Spigot Plugin Development' started by Nighterance, Apr 25, 2017.

  1. Hello!
    So I am currently making plugin, that checks, if you, on config file (playerdata.yml) has string as value "ChatYES"

    To test out, here I set players group to chat yes

    config.set(p.getName()+".group", "ChatYES");

    Now I go to my main class, where I have if function:

    String pname = pc.config.getString(p.getName()+".group");
    if (pname == "ChatYES")

    So this returns, that that is not ChatYES, altought on config file it is, and when I wrote
    it returned ChatYES

    [The playerdata.yml]
    group: ChatYES
  2. Please don't compare Strings using ==
    Instead use equals:
    Code (Text):
    EDIT: Or in your case, even better:
    Code (Text):
    because it is NullPointerException proof
    • Agree Agree x 1
  3. Use .equals() instead of == when checking 2 strings. So try this:
    Code (Text):
    if(pname.equals("ChatYES")) {
       //Do something
  4. Ahh too fast :p
  5. I recomment checking #isSet(<playername>) before trying to access maybe non-existing values.
    This is false, because "ChatYES" creates a new String object instance. This instance is not the same instance like the one returned by #getString.
    Altough their Content might be the same, they're 2 different objects, that's why == will return false. Basic Java.
    Use #equals.

    EDIT: gg, double ninjad... but noone yet mentions #isSet...
    • Useful Useful x 1
  6. Thank you guys
    #6 Nighterance, Apr 25, 2017
    Last edited: Apr 25, 2017
  7. One more thing.
    I am canceling player chat message, so it does not show, then it checks, if other player has permission and then if yes, they will see that message. Everything works, i have like this

    for (Player onlinep : Bukkit.getOnlinePlayers()) {
    if (onlinep.hasPermission("chat.see")) {
    onlinep.sendMessage(e.getPlayer().getName() + " " + e.getMessage()));

    So far so good. But It destroys everything - players has no prefixes, op's has no red nick on chat ect. Is it possible to display message as it was or only way would be to implement plugin like vault or permissionex or whatever supports those prefixed and colors? Thanks!
  8. Did you already search the forums / google for it?
    I'm pretty sure, there is a solution somewhere.

    If not, these are my first thoughts
    1. Don't use #sendMessage, don't cancel the chat event. Iterate through #getRecipients of the event and try to remove those without permission. Because this method only returns a set and the field is private, this might not be easy and may require reflections. I somehow do not recomment it like this
    2. Cancel the event. Instead of using #sendMessage, construct a new AsyncPlayerChatEvent, pass a list of permitted recipients to this constuctor, so only those will recipe the message. Then fire the event using bukkits #callEvent. Should work fine. I guess this solution might be the best.
    3. Make your chat event listener by the lowest priority (so all chat plugins will be called before), then use #getFormat to get the latest formatting. Cancel the event. Apply the formatting and use #sendMessage for all permitted players.
    I somehow like the second solution most.
  9. Must you spoonfeed?