Class per command

Discussion in 'Plugin Development' started by Minermax7, Apr 6, 2014.

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

    Minermax7

    Hey guys, I am making a plugin with many different commands and I tried making a command per class. But it doesn't work. I type the command in game thats inside its own class and nothing happens, no lines pop up or anything, and the console shoes no errors.
    Here's my class code:
    Code:java
    1. public class CommandCC extends Main {
    2.  
    3. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] Args) {
    4. if(sender instanceof Player) {
    5. Player player = (Player) sender;
    6.  
    7. // PLUGIN PREFIX
    8. String Pluginprefix = "[Prefix] ";
    9.  
    10. // START COMMANDS
    11.  
    12. // CC
    13. if(cmd.getName().equalsIgnoreCase("cc")) {
    14. code code code;
    15. }
    16. Bukkit.broadcastMessage(Pluginprefix + "Chats cleared!");
    17. }
    18. }
    19. return false;
    20. }
    21. }

    I have the same public boolean in my main class (Main.java) and yeah, I was wondering if anyone could shed some light on this problem, thanks.
     
  2. Offline

    Mr360zack

    Ok, so you have your class main. Great. Now, let's say you had another class, called oranges. In our oranges, we would make it say this: public class oranges implements CommandExecutor {
    Then do your ONCOMMAND method here.

    Now, go back to your main class and put this:
    getCommand("oranges").setExecutor(new oranges());

    and add the command to your plugin.yml
     
  3. Offline

    FlareLine

    Mr360zack This is incorrect
    The class listening for commands should implement CommandExecutor and not Listener
     
  4. Offline

    Mr360zack

    Crap I meant that :mad: it's 2AM ok :(
     
  5. Offline

    FlareLine

  6. Offline

    Minermax7

    Mr360zack FlareLine Ok, I added
    getCommand("CommandCC").setExecutor(new CommandCC());
    to my Main.java class. (By the way, my other /cc class is CommandCC. And these are underlined in red:
    getCommand("CommandCC").setExecutor(new CommandCC());
    The getCommand error is: "Return type for the method is missing".
     
  7. Offline

    FlareLine

    Please post both of your classes :)
     
  8. Offline

    Mr360zack

    You need to put the getCommand inside of the onEnable
    Also getCommand("THIS") - this = the name of the command, not the class name.
     
  9. Offline

    Minermax7

    FlareLine
    Code:java
    1. package net.minermax7.manycommands;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8.  
    9. public class CommandCC extends Main implements CommandExecutor{
    10.  
    11. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] Args) {
    12. if(sender instanceof Player) {
    13. Player player = (Player) sender;
    14.  
    15. // PLUGIN PREFIX [ManyCommands]
    16. String Pluginprefix = "§3§l[§2§lMany§2§lCommands§3§l] ";
    17.  
    18. // START COMMANDS
    19.  
    20. // CC
    21. if(cmd.getName().equalsIgnoreCase("cc")) {
    22. code code code;
    23. }
    24. }
    25. return false;
    26. }
    27. }

    Main.java class:
    Code:java
    1. package net.minermax7.manycommands;
    2.  
    3. import java.util.Arrays;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.GameMode;
    8. import org.bukkit.Location;
    9. import org.bukkit.Material;
    10. //import org.bukkit.Sound;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Arrow;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.player.PlayerInteractEvent;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.inventory.meta.ItemMeta;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21. import org.bukkit.potion.PotionEffect;
    22.  
    23. //TODO
    24. /* kickall
    25. * mute
    26. * godmode
    27. * unban
    28. * ****ADD IF ARGS == 1! (/heal [name]) (/msg [name] message)
    29. *
    30. *
    31. *
    32. *
    33. */
    34. public class Main extends JavaPlugin implements Listener {
    35. public void onEnable() {
    36. getServer().getPluginManager().registerEvents(this, this);
    37. getLogger().info("ManyCommands has been enabled!");
    38. }
    39.  
    40. public void onDisable() {
    41. getLogger().info("ManyCommands has been disabled.");
    42. }
    43.  
    44. private Location spawn = null;
    45.  
    46. //CLASS REFERENCES
    47. getCommand("CommandCC").setExecutor(new CommandCC());
    48.  
    49. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] Args) {
    50. if(sender instanceof Player) {
    51. Player player = (Player) sender;


    Many more command down but doubt they will be needed.
     
  10. Offline

    coasterman10

    That needs to go in onEnable().
     
  11. Offline

    Minermax7

    I have changed it and it's in onEnable(). When I enter the command nothing happens... And the server gets an error when I load the plugin up.
     
  12. Offline

    MexMaster

    You set the command in your plugin.yml?
     
  13. Offline

    Minermax7

  14. Offline

    MexMaster

    Then show us your error :)
     
  15. Offline

    Minermax7

  16. Offline

    MexMaster

    do you have the file 2 time in your plugins folder? like ManyCommands(1).jar and ManyCommands.jar

    also i don't think this would be the problem but try it

    //edit:

    i tried to install a plugin 2 times and it worked... no error

    Try installing another bukkit build

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

    Zethariel

    You cannot extend Main in your command class, because it extends JavaPlugins, and thus tries to create another Main-class Plugin. That's the error right there (I know since I did a silly mistake like that as well)
     
    Benlewis9000 and Minermax7 like this.
  18. Offline

    MexMaster

    last time i read this, i was not sure why he did this but then i ignored it....
    but good to know
     
    Minermax7 likes this.
  19. Offline

    FlareLine

    Zethariel What exactly does extending your main class with Main result in?
     
  20. Offline

    xTigerRebornx

  21. Offline

    FlareLine

Thread Status:
Not open for further replies.

Share This Page