Command not throwing back a response

Discussion in 'Plugin Development' started by AlexHH251997, Nov 3, 2013.

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

    AlexHH251997

    Hi there,

    I am writing some code for a gamemode and I have created all of the code for it. I have a package inside a package called "Commands" that is containing it all and this is my layout inside the class.

    MapCommands.java
    Code:java
    1. package slideball.theslidemc.Commands;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.List;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandExecutor;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.entity.Player;
    13.  
    14. import slideball.theslidemc.Database.PlayerInfo;
    15. import slideball.theslidemc.Gametimes.EndGame;
    16. import slideball.theslidemc.Gametimes.InGame;
    17. import slideball.theslidemc.Gametimes.Lobby;
    18. import slideball.theslidemc.Listeners.PreGameListener;
    19. import slideball.theslidemc.com.Main;
    20.  
    21. public class MapCommands implements CommandExecutor{
    22.  
    23. //CLASSES
    24. public PlayerInfo pi = new PlayerInfo(plugin);
    25. public PreGameListener pgl = new PreGameListener(plugin);
    26. public EndGame end = new EndGame(plugin);
    27. public Lobby lob = new Lobby(plugin);
    28. public InGame ing = new InGame(plugin);
    29.  
    30. //CHAT INFORMATION
    31. public String Prefix = ChatColor.DARK_AQUA + "[SlideBall] " + ChatColor.WHITE;
    32. public String Granny = ChatColor.DARK_RED + "[Granny] " + ChatColor.WHITE;
    33. public String NoPerm = ChatColor.DARK_RED + "You are not tall enough to enter our slide.";
    34. public String Error = ChatColor.DARK_RED + "[Granny]" + ChatColor.WHITE + "Dont climb on the slide! Its broken!";
    35.  
    36. //RANK STRINGS
    37. public String PlayerPerm = "slidemc.rank.player";
    38. public String IronPerm = "slidemc.rank.iron";
    39. public String GoldPerm = "slidemc.rank.gold";
    40. public String DiamondPerm = "slidemc.rank.diamond";
    41. public String FriendPerm = "slidemc.rank.friend";
    42. public String VIPPerm = "slidemc.rank.vip";
    43. public String ModPerm = "slidemc.rank.mod";
    44. public String SrModPerm = "slidemc.rank.srmod";
    45. public String AdminPerm = "slidemc.rank.admin";
    46. public String DevPerm = "slidemc.rank.dev";
    47.  
    48. //OTHER
    49. private FileConfiguration customConfig;
    50. private File locationFile;
    51. int spawnNumber = 1;
    52.  
    53. public static Main plugin;
    54.  
    55. public MapCommands(Main main){
    56. plugin = main;
    57. }
    58.  
    59. public boolean onCommand(CommandSender sender, Player player, Command cmd, String commandLabel, String [] args){
    60. if(cmd.getName().equalsIgnoreCase("setspawn")){
    61. if(sender.hasPermission(AdminPerm) || sender.hasPermission(DevPerm)){
    62. customConfig.set(spawnNumber + ".x", player.getLocation().getX());
    63. customConfig.set(spawnNumber + ".y", player.getLocation().getY());
    64. customConfig.set(spawnNumber + ".z", player.getLocation().getZ());
    65. try {
    66. customConfig.save(locationFile);
    67. sender.sendMessage(Prefix + ChatColor.GOLD + "Spawn Point #" + ChatColor.DARK_AQUA + spawnNumber + ChatColor.GOLD + " Set.");
    68. spawnNumber++;
    69. } catch (IOException e) {
    70. player.sendMessage(Prefix + ChatColor.GOLD + "Spawn Point Saving Encountered A Error.");
    71. e.printStackTrace();
    72. }
    73. player.sendMessage(Prefix + ChatColor.GOLD + "Ready To Set Spawn Point #" + ChatColor.DARK_AQUA + spawnNumber);
    74. }else{
    75. player.sendMessage(NoPerm);
    76. }
    77. }else if(cmd.getName().equalsIgnoreCase("addmap")){
    78. if(sender.hasPermission(AdminPerm) || sender.hasPermission(DevPerm)){
    79. if(!plugin.getConfig().getStringList("maps").contains(player.getLocation().getWorld().getName())){
    80. plugin.getConfig().getStringList("maps").add(player.getLocation().getWorld().getName());
    81. }else{
    82. player.sendMessage(Prefix + ChatColor.GRAY + "World Already Defined In Config.");
    83. }
    84.  
    85. File locationDirectory = new File(plugin.getDataFolder() + File.separator + "maps" + File.separator + player.getWorld().getName());
    86. File locationConfig = new File(plugin.getDataFolder() + File.separator + "maps" + File.separator + player.getWorld().getName(), "locations.yml");
    87. if(locationConfig.exists()){
    88. player.sendMessage(Prefix + ChatColor.GOLD + "Location config already exist!");
    89. }else{
    90. try {
    91. locationDirectory.mkdirs();
    92. locationConfig.createNewFile();
    93. player.sendMessage(Prefix + ChatColor.GOLD + "Locations Successfully Created!");
    94. } catch (IOException e) {
    95. player.sendMessage(Prefix + ChatColor.GRAY + "Failed To Create Config.");
    96. e.printStackTrace();
    97. }
    98. }
    99.  
    100. List<String> configList = plugin.getConfig().getStringList("maps");
    101. if(configList.contains(player.getWorld().getName())){
    102. player.sendMessage(Prefix + ChatColor.GOLD + "World Already Defined In Config.");
    103. }else{
    104. configList.add(player.getWorld().getName());
    105. plugin.getConfig().set("maps", configList);
    106. plugin.saveConfig();
    107. player.sendMessage(Prefix + ChatColor.GOLD + "World Added To Config.");
    108. }
    109. }else{
    110. player.sendMessage(NoPerm);
    111. }
    112. }
    113. return false;
    114. }
    115. }

    It then tells me this when hovering over "MapCommands" on the "public clas MapCommands..."
    Code:
    The type MapCommands must implement the inherited abstract method CommandExecutor.onCommand(CommandSender, Command, String, String[])
    When clicking
    Code:
    Add unimplemented methods.
    It adds this to the bottom of the class
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender arg0, Command arg1, String arg2,
    3. String[] arg3) {
    4. // TODO Auto-generated method stub
    5. return false;
    6. }

    Plugin.yml
    Code:
    name: SlideBall
    version: 1.0.0
    description: TheSlideMC SlideBall Gamemode
    author: AlexHH25
    main: slideball.theslidemc.com.Main
     
    commands:
      setspawn:
        description: Sets a spawn point.
      addmap:
        description: Adds a map.
    permissions:
      slidemc.rank.player:
        description: Player Permission Node
      slidemc.rank.iron:
        description: Iron Permission Node
      slidemc.rank.gold:
        description: Gold Permission Node
      slidemc.rank.diamond:
        description: Diamond Permission Node
      slidemc.rank.friend:
        description: Friend Permission Node
      slidemc.rank.vip:
        description: VIP Permission Node
      slidemc.rank.mod:
        description: Mod Permission Node
      slidemc.rank.srmod:
        description: Sr Mod Permission Node
      slidemc.rank.admin:
        description: Admin Permission Node
      slidemc.rank.dev:
        description: Developer Permission Node
    The problem is when using these commands ingame I am not getting and response out of them. There is no error in the console and there is no error when starting the plugin. Anyone have any suggestions?
     
  2. Offline

    CubieX

    In the class where you instanciate your MapCommands class (I guess your main class?) you have to set the CommandExecutor for your commands to your MapCommands instance.
    Something like this:
    Code:
    getCommand("setspawn").setExecutor(myMapCommandsInstance);
    getCommand("addmap").setExecutor(myMapCommandsInstance);
    If you do not instanciate it in your main class, use your "plugin" variable for the call.
     
  3. Offline

    AlexHH251997

    CubieX
    Sorry I don't really understand that, would I have to create my own Command Instance or would I be able to use the Bukkit Command Instance?
     
  4. Offline

    OffLuffy

    When you implement the CommandExecutor, the class implementing it is forced to have a method in it that was auto-generated when you clicked the auto fix. The difference between that and the one you wrote being the arguments being passed to it. The CommandExecutor dictates that the arguments must be (in order): CommandSender (The player sending the command), Command (The command object being used), String (The label of the command), String[] (arguments of the command, ie, every word after the command, delimited by spaces). The one you wrote uses different method arguments (sender, player, etc). Also, as a hint, the Player can be derived from the CommandSender object. Just check if it is a Player:
    Code:java
    1. if (sender instanceof Player)


    Then cast it to a Player object:
    Code:java
    1. Player player = (Player)sender;


    Then you can remove the Player player argument in your onCommand event, and it would count for the method that CommandExecutor wants you to put in.

    Also forgot that after that, you would need to get the command object and set it's executor:
    Code:java
    1. getServer().getPluginCommand("command").setExecutor(this);
     
  5. Offline

    AlexHH251997

    OffLuffy

    I am afraid that didn't work, the command is able to be used but when you type it for example /addmap it returns nothing to you ingame like it should.
     
  6. Offline

    OffLuffy

    Could you post the code changes? If the command is registered in plugin.yml, but not registered properly in the code, then using the command may not return anything. It would normally return the usage for that command specified in the plugin.yml, but not sure what it would do if not specified. You could also put the output of the command if anything.
     
  7. Offline

    AlexHH251997

    OffLuffy

    Here is the new code, I am still getting the error on MapCommands line
    Code:java
    1. package slideball.theslidemc.Commands;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.List;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandExecutor;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.entity.Player;
    13.  
    14. import slideball.theslidemc.Database.PlayerInfo;
    15. import slideball.theslidemc.Gametimes.EndGame;
    16. import slideball.theslidemc.Gametimes.InGame;
    17. import slideball.theslidemc.Gametimes.Lobby;
    18. import slideball.theslidemc.Listeners.PreGameListener;
    19. import slideball.theslidemc.com.Main;
    20.  
    21. public class MapCommands implements CommandExecutor{
    22.  
    23. //CLASSES
    24. public PlayerInfo pi = new PlayerInfo(plugin);
    25. public PreGameListener pgl = new PreGameListener(plugin);
    26. public EndGame end = new EndGame(plugin);
    27. public Lobby lob = new Lobby(plugin);
    28. public InGame ing = new InGame(plugin);
    29.  
    30. //CHAT INFORMATION
    31. public String Prefix = ChatColor.DARK_AQUA + "[SlideBall] " + ChatColor.WHITE;
    32. public String Granny = ChatColor.DARK_RED + "[Granny] " + ChatColor.WHITE;
    33. public String NoPerm = ChatColor.DARK_RED + "You are not tall enough to enter our slide.";
    34. public String Error = ChatColor.DARK_RED + "[Granny]" + ChatColor.WHITE + "Dont climb on the slide! Its broken!";
    35.  
    36. //RANK STRINGS
    37. public String PlayerPerm = "slidemc.rank.player";
    38. public String IronPerm = "slidemc.rank.iron";
    39. public String GoldPerm = "slidemc.rank.gold";
    40. public String DiamondPerm = "slidemc.rank.diamond";
    41. public String FriendPerm = "slidemc.rank.friend";
    42. public String VIPPerm = "slidemc.rank.vip";
    43. public String ModPerm = "slidemc.rank.mod";
    44. public String SrModPerm = "slidemc.rank.srmod";
    45. public String AdminPerm = "slidemc.rank.admin";
    46. public String DevPerm = "slidemc.rank.dev";
    47.  
    48. //OTHER
    49. private FileConfiguration customConfig;
    50. private File locationFile;
    51. int spawnNumber = 1;
    52.  
    53. public static Main plugin;
    54.  
    55. public MapCommands(Main main){
    56. plugin = main;
    57. }
    58.  
    59. public boolean onCommand(CommandSender sender, Command cmd, String [] args){
    60. if(cmd.getName().equalsIgnoreCase("setspawn")){
    61. if(sender.hasPermission(AdminPerm) || sender.hasPermission(DevPerm)){
    62. Player player = (Player) sender;
    63. customConfig.set(spawnNumber + ".x", player.getLocation().getX());
    64. customConfig.set(spawnNumber + ".y", player.getLocation().getY());
    65. customConfig.set(spawnNumber + ".z", player.getLocation().getZ());
    66. try {
    67. customConfig.save(locationFile);
    68. sender.sendMessage(Prefix + ChatColor.GOLD + "Spawn Point #" + ChatColor.DARK_AQUA + spawnNumber + ChatColor.GOLD + " Set.");
    69. spawnNumber++;
    70. } catch (IOException e) {
    71. player.sendMessage(Prefix + ChatColor.GOLD + "Spawn Point Saving Encountered A Error.");
    72. e.printStackTrace();
    73. }
    74. player.sendMessage(Prefix + ChatColor.GOLD + "Ready To Set Spawn Point #" + ChatColor.DARK_AQUA + spawnNumber);
    75. }else{
    76. Player player = (Player) sender;
    77. player.sendMessage(NoPerm);
    78. }
    79. return true;
    80. }else if(cmd.getName().equalsIgnoreCase("addmap")){
    81. if(sender.hasPermission(AdminPerm) || sender.hasPermission(DevPerm)){
    82. Player player = (Player) sender;
    83. if(!plugin.getConfig().getStringList("maps").contains(player.getLocation().getWorld().getName())){
    84. plugin.getConfig().getStringList("maps").add(player.getLocation().getWorld().getName());
    85. }else{
    86. player.sendMessage(Prefix + ChatColor.GRAY + "World Already Defined In Config.");
    87. }
    88.  
    89. File locationDirectory = new File(plugin.getDataFolder() + File.separator + "maps" + File.separator + player.getWorld().getName());
    90. File locationConfig = new File(plugin.getDataFolder() + File.separator + "maps" + File.separator + player.getWorld().getName(), "locations.yml");
    91. if(locationConfig.exists()){
    92. player.sendMessage(Prefix + ChatColor.GOLD + "Location config already exist!");
    93. }else{
    94. try {
    95. locationDirectory.mkdirs();
    96. locationConfig.createNewFile();
    97. player.sendMessage(Prefix + ChatColor.GOLD + "Locations Successfully Created!");
    98. } catch (IOException e) {
    99. player.sendMessage(Prefix + ChatColor.GRAY + "Failed To Create Config.");
    100. e.printStackTrace();
    101. }
    102. }
    103.  
    104. List<String> configList = plugin.getConfig().getStringList("maps");
    105. if(configList.contains(player.getWorld().getName())){
    106. player.sendMessage(Prefix + ChatColor.GOLD + "World Already Defined In Config.");
    107. }else{
    108. configList.add(player.getWorld().getName());
    109. plugin.getConfig().set("maps", configList);
    110. plugin.saveConfig();
    111. player.sendMessage(Prefix + ChatColor.GOLD + "World Added To Config.");
    112. }
    113. }else{
    114. Player player = (Player) sender;
    115. player.sendMessage(NoPerm);
    116. }
    117. }
    118. return true;
    119. }
    120. }


    EDIT: I can make the class abstract and that removes the errors, would this help or would it make it worse?
     
  8. Offline

    OffLuffy

    You're missing an argument and there doesn't need to be a space between String and the brackets [] on this line:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String [] args){


    This should be
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){


    Even if you don't use them, all these arguments need to be here for the CommandExecutor to accept it.
     
  9. Offline

    AlexHH251997

    What about making the class a abstract class, will that affect it?
    Code:java
    1. public abstract class MapCommands implements CommandExecutor
     
  10. Offline

    OffLuffy

    The abstract class is designed to be implemented by other classes. I'm not sure why you would want to do that. It certainly wouldn't allow you to execute the command. Just because the compiler doesn't report errors, doesn't mean that it's valid / logical code.
     
  11. Offline

    AlexHH251997

    I tried it because it allowed me to remove all class errors and not have the second onCommand code appear when clicking "Add unimplemented methods." so I tried it. I will remove it now.

    The problem is still occurring, the command is usable but it gives no response what so ever. Any idea why OffLuffy?

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

    OffLuffy

    If you've changed anything, could you please re-post the code and any errors?
     
  13. Offline

    AlexHH251997

    Code:java
    1. package slideball.theslidemc.Commands;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.List;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandExecutor;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.entity.Player;
    13.  
    14. import slideball.theslidemc.Database.PlayerInfo;
    15. import slideball.theslidemc.Gametimes.EndGame;
    16. import slideball.theslidemc.Gametimes.InGame;
    17. import slideball.theslidemc.Gametimes.Lobby;
    18. import slideball.theslidemc.Listeners.PreGameListener;
    19. import slideball.theslidemc.com.Main;
    20.  
    21. public class MapCommands implements CommandExecutor{
    22.  
    23. //CLASSES
    24. public PlayerInfo pi = new PlayerInfo(plugin);
    25. public PreGameListener pgl = new PreGameListener(plugin);
    26. public EndGame end = new EndGame(plugin);
    27. public Lobby lob = new Lobby(plugin);
    28. public InGame ing = new InGame(plugin);
    29.  
    30. //CHAT INFORMATION
    31. public String Prefix = ChatColor.DARK_AQUA + "[SlideBall] " + ChatColor.WHITE;
    32. public String Granny = ChatColor.DARK_RED + "[Granny] " + ChatColor.WHITE;
    33. public String NoPerm = ChatColor.DARK_RED + "You are not tall enough to enter our slide.";
    34. public String Error = ChatColor.DARK_RED + "[Granny]" + ChatColor.WHITE + "Dont climb on the slide! Its broken!";
    35.  
    36. //RANK STRINGS
    37. public String PlayerPerm = "slidemc.rank.player";
    38. public String IronPerm = "slidemc.rank.iron";
    39. public String GoldPerm = "slidemc.rank.gold";
    40. public String DiamondPerm = "slidemc.rank.diamond";
    41. public String FriendPerm = "slidemc.rank.friend";
    42. public String VIPPerm = "slidemc.rank.vip";
    43. public String ModPerm = "slidemc.rank.mod";
    44. public String SrModPerm = "slidemc.rank.srmod";
    45. public String AdminPerm = "slidemc.rank.admin";
    46. public String DevPerm = "slidemc.rank.dev";
    47.  
    48. //OTHER
    49. private FileConfiguration customConfig;
    50. private File locationFile;
    51. int spawnNumber = 1;
    52.  
    53. public static Main plugin;
    54.  
    55. public MapCommands(Main main){
    56. plugin = main;
    57. }
    58.  
    59. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    60. if(cmd.getName().equalsIgnoreCase("addspawn")){
    61. if(sender.hasPermission(AdminPerm) || sender.hasPermission(DevPerm)){
    62. Player player = (Player) sender;
    63. customConfig.set(spawnNumber + ".x", player.getLocation().getX());
    64. customConfig.set(spawnNumber + ".y", player.getLocation().getY());
    65. customConfig.set(spawnNumber + ".z", player.getLocation().getZ());
    66. try {
    67. customConfig.save(locationFile);
    68. sender.sendMessage(Prefix + ChatColor.GOLD + "Spawn Point #" + ChatColor.DARK_AQUA + spawnNumber + ChatColor.GOLD + " Set.");
    69. spawnNumber++;
    70. } catch (IOException e) {
    71. player.sendMessage(Prefix + ChatColor.GOLD + "Spawn Point Saving Encountered A Error.");
    72. e.printStackTrace();
    73. }
    74. player.sendMessage(Prefix + ChatColor.GOLD + "Ready To Set Spawn Point #" + ChatColor.DARK_AQUA + spawnNumber);
    75. }else{
    76. Player player = (Player) sender;
    77. player.sendMessage(NoPerm);
    78. }
    79. return true;
    80. }else if(cmd.getName().equalsIgnoreCase("addmap")){
    81. if(sender.hasPermission(AdminPerm) || sender.hasPermission(DevPerm)){
    82. Player player = (Player) sender;
    83. if(!plugin.getConfig().getStringList("maps").contains(player.getLocation().getWorld().getName())){
    84. plugin.getConfig().getStringList("maps").add(player.getLocation().getWorld().getName());
    85. }else{
    86. player.sendMessage(Prefix + ChatColor.GRAY + "World Already Defined In Config.");
    87. }
    88.  
    89. File locationDirectory = new File(plugin.getDataFolder() + File.separator + "maps" + File.separator + player.getWorld().getName());
    90. File locationConfig = new File(plugin.getDataFolder() + File.separator + "maps" + File.separator + player.getWorld().getName(), "locations.yml");
    91. if(locationConfig.exists()){
    92. player.sendMessage(Prefix + ChatColor.GOLD + "Location config already exist!");
    93. }else{
    94. try {
    95. locationDirectory.mkdirs();
    96. locationConfig.createNewFile();
    97. player.sendMessage(Prefix + ChatColor.GOLD + "Locations Successfully Created!");
    98. } catch (IOException e) {
    99. player.sendMessage(Prefix + ChatColor.GRAY + "Failed To Create Config.");
    100. e.printStackTrace();
    101. }
    102. }
    103.  
    104. List<String> configList = plugin.getConfig().getStringList("maps");
    105. if(configList.contains(player.getWorld().getName())){
    106. player.sendMessage(Prefix + ChatColor.GOLD + "World Already Defined In Config.");
    107. }else{
    108. configList.add(player.getWorld().getName());
    109. plugin.getConfig().set("maps", configList);
    110. plugin.saveConfig();
    111. player.sendMessage(Prefix + ChatColor.GOLD + "World Added To Config.");
    112. }
    113. }else{
    114. Player player = (Player) sender;
    115. player.sendMessage(NoPerm);
    116. }
    117. }
    118. return true;
    119. }
    120. }
     
  14. Offline

    maxben34

Thread Status:
Not open for further replies.

Share This Page