Wont let me add a command to the main class?

Discussion in 'Plugin Development' started by Niknea, Dec 26, 2013.

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

    Niknea

    Hey everyone, so when I type this method
    Code:java
    1. getCommand("gdeagles").setExecutor(new gDeagles());
    I get an error at "setExecutor"
    Here is my gDeagles class
    Code:java
    1. package me.niknea.EnhancedGuns;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.entity.Snowball;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.Action;
    15. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.inventory.meta.ItemMeta;
    19.  
    20. public class gDeagles implements Listener, CommandExecutor
    21. {
    22. @EventHandler
    23. public void onPlayerInteract (PlayerInteractEvent event)
    24. {
    25. Player player = event.getPlayer();
    26. if(player.getItemInHand().getType() == Material.GOLD_SPADE)
    27. {
    28. if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
    29. {
    30. player.launchProjectile(Snowball.class);
    31. }
    32. }
    33. }
    34. @SuppressWarnings("deprecation")
    35. @EventHandler
    36. public void onEntityDamage(EntityDamageByEntityEvent event) {
    37. if (event.getDamager() instanceof Snowball)
    38. {
    39. Snowball e = (Snowball) event.getDamager();
    40. if (e.getShooter() instanceof Player)
    41. {
    42. Player shooter = (Player) e.getShooter();
    43. if (shooter.getItemInHand().getType() == Material.GOLD_SPADE)
    44. {
    45. event.setDamage(4);
    46. }
    47. }
    48. }
    49. }
    50. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    51. {
    52. Player player = (Player) sender;
    53. if(cmd.getName().equalsIgnoreCase("gdeagles"))
    54. {
    55. ItemStack gspade = new ItemStack(Material.GOLD_SPADE);
    56. ItemMeta ak47meta = gspade.getItemMeta();
    57. ak47meta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Golden Deagles");
    58. ArrayList<String> ak47lore = new ArrayList<String>();
    59. ak47lore.add(ChatColor.DARK_PURPLE + "This weapon deals 2 hearts per shot.");
    60. ak47meta.setLore(ak47lore);
    61. gspade.setItemMeta(ak47meta);
    62. player.getInventory().addItem(gspade);
    63. player.sendMessage(ChatColor.GOLD + "Here is your" + ChatColor.GREEN + "Golden Deagles" + ChatColor.GOLD + "!");
    64. }
    65. return true;
    66. }
    67. }

    Thanks again,
    Niknea
     
  2. Offline

    Maulss

    Not sure if it helps, but I think you should put @Override ontop of your onCommand() method
     
  3. Offline

    Niknea

    Maulss Nope doesn't work.
     
  4. Offline

    Maulss

    Niknea
    Where do you get this error, in your software (Compilation error) or when you actually enable the plugin?
    If it's the compilation error, what error does it give you? Otherwise, what exception do you get in the console?

    I've tested the code myself and it all works fine. It might be a silly thing to think about, but is there a chance that you have this:
    getCommand("gdeagles").setExecutor(new gDeagles());
    Set up outside of onEnable()?
     
  5. Offline

    Niknea

    Maulss Nope
    Here is my main class
    Code:java
    1. package me.niknea.EnhancedGuns;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.plugin.Plugin;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8.  
    9. public class Main extends JavaPlugin
    10. {
    11.  
    12. private static Plugin plugin;
    13. @Override
    14. public void onEnable() {
    15. plugin = this;
    16. registerEvents(this, new Ak47(), new myCustomInventory(), new gDeagles());
    17. getCommand("ak47").setExecutor(new Ak47());
    18. getCommand("guns").setExecutor(new oMenu());
    19. getCommand("gdeagles").setExecutor(new gDeagles());
    20. getLogger().info("Enabling Enhanced Guns! Created by Niknea.");
    21.  
    22. }
    23. @Override
    24. public void onDisable() {
    25. getLogger().info("Disabling Enhanced Guns! Created by Niknea.");
    26. }
    27.  
    28. //Much easier then registering events in 10 different methods
    29. public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
    30. for (Listener listener : listeners) {
    31. Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
    32. }
    33. }
    34.  
    35. //To access the plugin variable from other classes
    36. public static Plugin getPlugin() {
    37. return plugin;
    38. }
    39.  
    40.  
    41.  
    42.  
    43. }
    44.  
    45.  

    And here is the error I got on
    Code:
    setExecutor
    .
    "The method setExecutor in the type PluginCommand is not applicable for the arguments (gDeagles)"

    Also my other two commands that I added to the main class work, it's just the gDeagle one that dosent.
     
  6. Offline

    Maulss

    Niknea
    Can I see another one of your CommandExecutor classes?
     
  7. Offline

    Niknea

    Maulss
    Code:java
    1. package me.niknea.EnhancedGuns;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.entity.Snowball;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.Action;
    15. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.inventory.meta.ItemMeta;
    19.  
    20. public class Ak47 implements Listener, CommandExecutor
    21. {
    22.  
    23. @EventHandler
    24. public void onPlayerInteract (PlayerInteractEvent event)
    25. {
    26. Player player = event.getPlayer();
    27. if(player.getItemInHand().getType() == Material.WOOD_SPADE)
    28. {
    29. if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
    30. {
    31. player.launchProjectile(Snowball.class);
    32.  
    33. }
    34. }
    35. }
    36. @SuppressWarnings("deprecation")
    37. @EventHandler
    38. public void onEntityDamage(EntityDamageByEntityEvent event) {
    39. if (event.getDamager() instanceof Snowball)
    40. {
    41. Snowball s = (Snowball) event.getDamager();
    42. if (s.getShooter() instanceof Player)
    43. {
    44. Player shooter = (Player) s.getShooter();
    45. if (shooter.getItemInHand().getType() == Material.WOOD_SPADE)
    46. {
    47. event.setDamage(3);
    48. }
    49. }
    50. }
    51. }
    52.  
    53. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    54. {
    55. Player player = (Player) sender;
    56. if(cmd.getName().equalsIgnoreCase("ak47"))
    57. {
    58. ItemStack wspade = new ItemStack(Material.WOOD_SPADE);
    59. ItemMeta ak47meta = wspade.getItemMeta();
    60. ak47meta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Ak47");
    61. ArrayList<String> ak47lore = new ArrayList<String>();
    62. ak47lore.add(ChatColor.DARK_PURPLE + "This weapon deals 1.5 hearts per shot.");
    63. ak47meta.setLore(ak47lore);
    64. wspade.setItemMeta(ak47meta);
    65. player.getInventory().addItem(wspade);
    66. player.sendMessage(ChatColor.GOLD + "Here is your" + ChatColor.GREEN + "Ak47" + ChatColor.GOLD + "!");
    67.  
    68.  
    69.  
    70.  
    71. }
    72.  
    73.  
    74. return true;
    75. }
    76. }
    77.  
     
  8. Offline

    Maulss

    Niknea
    Hmm, both work fine for me:
    [​IMG]
    Make sure there isn't another class called gDeagles that is overriding that one.

    EDIT: Just realized, that error pops up when you don't implement CommandExecutor in your Executor class. You have it implemented, which means there is a more than likely chance that there is another class called gDeagles in your plugin that is interfering with the other one (Which doesn't implement CommandExecutor).
     
  9. Offline

    jimbo8

    I usually write getCommand ("blahblah").setExecutor(new blubub, this);

    Try that.

    [EDIT]

    Sorry, i need to learn how to read the whole posts before posting :p
     
  10. Offline

    Niknea

    Maulss Nope, no other class named gDeagles

    Maulss I renamed the class to Deagles and I don't get an error, however now the command dosen't work. Any ideas?

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

    Maulss

    Niknea
    Renaming the class to Deagles actually proves you had something overriding it.
    Nevertheless, do you get any stacktraces or some kind of debugging messages when you try to execute the command?
     
  12. Offline

    Niknea

    Maulss Nope, just says it's an unknown command
     
  13. Offline

    Maulss

    Niknea
    Perhaps you forgot to add it to the plugin.yml?
     
  14. Offline

    Niknea

  15. Offline

    Maulss

    Niknea
    I've never played around with commands in separate classes, so I'm not positive your method should work. Try executing all commands from the same CommandExecutor class. Don't forget to change the executors in the main class.
     
  16. Offline

    Niknea

    Maulss
    Still dosen't work here is my new class I added the command.
    Code:java
    1. package me.niknea.EnhancedGuns;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.entity.Snowball;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.block.Action;
    15. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.inventory.meta.ItemMeta;
    19.  
    20. public class Ak47 implements Listener, CommandExecutor
    21. {
    22.  
    23. @EventHandler
    24. public void onPlayerInteract (PlayerInteractEvent event)
    25. {
    26. Player player = event.getPlayer();
    27. if(player.getItemInHand().getType() == Material.WOOD_SPADE)
    28. {
    29. if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
    30. {
    31. player.launchProjectile(Snowball.class);
    32.  
    33. }
    34. }
    35. }
    36. @SuppressWarnings("deprecation")
    37. @EventHandler
    38. public void onEntityDamage(EntityDamageByEntityEvent event) {
    39. if (event.getDamager() instanceof Snowball)
    40. {
    41. Snowball s = (Snowball) event.getDamager();
    42. if (s.getShooter() instanceof Player)
    43. {
    44. Player shooter = (Player) s.getShooter();
    45. if (shooter.getItemInHand().getType() == Material.WOOD_SPADE)
    46. {
    47. event.setDamage(3);
    48. }
    49. }
    50. }
    51. }
    52.  
    53. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    54. {
    55. Player player = (Player) sender;
    56. if(cmd.getName().equalsIgnoreCase("ak47"))
    57. {
    58. ItemStack wspade = new ItemStack(Material.WOOD_SPADE);
    59. ItemMeta ak47meta = wspade.getItemMeta();
    60. ak47meta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Ak47");
    61. ArrayList<String> ak47lore = new ArrayList<String>();
    62. ak47lore.add(ChatColor.DARK_PURPLE + "This weapon deals 1.5 hearts per shot.");
    63. ak47meta.setLore(ak47lore);
    64. wspade.setItemMeta(ak47meta);
    65. player.getInventory().addItem(wspade);
    66. player.sendMessage(ChatColor.GOLD + "Here is your" + ChatColor.GREEN + "Ak47" + ChatColor.GOLD + "!");
    67.  
    68.  
    69.  
    70.  
    71. }
    72. else if(cmd.getName().equalsIgnoreCase("gdeagles"))
    73. {
    74. Player player1 = (Player) sender;
    75. ItemStack gspade = new ItemStack(Material.GOLD_SPADE);
    76. ItemMeta ak47meta = gspade.getItemMeta();
    77. ak47meta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Golden Deagles");
    78. ArrayList<String> ak47lore = new ArrayList<String>();
    79. ak47lore.add(ChatColor.DARK_PURPLE + "This weapon deals 2 hearts per shot.");
    80. ak47meta.setLore(ak47lore);
    81. gspade.setItemMeta(ak47meta);
    82. player1.getInventory().addItem(gspade);
    83. player1.sendMessage(ChatColor.GOLD + "Here is your" + ChatColor.GREEN + "Golden Deagles" + ChatColor.GOLD + "!");
    84.  
    85. return true;
    86. }
    87. return true;
    88. }
    89. }
    90.  
     
  17. Offline

    Maulss

    Niknea
    Do all other commands work?
     
  18. Offline

    Niknea

  19. Offline

    Maulss

    Niknea
    Can I see your onEnable() and plugin.yml please?
     
  20. Offline

    Niknea

    Maulss
    Code:java
    1. @Override
    2. public void onEnable() {
    3. plugin = this;
    4. registerEvents(this, new Ak47(), new myCustomInventory(), new Deagles());
    5. getCommand("ak47").setExecutor(new Ak47());
    6. getCommand("guns").setExecutor(new oMenu());
    7. getCommand("gdeagles").setExecutor(new Ak47());
    8. getLogger().info("Enabling Enhanced Guns! Created by Niknea.");
    9.  
    10. }

    Plugin.yml
    Code:
    name: Enhanced Guns
    version: 1.0
    main: me.niknea.EnhancedGuns.Main
    description: Enhanced Guns!
     
    commands:
      ak47:
          description: This command gives you the Ak47!
      guns:
          description: This command displays a menu with all the guns!
      gdeagles:
          description: This command gives you the Golden Deagles!
     
    
     
  21. Offline

    Maulss

    Niknea
    Thanks. Can I also see your server log with you attempting all of the commands?
     
  22. Offline

    Wehttam664

    Not sure if this has anything to do with it, but don't issues arise from multiple implements?
    Out of curiosity, try swapping the implementations or removing Listener completely and see what it does.
     
  23. Offline

    Niknea

    Maulss It won't be useful, it just says unknown command.

    Wehttam664 That won't work as the ak47 class implements both yet everything works, besides the gdeagles.

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

    Maulss

    This error is pretty interesting...
    Out of curiosity, try changing the label of the command. If it still doesn't work, try making that command a second argument of another one. This way we should ease in on the actual error
     
  25. Offline

    Wehttam664

    For debugging purposes, try creating the new gDeagles class as a variable, checking if that variable is an instance of CommandExecutor; printing something if false or calling the setExecutor if true. Can't hurt to try.
     
  26. Offline

    Niknea

    Maulss How would I try #2?

    Wehttam664 Didn't work :(

    Maulss I even made a new command and didn't work :( Any ideas?

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

    AoH_Ruthless

    Niknea
    Repaste the class that isn't working
     
  28. Offline

    Niknea

    AoH_Ruthless
    Code:java
    1. package me.niknea.EnhancedGuns;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.inventory.meta.ItemMeta;
    13.  
    14. public class GoldenGet implements CommandExecutor
    15. {
    16. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    17. {
    18. Player player = (Player) sender;
    19. if(cmd.getName().equalsIgnoreCase("gdeagles"))
    20. {
    21. ItemStack gspade = new ItemStack(Material.GOLD_SPADE);
    22. ItemMeta gmeta = gspade.getItemMeta();
    23. gmeta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Golden Deagles");
    24. ArrayList<String> glore = new ArrayList<String>();
    25. glore.add(ChatColor.DARK_PURPLE + "This weapon deals 2 hearts per shot.");
    26. gmeta.setLore(glore);
    27. gspade.setItemMeta(gmeta);
    28. player.getInventory().addItem(gspade);
    29. player.sendMessage(ChatColor.GOLD + "Here is your" + ChatColor.GREEN + "Golden Deagles" + ChatColor.GOLD + "!");
    30.  
    31. }
    32. return true;
    33. }
    34. }
     
  29. Offline

    AoH_Ruthless

    Niknea
    You should be returning false on line 32 .., and add a return true after line 29.
    If you're doing similar things on other commands, fix them.
     
  30. Offline

    Niknea

Thread Status:
Not open for further replies.

Share This Page