Per command classes

Discussion in 'Spigot Plugin Development' started by KingTux, May 11, 2016.

Thread Status:
Not open for further replies.
  1. I am getting an error When the plugin enables http://pastebin.com/MnrzzK0q
    Code (Text):


    package me.KingLinux01;

    import java.util.List;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Serverinfo extends JavaPlugin implements CommandExecutor {
       Permission serverinfo = new Permission("serverinfo.info");
       FileConfiguration config = getConfig();


       @Override
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         Player player = (Player) sender;

         if (cmd.getName().equalsIgnoreCase("serverinfo")) {

           if (player.hasPermission(serverinfo)) {
             List<String> info = this.getConfig().getStringList("server-info");
             for (String s : info) {
               player.sendMessage(s);
             }

           } else {

             player.sendMessage(ChatColor.DARK_RED + "You need permission to do that");

           }
           return true;
         }
         
         return false;
       }

    }

     
    Code (Text):

      public void onEnable() {

         //adds some defaults for the config
         config.addDefault("Welcome to ServerInfo  The Plugin ", config);
         config.addDefault("website-link", "serverwebsite.domainname");
         config.addDefault("server-info", "");
         config.addDefault("rank-link", "link.to.ranks");
         config.addDefault("owner", "");
         config.addDefault("staff", "");
         // save the default config
         this.getConfig().options().copyDefaults(true);
         this.saveDefaultConfig();
         
      getCommand("serverinfo").setExecutor(new Serverinfo());
         
         
       }
     
     
  2. What happens if you plain start the server? Don't reload the server.
     
  3. Code (Text):
    getCommand("serverinfo").setExecutor(new Serverinfo());
    Are you creating another instance of your plugin there?
     
  4. Possibly a similar name to other plugins? Maybe it's doubled up somewhere?
     
  5. I want every command to have it's own class.
     
  6. MiniDigger

    Supporter

    getCommand("serverinfo").setExecutor(new Serverinfo());

    don't do that. there can only be one instance of your main class, and bukkit creates that one. use 'this'
     
  7. On my test server that is the only plugin install.
     
  8. Then what do i do?
     
  9. Make a seperate class that doesn't extend JavaPlugin. You are trying to initalize another instance of your plugin which results in the error.
     
  10. When i add "this" I get a error in eclipse.
     
  11. aaaaaaand what does the error say?^^
     
  12. How do i get the config then?
     
  13. Btw using this like @MiniDigger suggested should be redundant if you extend JavaPlugin in that class. It should work without setting an executor there.
    If you want to make a seperate class you can pass it a reference of your main class/config in the constructor to get access to it. Or use a singleton (static reference in the main class)
     
  14. You pass the instance on. Constructors/plugin getters.

    Method 1:
    Code (Java):
    // Main
    public static Main getPlugin() { return this.getPlugin(Main.class); }

    // Used.
    Main.getPlugin().getConfig().etc;
    Method 2:
    Code (Java):
    // The command class
    private final Main main;
    public ClassName(Main main) {
    this.main = main;
    }

    // Command
    main.getConfig().etc

    //Main
    getCommand("cmd").setExecutor(new ClassName(this));
     
  15. http://pastebin.com/X4x1Hu4y
    added this code
    public FileConfiguration getConfig() {
    return getConfig();
    }
    FileConfiguration config = getConfig();
     
  16. Name your function different. The function is calling itself. E.g.
    Code (Text):
    public FileConfiguration getFileConfig() {
      return getConfig();
    }
    Edit: that function is useless, use the normal getConfig() function^^
     
  17. It wont let me return getConfig();
     
    • Optimistic Optimistic x 1
  18. MiniDigger

    Supporter

    did you read his edit? the function is completly useless since you already have that function thru inheritance
     
  19. Code (Text):

    package me.KingLinux01;

    import java.util.List;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.permissions.Permission;

    public class ServerinfoC implements CommandExecutor {
       Permission serverinfo = new Permission("serverinfo.info");

        FileConfiguration config = getConfig();

       @Override
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         Player player = (Player) sender;

         if (cmd.getName().equalsIgnoreCase("serverinfo")) {

           if (player.hasPermission(serverinfo)) {
             List<String> info = getConfig().getStringList("server-info");
             for (String s : info) {
               player.sendMessage(s);
             }

           } else {

             player.sendMessage(ChatColor.DARK_RED + "You need permission to do that");

           }
           return true;
         }
         return false;
       
       }

       public FileConfiguration getConfig()  {
          return getConfig();
       }

    }
     
     
Thread Status:
Not open for further replies.