TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/command/Command_manageshop.java

210 lines
9.2 KiB
Java
Raw Normal View History

2019-11-02 17:21:23 +00:00
package me.totalfreedom.totalfreedommod.command;
import me.totalfreedom.totalfreedommod.rank.Rank;
import me.totalfreedom.totalfreedommod.shop.ShopData;
2020-02-05 22:07:16 +00:00
import me.totalfreedom.totalfreedommod.shop.ShopItem;
2020-04-08 08:34:08 +00:00
import me.totalfreedom.totalfreedommod.util.FUtil;
2020-04-08 02:20:01 +00:00
import org.apache.commons.lang.StringUtils;
2019-11-02 17:21:23 +00:00
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@CommandPermissions(level = Rank.SENIOR_ADMIN, source = SourceType.ONLY_CONSOLE)
2020-04-08 02:20:01 +00:00
@CommandParameters(description = "Manage the shop", usage = "/<command> <coins: <add | set | remove> <amount> <player | all> | items: <give | take> <item> <player>", aliases = "ms")
2019-11-02 17:21:23 +00:00
public class Command_manageshop extends FreedomCommand
{
@Override
public boolean run(final CommandSender sender, final Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
2020-04-08 08:34:08 +00:00
if (!FUtil.isExecutive(sender.getName()))
{
return noPerms();
}
2020-04-08 02:20:01 +00:00
if (args.length < 2)
2019-11-02 17:21:23 +00:00
{
2020-04-08 02:20:01 +00:00
return false;
}
if (args[0].equals("coins"))
{
if (args.length < 4)
2019-11-02 17:21:23 +00:00
{
2020-04-08 02:20:01 +00:00
return false;
}
switch (args[1])
{
case "add":
try
2019-11-02 17:21:23 +00:00
{
2020-04-08 02:20:01 +00:00
int amount = Math.max(0, Math.min(1000000, Integer.parseInt(args[2])));
if (!args[3].equals("all"))
{
Player player = getPlayer(args[3]);
if (player == null)
2019-11-02 17:21:23 +00:00
{
2020-04-08 02:20:01 +00:00
msg(PLAYER_NOT_FOUND);
2019-11-02 17:21:23 +00:00
return true;
}
2020-04-08 02:20:01 +00:00
ShopData shopData = plugin.sh.getData(player);
shopData.setCoins(shopData.getCoins() + amount);
plugin.sh.save(shopData);
msg("Successfully added " + amount + " coins to " + player.getName() + ". Their new balance is " + shopData.getCoins(), ChatColor.GREEN);
player.sendMessage(ChatColor.GREEN + sender.getName() + " gave you " + amount + " coins. Your new balance is " + shopData.getCoins());
return true;
}
else
{
for (Player player : server.getOnlinePlayers())
2019-11-02 17:21:23 +00:00
{
2020-04-08 02:20:01 +00:00
ShopData shopData = plugin.sh.getData(player);
shopData.setCoins(shopData.getCoins() + amount);
plugin.sh.save(shopData);
player.sendMessage(ChatColor.GREEN + sender.getName() + " gave you " + amount + " coins. Your new balance is " + shopData.getCoins());
2019-11-02 17:21:23 +00:00
}
2020-04-08 02:20:01 +00:00
msg("Successfully added " + amount + " coins to all online players.", ChatColor.GREEN);
return true;
}
}
catch (NumberFormatException ex)
{
msg("Invalid number: " + args[2], ChatColor.RED);
return true;
}
case "remove":
try
{
int amount = Math.max(0, Math.min(1000000, Integer.parseInt(args[2])));
if (!args[3].equals("all"))
{
Player player = getPlayer(args[3]);
if (player == null)
2019-11-02 17:21:23 +00:00
{
2020-04-08 02:20:01 +00:00
msg(PLAYER_NOT_FOUND);
2019-11-02 17:21:23 +00:00
return true;
}
2020-04-08 02:20:01 +00:00
ShopData shopData = plugin.sh.getData(player);
shopData.setCoins(shopData.getCoins() + amount);
if (shopData.getCoins() < 0)
2019-11-02 17:21:23 +00:00
{
2020-04-08 02:20:01 +00:00
shopData.setCoins(0);
2019-11-02 17:21:23 +00:00
}
2020-04-08 02:20:01 +00:00
plugin.sh.save(shopData);
msg("Successfully removed " + amount + " coins from " + player.getName() + ". Their new balance is " + shopData.getCoins(), ChatColor.GREEN);
player.sendMessage(ChatColor.RED + sender.getName() + " took " + amount + " coins from you. Your new balance is " + shopData.getCoins());
2020-02-05 22:07:16 +00:00
return true;
}
2020-04-08 02:20:01 +00:00
else
2020-02-05 22:07:16 +00:00
{
2020-04-08 02:20:01 +00:00
for (Player player : server.getOnlinePlayers())
{
ShopData shopData = plugin.sh.getData(player);
shopData.setCoins(shopData.getCoins() - amount);
if (shopData.getCoins() < 0)
{
shopData.setCoins(0);
}
plugin.sh.save(shopData);
player.sendMessage(ChatColor.RED + sender.getName() + " took " + amount + " coins from you. Your new balance is " + shopData.getCoins());
}
msg("Successfully took " + amount + " coins from all online players.", ChatColor.GREEN);
2020-02-05 22:07:16 +00:00
return true;
}
2020-04-08 02:20:01 +00:00
}
catch (NumberFormatException ex)
{
msg("Invalid number: " + args[2], ChatColor.RED);
2020-02-05 22:07:16 +00:00
return true;
}
2020-04-08 02:20:01 +00:00
case "set":
try
2020-02-05 22:07:16 +00:00
{
2020-04-08 02:20:01 +00:00
int amount = Math.max(0, Math.min(1000000, Integer.parseInt(args[2])));
Player player = getPlayer(args[3]);
2020-02-05 22:07:16 +00:00
if (player == null)
{
2020-04-08 02:20:01 +00:00
msg(PLAYER_NOT_FOUND);
2020-02-05 22:07:16 +00:00
return true;
}
2020-04-08 02:20:01 +00:00
ShopData shopData = plugin.sh.getData(player);
shopData.setCoins(amount);
plugin.sh.save(shopData);
msg("Successfully set " + player.getName() + "'s coins to " + amount, ChatColor.GREEN);
player.sendMessage(ChatColor.GREEN + sender.getName() + " set your coin balance to " + amount);
return true;
}
catch (NumberFormatException ex)
{
msg("Invalid number: " + args[2], ChatColor.RED);
2020-02-05 22:07:16 +00:00
return true;
}
2020-04-08 02:20:01 +00:00
}
}
else if (args[0].equals("items"))
{
if (args[1].equals("list"))
{
msg("List of all shop 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)
{
msg(args[2] + " is not a valid item.", ChatColor.RED);
return true;
}
Player player = getPlayer(args[3]);
if (player == null)
{
msg(PLAYER_NOT_FOUND);
return true;
2020-02-05 22:07:16 +00:00
}
2020-04-08 02:20:01 +00:00
ShopData shopData = plugin.sh.getData(player);
shopData.giveItem(item);
plugin.sh.save(shopData);
msg("Successfully gave the " + item.getName() + " to " + player.getName(), ChatColor.GREEN);
player.sendMessage(ChatColor.GREEN + sender.getName() + " gave the " + item.getName() + " to you");
return true;
2020-02-05 22:07:16 +00:00
}
2020-04-08 02:20:01 +00:00
else if (args[1].equals("take"))
{
ShopItem item = ShopItem.findItem(args[2].toUpperCase());
if (item == null)
{
msg(args[2] + " is not a valid item.", ChatColor.RED);
return true;
}
Player player = getPlayer(args[3]);
if (player == null)
{
msg(PLAYER_NOT_FOUND);
return true;
}
ShopData shopData = plugin.sh.getData(player);
shopData.removeItem(item);
plugin.sh.save(shopData);
msg("Successfully took the " + item.getName() + " from " + player.getName(), ChatColor.GREEN);
player.sendMessage(ChatColor.RED + sender.getName() + " took the " + item.getName() + " from you");
return true;
}
2019-11-02 17:21:23 +00:00
}
return false;
}
2019-11-04 12:42:01 +00:00
}