Calling a SPECIFIC event from running a command?

Discussion in 'Plugin Development' started by webbhead, Jul 13, 2014.

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

    webbhead

    What I need help with is, I need something where when you use a command it stops the flow of player chatting but I cannot figure out how to fire the event on the command sending.
    I am trying to call it on Line 37. and the event starts on Line 42 - 46.
    Here is the code:
    Code:java
    1. package me.webbhead.CommandMode;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.Event;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.AsyncPlayerChatEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class commandmode extends JavaPlugin implements Listener {
    15.  
    16. String error = ChatColor.BLACK + "[" + ChatColor.DARK_RED + "Error" + ChatColor.BLACK + "] ";
    17.  
    18. public void onEnable() {
    19. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    20. }
    21.  
    22. public void onDisable() {
    23.  
    24. }
    25.  
    26. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[]) {
    27. if (!(sender instanceof Player)) {
    28. sender.sendMessage("This command is only for Players.");
    29. return true;
    30. }
    31. if (cmd.getName().equalsIgnoreCase("cm")) {
    32. if (!(sender.hasPermission("cm.use"))) {
    33. sender.sendMessage(error + ChatColor.RED + "You do not have permission to use that.");
    34. return true;
    35. }
    36. if (args[0].equalsIgnoreCase("on")) {
    37. Bukkit.getServer().getPluginManager().callEvent(new onPlayerChat());
    38. }
    39. }
    40. return true;
    41. }
    42. @EventHandler
    43. public void onPlayerChat(AsyncPlayerChatEvent e) {
    44. Player player = e.getPlayer();
    45. Player target = Bukkit.getServer().getPlayer(player.getName());
    46. e.getRecipients().remove(target);
    47. }
    48. }
    49.  
     
  2. Offline

    teej107

    These are the exact same thing. You don't need 2 different variables with the same value.
    Are you trying to disable chat viewing for everybody or just one person?
    Also by the way you are trying to disable the chat, I would recommend looking up on how events actually work/get fired.
     
  3. Offline

    ResultStatic

    webbhead how have you posted 3 plugins and still have no clue what your doing???

    how would this ever
    Code:
     if (args[0].equalsIgnoreCase("on")) {
                    Bukkit.getServer().getPluginManager().callEvent(new onPlayerChat());
                }
    re-enable chat. im not trying to be rude but really?

    this is how i would do it.
    set a boolean when u want the chat to be stopped (if you want it to globally stop chat; not sure what you exactly mean, if you want to cancel the chat per player use a hashmap)

    so
    Code:
    boolean canChat = true
    
    then listen on the chat event, which is per player btw, then do

    Code:
    e.setCancelled(canChat);
    then you can set the boolean true/false with the command

    JBoss925 not that it really matters but you should use AsyncPlayerChatEvent instead of the old as that one is deprecated, and you can just do
    Code:
    e.setCancelled(disabled);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Offline

    webbhead

    I am new to bukkit coding I have posted easy plugins and two I have helped with.

    What I am trying to do is that the person who types the command it disables chat so nothing can come through but server messages. Not player chat.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. Offline

    JBoss925

    Very easy.

    Create a global boolean called disabled:
    Code:java
    1. boolean disabled = false;

    Now do the on command stuff:
    Code:java
    1. //onCommand whatever
    2. if(args[0].equalsIgnoreCase("on"){
    3. disabled = false;
    4. }
    5. if(args[0].equalsIgnoreCase("off"){
    6. disabled = true;
    7. }

    Now get the asynch player chat event and set the cancelled value to disabled.
    Code:java
    1. @EventHandler
    2. public void onChat(AsynchPlayerChatEvent e){
    3. e.setCancelled(disabled);
    4. }
     
  6. Offline

    webbhead

    JBoss925
    What I want to do is to disable it for one single person. I have been trying for a while and cannot get it working here is the code so far:
    Code:java
    1. package me.webbhead.CommandMode;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.player.AsyncPlayerChatEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class commandmode extends JavaPlugin implements Listener {
    16.  
    17.  
    18. ArrayList<Player> on = new ArrayList<Player>();
    19. ArrayList<Player> off = new ArrayList<Player>();
    20. String prefix = ChatColor.BLACK + "[" + ChatColor.DARK_RED + "CommandMode" + ChatColor.BLACK + "] ";
    21. String error = ChatColor.BLACK + "[" + ChatColor.DARK_RED + "Error" + ChatColor.BLACK + "] ";
    22.  
    23. public void onEnable() {
    24. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    25. }
    26.  
    27. public void onDisable() {
    28.  
    29. }
    30.  
    31. @EventHandler
    32. public void onPlayerChat(AsyncPlayerChatEvent e) {
    33. for (Player players : Bukkit.getServer().getOnlinePlayers()) {
    34. if (on.contains(e.getPlayer())) {
    35. e.getRecipients().remove(players);
    36. e.getRecipients().add(e.getPlayer());
    37. }
    38. if (off.contains(e.getPlayer())) {
    39. e.getRecipients().add(players);
    40. }
    41. return;
    42. }
    43. }
    44.  
    45. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[]) {
    46. if (!(sender instanceof Player)) {
    47. sender.sendMessage("This command is only for Players.");
    48. return true;
    49. }
    50. Player player = (Player) sender;
    51. if (cmd.getName().equalsIgnoreCase("mode")) {
    52. if (!(sender.hasPermission("mode.use"))) {
    53. sender.sendMessage(error + ChatColor.RED + "You do not have permission to use that.");
    54. return true;
    55. }
    56. if (args.length == 0) {
    57. sender.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "CommandMode" + ChatColor.BLACK + "]");
    58. sender.sendMessage(ChatColor.DARK_GREEN + "/mode on | Turn off the chat.");
    59. sender.sendMessage(ChatColor.DARK_RED + "/mode off | Turn on the chat.");
    60. sender.sendMessage(ChatColor.YELLOW + "/mode help | Get the help menu.");
    61. sender.sendMessage(ChatColor.DARK_RED + "Developer: webbhead | Version: v1.0");
    62. return true;
    63. }
    64. if (args[0].equalsIgnoreCase("on")) {
    65. sender.sendMessage(prefix + ChatColor.RED + "CommandMode is now turned " + ChatColor.DARK_GREEN + "on" + ChatColor.RED + ".");
    66. on.add(player);
    67. return true;
    68. }
    69. if (args[0].equalsIgnoreCase("off")) {
    70. on.remove(player);
    71. off.add(player);
    72. sender.sendMessage(prefix + ChatColor.RED + "CommandMode is now turned " + ChatColor.DARK_RED + "off" + ChatColor.RED + ".");
    73. return true;
    74. }
    75. if (args[0].equalsIgnoreCase("help")) {
    76. sender.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "CommandMode" + ChatColor.BLACK + "]");
    77. sender.sendMessage(ChatColor.DARK_GREEN + "/mode on | Turn off the chat.");
    78. sender.sendMessage(ChatColor.DARK_RED + "/mode off | Turn on the chat.");
    79. sender.sendMessage(ChatColor.YELLOW + "/mode help | Get the help menu.");
    80. sender.sendMessage(ChatColor.DARK_RED + "Developer: webbhead | Version: v1.0");
    81. return true;
    82. }
    83. }
    84. return true;
    85. }
    86. }
    87.  


    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  7. Offline

    Traks

    1. Please include the Override annotation when overriding methods, it will help you prevent thinking you overrode a method when you actually didn't and tells other people the default implementation/definition can be found in a super class.

    2. There's no need for Bukkit.getServer(), just invoke the method directly from the Bukkit class.

    3. Why include an onDisable() implementation if it's empty and the super class' one is also?

    4. You don't need a list for players who have turned chat off and those who haven't. Just check if one contains or doesn't contain a specific player.

    5. The AsyncPlayerChatEvent listener method contains excessive code and as a whole, doesn't make much sense, looking at its purpose. Get an Iterator of the Set returned by AsyncPlayerChatEvent#getRecipients() instead and remove the players that are contained by the 'on' list, or aren't contained by the 'off list'.

    6. Store player UUIDs instead of the Player object itself and handle accordingly.

    7. Instead of returning true at the end of every if-block that checks 'args[0]', use else if. It will look much neater that way in my opinion.

    8. I recommend creating a separate method for the help menu as it is easier to manage it that way and more clear. You could also use the new line character (\n) instead of sending a new message, to avoid chat messages popping up between help menu messages. It also reduces the amount of packets being sent to the client.
     
  8. Offline

    webbhead

    Bump

    I still cannot get it to work.

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

    webbhead

  10. Offline

    LCastr0

    Create a List<Player>, add the player there when he runs the command, then in the chat event do this:
    Code:java
    1. Stirng msg = event.getMessage();
    2. for(Player online : Bukkit.getOnlinePlayers()){
    3. if(players.contains(online))
    4. online.sendMessage(msg);
    5. }
    6. event.setCancelled(true);

    (P.S.: The "players" is the name of your list.)
     
Thread Status:
Not open for further replies.

Share This Page