package cn.citycraft.LuckLottery.config; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import cn.citycraft.LuckLottery.LuckLottery; import cn.citycraft.LuckLottery.utils.ChatUtils; import cn.citycraft.LuckLottery.utils.LotteryUtils; import cn.citycraft.PluginHelper.config.FileConfig; public class PlayerData { public static Map> playerLottery = new HashMap<>(); public static FileConfig playerdata = new FileConfig(LuckLottery.plugin, "playerdate.yml"); public static void addCount() { playerdata.set("Count", playerdata.getInt("Count", 0) + 1); playerdata.set("PriceTotal", playerdata.getInt("PriceTotal", 0) + LotteryUtils.getPrice()); playerdata.save(); } public static Map addLottery(final Player player, final String lottery) { Map playerhas = playerLottery.get(player.getName()); if (playerhas == null) { playerhas = new HashMap<>(); } if (!player.isOnline()) { return playerhas; } playerhas.put(lottery, playerhas.containsKey(lottery) ? playerhas.get(lottery) + 1 : 1); playerLottery.put(player.getName(), playerhas); playerdata.set(player.getName(), playerhas); addCount(); return playerhas; } public static void clearLottery() { for (final Entry> pl : playerLottery.entrySet()) { final String p = pl.getKey(); if (p != null && !"".equals(p)) { playerdata.set(p, null); } } playerLottery.clear(); playerdata.save(); } public static Map> getPlayerLottery() { return playerLottery; } @SuppressWarnings("unchecked") public static void loadPlayerLottery(final String p) { final Map pl = (Map) playerdata.get(p); if (pl != null) { playerLottery.put(p, pl); } } public static void printStatus(final CommandSender sender) { int nc = 0; int np = 0; int ncc = 0; for (final Map lls : playerLottery.values()) { for (final Entry a : lls.entrySet()) { nc += a.getValue(); np += LotteryUtils.getPrice() * a.getValue(); ncc += LuckLottery.plugin.getConfig().getInt("Reward." + LotteryUtils.getSameNumber(a.getKey()), 200) * a.getValue(); } } ChatUtils.sendMessage(sender, String.format("§a本轮当前售出彩票 §e%s §a张", nc)); ChatUtils.sendMessage(sender, String.format("§a本轮预计回收金额 §e%s§a(总: §e%s §a中奖返还: §e%s§a) §a元", np - ncc, np, ncc)); ChatUtils.sendMessage(sender, String.format("§d总计出售彩票 §e%s §d张", playerdata.getInt("Count", 0))); ChatUtils.sendMessage(sender, String.format("§d总计回收金额 §e%s §d元", playerdata.getInt("PriceTotal", 0) - ncc)); } public static void reloadPlayerLottery() { final Set apl = playerdata.getKeys(false); for (final String p : apl) { loadPlayerLottery(p); } } public static void reward(final int price) { playerdata.set("PriceTotal", playerdata.getInt("PriceTotal", 0) - price); playerdata.save(); } public static void saveLottery() { for (final Entry> pl : playerLottery.entrySet()) { final String p = pl.getKey(); final Map l = pl.getValue(); playerdata.set(p, l); } playerdata.save(); } public static void showAllPlayerLottery(final CommandSender sender) { ChatUtils.sendMessage(sender, "§c正在检索玩家彩票数据..."); boolean has = false; for (final Entry> phas : playerLottery.entrySet()) { ChatUtils.sendMessage(sender, "§6玩家: §a" + phas.getKey() + "§6购买的彩票有"); final Map pls = phas.getValue(); for (final String lry : pls.keySet()) { has = true; final int count = pls.get(lry); ChatUtils.sendMessage(sender, "§a" + lry + (count > 1 ? "*" + count : "")); } } if (!has) { ChatUtils.sendMessage(sender, "§d没有玩家购买本轮彩票..."); } } public static void showPlayerLottery(final Player p) { final Map playerhas = playerLottery.get(p.getName()); if (playerhas != null && !playerhas.isEmpty()) { ChatUtils.sendMessage(p, "§6您当前购买的彩票有:"); for (final String lry : playerhas.keySet()) { final int count = playerhas.get(lry); ChatUtils.sendMessage(p, "§a" + lry + (count > 1 ? "*" + count : "")); } } else { ChatUtils.sendMessage(p, "§c您当前没有购买的彩票!"); } } }