Search in sources :

Example 1 with StartMinigameEvent

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

the class PlayerData method startMPMinigame.

public void startMPMinigame(Minigame minigame, boolean teleport) {
    List<MinigamePlayer> players = new ArrayList<MinigamePlayer>();
    players.addAll(minigame.getPlayers());
    Collections.shuffle(players);
    if (minigame.isTeamGame() && GameMechanics.getGameMechanic(minigame.getMechanicName()) != null) {
        GameMechanics.getGameMechanic(minigame.getMechanicName()).balanceTeam(players, minigame);
    }
    Location start = null;
    int pos = 0;
    Map<Team, Integer> tpos = new HashMap<Team, Integer>();
    for (Team t : TeamsModule.getMinigameModule(minigame).getTeams()) {
        tpos.put(t, 0);
    }
    for (MinigamePlayer ply : players) {
        if (!minigame.isTeamGame()) {
            if (pos < minigame.getStartLocations().size()) {
                ply.setStartTime(Calendar.getInstance().getTimeInMillis());
                if (teleport) {
                    start = minigame.getStartLocations().get(pos);
                }
            } else {
                pos = 0;
                if (!minigame.getStartLocations().isEmpty()) {
                    if (teleport) {
                        start = minigame.getStartLocations().get(0);
                    }
                } else {
                    ply.sendMessage(MinigameUtils.getLang("minigame.error.incorrectStart"), "error");
                    quitMinigame(ply, false);
                }
            }
            ply.setCheckpoint(start);
        } else {
            Team team = ply.getTeam();
            if (TeamsModule.getMinigameModule(minigame).hasTeamStartLocations()) {
                if (tpos.get(team) >= team.getStartLocations().size()) {
                    tpos.put(team, 0);
                }
                start = team.getStartLocations().get(tpos.get(team));
                tpos.put(team, tpos.get(team) + 1);
            } else {
                if (pos < minigame.getStartLocations().size()) {
                    if (teleport) {
                        start = minigame.getStartLocations().get(pos);
                    }
                } else {
                    pos = 0;
                    if (!minigame.getStartLocations().isEmpty()) {
                        if (teleport) {
                            start = minigame.getStartLocations().get(0);
                        }
                    } else {
                        ply.sendMessage(MinigameUtils.getLang("minigame.error.incorrectStart"), "error");
                        quitMinigame(ply, false);
                    }
                }
            }
            if (minigame.getLives() > 0) {
                ply.sendMessage(MinigameUtils.formStr("minigame.livesLeft", minigame.getLives()), null);
            }
        }
        if (start != null) {
            if (teleport) {
                ply.teleport(start);
                ply.setCheckpoint(start);
            }
            if (minigame.getMaxScore() != 0) {
                ply.sendMessage(MinigameUtils.formStr("minigame.scoreToWin", minigame.getMaxScorePerPlayer()), null);
            }
        }
        pos++;
        ply.getLoadout().equiptLoadout(ply);
        if (minigame.isAllowedFlight()) {
            ply.setCanFly(true);
            if (minigame.isFlightEnabled())
                ply.getPlayer().setFlying(true);
        }
        PlayMGSound.playSound(ply, MGSounds.getSound("gameStart"));
    }
    Bukkit.getServer().getPluginManager().callEvent(new StartMinigameEvent(players, minigame, teleport));
    minigame.setState(MinigameState.STARTED);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StartMinigameEvent(au.com.mineauz.minigames.events.StartMinigameEvent) Team(au.com.mineauz.minigames.minigame.Team) Location(org.bukkit.Location)

Aggregations

StartMinigameEvent (au.com.mineauz.minigames.events.StartMinigameEvent)1 Team (au.com.mineauz.minigames.minigame.Team)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Location (org.bukkit.Location)1