Mutting Players?

Discussion in 'Spigot Plugin Development' started by ShishKaBobz, Apr 17, 2017.

  1. Hey! I have never tried this before, I was following a tutorial on bukkit, First of all, Would this even be right code?

    Command Class:
    Code (Text):
    package com.ionmc.ban;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class GUI implements CommandExecutor {

        public boolean onCommand(CommandSender sender, Command command,
                String label, String[] args) {
            if(label.equalsIgnoreCase("Punish")){
                 if (args.length != 2) {
                        sender.sendMessage("§c§lBanGUI §7§l>> §c§l/Punish <PlayerName> <mute | ban>");
                        return true;
            }
            if(!(sender instanceof Player)) {
                sender.sendMessage("You Must be a player to use this command!");
                return false;          
            }
         
            }      
            Player player = (Player) sender;
         
            if(!sender.hasPermission("punish.use")){
                sender.sendMessage("§c§lNo permission!");
                return true;
            }
         
            Player otherPlayer = Bukkit.getPlayer(args[0]);
            String otherPlayerName = otherPlayer.getName();

            if (args[1].equalsIgnoreCase("mute")){
               
                player.sendMessage(otherPlayerName + "§2 Has Been Muted!");
                Bukkit.broadcastMessage(otherPlayerName + "§2 Has Been Muted By " + player.getName());
            }
         
            return true;
            }

    }

     
    Events Class:
    Code (Text):
    package com.ionmc.ban;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    public class Events implements Listener {
     
        private Main plugin;
     
        public Events(Main pl){
            plugin = pl;
         
    }
        List<String> bannedFromChat = new ArrayList<String>();
     
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event){
            Player p = event.getPlayer();
            for (String s : bannedFromChat) {
                if (bannedFromChat.contains(p.getName())) {
                    event.setCancelled(true);
                    p.sendMessage(ChatColor.RED + "You have been muted. You can not chat.");
                }
            }
        }

    }
     
    Second off all, How can i make it so i can use
    bannedFromChat.add(args[0]);
    or
    bannedFromChat.remove(args[0]);
    In my command class? Im not very good with events, I was also following this tutorial
    https://bukkit.org/threads/how-to-mute-a-player-from-chat.85111/
    Thanks for the Help In advance!
     
  2. You would need to create an instance of your event class in your main class to access your bannedFromChat list. And did you even run that code?
     
  3. What are you talking about? What capitalization? There's no point to save the player... it takes up more space when all you need is the players name which is a string.

    Clearly you do not have an understanding of Java :p
     
  4. Mas

    Mas

    Out of interest, why are you restricting this command to players only? There isn't a real reason the console should be prohibited from using the command in this case.

    Also, instead of using the section symbol (§) for chat colours, instead use the ChatColor enum or ChatColor.translateAlternateColorCodes('&', message).

    To mute the player, you would get an instance of the 'Events' class and make a method to add a player to the list of muted players. Also, you should store the player's UUID instead of their name.
     
  5. Hey! Thanks so much, But as i said im fairly new to code and i really dont know much about creating instances.. Would you mind giving me an example? Thanks!
     
  6. Yes i know i copy and paste my code from other project, Its outdated Ill fix it :D
     
  7. Is there really a point to save the UUID instead of the players name? I know they're both Strings but does it really matter for something like disabling chat?
     
  8. It always matters.
     
  9. Code (Text):
    Events eventClass = new Events();
     
  10. Mas

    Mas

    The player UUID is stored as a java.util.UUID, not a String :p What would be the point in that?
    You should use UUIDs because at some point you are going to want to save the muted players over server-restart using a config, in which case you will realise player names can be changed, so you are better to start with UUIDs now. Also, they are faster to compare than Strings IIRC.

    If you read their post you'd see their Events class takes 'Main' in its constructor. Also, it's better in the long-term if you don't spoonfeed, then the OP is made to actually learn things instead of copying it without understanding.

    If you can't do this you will struggle with everything else you try and do. Learn basic Java and you will become a much better developer. Also, it seems you are just copy and pasting code without understanding it, which is never a good thing to do.

    Java tutorial home: https://docs.oracle.com/javase/tutorial/
    Constructors: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
    Objects and classes: https://docs.oracle.com/javase/tutorial/java/javaOO/
    Creating instances of classes: https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
     
  11. Mas

    Mas

    Read my post.

    You should use UUIDs because at some point you are going to want to save the muted players over server-restart using a config, in which case you will realise player names can be changed, so you are better to start with UUIDs now. Also, they are faster to compare than Strings IIRC.

    Also UUIDs take up less space than Strings. Do your research before arguing with facts :p
     
    #11 Mas, Apr 17, 2017
    Last edited: Apr 17, 2017
    • Agree Agree x 1
  12. First you wanna store the player's uuid in an ArrayList
    Code (Text):
    ArrayList<UUID> muted = new ArrayList<>();
    Then you wanna add the player once you mute them
    Code (Text):
    //I'm on phone, so I'm gonna improvise code a bit

    if(cmd = command) {
    Player target = Bukkit.getOnlinePlayers();
    UUID uuid = target.getUniqueID();
    muted.add(UUID);
    target.send Message("you have been muted");
     
    Then for your event you're gonna wanna do this:
    Code (Text):
    public void onPlayerChat(AsyncPlayerChatEvent event) {
    if (muted.contains(event.getPlayer().getUniqueID)) {
    event.setCancelled(true);
    event.getPlayer.send Message("you are muted");
     
     
  13. So, apparently in Java 8 UUIDs do indeed take up less space (this changes in Java 9 apparently. Thanks, Twitch chat!) - Although I'd hesitate to say comparison would be faster than strings if you're using hashCode(). I could be wrong, though.
     
  14. You put this code in command code
    Code (Text):
    package com.ionmc.ban;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class GUI implements CommandExecutor {
        List<String> bannedFromChat = new ArrayList<String>();
        public List<String> Get(){
             return bannedFromChat;
        }
       
        public boolean onCommand(CommandSender sender, Command command,
                String label, String[] args) {
            if(label.equalsIgnoreCase("Punish")){
                 if (args.length != 2) {
                        sender.sendMessage("§c§lBanGUI §7§l>> §c§l/Punish <PlayerName> <mute | ban>");
                        return true;
            }
            if(!(sender instanceof Player)) {
                sender.sendMessage("You Must be a player to use this command!");
                return false;        
            }
         
            }    
            Player player = (Player) sender;
         
            if(!sender.hasPermission("punish.use")){
                sender.sendMessage("§c§lNo permission!");
                return true;
            }
         
            Player otherPlayer = Bukkit.getPlayer(args[0]);
            String otherPlayerName = otherPlayer.getName();

            if (args[1].equalsIgnoreCase("mute")){
               bannedFromChat.add(args[0]);//args[0] need to be the player name
                player.sendMessage(otherPlayerName + "§2 Has Been Muted!");
                Bukkit.broadcastMessage(otherPlayerName + "§2 Has Been Muted By " + player.getName());
            }
         
            return true;
            }
     
    And in the events class:
    Code (Text):
    public class Events implements Listener {
        public GUI gui = new GUI();
        private Main plugin;
     
        public Events(Main pl){
            plugin = pl;
         
    }
        List<String> bannedFromChat = gui.Get();
     
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent event){
            Player p = event.getPlayer();
            for (String s : bannedFromChat) {
                if (bannedFromChat.contains(p.getName())) {
                    event.setCancelled(true);
                    p.sendMessage(ChatColor.RED + "You have been muted. You can not chat.");
                }
            }
        }

    }
     
     
    • Agree Agree x 1
  15. Mas

    Mas

    Damn, that sucks.
    Where does Twitch chat come into this? :eek:

    EDIT: You're right, comparing strings is faster: https://puu.sh/vnmsI/ccd821cb8c.png
     
    #15 Mas, Apr 17, 2017
    Last edited: Apr 17, 2017
  16. Dev stream I had going on in which a fellow dev told me about UUID storage. Currently the library I've built has a Registry class that uses Strings as keys and they were describing the benefits of storing UUIDs as a whole instead of storing the strings themselves. Would force me to re-write a fair amount and the memory benefit is minimal.
     
  17. Mas

    Mas

    Ah, ok.

    You were right about strings being faster to compare, thanks!
    https://puu.sh/vnmsI/ccd821cb8c.png