Search in sources :

Example 1 with QuitMinigameEvent

use of au.com.mineauz.minigames.events.QuitMinigameEvent 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

QuitMinigameEvent (au.com.mineauz.minigames.events.QuitMinigameEvent)1 Minigame (au.com.mineauz.minigames.minigame.Minigame)1 Team (au.com.mineauz.minigames.minigame.Team)1 DynamicMinigameStat (au.com.mineauz.minigames.stats.DynamicMinigameStat)1 StoredGameStats (au.com.mineauz.minigames.stats.StoredGameStats)1 Player (org.bukkit.entity.Player)1 Vehicle (org.bukkit.entity.Vehicle)1 PotionEffect (org.bukkit.potion.PotionEffect)1