feat: 合并多次购买

Signed-off-by: 502647092 <admin@yumc.pw>
master
502647092 2016-08-16 08:56:32 +08:00
parent 92a839ce5d
commit 46760c331e
7 changed files with 114 additions and 113 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>cn.citycraft</groupId>
<artifactId>LuckLottery</artifactId>
<version>1.3</version>
<version>1.3.1</version>
<build>
<finalName>${project.name}</finalName>
<resources>

View File

@ -12,8 +12,9 @@ import cn.citycraft.LuckLottery.runnable.LotteryReward;
import cn.citycraft.LuckLottery.utils.ChatUtils;
import cn.citycraft.LuckLottery.utils.LotteryUtils;
import cn.citycraft.PluginHelper.config.FileConfig;
import cn.citycraft.PluginHelper.utils.VersionChecker;
import net.milkbowl.vault.economy.Economy;
import pw.yumc.YumCore.statistic.Statistics;
import pw.yumc.YumCore.update.SubscribeTask;
public class LuckLottery extends JavaPlugin {
public static Economy economy = null;
@ -59,7 +60,8 @@ public class LuckLottery extends JavaPlugin {
this.getLogger().info("彩票系统已开启...");
pm.registerEvents(new PlayerListen(this), this);
new LuckLotteryCommand();
new VersionChecker(this);
new Statistics();
new SubscribeTask(true, true);
}
@Override

View File

@ -1,8 +1,6 @@
package cn.citycraft.LuckLottery.config;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
@ -16,7 +14,7 @@ import cn.citycraft.LuckLottery.utils.LotteryUtils;
import cn.citycraft.PluginHelper.config.FileConfig;
public class PlayerData {
public static Map<String, List<List<String>>> playerLottery = new HashMap<String, List<List<String>>>();
public static Map<String, Map<String, Integer>> playerLottery = new HashMap<>();
public static FileConfig playerdata = new FileConfig(LuckLottery.plugin, "playerdate.yml");
public static void addCount() {
@ -25,15 +23,15 @@ public class PlayerData {
playerdata.save();
}
public static List<List<String>> addLottery(final Player player, final List<String> lottery) {
List<List<String>> playerhas = playerLottery.get(player.getName());
public static Map<String, Integer> addLottery(final Player player, final String lottery) {
Map<String, Integer> playerhas = playerLottery.get(player.getName());
if (playerhas == null) {
playerhas = new ArrayList<List<String>>();
playerhas = new HashMap<>();
}
if (!player.isOnline()) {
return playerhas;
}
playerhas.add(lottery);
playerhas.put(lottery, playerhas.containsKey(lottery) ? playerhas.get(lottery) + 1 : 1);
playerLottery.put(player.getName(), playerhas);
playerdata.set(player.getName(), playerhas);
addCount();
@ -41,7 +39,7 @@ public class PlayerData {
}
public static void clearLottery() {
for (final Entry<String, List<List<String>>> pl : playerLottery.entrySet()) {
for (final Entry<String, Map<String, Integer>> pl : playerLottery.entrySet()) {
final String p = pl.getKey();
if (p != null && !"".equals(p)) {
playerdata.set(p, null);
@ -51,15 +49,15 @@ public class PlayerData {
playerdata.save();
}
public static Map<String, List<List<String>>> getPlayerLottery() {
public static Map<String, Map<String, Integer>> getPlayerLottery() {
return playerLottery;
}
@SuppressWarnings("unchecked")
public static void loadPlayerLottery(final Player p) {
final List<?> pl = playerdata.getList(p.getName());
public static void loadPlayerLottery(final String p) {
final Map<String, Integer> pl = (Map<String, Integer>) playerdata.get(p);
if (pl != null) {
playerLottery.put(p.getName(), (List<List<String>>) pl);
playerLottery.put(p, pl);
}
}
@ -67,27 +65,23 @@ public class PlayerData {
int nc = 0;
int np = 0;
int ncc = 0;
for (final List<List<String>> lls : playerLottery.values()) {
nc += lls.size();
np += LotteryUtils.getPrice();
for (final List<String> a : lls) {
ncc += LuckLottery.plugin.getConfig().getInt("Reward." + LotteryUtils.getSameNumber(a), 200);
for (final Map<String, Integer> lls : playerLottery.values()) {
for (final Entry<String, Integer> 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)));
ChatUtils.sendMessage(sender, String.format("§d总计回收金额 §e%s §d元", playerdata.getInt("PriceTotal", 0) - ncc));
}
@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);
}
loadPlayerLottery(p);
}
}
@ -97,9 +91,9 @@ public class PlayerData {
}
public static void saveLottery() {
for (final Entry<String, List<List<String>>> pl : playerLottery.entrySet()) {
for (final Entry<String, Map<String, Integer>> pl : playerLottery.entrySet()) {
final String p = pl.getKey();
final List<List<String>> l = pl.getValue();
final Map<String, Integer> l = pl.getValue();
playerdata.set(p, l);
}
playerdata.save();
@ -108,11 +102,13 @@ public class PlayerData {
public static void showAllPlayerLottery(final CommandSender sender) {
ChatUtils.sendMessage(sender, "§c正在检索玩家彩票数据...");
boolean has = false;
for (final Entry<String, List<List<String>>> phas : playerLottery.entrySet()) {
for (final Entry<String, Map<String, Integer>> phas : playerLottery.entrySet()) {
ChatUtils.sendMessage(sender, "§6玩家: §a" + phas.getKey() + "§6购买的彩票有");
for (final List<String> lry : phas.getValue()) {
final Map<String, Integer> pls = phas.getValue();
for (final String lry : pls.keySet()) {
has = true;
ChatUtils.sendMessage(sender, "§a" + lry.toString());
final int count = pls.get(lry);
ChatUtils.sendMessage(sender, "§a" + lry + (count > 1 ? "*" + count : ""));
}
}
if (!has) {
@ -121,11 +117,12 @@ public class PlayerData {
}
public static void showPlayerLottery(final Player p) {
final List<List<String>> playerhas = playerLottery.get(p.getName());
final Map<String, Integer> 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());
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您当前没有购买的彩票");

View File

@ -22,77 +22,73 @@ import cn.citycraft.LuckLottery.utils.InvUtils;
import cn.citycraft.LuckLottery.utils.LotteryUtils;
public class PlayerListen implements Listener {
LuckLottery plugin;
LuckLottery plugin;
public PlayerListen(final LuckLottery luckLottery) {
this.plugin = luckLottery;
}
public PlayerListen(final LuckLottery luckLottery) {
this.plugin = luckLottery;
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onInventoryClick(final InventoryClickEvent e) {
if (InvUtils.isLotteryGUI(e.getInventory())) {
e.setCancelled(true);
final Player p = (Player) e.getWhoClicked();
final ItemStack ci = e.getCurrentItem();
final Inventory inv = e.getInventory();
final int solt = e.getRawSlot();
if (ci == null || ci.getType() == Material.AIR) {
return;
}
if (!ci.getItemMeta().hasDisplayName()) {
return;
}
final String clickName = ci.getItemMeta().getDisplayName();
switch (clickName) {
case InvUtils.Create:
if (!LuckLottery.economy.hasAccount(p) || !LuckLottery.economy.has(p, LotteryUtils.getPrice())) {
ChatUtils.sendMessage(p, ChatColor.GOLD + "你没有足够的金钱购买彩票,每张彩票" + ChatColor.RED + LotteryUtils.getPrice() + ChatColor.GOLD + "元!");
p.closeInventory();
return;
}
final List<String> lottery = InvUtils.getLotteryNumber(inv);
PlayerData.addLottery(p, lottery);
ChatUtils.sendMessage(p, "§6您当前购买的彩票号码为: " + ChatColor.GREEN + lottery.toString());
ChatUtils.howToShow(p);
LuckLottery.economy.withdrawPlayer(p, LotteryUtils.getPrice());
p.closeInventory();
break;
case InvUtils.ReSet:
inv.setContents(InvUtils.getGui());
break;
default:
if (solt < 36 && InvUtils.isLotteryItem(ci)) {
if (setSelect(inv, ci)) {
inv.setItem(solt, InvUtils.A);
}
}
break;
}
if (InvUtils.selectFinish(inv)) {
inv.setItem(53, InvUtils.Creat);
} else {
inv.setItem(53, InvUtils.CantCreat);
}
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onInventoryClick(final InventoryClickEvent e) {
if (InvUtils.isLotteryGUI(e.getInventory())) {
e.setCancelled(true);
final Player p = (Player) e.getWhoClicked();
final ItemStack ci = e.getCurrentItem();
final Inventory inv = e.getInventory();
final int solt = e.getRawSlot();
if (ci == null || ci.getType() == Material.AIR) {
return;
}
if (!ci.getItemMeta().hasDisplayName()) {
return;
}
final String clickName = ci.getItemMeta().getDisplayName();
switch (clickName) {
case InvUtils.Create:
if (!LuckLottery.economy.hasAccount(p) || !LuckLottery.economy.has(p, LotteryUtils.getPrice())) {
ChatUtils.sendMessage(p, ChatColor.GOLD + "你没有足够的金钱购买彩票,每张彩票" + ChatColor.RED + LotteryUtils.getPrice() + ChatColor.GOLD + "元!");
p.closeInventory();
return;
}
final String lottery = InvUtils.getLotteryNumber(inv);
PlayerData.addLottery(p, lottery);
ChatUtils.sendMessage(p, "§6您当前购买的彩票号码为: " + ChatColor.GREEN + lottery.toString());
ChatUtils.howToShow(p);
LuckLottery.economy.withdrawPlayer(p, LotteryUtils.getPrice());
p.closeInventory();
break;
case InvUtils.ReSet:
inv.setContents(InvUtils.getGui());
break;
default:
if (solt < 36 && InvUtils.isLotteryItem(ci)) {
if (setSelect(inv, ci)) {
inv.setItem(solt, InvUtils.A);
}
}
break;
}
inv.setItem(53, InvUtils.selectFinish(inv) ? InvUtils.Creat : InvUtils.CantCreat);
}
}
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerInteract(final PlayerJoinEvent e) {
final Player p = e.getPlayer();
final List<String> messages = plugin.getOfflineData().getMessage(p.getName());
if (messages != null && !messages.isEmpty()) {
Bukkit.getServer().getScheduler().runTaskLaterAsynchronously(LuckLottery.plugin, new LaterShow(p, messages), 5);
plugin.getOfflineData().clearMessage(p.getName());
}
}
@EventHandler(priority = EventPriority.HIGH)
public void onPlayerInteract(final PlayerJoinEvent e) {
final Player p = e.getPlayer();
final List<String> messages = plugin.getOfflineData().getMessage(p.getName());
if (messages != null && !messages.isEmpty()) {
Bukkit.getServer().getScheduler().runTaskLaterAsynchronously(LuckLottery.plugin, new LaterShow(p, messages), 5);
plugin.getOfflineData().clearMessage(p.getName());
}
}
private boolean setSelect(final Inventory inv, final ItemStack is) {
for (int i = 45; i < 53; i++) {
if (inv.getItem(i) == null) {
inv.setItem(i, is);
return true;
}
}
return false;
}
private boolean setSelect(final Inventory inv, final ItemStack is) {
for (int i = 45; i < 53; i++) {
if (inv.getItem(i) == null) {
inv.setItem(i, is);
return true;
}
}
return false;
}
}

View File

@ -1,6 +1,6 @@
package cn.citycraft.LuckLottery.runnable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
@ -31,14 +31,14 @@ public class LotteryReward implements Runnable {
ChatUtils.broadcastMessage(ChatColor.GOLD + "本轮彩票开奖: " + ChatColor.RED + LotteryUtils.getSystemLottery().toString());
ChatUtils.broadcastMessage(ChatColor.BLUE + "使用命令: " + ChatColor.RED + "/ll" + ChatColor.BLUE + " 或闹钟菜单可以购买彩票!");
int priceTotal = 0;
for (final Entry<String, List<List<String>>> players : PlayerData.playerLottery.entrySet()) {
for (final Entry<String, Map<String, Integer>> players : PlayerData.playerLottery.entrySet()) {
final OfflinePlayer p = Bukkit.getOfflinePlayer(players.getKey());
final List<List<String>> pl = players.getValue();
final Map<String, Integer> pl = players.getValue();
if (pl != null && !pl.isEmpty()) {
for (final List<String> playerlottery : pl) {
for (final String playerlottery : pl.keySet()) {
final int win = LotteryUtils.getSameNumber(playerlottery);
int winprices = 0;
winprices = plugin.getConfig().getInt("Reward." + win, 200);
winprices = plugin.getConfig().getInt("Reward." + win, 200) * pl.get(playerlottery);
if (winprices > 0) {
priceTotal += winprices;
ChatUtils.broadcastMessage(

View File

@ -184,13 +184,14 @@ public class InvUtils {
return null;
}
public static List<String> getLotteryNumber(final Inventory inv) {
final List<String> lty = new ArrayList<String>();
public static String getLotteryNumber(final Inventory inv) {
final StringBuffer lty = new StringBuffer();
for (int i = 45; i < 51; i++) {
lty.add(getItemNumber(inv.getItem(i)));
lty.append(getItemNumber(inv.getItem(i)));
lty.append("-");
}
lty.add(getItemNumber(inv.getItem(52)));
return lty;
lty.append(getItemNumber(inv.getItem(52)));
return lty.toString();
}
public static boolean isLotteryGUI(final Inventory inv) {

View File

@ -1,6 +1,7 @@
package cn.citycraft.LuckLottery.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
@ -67,6 +68,10 @@ public class LotteryUtils {
return samenum;
}
public static int getSameNumber(final String lottery) {
return getSameNumber(Arrays.asList(lottery.split("-")), systemLottery);
}
public static List<String> getSystemLottery() {
return systemLottery;
}