Search in sources :

Example 1 with ClosedState

use of com.ebicep.warlords.game.state.ClosedState in project Warlords by ebicep.

the class Game method run.

@Override
public void run() {
    if (this.nextState == null && this.state != null) {
        this.nextState = this.state.run();
    }
    while (this.nextState != null) {
        for (GameAddon addon : this.addons) {
            nextState = addon.stateWillChange(this, this.state, nextState);
            if (nextState == null) {
                return;
            }
        }
        if (this.state != null) {
            this.state.end();
        }
        State newState = nextState == null ? new ClosedState(this) : nextState;
        nextState = null;
        // System.out.println("DEBUG OLD TO NEW STATE");
        // this.printDebuggingInformation();
        State oldState = this.state;
        this.state = newState;
        newState.begin();
        for (GameAddon addon : this.addons) {
            addon.stateHasChanged(this, oldState, newState);
        }
    }
}
Also used : State(com.ebicep.warlords.game.state.State) ClosedState(com.ebicep.warlords.game.state.ClosedState) ClosedState(com.ebicep.warlords.game.state.ClosedState)

Example 2 with ClosedState

use of com.ebicep.warlords.game.state.ClosedState in project Warlords by ebicep.

the class Game method close.

@Override
public void close() {
    if (this.closed) {
        return;
    }
    this.closed = true;
    List<Throwable> exceptions = new ArrayList<>();
    for (BukkitTask task : gameTasks) {
        task.cancel();
    }
    gameTasks.clear();
    for (Listener listener : eventHandlers) {
        HandlerList.unregisterAll(listener);
    }
    eventHandlers.clear();
    try {
        removeAllPlayers();
    } catch (Throwable e) {
        exceptions.add(e);
    }
    for (Option option : options) {
        try {
            option.onGameCleanup(this);
        } catch (Throwable e) {
            exceptions.add(e);
        }
    }
    this.acceptsPlayers = false;
    this.acceptsSpectators = false;
    this.nextState = null;
    if (this.state != null && !(this.state instanceof ClosedState)) {
        try {
            this.state.end();
        } catch (Throwable e) {
            exceptions.add(e);
        }
        this.state = new ClosedState(this);
    }
    this.options = Collections.emptyList();
    if (!exceptions.isEmpty()) {
        RuntimeException e = new RuntimeException("Problems closing the game", exceptions.get(0));
        for (int i = 1; i < exceptions.size(); i++) {
            e.addSuppressed(exceptions.get(i));
        }
        throw e;
    }
}
Also used : Listener(org.bukkit.event.Listener) RegisteredListener(org.bukkit.plugin.RegisteredListener) BukkitTask(org.bukkit.scheduler.BukkitTask) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Option(com.ebicep.warlords.game.option.Option) ClosedState(com.ebicep.warlords.game.state.ClosedState)

Aggregations

ClosedState (com.ebicep.warlords.game.state.ClosedState)2 Option (com.ebicep.warlords.game.option.Option)1 State (com.ebicep.warlords.game.state.State)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Listener (org.bukkit.event.Listener)1 RegisteredListener (org.bukkit.plugin.RegisteredListener)1 BukkitTask (org.bukkit.scheduler.BukkitTask)1