2020-01-07 20:13:59 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.command;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2020-01-07 20:53:31 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
2020-01-07 20:13:59 +00:00
|
|
|
import java.util.List;
|
2020-01-07 20:53:31 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Random;
|
2020-01-07 20:13:59 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
|
|
|
import me.totalfreedom.totalfreedommod.rank.Rank;
|
|
|
|
import me.totalfreedom.totalfreedommod.shop.ShopData;
|
|
|
|
import me.totalfreedom.totalfreedommod.shop.ShopItem;
|
|
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.inventory.Inventory;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.inventory.meta.ItemMeta;
|
2020-01-12 14:51:29 +00:00
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
2020-01-07 20:13:59 +00:00
|
|
|
|
|
|
|
@CommandPermissions(level = Rank.OP, source = SourceType.ONLY_IN_GAME)
|
2020-03-30 23:43:57 +00:00
|
|
|
@CommandParameters(description = "Access the shop - this will be much simplified in the future.", usage = "/<command> <buy <item> | action | list>")
|
2020-01-07 20:13:59 +00:00
|
|
|
public class Command_shop extends FreedomCommand
|
|
|
|
{
|
2020-01-07 20:53:31 +00:00
|
|
|
private final List<String> locations = Arrays.asList("Sofa", "Car", "Bed", "Kitchen", "Garage", "Basement", "Home Study");
|
|
|
|
private Map<CommandSender, String> featureCooldown = new HashMap<>();
|
|
|
|
|
2020-01-07 20:13:59 +00:00
|
|
|
@Override
|
|
|
|
public boolean run(final CommandSender sender, final Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
|
|
|
|
{
|
|
|
|
if (!ConfigEntry.SHOP_ENABLED.getBoolean())
|
|
|
|
{
|
|
|
|
msg("The shop is currently disabled!", ChatColor.RED);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
final String prefix = FUtil.colorize(ConfigEntry.SHOP_PREFIX.getString() + " ");
|
|
|
|
ShopData sd = plugin.sh.getData(playerSender);
|
|
|
|
if (args.length == 0)
|
|
|
|
{
|
|
|
|
msg(prefix + ChatColor.GREEN + "Balance: " + ChatColor.RED + sd.getCoins());
|
|
|
|
return true;
|
|
|
|
}
|
2020-01-07 20:53:31 +00:00
|
|
|
if (args.length == 1)
|
|
|
|
{
|
|
|
|
if (featureCooldown.containsKey(sender) && featureCooldown.containsValue(args[0]))
|
|
|
|
{
|
|
|
|
msg("You're on cooldown for this feature.", ChatColor.RED);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Random r = new Random();
|
|
|
|
switch (args[0])
|
|
|
|
{
|
|
|
|
case "daily":
|
|
|
|
{
|
|
|
|
sd.setCoins(sd.getCoins() + 100);
|
|
|
|
plugin.sh.save(sd);
|
|
|
|
msg(prefix + ChatColor.GREEN + "You received your 100 coins!");
|
2020-01-12 14:51:29 +00:00
|
|
|
cooldown(86400, sender, args[0]);
|
2020-01-07 20:53:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case "search":
|
|
|
|
{
|
|
|
|
int amount = FUtil.random(5, 10);
|
|
|
|
String location = locations.get(r.nextInt(locations.size()));
|
|
|
|
sd.setCoins(sd.getCoins() + amount);
|
|
|
|
plugin.sh.save(sd);
|
|
|
|
msg(prefix + ChatColor.AQUA + location + ChatColor.GREEN + " - Found " + ChatColor.RED + amount + ChatColor.GREEN + " coins!");
|
2020-01-12 14:51:29 +00:00
|
|
|
cooldown(30, sender, args[0]);
|
2020-01-11 22:44:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case "list":
|
|
|
|
case "items":
|
|
|
|
{
|
|
|
|
msg(prefix + ChatColor.GREEN + "Available items:");
|
|
|
|
for (ShopItem item : ShopItem.values())
|
|
|
|
{
|
2020-02-05 22:07:16 +00:00
|
|
|
if (!item.isPurchaseable())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-01-11 22:44:13 +00:00
|
|
|
msg(prefix + ChatColor.AQUA + item.getName() + ChatColor.GREEN + " - " + ChatColor.RED + item.getCost());
|
|
|
|
}
|
2020-01-07 20:53:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-01-12 14:51:29 +00:00
|
|
|
Player player = getPlayer(args[0]);
|
|
|
|
if (player == null)
|
|
|
|
{
|
|
|
|
msg(PLAYER_NOT_FOUND);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ShopData psd = plugin.sh.getData(player);
|
|
|
|
msg(prefix + ChatColor.AQUA + player.getName() + ChatColor.GREEN + "'s balance: " + ChatColor.RED + psd.getCoins());
|
|
|
|
return true;
|
2020-01-07 20:53:31 +00:00
|
|
|
}
|
2020-01-07 20:13:59 +00:00
|
|
|
if (args.length != 2)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (args[0])
|
|
|
|
{
|
|
|
|
case "buy":
|
|
|
|
{
|
|
|
|
ShopItem item = ShopItem.findItem(args[1]);
|
2020-01-13 01:56:31 +00:00
|
|
|
if (item == null || !item.isPurchaseable())
|
2020-01-07 20:13:59 +00:00
|
|
|
{
|
|
|
|
msg("Invalid item: " + item);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (sd.hasItem(item))
|
|
|
|
{
|
|
|
|
msg(prefix + ChatColor.GREEN + "You already have that item! To get it, use " + ChatColor.RED + "/shop get " + item.name() + ChatColor.GREEN + "!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (item.getCost() > sd.getCoins())
|
|
|
|
{
|
|
|
|
msg(prefix + ChatColor.GREEN + "You don't have enough money for this item!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
String signature = sd.giveItem(item);
|
|
|
|
sd.setCoins(sd.getCoins() - item.getCost());
|
|
|
|
plugin.sh.save(sd);
|
|
|
|
ItemStack stack = new ItemStack(item.getMaterial(), 1);
|
|
|
|
ItemMeta meta = stack.getItemMeta();
|
|
|
|
meta.setDisplayName(item.getColoredName());
|
|
|
|
List<String> lore = new ArrayList<>();
|
|
|
|
lore.add(ChatColor.DARK_GRAY + signature);
|
|
|
|
meta.setLore(lore);
|
|
|
|
stack.setItemMeta(meta);
|
|
|
|
Inventory inv = playerSender.getInventory();
|
|
|
|
inv.setItem(inv.firstEmpty(), stack);
|
|
|
|
msg(prefix + ChatColor.GREEN + "You bought a " + item.getColoredName() + ChatColor.GREEN + "!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case "get":
|
|
|
|
{
|
|
|
|
ShopItem item = ShopItem.findItem(args[1]);
|
2020-02-05 22:07:16 +00:00
|
|
|
if (item == null)
|
2020-01-07 20:13:59 +00:00
|
|
|
{
|
|
|
|
msg("Invalid item: " + item);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!sd.hasItem(item))
|
|
|
|
{
|
2020-02-05 22:07:16 +00:00
|
|
|
msg(prefix + ChatColor.GREEN + "You don't have that item! To buy it, use " + ChatColor.RED + "/shop buy " + item.name() + ChatColor.GREEN + "!");
|
2020-01-07 20:13:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Inventory inv = playerSender.getInventory();
|
|
|
|
inv.setItem(inv.firstEmpty(), sd.getItem(item));
|
|
|
|
msg(prefix + ChatColor.GREEN + "You got your " + item.getColoredName() + ChatColor.GREEN + "!");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 14:51:29 +00:00
|
|
|
private void cooldown(int seconds, CommandSender sender, String feature)
|
2020-01-07 20:53:31 +00:00
|
|
|
{
|
|
|
|
featureCooldown.put(sender, feature);
|
2020-01-12 14:51:29 +00:00
|
|
|
new BukkitRunnable()
|
2020-01-07 20:53:31 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
featureCooldown.remove(sender);
|
|
|
|
}
|
2020-01-12 14:51:29 +00:00
|
|
|
}.runTaskLater(plugin, seconds * 20);
|
2020-01-07 20:53:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 20:13:59 +00:00
|
|
|
}
|