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.configuration.caption.TranslatableCaption;
023import com.plotsquared.core.events.PlotFlagAddEvent;
024import com.plotsquared.core.events.PlotFlagRemoveEvent;
025import com.plotsquared.core.events.Result;
026import com.plotsquared.core.player.PlotPlayer;
027import com.plotsquared.core.plot.Plot;
028import com.plotsquared.core.plot.flag.implementations.DescriptionFlag;
029import com.plotsquared.core.util.EventDispatcher;
030import net.kyori.adventure.text.Component;
031import net.kyori.adventure.text.minimessage.tag.Tag;
032import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
033import org.checkerframework.checker.nullness.qual.NonNull;
034
035@CommandDeclaration(command = "setdescription",
036        permission = "plots.set.desc",
037        usage = "/plot desc <description>",
038        aliases = {"desc", "setdesc", "setd", "description"},
039        category = CommandCategory.SETTINGS,
040        requiredType = RequiredType.PLAYER)
041public class Desc extends SetCommand {
042
043    private final EventDispatcher eventDispatcher;
044
045    @Inject
046    public Desc(final @NonNull EventDispatcher eventDispatcher) {
047        this.eventDispatcher = eventDispatcher;
048    }
049
050    @Override
051    public boolean set(PlotPlayer<?> player, Plot plot, String desc) {
052        if (desc.isEmpty()) {
053            PlotFlagRemoveEvent event = this.eventDispatcher.callFlagRemove(plot
054                    .getFlagContainer()
055                    .getFlag(DescriptionFlag.class), plot);
056            if (event.getEventResult() == Result.DENY) {
057                player.sendMessage(
058                        TranslatableCaption.of("events.event_denied"),
059                        TagResolver.resolver("value", Tag.inserting(Component.text("Description removal")))
060                );
061                return false;
062            }
063            plot.removeFlag(event.getFlag());
064            player.sendMessage(TranslatableCaption.of("desc.desc_unset"));
065            return true;
066        }
067        PlotFlagAddEvent event = this.eventDispatcher.callFlagAdd(plot
068                .getFlagContainer()
069                .getFlag(DescriptionFlag.class)
070                .createFlagInstance(desc), plot);
071        if (event.getEventResult() == Result.DENY) {
072            player.sendMessage(
073                    TranslatableCaption.of("events.event_denied"),
074                    TagResolver.resolver("value", Tag.inserting(Component.text("Description set")))
075            );
076            return false;
077        }
078        boolean result = plot.setFlag(event.getFlag());
079        if (!result) {
080            player.sendMessage(TranslatableCaption.of("flag.flag_not_added"));
081            return false;
082        }
083        player.sendMessage(TranslatableCaption.of("desc.desc_set"));
084        return true;
085    }
086
087}