Solved Custom language supported sendMessage method

Discussion in 'Spigot Plugin Development' started by GCNT, Jun 1, 2018.

  1. hello spigot!

    I'm trying to make a Custom sendMessage method which gets the selected path from the config and add something to it. The sendMessage method works fine, but i want to be able to use the #replaceAll() method in my custom method.
    For example:
    Code (Java):
    Utils.sendMessage(player, "clear").replaceAll("%player%, player.getName())
    My method looks like this:
    Code (Java):
    public class Utils implements Listener {
    static public void sendMessage(Player player, String path) {
            String language = AdvancedMultiLanguageAPI.getLanguageOfUuid(player.getUniqueId().toString());
            player.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getInstance().getConfig().getString("messages." + language.toString() + "." + path)));
    the language string has been done by using the AdvancedMultiLanguageAPI plugin.

    Can someone tell me how i can make it possible to use the the example code? (the #replaceAll() method)
  2. #replaceAll has to be used on a string. You'd probably be best including it somewhere in your sendMessage method, since it's (or I would expect it to be) a void method that sends the message. Otherwise, you'd have to define the string message, replaceAll, then send the message.. if that makes sense >.>
  3. I know it has to be used on a string.
    But i don't want to include it. Because i won't replace the same thing in every message
  4. Then set the method's return type to String and use the method on the string it returns..?
  5. I tried, but don't know how.
  6. You don't know something as basic as changing a method's return type?
    What I expect your method currently looks like:
    Code (Java):
    public void sendMessage(Player player, String configPath) {
    What it should look like instead:
    Code (Java):
    public String getMsg(String configPath) {
        return getConfig().getString(configPath);
    /*...using it, but rather than a custom sendMessage method,
    * make use of the already existing one unless your method
    * does something special. Instead, get the String using the
    * getMsg method (old sendMessage), and use #replaceAll
    * on the String it returns.

    #6 Escad, Jun 1, 2018
    Last edited: Jun 1, 2018
  7. JanTuck


    Use String#replace instead of String#replaceAll
    • Like Like x 2
    • Agree Agree x 1
  8. I know this.
    But i don't want to just make a getMsg thing.
    I just want something like:
    Code (Java):
    Utils.sendMessage(player, "noob").replaceAll("%player%", player.getName());
    Utils.sendMessage(player "noob").replace("%player%", player.getName());
    //which actually is:
    player.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.getInstance().getConfig().getString("messages." + language + "." + "noob").replace("%player%", player.getName())))
  9. My suggestion would give the same exact effect that you're going for. The only difference is the return type, what I called the method after changing the return type, and the parameters of the method (instead of Player, String, just String).
  10. It has to be (Player player, String path) because the method gets the language of the player and after that it gets the string messages. + language + . + path from the config and replaces the '&' with chatcolors.
  11. But I made it something like this
  12. I now use:
    Code (Java):
    player.sendMessage(Utils.sendMessage(player, "clear"));
    And it works fine cause I can use the #replace method.
  13. I now use:
    Code (Java):
    player.sendMessage(Utils.sendMessage(player, "clear"));
    And it works fine cause I can use the #replace method.
  14. Please mark as solved.