Search in sources :

Example 1 with Feature

use of com.voxelgameslib.voxelgameslib.api.feature.Feature 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.api.feature.FeatureCommandImplementor) EmptyVictoryCondition(com.voxelgameslib.voxelgameslib.api.condition.conditions.EmptyVictoryCondition) VictoryCondition(com.voxelgameslib.voxelgameslib.api.condition.VictoryCondition) SpectatorFeature(com.voxelgameslib.voxelgameslib.api.feature.features.SpectatorFeature) Feature(com.voxelgameslib.voxelgameslib.api.feature.Feature) NoSuchFeatureException(com.voxelgameslib.voxelgameslib.api.exception.NoSuchFeatureException) VoxelGameLibException(com.voxelgameslib.voxelgameslib.api.exception.VoxelGameLibException) DependencyGraphException(com.voxelgameslib.voxelgameslib.api.exception.DependencyGraphException) AbstractFeatureCommand(com.voxelgameslib.voxelgameslib.api.feature.AbstractFeatureCommand)

Example 2 with Feature

use of com.voxelgameslib.voxelgameslib.api.feature.Feature in project VoxelGamesLibv2 by VoxelGamesLib.

the class AbstractPhase method tick.

@Override
public void tick() {
    phaseTiming.startTiming();
    for (Feature feature : features) {
        MCTiming timing = timingManager.ofStart("Phase::Tickables::" + getName() + "::" + feature.getName(), phaseTiming);
        feature.tick();
        timing.stopTiming();
    }
    for (Tickable tickable : phaseTickables.values()) {
        MCTiming timing = timingManager.ofStart("Phase::Tickables::" + getName() + "::" + tickable.getClass().getSimpleName(), phaseTiming);
        tickable.tick();
        timing.stopTiming();
    }
    phaseTiming.stopTiming();
    checkEnd();
}
Also used : Tickable(com.voxelgameslib.voxelgameslib.api.tick.Tickable) SpectatorFeature(com.voxelgameslib.voxelgameslib.api.feature.features.SpectatorFeature) Feature(com.voxelgameslib.voxelgameslib.api.feature.Feature) MCTiming(co.aikar.commands.lib.timings.MCTiming)

Example 3 with Feature

use of com.voxelgameslib.voxelgameslib.api.feature.Feature in project VoxelGamesLibv2 by VoxelGamesLib.

the class AbstractPhase method enable.

@Override
public void enable() {
    phaseTiming = timingManager.of("Phase::Tickables::" + getName());
    if (!checkDependencies()) {
        game.abortGame();
        return;
    }
    if (!checkVictoryConditionDependencies()) {
        game.abortGame();
        return;
    }
    if (victoryConditions.size() == 0) {
        addVictoryCondition(getGame().createVictoryCondition(EmptyVictoryCondition.class, this));
    }
    // check for spec feature
    if (allowSpectate && !getOptionalFeature(SpectatorFeature.class).isPresent()) {
        log.warning(getName() + " does allow spectators but doesn't use the spectator feature! Did you forget to add it?");
    }
    // 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.api.condition.conditions.EmptyVictoryCondition) Listener(org.bukkit.event.Listener) Tickable(com.voxelgameslib.voxelgameslib.api.tick.Tickable) FeatureCommandImplementor(com.voxelgameslib.voxelgameslib.api.feature.FeatureCommandImplementor) SpectatorFeature(com.voxelgameslib.voxelgameslib.api.feature.features.SpectatorFeature) EmptyVictoryCondition(com.voxelgameslib.voxelgameslib.api.condition.conditions.EmptyVictoryCondition) VictoryCondition(com.voxelgameslib.voxelgameslib.api.condition.VictoryCondition) SpectatorFeature(com.voxelgameslib.voxelgameslib.api.feature.features.SpectatorFeature) Feature(com.voxelgameslib.voxelgameslib.api.feature.Feature) NoSuchFeatureException(com.voxelgameslib.voxelgameslib.api.exception.NoSuchFeatureException) VoxelGameLibException(com.voxelgameslib.voxelgameslib.api.exception.VoxelGameLibException) DependencyGraphException(com.voxelgameslib.voxelgameslib.api.exception.DependencyGraphException) AbstractFeatureCommand(com.voxelgameslib.voxelgameslib.api.feature.AbstractFeatureCommand)

Example 4 with Feature

use of com.voxelgameslib.voxelgameslib.api.feature.Feature in project VoxelGamesLibv2 by VoxelGamesLib.

the class AbstractPhase method checkDependencies.

private boolean checkDependencies() {
    List<Class<? extends Feature>> orderedFeatures = new ArrayList<>();
    List<Class<? extends Feature>> added = new ArrayList<>();
    List<Class<? extends Feature>> missingSoftDependencies = new ArrayList<>();
    try {
        Graph<Class<? extends Feature>> graph = new Graph<>(orderedFeatures::add);
        // add all dependencies to the graph
        for (Feature feature : getFeatures()) {
            for (Class<? extends Feature> dependency : feature.getDependencies()) {
                if (dependency.equals(feature.getClass())) {
                    log.severe(feature.getName() + " tried to depend on itself...");
                    continue;
                }
                graph.addDependency(feature.getClass(), dependency);
                added.add(feature.getClass());
                added.add(dependency);
                try {
                    getFeature(dependency);
                } catch (NoSuchFeatureException ex) {
                    log.severe("could not find dependency " + dependency.getName() + " for feature " + feature.getClass().getName() + " in phase " + getName());
                    return false;
                }
            }
            for (Class<? extends Feature> dependency : feature.getSoftDependencies()) {
                if (dependency.equals(feature.getClass())) {
                    log.severe(feature.getName() + " tried to depend on itself...");
                    continue;
                }
                graph.addDependency(feature.getClass(), dependency);
                added.add(feature.getClass());
                added.add(dependency);
                try {
                    getFeature(dependency);
                } catch (NoSuchFeatureException ex) {
                    missingSoftDependencies.add(dependency);
                }
            }
        }
        // add features that have no dependency connection to any other feature. they can't be left out alone!
        for (Feature feature : getFeatures()) {
            if (!added.contains(feature.getClass())) {
                orderedFeatures.add(feature.getClass());
            }
        }
        added.clear();
        if (graph.size() != 0) {
            // do the magic! (but only if there are actually nodes on the graph)
            graph.generateDependencies();
        }
    } catch (DependencyGraphException ex) {
        log.severe("error while trying to generate dependency graph: " + ex.getMessage());
        ex.printStackTrace();
        return false;
    }
    // no need to keep stuff that isn't present
    orderedFeatures.removeAll(missingSoftDependencies);
    if (features.size() != orderedFeatures.size()) {
        throw new RuntimeException("WTF HAPPENED HERE?!" + features.size() + " " + orderedFeatures.size());
    }
    // reverse order because dependencies need to be run before dependend features
    Collections.reverse(orderedFeatures);
    // remap classes to features
    features = orderedFeatures.stream().map((Function<Class, Feature>) this::getFeature).collect(Collectors.toList());
    return true;
}
Also used : Function(java.util.function.Function) Graph(com.voxelgameslib.voxelgameslib.internal.graph.Graph) NoSuchFeatureException(com.voxelgameslib.voxelgameslib.api.exception.NoSuchFeatureException) DependencyGraphException(com.voxelgameslib.voxelgameslib.api.exception.DependencyGraphException) ArrayList(java.util.ArrayList) SpectatorFeature(com.voxelgameslib.voxelgameslib.api.feature.features.SpectatorFeature) Feature(com.voxelgameslib.voxelgameslib.api.feature.Feature)

Example 5 with Feature

use of com.voxelgameslib.voxelgameslib.api.feature.Feature in project VoxelGamesLibv2 by VoxelGamesLib.

the class AbstractGame method initGameFromDefinition.

@Override
public void initGameFromDefinition(@Nonnull GameDefinition gameDefinition) {
    setMaxPlayers(gameDefinition.getMaxPlayers());
    setMinPlayers(gameDefinition.getMinPlayers());
    activePhase = gameDefinition.getPhases().get(0);
    gameData = gameDefinition.getGameData();
    // fix stuff
    for (int i = 0; i < gameDefinition.getPhases().size(); i++) {
        Phase nextPhase;
        if (gameDefinition.getPhases().size() > i + 1) {
            nextPhase = gameDefinition.getPhases().get(i + 1);
        } else {
            log.severe("Couldn't fix next phase for phase " + gameDefinition.getPhases().get(i).getName());
            return;
        }
        Phase currPhase = gameDefinition.getPhases().get(i);
        currPhase.setNextPhase(nextPhase);
        currPhase.setGame(this);
        for (Feature feature : currPhase.getFeatures()) {
            feature.setPhase(currPhase);
        }
        for (Feature feature : currPhase.getFeatures()) {
            feature.init();
        }
    }
}
Also used : Phase(com.voxelgameslib.voxelgameslib.api.phase.Phase) Feature(com.voxelgameslib.voxelgameslib.api.feature.Feature) TeamFeature(com.voxelgameslib.voxelgameslib.api.feature.features.TeamFeature) DuelFeature(com.voxelgameslib.voxelgameslib.api.feature.features.DuelFeature)

Aggregations

Feature (com.voxelgameslib.voxelgameslib.api.feature.Feature)5 SpectatorFeature (com.voxelgameslib.voxelgameslib.api.feature.features.SpectatorFeature)4 DependencyGraphException (com.voxelgameslib.voxelgameslib.api.exception.DependencyGraphException)3 NoSuchFeatureException (com.voxelgameslib.voxelgameslib.api.exception.NoSuchFeatureException)3 VictoryCondition (com.voxelgameslib.voxelgameslib.api.condition.VictoryCondition)2 EmptyVictoryCondition (com.voxelgameslib.voxelgameslib.api.condition.conditions.EmptyVictoryCondition)2 VoxelGameLibException (com.voxelgameslib.voxelgameslib.api.exception.VoxelGameLibException)2 AbstractFeatureCommand (com.voxelgameslib.voxelgameslib.api.feature.AbstractFeatureCommand)2 FeatureCommandImplementor (com.voxelgameslib.voxelgameslib.api.feature.FeatureCommandImplementor)2 Tickable (com.voxelgameslib.voxelgameslib.api.tick.Tickable)2 Listener (org.bukkit.event.Listener)2 MCTiming (co.aikar.commands.lib.timings.MCTiming)1 DuelFeature (com.voxelgameslib.voxelgameslib.api.feature.features.DuelFeature)1 TeamFeature (com.voxelgameslib.voxelgameslib.api.feature.features.TeamFeature)1 Phase (com.voxelgameslib.voxelgameslib.api.phase.Phase)1 Ability (com.voxelgameslib.voxelgameslib.components.ability.Ability)1 Graph (com.voxelgameslib.voxelgameslib.internal.graph.Graph)1 ArrayList (java.util.ArrayList)1 Function (java.util.function.Function)1