Util Chatter: A prefix and suffix manager

Discussion in 'Resources' started by PhantomUnicorns, Jun 19, 2017.

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

    PhantomUnicorns

    Ok so I've made a simple chat prefix and suffix manager! All you have to do to use it is add it as a listener in your onEnable

    And to use it, use these two functions!
    Chatter.putPrefix(String permission, String prefix, int priority);
    Chatter.putSuffix(String permission, String suffix, int priority);
    Code:
    Chatter.putPrefix("chat.player", "&7<&6", 1);
    Chatter.putSuffix("chat.player", "&7> &a", 1);
    
    or this one
    Chatter.format(String permission, String format, String playerPlaceHolder, int priority);
    Code:
    Chatter.format("chat.player", "&7<&6%P%&7> &a", "%P%", 1);
    
    (Both code examples give the same outcome!)
    The priority parameter judges which prefix/suffix is put over other prefix/suffix
    You would want player as the highest priority and owner to be the lowest priority
    If you would like no permission, put the permission as ""
    This will generally by used for players or people who just joined the server

    Make sure to add the class to your project and that's it! Here is the class (Relatively small):
    Code:
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    
    public class Chatter implements Listener {
    
        private static Map<String, Root> prefixes;
        private static Map<String, Root> suffixes;
        private static Chatter c;
    
        static {
            prefixes = new HashMap<>();
            suffixes = new HashMap<>();
            c = new Chatter();
        }
    
        public static boolean putPrefix(String permission, String prefix, int priority) {
            String perm = permission.toLowerCase();
            if (prefixes.containsKey(perm)) {
                int oldPriority = prefixes.get(perm).getPriority();
                if (priority < oldPriority) {
                    prefixes.replace(perm, c.new Root(prefix, priority));
                    return true;
                }else {
                    return false;
                }
            } else {
                prefixes.put(permission, c.new Root(prefix, priority));
                return true;
            }
        }
    
        public static boolean putSuffix(String permission, String suffix, int priority) {
            String perm = permission.toLowerCase();
            if (suffixes.containsKey(perm)) {
                int oldPriority = prefixes.get(perm).getPriority();
                if (priority < oldPriority) {
                    suffixes.replace(perm, c.new Root(suffix, priority));
                    return true;
                }else {
                    return false;
                }
            } else {
                suffixes.put(permission, c.new Root(suffix, priority));
                return true;
            }
        }
    
        public static boolean format(String permission, String format, String playerPlaceHolder, int priority) {
            String[] split = format.split(playerPlaceHolder);
            boolean prefix = putPrefix(permission, split[0], priority);
            boolean suffix = putSuffix(permission, split[1], priority);
            if (prefix && suffix) {
                return true;
            }else {
                return false;
            }
        }
    
        @EventHandler
        public void onAsyncPlayerChatEvent(AsyncPlayerChatEvent event) {
            int currentPrefixPriority = 0, currentSuffixPriority = 0;
    
            String prefix = "";
            for (String perm : prefixes.keySet()) {
                if (perm.equals("") || event.getPlayer().hasPermission(perm)) {
                    if (prefixes.get(perm).getPriority() < currentPrefixPriority) {
                        prefix = prefixes.get(perm).getRoot();
                        currentPrefixPriority = prefixes.get(perm).getPriority();
                    }
                }
            }
    
            String suffix = "";
            for (String perm : suffixes.keySet()) {
                if (perm.equals("") || event.getPlayer().hasPermission(perm)) {
                    if (suffixes.get(perm).getPriority() < currentSuffixPriority) {
                        suffix = suffixes.get(perm).getRoot();
                        currentSuffixPriority = suffixes.get(perm).getPriority();
                    }
                }
            }
    
            if (!prefix.equals("") || !suffix.equals("")) {
                event.setFormat(ChatColor.translateAlternateColorCodes('&', prefix + "%s"+ suffix + "%s"));
            }
        }
    
        private class Root {
    
            private String root;
            private int priority;
    
            private Root(String root, int priority) {
                this.root = root;
                this.priority = priority;
            }
    
            public String getRoot() {
                return root;
            }
    
            public int getPriority() {
                return priority;
            }
    
        }
    }
    
    And if this gets enough likes/views I'll make a resource to do a JSON chat formatter to add upgraded text to your plugin!
    With it you can make a chat plugin just by putting this in your onEnable()!!!!
    Code:
    Chatter.format("", "&f[&7Player&f] &6&o%P%&r&f&l> &r&a", "%P%", 6);
    Chatter.format("chat.helper", "&f[&dHelper&f] &6&o%P%&r&f&l> &r&a", "%P%", 5);
    Chatter.format("chat.builder", "&f[&6Builder&f] &6&o%P%&r&f&l> &r&a", "%P%", 4);
    Chatter.format("chat.mod", "&f[&bModerator&f] &6&o%P%&r&f&l> &r&a", "%P%", 3);
    Chatter.format("chat.admin", "&f[&cAdmin&f] &6&o%P%&r&f&l> &r&a", "%P%", 2);
    Chatter.format("chat.owner", "&f[&4&lOwner&f] &6&o%P%&r&f&l> &r&a", "%P%", 1);
    
    Change log:
    • Fixed small error which gave priority to the wrong one when setting a new permission
    • Flipped priority so the lower the number the higher the rank
    • Added changing message support!
     
    Last edited: Jun 28, 2017
    MCMastery likes this.
  2. Offline

    Horsey

    But.. but.. Vault?
     
    timtower likes this.
  3. Offline

    timtower Moderator Moderator

    @PhantomUnicorns You pretty much wrote a chat plugin where you can't set the message anymore.
    setFormat should take "%s" twice, first one gets replaced with the displayname, second one gets replaced with the message.
    No way to set a global format either that is independent from the suffixes.
     
  4. Offline

    PhantomUnicorns

  5. Offline

    timtower Moderator Moderator

    @PhantomUnicorns Now you have, still stuck with the fixed format though.
    And Vault has a getPrefix and getSuffix which will get retreived from the permissions plugin I believe.
     
  6. Offline

    Horsey

  7. Offline

    PhantomUnicorns

    So you're telling me that instead of using this one class for a quick prefix and suffix manager, I should download vault and a permission plugin with a prefix and suffix manager and customize the configuration file with all the groups and permissions? It seems like if you want a quick prefix and suffix manager, this is the way to go @Horsey

    @timtower It's suppose to be fixed, would you recommend I add something like tags that are just put on players with the right permissions? For something like that, might as well make a plugin. This util is only for the beginning of a chat formater, I wasn't going to post the code for a plugin
     
  8. Offline

    Horsey

    @PhantomUnicorns if you find a server that doesn't have vault and a permission plugin (not gonna happen) then feel free to use this.
     
    timtower likes this.
  9. Offline

    MattTheBeast

    @PhantomUnicorns

    Nice util, Il be using this for my servers, its very simple and good. I only use my plugins when I make a server, no vault :) @Horsey
     
Thread Status:
Not open for further replies.

Share This Page