My command is not returning.

Discussion in 'Plugin Development' started by ShadowWizardMC, Mar 3, 2014.

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

    ShadowWizardMC

    Well back with another problem for some reason my command is not returning the way its suppose to. I am making a custom currency plugin and i setup command so i can give and take my tokens.
    But when i run the command it does nothing. Not even an error here is my code. Also anyone know how i can make it so i can send and take custom amounts? Example /sendcoins <amount> and it sends the amount or /takecoins <amount> so that it takes the amout of coins/tokens?

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. Player p = (Player) sender;
    3. if(cmd.getName().equalsIgnoreCase("sendcoins")){
    4. MyAPI.givecoins(p, 100);
    5.  
    6.  
    7. }else{
    8. if(cmd.getName().equalsIgnoreCase("takecoins"));
    9. MyAPI.takecoins(p, 100);
    10.  
    11. return true;
    12. }
    13. return false;
    14. }


    Also included is my Api

    Code:java
    1. package me.ShadowWizard.VirtualShop;
    2.  
    3. import org.bukkit.entity.Player;
    4.  
    5. public class MyAPI{
    6.  
    7. public static void givecoins(Player p, int i) {
    8. Main.config.set(p.getName() + ".coins",
    9. Main.config.getInt(p.getName() + ".coins", 0) + i);
    10. Main.saveFile();
    11. p.sendMessage("§6$" + i + " §7coins received!");
    12. }
    13.  
    14. public static void takecoins(Player p, int i) {
    15. Main.config.set(p.getName() + ".coins",
    16. Main.config.getInt(p.getName() + ".coins", 0) - i);
    17. Main.saveFile();
    18. p.sendMessage("§6$" + i + " §7coins taken!");
    19. }
    20.  
    21. public static boolean hasEnough(Player p, int i) {
    22. if (Main.config.getInt(p.getName() + ".coins") >= i)
    23. return true;
    24. return false;
    25. }
    26. }
     
  2. Offline

    SuperOmegaCow

    ShadowWizardMC
    Are you doing the commands in a different class than the one that extends JavaPlugin?
    Proper command structure when doing the command (directly from my plugin):
    Code:java
    1. public class CommandCreateCube implements CommandExecutor {
    2.  
    3. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    4.  
    5. if (!(sender instanceof Player)) {
    6. return false;
    7. }
    8.  
    9. if (sender.isOp()) {
    10. if (args.length > 1 || args.length == 0) {
    11. sender.sendMessage(ChatColor.DARK_RED + "Usage: /createcube <map name>");
    12. return false;
    13. } else {
    14. String mapName = args[0];
    15. if (CubeGameManager.getInstance().getGame(mapName) != null) {
    16. sender.sendMessage(ChatColor.DARK_RED + "Cube already Exists");
    17. return false;
    18. } else {
    19. CubeGameManager.getInstance().createGame(mapName);
    20. sender.sendMessage(ChatColor.DARK_RED + "Created cube");
    21. return false;
    22. }
    23. }
    24. } else {
    25. sender.sendMessage(ChatColor.DARK_RED + "You are not an Administrator.");
    26. return false;
    27. }
    28. }
    29.  
    30. }
    31.  


    Then on the onEnable:
    Code:java
    1. getCommand("createcube").setExecutor(new CommandCreateCube());
     
  3. Offline

    ShadowWizardMC

  4. Offline

    SuperOmegaCow

    ShadowWizardMC
    Well let me explain how Commands work when using a different class. Firstly, you need to register the command and set the class that will receive the command. onEnable put: getCommand("YourCommand").setExecutor(new YourClass());
    So YourClass is now the executor of the command YourCommand. So when a player types /YourCommand it goes to that class, meaning the only command that it will receive is YourCommand. So checking if the command is YourCommand is pointless since it will always be.
    Now for multiple command use a different class for a different command.
     
  5. Offline

    tomudding

    Register your command like SuperOmegaCow said. And maybe you should use his code for the command
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. Player p = (Player) sender;
    3.  
    4. if(cmd.getName().equalsIgnoreCase("sendcoins")){
    5. MyAPI.givecoins(p, 100);
    6. return true;
    7. }else if(cmd.getName().equalsIgnoreCase("takecoins")); // Instead of using } else { if() { use } else if() {
    8. MyAPI.takecoins(p, 100);
    9. return true;
    10. }
    11. return false;
    12. }
     
  6. Offline

    SuperOmegaCow

    tomudding
    When you set a class as the executor, you can't have multiple commands with the same onCommand executor.
     
  7. Offline

    Rocoty

  8. Offline

    HungerCraftNL

    Add in your CommandExecutor class:
    PHP:
    private PLUGINCLASS plugin;
    public 
    THISCLASS(PLUGINCLASS plugin){
      
    this.plugin plugin;
    }
    Then register your command in the main class in the methode onEnable:
    PHP:
    getCommand("command").setExecutor(new CLASS(this));
     
  9. Offline

    SuperOmegaCow

    Rocoty
    When only setting one executor to that class it will only receive one command.
     
  10. Offline

    Rocoty

    SuperOmegaCow You can assign multiple commands to the same executor
     
  11. Offline

    ShadowWizardMC

Thread Status:
Not open for further replies.

Share This Page