package pw.yumc.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.configuration.ConfigurationSection; import org.bukkit.entity.Player; import pw.yumc.LuckLottery.LuckLottery; import pw.yumc.LuckLottery.utils.ChatUtils; import pw.yumc.LuckLottery.utils.LotteryUtils; import pw.yumc.YumCore.config.FileConfig; public class PlayerData { public static Map> playerLottery = new HashMap<>(); public static FileConfig playerdata = new FileConfig("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; } public static void loadPlayerLottery(final String p) { final ConfigurationSection pcfg = playerdata.getConfigurationSection(p); final Set pl = pcfg.getKeys(false); if (pl != null) { final Map tpl = new HashMap<>(); for (final String ll : pl) { tpl.put(ll, pcfg.getInt(ll, 0)); } playerLottery.put(p, tpl); } } public static void printStatus(final CommandSender sender) { int nc = 0; int np = 0; int ncc = 0; for (final Map lls : playerLottery.values()) { if (lls == null) { continue; } 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) { if (playerdata.isConfigurationSection(p)) { 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; showToPlayer(sender, lry, pls.get(lry)); } } 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()) { showToPlayer(p, lry, playerhas.get(lry)); } } else { ChatUtils.sendMessage(p, "§c您当前没有购买的彩票!"); } } public static void showToPlayer(final CommandSender sender, final String lry, final int count) { ChatUtils.sendMessage(sender, "§a" + lry + (count > 1 ? " §6* §e" + count : "")); } }