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