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