onCommand Help

Discussion in 'Plugin Development' started by iKanak, Apr 7, 2012.

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

    iKanak

    I'm still trying to learn java and such, and I think I'm doing fairly well. But I can't seem to figure out what the problem with my onCommand is.

    Console Erros:
    If you need more then this file let me know.
    My main file containing my onCommand method

    Code:java
    1.  
    2. package me.iKanak.EncryptedChat;
    3.  
    4. import java.util.HashMap;
    5. import java.util.logging.Logger;
    6.  
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.PluginManager;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class EncryptedChat extends JavaPlugin {
    15.  
    16. public final Logger logger = Logger.getLogger("Minecraft");
    17. private final ServerChatPlayerListener playerListener = new ServerChatPlayerListener(this);
    18. public final HashMap<Player, Boolean> encryptedUsers = new HashMap<Player, Boolean>();
    19.  
    20. public void onDisable() {
    21. PluginDescriptionFile pdfFile = this.getDescription();
    22. this.logger.info(pdfFile.getName() + " is now disabled!");
    23. }
    24.  
    25. public void onEnable() {
    26. PluginManager pm = getServer().getPluginManager();
    27. pm.registerEvents(this.playerListener, this);
    28.  
    29. PluginDescriptionFile pdfFile = this.getDescription();
    30. this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!");
    31. }
    32.  
    33. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    34. if(cmd.getName().equalsIgnoreCase("EncryptChat")) {
    35. if(sender instanceof Player) {
    36. if(args[1].equalsIgnoreCase("on")) {
    37. setEncryption((Player) sender, true);
    38. return true;
    39. } else if (args[1].equalsIgnoreCase("off")) {
    40. setEncryption((Player) sender, false);
    41. return true;
    42. } else {
    43. toggleEncryption((Player) sender);
    44. return true;
    45. }
    46. } else {
    47. this.logger.info("That command can only be used as a player!");
    48. }
    49. }
    50. return false;
    51. }
    52.  
    53. public void toggleEncryption(Player player) {
    54. if(encrpytionEnabled(player)) {
    55. player.sendMessage("Leaving encryption mode...");
    56. this.encryptedUsers.remove(player);
    57. } else {
    58. player.sendMessage("Entering encryption mode...");
    59. this.encryptedUsers.put(player, null);
    60. }
    61. }
    62.  
    63. public void setEncryption(Player player, boolean startEncrypting) {
    64. if (encrpytionEnabled(player)) {
    65. if (startEncrypting) {
    66. player.sendMessage("You're already encrypting!");
    67. }
    68. else {
    69. player.sendMessage("Leaving encryption mode...");
    70. this.encryptedUsers.remove(player);
    71. }
    72. } else {
    73. if (!startEncrypting) {
    74. player.sendMessage("You're not in encryption mode!");
    75. } else {
    76. player.sendMessage("Entering encryption mode...");
    77. this.encryptedUsers.put(player, null);
    78. }
    79. }
    80. }
    81.  
    82. public boolean encrpytionEnabled(Player player){
    83. return this.encryptedUsers.containsKey(player);
    84. }
    85.  
    86. }
    87.  
     
  2. Offline

    Giant

    Code:java
    1.  
    2. if(args[1].equalsIgnoreCase("on")) {
    3.  

    You forget to check if args is actually 2 long in this case. If you only pass 1 argument it will be 1 long and args[0].
     
Thread Status:
Not open for further replies.

Share This Page