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