AsyncPlayerChatEvent doesn't work properly

Discussion in 'Plugin Development' started by Funky StylzZz, Apr 16, 2014.

Thread Status:
Not open for further replies.
  1. So i try to learn Java and i looke up up a source from a released little plugin. BTW: This is only for my own purpose i dont want to release it or a kind of thing!

    So i looked up the source from a Plugin called SimpleOPChat. I tried to implement this in my existing plugin. Everything works fine except of the toogle argument.

    In my Main Class I Creeate and load the config and call the commands because the commands are in external classes:

    Main: (This are only the important parts for my question)

    Code:java
    1. public class Main extends JavaPlugin {
    2.  
    3. Logger log = Logger.getLogger("Minecraft");
    4.  
    5.  
    6. @Override
    7. public void onEnable() {
    8. loadConfig();
    9. registerCommands();
    10.  
    11. }
    12.  
    13. private void loadConfig() {
    14. getConfig().addDefault("prefix", ChatColor.DARK_BLUE +"[SupportChat]"+ ChatColor.WHITE);
    15. getConfig().addDefault("enable-supportchat", "true");
    16. getConfig().options().copyDefaults(true);
    17. saveConfig();
    18. }
    19.  
    20. public void registerCommands() {
    21. SupportCommand cSupportCommand = new SupportCommand(this);
    22. getCommand("support").setExecutor(cSupportCommand);
    23. if(getConfig().getBoolean("enable-supportchat")){
    24. SupportChatCommand cSupportChatCommand = new SupportChatCommand(this);
    25. getCommand("sc").setExecutor(cSupportChatCommand);
    26. }
    27. else {
    28.  
    29. }
    30. }


    SupportChatCommand.class:
    Code:java
    1. public class SupportChatCommand implements CommandExecutor, Listener {
    2. public static final Logger logger = Logger.getLogger("Minecraft");
    3.  
    4. private List<String> chatters = new ArrayList<String>();
    5.  
    6. private Main plugin;
    7. public SupportChatCommand(Main plugin) {
    8. this.plugin = plugin;
    9. }
    10.  
    11.  
    12. private boolean canUseAdminChat(CommandSender sender)
    13. {
    14. if ((sender.hasPermission("supportchat.use")) || (sender.isOp())) {
    15. return true;
    16. }
    17. return sender.isOp();
    18. }
    19.  
    20. private String buildMessage(String[] args, int start)
    21. {
    22. StringBuilder msg = new StringBuilder();
    23. for (int i = start; i < args.length; i++)
    24. {
    25. if (i != start) {
    26. msg.append(" ");
    27. }
    28. msg.append(args[I]);[/I]
    29. [I] }[/I]
    30. [I] return msg.toString();[/I]
    31. [I] }[/I]
    32.  
    33. [I] public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)[/I]
    34. [I] {[/I]
    35. [I] String msg = buildMessage(args, 0);[/I]
    36. [I] if (commandLabel.equalsIgnoreCase("sc")) {[/I]
    37. [I] if (canUseAdminChat(sender))[/I]
    38. [I] {[/I]
    39. [I] if (args.length > 0)[/I]
    40. [I] {[/I]
    41. [I] if (args[0].equalsIgnoreCase("toggle"))[/I]
    42. [I] {[/I]
    43. [I] if (!(sender instanceof Player))[/I]
    44. [I] {[/I]
    45. [I] sender.sendMessage("[SimpleOpChat] Console can't use the toggle!");[/I]
    46. [I] return false;[/I]
    47. [I] }[/I]
    48. [I] if (this.chatters.contains(sender.getName()))[/I]
    49. [I] {[/I]
    50. [I] this.chatters.remove(sender.getName());[/I]
    51. [I] sender.sendMessage("[SimpleOpChat] Toggle disabled!");[/I]
    52. [I] }[/I]
    53. [I] else[/I]
    54. [I] {[/I]
    55. [I] this.chatters.add(sender.getName());[/I]
    56. [I] sender.sendMessage("[SimpleOpChat] Toggle enabled!");[/I]
    57. [I] }[/I]
    58. [I] return true;[/I]
    59. [I] }[/I]
    60. [I] sendToChat(msg, sender);[/I]
    61. [I] return true;[/I]
    62. [I] }[/I]
    63. [I] else[/I]
    64. [I] {[/I]
    65. [I] sender.sendMessage(ChatColor.RED + "[SimpleOpChat] Usage: /a <your message>");[/I]
    66. [I] return true;[/I]
    67. [I] }[/I]
    68. [I] }[/I]
    69. [I] else {[/I]
    70. [I] sender.sendMessage(ChatColor.RED + "Permission denied!");[/I]
    71. [I] return true;[/I]
    72. [I] }[/I]
    73. [I] }[/I]
    74. [I] return false;[/I]
    75.  
    76. [I] }[/I]
    77.  
    78. [I] @EventHandler[/I]
    79. [I] public void onPlayerChat(AsyncPlayerChatEvent event)[/I]
    80. [I] {[/I]
    81. [I] if (this.chatters.contains(event.getPlayer().getName()))[/I]
    82. [I] {[/I]
    83. [I] sendToChat(event.getMessage(), event.getPlayer());[/I]
    84. [I] event.setCancelled(true);[/I]
    85. [I] }[/I]
    86. [I] }[/I]
    87.  
    88. [I] @EventHandler[/I]
    89. [I] public void onPlayerQuit(PlayerQuitEvent event)[/I]
    90. [I] {[/I]
    91. [I] String pname = event.getPlayer().getName();[/I]
    92. [I] if (this.chatters.contains(pname)) {[/I]
    93. [I] this.chatters.remove(pname);[/I]
    94. [I] }[/I]
    95. [I] }[/I]
    96.  
    97. [I] private void sendToChat(String msg, CommandSender sender)[/I]
    98. [I] {[/I]
    99. [I] String pname = sender.getName();[/I]
    100. [I] if ((sender instanceof Player)) {[/I]
    101. [I] pname = ((Player)sender).getDisplayName();[/I]
    102. [I] }[/I]
    103. [I] for (Player p : Bukkit.getServer().getOnlinePlayers()) {[/I]
    104. [I] if (canUseAdminChat(p)) {[/I]
    105. [I] p.sendMessage(colorizeString(plugin.getConfig().getString("prefix")) + " <" + ChatColor.RESET + pname + ChatColor.WHITE + "> " + msg);[/I]
    106. [I] }[/I]
    107. [I] }[/I]
    108. [I] logger.info(plugin.getConfig().getString("prefix") + " <" + pname + "> " + msg);[/I]
    109. [I] }[/I]
    110.  
    111.  
    112.  
    113. [I] private String colorizeString(String str)[/I]
    114. [I] {[/I]
    115. [I] return ChatColor.translateAlternateColorCodes('&', str);[/I]
    116. [I] }[/I]
    117. [I] }[/I]


    the /sc command works fine but when i try to write permanently in the normal chat with /sc toggle it shows the message that it successfully toggled but my messages haven't got the prefix and anyone can read them so it seems like the AsyncPlayerChatEvent doesn't work when i issue /sc toggle?
     
  2. Offline

    blablubbabc

    You have to register your listener.
     
    N3rdFall and Asgernohns like this.
Thread Status:
Not open for further replies.

Share This Page