"Unknown command" Error

Discussion in 'Plugin Development' started by DefiantPie, Apr 10, 2014.

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

    DefiantPie

    My friend asked me to make him a plugin that shortens /warp survival to just /survival. But I am having some errors, most of which I fixed by myself but now when I type "/survival" It just comes up with "Unknown command" in chat. There are no errors in the console or anything.

    Heres my code
    Show Spoiler

    Code:java
    1. package me.DefiantPie.Teleports;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Location;
    5. import org.bukkit.World;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class main extends JavaPlugin {
    12.  
    13. public void onEnable(){
    14. getLogger().info("SUPA EPIC TELEPORT ENABLED");
    15. getConfig().options().copyDefaults(true);
    16. saveConfig();
    17. }
    18.  
    19. public void onDisable(){
    20. getLogger().info("SUPA MLG TELEPORT DISBALED!");
    21. saveConfig();
    22. }
    23.  
    24.  
    25. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    26. Player player = (Player) sender;
    27. if(cmd.getName().equalsIgnoreCase("survival")){
    28. int x = getConfig().getInt("survival.x"), y = getConfig().getInt("survival.y"), z = getConfig().getInt("survival.z");
    29. World world = Bukkit.getWorld("world");
    30. player.teleport(new Location(world, x, y, z));
    31.  
    32. } else if(cmd.getName().equalsIgnoreCase("minigames")){
    33. int x = getConfig().getInt("minigames.x"), y = getConfig().getInt("minigames.y"), z = getConfig().getInt("minigames.z");
    34. World games = Bukkit.getWorld("games");
    35. player.teleport(new Location(games, x, y, z));
    36.  
    37. }else if(cmd.getName().equalsIgnoreCase("city")){
    38. int x = getConfig().getInt("city.x"), y = getConfig().getInt("city.y"), z = getConfig().getInt("city.z");
    39. World world = Bukkit.getWorld("world");
    40. player.teleport(new Location(world, x, y, z));
    41.  
    42. }else if(cmd.getName().equalsIgnoreCase("shop")){
    43. int x = getConfig().getInt("shop.x"), y = getConfig().getInt("shop.y"), z = getConfig().getInt("shop.z");
    44. World world = Bukkit.getWorld("world");
    45. player.teleport(new Location(world, x, y, z));
    46. }
    47. return false;
    48. }
    49. }
    50.  
    51.  


    I am using Bukkit for Minecraft 1.7.2
    If you need more info, just ask
     
  2. Offline

    RawCode

    post plugin.yml
     
  3. Offline

    Pik0

    I usualy dont use cmd.getName(), I use label.equalsIgnoreCase() and it works. Try to use label.equalsIgnoreCase() and dont forget to register the commands in the plugin.yml
     
  4. Offline

    DefiantPie

    So it would be: if(label.equalsIgnoreCase()("survival"))?
    Because when I do that eclipse gives me an error that says "The left-hand side of an assignment must be a variable"

    EDIT: nvm, im stupid lol
     
  5. Offline

    ImPhantom

    DefiantPie
    Register your commands in your onEnable.
    getCommand("command").setExecutor(this);
     
  6. Offline

    DefiantPie

    And here is my plugin.yml
    Code:
    name: HaydensTeleportThing
    version: 1.0
    main: me.DefiantPie.Teleports.main
     
    commands:
      survival:
      description: Teleport to survival
      usage: /survival
      minigames:
        description: Teleports you to the minigames world
        usage: /minigames
      city:
      description: Teleports you to the city
      usage: /city
      shop:
      description: Teleports you to the shop
      usage: /shop
    I have tried everything here, registering the commands in onEnable did nothing, and changing the code to cmd.getName() didnt work either :/

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

    Twisted_Panda

    You dont need to do that if its in the Main class.

    Try removing descriptions and usages.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  8. Offline

    DefiantPie

    I figured out the problem and it had nothing to do with the code...
    FOR SOME REASON which I will never understand, there were 2 servers being hosted on my localhost, 1 had the plugin and the other didnt, the one i was connecting to didnt have it. But the plugin works fine now, but something else is happening. When i type the command it teleports me like it should, but it prints /survival in the chat
     
  9. Offline

    RawCode

    multiple servers on same machine maynot effect each other - they ran on different JVM instances and maynot see each other due mamory barriers.
     
  10. No offense, but this here is a bad way to do it. Using label rather than cmd.getName() reduces the support for aliases. You should always use cmd.getName() unless you have good reason for doing otherwise. But in either case, there's no reason label should've worked if cmd didn't.

    DefiantPie The return in the onCommand is basically a "did this command work?" If it did (returning true) then good! But if it didn't (return false) then it displays the usage. Put in return true whenever you know that the command has been successful and you want to end the checking of it (i.e. after the person has been teleported).

    Edit:

    I think he was just saying that he was uploading to one server but testing on a different one, basically. :)
     
  11. Offline

    Pik0

    This is the way i do it. I started coding plugins doing like this and i keep like this. I dont know if the problem was my bukkit file in that time but cmd.getName never worked for me. I usually do label.equalsIgnoreCase("blabla) || label...
    and then add all the aliases one by one in the plugin.yml
    Usually i use 3 or 4 aliases so its not that pain
     
  12. Pik0 But the beauty of doing cmd.getName() is that you can have any amount of aliases and all you need to do is add them to the plugin.yml. If you only intend to add 3 or 4, then sure, you could do it manually. But what about the server admins? They can change the plugin.yml too, you know. And I know for sure that some of them do that for the sole purpose of adding aliases without any real additional setup, other than the one or two words.

    I still don't see why cmd.getName() wouldn't work but, if it doesn't, then there could be something else wrong. You should probably come up with an example of when cmd doesn't work but label does.
     
Thread Status:
Not open for further replies.

Share This Page