Chat - Making prefix's ?

Discussion in 'Plugin Development' started by ServerfromMinecraft, Aug 11, 2012.

Thread Status:
Not open for further replies.
  1. Hi!

    how i can add a prefix to a player in the chat with the asyncchatevent or other? Without setFormat and changing the displayName() , because if i do that the pex prefix and essential prefix not works :/
     
  2. Offline

    Jogy34

    use setFormat like this:
    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
    //event
     
    event.setFormat("[Prefix]" + event.getFormat());
    
    This makes sure that any other format editing stays in with what you add to it. Setting the EventPriority to highest will ensure that your event happens last incase there are other plugins that also edit the prefix
     
  3. Thanks! :D

    edit:// hm, but how i can make that i show the prefix? if i place the event.format() in a event.setMessage its not work, but how i do it?
     
  4. Offline

    Jogy34

    it should be in an event.setFormat not event.setMessage
     
  5. yes, but if i just make the setFormat, nothing happens :(
     
  6. Offline

    travja

    What I used to do was cancel the event and send it how I wanted... probably not the best way.
     
  7. Offline

    Jogy34

    post your code
     
  8. Yes, but if i make it so, the pex and other prefix's are not sendet, not?

    Code:
    @EventHandler(priority = EventPriority.LOW)
        public void chatevent(AsyncPlayerChatEvent event){
            if (!event.isCancelled()) {
    event.setFormat(ChatColor.BLUE + "["+ "]" + event.getFormat());
                 
                }
                     
                     
            }
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  9. Offline

    travja

    So you want a standalone plugin that will add to the PEX prefixes?? If you want that you will need to explore the PEX API
     
  10. Offline

    Jogy34

    change the event priority to highest. If you have another plugin that changes the format and it happens after yours it will probably over-write it.
     
  11. nope, its change nothing :'(
     
  12. Offline

    Jogy34

    are you sure you are registering the listener?
    Also you don't really need the if(!event.isCancelled())
     
  13. Offline

    Jogy34

    can you post all of your code
     
  14. Ok.
    Here the Listener:

    Code:
    package me.universum.points;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
     
    public class bpl implements Listener {
       
       
       
        Points plugin;
        public bpl(Points plugin) {
            this.plugin = plugin;
        }
       
        @EventHandler(priority = EventPriority.HIGHEST)
        public void chatevent(AsyncPlayerChatEvent event){
            if (!event.isCancelled()) {
                Player p = event.getPlayer();
    event.setFormat(ChatColor.BLUE + "["+ ChatColor.GOLD +ChatColor.BLUE +"]" + event.getFormat());
                   
                }
                       
                       
            }
        }}
    Here the main-class:
    Code:
    package me.universum.points;
     
     
    import java.io.File;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class Points extends JavaPlugin{
       
       
       
       
       
        public void onDisable() {
            System.out.println("[Points] wurde deaktiviert");
        }
       
        public void onEnable() {
            System.out.println("[Points] Erfolgreich aktiviert!");
            getServer().getPluginManager().registerEvents(new bpl(this), this);
           
           
           
     
        }}
     
  15. Offline

    Jogy34

    It works fine for me. What other plugins do you have installed?
     
  16. Uuh xD Many :D

    the current use list (other are not used in moment, because its just my testserver)

    1.worldedit
    2.worldguard
    3.lottery
    4.falsebook
    5.deathmessage
    6.signteleporter
    7.iconomy
    8.chestshop
    9.dynmap
    10.lwc
    11.mobarena
    12.permissionsex
    13.vault
    14.simplesignedit
     
  17. Offline

    Firefly

    Try EventPriority.MONITOR What I've heard from this is that your event will get the latest changed state of the event if you use MONITOR.
     
  18. nope, not working :(
     
  19. Offline

    Jogy34

    I was messing around with it more and came up with this:
    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
    public void chatevent(AsyncPlayerChatEvent event)
    {
    if (!event.isCancelled())
    {
    final Player p = event.getPlayer();
    p.setDisplayName("[]" + p.getDisplayName());
    plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable()
    {
    public void run()
    {
    String name = p.getDisplayName();
    name = name.replace("[]", "");
    p.setDisplayName(name);
    }
    }, 1);
     
    }
    }
    
    I've been told that MONITOR allows you to get the state after it was fired so it allows you to respond to something but not modify the event.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  20. EDIT:// if i use nothing plugin, just my plugin, its works!

    EDIT:// Jogy34 Thaanks!!! with your code its works :D Jey :D

    Ok, next problem :( If i use chatcolors, its override all other chatcolors of my name + text .. But i would only have it on my prefix... its exist a chatcolor "nothing"? so after my prefix i make chatcolornothing and the others chatcolors are invoked..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  21. Offline

    Jogy34

    ChatColor.WHITE will turn it back to how it normally is in the chat
     
  22. Offline

    caseif

    Wouldn't that code change the display name, then change it back?
     
  23. Offline

    Jogy34

    It changes it back after the chat message displays
     
  24. Offline

    caseif

    I see. I tried using this code in my plugin, but for some reason, the chatevent won't fire, even if I change it to onPlayerChat. Any suggestions?
     
  25. Offline

    Jogy34

    Make sure you registered your event and that you have the @EventHandler annotation above the method
     
  26. Offline

    caseif

    Right. I actually realized before I had refreshed the page that I forgot to register the event, so I did, and it works fine now. Now to fix all the other errors...
     
Thread Status:
Not open for further replies.

Share This Page