help needed with AsyncPlayerChatEvent

Discussion in 'Plugin Development' started by Beno65Dev, Jun 11, 2014.

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

    Beno65Dev

    I have a problem I want to make prefixes with its own plugin and permissions but if I give permission that I'm right all other prefixes to allow me to name

    here is the code:

    Code:java
    1. package me.beno65dev.chat;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.AsyncPlayerChatEvent;
    9.  
    10. public class Chat implements Listener{
    11.  
    12. @EventHandler
    13. public void onChat(AsyncPlayerChatEvent e){
    14. Player p = e.getPlayer();
    15. String name = p.getName();
    16. String message = e.getMessage();
    17. e.setCancelled(true);
    18. StringBuilder sb = new StringBuilder();
    19. if (p.hasPermission("chat.member") && !p.isOp()){
    20. sb.append(ChatColor.GRAY + "Member " + ChatColor.WHITE + name + ": " + message);
    21. if (p.hasPermission("chat.elite") && !p.isOp()){
    22. sb.append(ChatColor.DARK_GREEN + "Elite" + name + ChatColor.WHITE +": " + message);
    23. }
    24. if (p.hasPermission("chat.vip") && !p.isOp()){
    25. sb.append(ChatColor.GOLD + "VIP" + name + ChatColor.WHITE + ": " + message);
    26. }
    27. if (p.hasPermission("chat.team") && p.isOp()){
    28. sb.append(ChatColor.AQUA + "Team" + name + ChatColor.WHITE + ": " + message);
    29. }
    30. }
    31. Bukkit.broadcastMessage(sb.toString());
    32. }
    33. }


    i hope you can help me
     
  2. What is being asked is unclear, but I'm going to take a guess by looking at your code. It looks like if a player has "chat.vip" for example, he will also have all the prefixes before it. This can easily be solved by using "else if" statements, instead of using just "if" statements. The changed code should look like this.
    Code:java
    1. if (p.hasPermission("chat.member") && !p.isOp()){
    2. sb.append(ChatColor.GRAY + "Member " + ChatColor.WHITE + name + ": " + message);
    3. }else if (p.hasPermission("chat.elite") && !p.isOp()){
    4. sb.append(ChatColor.DARK_GREEN + "Elite" + name + ChatColor.WHITE +": " + message);
    5. }else if (p.hasPermission("chat.vip") && !p.isOp()){
    6. sb.append(ChatColor.GOLD + "VIP" + name + ChatColor.WHITE + ": " + message);
    7. }else if (p.hasPermission("chat.team") && p.isOp()){
    8. sb.append(ChatColor.AQUA + "Team" + name + ChatColor.WHITE + ": " + message);
    9. }
     
  3. Offline

    mine-care

    What you wana do?
     
  4. Offline

    Beno65Dev

    mine-care
    its for a server that a friend of my have made
     
  5. AndrewAnderVille That would only be the case if they have more than one of the permissions listed.

    Beno65Dev You don't really explain what's wrong but you don't really seem to be using StringBuilder correctly. Generally it's used as opposed to concatenation, but you concatenate Strings inside the append statement. It's pretty pointless the way you're doing it.
     
  6. Offline

    ever_x

    You might want event.setMessage(messageString), or event.setFormat(formatString). I wouldn't know, as the question is very unclear!
     
  7. Offline

    mine-care

    ever_x that will override the existing ones like group manager ranks won't show prefixes or suffixes... Mabe getdisplayname plus the custom name thingy if you want both ;)
     
  8. Offline

    ever_x

    mine-care It wont override stuff if you use the monitor event priority ( so your listener gets called last, I think - you may want to check on JavaDocs), then use e.setFormat/Message(e.getFormat / message then your new stuff). That way you can add formats and keep the existing stuff from other plugins. I haven't done this myself, so I cant give you code, but hey, why would I?
     
  9. Offline

    fireblast709

    It will still override stuff, but it is not ment to do that in MONITOR (pick from LOWEST-HIGHEST instead)
    Beno65Dev If you are op you won't get any prefix. Instead, use plugin.yml to set op perms as default false and remove the !player.isOp()
    Code:
    permissions:
        chat.member:
            default: false
        chat.vip:
            default: false
        chat.elite:
            default: false
        chat.team:
            default: false
    (Something to read)
     
Thread Status:
Not open for further replies.

Share This Page