[Tutorial] Chat management (muting, etc)

Discussion in 'Resources' started by ItsLeoFTW, Mar 30, 2014.

?

Was this helpful?

  1. Yes

    6 vote(s)
    60.0%
  2. No

    4 vote(s)
    40.0%
Thread Status:
Not open for further replies.
  1. Offline

    ItsLeoFTW

    Hello there,

    This tutorial will be about muting players from speaking, and blocking all caps messages.
    NOTES:
    • Make sure to register the command in the plugin.yml file!
    • Make sure to create a configuration file.
    • When the plugin loads, the configuration will be empty. To make the file have something in it: De-op yourself, then type /mute <your name here> in the console. The config file will update, and if you reload the server and try to speak, you won't be able to, ONLY if you are muted. Then to unmute yourself, just type /mute <your name here> again.
    UPCOMING:
    - Censoring words
    - Keep players from spamming symbols such as $, %, etc
    - Chat cooldown


    [1]: Muting
    You will first need a class that:
    - Extends the JavaPlugin class
    - Implements the Listener class
    - Implements the CommandExecutor class

    Once you have that, let's get started.

    First off, the mute command. I will paste the code for it and add comments to help you understand it better.

    CODE:
    Code:java
    1. @Override
    2. public boolean onCommand(org.bukkit.command.CommandSender s, org.bukkit.command.Command cmnd, String alias, String[] args) {
    3. org.bukkit.command.CommandSender p = s;
    4. if (alias.equalsIgnoreCase("mute")) {
    5. if (args.length < 1) {
    6. s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&cUsage: /mute <player>"));
    7. } else if (args.length == 1) {
    8. String name = args[0];//The name is the first argument
    9. org.bukkit.entity.Player target = org.bukkit.Bukkit.getPlayer(name);//Get the player by the name of the first argument
    10.  
    11. if (target != null && !target.isOp() && !target.getName().equals(s.getName())) {//Check to see if the target: A- Exists, B- Is not an op, C- Their name does not equal the name of the sender
    12. if (!muted.containsKey(target)) {
    13. muted.put(target, target);//We don't really need this, just there to be safe.
    14. s.sendMessage(ChatColor.GREEN + "You have muted " + args[0] + ".");//Muted the player
    15. target.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6You have been muted by &c" + p.getName()));
    16. getConfig().set("Players." + target.getName() + ".Muted", true);//Edit the boolean "Muted" in the key with the player's name and set it to true
    17. saveConfig();//Save the config
    18. } else {
    19. muted.remove(target);//We don't really need this, just there to be safe.
    20. s.sendMessage(ChatColor.GREEN + "You have unmuted " + args[0] + ".");//Unmuted a player
    21. target.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6You have been unmuted by &c" + p.getName()));
    22. getConfig().set("Players." + target.getName() + ".Muted", false);//Edit the boolean "Muted" in the key with the player's name and set it to false
    23. saveConfig();//Save the config
    24.  
    25. }
    26.  
    27. } else {
    28. if (target != null && target.isOp()){
    29. s.sendMessage(ChatColor.RED + "You may not mute operators.");
    30. }
    31. if (target != null &&target.getName().equals(s.getName())){
    32. s.sendMessage(ChatColor.RED + "You may not mute yourself.");
    33. }
    34. }
    35. return true;
    36. }
    37. return true;
    38. }
    39. return true;
    40. }



    Next up, the listener.
    Code:java
    1. @EventHandler//Good ol' @EventHandler annotation
    2. public void onPlayerChat(org.bukkit.event.player.AsyncPlayerChatEvent e) {//Listens for the chat event
    3. org.bukkit.entity.Player p = e.getPlayer();//gets the player
    4. if (getConfig().getBoolean("Players." + p.getName() + ".Muted")) {//checks the boolean "Muted" in the key with the player's name
    5. e.setCancelled(true);//Cancels the event
    6. p.sendMessage(org.bukkit.ChatColor.RED + "You may not talk while muted.");//Pretty obvious what this does
    7. }
    8.  
    9. }

    And finally, the onEnable method:

    Code:java
    1. public HashMap<Player, Player> muted = new HashMap<Player, Player>(); //Unneeded hashmap, it's there from a while ago
    2.  
    3. @Override
    4. public void onEnable() {
    5. org.bukkit.plugin.PluginManager pm = org.bukkit.Bukkit.getPluginManager();//Get the pluginmanager
    6. pm.registerEvents(this, this);//Register events
    7. this.saveDefaultConfig();//Save the default config
    8. getCommand("mute").setExecutor(this);//Set the executor of the command "/mute" to this class
    9. String MUTE_PERM_MESSAGE = "[Mute] You don't have permission.";//permission message
    10. getCommand("mute").setPermissionMessage(org.bukkit.ChatColor.RED + MUTE_PERM_MESSAGE);//set the permission message
    11. getCommand("mute").setPermission("chattools.mute");//set the permission
    12. pm.addPermission(new org.bukkit.permissions.Permission("chattools.mute"));//add the permission
    13. pm.getPermission("chattools.mute").setDefault(PermissionDefault.OP);//set the "default" property for the permission to operators only
    14. saveConfig();//save the config
    15. }

    [2] Blocking all caps
    You can put this code in the event handler for the AsyncPlayerChatEvent from above.

    The code is:
    Code:java
    1. String m = e.getMessage();
    2. if (m.toUpperCase().equals(m)){
    3. e.setCancelled(true);
    4. p.sendMessage(org.bukkit.ChatColor.RED + "You may not talk in all caps.");
    5. }

    So now the event handler will look like:
    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerChat(org.bukkit.event.player.AsyncPlayerChatEvent e) {
    4. org.bukkit.entity.Player p = e.getPlayer();
    5. String m = e.getMessage();
    6.  
    7. if (m.toUpperCase().equals(m)) {
    8. e.setCancelled(true);
    9. p.sendMessage(org.bukkit.ChatColor.RED + "You may not talk in all caps.");
    10. }
    11. if (getConfig().getBoolean("Players." + p.getName() + ".Muted")) {
    12. e.setCancelled(true);
    13. p.sendMessage(org.bukkit.ChatColor.RED + "You may not talk while muted.");
    14. }
    15.  
    16. }
    17.  


    I hope this helps!
     
    mickedplay likes this.
  2. Offline

    Org

    Seems like a great tut! :)

    Show Spoiler

    Got a message saying I was tagged in this post. Am I missing something? :confused:
     
  3. Offline

    ItsLeoFTW

    Oh yeah I was using the org.bukkit.event.EventHandler in the annotation, so it tagged you.

    Show Spoiler

    never knew that there was an actual user named "Org" :p
     
  4. Offline

    ItsLeoFTW

    Bump, hope this is useful!
     
  5. Offline

    iiHeroo

    I don't see why you're using a HashMap, when you could be doing an ArrayList, not to mention, you should be doing UUID's since name changing will happen in 1.8

    Also, why use a HashMap when you're storing it in the config.yml?
     
  6. Offline

    iarezach

    why do you use a HashMap and a config? In the end, you just end up using the config.

    I tried to clean up some of your code, changed a few variables here and there. I think you had a right idea, but you over-did it a bit and had a lot of messy / unused code.

    (I only did the first class, the event for it seems well and I'm too tired to help you out on the caps one)
    Code:java
    1.  
    2. import org.bukkit.Bukkit;
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.configuration.Configuration;
    5. import org.bukkit.entity.Player;
    6.  
    7. public class Snippet {
    8. public boolean onCommand(org.bukkit.command.CommandSender s, org.bukkit.command.Command cmnd, String alias, String[] args) {
    9. Player player = (Player) s;
    10. if (alias.equalsIgnoreCase("mute")) {
    11. if (args.length < 1) {
    12.  
    13. player.sendMessage(ChatColor.RED + "Usage: /mute <player>");
    14. } else if (args.length == 1) {
    15. String name = args[0];
    16. Player target = Bukkit.getPlayer(name);
    17.  
    18. if(target != null){
    19. if (!target.isOp() && !target.getName().equals(player.getName())) {
    20. if (getConfig().get("Players." + target.getName() + ".Muted") == null || getConfig().getBoolean("Players." + target.getName() + ".Muted") == false) {
    21. player.sendMessage(ChatColor.GREEN + "You have muted " + args[0] + ".");
    22. target.sendMessage(ChatColor.RED + "You have been muted by " + player.getName());
    23. getConfig().set("Players." + target.getName() + ".Muted", true);
    24. saveConfig();
    25. } else {
    26. player.sendMessage(ChatColor.GREEN + "You have unmuted " + args[0] + ".");
    27. target.sendMessage(ChatColor.GREEN + "You have been unmuted by " + player.getName());
    28. getConfig().set("Players." + target.getName() + ".Muted", false);
    29. saveConfig();
    30.  
    31. }
    32.  
    33. } else {
    34. if (target.isOp()){
    35. player.sendMessage(ChatColor.RED + "You may not mute operators.");
    36. }
    37. if (target.getName().equals(s.getName())){
    38. player.sendMessage(ChatColor.RED + "You may not mute yourself.");
    39. }
    40. }
    41. }else{
    42. player.sendMessage(ChatColor.RED + "The specified player is offline or does not exist");
    43. }
    44. }
    45. }
    46. return false;
    47. }
    48. }
     
    Benlewis9000 likes this.
  7. Offline

    ItsLeoFTW

    Thanks I'll try that later, I just use HashMaps for now. I'll change that when I have time
     
  8. Offline

    RawCode

    start from learning about code formatting and usage of code tags.

    hardcoded string constants also bad move.
     
  9. Offline

    ItsLeoFTW

    I literally have no idea what you just said... anyway, I have my way of doing things. I used the code tags correctly as I can see...
     
  10. Offline

    Garris0n

    The formatting breaks when you edit with java syntax.

    And not knowing what "hardcoded string constants" means but at the same time making a tutorial seems like a bad combination, to be quite honest.
     
    LegitJava and DevRosemberg like this.
Thread Status:
Not open for further replies.

Share This Page