Commands in different classes

Discussion in 'Plugin Development' started by LocoCat, May 3, 2012.

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

    LocoCat

    for some reason, my commands won't work. putting them in the same class produces errors with either the name or the return value. putting them in different classes just doesn't work for me because the main class won't read them for some reason. Thanks in advance!

    -LocoCat


    For anyone out there thinking of replying to this, I have been developing plugins for about 2 days now, and have a decent knowledge of java. the code of Basic.java and CommandBasic.java are right here:


    -Basic.java-
    Code:
    package me.duncan.basic;
     
    import java.util.logging.Logger;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Basic extends JavaPlugin {
       
        Logger log;
       
        @Override
        public void onEnable() {
            log = this.getLogger();
            log.info("Plugin Enabled!");
        }
       
        @Override
        public void onDisable() {
            log.info("Plugin Disabled!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            getCommand("basic").setExecutor(new CommandBasic(this));
            return false;
        }
           
        }

    -CommandBasic.java-
    Code:
    package me.duncan.basic;
     
    import java.util.logging.Logger;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class CommandBasic extends Basic implements CommandExecutor {
     
        Logger log;
       
        public CommandBasic(Basic plugin){
        }
     
        public boolean onCommand(CommandSender cs, Command cmd, String alias, String[] args) {
            Player player = (Player)cs;
            player.sendMessage("You executed a basic command");
            log = this.getLogger();
            log.info("<Player> used a basic command");
            return false;
            }
     
        }    
     
  2. Offline

    CorrieKay

    you register the command in onEnable, not onCommand :3
     
  3. Offline

    LocoCat

    I changed my code to this and it gives me this error when i start bukkit.


    Code:
    14:59:06 [SEVERE] Error occurred while enabling Basic v1.0 ALPHA (Is it up to da
    te?)
    java.lang.NullPointerException
            at me.duncan.basic.Basic.onEnable(Basic.java:15)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:215)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:336)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:381)
            at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:250)
            at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:232
    )
            at net.minecraft.server.MinecraftServer.t(MinecraftServer.java:371)
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:358)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:187)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:422)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    14:59:06 [INFO] [PailPlus] Enabling PailPlus v0.3
    14:59:06 [INFO] Server permissions file permissions.yml is empty, ignoring it
    14:59:06 [INFO] Done (2.835s)! For help, type "help" or "?"

    Code:
    package me.duncan.basic;
     
    import java.util.logging.Logger;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Basic extends JavaPlugin {
       
        Logger log;
       
        @Override
        public void onEnable() {
            log = this.getLogger();
            log.info("Plugin Enabled!");
            getCommand("basic").setExecutor(new CommandBasic(this));
            getCommand("hello").setExecutor(new CommandHello());
            getCommand("food").setExecutor(new CommandFood());
            getCommand("complaint").setExecutor(new CommandComplaint());
            getCommand("howareyou").setExecutor(new CommandHowAreYou());
            getCommand("question").setExecutor(new CommandQuestion());
        }
       
        @Override
        public void onDisable() {
            log.info("Plugin Disabled!");
        }
           
        }
     
  4. Offline

    CorrieKay

    lets see your plugin.yml, getCommand("basic") is returning null.
     
    frogman6102 likes this.
  5. Offline

    LocoCat

    I tried the same thing with a different command, and i think i found the problem. the line
    Code:
    public CommandBasic(Basic plugin){
    references back to the main class. but here's the plugin.yml (the extra commands in there all have their own references and classes BTW)

    Code:
    name: Basic
    version: 1.0 ALPHA
    description: Testing ATM.
    author: Duncan
    main: me.duncan.basic.Basic
     
     
    commands:
      basic:
        description: Testing Testing 123!
        usage: /basic /b
        aliases: [b]
        permission: CommandBasic.Basic
     
     
    commands:
      hello:
        description: Testing Testing 123!
        aliases: [hi]
        permission: CommandHello.Hello
        usage: /hello /hi
     
    commands:
      howareyou:
        description: Testing Testing 123!
        aliases: [hay, hey, whatsup, whattup]
        permission: CommandHowAreYou.howareyou
        usage: /howareyou /hay /hey /whatsup /whattup
     
    commands:
      complaint:
        description: Testing Testing 123!
        aliases: [complain, dontlike]
        permission: CommandComplaint.Complaint
        usage: /complain /complaint /dontlike
     
    commands:
      question:
        description: Testing Testing 123!
        aliases: [?, q]
        permission: CommadnQuestion.Question
        usage: /question /? /q
     
    commands:
      food:
        description: Testing Testing 123!
        aliases: [feed, cookies]
        permission: CommandFood.Food
        usage: /food /feed /cookies
    maybe it's because it says "commands" instead of "command"?
     
  6. Offline

    CorrieKay

    wow, woah, theyre all supposed to be under the same configuration section..

    try this instead
    Code:
     
    name: Basic
    version: 1.0 ALPHA
    description: Testing ATM.
    author: Duncan
    main: me.duncan.basic.Basic
    commands:
      basic:
        description: Testing Testing 123!
        usage: /basic /b
        aliases: [b]
        permission: CommandBasic.Basic
      hello:
        description: Testing Testing 123!
        aliases: [hi]
        permission: CommandHello.Hello
        usage: /hello /hi
      howareyou:
        description: Testing Testing 123!
        aliases: [hay, hey, whatsup, whattup]
        permission: CommandHowAreYou.howareyou
        usage: /howareyou /hay /hey /whatsup /whattup
      complaint:
        description: Testing Testing 123!
        aliases: [complain, dontlike]
        permission: CommandComplaint.Complaint
        usage: /complain /complaint /dontlike
      question:
        description: Testing Testing 123!
        aliases: [?, q]
        permission: CommadnQuestion.Question
        usage: /question /? /q
      food:
        description: Testing Testing 123!
        aliases: [feed, cookies]
        permission: CommandFood.Food
        usage: /food /feed /cookies
    also, you cant use capitalizations in your permissions, they will fail

    yml is very very picky about its syntax :p
     
  7. Offline

    LocoCat

    alright, tried that, copied and pasted it right in. started up fine, then i tried the "basic" command and it gave me this error. Do you think it has anything to do with the line
    Code:
    public CommandBasic(Basic plugin){
    and it referencing back to the main class? here's the error:


    Code:
    03:23 PM [SEVERE] Exception in thread "AWT-EventQueue-0"
    03:23 PM [SEVERE] org.bukkit.command.CommandException: Unhandled exception executing command 'basic' in plugin Basic v1.0 ALPHA
    03:23 PM [SEVERE] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
    03:23 PM [SEVERE] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
    03:23 PM [SEVERE] at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:473)
    03:23 PM [SEVERE] at me.escapeNT.pail.GUIComponents.ServerConsolePanel$ConsoleCommandListener.actionPerformed(ServerConsolePanel.java:78)
    03:23 PM [SEVERE] at javax.swing.JTextField.fireActionPerformed(Unknown Source)
    03:23 PM [SEVERE] at javax.swing.JTextField.postActionEvent(Unknown Source)
    03:23 PM [SEVERE] at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
    03:23 PM [SEVERE] at javax.swing.SwingUtilities.notifyAction(Unknown Source)
    03:23 PM [SEVERE] at javax.swing.JComponent.processKeyBinding(Unknown Source)
    03:23 PM [SEVERE] at javax.swing.JComponent.processKeyBindings(Unknown Source)
    03:23 PM [SEVERE] at javax.swing.JComponent.processKeyEvent(Unknown Source)
    03:23 PM [SEVERE] at java.awt.Component.processEvent(Unknown Source)
    03:23 PM [SEVERE] at java.awt.Container.processEvent(Unknown Source)
    03:23 PM [SEVERE] at java.awt.Component.dispatchEventImpl(Unknown Source)
    03:23 PM [SEVERE] at java.awt.Container.dispatchEventImpl(Unknown Source)
    03:23 PM [SEVERE] at java.awt.Component.dispatchEvent(Unknown Source)
    03:23 PM [SEVERE] at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    03:23 PM [SEVERE] at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    03:23 PM [SEVERE] at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    03:23 PM [SEVERE] at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    03:23 PM [SEVERE] at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    03:23 PM [SEVERE] at java.awt.Component.dispatchEventImpl(Unknown Source)
    03:23 PM [SEVERE] at java.awt.Container.dispatchEventImpl(Unknown Source)
    03:23 PM [SEVERE] at java.awt.Window.dispatchEventImpl(Unknown Source)
    03:23 PM [SEVERE] at java.awt.Component.dispatchEvent(Unknown Source)
    03:23 PM [SEVERE] at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    03:23 PM [SEVERE] at java.awt.EventQueue.access$000(Unknown Source)
    03:23 PM [SEVERE] at java.awt.EventQueue$3.run(Unknown Source)
    03:23 PM [SEVERE] at java.security.AccessController.doPrivileged(Native Method)
    03:23 PM [SEVERE] at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    03:23 PM [SEVERE] at java.awt.EventQueue$4.run(Unknown Source)
    03:23 PM [SEVERE] at java.security.AccessController.doPrivileged(Native Method)
    03:23 PM [SEVERE] at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    03:23 PM [SEVERE] at java.awt.EventQueue.dispatchEvent(Unknown Source)
    03:23 PM [SEVERE] at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    03:23 PM [SEVERE] at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    03:23 PM [SEVERE] at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    03:23 PM [SEVERE] at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    03:23 PM [SEVERE] at java.awt.EventDispatchThread.run(Unknown Source)
    03:23 PM [SEVERE] Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    03:23 PM [SEVERE] at me.duncan.basic.CommandBasic.onCommand(CommandBasic.java:15)
    03:23 PM [SEVERE] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
    03:23 PM [SEVERE] ... 42 more 

    EDIT: i won't be on for the next 60-90 minutes, gotta get home from school, so don't give up on this thread. (7th grade sucks)
     
  8. Offline

    CorrieKay

    okay, youre getting a class cast exception..
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player

    are you trying to access this by typing the command into the console?
     
  9. Offline

    LocoCat

    i should probably try typing it as a player. the only problem was that i was in school so i really couldn't at the time.


    EDIT: alright, it said what it should, but then it said after that that an internal error occurred while attempting to perform this command.
     
  10. Offline

    CorrieKay

    what was the stacktrace
    edit: log = this.getLogger();
    this is returning null, isnt it? you dont extend a listener as your plugin. use Bukkit.getLogger() instead
     
  11. Offline

    LocoCat

    okay. thank you.

    and i was just about to start playing TF2 :( lol

    alright. changed log = this.getLogger(); to log = Bukkit.getLogger(); and it returns the same error ingame and nothing shows up in the server console.

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

    evilmidget38

    You could always get the logger this way:
    Code:
     Logger log = Logger.getLogger("Minecraft");
     
  13. Offline

    CorrieKay

    then lets see your stacktrace and current code.
     
  14. Offline

    LocoCat

    but the thing is, above it i have Logger log;
    where can i get the stacktrace from? i normally use netbeans, not eclipse. can i use the logcat log?
     
  15. Offline

    Komak57

    try {} catch (Exception e) { e.printStackTrace(); } This will give you the same output as any normal error, but will continue your plugin past the statement.
     
  16. Offline

    evilmidget38

    You can replace the Logger log at the top with what I posted.
     
  17. Offline

    LocoCat

    Okay, so the code is as follows:

    Code:
    package me.duncan.basic;
     
    import java.util.logging.Logger;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Basic extends JavaPlugin {
     
        Logger log = Logger.getLogger("Minecraft");
     
        @Override
        public void onEnable() {
            log.info("Plugin Enabled!");
            getCommand("basic").setExecutor(new CommandBasic(this));
            getCommand("hello").setExecutor(new CommandHello());
            getCommand("food").setExecutor(new CommandFood());
            getCommand("complaint").setExecutor(new CommandComplaint());
            getCommand("howareyou").setExecutor(new CommandHowAreYou());
            getCommand("question").setExecutor(new CommandQuestion());
        }
     
        @Override
        public void onDisable() {
            log.info("Plugin Disabled!");
        }
         
        }
    BTW, i'm in school so i can't really test the plugin.

    EDIT: also, how do i get the stacktrace in eclipse?

    what i want to do is not continue the plugin, but to fix it.

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

    Komak57

    Template.java:
    Code:
    package pkg;
     
    import java.util.logging.Logger;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Template extends JavaPlugin {
        private Commands Commander;
        static Logger log;
     
        public void onEnable() {
            log = this.getLogger();
            Commander = new Commands(this);
            // player commands
            getCommand("run").setExecutor(Commander);
            // listeners
            //new myBlockListener(this);
            log.info("Template: Enabled");
        }
        public void onDisable(){
            log.info(" Template : Disabled");
        }
    }
    
    Commands.java:
    Code:
    package pkg;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class Commands implements CommandExecutor {
        @SuppressWarnings("unused")
        private Template plugin;
     
        public Commands(Template plugin) {
            this.plugin = plugin;
        }
     
        @SuppressWarnings("unused")
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            boolean IsPlyr = false;
            Player player = null;
            if (sender instanceof Player) {
                player = (Player) sender;
                IsPlyr = true;
            }
            if (IsPlyr == false) { // console commands
                if (cmd.getName().equalsIgnoreCase("run")) {
                    // command functions for a console using the command /run
                    // in here, you will need to designate a target if you wish to interact with a player
                }
            } else { // is player commands
                if (cmd.getName().equalsIgnoreCase("run")) {
                    // command functions for a player using /run
                    // in here, you can auto-cast functions based on the player
                }
            }
            return false;
        }
    }
    
    plugin.yml:
    Code:
    name: Template
    main: pkg.Template
    version: 0.0 - 1.2.5
    commands:
      run:
        description: Runs a run-time compiler based on the input.
        usage: /<command> [script1;[...]]
        permission: Template.run
        permission-message: This command requires a higher permission!
    This is your basic command template for 1.2.5-R1.0, make the modifications necessary
     
  19. Offline

    LocoCat

    that should work, i'll try it when i get back from lunch.
     
  20. Offline

    CorrieKay

    the stacktrace is the error printed out to the console when you run the plugin and it throws an exception.
     
  21. Offline

    LocoCat

    the thing is, it doesn't give me an error in the console. just ingame, when it says that there is an internal error.
     
  22. Offline

    CorrieKay

    wat.

    that makes no sense. it must be being caught somwhere and not having the stacktrace printed.
     
Thread Status:
Not open for further replies.

Share This Page