Solved Deny specified characters in command

Discussion in 'Plugin Development' started by VladStarr, Jan 5, 2015.

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

    VladStarr

    Hi, guys.
    I'm trying to make my own plugin. The feature is: plugin must deny the /clan command, if it's arguments contains the character "&".
    It seems, that i'm doing something wrong, but i can't understand. :(
    If anyone knows, please help.
    Sorry for my english, i'm russian.

    Code:
    Code:
    package me.vladstarr.fixclans;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ClanFix extends JavaPlugin
        implements Listener {
    
        @EventHandler
         public void onPreCommand(PlayerCommandPreprocessEvent e)
         {
            //get the command sender
            Player player = e.getPlayer();
            //check permission for bypass
            if(player.hasPermission("fixclans.bypass"))
           {
               return;
           }
            //if command contains /clan and & char
            if (e.getMessage().contains("/clan") & e.getMessage().contains("&")) {
                //deny the command
                e.setCancelled(true);
              //and show the exception message
               e.getPlayer().sendMessage(ChatColor.RED + "You cannot use color codes in your clan tag!");
               return;
           }
         }
    }
    I've read this tutorial and i've tried to correct my code.
    But it still doesn't work.

    Code:
    package me.vladstarr.fixclans;
    
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ClanFix extends JavaPlugin
        implements Listener {
    
        @EventHandler
         public void onPreCommand(PlayerCommandPreprocessEvent e)
         {
            String message = e.getMessage();
            String[] array = message.split(" ");
                if(array.length == 3)
                {
                   if(array[0].equalsIgnoreCase("/clan") && array[1].equalsIgnoreCase("create") && array[2].startsWith("&")) {
                      e.setCancelled(true);
                      e.getPlayer().sendMessage(ChatColor.RED + "You cannot use color codes in your clan tag!");
                      return;
                   }
                  if(array[0].equalsIgnoreCase("/clan") && array[1].equalsIgnoreCase("modtag") && array[2].startsWith("&")) {
                      e.setCancelled(true);
                     e.getPlayer().sendMessage(ChatColor.RED + "You cannot use color codes in your clan tag!");
                     return;
                  }
                }
         }
    }
    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 5, 2015
  2. Offline

    Freack100

    You'll have to register the listener in your onEnable: (You don't even have an onEnable in there yet :O)
     
    VladStarr likes this.
  3. Offline

    VladStarr

    Thx very much! I'm NOOB. :D
     
Thread Status:
Not open for further replies.

Share This Page