Solved Need help with Multiple comamnds!

Discussion in 'Plugin Development' started by gabe4356, Aug 31, 2014.

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

    gabe4356

    So, I am making a plugin, and I can't figure out how to add multiple commands to my main class, this just isn't working! Anyone can help? This is my code:
    Code:java
    1. package com.gabe.thor;
    2.  
    3. import java.awt.Event;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.Sound;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. @SuppressWarnings("unused")
    19. public class Thor extends JavaPlugin implements Listener {
    20.  
    21. @SuppressWarnings("deprecation")
    22. @EventHandler
    23. public void onPlayerInteractBlock(PlayerInteractEvent event) {
    24. Player player = event.getPlayer();
    25. if (player.getItemInHand().getType() == Material.DIAMOND_AXE) {
    26. player.getWorld().strikeLightning(player.getTargetBlock(null, 200).getLocation());
    27. player.damage(5);
    28. player.getWorld().playSound(player.getLocation(), Sound.ENDERDRAGON_HIT, 1F, 1F);
    29. }
    30. }
    31.  
    32.  
    33. @SuppressWarnings("deprecation")
    34. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    35. Player player = (Player) sender;
    36. if(cmd.getName().equalsIgnoreCase("ThoR")){
    37. String Thor = "Thors Hammer";
    38. ItemStack myItem = new ItemStack(Material.DIAMOND_AXE);
    39. ItemMeta im = myItem.getItemMeta();
    40. im.setDisplayName(Thor);
    41. myItem.setItemMeta(im);
    42. player.sendMessage(ChatColor.RED+ "You have been given Thors hammer, click to spawn lightning and kill everything around you! range is 200!");
    43. getServer().broadcastMessage(ChatColor.DARK_AQUA+ "BEWARE! Someone has been given the Thor hammer!");
    44. player.getInventory().addItem(new ItemStack(Material.DIAMOND_AXE));
    45. player.damage(5);
    46. if(cmd.getName().equalsIgnoreCase("cmds")){
    47. player.sendMessage(ChatColor.GOLD+ "=================================");
    48. player.sendMessage(ChatColor.AQUA+ " Commands ");
    49. player.sendMessage(ChatColor.GOLD+ "=================================");
    50. player.sendMessage(ChatColor.AQUA+ "Thor : Gives you Thors hammer, click anywhere with it");
    51. player.sendMessage(ChatColor.AQUA+ "Cmds : Lists these commands");
    52.  
    53. }
    54. return true;
    55. }
    56. return true;
    57. }
    58.  
    59. public void onDisable() {
    60. getLogger().info(ChatColor.GREEN+ "Thor plugin has been disabled!");
    61. getServer().broadcastMessage(ChatColor.GREEN+ "Thor has been disabled!");
    62. //On Disable method stub
    63. }
    64.  
    65. public void onEnable() {
    66. getLogger().info(ChatColor.GREEN+ "Thor plugin has been enabled!");
    67. getServer().getPluginManager().registerEvents(this, this);
    68. getServer().broadcastMessage(ChatColor.GREEN+ "Thor has been enabled!");
    69.  
    70. //on Enable Method stub
    71.  
    72.  
    73. {
    74. }
    75. }
    76.  
    77. }
    78.  


    This is my plugin.yml file code:
    Code:
    name: Thor
    main: com.gabe.thor.Thor
    version: 1.0.0
    commands:
      Thor:
        description: Gives you Thors hammer, right click with it to spawn lightning
        usage: /Thor
        permission: thor.hammer
        permission-message: You do not permission to be Thor!
      cmds:
        description: Lists all Thor Commands
        usage: /cmds
        permission: thor.help
        permission-message: You can not list Thor commands!
        
    Please help?
     
  2. Offline

    hintss

    fix your brackets
     
  3. Offline

    gabe4356

    What line?
     
  4. Offline

    hintss

    all over the place.
     
  5. Offline

    gabe4356

    Sorry, I'm sort of new to creating plugins, and you are probably going to hate me when I ask this, but What do you mean fix the brackets?
     
  6. Offline

    hintss

    ok, toward the end, there's a pair of {} with nothing in them, go ahead and delete those, and the second if in the onCommand should be an else if, so it looks like

    Code:java
    1. if (foo()) {
    2. // stuff
    3. } else if (foo()) {
    4. // other stuff
    5. }
     
  7. Offline

    gabe4356

    Thanks!

    Wait..I don't see any pair of {}, do you mean ()? and, also, Should I change foo to a command?

    Ok, so , I did what you said, here is my code, but it still doesn't work.
    Code:java
    1. package com.gabe.thor;
    2.  
    3. import java.awt.Event;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.Sound;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. @SuppressWarnings("unused")
    19. public class Thor extends JavaPlugin implements Listener {
    20.  
    21. @SuppressWarnings("deprecation")
    22. @EventHandler
    23. public void onPlayerInteractBlock(PlayerInteractEvent event) {
    24. Player player = event.getPlayer();
    25. if (player.getItemInHand().getType() == Material.DIAMOND_AXE) {
    26. player.getWorld().strikeLightning(player.getTargetBlock(null, 200).getLocation());
    27. player.damage(5);
    28. player.getWorld().playSound(player.getLocation(), Sound.ENDERDRAGON_HIT, 1F, 1F);
    29. }
    30. }
    31.  
    32.  
    33. @SuppressWarnings("deprecation")
    34. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    35. Player player = (Player) sender;
    36. if (cmd.getName().equalsIgnoreCase("ThoR")) {
    37. player.sendMessage(ChatColor.RED+ "You have been given Thors hammer, click to spawn lightning and kill everything around you! range is 200!");
    38. getServer().broadcastMessage(ChatColor.DARK_AQUA+ "BEWARE! Someone has been given the Thor hammer!");
    39. player.getInventory().addItem(new ItemStack(Material.DIAMOND_AXE));
    40. player.damage(5);
    41. if (cmd.getName().equalsIgnoreCase("cmds")) {
    42. player.sendMessage(ChatColor.GOLD+ "=================================");
    43. player.sendMessage(ChatColor.AQUA+ " Thor Commands ");
    44. player.sendMessage(ChatColor.GOLD+ "=================================");
    45. player.sendMessage(ChatColor.AQUA+ "Thor : Gives you Thors hammer, click anywhere with it");
    46. player.sendMessage(ChatColor.AQUA+ "Cmds : Lists these commands");
    47.  
    48. } else if (foo()) {
    49. player.sendMessage(ChatColor.DARK_RED+ "Unknown command, type /cmds");
    50. }
    51. }
    52. return true;
    53. }
    54.  
    55. private boolean foo() {
    56. // TODO Auto-generated method stub
    57. return false;
    58. }
    59.  
    60.  
    61. public void onDisable() {
    62. getLogger().info(ChatColor.GREEN+ "Thor plugin has been disabled!");
    63. getServer().broadcastMessage(ChatColor.GREEN+ "Thor has been disabled!");
    64. //On Disable method stub
    65. }
    66.  
    67. public void onEnable() {
    68. getLogger().info(ChatColor.GREEN+ "Thor plugin has been enabled!");
    69. getServer().getPluginManager().registerEvents(this, this);
    70. getServer().broadcastMessage(ChatColor.GREEN+ "Thor has been enabled!");
    71.  
    72. //on Enable Method stub
    73.  
    74.  
    75.  
    76.  
    77. }
    78. }
    79.  
    80.  
    81.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 28, 2017
  8. Offline

    hintss

    lines 73-74, there's a pair of {} with nothing else in between. As for the foo question, yes
     
  9. Offline

    JBoss925

    Notice how you don't close the bracket after line 40. That's the error.
     
    hintss likes this.
  10. Offline

    gabe4356

    Ok, I did that, still doesn't work! My code:
    Code:java
    1. package com.gabe.thor;
    2.  
    3. import java.awt.Event;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.Sound;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. @SuppressWarnings("unused")
    19. public class Thor extends JavaPlugin implements Listener {
    20.  
    21. @SuppressWarnings("deprecation")
    22. @EventHandler
    23. public void onPlayerInteractBlock(PlayerInteractEvent event) {
    24. Player player = event.getPlayer();
    25. if (player.getItemInHand().getType() == Material.DIAMOND_AXE) {
    26. player.getWorld().strikeLightning(player.getTargetBlock(null, 200).getLocation());
    27. player.damage(5);
    28. player.getWorld().playSound(player.getLocation(), Sound.ENDERDRAGON_HIT, 1F, 1F);
    29. }
    30. }
    31.  
    32.  
    33. @SuppressWarnings("deprecation")
    34. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    35. Player player = (Player) sender;
    36. if (cmd.getName().equalsIgnoreCase("ThoR")) {
    37. player.sendMessage(ChatColor.RED+ "You have been given Thors hammer, click to spawn lightning and kill everything around you! range is 200!");
    38. getServer().broadcastMessage(ChatColor.DARK_AQUA+ "BEWARE! Someone has been given the Thor hammer!");
    39. player.getInventory().addItem(new ItemStack(Material.DIAMOND_AXE));
    40. player.damage(5);
    41.  
    42. {
    43.  
    44. if (cmd.getName().equalsIgnoreCase("cmds")) {
    45. player.sendMessage(ChatColor.GOLD+ "=================================");
    46. player.sendMessage(ChatColor.AQUA+ " Thor Commands ");
    47. player.sendMessage(ChatColor.GOLD+ "=================================");
    48. player.sendMessage(ChatColor.AQUA+ "Thor : Gives you Thors hammer, click anywhere with it");
    49. player.sendMessage(ChatColor.AQUA+ "Cmds : Lists these commands");
    50.  
    51. } else if (foo()) {
    52. player.sendMessage(ChatColor.DARK_RED+ "Unknown command, type /cmds");
    53. }
    54. }
    55. return true;
    56. }
    57. return true;
    58. }
    59.  
    60. private boolean foo() {
    61. // TODO Auto-generated method stub
    62. return false;
    63. }
    64.  
    65.  
    66. public void onDisable() {
    67. getLogger().info(ChatColor.GREEN+ "Thor plugin has been disabled!");
    68. getServer().broadcastMessage(ChatColor.GREEN+ "Thor has been disabled!");
    69. //On Disable method stub
    70. }
    71.  
    72. public void onEnable() {
    73. getLogger().info(ChatColor.GREEN+ "Thor plugin has been enabled!");
    74. getServer().getPluginManager().registerEvents(this, this);
    75. getServer().broadcastMessage(ChatColor.GREEN+ "Thor has been enabled!");
    76.  
    77. //on Enable Method stub
    78.  
    79.  
    80.  
    81.  
    82. }
    83. }
    84.  
    85.  
    86.  
     
  11. Offline

    JBoss925

    On line 42 you put a start bracket, not an end bracket. Replace line 42 with "}" instead of "{".
     
  12. Offline

    gabe4356

    Thanks!
     
  13. Offline

    Tecno_Wizard

    gabe4356, You may want to learn some more Java before you continue. I am not saying you should not program with Bukkit, I'm saying that it will be extremely difficult. All of us here at some point were new to Java.
    My high school programming teacher was completely incompetent. I used this YouTube channel to get myself to where I am now. Watch all of the videos here, and you can be off to making your own Bukkit plugins without assistance in no time.
     
    hintss likes this.
  14. Offline

    Lewishjames

    @gabe4356
    What Part of the plugin is not working?
    is the plugin listed in /plugins or not?
    If its not listed you should recheck your plugin.yml
     
  15. Offline

    gabe4356

    yea, everything works fine. lol, Lewishjames your profile pic xD
     
  16. Offline

    fireblast709

    He's doing it all wrong. First learn Java, then Bukkit. This is why we can't have nice things :c.

    Also, he is using the Bukkit Logger... (which is Logger.getLogger("Minecraft"), not really what you should be using). And he uses labels instead of Command names... And unchecked casts... I should start making a list of this
     
    hintss likes this.
  17. Offline

    macboinc

  18. Offline

    Tecno_Wizard

    I suggested him because he explains stuff and doesn't make nearly as many mistakes as most. The BCBroz anyone?

    Also, HOLY CRAP THAT GUY IS GOOD. I'll be looking over some of those later, I'm still learning things about Java and do not plan to stop anytime soon.
     
    hintss likes this.
  19. Offline

    Lewishjames

    I learnt everything from Pogo and the bcbroz also i learnt java before hand just to get some simple threads and methods set.
     
  20. Offline

    BeastCraft3

    gabe4356
    1) Fix your brackets.
    2) remove the return true;
    3) add return false; under the last command.

    If that doesnt work, Please tahg me and tell me the new issue
     
  21. Offline

    JonasXPX

    1.Create a class for a command, forexample "cmd" and put the implementation CommandExecutor..

    2.create a main class and place it in onEnabled method ()
    Code:java
    1. public void onEnabled(){
    2. getCommand("x1").setExecutor(new cmd());
    3.  
    4. }


    or

    Code:java
    1. cmd comand = new cmd();
    2.  
    3. public void onEnabled(){
    4. getCommand("x1").setExecutor(comand);
    5. }
     
  22. Offline

    gabe4356

    ...I forgot to put solved on this a long time ago.
     
Thread Status:
Not open for further replies.

Share This Page