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.plotsquared.core.configuration.caption.StaticCaption;
022import com.plotsquared.core.configuration.caption.TranslatableCaption;
023import com.plotsquared.core.player.PlotPlayer;
024import com.plotsquared.core.plot.Plot;
025import com.plotsquared.core.util.StringMan;
026import com.sk89q.worldedit.command.util.SuggestionHelper;
027import com.sk89q.worldedit.world.biome.BiomeType;
028import com.sk89q.worldedit.world.biome.BiomeTypes;
029import net.kyori.adventure.text.Component;
030import net.kyori.adventure.text.minimessage.tag.Tag;
031import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
032
033import java.util.Collection;
034import java.util.Locale;
035import java.util.stream.Collectors;
036
037@CommandDeclaration(command = "setbiome",
038        permission = "plots.set.biome",
039        usage = "/plot biome [biome]",
040        aliases = {"biome", "sb", "setb", "b"},
041        category = CommandCategory.APPEARANCE,
042        requiredType = RequiredType.NONE)
043public class Biome extends SetCommand {
044
045    @Override
046    public boolean set(final PlotPlayer<?> player, final Plot plot, final String value) {
047        BiomeType biome = null;
048        try {
049            biome = BiomeTypes.get(value.toLowerCase());
050        } catch (final Exception ignore) {
051        }
052        if (biome == null) {
053            String separator = TranslatableCaption.of("blocklist.block_list_separator").getComponent(player);
054            player.sendMessage(TranslatableCaption.of("biome.need_biome"));
055            player.sendMessage(
056                    StaticCaption.of(
057                            TranslatableCaption.of("commandconfig.subcommand_set_options_header_only").getComponent(player)
058                                    + StringMan.join(BiomeType.REGISTRY.values(), separator)
059                    )
060            );
061            return false;
062        }
063        if (plot.getRunning() > 0) {
064            player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
065            return false;
066        }
067        if (plot.getVolume() > Integer.MAX_VALUE) {
068            player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
069            return false;
070        }
071        plot.addRunning();
072        plot.getPlotModificationManager().setBiome(biome, () -> {
073            plot.removeRunning();
074            player.sendMessage(
075                    TranslatableCaption.of("biome.biome_set_to"),
076                    TagResolver.resolver("value", Tag.inserting(Component.text(value.toLowerCase())))
077            );
078        });
079        return true;
080    }
081
082    @Override
083    public Collection<Command> tab(final PlotPlayer<?> player, final String[] args, final boolean space) {
084        return SuggestionHelper.getNamespacedRegistrySuggestions(BiomeType.REGISTRY, args[0])
085                .map(value -> value.toLowerCase(Locale.ENGLISH).replace("minecraft:", ""))
086                .filter(value -> value.startsWith(args[0].toLowerCase(Locale.ENGLISH)))
087                .map(value -> new Command(null, false, value, "", RequiredType.NONE, null) {
088                }).collect(Collectors.toList());
089    }
090
091}