My permissions plugin's commands dont work.

Discussion in 'Spigot Plugin Development' started by ArmouredEnderBoy, Jul 13, 2018 at 8:55 PM.

  1. Hello there! I need some help with a problem that I am having with my spigot plugin. I wanted to be able to assign some permissions to players, but when I send the command it just says
    group not found and then the name of my plugin. Can I get some help? here's the code.
    Code (Java):
    package me.astronomize.up;

    import java.lang.reflect.Array;
    import java.util.logging.Level;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;

    @SuppressWarnings("unused")
    public class Main extends JavaPlugin {
       
        PluginDescriptionFile pdf = this.getDescription();
        public void onEnable() {
            Bukkit.getLogger().log(Level.INFO, "[UltraPerms]The Plugin has been enabled.");
            this.saveDefaultConfig();
           

                for (Player pl  :  Bukkit.getOnlinePlayers()) {
                    this.getConfig().addDefault("players." + pl.getUniqueId().toString(), "");
                    this.getConfig().addDefault("players." + pl.getUniqueId().toString() + ".group", "default");
                    this.getConfig().addDefault("players." + pl.getUniqueId().toString() + ".prefix", "");
                    String uuid = pl.getUniqueId().toString();
                    pl.setPlayerListName(this.getConfig().getString("players." + uuid + ".prefix" + pl.getDisplayName()));
                    pl.setDisplayName(this.getConfig().getString("players." + uuid + ".prefix" + pl.getDisplayName()));

            }


           
        }
       
       
        public void onDisable() {
            Bukkit.getLogger().log(Level.INFO, "[UltraPerms]The Plugin has been disabled.");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            String name = sender.getName();
           
            if (cmd.getName().equalsIgnoreCase("ultraperms")) {
                String version = pdf.getVersion();
           
                //version
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.AQUA + "[UltraPerms] Version: " + version);
                    Bukkit.getLogger().log(Level.INFO, "[UltraPerms] " + name + "ran the command /ultraperms.");
                }
               
                if (args.length == 1  || args.length > 1) {
                   
                    //reload
                    if (args[0].equalsIgnoreCase("reload")) {
                        Bukkit.getPluginManager().getPlugin("UltraPermissions").reloadConfig();
                        sender.sendMessage(ChatColor.AQUA + "[UltraPerms] Reloaded configuration.");
                        Bukkit.getLogger().log(Level.INFO, "[UltraPerms] " + name + "ran the command /ultraperms reload.");
                    }
                   
                    //add perm
                    if (args[0].equalsIgnoreCase("add")) {
                        String un = args[1];
                        String perm = args[2];
                       
                        Player p = Bukkit.getPlayer(un);
                        Bukkit.getPluginManager().getPermission(perm);
                        Bukkit.getPluginManager().subscribeToPermission(perm, p);
                    }
                   
                    //remove perm
                    if (args[0].equalsIgnoreCase("remove")) {
                        String un = args[1];
                        String perm = args[2];
                       
                        Player p = Bukkit.getPlayer(un);
                        if (p.hasPermission(perm)) {
                            Bukkit.getPluginManager().getPermission(perm);
                            Bukkit.getPluginManager().unsubscribeFromPermission(perm, p);
                        } else {
                            sender.sendMessage(ChatColor.RED + "This player doesn't have this permission.");
                        }
                    }
                   
                    //groups
                    if (args[0].equalsIgnoreCase("group")) {
                        String un = args[1];
                        String group = args[2];
                       
                        Player p = Bukkit.getPlayer(un);
                        String uuid = p.getUniqueId().toString();
                       
                        if (this.getConfig().contains(group)) {
                            this.getConfig().set("players." + uuid + ".group", group);
                            String prefix = this.getConfig().get("groups" + group + ".prefix").toString();
                            Prefixes.givePrefix(p, prefix);
                            for (String perms  :  this.getConfig().getStringList("groups." + group + ".permissions")) {
                                perms = perms.toString();
                                Bukkit.getPluginManager().getPermission(perms);
                                Bukkit.getPluginManager().subscribeToPermission(perms, p);
                            }
                        }
                       
                    }else {
                        sender.sendMessage(ChatColor.RED + "This group doesnt exist.");
                   }
                   
                   if (args[0].equalsIgnoreCase("prefix")) {
                       String player = args[1];
                       String prefix = args[2];
                     
                       Player pl = Bukkit.getPlayer(player);
                       String uuid = pl.getUniqueId().toString();
                       Prefixes.givePrefix(pl, prefix);
                       this.getConfig().set("players." + uuid + "prefix", prefix);
                   }
                }

            }
            //damage command: /damage player amount
           
            //command style: /ultraperms add {player} {permission}
            //group command: /ultraperms group {player} {group}
            //prefix command /ultraperms prefix {player} {prefix}
            return false;
        }
    }
    plugin.yml:
    Code (YAML):
    name: UltraPermissions
    description
    : a simple plpugin for configuring permissions.
    version
    : 1.1.1
    main
    : me.astronomize.up.Main

    commands
    :
      ultraperms
    :
        permission
    : ultraperms.main
        usage
    : ultraperms
    config.yml:
    Code (YAML):
    ops-have-all-perms: false

    groups
    :
      default
    :
        default
    : true
        can-pick-up-items
    : true
        permissions
    :
       
      admin
    :
        default
    : false
        can-pick-up-items
    : true
        prefix
    :
        permissions
    :
         - ultraperms.main
          - ultraperms.add
          - ultraperms.remove
         
    players
    :
      uuid
    :
        group
    : default
        prefix
    :
       

    thanks for the help :D
     
  2. Your else block matches the wrong if clause

    Code (Text):
    if (args[0].equalsIgnoreCase("group")) {
        ...
        if (this.getConfig().contains(group)) {
             ...
        }

    } else {
        sender.sendMessage(ChatColor.RED + "This group doesnt exist.");
    }
    Try indenting the code properly. Most IDEs can do that for you with a key combination (Format code or similar)
     
  3. sorry i dont really understand :/ I am a bit of a noob
     
  4. In your on enable you need to register the command with 'this.getCommand("[command name here]")'.
     
  5. So do I use the args in it or what is the syntax? Sorry I know im a noob.
     
  6. No, this just tells Spigot that your command is a command & than it will run the onCommand event in your class where you registered it. For syntax your 'public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)' returns args which are used for all sub commands if you want to do it that way. This is the easiest way for you in my opinion, so just test on args[0] for sub commands. Thus you have syntax.
     
  7. Okay thank you!
     
  8. I cant use the args in getCommand :/
     
  9. Its working now! Thanks so much!
     
  10. You are most certainly welcome tip look up this stuff I know for a fact that there's plenty of online resources that could've helped you.
     
  11. xD yeah your right
     

Share This Page