Need help with code

Discussion in 'Plugin Development' started by banjo-226, Mar 15, 2015.

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

    banjo-226

    Hi, I have this code:
    Code:
        public boolean clearOwn(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if(label.equalsIgnoreCase("cco") || label.equalsIgnoreCase("clearchatown") || label.equalsIgnoreCase("ccown")){
                if(!sender.hasPermission("cc.clearchat.own")){
                    player.sendMessage(ChatColor.DARK_RED + "You do not have permission to use this command.");
                    return true;
                }else{
                    for(int i = 0; i < 100; i++){
                        sender.sendMessage(" ");
                        }
                    sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.DARK_PURPLE + "Clear" + ChatColor.GRAY + "Chat" + ChatColor.DARK_GRAY + "] " + ChatColor.GOLD + "You cleared your chat.");
                }
            }
            return false;
        }
    }
    For players to clear their own chat, but my code isn't working. Any solutions?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @banjo-226 Is the command listed in your plugin.yml?
     
  3. @banjo-226 Use cmd.getName() instead of label
    Have you registered the command?
    Is it in your plugin.yml?
     
  4. Offline

    banjo-226

    @bwfcwalshy @timtower Yes, the commands have been entered into the plugin.yml.
    I will try the cmd.getName() too

    Just tried that, @bwfcwalshy cmd.getName() didn't change anything
     
    Last edited by a moderator: Mar 15, 2015
  5. Offline

    CraftCreeper6

    @banjo-226
    You need to override the onCommand method, your method is named incorrectly.
     
    Tecno_Wizard likes this.
  6. Offline

    kaif21

    Maybe try
    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("cco")
    3. && sender instanceof Player) {
    4. //Your code
    5.  
    6. }else if (cmd.getName().equalsIgnoreCase("clearchatown")) {
    7. //Same code as above
    8.  
    9. }
    10.  

    *Add both cmd in plugin.yml
     
  7. Offline

    SergeantBud

    Code:
     publicboolean clearOwn(CommandSender sender, Command cmd, String label, String[] args){
    Should Be
    Code:
        public boolean onCommand(final CommandSender sender, Command cmd, String label, String[] args) { 
    .
     
  8. Offline

    teej107

    @banjo-226 As @SergeantBud and @CraftCreeper6 have stated, you need to change the method name to onCommand. Due to the nature of overridden methods, name and parameters must be exact to override methods. Your clearOwn method is not being called which is why nothing is working. If you wish, you may implement the onCommand method and then call your own clearOwn method from there.
     
    SergeantBud likes this.
  9. Offline

    banjo-226

    I swapped the method to onCommand, and it gives me an error saying that I have to rename it, so I put it on a new .java file, exported it but nothing happened. is there a way to like link the two classes together?
     
  10. Offline

    teej107

  11. Offline

    mythbusterma

    @banjo-226 @teej107

    This is the EXACT reason the @Override annotation exists. In the future, use it.
     
    teej107 and Regablith like this.
Thread Status:
Not open for further replies.

Share This Page