Solved Problem with commands

Discussion in 'Plugin Development' started by Koyote_059, May 22, 2018.

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

    Koyote_059

    I'm creating a faction Plugin with Spigot, but commands I created don't work. I'm explaining how: I created the commands /f, /f create and /f invite. When I type /f in game, the game send me a text who says "/f" and stop, it doesn't do what I wrote on the code... could you please help me?

    Main class
    Code:
     
    package it.ss.factionplugin.koyote;
    
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class NewFactions extends JavaPlugin {
        NewFactions instance;
        @Override
        public void onEnable(){
            instance = this;
            loadConfig();
            registerCommands();
        }
       
        @Override
        public void onDisable() {
           
        }
       
       
        private void loadConfig() {
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
        private void registerCommands() {
            instance.getCommand("f").setExecutor(new cmds());
        }
       
        public boolean isPlayerOnline(String player) {
            for(Player tmp: this.getServer().getOnlinePlayers()) {
                if(tmp.getName().equals(player)) {return true; }
            }
            return false;
        }
       
        public Player getInstancePlayer(String player) {
            for(Player tmp: this.getServer().getOnlinePlayers()) {
                if(tmp.getName().equals(player)) {
                    return tmp;
                }
            } return null;
        }
       
        } 
    Class for the commands
    Code:
     
    package it.ss.factionplugin.koyote;
    
    import org.bukkit.Color;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
     public class cmds implements CommandExecutor{
        private NewFactions plugin = new NewFactions();
        private Factions faction = new Factions();
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(args.length == 0) {                                      // Se il comando non ha parametri
                sender.sendMessage(Color.BLUE+ "Scegli un comando valido");
                return true;
            } else if(label.equalsIgnoreCase("f") ) {// Al comando /f
                     if(args[0].equalsIgnoreCase("create")) { // Se il comando ha il 1° parametro "create" ( creare fazione )
                        if(args.length!=2) {            // Se il player NON ha inserito il nome della fazione
                            sender.sendMessage(Color.BLUE + "Sintassi: /f create Fazione");
                            return true;
                        } else {        //Se il player ha inserito il nome della fazione
                            if(!faction.checkName(args[1])) {
                                faction.createFaction(args[1], sender.getName());
                                sender.sendMessage(Color.GREEN + "Fazione " + Color.BLUE + args[1] + Color.GREEN + " creata con successo!");
                                return true;
                            } else {
                                sender.sendMessage(Color.RED + "Nome già in uso!");
                            }
                        }
                    } else if(args[0].equalsIgnoreCase("invite")) { //Se il 1° parametro è invite ( Invitare player )
                        if(args.length != 2) {
                            sender.sendMessage(Color.WHITE + "Sintassi: " + Color.RED + "/f" + Color.BLUE + " invite" + Color.ORANGE + " [Nome Player] ");
                            return true;
                        } else {
                            if(plugin.isPlayerOnline(args[1])) { 
                            Player reciver = plugin.getInstancePlayer(args[1]);
                            String fazione = faction.getFaction(sender.getName());
                            reciver.sendMessage(Color.GREEN + "Sei stato invitato ad entrare nella fazione: " + Color.WHITE + fazione + Color.GREEN + "\nDigita /f join " + Color.WHITE + fazione + Color.WHITE + " per accettare l'invito");
                            sender.sendMessage(Color.LIME + args[1] + Color.GREEN + " invitato."); 
                            return true;
                            } else {
                                sender.sendMessage(Color.RED + "Il player non è online!");
                                return true;
                            }
                        }
                    } else if(args[0].equalsIgnoreCase("join")) {
                        if(args.length != 2) {
                            sender.sendMessage(Color.BLUE + "Sintassi: /f join [Nome Fazione]");
                        } else {
                           
                        }
                    } return false;
                } return false;
        }
    } 
    (Useless for the thread) Class for helping me in Faction files
    Code:
     
    package it.ss.factionplugin.koyote;
    
    import org.bukkit.entity.Player;
    
    public class Factions {
        NewFactions plugin = new NewFactions();
        private String fazione;
        private int c;
       
        public void createFaction(String name,String owner) {
            this.fazione=name;
            c = (int) plugin.getConfig().get("NFactions") + 1;
            plugin.getConfig().set("Fazioni." + c , fazione);
            plugin.getConfig().set("Fazioni." + c + "." + fazione + ".capo_", owner);
            plugin.getConfig().set("NFactions", c);
            plugin.saveConfig();
        }
       
        public void addMember(Player player, String fazione) {
            plugin.getConfig().set("Fazioni." + fazione , player.getName());
            plugin.saveConfig();
        }
       
        public String getFaction(String owner) {
            int c,i;
            String nomeFazione;
            c=  plugin.getConfig().getInt("NFactions");
            for(i=0;i<=c;i++) {
                nomeFazione = (String) plugin.getConfig().get("Fazioni."+ i);
                if(plugin.getConfig().get("Fazioni." + i + "." + nomeFazione + ".capo_").equals(owner)) {
                    return nomeFazione;
                }
            } return null;
        }
       
        public boolean checkName(String name) {
            int c, i;
            c= plugin.getConfig().getInt("NFactions");
            for(i=0;i<=c;i++) {
                if(plugin.getConfig().get("Fazioni." + i).equals(name)) {
                    return true;
                }
            }
            return false;
        }
    }
     
    YML File
    Code:
     
    name: NewFactions
    author: Koyote_059
    version: 1.0
    description: plugin
    main: it.ss.factionplugin.koyote.NewFactions
    commands:
        f: 
            description: Fazioni
            usage: /<command> 
            permission: <command>.perm
            permission-message: You don't have <command>.perm 
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    Koyote_059

  4. Offline

    timtower Administrator Administrator Moderator

    You can't construct a new class that extends JavaPlugin, you need to do that with instances.
     
  5. Offline

    Koyote_059

    Neither the main class? It can't extends JavaPlugin?
     
  6. Offline

    timtower Administrator Administrator Moderator

    The main class should extend JavaPlugin.
    But you are doing this:

    NewFactions plugin = new NewFactions();

    That is not possible.
     
  7. Offline

    Koyote_059

    Oh i got it, now i'm trying... but sincerely i can't understand why I can't create Objects of the main class... Sorry i'm a bit noob in this.
     
  8. Offline

    timtower Administrator Administrator Moderator

    Because you are missing a lot of information that is in the JavaPlugin portion of it. Not to mention all the protection that is on it on the Bukkit side.
    There should only be 1 instance off each plugin at all times.
     
  9. Offline

    Koyote_059

    Ok got it, you're right Thank you.
    Could you do an example if it's not a problem? Just to be sure, sorry for bothering you.
    I tryed to do this but it still doesn't work:

    on main class:
    private static NameMainClass plugin;
    onEnable(){
    plugin=this;
    }

    Then i set every method used in other classes static... but it doesn't work...
     
    Last edited: May 22, 2018
  10. Offline

    timtower Administrator Administrator Moderator

    @Koyote_059 Use constructors instead of those static instances.
     
  11. Offline

    Koyote_059

    Ok no i solved, I missed an instance in a class, now it works... thank you!
     
Thread Status:
Not open for further replies.

Share This Page