Begins implementing FS-7

should be good enough, but feedback would be lovely
This commit is contained in:
Video 2021-09-03 00:07:20 -06:00
parent 4c3f188bb8
commit dc490659c1

View File

@ -9,8 +9,11 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
@CommandPermissions(level = Rank.OP, source = SourceType.BOTH) @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. ", usage = "/<command> [player] | pay <player> <amount>")
public class Command_coins extends FreedomCommand public class Command_coins extends FreedomCommand
{ {
@Override @Override
@ -21,34 +24,119 @@ public class Command_coins extends FreedomCommand
msg("The shop is currently disabled!", ChatColor.RED); msg("The shop is currently disabled!", ChatColor.RED);
return true; return true;
} }
Player p;
final String prefix = FUtil.colorize(ConfigEntry.SHOP_PREFIX.getString() + " "); 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]); if (senderIsConsole)
} {
else msg("When used from the console, you must define a target player.");
{ }
msg(PLAYER_NOT_FOUND); else
{
PlayerData playerData = getData(playerSender);
msg(prefix + ChatColor.GREEN + "You have " + ChatColor.RED + playerData.getCoins() + ChatColor.GREEN
+ " coins.");
}
return true; return true;
} }
}
else // Mode for seeing how many coins a player has.
{ case 1:
if (senderIsConsole)
{ {
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; 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();
} }
} }