Registering Methods in onEnable

Discussion in 'Plugin Development' started by Doooogle, Jan 24, 2014.

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

    Doooogle

    So I was wondering how you register a class with methods in it in a core class with the onEnable, if you need to. One of my plugins isn't working properly, due to one of the commands being faulty, but the command uses a method in a separate class which all works good. The command doesn't work I believe because the methods in the other class aren't being registered in the onEnable, therefore the command isn't working.

    If this doesn't make sense, please let me know, and if you need any code to help that'd be amazing. Thanks!
     
  2. Offline

    fireblast709

    Doooogle normally you don't 'register a class with methods', that is the job of the classloader. You should check the console for errors on startup, which is likely the cause of something not working.
     
  3. Offline

    Doooogle

    I didn't know if you had to register the methods, but would you need to do anything in the onEnable? There's no errors on the startup :/
     
  4. Offline

    fireblast709

    Doooogle do you have any code to show?
     
  5. Offline

    Doooogle

    Command class
    Code:java
    1. public static ClansSetup plugin;
    2.  
    3. @Override
    4. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    5. Player p = (Player) sender;
    6. if(cmd.getName().equalsIgnoreCase("createclan")){
    7. if(args.length == 0){
    8. p.sendMessage(ChatColor.RED + "Not enough arguments! Please enter a clan name.");
    9. } else {
    10. if(plugin.clan.containsKey(args[0])){
    11. p.sendMessage(ChatColor.RED + "This clan name has already been taken!");
    12. } else {
    13. plugin.createClan(args[0]);
    14. p.sendMessage(ChatColor.GREEN + "Clan created with name: " + ChatColor.BLUE + args[0]);
    15. }
    16. }
    17. }
    18. return false;
    19. }

    Clan setup class
    Code:java
    1. public static ClansCore plugin;
    2.  
    3. public ClansSetup(ClansCore plugin){
    4. ClansSetup.plugin = plugin;
    5. }
    6.  
    7. @SuppressWarnings("rawtypes")
    8. public HashMap<String, ArrayList> clan = new HashMap<String, ArrayList>();
    9.  
    10. public void createClan(String name){
    11. clan.put(name, new ArrayList<Player>());
    12. }
    13.  
    14. public void deleteClan(String name, Player p){
    15. clan.remove(name);
    16. }
    17.  
    18. @SuppressWarnings("unchecked")
    19. public void addPlayers(String name, Player p){
    20. if(clan.containsKey(name)){
    21. clan.get(name).add(p);
    22. }
    23. }
    24.  
    25. public void removePlayers(String name, Player p){
    26. if(clan.containsKey(name)){
    27. clan.get(name).remove(p);
    28. }
    29. }

    Clan Core class
    Code:java
    1. @Override
    2. public void onEnable(){
    3. getLogger().info(getName() + " has been enabled!");
    4. ClansCommands ccommands= new ClansCommands();
    5.  
    6. getCommand("createclan").setExecutor(ccommands);
    7. }
    8.  
    9. @Override
    10. public void onDisable(){
    11. getLogger().info(getName() + " has been disabled!");
    12. }
     
  6. Offline

    fireblast709

    1. Does your CommandClass implement CommandExecutor.
    2. Did you register the createclan command in plugin.yml
      Code:
      commands:
        createclan:
          default: true 
    3. Please don't create a static plugin instance in your class. If you don't know how to properly take care of static, it will cause a lot of trouble later. In 99% of the cases, use the constructor to pass a reference of your main class ;)
     
  7. Offline

    Doooogle

    1. Yes
    2. Yes
    3. Sure, what would I put in the constructor while we're at it ;)

    fireblast709 it comes up with an error on the console when you type the command with an argument. Here it is:
    Code:
    issued server command: /createclan test
    2014-01-24 21:04:49 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'createclan' in plugin CoBClans v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:532)
        at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:986)
        at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:897)
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:838)
        at net.minecraft.server.v1_6_R3.Packet3Chat.handle(Packet3Chat.java:47)
        at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:237)
        at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:117)
        at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
        at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:131)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:604)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:240)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:493)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:425)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.NullPointerException
        at me.doooogle.cobclans.ClansCommands.onCommand(ClansCommands.java:20)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  8. Offline

    fireblast709

    Doooogle In case you would need access to the main class (or you need it for a runnable), it is better to pass it via the constructor. For instance, the error is caused due to the fact that plugin isn't initialized.
    Code:
    private final ClansCore plugin;
    public ClansCommands(ClansCore plugin)
    {
        this.plugin = plugin;
    }
    This will assign the variable using the constructor to pass the reference. Now you will notice that
    Code:
    new ClansCommands();
    is no longer valid. This is caused by defining your own constructor (which means the default empty constructor no longer exists), and the current constructor takes one argument, namely the main class.
    Code:
    new ClansCommands(this)
    This is the syntax of creating the ClansCommands object, passing 'this' as the reference.
     
  9. Offline

    Doooogle

    fireblast709 in the ClansCommands class I need to get the methods from the ClansSetup class, is this possible to add into the constructor? When I removed the static line it no longer could find the clan hashmap below.
     
  10. Offline

    fireblast709

    In ClansSetup you could create a public boolean method clanExists(String name)
    Code:
    public boolean clanExists(String name)
    {
        return this.clans.containsKey(name);
    }
     
  11. Offline

    Doooogle

    That's not what I wanted. Once I take out the static part in the commands section, it doesn't let me access any of the methods in the ClansSetup class.
     
Thread Status:
Not open for further replies.

Share This Page