Command Help

Discussion in 'Plugin Development' started by boysnnoco, Nov 25, 2013.

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

    boysnnoco

    While this debug works I can't understand why my actually command works!
    Debug Command (works)
    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command command, String commandLabel,String[] args) {
        if(sender instanceof Player){
            if(commandLabel.equalsIgnoreCase("ban")){
                Player player = (Player)sender;
                player.sendMessage("DEBUG");
                return true;
            }
        }else{
            return false;
        }
        return false;
       
        }
    But this doesn't seem to work:
    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player)sender;
            if(commandLabel.equalsIgnoreCase("ban")){
                if(player.hasPermission("gban.canUseBan")){
                    if(args.length >= 3){
                        if(args[0].equalsIgnoreCase("add")){
                            if(args[1].equalsIgnoreCase("public")){
                                Player target = Bukkit.getServer().getPlayer(args[2]);
                                if(target != null){
                                    Bukkit.broadcastMessage(ChatColor.GRAY + target.getName() + " Has Been Banned!");
                                    target.kickPlayer(ChatColor.RED + "You have been banned by "+ player.getName() +" for " + Main.getMessage(args, 3) + "!" );
                                    target.setBanned(true);
                                    return true;
                                }else{
                                    player.sendMessage(ChatColor.RED + "Could not find '" + target + "'");
                                    return true;
                                }
     
                            }else if(args[1].equalsIgnoreCase("private")){
                                Player target = Bukkit.getServer().getPlayer(args[2]);
                                if(target != null){
                                    target.kickPlayer(ChatColor.RED + "You have been banned by "+ player.getName() +" for " + Main.getMessage(args, 3) + "!");
                                    target.setBanned(true);
                                    return true;
                                }else{
                                    player.sendMessage(ChatColor.RED + "Could not find '" + target + "'");
                                    return true;
                                }
                            }else if(args[1].equalsIgnoreCase("offline")){
                                OfflinePlayer target = Bukkit.getOfflinePlayer(args[2]);
                                if(target != null){
                                    if(target.isBanned()){
                                        player.sendMessage(ChatColor.RED + target.getName() + " is already banned!");
                                        return true;
                                    }else{
                                        Bukkit.broadcastMessage(ChatColor.GRAY + target.getName() + " has been banned!");
                                        target.setBanned(true);
                                        player.sendMessage(ChatColor.AQUA + "Banning player " + target.getName());
                                        return true;
                                    }
                                }else{
                                    player.sendMessage(ChatColor.RED + "Could not find '" + target + "'");
                                    return true;
                                }
                            }else{
                                player.sendMessage(ChatColor.RED + "Wrong ussage /ban [Add || Del] [Public || Private || Offline||] [Player] [Reason]");
                                return true;
                            }
                        }else if(args[0].equalsIgnoreCase("del")){
                            OfflinePlayer target = Bukkit.getOfflinePlayer(args[3]);
                            if(target != null){
                                target.setBanned(false);
                                Bukkit.broadcastMessage(ChatColor.GRAY + target.getName() + " has been unbanned by " + player.getName() + " because " + Main.getMessage(args, 4));
                                return true;
                            }else{
                                player.sendMessage(ChatColor.RED + "Could not find '" + target + "'");
                                return true;
                            }
                        }else{
                            player.sendMessage(ChatColor.RED + "Wrong ussage /ban [Add || Del] [Public || Private || Offline||] [Player] [Reason]");
                            return true;
                        }
                    }else{
                        player.sendMessage(ChatColor.RED + "Wrong ussage /ban [Add || Del] [Public || Private || Offline||] [Player] [Reason]");
                        return true;
                    }
                }
            }
            return false;
        }
     
  2. Offline

    amhokies

    boysnnoco
    Add debug messages in your code to see where in the method you are getting (and even if it is firing at all)
     
  3. Offline

    AoH_Ruthless

    boysnnoco
    Stacktrace?

    amhokies
    In a way he already has a debug .... each command has a player.sendMessage(); so he get's a message.
     
  4. Offline

    amhokies

  5. Offline

    AoH_Ruthless

    amhokies
    Fair point. But a stacktrace would be better (there should be a stacktrace if it's an error with the onCommand)
     
  6. Offline

    boysnnoco

    AoH_Ruthless there are no errors in console nothing at all
    amhokies Ill add some debug messages and get back to you

    amhokies I added some debugs with no success it seems as though the command isn't even getting registered, I am extending java plugin and it is loading the plugin correctly (it is also in the plugin.yml) any help?

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

    felixfritz

    boysnnoco wait, so it didn't even get past the if(commandLabel.equalsIgnoreCase("ban")) statement?
     
  8. Offline

    boysnnoco

    felixfritz yeah it doesn't I don't understand why p-p
     
  9. Offline

    felixfritz

    boysnnoco Did you change anything else besides the content of the onCommand method? Because if your first version worked, there's no way the second version shouldn't pass the if-statement.

    Maybe it helps if you paste the whole source code in here.
     
  10. Offline

    boysnnoco

    felixfritz my first version was in a test plugin where I test stuff to see if it works :3

    But anyway here's my source code:
    Code:java
    1. package me.boysnnoco.CustomBan;
    2.  
    3.  
    4.  
    5.  
    6. import java.util.ArrayList;
    7. import java.util.HashMap;
    8. import java.util.logging.Logger;
    9.  
    10. import me.boysnnoco.CustomBan.Commands.CommandAdmin;
    11. import me.boysnnoco.CustomBan.Commands.CommandAdminChat;
    12. import me.boysnnoco.CustomBan.Commands.CommandClearChat;
    13. import me.boysnnoco.CustomBan.Commands.CommandInvs;
    14. import me.boysnnoco.CustomBan.Commands.CommandIp;
    15. import me.boysnnoco.CustomBan.Commands.CommandKick;
    16. import me.boysnnoco.CustomBan.Commands.CommandMsg;
    17. import me.boysnnoco.CustomBan.Commands.CommandReloadConfig;
    18. import me.boysnnoco.CustomBan.Commands.CommandReply;
    19. import me.boysnnoco.CustomBan.Commands.CommandReport;
    20. import me.boysnnoco.CustomBan.Commands.CommandVis;
    21. import me.boysnnoco.CustomBan.Commands.commandLock;
    22. import me.boysnnoco.CustomBan.Commands.commandUnlock;
    23.  
    24. import org.bukkit.Bukkit;
    25. import org.bukkit.ChatColor;
    26. import org.bukkit.Location;
    27. import org.bukkit.OfflinePlayer;
    28. import org.bukkit.command.Command;
    29. import org.bukkit.command.CommandSender;
    30. import org.bukkit.configuration.file.FileConfiguration;
    31. import org.bukkit.entity.Player;
    32. import org.bukkit.event.EventHandler;
    33. import org.bukkit.event.Listener;
    34. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    35. import org.bukkit.event.player.PlayerJoinEvent;
    36. import org.bukkit.event.player.PlayerLoginEvent;
    37. import org.bukkit.event.player.PlayerMoveEvent;
    38. import org.bukkit.event.player.PlayerPickupItemEvent;
    39. import org.bukkit.event.player.PlayerQuitEvent;
    40. import org.bukkit.plugin.PluginDescriptionFile;
    41. import org.bukkit.plugin.java.JavaPlugin;
    42.  
    43. public class Main extends JavaPlugin implements Listener{
    44.  
    45.  
    46. Logger logger = Logger.getLogger("Minecraft");
    47. public Main plugin;
    48. public boolean lock = false;
    49. public HashMap<Player,Player> reply = new HashMap<Player,Player>();
    50. public HashMap<String,Integer> banned = new HashMap<String,Integer>();
    51. public HashMap<String,Integer> task = new HashMap<String,Integer>();
    52. public Main(){
    53. plugin = this;
    54. }
    55. public void registerCommands(){
    56. getCommand("ip").setExecutor(new CommandIp(this));
    57. getCommand("invs").setExecutor(new CommandInvs(this));
    58. getCommand("vis").setExecutor(new CommandVis(this));
    59. getCommand("admin").setExecutor(new CommandAdmin(this));
    60. getCommand("report").setExecutor(new CommandReport(this));
    61. getCommand("clearchat").setExecutor(new CommandClearChat(this));
    62. getCommand("gkick").setExecutor(new CommandKick(this));
    63. getCommand("msg").setExecutor(new CommandMsg(this));
    64. getCommand("lock").setExecutor(new commandLock(this));
    65. getCommand("unlock").setExecutor(new commandUnlock(this));
    66. getCommand("r").setExecutor(new CommandReply(this));
    67. getCommand("adminchat").setExecutor(new CommandAdminChat(this));
    68. getCommand("greload").setExecutor(new CommandReloadConfig(this));
    69. }
    70.  
    71. //Array Lists
    72. public ArrayList<Player> mhidingPlayers = new ArrayList<Player>();
    73. public ArrayList<Player> ahidingPlayers = new ArrayList<Player>();
    74. public ArrayList<Player> ohidingPlayers = new ArrayList<Player>();
    75. public ArrayList<Player> combat = new ArrayList<Player>();
    76. public ArrayList<Player> dead = new ArrayList<Player>();
    77. public ArrayList<Player> adminchat = new ArrayList<Player>();
    78.  
    79. public void onEnable(){
    80. PluginDescriptionFile pdfFile = this.getDescription();
    81. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled");
    82. getServer().getPluginManager().registerEvents(this, this);
    83. registerCommands();
    84. final FileConfiguration config = this.getConfig();
    85. config.addDefault("Server.Ip", "RandyKits.Net");
    86. config.options().copyDefaults(true);
    87. saveConfig();
    88. }
    89. public void onDisable(){
    90. PluginDescriptionFile pdfFile = this.getDescription();
    91. this.logger.info(pdfFile.getName() + " Version" + pdfFile.getVersion() + " Has Been Disabled!");
    92.  
    93. }
    94.  
    95. @Override
    96. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    97. Player player = (Player)sender;
    98. player.sendMessage("command");
    99. if(commandLabel.equalsIgnoreCase("ban")){
    100. if(player.hasPermission("gban.canUseBan")){
    101. if(args.length >= 3){
    102. if(args[0].equalsIgnoreCase("add")){
    103. player.sendMessage("add");
    104. if(args[1].equalsIgnoreCase("public")){
    105. player.sendMessage("public");
    106. Player target = Bukkit.getServer().getPlayer(args[2]);
    107. if(target != null){
    108. Bukkit.broadcastMessage(ChatColor.GRAY + target.getName() + " Has Been Banned!");
    109. target.kickPlayer(ChatColor.RED + "You have been banned by "+ player.getName() +" for " + Main.getMessage(args, 3) + "!" );
    110. target.setBanned(true);
    111. return true;
    112. }else{
    113. player.sendMessage(ChatColor.RED + "Could not find '" + target + "'");
    114. return true;
    115. }
    116.  
    117. }else if(args[1].equalsIgnoreCase("private")){
    118. player.sendMessage("private");
    119. Player target = Bukkit.getServer().getPlayer(args[2]);
    120. if(target != null){
    121. target.kickPlayer(ChatColor.RED + "You have been banned by "+ player.getName() +" for " + Main.getMessage(args, 3) + "!");
    122. target.setBanned(true);
    123. return true;
    124. }else{
    125. player.sendMessage(ChatColor.RED + "Could not find '" + target + "'");
    126. return true;
    127. }
    128. }else if(args[1].equalsIgnoreCase("offline")){
    129. player.sendMessage("offline");
    130. OfflinePlayer target = Bukkit.getOfflinePlayer(args[2]);
    131. if(target != null){
    132. if(target.isBanned()){
    133. player.sendMessage(ChatColor.RED + target.getName() + " is already banned!");
    134. return true;
    135. }else{
    136. Bukkit.broadcastMessage(ChatColor.GRAY + target.getName() + " has been banned!");
    137. target.setBanned(true);
    138. player.sendMessage(ChatColor.AQUA + "Banning player " + target.getName());
    139. return true;
    140. }
    141. }else{
    142. player.sendMessage(ChatColor.RED + "Could not find '" + target + "'");
    143. return true;
    144. }
    145. }else{
    146. player.sendMessage(ChatColor.RED + "Wrong ussage /ban [Add || Del] [Public || Private || Offline||] [Player] [Reason]");
    147. return true;
    148. }
    149. }else if(args[0].equalsIgnoreCase("del")){
    150. player.sendMessage("del");
    151. OfflinePlayer target = Bukkit.getOfflinePlayer(args[3]);
    152. if(target != null){
    153. target.setBanned(false);
    154. Bukkit.broadcastMessage(ChatColor.GRAY + target.getName() + " has been unbanned by " + player.getName() + " because " + Main.getMessage(args, 4));
    155. return true;
    156. }else{
    157. player.sendMessage(ChatColor.RED + "Could not find '" + target + "'");
    158. return true;
    159. }
    160. }else{
    161. player.sendMessage(ChatColor.RED + "Wrong ussage /ban [Add || Del] [Public || Private || Offline||] [Player] [Reason]");
    162. return true;
    163. }
    164. }else{
    165. player.sendMessage(ChatColor.RED + "Wrong ussage /ban [Add || Del] [Public || Private || Offline||] [Player] [Reason]");
    166. return true;
    167. }
    168. }
    169. }
    170. return false;
    171. }
    172.  
    173. @EventHandler
    174. public void onPlayerLogin(PlayerJoinEvent event){
    175. Player player = event.getPlayer();
    176. if(player.hasPermission("gban.MInvs")){
    177. Bukkit.broadcast(ChatColor.DARK_AQUA + player.getName() + " has joined invisibly!","gban.MInvs");
    178. Bukkit.broadcast(ChatColor.DARK_AQUA + player.getName() + " has joined invisibly!","gban.OInvs");
    179. Bukkit.broadcast(ChatColor.DARK_AQUA + player.getName() + " has joined invisibly!","gban.AInvs");
    180. event.setJoinMessage(null);
    181. }else if(player.hasPermission("gban.AInvs")){
    182. Bukkit.broadcast(ChatColor.DARK_AQUA + player.getName() + " has joined invisibly!","gban.AInvs");
    183. Bukkit.broadcast(ChatColor.DARK_AQUA + player.getName() + " has joined invisibly!","gban.OInvs");
    184. event.setJoinMessage(null);
    185. }else if(player.hasPermission("gban.OInvs")){
    186. Bukkit.broadcast(ChatColor.DARK_AQUA + player.getName() + " has joined invisibly!","gban.OInvs");
    187. event.setJoinMessage(null);
    188. }else{
    189. event.setJoinMessage(ChatColor.GRAY + "+ " + player.getName());
    190. }
    191. for(Player p : ohidingPlayers){
    192. player.hidePlayer(p);
    193. }
    194. for(Player p : ahidingPlayers){
    195. if(p.hasPermission("gban.OInvs") || p.hasPermission("gban.AInvs")){
    196. p.showPlayer(player);
    197. }else{
    198. player.hidePlayer(p);
    199. }
    200. }
    201. for(Player p : mhidingPlayers){
    202. if(p.hasPermission("gban.OInvs") || p.hasPermission("gban.AInvs")||p.hasPermission("gban.MInvs")){
    203. p.showPlayer(player);
    204. }else{
    205. player.hidePlayer(p);
    206. }
    207. }
    208.  
    209.  
    210. }
    211. @EventHandler
    212. public void onPlayerLeave(PlayerQuitEvent event){
    213. Player player = event.getPlayer();
    214. if(ohidingPlayers.contains(player) || ahidingPlayers.contains(player) || mhidingPlayers.contains(player)){
    215. mhidingPlayers.remove(player);
    216. ohidingPlayers.remove(player);
    217. ahidingPlayers.remove(player);
    218. }else{
    219. return;
    220. }
    221.  
    222.  
    223. }
    224. @EventHandler
    225. public void onPlayerPick(PlayerPickupItemEvent event){
    226. Player player = event.getPlayer();
    227. if(ohidingPlayers.contains(player) || ahidingPlayers.contains(player) || mhidingPlayers.contains(player)){
    228. event.setCancelled(true);
    229. }else{
    230. return;
    231. }
    232.  
    233.  
    234.  
    235. }
    236. public static String getMessage(final String[] args, final int start){
    237. final StringBuilder stringbuilder = new StringBuilder();
    238.  
    239. for(int i = start; i < args.length; i++){
    240. if(i != start){
    241. stringbuilder.append(" ");
    242. }
    243. stringbuilder.append(args[I]);[/I]
    244. [I] }[/I]
    245. [I] return stringbuilder.toString();[/I]
    246.  
    247. [I] }[/I]
    248.  
    249. [I] @EventHandler[/I]
    250. [I] public void onBannedLogin(PlayerLoginEvent event){[/I]
    251. [I] if(event.getPlayer().isBanned()){[/I]
    252. [I] event.disallow(PlayerLoginEvent.Result.KICK_BANNED, ChatColor.RED + "You are banned!");[/I]
    253. [I] }else{[/I]
    254. [I] return;[/I]
    255. [I] }[/I]
    256.  
    257. [I] }[/I]
    258. [I] @EventHandler[/I]
    259. [I] public void onPlayerAttack(EntityDamageByEntityEvent event){[/I]
    260. [I] if(event.getDamager() instanceof Player){[/I]
    261. [I] final Player player = (Player) event.getDamager();[/I]
    262. [I] if(ohidingPlayers.contains(player) || ahidingPlayers.contains(player) || mhidingPlayers.contains(player)){[/I]
    263. [I] event.setCancelled(true);[/I]
    264. [I] }else{[/I]
    265. [I] return;[/I]
    266. [I] }[/I]
    267. [I] }[/I]
    268. [I] }[/I]
    269. [I] @EventHandler[/I]
    270. [I] public void onLockedLogin(PlayerJoinEvent event){[/I]
    271. [I] final Player player = event.getPlayer();[/I]
    272. [I] if(lock == false){[/I]
    273. [I] return;[/I]
    274. [I] }else if(!(player.hasPermission("gban.bypass"))){[/I]
    275. [I] player.kickPlayer(ChatColor.RED + "The server is currently locked!");[/I]
    276. [I] }else{[/I]
    277. [I] Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){[/I]
    278. [I] public void run(){[/I]
    279. [I] player.sendMessage(ChatColor.AQUA + "You joined a locked server!");[/I]
    280. [I] }[/I]
    281. [I] }, 10);[/I]
    282. [I] }[/I]
    283. [I] }[/I]
    284. [I] @EventHandler[/I]
    285. [I] public void onMove(PlayerMoveEvent event){[/I]
    286. [I] Player player = event.getPlayer();[/I]
    287. [I] if(player.getLocation().getX() >= 10000){[/I]
    288. [I] Location loc = new Location(player.getWorld(),player.getLocation().getX()-2,player.getLocation().getY(),player.getLocation().getZ());[/I]
    289. [I] player.teleport(loc);[/I]
    290. [I] }else if(player.getLocation().getX() <= -10000){[/I]
    291. [I] Location loc = new Location(player.getWorld(),player.getLocation().getX()+2,player.getLocation().getY(),player.getLocation().getZ());[/I]
    292. [I] player.teleport(loc);[/I]
    293. [I] }else if(player.getLocation().getZ() <= -10000){[/I]
    294. [I] Location loc = new Location(player.getWorld(),player.getLocation().getX(),player.getLocation().getY(),player.getLocation().getZ()+2);[/I]
    295. [I] player.teleport(loc);[/I]
    296. [I] }else if(player.getLocation().getZ() >= 10000){[/I]
    297. [I] Location loc = new Location(player.getWorld(),player.getLocation().getX(),player.getLocation().getY(),player.getLocation().getZ()-2);[/I]
    298. [I] player.teleport(loc);[/I]
    299. [I] }[/I]
    300.  
    301. [I] }[/I]
    302. [I]}[/I]
    + all my other commands work just fine it is only this one (which wasn't working in it's own call so I moved it here).
     
  11. Offline

    felixfritz

    boysnnoco Did you add the ban-command into your plugin.yml file?
     
  12. Offline

    boysnnoco

    felixfritz I did and here is my plugin.yml if you would like to see:
    Code:
    name: GlobalBan
    main: me.boysnnoco.CustomBan.Main
    version: 1.0
    description: >
                Youtube Plugin.
    commands:
      gban:
        description: Global Ban Someone!
      iban:
        description: Private Ban Someone!
      invs:
        description: Become Invisible!
        aliases: [v,vanish]
      vis:
        description: Become Visible!
      admin:
        description: Change Your Gamemode!
      ip:
        description: Get The Server IP!
      report:
        description: Report A Player!
      clearchat:
        description: Clear the Chat!
      gkick:
        description: Kick a player!
      msg:
      description: Message a player!
      aliases: [m,t,tell]
      lock:
        description: Lock the server!
      unlock:
        description: Unlock the server!
      offlineban:
        description: Ban an offline player!
      greload:
        description: Reload the config from file!
      temp:
        description: Tempban a player!
      r:
      description: Reply to a message!
      aliases: [reply]
      unban:
      description: Unban a player!
      aliases: [pardon]
      adminchat:
      description: Talk between admins!
      aliases: [ac]
     
  13. Offline

    felixfritz

    @boysnnoco that's odd... would it work, if you made an extra class for that command, just as you did with all the other commands?
     
  14. Offline

    boysnnoco

    felixfritz it seems like my plugin.yml reverted back or it didn't save let me add it in there how odd..

    felixfritz omg, it is fixed sorry about all the fuss about me being completly blind xD!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page