Commands not registering.

Discussion in 'Plugin Development' started by CodingAddiction, Sep 9, 2014.

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

    CodingAddiction

    I'll show all the code and please tell me if anything is wrong. I've been trying to fix this for hours now.

    Code:
    Code:java
    1. package com.vetrixmc.hub.punish;
    2.  
    3. import com.vetrixmc.hub.Hub;
    4. import com.vetrixmc.hub.util.C;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.Listener;
    11.  
    12. import java.util.ArrayList;
    13.  
    14. public class Punish implements Listener {
    15.  
    16. public ArrayList<Player> muted = new ArrayList<Player>();
    17. public ArrayList<Player> banned = new ArrayList<Player>();
    18.  
    19. private Hub main;
    20. private int timeAmount;
    21.  
    22. public Punish() {
    23. this.main = Hub.instance;
    24. }
    25.  
    26. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    27. Player player = (Player) sender;
    28. final Player target = Bukkit.getServer().getPlayer(args[2]);
    29. String reason = C.cDBlue + args[3];
    30. String altReason = C.cDBlue + args[4];
    31.  
    32. if (!(sender instanceof Player)) {
    33. sender.sendMessage(ChatColor.RED + "This command can only be done in-game!");
    34. }
    35.  
    36. if (cmd.getName().equalsIgnoreCase("punish")) {
    37. if (target == null) {
    38. player.sendMessage(ChatColor.RED + "Could not find player, " + target + "!");
    39. return true;
    40. }
    41.  
    42. if (target.isOp()) {
    43. player.sendMessage(ChatColor.RED + "You cannot punish OP players!");
    44. return true;
    45. }
    46.  
    47. if (!player.hasPermission("mod.permission")) {
    48. player.sendMessage(C.cRed + "You are not allowed to punish!");
    49. return true;
    50. }
    51.  
    52. if (target.getName().equals(player.getName())) {
    53. player.sendMessage(ChatColor.RED + "You can't punish yourself!");
    54. return true;
    55. }
    56.  
    57. if (args.length == 0 || args.length == 1) {
    58. player.sendMessage(punish + C.cYellow + "Sub-Commands: kick, ban, pban, warn, mute, pmute, unban, unmuted");
    59. player.sendMessage(punish + C.cYellow + "Punish Reasons: HACKING, EXPLOITING, SPAM, or the name of the report.");
    60. }
    61.  
    62. if (args.length == 2) {
    63. player.sendMessage(C.cRed + "/punish <Sub-Command> <Player> <Reason>");
    64. }
    65.  
    66. if (args.length >= 3) {
    67. if (args[1].equalsIgnoreCase("kick") && args[2].equals(target) && args[3].equals(reason)) {
    68. player.sendMessage(C.cYellow + "You have kicked " + target.getName() + ".");
    69. target.kickPlayer(punish + kickText + player.getName() + " for " + reason);
    70. }
    71.  
    72. } else if (args[1].equalsIgnoreCase("ban") && args[2].equals(target) && args[3].equals(timeAmount)) {
    73. target.setBanned(true);
    74. target.kickPlayer(punish + punishText + player.getName() + " for " + reason + " for " + timeAmount + " seconds");
    75. player.sendMessage(C.cYellow + "You have banned " + target.getName() + " for " + timeAmount + ".");
    76. banned.add(target);
    77. main.getConfig().set("Players." + target.getName() + ".Banned", true);
    78.  
    79. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
    80. public void run() {
    81. target.setBanned(false);
    82. banned.remove(target);
    83. main.getConfig().set("Players" + target.getName() + ".Banned", false);
    84. }
    85. }, 20 * timeAmount);
    86.  
    87. } else if (args[1].equalsIgnoreCase("pban") && args[2].equals(target) && args[3].equals(reason)) {
    88. target.setBanned(true);
    89. target.kickPlayer(punish + C.cYellow + punishText + player.getName() + " for " + reason + " for " + perma);
    90. player.sendMessage(C.cYellow + "You have permanently banned " + target.getName() + ".");
    91. banned.add(target);
    92. main.getConfig().set("Players." + target.getName() + ".Banned", true);
    93.  
    94. } else if (args[1].equalsIgnoreCase("warn") && args[2].equals(target) && args[3].equals(reason)) {
    95. target.sendMessage(punish + warnText + player.getName() + " for " + reason);
    96. player.sendMessage(C.cYellow + "You have warned " + target.getName() + ".");
    97.  
    98. } else if (args[1].equalsIgnoreCase("mute") && args[2].equals(target) && args[3].equals(reason) && args[3].equals(timeAmount)) {
    99. target.sendMessage(punish + C.cYellow + muteText + " for " + reason + " for " + timeAmount);
    100. muted.add(target);
    101. player.sendMessage(C.cYellow + "You have muted " + target.getName() + ".");
    102. main.getConfig().set("Players." + target.getName() + ".Muted", true);
    103.  
    104. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
    105. public void run() {
    106. muted.remove(target);
    107. main.getConfig().set("Players." + target.getName() + ".Muted", false);
    108. }
    109. }, 20 * timeAmount);
    110.  
    111. } else if (args[1].equalsIgnoreCase("pmute") && args[2].equals(target) && args[3].equals(reason)) {
    112. target.sendMessage(punish + C.cYellow + "You have been permanently muted by " + player.getName() + " for " + reason);
    113. muted.add(target);
    114. player.sendMessage(C.cYellow + "You have permanently muted " + target.getName() + ".");
    115. main.getConfig().set("Players." + target.getName() + ".Muted", true);
    116.  
    117. } else if (args[1].equalsIgnoreCase("unban") && args[2].equals(target)) {
    118. player.sendMessage(C.cYellow + "You have un-bannned " + target.getName() + ".");
    119.  
    120. if (banned.contains(target)) {
    121. banned.remove(target);
    122. main.getConfig().set("Players." + target.getName() + ".Banned", false);
    123. } else {
    124. player.sendMessage(ChatColor.RED + "That player is not banned!");
    125. return true;
    126. }
    127.  
    128. } else if (args[1].equalsIgnoreCase("unmute") && args[2].equals(target)) {
    129. if (muted.contains(target)) {
    130. target.sendMessage(C.cYellow + "You have been unmuted by " + target.getName() + ".");
    131. player.sendMessage(C.cYellow + "You have unmuted " + target.getName());
    132. muted.remove(target);
    133. main.getConfig().set("Players." + target.getName() + ".Muted", false);
    134. } else {
    135. player.sendMessage(C.cRed + "That player is not muted!");
    136. return true;
    137. }
    138. }
    139. }
    140. return true;
    141. }
    142.  
    143.  
    144. public String kickText = C.cYellow + "You have been kicked by ";
    145. public String muteText = C.cYellow + "You have been muted by ";
    146. public String warnText = C.cRed + "You have been warned by ";
    147. public String punish = C.cBlue + "[Punish] ";
    148. public String punishText = C.cYellow + "You have been punished by ";
    149. public String perma = C.cBlue + "Permanent";
    150. }
     
  2. Offline

    teej107

    This is at the very beginning of your onCommand().
    • Can you tell me what would happen if "sender" is the console? In regards to Player player = (Player) sender;
    • Can you tell me what would happen if the args length is below 3?, 4?, 5? What if the args length is 0? Yet you are trying to get the strings at postions 3, 4, and 5
     
  3. Offline

    CodingAddiction

    • If the sender is the console, is gonna send the console a message saying: "This command can only be done in-game!".
    • If the args length is below 3, is gonna send the player the message stated in the code.
    Anything else I have to answer?
     
  4. Offline

    teej107

    Yes, but
    Code:java
    1. Player player = (Player) sender;
    2. final Player target = Bukkit.getServer().getPlayer(args[2]);
    3. String reason = C.cDBlue + args[3];
    4. String altReason = C.cDBlue + args[4];
    comes first. Therefore something has to happen before the rest of the code is executed. Should I explain more?
     
  5. Offline

    CodingAddiction

    Yes, I think you should explain more, well, if you have time. ;)
     
  6. Offline

    Konato_K

    CodingAddiction You're casting sender into a player before checking if it's an actual player, you will get an exception for bad casting here, you need to check if sender is a instance of player BEFORE you cast it, now, I don't see why the console can't have permissions to run this commands, as I don't see where they need an actual player (I might be wrong here, since I didn't read everything, but my point is why are you not allowing a console to use certain commands that do NOR require a player?).

    Now, how are you planning to register the commands? Your class is a Listener, not a Plugin nor a CommandExecutor, it can't use commands, in the case you move your onCommand to your main plugin or make your "Listener" be a CommandExecutor instead (you're not even using anything in the Listener class).

    Please, refer to this http://wiki.bukkit.org/Plugin_Tutorial#Using_a_separate_CommandExecutor_class
     
  7. Offline

    Unica

    Like the above dudes stated, this code is kinda messed up.
    A quick sum,

    • You're casting before checking, that's like naming a kid "Jennifer' and then check if it's a girl.
    • You created the variable 'reason', which will return an error if you use one argument, even if you are checking it later on. Don't create a variable w/o checking if can generate an error (which it will)
    • You need to return once the sender is not an player, because the code will just move on, even if the sender is the console, which ends out in errors (Casting errors)
    • You're setting things in the config w/o saving.
    • Use switch -> case statements if you're using alot of subcommands (personal opinion)
    • Why the cowballs are u implementing a listener? You are not using an event, use CommandExecuter
    • Store the players UUID, rather than the object.
     
    teej107 likes this.
  8. Offline

    Nateb1121

    I know a lot of people have been helpful in pointing out some slight mistakes, but I'm pretty sure your issue is when you do a command it's not even working? Like, even if you command just did
    Code:
    System.out.println("I WORK!");
    
    Wouldn't work? If so, that's because onCommand needs to be in a class that extends JavaPlugin, not Listener.

    Correct me if I'm wrong.

    Yes, all the corrections some people have given are right. But they're not right for the problem you've described. I think Konato_K did originally mention the JavaPlugin extension issue.
     
  9. Offline

    teej107

    Nateb1121 When the OP corrects his current issue, more will arise from his lack of checking. And for your class to use onCommand, it must implement CommandExecutor. JavaPlugin already does implement it but if you want onCommand to work outside of your JavaPlugin extended class, you'll need to do an extra step that is mentioned in the Bukkit Wiki tutorial. You can have one class implement both CommandExecutor and Listener.
     
Thread Status:
Not open for further replies.

Share This Page