Merge pull request #129 from AtlasMediaGroup/FS-7

Allow players to pay other players coins [FS-7]
This commit is contained in:
Video 2021-11-29 06:23:57 -07:00 committed by GitHub
commit aa20a6e579
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 = "/<command> [playername]")
@CommandParameters(description = "Shows the amount of coins you or another player has. Also allows you to give coins to other players.", usage = "/<command> [player] | pay <player> <amount>")
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 <playername>");
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<String> getTabCompleteOptions(CommandSender sender, Command command, String alias, String[] args)
{
if (args.length == 1)
{
List<String> options = new ArrayList<>(FUtil.getPlayerList());
options.add("pay");
return options;
}
return FUtil.getPlayerList();
}
}