Search in sources :

Example 1 with EndMinigameEvent

use of au.com.mineauz.minigames.events.EndMinigameEvent in project Minigames by AddstarMC.

the class PlayerData method endMinigame.

public void endMinigame(Minigame minigame, List<MinigamePlayer> winners, List<MinigamePlayer> losers) {
    EndMinigameEvent event = new EndMinigameEvent(winners, losers, minigame);
    Bukkit.getServer().getPluginManager().callEvent(event);
    if (!event.isCancelled()) {
        winners = event.getWinners();
        losers = event.getLosers();
        //Call Mechanics End
        minigame.getMechanic().endMinigame(minigame, winners, losers);
        //Prepare split money
        double bets = 0;
        if (minigame.getMpBets() != null) {
            if (minigame.getMpBets().hasMoneyBets()) {
                List<MinigamePlayer> plys = new ArrayList<MinigamePlayer>();
                plys.addAll(event.getWinners());
                if (!plys.isEmpty()) {
                    bets = minigame.getMpBets().claimMoneyBets() / (double) plys.size();
                    BigDecimal roundBets = new BigDecimal(bets);
                    roundBets = roundBets.setScale(2, BigDecimal.ROUND_HALF_UP);
                    bets = roundBets.doubleValue();
                }
                minigame.setMpBets(null);
            }
        }
        //Broadcast Message
        if (plugin.getConfig().getBoolean("broadcastCompletion") && minigame.isEnabled()) {
            if (minigame.isTeamGame()) {
                if (winners.size() > 0 || ((TeamsModule) minigame.getModule("Teams")).getDefaultWinner() != null) {
                    Team team;
                    if (winners.size() > 0)
                        team = winners.get(0).getTeam();
                    else
                        team = ((TeamsModule) minigame.getModule("Teams")).getTeam(((TeamsModule) minigame.getModule("Teams")).getDefaultWinner());
                    String score = "";
                    List<Team> teams = TeamsModule.getMinigameModule(minigame).getTeams();
                    for (Team t : teams) {
                        score += t.getColor().getColor().toString() + t.getScore();
                        if (t != teams.get(teams.size() - 1)) {
                            score += ChatColor.WHITE + " : ";
                        }
                    }
                    String nscore = ", " + MinigameUtils.formStr("player.end.team.score", score);
                    if (team.getScore() > 0) {
                        MinigameUtils.broadcast(MinigameUtils.formStr("player.end.team.win", team.getChatColor() + team.getDisplayName() + ChatColor.WHITE, minigame.getName(true)) + nscore, minigame, ChatColor.GREEN);
                    } else {
                        MinigameUtils.broadcast(MinigameUtils.formStr("player.end.team.win", team.getChatColor() + team.getDisplayName() + ChatColor.WHITE, minigame.getName(true)), minigame, ChatColor.GREEN);
                    }
                } else {
                    MinigameUtils.broadcast(MinigameUtils.formStr("player.end.broadcastNobodyWon", minigame.getName(true)), minigame, ChatColor.RED);
                }
            } else {
                if (winners.size() == 1) {
                    String score = "";
                    if (winners.get(0).getScore() != 0)
                        score = MinigameUtils.formStr("player.end.broadcastScore", winners.get(0).getScore());
                    MinigameUtils.broadcast(MinigameUtils.formStr("player.end.broadcastMsg", winners.get(0).getDisplayName(minigame.usePlayerDisplayNames()), minigame.getName(true)) + ". " + score, minigame, ChatColor.GREEN);
                } else if (winners.size() > 1) {
                    String win = "";
                    Collections.sort(winners, new Comparator<MinigamePlayer>() {

                        @Override
                        public int compare(MinigamePlayer o1, MinigamePlayer o2) {
                            return Integer.valueOf(o1.getScore()).compareTo(o2.getScore());
                        }
                    });
                    for (MinigamePlayer pl : winners) {
                        if (winners.indexOf(pl) < 2) {
                            win += pl.getDisplayName(minigame.usePlayerDisplayNames());
                            if (winners.indexOf(pl) + 2 >= winners.size()) {
                                win += " and ";
                            } else {
                                win += ", ";
                            }
                        } else {
                            win += String.valueOf(winners.size() - 3) + " others";
                        }
                    }
                    MinigameUtils.broadcast(MinigameUtils.formStr("player.end.broadcastMsg", win, minigame.getName(true)) + ". ", minigame, ChatColor.GREEN);
                } else {
                    MinigameUtils.broadcast(MinigameUtils.formStr("player.end.broadcastNobodyWon", minigame.getName(true)), minigame, ChatColor.RED);
                }
            }
        }
        GameOverModule gom = GameOverModule.getMinigameModule(minigame);
        boolean usedTimer = false;
        gom.setWinners(winners);
        gom.setLosers(losers);
        if (gom.getTimer() > 0 && minigame.getType() == MinigameType.MULTIPLAYER) {
            gom.startEndGameTimer();
            usedTimer = true;
        }
        for (MinigamePlayer player : losers) {
            player.setEndTime(System.currentTimeMillis());
            if (!usedTimer)
                quitMinigame(player, true);
            PlayMGSound.playSound(player, MGSounds.getSound("lose"));
        }
        for (MinigamePlayer player : winners) {
            player.setEndTime(System.currentTimeMillis());
            StoredGameStats saveData = new StoredGameStats(minigame, player);
            saveData.addStat(MinigameStats.Attempts, 1);
            saveData.addStat(MinigameStats.Wins, 1);
            saveData.addStat(MinigameStats.Kills, player.getKills());
            saveData.addStat(MinigameStats.Deaths, player.getDeaths());
            saveData.addStat(MinigameStats.Score, player.getScore());
            saveData.addStat(MinigameStats.Reverts, player.getReverts());
            saveData.addStat(MinigameStats.CompletionTime, player.getEndTime() - player.getStartTime() + player.getStoredTime());
            for (DynamicMinigameStat stat : MinigameStats.getDynamicStats()) {
                if (stat.doesApply(minigame, player, true)) {
                    saveData.addStat(stat, stat.getValue(minigame, player, true));
                }
            }
            saveData.applySettings(minigame.getStatSettings(saveData));
            if (!usedTimer)
                quitMinigame(player, true);
            //Group money bets
            if (bets != 0) {
                plugin.getEconomy().depositPlayer(player.getPlayer().getPlayer(), bets);
                player.sendMessage(MinigameUtils.formStr("player.bet.winMoney", Minigames.plugin.getEconomy().format(bets)), null);
            }
            // Record player completion and give rewards
            if (minigame.isEnabled()) {
                plugin.queueStatSave(saveData, true);
            } else {
                MinigameUtils.debugMessage("Skipping SQL data save for " + saveData + "; minigame is disabled");
            }
            //Item Bets (for non groups)
            if (minigame.getMpBets() != null) {
                if (minigame.getMpBets().hasBets()) {
                    if (!player.isInMinigame())
                        player.getPlayer().getInventory().addItem(minigame.getMpBets().claimBets());
                    else {
                        for (ItemStack i : minigame.getMpBets().claimBets()) {
                            player.addTempRewardItem(i);
                        }
                    }
                    minigame.setMpBets(null);
                }
            }
            PlayMGSound.playSound(player, MGSounds.getSound("win"));
        }
        if (!usedTimer) {
            gom.clearLosers();
            gom.clearWinners();
        }
        mdata.clearClaimedScore(minigame);
        //Call Types End.
        mdata.minigameType(minigame.getType()).endMinigame(winners, losers, minigame);
        minigame.getScoreboardData().reload();
    }
}
Also used : StoredGameStats(au.com.mineauz.minigames.stats.StoredGameStats) ArrayList(java.util.ArrayList) DynamicMinigameStat(au.com.mineauz.minigames.stats.DynamicMinigameStat) TeamsModule(au.com.mineauz.minigames.minigame.modules.TeamsModule) BigDecimal(java.math.BigDecimal) Comparator(java.util.Comparator) EndMinigameEvent(au.com.mineauz.minigames.events.EndMinigameEvent) Team(au.com.mineauz.minigames.minigame.Team) GameOverModule(au.com.mineauz.minigames.minigame.modules.GameOverModule) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

EndMinigameEvent (au.com.mineauz.minigames.events.EndMinigameEvent)1 Team (au.com.mineauz.minigames.minigame.Team)1 GameOverModule (au.com.mineauz.minigames.minigame.modules.GameOverModule)1 TeamsModule (au.com.mineauz.minigames.minigame.modules.TeamsModule)1 DynamicMinigameStat (au.com.mineauz.minigames.stats.DynamicMinigameStat)1 StoredGameStats (au.com.mineauz.minigames.stats.StoredGameStats)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 ItemStack (org.bukkit.inventory.ItemStack)1