Odd Glitch

Discussion in 'Plugin Development' started by mattrick, Nov 24, 2013.

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

    mattrick

    Sorry if this is something stupid, but when attempting to get something from a config file and send it to the player, it just send the word "false" to the player. here is my code if it helps:
    Show Spoiler

    Code:java
    1. public class EventListener implements Listener {
    2. public EasyAnswers plugin;
    3.  
    4. public EventListener(EasyAnswers plugin) {
    5. this.plugin = plugin;
    6. }
    7.  
    8. @EventHandler
    9. public void onChat(AsyncPlayerChatEvent event) {
    10. if (plugin.getConfig().contains(
    11. event.getMessage().toLowerCase().replaceAll(" ", "_"))) {
    12. event.setCancelled(true);
    13. event.getPlayer().sendMessage(
    14. plugin.getConfig().getString(event.getMessage().toLowerCase() .replaceAll(" ", "_")+".answer"));
    15. }
    16. }
    17.  
    18. }
    19.  

     
  2. mattrick16
    Maybe have an else statement to allow a chat message to be sent if it doesn't contain "_" ?
     
  3. Offline

    1Rogue

    Print out variables. My guess is the path doesn't exist.
     
  4. Offline

    mattrick

    That's not the problem. It allows the event anyway, but when the criteria is met, the message sent is "false".
     
  5. Offline

    AoH_Ruthless

    mattrick16
    That seems really strange...
    Do you mind posting the config? I'm not even sure what you are trying to do.
     
  6. mattrick16
    Like what 1Rogue said, try a null check to see if the path exists.
     
  7. Offline

    mattrick

    AoH_Ruthless
    Code:
    can_i_be_op:
      answer: No
    The Gaming Grunts 1Rogue
    Nope it exists.....printed out variables, they were correct. It just sends "false" to the player which makes no sense as I never even added a possibility for that to happen :/
     
  8. mattrick16
    Well, idk if this has anything to do with the problem, but you don't need to have an underscore in the config ("_" <--this thing). You can just put normal spaces. So in your code, you don't have to try to replace anything. You can simply do getConfig().getString("some string") and what-not.
     
  9. Offline

    mattrick

    Yeah I know, I just think it would look cleaner. I'll try it though.
     
  10. Offline

    Gamecube762

    Try putting No in quotes. Its looking for a string, and it being in quotes says "Hey! this is a string!". I'm not sure if YML likes the double quotes " or single quotes '.

    Code:
    can_i_be_op:
      answer: "No"
     
    1Rogue likes this.
  11. Offline

    1Rogue

    When .getString() attempts to get a value of "No", it will return false. Looking into the reason now. (Tested externally and confirmed)

    Edit: Putting the word in quotes works. This isn't a bukkit issue, yaml considers "no" as a false value.


    Either quote is fine.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  12. Offline

    AoH_Ruthless


    Good to know for future reference.

    Double quote and single quotes essentially do the same thing. I don't believe there is a difference.

    EDIT: I got ninja'ed :(
     
  13. Offline

    mattrick

    1Rogue

    Thanks! That is odd, Bukkit should throw an error instead.....
     
  14. Offline

    Skye

    There's way better ways to do what you're trying to do, but...

    Without quotes, Bukkit will interpret that field as a boolean and default to returning "false" when getString is called. Surrounding the answer with quotes will solidify it as a String and return "no" like you expect.
     
  15. Offline

    mattrick


    In Java, "A" != 'A'. Single quotes represent a character.
     
  16. Offline

    Gamecube762

    Learn something new everyday =P
     
    mattrick16 likes this.
  17. Offline

    mattrick

    Just a follow up question, I am also trying to replace question mark characters, but using just a "?" throws an error, and using a double backslash question mark ("\\?") also doesn't work. Any ideas? AoH_Ruthless 1Rogue
     
  18. Offline

    1Rogue


    Not quite, YAML itself returns a b0olean value. This is independant of bukkit


    Again, in YAML, single or double quotes make no difference.


    Put it in quotes or use a "\" in double quotes. Alternately use the unicode value of the character.
     
    mattrick16 likes this.
  19. Offline

    mattrick

    Ok thanks :D
     
  20. Offline

    Skye

    Technically, yes...

    However, the object returned by Bukkit's MemorySection.getString does not get casted to String, but instead calls toString() from the object. This is kind of a stretch, as you would expect a method called getString would either return null or log a ClassCastException in the event that a requested field isn't a String. I'm not really sure why it doesn't do this, but it's probably so that plugin makers can conveniently readout configurations to administrators without having to write too many statements.
     
  21. Offline

    1Rogue


    What?

    This makes perfect sense. (Primative) booleans can have two values: true or false. When you cast a boolean to a string it can only be two things, so it returns "false" when you get the string value.
     
  22. Offline

    Skye

    Yes, it makes sense. It is because of Bukkit's implementation that this thread was created. ;)
     
Thread Status:
Not open for further replies.

Share This Page