Having trouble with command...

Discussion in 'Plugin Development' started by vhbob, Feb 3, 2016.

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

    vhbob

    Hey, with this command it is supposed to take a command like : /give coins (player) (amount) but it does not work... and ideas? maybe the args are wrong???
    Command:
    Code:
    if (cmd.getName().equalsIgnoreCase("Give")) {
                    if (args.length == 3) {
                        if (args[1].equalsIgnoreCase("Coins")) {
                            if (p.isOp()) {
                                String reciver = args[2];
                                if (Bukkit.getOnlinePlayers().contains(reciver)) {
                                    Player r = (Player) Bukkit.getOfflinePlayer(reciver);
                                    giveCoins(r, Integer.parseInt(args[3]));           
                                } else {
                                    p.sendMessage(ChatColor.RED + "This player is offline!");
                                }
                            } else {
                                sender.sendMessage(ChatColor.RED + "You don't have the correct permissions!");
                            }
                        }
                    } else {
                        p.sendMessage(ChatColor.RED + "Usage: /give coins (player) (amount)");
                    }
                }
    Give coins:
    Code:
    public void giveCoins(Player p, int i) {
            int bal = getConfig().getInt(p.getName() + " Coins");
            getConfig().set(p.getName() + ".Coins", bal + i);
            saveConfig();
            p.sendMessage("You Have Earned " + ChatColor.GREEN + i + ChatColor.RESET + ".Coins");
        }
     
  2. Offline

    teej107

    What doesn't work about it?
     
  3. Offline

    567legodude

    @vhbob In java the index number normally starts at 0.
    coins is actually args[0] not args[1], the receiver is args[1] not args[2], etc....
     
  4. Offline

    vhbob

    @567legodude thanks ill try that. @teej107 the whole command does not work

    @567legodude now it works... but it wont recognize a player at all... @teej107
    Code:
    if (cmd.getName().equalsIgnoreCase("Give")) {
                    if (args.length == 3) {
                        if (args[0].equalsIgnoreCase("Coins")) {
                            if (p.isOp()) {
                                String reciver = args[1];
                                if (Bukkit.getOnlinePlayers().contains(reciver)) {
                                    Player r = (Player) Bukkit.getOfflinePlayer(reciver);
                                    giveCoins(r, Integer.parseInt(args[2]));
                                } else {
                                    p.sendMessage(ChatColor.RED + "This player is offline!");
                                }
                            } else {
                                sender.sendMessage(ChatColor.RED + "You don't have the correct permissions!");
                            }
                        }
                    } else {
                        p.sendMessage(ChatColor.RED + "Usage: /give coins (player) (amount)");
                    }
                }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 20, 2018
  5. Offline

    teej107

    That's still too broad. There are countless reasons to why anything "won't work". Please be more specific next time.
    @567legodude Most likely pointed out the problem.
     
  6. Offline

    WinX64

    This won't work. Instead, get the Player object from the server using the name you inputted, then check if it's null.

    Also, on the second piece of code you posted. This won't work as well, you're actually getting the value from one placed and saving it on a completely different one, check it again.
     
  7. Offline

    vhbob

    @WinX64 how would i solve this :\
     
  8. Offline

    WinX64

    For the first one, get the Player using the name you specified:
    Code:
    Bukkit.getPlayer(receiver)
    This will return the player with closest name, and null if none is online.

    For the second, you're getting your value from "<name> Coins" and saving it on "<name>.Coins"
     
  9. Offline

    vhbob

    @WinX64 when i use this it still cant find a player, and i am typing the name in correctly
    Code:
    String reciver = args[1];
                                if (Bukkit.getOnlinePlayers().contains(reciver)) {
                                    Player r = Bukkit.getPlayer(reciver);
     
  10. Offline

    Tecno_Wizard

    @vhbob, you have to fix that spacing...

    Using the online player list as a method of getting a player will not work. You must use Bukkit#getPlayer(String name) in this case. Also I am fairly sure it is case sensitive.
     
  11. Offline

    vhbob

  12. Offline

    Tecno_Wizard

    @vhbob OfflinePlayer#isOnline() if I remember correctly.
     
  13. Offline

    vhbob

    @Tecno_Wizard from my understanding you want me to change the if statement in this right?
    Code:
    if (Bukkit.getOnlinePlayers().contains(reciver)) {
                                    Player r = (Player) Bukkit.getServer().getOfflinePlayer(reciver);
                                    giveCoins(r, Integer.parseInt(args[2]));
                                }
    if so, how will i case "reciver" which is a string, to a player
     
  14. Offline

    Tecno_Wizard

    @vhbob, cut out the first if case entirely. Bukkit treats all names as possible players regardless of whether or not they exist, so Bukkit#getOfflinePlayer(receiver) will always return a player, regardless of whether or not it exists. Then call #isOnline.
     
  15. Offline

    vhbob

Thread Status:
Not open for further replies.

Share This Page