Solved command arguments not working

Discussion in 'Plugin Development' started by shohouku, Jul 3, 2014.

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


    I don't know whats wrong...

    I get no errors also, nothing happens.

    This is what it's suppose to look like:

    /createnpc Xephos Notch

    Xephos = Minecraft Name
    Notch = Minecraft Skin

    then it'll create the npc where the player who sent the message is standing

    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. final Player p = (Player) sender;
    3. final Plugin plugin = Bukkit.getPluginManager().getPlugin("NPC");
    4. final Location location = p.getLocation();
    5. final NPCFactory factory = new NPCFactory(this);
    6. if(label.equalsIgnoreCase("createnpc")){
    7. if (args.length == 1) {
    8. if(args.length == 2) {
    9. final String name = args[1];
    10. final String skin = args[2];
    11. new Thread() {
    12. @Override
    13. public void run() {
    14. Bukkit.getScheduler().runTask(plugin, new Runnable() {
    15. @Override
    16. public void run() {
    17. final NPCProfile profile = new NPCProfile(name, skin);
    18. final NPC npc = factory.spawnHumanNPC(location, profile);
    19. npc.setYaw(location.getYaw());
    20. npc.lookAt(p.getLocation());
    21. npc.setTarget(p);
    22. p.sendMessage(name + " was spawned!");
    23. }
    24. });
    25. }
    26. }.start();
    27. }
    28. }
    29. }
    30. return false;
    31. }
  2. Offline


    shohouku is your command in the plugin.yml?
  3. Offline



    name: NPC
    main: plugin.Main
    version: 0.1
    author: NPC
        description: /createnpc [name] [skin].
  4. Offline


    shohouku why is everything final? also is the oncommand class in the same as on enable or in a seperate class file?
  5. Offline


    1. Making all your variables final is rather excessive.
    2. Before casting CommandSender to Player, check if it can be casted to Player
    3. Invoking Bukkit.getPluginManager().getPlugin("NPC") every time the command is ran is inefficient. I suggest creating a constant instead. The same goes for a few other variables you're defining.
    4. There is no need to create a new thread
    5. Why do you create a bukkit scheduler task in an unwarranted thread?
  6. Offline


    I'v tried everything, nothing seems to have changed...

    Maybe it's my API that I'm using.
  7. Offline


    shohouku Its the logic of your code.
    if (args.length == 1) {
    if(args.length == 2) {
    Will never get passed this, and should be fairly obvious what the problem is.
    mythbusterma likes this.
  8. Offline


    No it's the logical fallacy of lines 7 and 8 in your code snippet, under no circumstances is the length of array both one and two. Instead make one if statement that checks if the length of args is greater than or equal to two.
  9. Offline


    xTigerRebornx That, and he is using args[1] and args[2] instead of args[0] and args[1] as well
    shohouku likes this.
  10. Offline


    I see two options: 1) The Player(in this case, the sender) and the Location, as well as the String name and String skin shouldn't be final, at least in my own personal opinion. Try to remove that and then keep us posted. 2) WHY would you do this:
    1. if(args[1) {
    3. // There's nothing in here
    4. // You always start from 0, for 0 in Java = 1
    6. if(args[2]) {
    7. }
    8. }

    Try to fix these and we'll see how it goes.
Thread Status:
Not open for further replies.

Share This Page