My simple economy plugin

Discussion in 'Plugin Development' started by UnpeeledBanana1, Oct 12, 2016.

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

    UnpeeledBanana1

    Right, so I'm attempting to make my own economy plugin simply by setting a number in the config under a player's UUID. However, I'm coming into a dilemma when attempting to edit the player's token balance.
    So for I'm only concentrating on adding tokens to a player's account.

    Here's the code:

    Code:
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    
        Player player = (Player) sender;
    
        if (player.hasPermission("bananacloud.tokens")) {
            player.sendMessage("§9§lUSAGE:§7 /addtokens <amount> <player>");
        }
        if (args.length == 1 && args[0].matches("[0-9]")) {
            player.sendMessage("§9§lUSAGE:§7 /addtokens " + args[0] + " <player>");
        }
        if (args.length == 2) {
            if (Bukkit.getOfflinePlayer(args[0]).isOnline()) {
            
                Player target = Bukkit.getPlayer(args[0]);
    
                main.getConfig().set("players." + target.getUniqueId() + ".tokens", main.getConfig().getInt("players." + target.getUniqueId() + ".tokens") + args[0]);
                main.saveConfig();
                player.sendMessage("§9§lTOKENS:§7 " + target + " has recieved " + args[0] + " tokens!");
                target.sendMessage("§9§lTOKENS:§7 You have recieved " + args[0] + " tokens!");
            }
        }
        return false;
     }
    (The command, /addtokens, is registered in another class)

    Basically the first part is correct but I'm struggling on getting a specific amount to be added to a player's account (args.length 1) and connecting that to args.length 2.
    Instead of just adding the tokens to the config, it sends me the same message as /addtokens.

    When I do /addtokens <amount> it all works fine but when I do /addtokens <amount> <player> nothing happens, I just get sent the message "USAGE: /addtokens <amount> <player>".

    I think the problem is to do with the 3rd word being the player's name and it isn't connecting to the second word, the amount, but I'm not sure.
     
    Last edited: Oct 12, 2016
  2. Offline

    Zombie_Striker

    Stop blindly casting objects! Why is this such a common thing?!?. What if a plugin sends ANY command? What if it's a console? consoels and plugins are not players, so this would break your plugin.

    Ad d an instanceof check before you create this varaible.
    What this is doing is saying "If a player has the permission, tell them how to use it and continue doing the rest of the command". You should return true right after you send this message to stop it from running longer than it needs to.

    Again, same as above. Also, if the args.length is equal to 1, but I put in anything that is not a number, I will not receive a message. In that case, I will not know what went wrong.

    This is useless, because the only way this can be false is if it will throw an error and break the command. If a player is not online, this will return a Null variable. That method ".isOnline" is only meant to be used on OfflinePlayer.

    Null check instead of using that method.

    1. Add "toString" right after "player.getUniqueID()" because currently you are using the instnace for the path, not the actual UUID.
    2. You cannot add a string to an int. Let me show you an example:
    The int in the config is equal to 10. The args[0] lets say is 10. What you excpect to get (numaricly) would be 20 (10+10=20), BUT you are treating it as a string, so it will actually be equal to 1010 because you are addinng two strings together.

    To change this, convert args[0] to an integer, and add brackets around those two ints.


    This one really confuses me. How can a string be BOTH an amount an a player?

    Using your format, you want the int to come first before the player (which I do not agree with, but it's your plugin). Change the "target" varaible to take in args[1] to achieve this.
     
  3. Online

    timtower Administrator Administrator Moderator

    He is using getOfflinePlayer though
     
  4. Offline

    UnpeeledBanana1

    Well I tried my best... I'll try and work off this.

    EDIT#01
    Well, after a little stress trying to sort it out, it's finally fixed (well it works fine). Thank you @Zombie_Striker for your... Constructive Criticism!

    EDIT#02
    By the way
    Code:
    if (Bukkit.getOfflinePlayer(args[0]).isOnline()) {
    doesn't return a null variable, this method works perfectly fine.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Oct 12, 2016
Thread Status:
Not open for further replies.

Share This Page