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.TranslatableCaption;
022import com.plotsquared.core.player.PlotPlayer;
023import com.plotsquared.core.plot.Plot;
024import com.plotsquared.core.util.StringMan;
025import com.sk89q.worldedit.command.util.SuggestionHelper;
026import com.sk89q.worldedit.world.biome.BiomeType;
027import com.sk89q.worldedit.world.biome.BiomeTypes;
028import net.kyori.adventure.text.minimessage.Template;
029
030import java.util.Collection;
031import java.util.Locale;
032import java.util.stream.Collectors;
033
034@CommandDeclaration(command = "setbiome",
035        permission = "plots.set.biome",
036        usage = "/plot biome [biome]",
037        aliases = {"biome", "sb", "setb", "b"},
038        category = CommandCategory.APPEARANCE,
039        requiredType = RequiredType.NONE)
040public class Biome extends SetCommand {
041
042    @Override
043    public boolean set(final PlotPlayer<?> player, final Plot plot, final String value) {
044        BiomeType biome = null;
045        try {
046            biome = BiomeTypes.get(value.toLowerCase());
047        } catch (final Exception ignore) {
048        }
049        if (biome == null) {
050            String biomes = StringMan.join(
051                    BiomeType.REGISTRY.values(),
052                    MINI_MESSAGE.serialize(MINI_MESSAGE.parse(TranslatableCaption
053                            .of("blocklist.block_list_separator")
054                            .getComponent(player)))
055            );
056            player.sendMessage(TranslatableCaption.of("biome.need_biome"));
057            player.sendMessage(
058                    TranslatableCaption.of("commandconfig.subcommand_set_options_header"),
059                    Template.of("values", biomes)
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                    Template.of("value", 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}