Help with CommandListener.

Discussion in 'Plugin Development' started by Phinary, Jul 7, 2012.

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

    Phinary

    Hey. I have only been coding Java for about 2 weeks now, so my experience is very low. I do have quite a bit of experience with PHP though. Basically, I am trying to make a commandlistener for a plugin that has quite a bit of different commands. My biggest issue here is that I am wanting to do a switch statement to figure out which command they did, then execute methods from there. Basically, I have most of the commandListener done, the issue is that switch doesn't seem to let you use strings with it inside java 1.6, and I don't want to switch to 1.7 since it breaks a lot of my code. My big question is, should I just use if statements, or is there some way I can get this to work using switch statements. (from my experiences switch statements are faster and cleaner then a bunch of if statements, so I try to use that instead) Here is the code:

    Code:
    package me.shaun.basic.Listeners;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    import me.shaun.basic.basics;
     
    public class CommandListener extends basics {
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player) sender;
            if (sender instanceof Player) {
                //User is a player. Continue
               
               
               
               
               
               
            } else { //User is not a player, console sent command
                sender.sendMessage("[" + ChatColor.AQUA + "nlBasics" + ChatColor.WHITE + "] " + ChatColor.DARK_RED + "You must be a player to use this command!");
            }
           
           
           
           
           
           
            return false;
        }
       
       
       
       
       
    }

    I was trying to do a switch statement with commandLabel, but ofcourse that did not work, so what would be my best option.

    Sorry for my noobness.
     
  2. most people use java 6 for their servers, and at java 6, there is no switch statement for string, so you need to if/else on the cmd.getName()
     
  3. Offline

    Phinary

    Okay, I have a new question. I have 2 classes now. All the plugins I have made in the past have only used 1 class. Basically, I have a main class that stores a bunch of methods and stuff, then a folder called Listeners and inside a class called CommandListener that says what to do when a command is done and directs it to the methods and stuff. Basically my issue is that I believe when I run the plugin, the command listener isn't initialized right when the server starts up, so I think I need to add something to the onEnable thing so that it initializes the commandlistener when the server starts up since it is inside a different class. Any idea how I would go about doing this? I could be completely wrong though, this is just my best guess.
     
  4. Offline

    Firefly

    You need for that class to implement CommandExecutor and have your onEnable register that class as the executor for whatever command you want.
     
Thread Status:
Not open for further replies.

Share This Page