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.command.Command;
022import com.plotsquared.core.command.RequiredType;
023import com.plotsquared.core.configuration.ConfigurationNode;
024import com.plotsquared.core.configuration.caption.TranslatableCaption;
025import com.plotsquared.core.player.PlotPlayer;
026import com.plotsquared.core.util.TabCompletions;
027import net.kyori.adventure.text.minimessage.Template;
028import org.checkerframework.checker.nullness.qual.NonNull;
029import org.checkerframework.checker.nullness.qual.Nullable;
030
031import java.util.Collection;
032import java.util.Collections;
033
034/**
035 * A SettingsNodeStep is a step wrapping a {@link ConfigurationNode}.
036 */
037public class SettingsNodeStep implements SetupStep {
038
039    private final ConfigurationNode configurationNode;
040    private final int id;
041    private final SetupStep next;
042
043    public SettingsNodeStep(
044            final ConfigurationNode configurationNode, final int id,
045            final SettingsNodesWrapper wrapper
046    ) {
047        this.configurationNode = configurationNode;
048        this.id = id;
049        if (wrapper.getSettingsNodes().length > id + 1) {
050            this.next = new SettingsNodeStep(wrapper.getSettingsNodes()[id + 1], id + 1, wrapper);
051        } else {
052            this.next = wrapper.getAfterwards();
053        }
054    }
055
056    @Override
057    public SetupStep handleInput(PlotPlayer<?> plotPlayer, PlotAreaBuilder builder, String argument) {
058        if (this.configurationNode.isValid(argument)) {
059            this.configurationNode.setValue(argument);
060        }
061        return this.next;
062    }
063
064    @NonNull
065    @Override
066    public Collection<String> getSuggestions() {
067        return this.configurationNode.getSuggestions();
068    }
069
070    @Nullable
071    @Override
072    public String getDefaultValue() {
073        return String.valueOf(this.configurationNode.getDefaultValue());
074    }
075
076    @Override
077    public void announce(PlotPlayer<?> plotPlayer) {
078        plotPlayer.sendMessage(
079                TranslatableCaption.of("setup.setup_step"),
080                Template.of("step", String.valueOf(this.getId() + 1)),
081                Template.of("description", this.configurationNode.getDescription().getComponent(plotPlayer)),
082                Template.of("type", this.configurationNode.getType().getType()),
083                Template.of("value", String.valueOf(this.configurationNode.getDefaultValue()))
084        );
085    }
086
087    @Override
088    public Collection<Command> createSuggestions(PlotPlayer<?> plotPlayer, String argument) {
089        switch (this.configurationNode.getType().getType()) {
090            case "BLOCK_BUCKET":
091                return TabCompletions.completePatterns(argument);
092            case "INTEGER":
093                if (getDefaultValue() != null && getDefaultValue().startsWith(argument)) {
094                    return Collections.singletonList(new Command(null, false,
095                            getDefaultValue(), "", RequiredType.NONE, null
096                    ) {
097                    });
098                }
099            case "BOOLEAN":
100                return TabCompletions.completeBoolean(argument);
101        }
102        return Collections.emptyList();
103    }
104
105    public ConfigurationNode getConfigurationNode() {
106        return this.configurationNode;
107    }
108
109    public int getId() {
110        return this.id;
111    }
112
113}