Command via Console = Error

Discussion in 'Plugin Development' started by Smex, Sep 30, 2011.

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

    Smex

    Well I've got another problem, if I use my commands via console it
    will fire me an error, because of the plugin wants to check if the
    console has permissions but as the console is no entity player or whatever
    it will give me that error.

    So, how do I bypass that?

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("antienderpick") || cmd.getName().equalsIgnoreCase("aep")) {
    
                if(args[0].equals("acti") || args[0].equals("on")) {
                    if (player.hasPermission("antienderpick.acti")){
                    a = true;
                    player.sendMessage("Anti-EnderPick activated!");
                    if (a==true){
                    this.getServer().getPluginManager().registerEvent(Event.Type.ENDERMAN_PICKUP, new EntityListener(){
                        public void onEndermanPickup(EndermanPickupEvent e){
                            e.setCancelled(true);
                        }
                    }, Event.Priority.Normal, this);
                    }
                    }else{
                        player.sendMessage("Wannabe admin?");
    
                    }
            }
     
  2. http://forums.bukkit.org/threads/co...ouldnt-just-return-if-instanceof-player.3186/
    You don't ever need a player in that, CommandSender implements Permissible so you can also check Permissions without casting ...

    And please don't register events in commands!!! You will add another listener everytime the command is executed.
    Register in onEnable and set a flag if it's enabled. When the flag is true -> cancel event.
     
    Smex likes this.
  3. Offline

    Don Redhorse

    well you check if the sender is an instance of player and than you do the permission check
     
  4. Offline

    Smex

    What
    What do I set than on
    Code:
    if(args[0].equals("acti") || args[0].equals("on")) {
                    if ([B]player[/B].hasPermission("antienderpick.acti")){
                    a = true;
    if the player variabel doesnt exist anymore?
     
  5. Offline

    bassfader

    You can just check if sender is a player by using something like:
    Code:
    if (sender instanceof Player) { .... }
     
  6. sender.hasPermission(...)

    @bassfader
    You don't have to do that, since you don't even need a player to check for permissions, just a Permissible (which a CommandSender is).
     
  7. Offline

    Smex

    So the onCommand checks automaticly if it is a player or not?

    Edit: nice it works, thank you .

    I will change the EventListener in to the onEnable Method.
    Thanks for that advice.
     
  8. Offline

    bassfader

    @Bone008

    ahh thx for clearing that up (haven't converted to SuperPerms yet, but about to do that soon ^^), didn't look at the API for quite a while, I guess some stuff changed a little since SuperPerms :rolleyes:
     
Thread Status:
Not open for further replies.

Share This Page