Permission check not firing?

Discussion in 'Plugin Development' started by RoboticPlayer, Oct 3, 2015.

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

    RoboticPlayer

    I am coding a plugin that will kill the player when they run a certain command, and if they have a certain permission, they will keep their inventory. Here is a snippet of the command:
    Code:
    if (player.hasPermission("iliketrains.keepinventory")) {
    Bukkit.getServer().broadcastMessage("Player has permission");
    inventory = player.getInventory().getContents();
    armor = player.getInventory().getArmorContents();
    }
    Yet for some reason, the check isn't firing. I have the permission defined in my plugin.yml as such:
    Code:
    permissions:
        iliketrains.keepinventory:
            default: op
    Can anyone explain why this wouldn't be firing? I am OP when I am running the command as well (I have even tried setting the default to true, same result). The player variable is the sender, and I have checked if they were the console.
     
  2. Offline

    Halmerson

    Can you show All of your code please?

    One of the problems could be, maybe you don't have anything to fire the check, like an event or command.
    @henderry2019
     
  3. Offline

    RoboticPlayer

    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("iliketrains")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "The console cannot use this command!");
                    return true;
                }
                final Player player = (Player) sender;
    
                if (!(sender.hasPermission("iliketrains.command"))) {
                    sender.sendMessage(ChatColor.RED + "You don't like trains D:");
                    return true;
                }
    
                if (player.hasPermission("iliketrains.keepinventory")) {
                    Bukkit.getServer().broadcastMessage("Player has permission");
                    inventory = player.getInventory().getContents();
                    armor = player.getInventory().getArmorContents();
                }
    
                new BukkitRunnable() {
                    @Override
                    public void run() {
                        player.setHealth(0);
                    }
                }.runTaskLater(this, 100L);
                trains.add(player.getName());
                player.chat(ChatColor.DARK_RED + "" + ChatColor.BOLD + "I like trains!");
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Watch out! Train!");
            }
            return true;
        }
    The rest of the code is not really relevant to this issue.
     
  4. Offline

    teej107

    @henderry2019 Add in debug code to make sure the method is getting called.
     
  5. Offline

    RoboticPlayer

    I did add a debug, and it's not getting called. That's my line with Bukkit#broadcastMessage() in my permission check. Everything else works fine in the command.
     
  6. Offline

    teej107

    is this being run?
     
  7. Offline

    RoboticPlayer

    @teej107 Yes, that method works fine. Everything in my command method works fine, except for this:
    Code:
    if (player.hasPermission("iliketrains.keepinventory")) {
        Bukkit.getServer().broadcastMessage("Player has permission"); // My debug message (not firing)
        inventory = player.getInventory().getContents();
        armor = player.getInventory().getArmorContents();
    }
     
  8. Offline

    teej107

    @henderry2019 So you are not getting the message:
    right?
     
  9. Offline

    567legodude

    @henderry2019 (@teej107) I've looked into this before, storing permissions as keys in YAML. I believe that it doesn't work because since YAML uses "." to separate keys, and permissions have a "." in them.

    So say your current config has this:
    Code:
    permissions:
        example.perm: OP
    You would assume that you could find it with:
    "permissions." + permName
    where permName is "example.perm"

    But the problem is that "permissions.example.perm" is trying to look for a structure like this:
    Code:
    permissions:
        example:
            perm: OP
    So unless you structure the config like that, then it can't find your value in the config.

    EDIT: In this case with permissions.yml it probably has its own way of doing it.
     
  10. Offline

    RoboticPlayer

    Wrong. That works fine. Checking the player for permission to run the command initially works.
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("iliketrains")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "The console cannot use this command!");
                    return true;
                }
                final Player player = (Player) sender;
                if (!(sender.hasPermission("iliketrains.command"))) {
                    sender.sendMessage(ChatColor.RED + "You don't like trains D:");
                    return true;
                }
                // The below check is the ONLY thing that doesn't work
                // Everything above this works
                if (player.hasPermission("iliketrains.keepinventory")) {
                    Bukkit.getServer().broadcastMessage("Player has permission");
                    inventory = player.getInventory().getContents();
                    armor = player.getInventory().getArmorContents();
                }
                // Everything below this works
                new BukkitRunnable() {
                    @Override
                    public void run() {
                        player.setHealth(0);
                    }
                }.runTaskLater(this, 100L);
                trains.add(player.getName());
                player.chat(ChatColor.DARK_RED + "" + ChatColor.BOLD + "I like trains!");
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Watch out! Train!");
            }
            return true;
        }
    Edit: @567legodude That's not it. The permission for iliketrains.command works, iliketrains.inventory does not.
     
  11. Offline

    teej107

    @henderry2019 What I'm trying to say is that are you exiting out of the method too soon? aka return;
     
  12. Offline

    RoboticPlayer

    @teej107 No. I don't want the console to be able to use the command. And anyone without the permission "iliketrains.command" I don't want to be able to run the command. Nowhere else in my command do I have a return statement (excluding the end of the onCommand method).
     
  13. Offline

    RoboticPlayer

  14. Offline

    teej107

    @henderry2019 well the sender doesn't have the permission. You aren't using any other plugins are you? You don't need to register permissions in the plugin yml.
     
  15. Offline

    RoboticPlayer

    @teej107 But the sender does have permission... that's why I'm so confused. I have even tried setting the default for the permission to true, but that line still didn't fire.
     
  16. Offline

    teej107

    Obviously not.
     
  17. Offline

    RoboticPlayer

    @teej107 However, even with the permission set with the default as "true" it still doesn't fire that method.
     
  18. Offline

    567legodude

    @henderry2019 Are you sure they have the permission, how are you giving it to them?
     
  19. Offline

    RoboticPlayer

    @567legodude The player is OP and the default for the permission is op.
     
  20. Offline

    Reynergodoy

    Code:
    if (player.hasPermission("iliketrains.keepinventory")) {
    Bukkit.getServer().broadcastMessage("Player has permission");
    inventory = player.getInventory().getContents();
    armor = player.getInventory().getArmorContents();
    }
    
    is it necessary to write the permissions in the plugin.yml file?
    i don't do this and it works fine, just using groupmanager or permissionsex
     
  21. Offline

    boomboompower

    Just have a if (player.isOp() == true) {

    You don't have to put the permission in the plugin.yml
     
  22. Offline

    567legodude

    @boomboompower They don't want to do it if they are op, they want it to run if they have the permission.
     
  23. Offline

    boomboompower

    @henderry2019 put something like this
    Code:
    Player p = (Player) sender;
    if (!p.hasPermission("a.permission")) {
             p.sendMessage(dr + "You do not have access to this command!");          
    } else {
             // Do something magical :D
     
    Last edited: Oct 9, 2015
  24. Offline

    Jayyy

    Here is the code you may or not be looking for :D
    Code:
    package me.jay.bukkit;
    
    import java.util.ArrayList;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
        static ArrayList<String> killed = new ArrayList<String>();
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("kill")) {
                if(!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "Player only command.");
                }
                Player p = (Player)sender;
                if(!p.hasPermission("permission.node")) {
                    p.sendMessage(ChatColor.RED + "No Permission ;l");
                    return true;
                } else {
                    killed.add(p.getName());
                    p.setHealth(0);
                    p.sendMessage(ChatColor.GRAY + "Killed.");
                }
            }
            return false;
        }
        @EventHandler
        public static void Death(PlayerDeathEvent e) {
            Player p = e.getEntity();
    
            if(killed.contains(p.getName())) {
                e.setKeepInventory(true);
            }
    
        }
    }
     
    Last edited: Oct 9, 2015
Thread Status:
Not open for further replies.

Share This Page