275 lines
12 KiB
Java
275 lines
12 KiB
Java
package me.totalfreedom.totalfreedommod.command;
|
|
|
|
import me.totalfreedom.totalfreedommod.api.ShopItem;
|
|
import me.totalfreedom.totalfreedommod.command.handling.*;
|
|
import me.totalfreedom.totalfreedommod.player.PlayerData;
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
|
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.bukkit.command.Command;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.entity.Player;
|
|
|
|
@CommandDependencies({"TF-Shoppe"})
|
|
@CommandPermissions(permission = "manageshop", source = SourceType.BOTH)
|
|
@CommandParameters(description = "Manage the shop", usage = "/<command> <coins: <add | set | remove> <amount> <player | all> | items: <give | take> <item> <player>", aliases = "ms")
|
|
public class Command_manageshop extends FreedomCommand
|
|
{
|
|
|
|
// These are here because SonarLint said "use a constant instead of duplicating this literal".
|
|
public static final String AMOUNT = "amount";
|
|
public static final String PLAYER = "player";
|
|
public static final String BALANCE = "balance";
|
|
|
|
@Override
|
|
public boolean run(final CommandSender sender, final Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
|
|
{
|
|
if (plugin.sh == null)
|
|
{
|
|
msgNew("<red>Shop is currently disabled.");
|
|
return true;
|
|
}
|
|
|
|
if (!FUtil.isExecutive(sender.getName()))
|
|
{
|
|
return noPerms();
|
|
}
|
|
|
|
if (args.length < 2)
|
|
{
|
|
return false;
|
|
}
|
|
if (args[0].equals("coins"))
|
|
{
|
|
if (args.length < 4)
|
|
{
|
|
return false;
|
|
}
|
|
switch (args[1])
|
|
{
|
|
case "add" ->
|
|
{
|
|
try
|
|
{
|
|
int amount = Math.max(0, Math.min(1000000, Integer.parseInt(args[2])));
|
|
if (!args[3].equals("all"))
|
|
{
|
|
PlayerData playerData = plugin.pl.getData(args[3]);
|
|
if (playerData == null)
|
|
{
|
|
msg(PLAYER_NOT_FOUND);
|
|
return true;
|
|
}
|
|
playerData.setCoins(playerData.getCoins() + amount);
|
|
plugin.pl.save(playerData);
|
|
msgNew("<green>Successfully gave <amount> coins to <player>. Their new balance is <balance>.",
|
|
Placeholder.unparsed(AMOUNT, String.valueOf(amount)),
|
|
Placeholder.unparsed(PLAYER, playerData.getName()),
|
|
Placeholder.unparsed(BALANCE, String.valueOf(playerData.getCoins())));
|
|
Player player = getPlayer(args[3]);
|
|
if (player != null)
|
|
{
|
|
msgNew(player, "<green><player> gave you <amount> coins. Your new balance is <balance>.",
|
|
Placeholder.unparsed(PLAYER, sender.getName()),
|
|
Placeholder.unparsed(AMOUNT, String.valueOf(amount)),
|
|
Placeholder.unparsed(BALANCE, String.valueOf(playerData.getCoins())));
|
|
}
|
|
} else
|
|
{
|
|
for (Player player : server.getOnlinePlayers())
|
|
{
|
|
PlayerData playerData = plugin.pl.getData(player);
|
|
playerData.setCoins(playerData.getCoins() + amount);
|
|
plugin.pl.save(playerData);
|
|
|
|
msgNew(player, "<green><player> gave you <amount> coins. Your new balance is <balance>.",
|
|
Placeholder.unparsed(PLAYER, sender.getName()),
|
|
Placeholder.unparsed(AMOUNT, String.valueOf(amount)),
|
|
Placeholder.unparsed(BALANCE, String.valueOf(playerData.getCoins())));
|
|
}
|
|
|
|
msgNew("<green>Successfully gave <amount> coins to all online players.",
|
|
Placeholder.unparsed(AMOUNT, String.valueOf(amount)));
|
|
}
|
|
return true;
|
|
} catch (NumberFormatException ex)
|
|
{
|
|
msgNew("<red>Invalid number: <amount>", Placeholder.unparsed(AMOUNT, args[2]));
|
|
return true;
|
|
}
|
|
}
|
|
case "remove" ->
|
|
{
|
|
try
|
|
{
|
|
int amount = Math.max(0, Math.min(1000000, Integer.parseInt(args[2])));
|
|
if (!args[3].equals("all"))
|
|
{
|
|
PlayerData playerData = plugin.pl.getData(args[3]);
|
|
if (playerData == null)
|
|
{
|
|
msg(PLAYER_NOT_FOUND);
|
|
return true;
|
|
}
|
|
playerData.setCoins(playerData.getCoins() - amount);
|
|
if (playerData.getCoins() < 0)
|
|
{
|
|
playerData.setCoins(0);
|
|
}
|
|
plugin.pl.save(playerData);
|
|
msgNew("<green>Successfully took <amount> coins from <player>. Their new balance is <balance>.",
|
|
Placeholder.unparsed(AMOUNT, String.valueOf(amount)),
|
|
Placeholder.unparsed(PLAYER, playerData.getName()),
|
|
Placeholder.unparsed(BALANCE, String.valueOf(playerData.getCoins())));
|
|
|
|
Player player = getPlayer(args[3]);
|
|
if (player != null)
|
|
{
|
|
msgNew(player, "<red><player> took <amount> coins from you. Your new balance is <balance>.",
|
|
Placeholder.unparsed(PLAYER, sender.getName()),
|
|
Placeholder.unparsed(AMOUNT, String.valueOf(amount)),
|
|
Placeholder.unparsed(BALANCE, String.valueOf(playerData.getCoins())));
|
|
}
|
|
} else
|
|
{
|
|
for (Player player : server.getOnlinePlayers())
|
|
{
|
|
PlayerData playerData = plugin.pl.getData(player);
|
|
playerData.setCoins(playerData.getCoins() - amount);
|
|
if (playerData.getCoins() < 0)
|
|
{
|
|
playerData.setCoins(0);
|
|
}
|
|
plugin.pl.save(playerData);
|
|
|
|
msgNew(player, "<red><player> took <amount> coins from you. Your new balance is <balance>.",
|
|
Placeholder.unparsed(PLAYER, sender.getName()),
|
|
Placeholder.unparsed(AMOUNT, String.valueOf(amount)),
|
|
Placeholder.unparsed(BALANCE, String.valueOf(playerData.getCoins())));
|
|
}
|
|
|
|
msgNew("<green>Successfully took <amount> coins from all online players.",
|
|
Placeholder.unparsed(AMOUNT, String.valueOf(amount)));
|
|
}
|
|
return true;
|
|
} catch (NumberFormatException ex)
|
|
{
|
|
msgNew("<red>Invalid number: <amount>", Placeholder.unparsed(AMOUNT, args[2]));
|
|
return true;
|
|
}
|
|
}
|
|
case "set" ->
|
|
{
|
|
try
|
|
{
|
|
int amount = Math.max(0, Math.min(1000000, Integer.parseInt(args[2])));
|
|
PlayerData playerData = plugin.pl.getData(args[3]);
|
|
if (playerData == null)
|
|
{
|
|
msg(PLAYER_NOT_FOUND);
|
|
return true;
|
|
}
|
|
playerData.setCoins(amount);
|
|
plugin.pl.save(playerData);
|
|
|
|
msgNew("<green>Successfully set <player>'s coin balance to <amount>.",
|
|
Placeholder.unparsed(PLAYER, playerData.getName()),
|
|
Placeholder.unparsed(AMOUNT, String.valueOf(amount)));
|
|
|
|
Player player = getPlayer(args[3]);
|
|
if (player != null)
|
|
{
|
|
msgNew(player, "<green><player> set your coin balance to <amount>.",
|
|
Placeholder.unparsed(PLAYER, sender.getName()),
|
|
Placeholder.unparsed(AMOUNT, String.valueOf(amount)));
|
|
}
|
|
return true;
|
|
} catch (NumberFormatException ex)
|
|
{
|
|
msgNew("<red>Invalid number: <amount>", Placeholder.unparsed(AMOUNT, args[2]));
|
|
return true;
|
|
}
|
|
}
|
|
default ->
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
} else if (args[0].equals("items"))
|
|
{
|
|
if (args[1].equals("list"))
|
|
{
|
|
msgNew("List of all shop items: <items>", Placeholder.unparsed("items", StringUtils.join(ShopItem.values(), ", ")));
|
|
return true;
|
|
}
|
|
|
|
if (args.length < 4)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (args[1].equals("give"))
|
|
{
|
|
ShopItem item = ShopItem.findItem(args[2].toUpperCase());
|
|
if (item == null)
|
|
{
|
|
msgNew("<red><item> is not a valid item.", Placeholder.unparsed("item", args[2]));
|
|
return true;
|
|
}
|
|
|
|
PlayerData playerData = plugin.pl.getData(args[3]);
|
|
if (playerData == null)
|
|
{
|
|
msg(PLAYER_NOT_FOUND);
|
|
return true;
|
|
}
|
|
playerData.giveItem(item);
|
|
plugin.pl.save(playerData);
|
|
msgNew("<green>Successfully gave the <item> to <player>.",
|
|
Placeholder.unparsed("item", item.getName()),
|
|
Placeholder.unparsed(PLAYER, playerData.getName()));
|
|
|
|
Player player = getPlayer(args[3]);
|
|
if (player != null)
|
|
{
|
|
msgNew(player, "<green><player> gave the <item> to you.",
|
|
Placeholder.unparsed("item", item.getName()),
|
|
Placeholder.unparsed(PLAYER, sender.getName()));
|
|
}
|
|
return true;
|
|
} else if (args[1].equals("take"))
|
|
{
|
|
ShopItem item = ShopItem.findItem(args[2].toUpperCase());
|
|
if (item == null)
|
|
{
|
|
msgNew("<red><item> is not a valid item.", Placeholder.unparsed("item", args[2]));
|
|
return true;
|
|
}
|
|
|
|
PlayerData playerData = plugin.pl.getData(args[3]);
|
|
if (playerData == null)
|
|
{
|
|
msg(PLAYER_NOT_FOUND);
|
|
return true;
|
|
}
|
|
playerData.removeItem(item);
|
|
plugin.pl.save(playerData);
|
|
|
|
msgNew("<green>Successfully took the <item> from <player>.",
|
|
Placeholder.unparsed("item", item.getName()),
|
|
Placeholder.unparsed(PLAYER, playerData.getName()));
|
|
|
|
Player player = getPlayer(args[3]);
|
|
if (player != null)
|
|
{
|
|
msgNew(player, "<red><player> took the <item> from you.",
|
|
Placeholder.unparsed("item", item.getName()),
|
|
Placeholder.unparsed(PLAYER, sender.getName()));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
}
|
|
return false;
|
|
}
|
|
} |