Search in sources :

Example 1 with DynamicMinigameStat

use of au.com.mineauz.minigames.stats.DynamicMinigameStat 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)

Example 2 with DynamicMinigameStat

use of au.com.mineauz.minigames.stats.DynamicMinigameStat in project Minigames by AddstarMC.

the class PlayerData method quitMinigame.

public void quitMinigame(MinigamePlayer player, boolean forced) {
    Minigame minigame = player.getMinigame();
    boolean isWinner = false;
    if (GameOverModule.getMinigameModule(minigame).getWinners().contains(player))
        isWinner = true;
    QuitMinigameEvent event = new QuitMinigameEvent(player, minigame, forced, isWinner);
    Bukkit.getServer().getPluginManager().callEvent(event);
    if (!event.isCancelled()) {
        if (!minigame.isSpectator(player)) {
            if (player.getEndTime() == 0)
                player.setEndTime(System.currentTimeMillis());
            if (isWinner)
                GameOverModule.getMinigameModule(minigame).getWinners().remove(player);
            else
                GameOverModule.getMinigameModule(minigame).getLosers().remove(player);
            if (!isWinner) {
                if (!minigame.canSaveCheckpoint() && minigame.isEnabled()) {
                    StoredGameStats saveData = new StoredGameStats(minigame, player);
                    saveData.addStat(MinigameStats.Attempts, 1);
                    for (DynamicMinigameStat stat : MinigameStats.getDynamicStats()) {
                        if (stat.doesApply(minigame, player, false)) {
                            saveData.addStat(stat, stat.getValue(minigame, player, false));
                        }
                    }
                    saveData.applySettings(minigame.getStatSettings(saveData));
                    plugin.queueStatSave(saveData, false);
                }
            }
            //Call Types quit.
            mdata.minigameType(minigame.getType()).quitMinigame(player, minigame, forced);
            //Call Mechanic quit.
            minigame.getMechanic().quitMinigame(minigame, player, forced);
            //Prepare player for quit
            if (player.getPlayer().getVehicle() != null) {
                Vehicle vehicle = (Vehicle) player.getPlayer().getVehicle();
                vehicle.eject();
            }
            player.getPlayer().closeInventory();
            if (player.getLoadout() != null) {
                player.getLoadout().removeLoadout(player);
            }
            player.removeMinigame();
            minigame.removePlayer(player);
            for (PotionEffect potion : player.getPlayer().getActivePotionEffects()) {
                player.getPlayer().removePotionEffect(potion.getType());
            }
            player.getPlayer().setFallDistance(0);
            player.getPlayer().setNoDamageTicks(60);
            final MinigamePlayer fplayer = player;
            Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

                @Override
                public void run() {
                    fplayer.getPlayer().setFireTicks(0);
                }
            });
            player.resetAllStats();
            if (!player.isDead()) {
                player.restorePlayerData();
                if (!isWinner)
                    player.teleport(minigame.getQuitPosition());
                else
                    player.teleport(minigame.getEndPosition());
            } else {
                if (!isWinner)
                    player.setQuitPos(minigame.getQuitPosition());
                else
                    player.setQuitPos(minigame.getEndPosition());
                player.setRequiredQuit(true);
            }
            //Reward Player
            if (isWinner) {
                player.claimTempRewardItems();
            }
            player.claimRewards();
            //Reset Minigame
            if (minigame.getPlayers().size() == 0) {
                if (minigame.getMinigameTimer() != null) {
                    minigame.getMinigameTimer().stopTimer();
                    minigame.setMinigameTimer(null);
                }
                if (minigame.getFloorDegenerator() != null) {
                    minigame.getFloorDegenerator().stopDegenerator();
                }
                minigame.setState(MinigameState.IDLE);
                if (minigame.getBlockRecorder().hasData()) {
                    minigame.getBlockRecorder().restoreBlocks();
                    minigame.getBlockRecorder().restoreEntities();
                    minigame.getBlockRecorder().setCreatedRegenBlocks(false);
                }
                if (minigame.getMpTimer() != null) {
                    minigame.getMpTimer().pauseTimer();
                    minigame.getMpTimer().removeTimer();
                    minigame.setMpTimer(null);
                }
                if (minigame.getMpBets() != null) {
                    minigame.setMpBets(null);
                }
                mdata.clearClaimedScore(minigame);
                WeatherTimeModule.getMinigameModule(minigame).stopTimeLoop();
                GameOverModule.getMinigameModule(minigame).stopEndGameTimer();
                for (Team team : TeamsModule.getMinigameModule(minigame).getTeams()) {
                    team.setScore(0);
                }
            }
            minigame.getScoreboardManager().resetScores(player.getName());
            for (MinigamePlayer pl : minigame.getSpectators()) {
                player.getPlayer().showPlayer(pl.getPlayer());
            }
            if (minigame.getPlayers().size() == 0 && !minigame.isRegenerating()) {
                HandlerList.unregisterAll(minigame.getBlockRecorder());
            }
            //Send out messages
            if (!forced) {
                mdata.sendMinigameMessage(minigame, MinigameUtils.formStr("player.quit.plyMsg", player.getName(), minigame.getName(true)), "error", player);
            }
            plugin.getLogger().info(player.getName() + " quit " + minigame);
            player.updateInventory();
        } else {
            if (player.getPlayer().getVehicle() != null) {
                Vehicle vehicle = (Vehicle) player.getPlayer().getVehicle();
                vehicle.eject();
            }
            player.getPlayer().setFallDistance(0);
            player.getPlayer().setNoDamageTicks(60);
            final Player fplayer = player.getPlayer();
            for (PotionEffect potion : player.getPlayer().getActivePotionEffects()) {
                player.getPlayer().removePotionEffect(potion.getType());
            }
            Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

                @Override
                public void run() {
                    fplayer.setFireTicks(0);
                }
            });
            player.getPlayer().closeInventory();
            if (!player.isDead()) {
                player.restorePlayerData();
            }
            player.teleport(minigame.getQuitPosition());
            player.removeMinigame();
            minigame.removeSpectator(player);
            for (MinigamePlayer pl : minigame.getPlayers()) {
                pl.getPlayer().showPlayer(player.getPlayer());
            }
            player.sendMessage(MinigameUtils.formStr("player.spectate.quit.plyMsg", minigame.getName(true)), "error");
            mdata.sendMinigameMessage(minigame, MinigameUtils.formStr("player.spectate.quit.minigameMsg", player.getName(), minigame.getName(true)), "error", player);
        }
        if (player.getPlayer().getGameMode() != GameMode.CREATIVE)
            player.setCanFly(false);
        if (!forced) {
            minigame.getScoreboardData().reload();
        }
    }
}
Also used : Vehicle(org.bukkit.entity.Vehicle) QuitMinigameEvent(au.com.mineauz.minigames.events.QuitMinigameEvent) Player(org.bukkit.entity.Player) StoredGameStats(au.com.mineauz.minigames.stats.StoredGameStats) PotionEffect(org.bukkit.potion.PotionEffect) Team(au.com.mineauz.minigames.minigame.Team) Minigame(au.com.mineauz.minigames.minigame.Minigame) DynamicMinigameStat(au.com.mineauz.minigames.stats.DynamicMinigameStat)

Aggregations

Team (au.com.mineauz.minigames.minigame.Team)2 DynamicMinigameStat (au.com.mineauz.minigames.stats.DynamicMinigameStat)2 StoredGameStats (au.com.mineauz.minigames.stats.StoredGameStats)2 EndMinigameEvent (au.com.mineauz.minigames.events.EndMinigameEvent)1 QuitMinigameEvent (au.com.mineauz.minigames.events.QuitMinigameEvent)1 Minigame (au.com.mineauz.minigames.minigame.Minigame)1 GameOverModule (au.com.mineauz.minigames.minigame.modules.GameOverModule)1 TeamsModule (au.com.mineauz.minigames.minigame.modules.TeamsModule)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 Player (org.bukkit.entity.Player)1 Vehicle (org.bukkit.entity.Vehicle)1 ItemStack (org.bukkit.inventory.ItemStack)1 PotionEffect (org.bukkit.potion.PotionEffect)1