Search in sources :

Example 1 with VictoryCondition

use of com.voxelgameslib.voxelgameslib.condition.VictoryCondition in project VoxelGamesLibv2 by VoxelGamesLib.

the class AbstractPhase method enable.

@Override
public void enable() {
    if (!checkDependencies()) {
        game.abortGame();
        return;
    }
    if (!checkVictoryConditionDependencies()) {
        game.abortGame();
        return;
    }
    if (victoryConditions.size() == 0) {
        addVictoryCondition(getGame().createVictoryCondition(EmptyVictoryCondition.class, this));
    }
    // enable timer
    startTime = LocalDateTime.now();
    log.finer("enable phase" + getName());
    phaseTickables.values().forEach(Tickable::enable);
    for (Feature feature : features) {
        if (game.isAborting()) {
            return;
        }
        log.finer("enable " + feature.getName());
        try {
            feature.enable();
        } catch (Exception ex) {
            log.severe("error while starting " + feature.getName());
            ex.printStackTrace();
            game.abortGame();
            return;
        }
        if (feature instanceof Listener) {
            eventHandler.registerEvents((Listener) feature, getGame());
        }
        if (feature instanceof FeatureCommandImplementor) {
            AbstractFeatureCommand cmd = injector.getInstance(((FeatureCommandImplementor) feature).getCommandClass());
            // noinspection unchecked
            cmd.setFeature(feature);
            commandHandler.register(cmd, this);
        }
        startedFeatures.add(feature);
    }
    for (VictoryCondition victoryCondition : victoryConditions) {
        if (victoryCondition instanceof Listener) {
            eventHandler.registerEvents((Listener) victoryCondition, getGame());
        }
    }
}
Also used : EmptyVictoryCondition(com.voxelgameslib.voxelgameslib.condition.conditions.EmptyVictoryCondition) Listener(org.bukkit.event.Listener) Tickable(com.voxelgameslib.voxelgameslib.tick.Tickable) FeatureCommandImplementor(com.voxelgameslib.voxelgameslib.feature.FeatureCommandImplementor) VictoryCondition(com.voxelgameslib.voxelgameslib.condition.VictoryCondition) EmptyVictoryCondition(com.voxelgameslib.voxelgameslib.condition.conditions.EmptyVictoryCondition) Feature(com.voxelgameslib.voxelgameslib.feature.Feature) VoxelGameLibException(com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException) NoSuchFeatureException(com.voxelgameslib.voxelgameslib.exception.NoSuchFeatureException) DependencyGraphException(com.voxelgameslib.voxelgameslib.exception.DependencyGraphException) AbstractFeatureCommand(com.voxelgameslib.voxelgameslib.feature.AbstractFeatureCommand)

Example 2 with VictoryCondition

use of com.voxelgameslib.voxelgameslib.condition.VictoryCondition in project VoxelGamesLibv2 by VoxelGamesLib.

the class AbstractPhase method disable.

@Override
public void disable() {
    // disable timer
    duration = Duration.between(startTime, LocalDateTime.now());
    log.finer("disable phase " + getName());
    // only disable features that have been started to avoid errors
    for (Feature feature : startedFeatures) {
        log.finer("disable " + feature.getName());
        try {
            feature.disable();
        } catch (Exception ex) {
            log.severe("error while stopping " + feature.getName());
            ex.printStackTrace();
            return;
        }
        if (feature instanceof Listener) {
            eventHandler.unregister((Listener) feature, getGame());
        }
        if (feature instanceof FeatureCommandImplementor) {
            AbstractFeatureCommand cmd = injector.getInstance(((FeatureCommandImplementor) feature).getCommandClass());
            commandHandler.unregister(cmd, this);
        }
    }
    for (VictoryCondition victoryCondition : victoryConditions) {
        if (victoryCondition instanceof Listener) {
            eventHandler.registerEvents((Listener) victoryCondition, getGame());
        }
    }
    phaseTickables.values().forEach(tickable -> {
        tickable.disable();
        if (tickable instanceof Ability) {
            ((Ability) tickable).unregister();
        }
    });
    startedFeatures.clear();
}
Also used : Ability(com.voxelgameslib.voxelgameslib.components.ability.Ability) Listener(org.bukkit.event.Listener) FeatureCommandImplementor(com.voxelgameslib.voxelgameslib.feature.FeatureCommandImplementor) VictoryCondition(com.voxelgameslib.voxelgameslib.condition.VictoryCondition) EmptyVictoryCondition(com.voxelgameslib.voxelgameslib.condition.conditions.EmptyVictoryCondition) Feature(com.voxelgameslib.voxelgameslib.feature.Feature) VoxelGameLibException(com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException) NoSuchFeatureException(com.voxelgameslib.voxelgameslib.exception.NoSuchFeatureException) DependencyGraphException(com.voxelgameslib.voxelgameslib.exception.DependencyGraphException) AbstractFeatureCommand(com.voxelgameslib.voxelgameslib.feature.AbstractFeatureCommand)

Example 3 with VictoryCondition

use of com.voxelgameslib.voxelgameslib.condition.VictoryCondition in project VoxelGamesLibv2 by VoxelGamesLib.

the class AbstractPhase method checkEnd.

private void checkEnd() {
    // check all victory conditions
    User winner = null;
    Team winnerTeam = null;
    for (VictoryCondition victoryCondition : victoryConditions) {
        if (!victoryCondition.completed()) {
            return;
        }
        if (victoryCondition.getWinner() != null) {
            if (!victoryCondition.getWinner().equals(winner)) {
                if (winner == null) {
                    if (winnerTeam != null && !winnerTeam.contains(victoryCondition.getWinner())) {
                        throw new VoxelGameLibException(victoryCondition.getName() + " defined a winner even tho we already have a winning team!");
                    }
                    winner = victoryCondition.getWinner();
                } else {
                    throw new VoxelGameLibException(victoryCondition.getName() + " defined a different winner than one of the conditions before it!");
                }
            }
        }
        if (victoryCondition.getWinnerTeam() != null) {
            if (!victoryCondition.getWinnerTeam().equals(winnerTeam)) {
                if (winnerTeam == null) {
                    if (winner != null && !victoryCondition.getWinnerTeam().contains(winner)) {
                        throw new VoxelGameLibException(victoryCondition.getName() + " defined a winning team even tho we already have a winning user!");
                    } else {
                        winnerTeam = victoryCondition.getWinnerTeam();
                    }
                } else {
                    throw new VoxelGameLibException(victoryCondition.getName() + " defined a different winning team than one of the conditions before it!");
                }
            }
        }
    }
    // all done, end this game
    getGame().endGame(winnerTeam, winner);
}
Also used : User(com.voxelgameslib.voxelgameslib.user.User) VoxelGameLibException(com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException) Team(com.voxelgameslib.voxelgameslib.components.team.Team) VictoryCondition(com.voxelgameslib.voxelgameslib.condition.VictoryCondition) EmptyVictoryCondition(com.voxelgameslib.voxelgameslib.condition.conditions.EmptyVictoryCondition)

Aggregations

VictoryCondition (com.voxelgameslib.voxelgameslib.condition.VictoryCondition)3 EmptyVictoryCondition (com.voxelgameslib.voxelgameslib.condition.conditions.EmptyVictoryCondition)3 VoxelGameLibException (com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException)3 DependencyGraphException (com.voxelgameslib.voxelgameslib.exception.DependencyGraphException)2 NoSuchFeatureException (com.voxelgameslib.voxelgameslib.exception.NoSuchFeatureException)2 AbstractFeatureCommand (com.voxelgameslib.voxelgameslib.feature.AbstractFeatureCommand)2 Feature (com.voxelgameslib.voxelgameslib.feature.Feature)2 FeatureCommandImplementor (com.voxelgameslib.voxelgameslib.feature.FeatureCommandImplementor)2 Listener (org.bukkit.event.Listener)2 Ability (com.voxelgameslib.voxelgameslib.components.ability.Ability)1 Team (com.voxelgameslib.voxelgameslib.components.team.Team)1 Tickable (com.voxelgameslib.voxelgameslib.tick.Tickable)1 User (com.voxelgameslib.voxelgameslib.user.User)1