Commands not working as expected

Discussion in 'Plugin Development' started by Heracles421, Feb 26, 2014.

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

    Heracles421

    So, I have the following problem: Whenever I use a command from my plugin, instead of doing what it's supposed to, it simply displays the "usage:" field on the plugin.yml
    I really don't know what's wrong, I already checked my code twice but I can't figurethis out. Some help would be greatly appreciated, thanks.

    Show Spoiler

    Code:java
    1. package me.Heracles421.MyMCMusic;
    2.  
    3. import java.io.File;
    4. import java.util.HashMap;
    5. import java.util.Map;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. import com.xxmicloxx.NoteBlockAPI.NBSDecoder;
    14. import com.xxmicloxx.NoteBlockAPI.RadioSongPlayer;
    15. import com.xxmicloxx.NoteBlockAPI.Song;
    16. import com.xxmicloxx.NoteBlockAPI.SongPlayer;
    17.  
    18. public class Main extends JavaPlugin {
    19. public Map<Integer, Song> tracks = new HashMap<Integer, Song>();
    20. public static Map<String, SongPlayer> playerMap = new HashMap<String, SongPlayer>();
    21.  
    22. File trackspath = new File(getDataFolder() + "/tracks");
    23. String prefix = ChatColor.BLUE + "[MyMCMusic]";
    24.  
    25. @Override
    26. public void onEnable(){
    27. getSongs();
    28. }
    29.  
    30. @Override
    31. public void onDisable(){
    32. // TODO Insert logic to be performed when the plugin is enabled
    33. }
    34.  
    35. public boolean onCommand(CommandSender sender, Command cmd, String[] args){
    36. Player player = (Player)sender;
    37. try {
    38. if (cmd.getName().equalsIgnoreCase("mcm") && player.hasPermission("mcm.use")){
    39. if (args.length <= 2){
    40. if (args[0].equalsIgnoreCase("help")){
    41. player.sendMessage(prefix + ChatColor.GREEN + "by Heracles421");
    42. player.sendMessage("--------------------------");
    43. player.sendMessage(ChatColor.RED + "/mcm help " + ChatColor.AQUA + "- Displays this help");
    44. player.sendMessage(ChatColor.RED + "/mcm list " + ChatColor.AQUA + "- Lists all available tracks");
    45. player.sendMessage(ChatColor.RED + "/mcm play [NUMBER] (PLAYER)" + ChatColor.AQUA + "- Plays the chosen number, optionally for the chosen player.");
    46. player.sendMessage(ChatColor.RED + "/mcm stop (PLAYER)" + ChatColor.AQUA + "- Stops the current track, optionally for the chosen player.");
    47. return true;
    48. }
    49. else if (args[0].equalsIgnoreCase("list") && player.hasPermission("mcm.list")) {
    50. player.sendMessage("These tracks are availabe for playing:");
    51. for (int i = 1; i <= tracks.size(); i++){
    52. String song = (tracks.get(i)).toString();
    53. player.sendMessage(ChatColor.RED + String.valueOf(i) + "- " + ChatColor.AQUA + song);
    54. }
    55. return true;
    56. }
    57. else if (args[0].equalsIgnoreCase("play") && player.hasPermission("mcm.play")) {
    58. String song = (tracks.get(args[1])).toString();
    59. play(new Player[] {player}, args[1]);
    60. player.sendMessage(ChatColor.AQUA + "Now playing: " + ChatColor.GREEN + song);
    61. return true;
    62. }
    63. else if (args[0].equalsIgnoreCase("stop") && player.hasPermission("mcm.stop")) {
    64. try {
    65. if (args.length == 2 && player.hasPermission("mcm.stop.others")){
    66. Player indirect = getServer().getPlayer(args[2]);
    67. play(new Player[] {indirect}, args[1]);
    68. } else {
    69. stopPlaying(new Player[] {player});
    70. player.sendMessage(ChatColor.AQUA + "You have been removed from the stream!");
    71. }
    72. } catch (Exception e){
    73. player.sendMessage(ChatColor.DARK_RED + args[1] + ChatColor.AQUA + " is not a player or is offline!");
    74. }
    75. return true;
    76. } else {
    77. player.sendMessage(ChatColor.DARK_RED + "Unknown command. Please, refer to the help.");
    78. }
    79. }
    80. else if (args.length == 3 && player.hasPermission("mcm.play.others")) {
    81. try {
    82. if (args[0].equalsIgnoreCase("play")){
    83. Player indirect = getServer().getPlayer(args[2]);
    84. play(new Player[] {indirect}, args[1]);
    85. return true;
    86. }
    87.  
    88. } catch (Exception e) {
    89. player.sendMessage(ChatColor.DARK_RED + args[1] + ChatColor.AQUA + " is not a player or is offline!");
    90. }
    91. } else {
    92. player.sendMessage(ChatColor.DARK_RED + "Unknown command. Please, refer to the help.");
    93. }
    94. }
    95.  
    96. }
    97. catch (Exception e)
    98. {
    99. player.sendMessage(ChatColor.DARK_RED + "Something went wrong! Please check spelling");
    100. player.sendMessage(ChatColor.RED + "Error: " + e.getMessage());
    101. }
    102. return false;
    103. }
    104.  
    105. // Custom functions
    106. public void stopPlaying(Player[] player){
    107. for (Player p : player){
    108. if (playerMap.containsKey(p.getName())){
    109. (playerMap.get(p.getName())).removePlayer(p);
    110. }
    111. playerMap.remove(p.getName());
    112. }
    113. }
    114.  
    115. public void play(Player[] player, String s){
    116. SongPlayer sp = new RadioSongPlayer(tracks.get(s));
    117. sp.setAutoDestroy(true);
    118. for (Player p : player){
    119. if (playerMap.containsKey(p.getName())){
    120. (playerMap.get(p.getName())).removePlayer(p);
    121. }
    122. sp.addPlayer(p);
    123. playerMap.put(p.getName(), sp);
    124. }
    125. sp.setPlaying(true);
    126. }
    127.  
    128. public void getSongs(){
    129. if (!trackspath.exists()) {
    130. getDataFolder().mkdir();
    131. trackspath.mkdir();
    132. System.out.println(prefix + "Tracks folder doesn't exist. It will be created...");
    133. }
    134.  
    135. File[] listOfFiles = trackspath.listFiles();
    136.  
    137. for (int i = 0; i < listOfFiles.length; i++){
    138. if (listOfFiles[i].isFile() && listOfFiles[i] != tracks){
    139. System.out.println(prefix + "New track found! Importing " + listOfFiles[i].getName());
    140. tracks.put(i, NBSDecoder.parse(listOfFiles[i].getAbsoluteFile()));
    141. }
    142. }
    143. }
    144. }[/i][/i][/i][/i]

     
  2. Offline

    Maulss

    Heracles421
    Remove all of the "return true;" statements, or at least change them to "return false;". Alternatively, you can remove the usage property in plugin.yml.
     
  3. Offline

    Heracles421

    Nope, it is not working yet. I removed all the return true's in there, and also the usage: field, but now it does not display anything, even though it should
     
  4. Offline

    Heracles421

    *bump*
    I still don't know what's the problem, help please!
     
  5. Offline

    PatoTheBest

    if (args.length <= 2){
    There's your problem
     
  6. Offline

    Heracles421

    PatoTheBest Why is it wrong? Maybe I'm just being stupid or blind, or both, but I really don't know why it won't work
     
  7. Offline

    Adamki11s

    Heracles421
    Perhaps the player doesn't have the permission node? Try removing that clause and see if it works.
     
  8. Offline

    Heracles421

    Adamki11s Nope, it's not the perms part. I removed all the permissions checks and still nothing. let me try to change the args.lenght part, and see what do I get

    PatoTheBest args.lenght is not the problem. Something else is causing it and I still haven't found it

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

    AoH_Ruthless

    Heracles421
    There are several things which must be fixed in your onCommand.
    1. You have to use the correct parameters. You are using "CommandSender sender, Command cmd, String[] args" as your params. This is the reason why nothing is working at all, as Bukkit doesn't recognize this method. To fix it, you must do one of two things: add a command executor for each command or add the CommandLabel parameter (It's a string) to make it look like the following: "CommandSender sender, Command cmd, String label, String[] args"
    2. You must check if the sender's instance is a player before casting the variable. Add the following line of code in the beginning of the onCommand: if (!(sender instanceof Player)) return false;
    3. Remove the big try/catch block. They should be used sparingly and when absolutely necessary. You might want to remove the other try/catch blocks if they aren't needed for sure.
    4. When checking "if (args.length <=2)" is wrong. That means the args.length could be 0. You need to make sure that the args.length > 0.
    Edit: So many edits so I can keep referring back as I go along, just ignore the edit count.

    Also, I am not telling you that everything will work after. I am checking the validity of your code, not the soundness of it.
     
  10. Offline

    Adamki11s

    Heracles421
    As AoH_Ruthless posted above you are using the wrong method. It's always good practice to use the @Override annotation to prevent this from happening.
     
  11. Offline

    AoH_Ruthless

    Adamki11s
    Just for clarity's sake, as Heracles421 might not know what the Override annotation does:
    • It will not fix the aforementioned issues. It silently tells the parent class "Hey, you are using the method incorrectly". This could be a parameter change, like you did, or a spelling change (onCommnad, for example). It's use is to quickly detect typos / deliberate API changes. So, while he said it "prevents this from happening", a slight error or message is thrown when you use @Override paired with an incorrect method.
     
    Adamki11s likes this.
  12. Offline

    Heracles421

    AoH_Ruthless I'd kiss you if I could.

    How was I so stupid to forget the CommandLabel parameter... Anyways, it all seems ok for now. I'll keep an eye on the plugin to see if anything breaks or is not working properly.
     
  13. Offline

    AoH_Ruthless

    MooshViolet
    What on earth are you talking about? I fixed the issue: there is no event he is listening for at all.
     
Thread Status:
Not open for further replies.

Share This Page