Even the Console Doesn't Have Access to the Command

Discussion in 'Plugin Development' started by AbandonedMinecart, Sep 22, 2013.

Thread Status:
Not open for further replies.
  1. I'm trying to make multiple Yaml files in my plugin's section, but every time I try to set something in a file different from the config.yml, it says I do not have permission to execute this command. I am certain it is not because of the actual permission nodes the player has, because of these reasons:
    1. I could execute the same command before when my code used the getConfig method instead of the getCustomConfig method(I'll show you it later).
    2. When I try to use the console, it says I don't have access to the command, and "If I believe this is an error, I can contact the server administrators"
    Here is my code:
    When I execute the command
    Code:java
    1. static Main plugin;
    2.  
    3. public SetspawnCommand(Main instance) {
    4. plugin = instance;
    5. }
    6.  
    7. @Override
    8. public boolean onCommand(CommandSender s, Command cmd, String l,
    9. String[] args) {
    10. Player player = (Player) s;
    11. if ((Methods.hasPermission(player, "spawnpoints.admin")) || player.isOp()) {
    12. if (l.equalsIgnoreCase("setspawn") && args.length == 0) {
    13.  
    14. s.sendMessage(Main.Prefix + ChatColor.GOLD + " The "
    15. + ChatColor.RED + "spawn" + ChatColor.GOLD
    16. + " SpawnPoint has been set!");
    17. Location loc = player.getLocation();
    18. String w = player.getLocation().getWorld().getName().toString();
    19. plugin.getData().set("spawn" + "." + "X",
    20. Double.valueOf(loc.getX()));
    21. plugin.getData().set("spawn" + "." + "Y",
    22. Double.valueOf(loc.getY()));
    23. plugin.getData().set("spawn" + "." + "Z",
    24. Double.valueOf(loc.getZ()));
    25. plugin.getData().set("spawn" + "." + "W", w);
    26. plugin.saveData();
    27. } else {
    28. s.sendMessage(Main.Prefix + ChatColor.RED
    29. + " Something went wrong!");
    30. }
    31.  
    32. } else {
    33. s.sendMessage(Main.Prefix + ChatColor.RED + " You don't have "
    34. + ChatColor.GOLD + "permission" + ChatColor.RED
    35. + " for this command!");
    36. }
    37. return false;
    38. }

    Here are my methods for the separate data file:
    Code:java
    1. public static Plugin me;
    2. public void onEnable() {
    3. me = this;
    4. }
    5. public FileConfiguration getData() {
    6. if (dFile == null) {
    7. reloadDataFile();
    8. }
    9. return dFile;
    10. }
    11. public void reloadDataFile() {
    12. if (dataFile == null) {
    13. dataFile = new File(Main.me.getDataFolder(), "data.yml");
    14. }
    15. dFile = YamlConfiguration.loadConfiguration(dataFile);
    16.  
    17.  
    18. InputStream defConfigStream = Main.me.getResource("data.yml");
    19. if (defConfigStream != null) {
    20. YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    21. dFile.setDefaults(defConfig);
    22. }
    23.  
    24. if (dFile == null || dataFile == null) {
    25. return;
    26. }
    27. try {
    28. getData().save(dataFile);
    29. } catch (IOException ex) {
    30. Main.me.getLogger().log(Level.SEVERE, "Could not save config to " + dataFile, ex);
    31. }
    32. }
    33. public void saveData() {
    34. if (dataFile == null) {
    35. dataFile = new File(Main.me.getDataFolder(), "data.yml");
    36. }
    37. if (!dataFile.exists()) {
    38. Main.me.saveResource("data.yml", false);
    39. }
    40. }

    I don't have access even when I'm opped on my server, and when I type the command into the console, I get this:
    Code:
    I'm sorry, but you do not have permission to perform this command. Please contact the server administrators if you believe that this is in error.
    What I'm thinking is that somehow my plugin is trying to do something that Bukkit's security prevents it from doing, because it thinks its malicious code... But I have no idea. If you find the problem, please tell me. Thanks!
     
  2. Offline

    1Achmed1

    Two things:
    1. You should use hash maps.
    2. Based solely on the fact that you're setting a spawn, around you command you should add:
    Code:java
    1. if (sender instanceof (Player) {}

    What that does, if I remember correctly, is it checks if the sender is a player and fires it.
     
    XvBaseballkidvX likes this.
  3. You're being very vague when you say "You should use hash maps". Hashmaps for what? And just casting and saying if the sender is a player won't help in this circumstance. My problem is related to permissions, not the sender.

    Anyone find an error?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  4. Offline

    1Rogue

    Can you paste your plugin.yml, and the Methods class?
     
    1Achmed1 likes this.
  5. Offline

    1Achmed1

    Casting the player checker is needed if your setting a home, the console is not a player, therefore not an entity.

    Also, You should replace your current permission checker with this:
    Code:java
    1. if (sender.hasPermission("spawnpoints.admin")) {} elseif (!sender.hasPermission("spawnpoints.admin")) return false;
     

  6. Code:
    name: Spawns
    version: 0.1
    main: me.abandonedminecart.spawns.main
    commands:
      setspawn:
        description: Set spawn
        aliases: [setsp, ss]
        permission: spawnpoints.admin
        usage: /setspawn
    permissions:
      spawnpoints.*:
        description: commands under 1 permission
        default: false
        children:
          spawnpoints.admin: true
      spawnpoints.admin:
        description: All admin permissions
        default: false
    Code:java
    1. public static boolean hasPermission(Player player, String permission) {
    2. if (player.hasPermission(permission)) {
    3. return true;
    4. }
    5. return false;
    6. }
     
  7. Offline

    1Rogue

    Remove the permission node from the command, so that your plugin.yml looks like this for the command:

    Code:
    commands:
      setspawn:
        description: Set spawn
        aliases: [setsp, ss]
        usage: /setspawn
    Then it should check itself within the plugin.

    That all being said, I don't know why you made a hasPermission method that just calls a boolean. You can simplify that method down to:

    Code:java
    1. public static boolean hasPermissions(Player player, String permission) {
    2. return player.hasPermission(permission);
    3. }


    But overall, it isn't even necessary.
     
  8. Offline

    krisdestruction

    I want to do that, but that's more boilerplate overhead code. Is there a way to allow console by default while also specifying the permission node?
     
  9. Offline

    1Rogue


    console has all perms
     
  10. Offline

    Necrodoom

    How can console use this command when you cast him to player?
     
  11. krisdestruction AbandonedMinecart
    Set default to 'op'. BUT the console isn't a 'Player' instance. You still can't run /setspawn from the console and have it work.
     
Thread Status:
Not open for further replies.

Share This Page