001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.setup;
020
021import com.plotsquared.core.player.PlotPlayer;
022
023import java.util.Stack;
024
025/**
026 * This class keeps track of a setup process.
027 * It holds the history and the current setup state.
028 */
029public class SetupProcess {
030
031    private final PlotAreaBuilder builder;
032    private final Stack<SetupStep> history;
033    private SetupStep current;
034
035    public SetupProcess() {
036        this.builder = PlotAreaBuilder.newBuilder();
037        this.history = new Stack<>();
038        this.current = CommonSetupSteps.CHOOSE_GENERATOR;
039    }
040
041    public SetupStep getCurrentStep() {
042        return this.current;
043    }
044
045    public void handleInput(PlotPlayer<?> plotPlayer, String argument) {
046        SetupStep previous = this.current;
047        this.current = this.current.handleInput(plotPlayer, this.builder, argument);
048        // push previous step into history
049        if (this.current != previous && this.current != null) {
050            this.history.push(previous);
051        }
052    }
053
054    public void back() {
055        if (!this.history.isEmpty()) {
056            this.current.onBack(this.builder);
057            this.current = this.history.pop();
058        }
059    }
060
061}