Solved How to do ChatFormat

Discussion in 'Spigot Plugin Development' started by josh13_, Aug 13, 2018.

  1. hello soo i wanna create first plugin of mine and i stuck at making ChatFormatting with prefixes support and i want all this stuff to be configurable
    Code (Java):
        @EventHandler
        public void ChatEvent(AsyncPlayerChatEvent event){

            Main.getPlugin(Main.class).getConfig().addDefault("Enabled", true);
            event.setFormat(Main.getPlugin(Main.class).getConfig().getString("Chat Format").replace("%player%", event.getPlayer().getName()).replace("%message%", event.getPlayer().hasPermission("chatify.chatcolor") ?
                    ChatColor.translateAlternateColorCodes('&', event.getMessage())
                    :
                    event.getMessage()));

        }
    this is what i currently have and got this example from google. please help i need quick examples of hoe can i do this in simple way
     
  2. "%message%", event.getPlayer().hasPermission("chatify.chatcolor")
    this does not make any sense. What do you want to do?

    if you want to check if the player has the permission "chatify.chatcolor"
    just do this
    Code (Text):
        @EventHandler
        public void ChatEvent(AsyncPlayerChatEvent event){

            Main.getPlugin(Main.class).getConfig().addDefault("Enabled", true);
            if (event.getPlayer().hasPermission("chatify.chatcolor"){
                event.setFormat(Main.getPlugin(Main.class).getConfig().getString("Chat Format").replace("%player%", event.getPlayer().getName()).replaceAll("&", "§");
                } else {
                    event.setFormat(Main.getPlugin(Main.class).getConfig().getString("Chat Format").replace("%player%", event.getPlayer().getName()));
                }

        }
     
    #2 Teh_Matt_GRyt, Aug 13, 2018
    Last edited: Aug 13, 2018
  3. as i said i got that example of code from google and all i need it better example to format chat with prefixes suffixes and everything that chatformat needs.
     
  4. The code that the OP provided is fine and there's no difference to what you did, rather than using #replaceAll("&", "§") which is wrong since replaceAll is for regex use mostly or even only.
    replace(..) would suffice. However, there's already ChatColor#translateAlternateColorCodes available for this cause.


    Code (Java):

    private Main instance;
    private Main getMain() {
    if (instance == null) instance = Main.getPlugin(Main.class);
    return instance;
    }

      @EventHandler
        public void ChatEvent(final AsyncPlayerChatEvent event){

               final Map<String, String> pholders = new HashMap<>(); // Map holding our placeholders

               final Player p = event.getPlayer();
               final String pn = p.getName();

               pholders.put("%player%", pn); // adding placeholders

            String result = placeHolderStr(getMain().getConfig().getString("Chat Format"), pholders);  // This is the format we'll be handling with the placeholders added

            if (p.hasPermission("colored.chat") result = color(result); // add colored prefix/suffix/name if player has permission

           event.setFormat(result); // setting the format

        }


       /**
        * Returns a message translated.
        *
        * @param msg
        *            The message to be translated
        *
        * @return A translated message
        */

        private String color(final String msg) {
           return ChatColor.translateAlternateColorCodes('&', msg);
       }

        /**
        * Inserts placeholders in a string using Java 8.
        *
        * @param str
        *            The string where the placeholders are going in
        * @param map
        *            The map with the placeholders to replace
        *
        * @return The string that has been replaced with placeholders
        *
        */

    private String placeholderStr(String str, final Map<String, String> map) {
           if (map == null)
               return str;
           for (Entry<String, String> entr : map.entrySet()) {
               str = str.replace(entr.getKey(), entr.getValue());
           }
           return str;
       }


     
     
    #4 darklazerog, Aug 13, 2018
    Last edited: Aug 13, 2018
  5. thanks but now i got another problem and can you help me?

    events classhttps://hastebin.com/ufiqovawiv.cs
    COnfigmanager classhttps://hastebin.com/pasuvamafe.java
    plugin.ymlhttps://hastebin.com/tabavemoge.css
    https://hastebin.com/lemaqajone.coffeescript default config

    config isnt updating after i change it. it isnt updating to new one i made as well. it stuck on other one that i made first time. after i updated it from IDEA it still isnt updating
     
  6. Create a new thread and mark this one as solved.