Some Sub commands not working

Discussion in 'Spigot Plugin Development' started by zackatack108, May 26, 2016.

  1. Okay I'm having an issue with some commands for my plugin. So right now I only have one main command with three sub commands. The thing is only two of the sub commands work and the third one I just get an error in Minecraft when I try to run the command.

    The Commands that I have are:
    • Show Create
    • Show Remove
    • Show List
    Show list is the command that's not working correctly. In the game I keep getting "An internal error occurred while attempting to perform this command"

    Command Code
    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
           
            Player player = (Player) sender;
           
            //Show List Add
            ArrayList<String> add = (ArrayList<String>) plugin.getConfig().getStringList("Shows");
            add.add(args[1]);
           
            //Show List Remove
            ArrayList<String> remove = (ArrayList<String>) plugin.getConfig().getStringList("Shows");
            remove.remove(args[1]);
            plugin.getConfig().set("Shows", remove);
           
            if(Label.equalsIgnoreCase("Show")){
               
                //Add a show to the shows list
                if(args[0].equalsIgnoreCase("Create") || args[0].equalsIgnoreCase("Add")){
                    if(args.length==2){
                        plugin.getConfig().set("Shows", add);
                        player.sendMessage(ChatColor.GREEN + "Show " + args[1] + " created.");
                        plugin.saveConfig();
                    }              
                }
               
                //Remove a show from the shows list
                else if(args[0].equalsIgnoreCase("Remove") || args[0].equalsIgnoreCase("Delete")){
                    if(args.length==2){
                        plugin.getConfig().set("Shows", remove);
                        player.sendMessage(ChatColor.RED + "Show " + args[1] + " Removed.");
                        plugin.saveConfig();
                    }
                }
               
                else if(args[0].equalsIgnoreCase("List")){
                    player.sendMessage(ChatColor.GOLD + "Shows that currently exist: " + plugin.getConfig().getStringList("Shows"));
                }
            }          
           
            return true;
        }
    }
    Console Error Code.
    Code (Text):
    show list
    [22:20:07 WARN]: Unexpected exception while parsing console command "show list"
    org.bukkit.command.CommandException: Unhandled exception executing command 'show' in plugin FireworkShow v1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-944aa20-420b983]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-944aa20-420b983]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot.jar:git-Spigot-944aa20-420b983]
            at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchServerCommand(CraftServer.java:631) [spigot.jar:git-Spigot-944aa20-420b983]
            at net.minecraft.server.v1_9_R1.DedicatedServer.aL(DedicatedServer.java:438) [spigot.jar:git-Spigot-944aa20-420b983]
            at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:401) [spigot.jar:git-Spigot-944aa20-420b983]
            at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot.jar:git-Spigot-944aa20-420b983]
            at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot.jar:git-Spigot-944aa20-420b983]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_9_R1.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
            at me.zackatack108.FireworkShow.Commands.onCommand(Commands.java:20) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-944aa20-420b983]
            ... 8 more
    >
    Any Idea what's going on?
     

  2. willing to show imported packages ?
     
  3. Are you wanting to see the other classes? I only have one other class and that's the main class.
     
  4. not that I mean imported class from api you're using

    seen like this above

    Code (Java):
    import org.blablabla
     
  5. Okay.
    Code (Text):
    import java.util.ArrayList;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
  6. check this one

    Code (Java):
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_9_R1.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    have you misspelled something in your imported class ?
     
  7. What's on line 20?
     
  8. Line 20 is Player player = (Player) sender;
     
  9. Always check if the sender is an instance of a player for commands that can only be run by players.
     
  10. Indeed this is a way to check if the commander is console or command block , too.
     
  11. Don't blind cast to player, as stated above:
    Don't blind cast to List, either. Remove the casting and save "add" and "remove" as List<String> variables instead of ArrayList<String> variables to ensure type safety.

    Another thing that you aren't doing properly is checking for arguments, you're using them before checking if they exist.
     

  12. wat ?? so you posted this message to the wrong thread ?
     
  13. Okay this is what the command looks like now. It's still not working though.

    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
           
            //Show List Add
            ArrayList<String> add = (ArrayList<String>) plugin.getConfig().getStringList("Shows");
            add.add(args[1]);
           
            //Show List Remove
            ArrayList<String> remove = (ArrayList<String>) plugin.getConfig().getStringList("Shows");
            remove.remove(args[1]);
            plugin.getConfig().set("Shows", remove);
           
            if(cmd.getName().equalsIgnoreCase("Show")){
               
                if(sender instanceof Player){
                   
                    Player player = (Player) sender;
                   
                    //Add a show to the shows list
                    if(args[0].equalsIgnoreCase("Create") || args[0].equalsIgnoreCase("Add")){
                        if(args.length==2){
                            plugin.getConfig().set("Shows", add);
                            player.sendMessage(ChatColor.GREEN + "Show " + args[1] + " created.");
                            plugin.saveConfig();
                        }              
                    }
                   
                    //Remove a show from the shows list
                    else if(args[0].equalsIgnoreCase("Remove") || args[0].equalsIgnoreCase("Delete")){
                        if(args.length==2){
                            plugin.getConfig().set("Shows", remove);
                            player.sendMessage(ChatColor.RED + "Show " + args[1] + " Removed.");
                            plugin.saveConfig();
                        }
                    }
                   
                    //List all shows that exist
                    else if(args[0].equalsIgnoreCase("List")){
                        player.sendMessage(ChatColor.GOLD + "Shows that currently exist: " + plugin.getConfig().getStringList("Shows"));
                    }
                }          
            }          
           
            return true;
        }
    }
     
  14. Nope? Msrules123 has a perfectly legit point. Build off the interface, not its implementation.

    @OP
    You should really learn Java a little bit more...
     
    • Agree Agree x 1
  15. check args length and define what's the error with ?
     
  16. No. All messages of mine go in their correct threads. I'd suggest reading that, in the thread you are referring to, the only one who pasted code was the original poster of the thread in question.
    Still not working is what? Are there errors in console, commands not running itself? Make sure you registered your commands, and add debugging messages to see exactly where your code stops at a breakpoint.
     
    • Creative Creative x 1
  17. So the commands work when I comment them out and test them one at a time. the list that the commands create are also working. The issue I'm having as stated in the first post was that the "Show list" command isn't working when I have all three commands set up.
     
  18. I'm well aware that I need to learn more java. But the thing is I can't just blindly watch how to videos for java without putting it to use. Minecraft is the only way I know how to put it to practical use.
     
  19. So the reason why I'm using an arraylist is because it's the only thing I could get working. I did a lot of searching on how to input a command to be saved to a config and that was the only way that worked for my code.
     
  20. ArrayList and List have the same external methods that I can recall (ArrayList has to define all the abstract methods defined by List), so using the List as opposed to ArrayList should have no effect on your code at all, other than reducing blind casting and building against the interface.