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.Settings;
022import com.plotsquared.core.configuration.caption.TranslatableCaption;
023import com.plotsquared.core.player.MetaDataAccess;
024import com.plotsquared.core.player.PlayerMetaDataKeys;
025import com.plotsquared.core.player.PlotPlayer;
026import com.plotsquared.core.util.task.TaskManager;
027import com.plotsquared.core.util.task.TaskTime;
028import net.kyori.adventure.text.minimessage.Template;
029import org.checkerframework.checker.nullness.qual.Nullable;
030
031public class CmdConfirm {
032
033    public static @Nullable CmdInstance getPending(PlotPlayer<?> player) {
034        try (final MetaDataAccess<CmdInstance> metaDataAccess = player.accessTemporaryMetaData(
035                PlayerMetaDataKeys.TEMPORARY_CONFIRM)) {
036            return metaDataAccess.get().orElse(null);
037        }
038    }
039
040    public static void removePending(PlotPlayer<?> player) {
041        try (final MetaDataAccess<CmdInstance> metaDataAccess = player.accessTemporaryMetaData(
042                PlayerMetaDataKeys.TEMPORARY_CONFIRM)) {
043            metaDataAccess.remove();
044        }
045    }
046
047    public static void addPending(
048            final PlotPlayer<?> player, String commandStr,
049            final Runnable runnable
050    ) {
051        removePending(player);
052        if (commandStr != null) {
053            player.sendMessage(
054                    TranslatableCaption.of("confirm.requires_confirm"),
055                    Template.of("command", commandStr),
056                    Template.of("timeout", String.valueOf(Settings.Confirmation.CONFIRMATION_TIMEOUT_SECONDS)),
057                    Template.of("value", "/plot confirm")
058            );
059        }
060        TaskManager.runTaskLater(() -> {
061            CmdInstance cmd = new CmdInstance(runnable);
062            try (final MetaDataAccess<CmdInstance> metaDataAccess = player.accessTemporaryMetaData(
063                    PlayerMetaDataKeys.TEMPORARY_CONFIRM)) {
064                metaDataAccess.set(cmd);
065            }
066        }, TaskTime.ticks(1L));
067    }
068
069}