Solved {} help at end - causing errors

Discussion in 'Plugin Development' started by nirajm34, Dec 1, 2013.

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

    nirajm34

    Hey when i do the command /enchant [player] nothing happens and I'm using the newest build of craftbukkit.

    Code:java
    1. package com.saicogaming;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.enchantments.Enchantment;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public final class start extends JavaPlugin {
    14.  
    15. @Override
    16. public void onEnable(){
    17. getLogger().info("onEnable has been invoked!");
    18. // TODO Insert logic to be performed when the plugin is enabled
    19. }
    20. @Override
    21. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    22. // Uses equalsIgnoreCase() over equals() to accept "ignite" and "IgNiTe."
    23. if (cmd.getName().equalsIgnoreCase("ignite")) {
    24. // Make sure that the player specified exactly one argument (the name of the player to ignite).
    25. if (args.length != 1) {
    26. // When onCommand() returns false, the help message associated with that command is displayed.
    27. return false;
    28. }
    29.  
    30. // Make sure the sender is a player.
    31. if (!(sender instanceof Player)) {
    32. sender.sendMessage("Only players can set other players on fire.");
    33. sender.sendMessage("This is an arbitrary requirement for demonstration purposes only.");
    34. return true;
    35. }
    36.  
    37. // Get the player who should be set on fire. Remember that indecies start with 0, not 1.
    38. Player target = Bukkit.getServer().getPlayer(args[0]);
    39.  
    40. // Make sure the player is online.
    41. if (target == null) {
    42. sender.sendMessage(args[0] + " is not currently online.");
    43. return true;
    44. }
    45.  
    46. // Sets the player on fire for 1,000 ticks (there are ~20 ticks in second, so 50 seconds total).
    47. target.setFireTicks(1000);
    48. target.setAllowFlight(true);
    49. this.getServer().broadcastMessage(ChatColor.RED + "HA YOUR ON FIRE");
    50. return true;
    51. }
    52. return false;
    53. }
    54.  
    55. public boolean onCommand1(CommandSender sender, Command cmd, String label, String[] args) {
    56. // Uses equalsIgnoreCase() over equals() to accept "echnant" and "EnChAnT."
    57. if (cmd.getName().equalsIgnoreCase("enchant")) {
    58. // Make sure that the player specified exactly one argument (the name of the player to give enchanted item too).
    59. if (args.length != 1) {
    60. // When onCommand() returns false, the help message associated with that command is displayed.
    61. return false;
    62. }
    63.  
    64. // Make sure the send is a player.
    65. if (!(sender instanceof Player)) {
    66. sender.sendMessage("Only players another player an item");
    67. sender.sendMessage("This is an arbitrary requirement for demonstration purposes only.");
    68. return true;
    69. }
    70.  
    71. // Get the player who should be set on fire. Remember that indecies start with 0, not 1.
    72. Player target = Bukkit.getServer().getPlayer(args[0]);
    73.  
    74. // Make sure the player is online.
    75. if (target == null) {
    76. sender.sendMessage(args[0] + " is not currently online.");
    77. return true;
    78. }
    79. ItemStack item = new ItemStack(Material.DIAMOND_SWORD);
    80. item.addEnchantment( Enchantment.DAMAGE_ALL , 100);
    81. target.getInventory().addItem(item);
    82. sender.sendMessage(ChatColor.BLUE + "[Enchantment]" + ChatColor.RED + args[0] + "Here is a Diamond Sword, Enchanted with Sharpness 100");
    83. return true;
    84. }
    85. return false;
    86. }
    87. }
     
  2. Offline

    clienthax

    you need all of your command processing code in the onCommand method.
     
  3. Offline

    nirajm34

    so literally just copy all the code in to the first oncommand method?
    Like this?

    Code:java
    1. package com.saicogaming;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.enchantments.Enchantment;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.inventory.ItemStack;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public final class start extends JavaPlugin {
    14.  
    15. @Override
    16. public void onEnable(){
    17. getLogger().info("onEnable has been invoked!");
    18. // TODO Insert logic to be performed when the plugin is enabled
    19. }
    20. @Override
    21. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    22. // Uses equalsIgnoreCase() over equals() to accept "ignite" and "IgNiTe."
    23. if (cmd.getName().equalsIgnoreCase("ignite")) {
    24. // Make sure that the player specified exactly one argument (the name of the player to ignite).
    25. if (args.length != 1) {
    26. // When onCommand() returns false, the help message associated with that command is displayed.
    27. return false;
    28. }
    29.  
    30. // Make sure the sender is a player.
    31. if (!(sender instanceof Player)) {
    32. sender.sendMessage("Only players can set other players on fire.");
    33. sender.sendMessage("This is an arbitrary requirement for demonstration purposes only.");
    34. return true;
    35. }
    36.  
    37. // Get the player who should be set on fire. Remember that indecies start with 0, not 1.
    38. Player target = Bukkit.getServer().getPlayer(args[0]);
    39.  
    40. // Make sure the player is online.
    41. if (target == null) {
    42. sender.sendMessage(args[0] + " is not currently online.");
    43. return true;
    44. }
    45.  
    46. // Sets the player on fire for 1,000 ticks (there are ~20 ticks in second, so 50 seconds total).
    47. target.setFireTicks(1000);
    48. target.setAllowFlight(true);
    49. this.getServer().broadcastMessage(ChatColor.RED + "HA YOUR ON FIRE");
    50. return true;
    51. }
    52. if (cmd.getName().equalsIgnoreCase("enchant")) {
    53. // Make sure that the player specified exactly one argument (the name of the player to give enchanted item too).
    54. if (args.length != 1) {
    55. // When onCommand() returns false, the help message associated with that command is displayed.
    56. return false;
    57. }
    58.  
    59. // Make sure the send is a player.
    60. if (!(sender instanceof Player)) {
    61. sender.sendMessage("Only players another player an item");
    62. sender.sendMessage("This is an arbitrary requirement for demonstration purposes only.");
    63. return true;
    64. }
    65.  
    66. // Get the player who should be set on fire. Remember that indecies start with 0, not 1.
    67. Player target = Bukkit.getServer().getPlayer(args[0]);
    68. // Make sure the player is online.
    69. if (target == null) {
    70. sender.sendMessage(args[0] + " is not currently online.");
    71. return true;
    72. }
    73. ItemStack item = new ItemStack(Material.DIAMOND_SWORD);
    74. item.addEnchantment( Enchantment.DAMAGE_ALL , 100);
    75. target.getInventory().addItem(item);
    76. sender.sendMessage(ChatColor.BLUE + "[Enchantment]" + ChatColor.RED + args[0] + "Here is a Diamond Sword, Enchanted with Sharpness 100");
    77. return true;
    78. }
    79. return false;
    80. }


    ooo that code don't work, theres an error at the last "}"

    can anyone see why?

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

    IkBenHarm

    nirajm34 probably because you have one too much or something, duble click one (in eclipse, do not know for others) to see what they "cover"
     
  5. Offline

    chrisman0091

    Only use one onCommand method, not one for each command. Just have an if statement to check the command itself every time. Check to see if that fixes your error.


    Yes, like that. I haven't checked the code itself to see if its all correct, but that's how you want it(all in once method).

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

    nirajm34

    IkBenHarm double click on the "}"? that does nothing :p that the error coming up.
    chrisman0091 and thanks! yeh i see its meant to be in one method now, if that for all command that you have in that .java?
     
  7. Offline

    chrisman0091

    Honestly, I suggest just making a class for your commands.

    Make your class(SomethingCommandExecuter implements CommandExecutor)

    Then just put
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. //all your commands
    4. }


    Then in your onEnable method register the commands
    Code:java
    1.  
    2. getCommand("commandgoeshere").setExecutor(new SomethingCommandExecutor());
    3.  


    Have one for each command in your SomethingCommandExecutor class. You can then use this class for all the commands in your plugin instead of your main class which you DON'T want cluttered, especially with bigger and more complex plugins.
     
  8. Offline

    Th3Br1x

  9. Offline

    nirajm34

    Th3Br1x chrisman0091 Perfect thank you guys! really helped!

    can you see if there is a issue with the code for the time being, cause i have tried to see whats wrong this it but i can't see anything!

    Code:java
    1. package com.saicogaming;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.enchantments.Enchantment;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.inventory.ItemStack;
    12.  
    13. public class StartCommandExecutor implements CommandExecutor {
    14.  
    15. @Override
    16. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    17. if (cmd.getName().equalsIgnoreCase("enchant")) {
    18. // Make sure that the player specified exactly one argument (the name of the player to give enchanted item too).
    19. if (args.length != 1) {
    20. // When onCommand() returns false, the help message associated with that command is displayed.
    21. return false;
    22. }
    23.  
    24. // Make sure the send is a player.
    25. if (!(sender instanceof Player)) {
    26. sender.sendMessage("Only players another player an item");
    27. sender.sendMessage("This is an arbitrary requirement for demonstration purposes only.");
    28. return true;
    29. }
    30.  
    31. // Get the player who should be set on fire. Remember that indecies start with 0, not 1.
    32. Player target = Bukkit.getServer().getPlayer(args[0]);
    33. // Make sure the player is online.
    34. if (target == null) {
    35. sender.sendMessage(args[0] + " is not currently online.");
    36. return true;
    37. }
    38. ItemStack item = new ItemStack(Material.DIAMOND_SWORD);
    39. item.addEnchantment( Enchantment.DAMAGE_ALL , 100);
    40. target.getInventory().addItem(item);
    41. sender.sendMessage(ChatColor.BLUE + "[Enchantment]" + ChatColor.RED + args[0] + "Here is a Diamond Sword, Enchanted with Sharpness 100");
    42. return true;
    43. }
    44. return false;
    45.  
    46. if (cmd.getName().equalsIgnoreCase("ignite")) {
    47. // Make sure that the player specified exactly one argument (the name of the player to ignite).
    48. if (args.length != 1) {
    49. // When onCommand() returns false, the help message associated with that command is displayed.
    50. return false;
    51. }
    52.  
    53. // Make sure the sender is a player.
    54. if (!(sender instanceof Player)) {
    55. sender.sendMessage("Only players can set other players on fire.");
    56. sender.sendMessage("This is an arbitrary requirement for demonstration purposes only.");
    57. return true;
    58. }
    59.  
    60. // Get the player who should be set on fire. Remember that indecies start with 0, not 1.
    61. Player target = Bukkit.getServer().getPlayer(args[0]);
    62.  
    63. // Make sure the player is online.
    64. if (target == null) {
    65. sender.sendMessage(args[0] + " is not currently online.");
    66. return true;
    67. }
    68.  
    69. // Sets the player on fire for 1,000 ticks (there are ~20 ticks in second, so 50 seconds total).
    70. target.setFireTicks(1000);
    71. target.setAllowFlight(true);
    72. return true;
    73. }
    74. return false;
    75.  
    76. }
    77.  
     
  10. Offline

    Th3Br1x

    nirajm34 You forgot the constructor:
    Code:java
    1. public class StartCommandExecutor implements CommandExecutor {
    2.  
    3. YourMainClass plugin;
    4.  
    5. public StartCommandExecutor(YourMainClass instance){
    6. plugin = instance;
    7. }
    8.  
    9. @Override
    10. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    11. if (cmd.getName().equalsIgnoreCase("enchant")) {
    12. // Make sure that the player specified exactly one argument (the name of the player to give enchanted item too).
    13. if (args.length != 1) {
    14. // When onCommand() returns false, the help message associated with that command is displayed.
    15. return false;
    16. }
    17.  
    18. // Make sure the send is a player.
    19. if (!(sender instanceof Player)) {
    20. sender.sendMessage("Only players another player an item");
    21. sender.sendMessage("This is an arbitrary requirement for demonstration purposes only.");
    22. return true;
    23. }
    24.  
    25. // Get the player who should be set on fire. Remember that indecies start with 0, not 1.
    26. Player target = plugin.getServer().getPlayer(args[0]);
    27. // Make sure the player is online.
    28. if (target == null) {
    29. sender.sendMessage(args[0] + " is not currently online.");
    30. return true;
    31. }
    32. ItemStack item = new ItemStack(Material.DIAMOND_SWORD);
    33. item.addEnchantment( Enchantment.DAMAGE_ALL , 100);
    34. target.getInventory().addItem(item);
    35. sender.sendMessage(ChatColor.BLUE + "[Enchantment]" + ChatColor.RED + args[0] + "Here is a Diamond Sword, Enchanted with Sharpness 100");
    36. return true;
    37. }
    38.  
    39. if (cmd.getName().equalsIgnoreCase("ignite")) {
    40. // Make sure that the player specified exactly one argument (the name of the player to ignite).
    41. if (args.length != 1) {
    42. // When onCommand() returns false, the help message associated with that command is displayed.
    43. return false;
    44. }
    45.  
    46. // Make sure the sender is a player.
    47. if (!(sender instanceof Player)) {
    48. sender.sendMessage("Only players can set other players on fire.");
    49. sender.sendMessage("This is an arbitrary requirement for demonstration purposes only.");
    50. return true;
    51. }
    52.  
    53. // Get the player who should be set on fire. Remember that indecies start with 0, not 1.
    54. Player target = plugin.getServer().getPlayer(args[0]);
    55.  
    56. // Make sure the player is online.
    57. if (target == null) {
    58. sender.sendMessage(args[0] + " is not currently online.");
    59. return true;
    60. }
    61.  
    62. // Sets the player on fire for 1,000 ticks (there are ~20 ticks in second, so 50 seconds total).
    63. target.setFireTicks(1000);
    64. target.setAllowFlight(true);
    65. return true;
    66. }
    67. return false;
    68.  
    69. }
    70.  


    Just replace "YourMainClass" with the name of your main class.
    Just modified a bot of the code, should work now.
     
Thread Status:
Not open for further replies.

Share This Page