222 lines
6.4 KiB
Java
222 lines
6.4 KiB
Java
package cn.citycraft.LuckLottery.utils;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.Map.Entry;
|
||
import java.util.Random;
|
||
import java.util.Set;
|
||
|
||
import org.bukkit.Bukkit;
|
||
import org.bukkit.ChatColor;
|
||
import org.bukkit.command.CommandSender;
|
||
import org.bukkit.entity.Player;
|
||
|
||
import cn.citycraft.LuckLottery.LuckLottery;
|
||
import cn.citycraft.LuckLottery.runnable.LotteryReward;
|
||
import cn.citycraft.PluginHelper.config.FileConfig;
|
||
|
||
public class LotteryUtils {
|
||
public static Map<String, List<List<String>>> playerLottery = new HashMap<String, List<List<String>>>();
|
||
protected static boolean numbersame;
|
||
protected static FileConfig playerdata;
|
||
protected static LuckLottery plugin;
|
||
protected static int price;
|
||
protected static List<String> systemLottery = new ArrayList<String>();
|
||
|
||
public static List<List<String>> addLottery(final Player player, final List<String> lottery) {
|
||
List<List<String>> playerhas = playerLottery.get(player.getName());
|
||
if (playerhas == null) {
|
||
playerhas = new ArrayList<List<String>>();
|
||
}
|
||
playerhas.add(lottery);
|
||
playerLottery.put(player.getName(), playerhas);
|
||
playerdata.set(player.getName(), playerhas);
|
||
playerdata.save();
|
||
return playerhas;
|
||
}
|
||
|
||
public static void clearLottery() {
|
||
for (final Entry<String, List<List<String>>> pl : playerLottery.entrySet()) {
|
||
final String p = pl.getKey();
|
||
playerdata.set(p, null);
|
||
}
|
||
playerLottery.clear();
|
||
playerdata.save();
|
||
}
|
||
|
||
public static void clearPlayerLottery(final CommandSender sender) {
|
||
ChatUtils.sendMessage(sender, "§c提示: 以清除所有玩家彩票数据...");
|
||
clearLottery();
|
||
}
|
||
|
||
public static int getPrice() {
|
||
return price;
|
||
}
|
||
|
||
public static List<String> getRandomNumber() {
|
||
final Random rnd = new Random();
|
||
final List<String> normal = new ArrayList<String>();
|
||
for (int i = 1; i < 8; i++) {
|
||
String rr = rnd.nextInt(32) + 1 + "";
|
||
while (normal.contains(rr)) {
|
||
rr = rnd.nextInt(32) + 1 + "";
|
||
}
|
||
normal.add(rr);
|
||
}
|
||
return normal;
|
||
}
|
||
|
||
public static int getSameNumber(final List<String> lottery) {
|
||
int samenum = 0;
|
||
final List<String> system = systemLottery;
|
||
if (numbersame) {
|
||
for (int i = 0; i < lottery.size(); i++) {
|
||
if (lottery.get(i).equals(system.get(i))) {
|
||
samenum++;
|
||
}
|
||
}
|
||
} else {
|
||
for (final String pn : lottery) {
|
||
for (final String sn : system) {
|
||
if (pn.equals(sn)) {
|
||
samenum++;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if (lottery.get(lottery.size() - 1).equalsIgnoreCase(system.get(system.size() - 1))) {
|
||
samenum += 9;
|
||
}
|
||
return samenum;
|
||
}
|
||
|
||
public static int getSameNumber(final List<String> lottery, final List<String> system) {
|
||
int samenum = 0;
|
||
if (numbersame) {
|
||
for (int i = 0; i < lottery.size(); i++) {
|
||
if (lottery.get(i).equals(system.get(i))) {
|
||
samenum++;
|
||
}
|
||
}
|
||
} else {
|
||
for (final String pn : lottery) {
|
||
for (final String sn : system) {
|
||
if (pn.equals(sn)) {
|
||
samenum++;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if (lottery.get(lottery.size() - 1).equalsIgnoreCase(system.get(system.size() - 1))) {
|
||
samenum += 9;
|
||
}
|
||
return samenum;
|
||
}
|
||
|
||
public static List<String> getSystemLottery() {
|
||
return systemLottery;
|
||
}
|
||
|
||
public static void init(final LuckLottery luckLottery) {
|
||
plugin = luckLottery;
|
||
playerdata = plugin.getPlayerdata();
|
||
}
|
||
|
||
/**
|
||
* @return numbersame
|
||
*/
|
||
public static boolean isNumbersame() {
|
||
return numbersame;
|
||
}
|
||
|
||
@SuppressWarnings("unchecked")
|
||
public static void loadPlayerLottery(final Player p) {
|
||
final List<?> pl = playerdata.getList(p.getName());
|
||
if (pl != null) {
|
||
playerLottery.put(p.getName(), (List<List<String>>) pl);
|
||
}
|
||
}
|
||
|
||
@SuppressWarnings("unchecked")
|
||
public static void reloadPlayerLottery() {
|
||
final Set<String> apl = playerdata.getKeys(false);
|
||
for (final String p : apl) {
|
||
final List<?> pl = playerdata.getList(p);
|
||
if (pl != null) {
|
||
playerLottery.put(p, (List<List<String>>) pl);
|
||
}
|
||
}
|
||
}
|
||
|
||
public static void rewardLastLottery() {
|
||
Bukkit.getServer().getScheduler().runTaskAsynchronously(LuckLottery.plugin, new LotteryReward(plugin, true));
|
||
}
|
||
|
||
public static void saveLottery() {
|
||
for (final Entry<String, List<List<String>>> pl : playerLottery.entrySet()) {
|
||
final String p = pl.getKey();
|
||
final List<List<String>> l = pl.getValue();
|
||
playerdata.set(p, l);
|
||
}
|
||
playerdata.save();
|
||
}
|
||
|
||
/**
|
||
* @param numbersame
|
||
* 要设置的 numbersame
|
||
*/
|
||
public static void setNumbersame(final boolean numbersame) {
|
||
LotteryUtils.numbersame = numbersame;
|
||
}
|
||
|
||
public static void setPrice(final int price) {
|
||
LotteryUtils.price = price;
|
||
}
|
||
|
||
public static void setSystemLottery(final List<String> systemLottery) {
|
||
LotteryUtils.systemLottery = systemLottery;
|
||
}
|
||
|
||
public static void showAllPlayerLottery(final CommandSender sender) {
|
||
ChatUtils.sendMessage(sender, "§c正在检索玩家彩票数据...");
|
||
boolean has = false;
|
||
for (final Entry<String, List<List<String>>> phas : playerLottery.entrySet()) {
|
||
ChatUtils.sendMessage(sender, "§6玩家: §a" + phas.getKey() + "§6购买的彩票有");
|
||
for (final List<String> lry : phas.getValue()) {
|
||
has = true;
|
||
ChatUtils.sendMessage(sender, "§a" + lry.toString());
|
||
}
|
||
}
|
||
if (!has) {
|
||
ChatUtils.sendMessage(sender, "§d没有玩家购买本轮彩票...");
|
||
}
|
||
}
|
||
|
||
public static void showPlayerLottery(final Player p) {
|
||
final List<List<String>> playerhas = playerLottery.get(p.getName());
|
||
if (playerhas != null && !playerhas.isEmpty()) {
|
||
ChatUtils.sendMessage(p, "§6您当前购买的彩票有:");
|
||
for (final List<String> lry : playerhas) {
|
||
ChatUtils.sendMessage(p, "§a" + lry.toString());
|
||
}
|
||
} else {
|
||
ChatUtils.sendMessage(p, "§c您当前没有购买的彩票!");
|
||
}
|
||
}
|
||
|
||
public static void showSystemLottery(final CommandSender p) {
|
||
ChatUtils.sendMessage(p, ChatColor.DARK_PURPLE + "此轮彩票号码为: " + ChatColor.YELLOW + LotteryUtils.systemLottery.toString());
|
||
}
|
||
|
||
public static void updateSystemLottery() {
|
||
LotteryUtils.systemLottery = LotteryUtils.getRandomNumber();
|
||
}
|
||
|
||
public static void updateSystemLottery(final CommandSender p) {
|
||
LotteryUtils.systemLottery = LotteryUtils.getRandomNumber();
|
||
ChatUtils.sendMessage(p, ChatColor.BLUE + "已随机生成彩票: " + ChatColor.YELLOW + LotteryUtils.systemLottery.toString());
|
||
}
|
||
}
|