My Command will not work

Discussion in 'Plugin Development' started by killerzz1, May 15, 2014.

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

    killerzz1

    ok so i am trying to add a command to my plugin and it is giving me an error here is the code for that command
    Code:java
    1. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    2. if (cmd.getName().equalsIgnoreCase("killermoney")) {
    3. sender.sendMessage(getConfig().getString("message"));
    4. return true;
    5. }
    6.  
    7.  
    8. if (cmd.getName().equalseIgnoreCase("setmoney")) {
    9. if (args.lenght == 0) {
    10. sender.sendMessage(ChatColor.RED + "Please add the ammount you want the player to get!");
    11. return true;
    12. }
    13. StringBuilder str = new StringBuilder();
    14. for (int i = 0; i < args.length; i++) {
    15. str.append(args[i] + " ");
    16. }
    17. String killermoney = str.toString();
    18. getConfig().set("message", motd);
    19. saveConfig();
    20. sender.sendMessage(ChatColor.GREEN + "The Money was set to" + killermoney);
    21. return true;
    22. }
    23. return true;
    24. }
    25.  
    26. [/i]

    here is all of my imports too
    Code:java
    1. import org.bukkit.Bukkit;
    2. import org.bukkit.ChatColor;
    3. import org.bukkit.OfflinePlayer;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.entity.PlayerDeathEvent;
    11. import org.bukkit.event.player.PlayerEvent;
    12. import org.bukkit.event.player.PlayerJoinEvent;
    13. import org.bukkit.plugin.RegisteredServiceProvider;
    14. import org.bukkit.plugin.java.JavaPlugin;
     
  2. Offline

    Drew1080

    killerzz1
    Please post the stacktrace you got then.
     
  3. Offline

    minoneer

    Please post the error - that will make it so much easier for us to help you :)
     
  4. Offline

    killerzz1

    what do you mean do not stacktrace you got then?

    ok i will put the errors in red

    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

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

    Drew1080

    killerzz1
    You said you got an error. Please post us that error.
     
  6. Offline

    killerzz1

    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
     
  7. Offline

    Drew1080

    killerzz1
    You misspelt 'length' on line 9 on the code you gave us above.
     
  8. Offline

    killerzz1

    oh ok so was that the error ?
     
  9. Offline

    minoneer

    It could very well be it. Correct it and tell us if it's gone :)
     
  10. Offline

    killerzz1

    ok i have fixed it and the it did not remove the error
    here is all of the code

    Code:java
    1. package me.killerzz1.killermoney;
    2.  
    3. import net.milkbowl.vault.economy.Economy;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.OfflinePlayer;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.entity.PlayerDeathEvent;
    15. import org.bukkit.event.player.PlayerEvent;
    16. import org.bukkit.event.player.PlayerJoinEvent;
    17. import org.bukkit.plugin.RegisteredServiceProvider;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class Main extends JavaPlugin implements org.bukkit.event.Listener{
    21. public static Economy econ = null;
    22. public static Economy eco;
    23.  
    24. @EventHandler
    25. public void onPlayerJoin(PlayerJoinEvent e) {
    26. Player p = e.getPlayer();
    27. p.sendMessage(ChatColor.BLUE + "This Server is Running KillerMoney Made By Killerzz1");
    28. }
    29.  
    30.  
    31. @Override
    32. public void onEnable() {
    33. System.out.println("KillerMoney Is Enabled!");
    34. Bukkit.getPluginManager().registerEvents(this, this);
    35. getConfig().options().copyDefaults(true);
    36. saveConfig();
    37. if (!setupEconomy() ) {
    38. getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    39. getServer().getPluginManager().disablePlugin(this);
    40. return;
    41.  
    42. }
    43. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    44. if (cmd.getName().equalsIgnoreCase("killermoney")) {
    45. sender.sendMessage(getConfig().getString("message"));
    46. return true;
    47. }
    48.  
    49.  
    50. if (cmd.getName().equalseIgnoreCase("setmoney")) {
    51. if (args.length == 0) {
    52. sender.sendMessage(ChatColor.RED + "Please add the ammount you want the player to get!");
    53. return true;
    54. }
    55. StringBuilder str = new StringBuilder();
    56. for (int i = 0; i < args.length; i++) {
    57. str.append(args[i] + " ");
    58. }
    59. String killermoney = str.toString();
    60. getConfig().set("message", motd);
    61. saveConfig();
    62. sender.sendMessage(ChatColor.GREEN + "The Money was set to" + killermoney);
    63. return true;
    64. }
    65. return true;
    66. }
    67.  
    68.  
    69. }
    70. private boolean setupEconomy() {
    71. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    72. return false;
    73. }
    74. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    75. if (rsp == null) {
    76. return false;
    77. }
    78. econ = rsp.getProvider();
    79. return econ != null;
    80. }
    81.  
    82. @EventHandler
    83. public void onPlayerDeath(PlayerDeathEvent e) {
    84. Player p = e.getEntity().getPlayer().getKiller();
    85.  
    86. if (p != null)
    87. {
    88. if (p.hasPermission("world.pvp")) {
    89. String message = getConfig().getString("Money");
    90. double money = getConfig().getDouble("Money");
    91.  
    92. p.sendMessage(message);
    93.  
    94. message = ChatColor.translateAlternateColorCodes('&', message);
    95. eco.depositPlayer(p.getName(), money);
    96.  
    97.  
    98.  
    99. }
    100. }
    101. }
    102. }[/i]
     
  11. Offline

    Azubuso

    killerzz1
    Random question, why do you have duplicate imports of CommandSender and Command?
     
  12. Offline

    killerzz1

    oh so i also dont know where there are two
     
  13. Offline

    Azubuso

    killerzz1 Also, I fixed up some smaller issues in your code.
    Code:java
    1.  
    2. package me.killerzz1.killermoney;
    3.  
    4. import net.milkbowl.vault.economy.Economy;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.OfflinePlayer;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.entity.PlayerDeathEvent;
    14. import org.bukkit.event.player.PlayerEvent;
    15. import org.bukkit.event.player.PlayerJoinEvent;
    16. import org.bukkit.plugin.RegisteredServiceProvider;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18. import org.bukkit.event.Listener;
    19.  
    20. public class Main extends JavaPlugin implements Listener {
    21. public static Economy econ = null;
    22. public static Economy eco;
    23.  
    24. @EventHandler
    25. public void onPlayerJoin(PlayerJoinEvent e) {
    26. Player p = e.getPlayer();
    27. p.sendMessage(ChatColor.BLUE + "This Server is Running KillerMoney Made By Killerzz1");
    28. }
    29.  
    30.  
    31. @Override
    32. public void onEnable() {
    33. getLogger().info("KillerMoney Is Enabled!");
    34. Bukkit.getPluginManager().registerEvents(this, this);
    35. getCommand("killermoney").setExecutor(this);
    36. getConfig().options().copyDefaults(true);
    37. saveConfig();
    38. if (!setupEconomy() ) {
    39. getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    40. getServer().getPluginManager().disablePlugin(this);
    41. return;
    42. }
    43.  
    44. }
    45.  
    46. @Override
    47. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    48. if (cmd.getName().equalsIgnoreCase("killermoney")) {
    49. sender.sendMessage(getConfig().getString("message"));
    50. return true;
    51. }
    52.  
    53.  
    54. if (cmd.getName().equalseIgnoreCase("setmoney")) {
    55. if (args.length == 0) {
    56. sender.sendMessage(ChatColor.RED + "Please add the ammount you want the player to get!");
    57. return true;
    58. }
    59.  
    60. StringBuilder str = new StringBuilder();
    61.  
    62. for (int i = 0; i < args.length; i++) {
    63. str.append(args[i] + " ");
    64. }
    65.  
    66. String killermoney = str.toString();
    67. getConfig().set("message", motd);
    68. saveConfig();
    69. sender.sendMessage(ChatColor.GREEN + "The Money was set to" + killermoney);
    70. return true;
    71. }
    72. return true;
    73. }
    74.  
    75. private boolean setupEconomy() {
    76. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    77. return false;
    78. }
    79. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    80. if (rsp == null) {
    81. return false;
    82. }
    83. econ = rsp.getProvider();
    84. return econ != null;
    85. }
    86.  
    87. @EventHandler
    88. public void onPlayerDeath(PlayerDeathEvent e) {
    89.  
    90. Player p = e.getEntity().getPlayer().getKiller();
    91.  
    92. if (p != null) {
    93.  
    94. if (p.hasPermission("world.pvp")) {
    95. String message = getConfig().getString("Money");
    96. double money = getConfig().getDouble("Money");
    97.  
    98. p.sendMessage(message);
    99.  
    100. message = ChatColor.translateAlternateColorCodes('&', message);
    101. eco.depositPlayer(p.getName(), money);
    102. }
    103. }
    104. }
    105. }[I][/I][/i]
     
  14. Offline

    killerzz1

    ok thanks for that but when i use the code that you edited it is giving me some other errors
     
  15. Offline

    Azubuso

    killerzz1
    What errors are you talking about? Is the IDE you're working in showing you them or is it in game?
     
  16. Offline

    killerzz1

    it is showing them in eclipse

    The errors are going to be red
    if (cmd.getName().equalseIgnoreCase("setmoney")) {
    getConfig().set("message", motd);
    if (cmd.getName().equalsIgnoreCase("killermoney")) {

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

    d3v1n302418

    killerzz1 You realize you had your onCommand() method inside of your onEnable()? I have seen multiple questions by you on this forum and you demonstrate little understanding of the Java programming language. I suggest learning it. Not to try to put you down, but it will make your life SO much easier.
     
  18. Offline

    killerzz1


    Ok thanks for that i am learning java at the same time thanks again so was that the error?
     
  19. Offline

    Azubuso

    killerzz1
    If it is in Eclipse, just hover over it and wait for it to pop up with a little dialog explaining the error.
     
  20. Offline

    killerzz1

    it is saying for the cmd error (CMD can not be resolved)​
    and for the motd it is saying(MOTD can not be resolved to a variable)​
     
  21. Offline

    99storm2

    you are getting a error with cmd because in you onCommand it says
    onCommand(CommandSender sender, Command command, String label, String[] args)
    so you should be doing command.getName not cmd.getName
     
  22. Offline

    ImPhantom

    killerzz1
    Is your "onCommand" method inside of your "onEnable"? Because it seems as if it is. (Its not supposed to be)
     
  23. Offline

    Azubuso

    ImPhantom
    In his code it was, In the little bit I posted I'd fixed that.

    killerzz1
    But it's as 99storm2 has said, make sure to double check what you're variables are actually defined as, you can't just go and use some imaginative variable you think may be correct.
     
    ImPhantom likes this.
Thread Status:
Not open for further replies.

Share This Page