Help with replacing a String

Discussion in 'Plugin Development' started by FletchTech90, Jun 14, 2014.

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

    FletchTech90

    Hi, I am having some troubles with trying to broadcast a message from a config file, while replacing "{PLAYER}" with the Player's name. Here's the code I've tried.

    Code:java
    1. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', this.getConfig().getString("fake-leave-message")).replaceAll("\\{PLAYER\\}", player.getName()));


    Code:java
    1. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', this.getConfig().getString("fake-leave-message")).replace("\\{PLAYER\\}", player.getName()));


    I have no idea what to do, I've checked [StackOverflow], the [Oracle Java Docs], and all kinds of places. This is urgent for [one of my plugins]!
     
  2. Offline

    thomasb454

    I'm not sure, but I've always done this:

    The keywords I use to replace are formatted like this:
    %player% (I'm not sure if this will help, but I've always done it this way so it might work)
     
  3. Offline

    FletchTech90

    thomasb454

    Thanks. I used {PLAYER} because that's what Essentials uses. :p I'll try %player%.
     
  4. Offline

    garbagemule

    Do yourself (and anyone who will be reading your code in the future) a favor and break your statements into multiple lines of smaller chunks. You have a grand total of six method invocations in a single line that's taking up 165 characters, and that's when it's not indented! It would wrap three times with an auto-formatter! Not only is such a long statement full of nested method invocations hard to grok and annoying to read, it's also tedious to debug. Split it up into a few extra lines, and you'll have a much easier time figuring out if you're looking in the right place.

    You said you checked the Javadocs, but you're calling the replace method with the String "\\{PLAYER\\}" - unless that is the literal String you're trying to replace, you're not going to get the behavior you're expecting. That said, your call to replaceAll should work just fine, assuming the call to translateAlternateColorCodes doesn't mess up the String somehow. Is there any reason you are translating color codes before replacing the template? If it's by mistake, that's just another good reason to split your code up into a few extra lines.

    Finally, depending on how your config-file is formatted, you may want to pick a different pair of delimiters. The curly braces are special in YAML, so your parsed config-file may be structured in a different way than what you expect. Again, split your lines up and do a bit of debug printing. It'll make things so much easier for you.
     
    thomasb454 and Garris0n like this.
  5. Offline

    thomasb454

  6. Offline

    FletchTech90

    garbagemule


    Thanks for the feedback, I'll work on it. :)

    Okay, thanks. :)

    Overall, I think I'll just stick to thomasb454's method. Thanks to both of you.
     
    thomasb454 likes this.
Thread Status:
Not open for further replies.

Share This Page