Adding a prefix

Discussion in 'Spigot Plugin Development' started by craterhater, May 28, 2017.

  1. Hey there!

    How would I ADD a prefix, not overwrite all prefixes that already exist and set my own?

    Thanks!
     
  2. explain more.
     
  3. For example:

    Code (Text):
    @EventHandler
        public void say(AsyncPlayerChatEvent event){
            Player p = event.getPlayer();

                    p.setDisplayName(c + " ["+prefix+"] " + ChatColor.RESET + p.getName());
            }
    This adds a prefix but when the player already has a prefix because of other plugins it overwrites them. How can I prevent that?
     
  4. Mh... i think i would do this with the code here... ahm.. maybe its wrong.
    Code (Text):

        Player player = (Player) e.getWhoClicked();
         String[] prefixes = {"[ADMIN]", "[MOD]", "[USER]"};
         for(String s : prefixes){
           player.setDisplayName(player.getDisplayName().replace(s, ""));
         }
         player.setDisplayName(prefixes[0] + player.getDisplayName());
     
    Problem: You have to store you prefixes in a array... but i think thats not a big problem.
     
  5. Do I have to store MY prefixes in an array or the prefixes you want to keep?
     
  6. hook into vault and get the prefix.
     
  7. If you want to remove your used prefixes when you change the prefix for somebody than yes... you have to add the prefix to the array.
    If you just want to add your prefix and ignore other prefixes from your plugin than you can just do

    p.setDisplayName(prefix + p.getDisplayName());
     
  8. Is it possible without hooking into anything?
     
  9. You could get the format and then add a prefix in front?

    Like this
    Code (Text):
    e.setFormat("(prefixie) " + e.getFormat());
     
    • Like Like x 1
  10. I want to keep existing prefixes
     
  11. This keeps existing prefixes. Those prefixes are stored in the format. As long as your plugin has the highest EventPriority it will get those from the format and will add yours in front.
     
  12. oh wait you meant only chat? lol ya then use @42Jamie42 's suggestion