Solved /me forbid for muted Players

Discussion in 'Spigot Plugin Development' started by Bluecrafter35, Jun 1, 2018.

  1. Code (Java):
    public class ChatListener implements Listener
    {
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onChat(PlayerCommandPreprocessEvent e)
        {
            if(e.getMessage().length()!=0)
            {
                if(e.getMessage().substring(0,3).equalsIgnoreCase("/me"))
                {
                    System.out.println("/me detected");
                    if(!e.getPlayer().hasPermission("nome")||e.getPlayer().isOp())
                    {
                        System.out.println("Player can use /me");
                        return;
                    }
                    else
                    {
                        System.out.println("Player can't use /me");
                        e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 60*20, 5));
                        e.getPlayer().sendMessage(Main.prefix+"§7 Du Schelm!");
                        e.setCancelled(true);
                    }
                }
                else if(e.getMessage().substring(0,5).equalsIgnoreCase("/mute"))
                {
                    System.out.println("/mute detected");
                      if(PermissionsEx.getUser(e.getPlayer()).inGroup("MOD")||PermissionsEx.getUser(e.getPlayer()).inGroup("DEV")||PermissionsEx.getUser(e.getPlayer()).inGroup("ADMIN"))
                    {
                        String playername="";
                        String name = e.getMessage().substring(6, e.getMessage().length());
                        for(int i =0; i<name.length();i++)
                        {
                            playername+=name.charAt(i);
                            boolean isonline = checkName(playername);
                            if(isonline)
                            {
                                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "pex user "+playername+" add nome");
                                e.getPlayer().sendMessage(Main.prefix+"§7 Erfolgreich Player: "+playername+" /me verboten!");
                                break;
                            }
                        }
                    }
                }
            }
        }
       
        public boolean checkName(String name)
        {
            boolean isonline=false;
            for(Player p : Bukkit.getServer().getOnlinePlayers())
            {
                if(p.getName().equals(name))
                {
                    isonline=true;
                    return isonline;
                }
                else
                {
                    isonline=false;
                }
            }
            return isonline;
        }
    So well this Code should disallow muted Players to use /me, but it doesn't work so i don't know why. Maybe because when you mute someone for Example LogischerCrafter you get the Message: "Main.prefix+"§7 Erfolgreich Player: L /me verboten!".
     
  2. That looks messy and I'm confused. Just use this.

    Code (Java):
        List<String> disabledCommands = Arrays.asList(
                // Commands you want to disable here
                "me",
                "othercommand"
        );

        @EventHandler
        public void onPlayerRunCommand(PlayerCommandPreprocessEvent event) {
            Player player = event.getPlayer();

            for (String command : disabledCommands) {
                if (event.getMessage().toLowerCase().equals("/" + command.toLowerCase())) {
                    if (player.hasPermission("core.commands.bypass")) {
                        return;
                    }
                    event.setCancelled(true);
                    Chat.msg(player, "&c&l(!) &cThat command has been disabled!");
                    break;
                }
            }

            if (event.getMessage().toLowerCase().startsWith("/minecraft")) {
                if (player.hasPermission("core.commands.bypass")) {
                    return;
                }
                event.setCancelled(true);
                Chat.msg(player, "&c&l(!) &cThat command has been disabled!");
            }
        }
     
  3. This Line wont work. You must check with startsWith() instead of equals

    I have made this some months ago:

    Code (Java):
        @EventHandler
        public void onLock(PlayerCommandPreprocessEvent event) {
            Player p = event.getPlayer();
            String[] split = event.getMessage().substring(1).split(" ");
         
            if(disabledCommands.contains(split[0].toLowerCase())) {
                //Detected
                if(!p.hasPermission("core.commands.bypass")) {
                    event.setCancelled(true);
                    p.sendMessage("§c§l(!) §cThat command has been disabled");
                }
            } else if(split[0].contains(":")) {
                String cmd = split[0].split(":")[1];
                if(disabledCommands.contains(cmd.toLowerCase())) {
                    //Detected
                    if(!p.hasPermission("core.commands.bypass")) {
                        event.setCancelled(true);
                        p.sendMessage("§c§l(!) §cThat command has been disabled");
                    }
                }
            }
        }
    Then you must fill the List without "/", because I used String.substring(1)
     
    #3 TimeoutHD, Jun 1, 2018
    Last edited: Jun 1, 2018
  4. Please mark as solved.
     
  5. Why it's not solved...
     
  6. I don't know. These methods which were sent are all working. If you would answer to the authors of these messages, maybe they would help you.
     
  7. Well it works now with my Method, but my Question was never mentioned.. I got a Problem with Checking the Username and i get Codes to disable commands... and sorry that i got busy with other things and just looked at my Mail today and saw your answer ^^
     
  8. But i still don't understand it, I mean you see my Code with checkname(String name)? it hasn't worked and than i edited other things on my Plugin and updated it and suddenly it worked, but i didn't had the wrong version because it knowed checkname(), I mean i wrote when it gets called something to put in the console and it was there.