diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_coins.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_coins.java index f50b8985..3d0a812d 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_coins.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_coins.java @@ -9,8 +9,11 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.util.ArrayList; +import java.util.List; + @CommandPermissions(level = Rank.OP, source = SourceType.BOTH) -@CommandParameters(description = "Shows the amount of coins you have or another player has", usage = "/ [playername]") +@CommandParameters(description = "Shows the amount of coins you or another player has. Also allows you to give coins to other players.", usage = "/ [player] | pay ") public class Command_coins extends FreedomCommand { @Override @@ -21,34 +24,119 @@ public class Command_coins extends FreedomCommand msg("The shop is currently disabled!", ChatColor.RED); return true; } - Player p; + final String prefix = FUtil.colorize(ConfigEntry.SHOP_PREFIX.getString() + " "); - if (args.length > 0) + + switch (args.length) { - if (getPlayer(args[0]) != null) + // Mode for seeing how many coins the sender has (doesn't work from console) + case 0: { - p = getPlayer(args[0]); - } - else - { - msg(PLAYER_NOT_FOUND); + if (senderIsConsole) + { + msg("When used from the console, you must define a target player."); + } + else + { + PlayerData playerData = getData(playerSender); + msg(prefix + ChatColor.GREEN + "You have " + ChatColor.RED + playerData.getCoins() + ChatColor.GREEN + + " coins."); + } return true; } - } - else - { - if (senderIsConsole) + + // Mode for seeing how many coins a player has. + case 1: { - msg(prefix + ChatColor.RED + "You are not a player, use /coins "); + Player target = getPlayer(args[0]); + + if (target == null) + { + msg(PLAYER_NOT_FOUND); + } + else + { + PlayerData playerData = getData(target); + msg(prefix + ChatColor.GREEN + target.getName() + " has " + ChatColor.RED + playerData.getCoins() + ChatColor.GREEN + " coins."); + } return true; } - else + + // Mode for paying another player coins + case 3: { - p = playerSender; + if (args[0].equalsIgnoreCase("pay")) + { + checkPlayer(); + + final Player target = getPlayer(args[1]); + final PlayerData senderData = getData(playerSender); + + int coinsToTransfer; + + // Processes args[2] + try + { + // Prevents players from trying to be cheeky with negative numbers. + coinsToTransfer = Math.max(Math.abs(Integer.parseInt(args[2])), 1); + } + catch (NumberFormatException ex) + { + msg("Invalid number: " + args[2], ChatColor.RED); + return true; + } + + // Prevents players from performing transactions they can't afford to do. + if (senderData.getCoins() < coinsToTransfer) + { + msg("You don't have enough coins to perform this transaction.", ChatColor.RED); + return true; + } + + if (target == null) + { + msg(PLAYER_NOT_FOUND); + } + else + { + PlayerData playerData = getData(target); + playerData.setCoins(playerData.getCoins() + coinsToTransfer); + senderData.setCoins(senderData.getCoins() - coinsToTransfer); + + msg(target, sender.getName() + + ChatColor.GREEN + " has given you " + + ChatColor.GOLD + coinsToTransfer + + ChatColor.GREEN + " coin" + (coinsToTransfer > 1 ? "s" : "") + "!", ChatColor.GOLD); + + msg("You have given " + + ChatColor.GOLD + coinsToTransfer + + ChatColor.GREEN + " coin" + (coinsToTransfer > 1 ? "s" : "") + + " to " + ChatColor.GOLD + target.getName() + ChatColor.GREEN + ".", ChatColor.GREEN); + } + + return true; + } + } + + default: + { + return false; } } - PlayerData playerData = plugin.pl.getData(p); - msg(prefix + ChatColor.GREEN + (args.length > 0 ? p.getName() + " has " : "You have ") + ChatColor.RED + playerData.getCoins() + ChatColor.GREEN + " coins."); - return true; + } + + @Override + public List getTabCompleteOptions(CommandSender sender, Command command, String alias, String[] args) + { + if (args.length == 1) + { + List options = new ArrayList<>(FUtil.getPlayerList()); + + options.add("pay"); + + return options; + } + + return FUtil.getPlayerList(); } } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opall.java b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opall.java index a02671cc..45ff1689 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opall.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/command/Command_opall.java @@ -24,6 +24,10 @@ public class Command_opall extends FreedomCommand msg(player, YOU_ARE_OP); plugin.rm.updateDisplay(player); } + else + { + player.recalculatePermissions(); + } } return true;