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.command; 020 021import com.google.inject.Inject; 022import com.plotsquared.core.PlotSquared; 023import com.plotsquared.core.configuration.caption.StaticCaption; 024import com.plotsquared.core.configuration.caption.TranslatableCaption; 025import com.plotsquared.core.generator.GeneratorWrapper; 026import com.plotsquared.core.player.MetaDataAccess; 027import com.plotsquared.core.player.PlayerMetaDataKeys; 028import com.plotsquared.core.player.PlotPlayer; 029import com.plotsquared.core.setup.SetupProcess; 030import com.plotsquared.core.setup.SetupStep; 031import com.plotsquared.core.util.SetupUtils; 032import net.kyori.adventure.text.minimessage.Template; 033import org.checkerframework.checker.nullness.qual.NonNull; 034 035import java.util.ArrayList; 036import java.util.Collection; 037import java.util.Collections; 038import java.util.List; 039import java.util.Map.Entry; 040 041@CommandDeclaration(command = "setup", 042 permission = "plots.admin.command.setup", 043 usage = "/plot setup", 044 aliases = {"create"}, 045 category = CommandCategory.ADMINISTRATION) 046public class Setup extends SubCommand { 047 048 private final SetupUtils setupUtils; 049 050 @Inject 051 public Setup(final @NonNull SetupUtils setupUtils) { 052 this.setupUtils = setupUtils; 053 } 054 055 public void displayGenerators(PlotPlayer<?> player) { 056 StringBuilder message = new StringBuilder(); 057 message.append(TranslatableCaption.of("setup.choose_generator").getComponent(player)); 058 for (Entry<String, GeneratorWrapper<?>> entry : SetupUtils.generators.entrySet()) { 059 if (entry.getKey().equals(PlotSquared.platform().pluginName())) { 060 message.append("\n<dark_gray> - </dark_gray><dark_green>").append(entry.getKey()).append( 061 " (Default Generator)</dark_green>"); 062 } else if (entry.getValue().isFull()) { 063 message.append("\n<dark_gray> - </dark_gray><gray>").append(entry.getKey()).append(" (Plot Generator)</gray>"); 064 } else { 065 message.append("\n<dark_gray> - </dark_gray><gray>").append(entry.getKey()).append(" (Unknown structure)</gray>"); 066 } 067 } 068 player.sendMessage(StaticCaption.of(message.toString())); 069 } 070 071 @Override 072 public boolean onCommand(PlotPlayer<?> player, String[] args) { 073 try (final MetaDataAccess<SetupProcess> metaDataAccess = 074 player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_SETUP)) { 075 SetupProcess process = metaDataAccess.get().orElse(null); 076 if (process == null) { 077 if (args.length > 0) { 078 player.sendMessage(TranslatableCaption.of("setup.setup_not_started")); 079 player.sendMessage( 080 TranslatableCaption.of("commandconfig.command_syntax"), 081 Template.of("value", "Use /plot setup to start a setup process.") 082 ); 083 return true; 084 } 085 process = new SetupProcess(); 086 metaDataAccess.set(process); 087 this.setupUtils.updateGenerators(false); 088 SetupStep step = process.getCurrentStep(); 089 step.announce(player); 090 displayGenerators(player); 091 return true; 092 } 093 if (args.length == 1) { 094 if ("back".equalsIgnoreCase(args[0])) { 095 process.back(); 096 process.getCurrentStep().announce(player); 097 } else if ("cancel".equalsIgnoreCase(args[0])) { 098 metaDataAccess.remove(); 099 player.sendMessage(TranslatableCaption.of("setup.setup_cancelled")); 100 } else { 101 process.handleInput(player, args[0]); 102 if (process.getCurrentStep() != null) { 103 process.getCurrentStep().announce(player); 104 } 105 } 106 } else { 107 process.getCurrentStep().announce(player); 108 } 109 return true; 110 } 111 } 112 113 @Override 114 public Collection<Command> tab(PlotPlayer<?> player, String[] args, boolean space) { 115 SetupProcess process; 116 try (final MetaDataAccess<SetupProcess> metaDataAccess = 117 player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_SETUP)) { 118 process = metaDataAccess.get().orElse(null); 119 } 120 if (process == null) { 121 return Collections.emptyList(); 122 } 123 // player already provided too many arguments 124 if (args.length > 1 || (args.length == 1 && space)) { 125 return Collections.emptyList(); 126 } 127 SetupStep setupStep = process.getCurrentStep(); 128 List<Command> commands = new ArrayList<>(setupStep.createSuggestions(player, space ? "" : args[0])); 129 tryAddSubCommand("back", args[0], commands); 130 tryAddSubCommand("cancel", args[0], commands); 131 return commands; 132 } 133 134 private void tryAddSubCommand(String subCommand, String argument, List<Command> suggestions) { 135 if (!argument.isEmpty() && subCommand.startsWith(argument)) { 136 suggestions.add(new Command(null, false, subCommand, "", RequiredType.NONE, null) { 137 }); 138 } 139 } 140 141}