I cant manage to make player only command and permissions work together

Discussion in 'Plugin Development' started by stickeric, Jan 15, 2013.

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

    stickeric

    I can't seem to manage to make Haspermissions and sender instanceof Player work together, I have been trying a lot and can't seem to get a fix.

    How do i make it that it will check the permissions and if the sender is a player and not a console.
    I tryed a lot with
    Code:java
    1.  
    2. if (sender instanceof Player) {
    3.  


    But can't seem to get them working together, the command does work but the console giving this error when i try something:
    Code:
    2013-01-15 21:27:19 [WARNING] Unexpected exception while parsing console command "fly"
    org.bukkit.command.CommandException: Unhandled exception executing command 'fly' in plugin EKCommands v0.2
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
    at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCommand(CraftServer.java:514)
    at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchServerCommand(CraftServer.java:506)
    at net.minecraft.server.v1_4_6.DedicatedServer.al(DedicatedServer.java:260)
    at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:225)
    at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:494)
    at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:427)
    at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_4_6.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    at me.stickeric.ekcommands.EKCommands.onCommand(EKCommands.java:34)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 8 more
    
    Where do i put the instance of player in this code correctly:
    Without getting that error?

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2. Player p = (Player) sender;
    3. if(commandLabel.equalsIgnoreCase("fly")){
    4. if(p.hasPermission("ek.fly")){
    5. {
    6. if (p.isFlying()) {
    7. p.setFlying(false);
    8. p.setAllowFlight(false);
    9. p.sendMessage(ChatColor.DARK_RED + "You are not flying anymore.");
    10. } else {
    11. p.sendMessage(ChatColor.DARK_RED + "You are now Flying.");
    12. p.setAllowFlight(true);
    13. p.setFlying(true);
    14. }
    15. }
    16. }
    17. else
    18. {
    19. p.sendMessage(ChatColor.BLUE + "No permissions :).");
    20. }
    21. }
    22. else if(commandLabel.equalsIgnoreCase("broadcast")){
    23. p.sendMessage(getConfig().getString("ekbroadcast"));
    24. }
    25. return false;
    26. }
    27. }
    28.  


    Thank you for your time.
     
  2. Offline

    Craftiii4

    Well, use the code you stated above "if (sender instanceof Player) {"

    Use an else here, so if a player did not issue the command print the error to the console.

    Then just simply check the permission within the instanceof Player?


    I'm not entirely sure what your problem is, sorry.
     
  3. Offline

    stickeric

    If you ment like this, it's still giving an error, I think i'm doing something wrong with the else from if if(p.hasPermission("ek.fly")){ but im not 100% sure.

    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    3. Player p = (Player) sender;
    4. if(commandLabel.equalsIgnoreCase("fly")){
    5. if (sender instanceof Player) {
    6. if(p.hasPermission("ek.fly")){
    7. {
    8. if (p.isFlying()) {
    9. p.setFlying(false);
    10. p.setAllowFlight(false);
    11. p.sendMessage(ChatColor.DARK_RED + "You are not flying anymore.");
    12. } else {
    13. p.sendMessage(ChatColor.DARK_RED + "You are now Flying.");
    14. p.setAllowFlight(true);
    15. p.setFlying(true);
    16. }
    17. }
    18. }else{
    19. p.sendMessage(ChatColor.BLUE + "Only the wonderfull staff members of EK has this command :).");
    20.  
    21. }
    22. }
    23. else{
    24. System.out.println("Test");
    25.  
    26. }
    27.  
    28. }
    29. else if(commandLabel.equalsIgnoreCase("broadcast")){
    30. p.sendMessage(getConfig().getString("ekbroadcast"));
    31. }
    32. return false;
    33. }
    34. }
    35.  
     
  4. Offline

    Craftiii4

    Try defining Player p after you have determined it is a player?
     
    stickeric likes this.
  5. Offline

    stickeric

    Thanks man!

    For the people i have fixed it with this

    Code:java
    1.  
    2. if (sender instanceof Player) {
    3. Player p = (Player) sender;
    4. if(p.hasPermission("ek.fly")){
     
  6. Offline

    Craftiii4

    np ;) happy to help :3
     
    stickeric likes this.
Thread Status:
Not open for further replies.

Share This Page