Can't use this command

Discussion in 'Plugin Development' started by CookCreeperz, Mar 17, 2013.

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

    CookCreeperz

    Hello, This command is not being executed correctly. The command "buy" above it works perfectly fine, along with all the others, but the "givemoney" command does not.
    Code:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            final Player player = (Player) sender;
            if (commandLabel.equals("sethome")) {
                Location loc = player.getLocation();
                getConfig().set(player.getName() + " homex", loc.getX());
                getConfig().set(player.getName() + " homey", loc.getY());
                getConfig().set(player.getName() + " homez", loc.getZ());
                saveConfig();
                player.sendMessage(ChatColor.BLUE + "Home has been set!");
            } else if (commandLabel.equals("home")) {
                if (getConfig().contains(player.getName() + " homex")) {
                    double x = getConfig().getDouble(player.getName() + " homex");
                    double y = getConfig().getDouble(player.getName() + " homey");
                    double z = getConfig().getDouble(player.getName() + " homez");
                    Location loc = new Location(player.getWorld(), x, y, z);
                    player.teleport(loc);
                    player.sendMessage(ChatColor.BLUE
                            + "Successfully teleported home!");
                } else {
                    player.sendMessage(ChatColor.RED + "Set a home first!");
                }
            } else if (commandLabel.equals("tpa")) {
                if (args.length == 0) {
                    player.sendMessage(ChatColor.DARK_RED
                            + "To Teleport To Someone Type: /tpa <playername>");
                } else if (args.length == 1) {
                    Player target = Bukkit.getPlayer(args[0]);
                    target.sendMessage(ChatColor.BLUE
                            + player.getName()
                            + " wants to teleport to you. Type /tpaccept if you want him to tp to you.");
                    player.sendMessage(ChatColor.BLUE + "Teleport request sent.");
                    teleport.put(target.getName(), player.getName());
                }
            } else if (commandLabel.equals("tpaccept")) {
                String callerName = teleport.get(player.getName());
                if (callerName == null) {
                } else {
                    Player caller = Bukkit.getPlayerExact(callerName);
                    if (caller == null) {
                    } else {
                        caller.teleport(player.getLocation());
                        caller.sendMessage(ChatColor.BLUE + "Teleported to "
                                + player.getName());
                    }
                    teleport.remove(player.getName());
                }
            } else if (commandLabel.equals("setspawn") && player.isOp()) {
                Location loc = player.getLocation();
                getConfig().set("spawnx", loc.getX());
                getConfig().set("spawny", loc.getY());
                getConfig().set("spawnz", loc.getZ());
                saveConfig();
                player.sendMessage(ChatColor.DARK_RED + "Spawn set.");
            } else if (commandLabel.equals("spawn")) {
                    double x = getConfig().getDouble("spawnx");
                    double y = getConfig().getDouble("spawny");
                    double z = getConfig().getDouble("spawnz");
                    Location loc = new Location(player.getWorld(), x, y, z);
                    player.teleport(loc);
                    player.sendMessage(ChatColor.DARK_RED + "Teleported to spawn.");
            } else if (commandLabel.equals("setrank")
                    && c.getCustomConfig().get(player.getName() + " Class")
                            .equals("admin")) {
                if (args.length == 0) {
                    player.sendMessage(ChatColor.RED + "Please Select A Player!");
                    player.sendMessage(ChatColor.BLUE + "Ranks:"
                            + ChatColor.DARK_RED + "user, mod, admin");
                } else if (args.length >= 2) {
                    Player target = Bukkit.getPlayer(args[0]);
                    c.getCustomConfig().set(target.getName() + " Class", args[1]);
                    c.saveCustomConfig();
                }
            } else if (commandLabel.equals("buy")) {
                if (args.length == 0) {
                    player.sendMessage(ChatColor.RED
                            + "Usage: /buy <item i> <quantity>");
                } else if (args.length >= 2
                        && c.getCustomConfig().getInt(player.getName() + " Money") >= 1000) {
                    int q = Integer.parseInt(args[1]);
                    int id = Integer.parseInt(args[0]);
                    if (q > 64
                            || c.getCustomConfig().getInt(
                                    player.getName() + " Money") < 1000) {
                        player.sendMessage(ChatColor.RED
                                + "You cannot purchase more than 64 blocks, or you don't have enough money!");
                    } else {
                        player.getInventory().addItem(
                                new ItemStack(Material.getMaterial(id), q));
                        c.getCustomConfig().set(
                                player.getName() + " Money",
                                c.getCustomConfig().getInt(
                                        player.getName() + " Money") - 1000);
                        player.sendMessage(ChatColor.BLUE
                                + "Purchase successful! You bought: " + "" + q + id);
                        c.saveCustomConfig();
                    }
                } else if (commandLabel.equals("givemoney")) {
                    if (args.length == 0) {
                        player.sendMessage(ChatColor.DARK_GREEN
                                + "Please specifiy the following: /givemoney <player name> <amount>");
                    } else if (args.length >= 2) {
                        Player t = Bukkit.getPlayer(args[0]);
                        int amt = c.getCustomConfig().getInt(
                                player.getName() + " Money");
                        int speed;
                        speed = Integer.parseInt(args[1]);
                        if (amt < speed) {
                            player.sendMessage(ChatColor.RED
                                    + "You can't send that much!");
                        } else if (amt >= speed) {
                            player.sendMessage(ChatColor.BLUE
                                    + "Transaction successful!");
                            c.getCustomConfig().set(
                                    t.getName() + " Money",
                                    c.getCustomConfig().getInt(
                                            t.getName() + " Money")
                                            + speed);
                            t.sendMessage(ChatColor.BLUE + player.getName()
                                    + " sent you " + speed);
                            c.getCustomConfig().set(
                                    player.getName() + " Money",
                                    c.getCustomConfig().getInt(
                                            player.getName() + " Money")
                                            - speed);
                            c.saveCustomConfig();
                        }
                    }
                }
            }
            return false;
        }
    }
    Thanks for the help,
    CookCreeperz
     
  2. Offline

    xmarinusx

    CookCreeperz
    Can you tell what doesn't work about the givemoney command? Have you added it to your plugin.yml? Are you getting any errors?
     
  3. Offline

    Murderscene

    Make sure it is in your plugin.yml.
    Also that the executer is set on the onEnable() method of your plugin:
    example:
    getCommand("givemoney").setExecutor(new MainCommandExecuter(this));

    Edit: On second look it looks like your 'givemoney' command is in the 'else if' of 'buy'. So it's first checking if the command equals 'buy' and THEN if it equals 'givemoney'. Which will never happen. You need to add one more '}'
     
  4. Offline

    CookCreeperz

    I fixed it thanks guys.

    Code:
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            final Player player = (Player) sender;
            if (commandLabel.equals("sethome")) {
                Location loc = player.getLocation();
                getConfig().set(player.getName() + " homex", loc.getX());
                getConfig().set(player.getName() + " homey", loc.getY());
                getConfig().set(player.getName() + " homez", loc.getZ());
                saveConfig();
                player.sendMessage(ChatColor.BLUE + "Home has been set!");
            } else if (commandLabel.equals("home")) {
                if (getConfig().contains(player.getName() + " homex")) {
                    double x = getConfig().getDouble(player.getName() + " homex");
                    double y = getConfig().getDouble(player.getName() + " homey");
                    double z = getConfig().getDouble(player.getName() + " homez");
                    Location loc = new Location(player.getWorld(), x, y, z);
                    player.teleport(loc);
                    player.sendMessage(ChatColor.BLUE
                            + "Successfully teleported home!");
                } else {
                    player.sendMessage(ChatColor.RED + "Set a home first!");
                }
            } else if (commandLabel.equals("tpa")) {
                if (args.length == 0) {
                    player.sendMessage(ChatColor.DARK_RED
                            + "To Teleport To Someone Type: /tpa <playername>");
                } else if (args.length == 1) {
                    Player target = Bukkit.getPlayer(args[0]);
                    target.sendMessage(ChatColor.BLUE
                            + player.getName()
                            + " wants to teleport to you. Type /tpaccept if you want him to tp to you.");
                    player.sendMessage(ChatColor.BLUE + "Teleport request sent.");
                    teleport.put(target.getName(), player.getName());
                }
            } else if (commandLabel.equals("tpaccept")) {
                String callerName = teleport.get(player.getName());
                if (callerName == null) {
                } else {
                    Player caller = Bukkit.getPlayerExact(callerName);
                    if (caller == null) {
                    } else {
                        caller.teleport(player.getLocation());
                        caller.sendMessage(ChatColor.BLUE + "Teleported to "
                                + player.getName());
                    }
                    teleport.remove(player.getName());
                }
            } else if (commandLabel.equals("setspawn") && player.isOp()) {
                Location loc = player.getLocation();
                getConfig().set("spawnx", loc.getX());
                getConfig().set("spawny", loc.getY());
                getConfig().set("spawnz", loc.getZ());
                saveConfig();
                player.sendMessage(ChatColor.DARK_RED + "Spawn set.");
            } else if (commandLabel.equals("spawn")) {
                double x = getConfig().getDouble("spawnx");
                double y = getConfig().getDouble("spawny");
                double z = getConfig().getDouble("spawnz");
                Location loc = new Location(player.getWorld(), x, y, z);
                player.teleport(loc);
                player.sendMessage(ChatColor.DARK_RED + "Teleported to spawn.");
            } else if (commandLabel.equals("setrank")
                    && c.getCustomConfig().get(player.getName() + " Class")
                            .equals("admin")) {
                if (args.length == 0) {
                    player.sendMessage(ChatColor.RED + "Please Select A Player!");
                    player.sendMessage(ChatColor.BLUE + "Ranks:"
                            + ChatColor.DARK_RED + "user, mod, admin");
                } else if (args.length >= 2) {
                    Player target = Bukkit.getPlayer(args[0]);
                    c.getCustomConfig().set(target.getName() + " Class", args[1]);
                    c.saveCustomConfig();
                }
            } else if (commandLabel.equals("buy")) {
                if (args.length == 0) {
                    player.sendMessage(ChatColor.RED
                            + "Usage: /buy <item i> <quantity>");
                } else if (args.length >= 2
                        && c.getCustomConfig().getInt(player.getName() + " Money") >= 1000) {
                    int q = Integer.parseInt(args[1]);
                    int id = Integer.parseInt(args[0]);
                    if (q > 64
                            || c.getCustomConfig().getInt(
                                    player.getName() + " Money") < 1000) {
                        player.sendMessage(ChatColor.RED
                                + "You cannot purchase more than 64 blocks, or you don't have enough money!");
                    } else {
                        player.getInventory().addItem(
                                new ItemStack(Material.getMaterial(id), q));
                        c.getCustomConfig().set(
                                player.getName() + " Money",
                                c.getCustomConfig().getInt(
                                        player.getName() + " Money") - 1000);
                        player.sendMessage(ChatColor.BLUE
                                + "Purchase successful! You bought: " + "" + q + id);
                        c.saveCustomConfig();
                    }
                }
            } else if (commandLabel.equals("givemoney")) {
                if (args.length == 0) {
                    player.sendMessage(ChatColor.DARK_GREEN
                            + "Please specifiy the following: /givemoney <player name> <amount>");
                } else if (args.length >= 2) {
                    Player t = Bukkit.getPlayer(args[0]);
                    int amt = c.getCustomConfig().getInt(
                            player.getName() + " Money");
                    int speed;
                    speed = Integer.parseInt(args[1]);
                    if (amt < speed) {
                        player.sendMessage(ChatColor.RED
                                + "You can't send that much!");
                    } else if (amt >= speed) {
                        player.sendMessage(ChatColor.BLUE
                                + "Transaction successful!");
                        c.getCustomConfig().set(
                                t.getName() + " Money",
                                c.getCustomConfig().getInt(t.getName() + " Money")
                                        + speed);
                        t.sendMessage(ChatColor.BLUE + player.getName()
                                + " sent you " + speed);
                        c.getCustomConfig().set(
                                player.getName() + " Money",
                                c.getCustomConfig().getInt(
                                        player.getName() + " Money")
                                        - speed);
                        c.saveCustomConfig();
                    }
                } else if (commandLabel.equals("sell")) {
                    if (args.length == 0) {
                        player.sendMessage(ChatColor.RED
                                + "Usage: /sell <item i> <quantity>");
                    } else if (args.length >= 2) {
                        int q = Integer.parseInt(args[1]);
                        int id = Integer.parseInt(args[0]);
                        if (q > 64) {
                            player.sendMessage(ChatColor.RED
                                    + "You cannot sell more than 64 at a time.");
                        } else {
     
                            player.getInventory().remove(
                                    new ItemStack(Material.getMaterial(id), q));
                            c.getCustomConfig().set(
                                    player.getName() + " Money",
                                    c.getCustomConfig().getInt(
                                            player.getName() + " Money") + 500);
                            player.sendMessage(ChatColor.DARK_GREEN
                                    + "Purchase successful! You sold: " + "" + q
                                    + "" + id);
                            c.saveCustomConfig();
                        }
                    }
                } else if (commandLabel.equals("balance")) {
                    player.sendMessage(ChatColor.RED
                            + "You have "
                            + c.getCustomConfig().getInt(
                                    player.getName() + " Money") + " Dollars");
     
                }
            }
            return false;
        }
    }
    Ok, so now the sell or balance commands arent working, and I'm sure it's because of the brackets.

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

    Murderscene


    Add one more "}" before:
    Code:
    } else if (commandLabel.equals("sell")) {
     
  6. Offline

    CookCreeperz

    Thanks fixed, also
    This sell command isn't removing the items correctly, you know anything about that?
    Code:
    Code:
            } else if (commandLabel.equals("sell")) {
                if (args.length == 0) {
                    player.sendMessage(ChatColor.RED
                            + "Usage: /sell <item i> <quantity>");
                } else if (args.length >= 2) {
                    int q = Integer.parseInt(args[1]);
                    int id = Integer.parseInt(args[0]);
                    if (q > 64) {
                        player.sendMessage(ChatColor.RED
                                + "You cannot sell more than 64 at a time.");
                    } else if(!player.getInventory().contains(id, q)) {
                        player.sendMessage(ChatColor.RED + "You don't have those items!");
                    }else{             
                        c.getCustomConfig().set(
                                player.getName() + " Money",
                                c.getCustomConfig().getInt(
                                        player.getName() + " Money") + 500);
                        player.sendMessage(ChatColor.DARK_GREEN
                                + "Purchase successful! You sold: " + "" + q + ""
                                + id);
                        player.updateInventory();
                        c.saveCustomConfig();
                    }
                }

    EDIT*** FIXED.
     
  7. Offline

    xmarinusx

    CookCreeperz
    Please tell us how you fixed it, maybe do others have the same problem and want to know how to fix this as well.
    Also if everything is working put it to "solved".
     
  8. Offline

    CookCreeperz

    I added more brackets
     
  9. Offline

    the_merciless

    So it had nothing to do with the fact this code has nothing in it to remove any items? Your sending a message then updating the inventory without actually removing any items
     
  10. Offline

    CookCreeperz

    Code:
            } else if (commandLabel.equals("sell")) {
                if (args.length == 0) {
                    player.sendMessage(ChatColor.RED
                            + "Usage: /sell <item i> <quantity>");
                } else if (args.length >= 2) {
                    int q = Integer.parseInt(args[1]);
                    int id = Integer.parseInt(args[0]);
                    if (q > 64) {
                        player.sendMessage(ChatColor.RED
                                + "You cannot sell more than 64 at a time.");
                    } else if (!player.getInventory().contains(id, q)) {
                        player.sendMessage(ChatColor.RED
                                + "You don't have those items!");
                    } else {
                        c.getCustomConfig().set(
                                player.getName() + " Money",
                                c.getCustomConfig().getInt(
                                        player.getName() + " Money") + 500);
                        player.sendMessage(ChatColor.DARK_GREEN
                                + "Purchase successful! You sold: " + "" + q + ""
                                + id);
                     //Removes the items   player.getInventory().remove(new ItemStack(id, q));
                        player.updateInventory();
                        c.saveCustomConfig();
                    }
                }
     
  11. Offline

    the_merciless

    That's not what you originaly posted though is it. Dont worry im just trolling.
     
  12. Offline

    CookCreeperz

    LOL no it isn't I just asked the forums for help.
     
Thread Status:
Not open for further replies.

Share This Page