Solved Commands show, but don't work

Discussion in 'Plugin Development' started by SuperEvan200, Feb 28, 2013.

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

    SuperEvan200

    I'm making a new RPG styled teams plugin. So far, very simple. Here is my code.

    Code:
    package me.superevan200.kingdoms;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Kingdoms extends JavaPlugin{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Kingdoms plugin;
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Disabled!");
        }
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
        }
       
        String kingdom1 = ChatColor.DARK_BLUE + "[Delolia] " + ChatColor.RESET;
        String kingdom2 = ChatColor.DARK_AQUA + "[Syviel] " + ChatColor.RESET;
        String kingdom3 = ChatColor.DARK_GREEN + "[Natura] " + ChatColor.RESET;
        String kingdom4 = ChatColor.DARK_RED + "[Valkyrie] " + ChatColor.RESET;
     
        public boolean onCommand(CommandSender sender, Command cmd,  String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("kingdoms help")){
                player.sendMessage(ChatColor.DARK_RED + "To join a kingdom, say /k join Delolia, Siviel, Natura, or Valkyerie");
            }
            if(commandLabel.equalsIgnoreCase("k join Delolia")){
                player.setDisplayName(kingdom1 + player.getDisplayName());   
            }
            if(commandLabel.equalsIgnoreCase("k join Syviel")){
                player.setDisplayName(kingdom2 + player.getDisplayName());   
            }
            if(commandLabel.equalsIgnoreCase("k join Natura")){
                player.setDisplayName(kingdom3 + player.getDisplayName());   
            }
            if(commandLabel.equalsIgnoreCase("k join Valkyrie")){
                player.setDisplayName(kingdom4 + player.getDisplayName());   
            }
            return false;
        }
    }
    
    The plugin loads fine with no errors. When I say /help, all the commands show up. However, if I execute any of the commands I coded (In game) it says "Unknown Command. Type "help" for help." Please help!

    Thanks. :)
     
  2. Offline

    Tomskied

    Have you registered your commands in your plugin.yml?
     
    SuperEvan200 likes this.
  3. Offline

    AmShaegar

    Did you register them in plugin.yml?

    Code:
    commands:
      all:
        description: "Let's you chat with everyone."
        usage: "Usage: /all [message]"
     
  4. Offline

    SuperEvan200

    This is what my plugin.yml looks like. I have registered them, but not added a usage. Is this required for the commands to work properly?

    Code:
    name: Kingdoms
    main: me.superevan200.kingdoms.Kingdoms
    version: 1.0
    description: An RPG team based plugin
    commands:
      kingdom help:
        description: See what kingdoms are available to join.
      k join Delolia:
        description: Join the Kingdom Delolia
      k join Syviel:
        description: Join the Kingdom Syviel
      k join Natura:
        description: Join the Kingdom Natura
      k join Valkyrie:
        description: Join the Kingdom Valkyrie
     
  5. Offline

    AmShaegar

    I am not sure, sorry. But I think your problem is different. The command is just one word without spaces. Everything after that are parameters/arguments. You have to register kingdom and k. Everything else should be handled by your CommandListener.
     
  6. Offline

    Tomskied

    What AmShaegar said. Register your first argument in the plugin.yml and handle the rest with arguments.
     
    SuperEvan200 likes this.
  7. Offline

    SuperEvan200

    I'm a bit confused here. How do I do this? Do I need to edit the actual code for this as well?
    Thanks.
     
  8. Offline

    AmShaegar

    Show your code ;)

    Your arguments are stored in args. So you have to check that array for all you want. You would create the k command and devide it into subsections for you first argument.

    Code:
    if(commandLabel.equalsIgnoreCase("k")) {
        if(args[0].equalsIgnoreCase("join"))
            if(args[1].equalsIgnoreCase("delolia"))
                ...
            else if(args[1].equalsIgnoreCase("syviel"))
    etc.
     
    SuperEvan200 likes this.
  9. Offline

    SuperEvan200

    Thanks for the help. Just to clear this up, is this correct?

    Code:
        public boolean onCommand(CommandSender sender, Command cmd,  String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("k")){
            if(args[0].equalsIgnoreCase("join"))
            if(args[1].equalsIgnoreCase("delolia"))
                player.setDisplayName(kingdom1 + player.getDisplayName());     
            }
    Also, for the plugin.yml, how would I register the commands? Should I keep them as is?
     
  10. Offline

    AmShaegar

    Looks correct so far. plugin.yml should look somehow like this:

    Code:
    name: Kingdoms
    main: me.superevan200.kingdoms.Kingdoms
    version: 1.0
    description: An RPG team based plugin
    commands:
      kingdom:
        description: See what kingdoms are available to join.
      k:
        description: "Join the Kingdom Delolia, Syviel ... etc"
        usage: "Type: /k join <kingdom>"
     
    SuperEvan200 likes this.
  11. Offline

    SuperEvan200

    Something appears to be wrong. /kingdom help works. When I do /k however, it says:
    "An internal error occurred while attempting to perform this command"

    Here is what appears on the console:
    Code:
    20:01:21 [INFO] SuperEvan200 issued server command: /k
    20:01:21 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'k' in plugin Kingdoms v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
            at org.bukkit.craftbukkit.v1_4_R1.CraftServer.dispatchCommand(CraftServer.java:514)
            at net.minecraft.server.v1_4_R1.PlayerConnection.handleCommand(PlayerConnection.java:980)
            at net.minecraft.server.v1_4_R1.PlayerConnection.chat(PlayerConnection.java:898)
            at net.minecraft.server.v1_4_R1.PlayerConnection.a(PlayerConnection.java:853)
            at net.minecraft.server.v1_4_R1.Packet3Chat.handle(Packet3Chat.java:44)
            at net.minecraft.server.v1_4_R1.NetworkManager.b(NetworkManager.java:290)
            at net.minecraft.server.v1_4_R1.PlayerConnection.d(PlayerConnection.java:113)
            at net.minecraft.server.v1_4_R1.ServerConnection.b(SourceFile:39)
            at net.minecraft.server.v1_4_R1.DedicatedServerConnection.b(SourceFile:30)
            at net.minecraft.server.v1_4_R1.MinecraftServer.r(MinecraftServer.java:598)
            at net.minecraft.server.v1_4_R1.DedicatedServer.r(DedicatedServer.java:224)
            at net.minecraft.server.v1_4_R1.MinecraftServer.q(MinecraftServer.java:494)
            at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java:427)
            at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.superevan200.kingdoms.Kingdoms.onCommand(Kingdoms.java:40)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
            ... 15 more
    >
    
    When I type /k join, I get the same error.

    When I type /k join nameofkingdom, for example /k join Delolia, it says:
    "Type: /k join <kingdom>" and does not give me the prefix for the kingdom.

    Sorry, forgot to put this also. Here is my updated code.

    Code:
        public boolean onCommand(CommandSender sender, Command cmd,  String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("kingdom")){
            if(args[0].equalsIgnoreCase("help"))
                player.sendMessage(ChatColor.DARK_RED + "To join a kingdom, say /k join Delolia, Siviel, Natura, or Valkyerie");
            }
            if(commandLabel.equalsIgnoreCase("k")){
            if(args[0].equalsIgnoreCase("join"))
            if(args[1].equalsIgnoreCase("delolia"))
                player.setDisplayName(kingdom1 + player.getDisplayName());       
            }
            if(commandLabel.equalsIgnoreCase("k")){
            if(args[0].equalsIgnoreCase("join"))
            if(args[1].equalsIgnoreCase("syviel"))
                player.setDisplayName(kingdom2 + player.getDisplayName());   
            }
            if(commandLabel.equalsIgnoreCase("k")){
            if(args[0].equalsIgnoreCase("join"))
            if(args[1].equalsIgnoreCase("natura"))
                player.setDisplayName(kingdom3 + player.getDisplayName());   
            }
            if(commandLabel.equalsIgnoreCase("k")){
            if(args[0].equalsIgnoreCase("join"))
            if(args[1].equalsIgnoreCase("valkyrie"))
                player.setDisplayName(kingdom4 + player.getDisplayName());   
            }
            return false;
     
  12. Offline

    AmShaegar

    • return false; causes the Plugin to print the "Usage" message.
    • Your error appears when you do not have enough parameters. you have to ensure that therer are enough to access args[1] so you need to check if args.length == 2
    • you dont have to check for "k" and "join" every time. Just once and include all the kingdoms into { ....}
     
  13. Offline

    SuperEvan200


    Thanks alot. :) Its working perfectly now.

    I changed:
    "return false;" to
    "return true;"
    Because eclipse indicated it needed a return statement.

    Thanks once again. :)
     
  14. Offline

    AmShaegar

    To make it perfect, you should return false when there are not enough arguments, no join or not a real kingdom.
     
    SuperEvan200 likes this.
  15. Offline

    BajanAmerican

    Instead of saying "if" for the command label, if you want to have multiple commands for the plugin.yml to pick it up, it has to be "else if". So, put "else if" for all the "if"'s after the original "if". Is that too confusing? Sorry, I am still a beginner, lol
     
    SuperEvan200 likes this.
  16. Offline

    jackiejoe4

    Noticed a few "flaws" in the coding:

    1. You can join each team as many times as you would like and when you are in a team you can join another meaning you can stack the prefixes
    Example: /k join natura -would give me the prefix of [natura] jackiejoe4, but if i type it in again i get the prefix of [natura] [natura] jackiejoe4.

    also if i typed in /k join valkyrie, i get the prefix [valkyrie] [natura] [natura] causing a stacking effect.

    2. there is no message saying you joined the team, to fix below the each of the code lines player.setDisplayName
    put this command player.sendMessage(ChatColor.InsertColor + "You have joined <insert kingdom>!")

    [edit] 3. need a way to leave your team

    hoped this helped!
     
    SuperEvan200 likes this.
  17. Offline

    SuperEvan200

    Thanks. So what I'm getting here is if the player says the wrong command I can make the else if tell him the proper syntax/command.

    Thanks. I'll play around with the code to see what I can fix myself.

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

    jackiejoe4

    Figured out how to fix the problem of name tag stacking and to do a /leave team.
    its actually pretty simple:
    just add player.setDisplayName(player.getName()); right above the code for setting the prefix
    use the same code to make a /leave team
     
    SuperEvan200 likes this.
Thread Status:
Not open for further replies.

Share This Page