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.util.task.RunnableVal2;
024import com.plotsquared.core.util.task.RunnableVal3;
025import net.kyori.adventure.text.minimessage.Template;
026
027@CommandDeclaration(command = "toggle",
028        aliases = {"attribute"},
029        permission = "plots.toggle",
030        usage = "/plot toggle <chat | chatspy | clear-confirmation | time | titles | worldedit>",
031        requiredType = RequiredType.NONE,
032        category = CommandCategory.SETTINGS)
033public class Toggle extends Command {
034
035    public Toggle() {
036        super(MainCommand.getInstance(), true);
037    }
038
039    @CommandDeclaration(command = "chatspy",
040            aliases = {"spy"},
041            permission = "plots.admin.command.chatspy")
042    public void chatspy(
043            Command command, PlotPlayer<?> player, String[] args,
044            RunnableVal3<Command, Runnable, Runnable> confirm,
045            RunnableVal2<Command, CommandResult> whenDone
046    ) {
047        if (toggle(player, "chatspy")) {
048            player.sendMessage(
049                    TranslatableCaption.of("toggle.toggle_disabled"),
050                    Template.of("setting", command.toString())
051            );
052        } else {
053            player.sendMessage(
054                    TranslatableCaption.of("toggle.toggle_enabled"),
055                    Template.of("setting", command.toString())
056            );
057        }
058    }
059
060    @CommandDeclaration(command = "worldedit",
061            aliases = {"we", "wea"},
062            permission = "plots.worldedit.bypass")
063    public void worldedit(
064            Command command, PlotPlayer<?> player, String[] args,
065            RunnableVal3<Command, Runnable, Runnable> confirm,
066            RunnableVal2<Command, CommandResult> whenDone
067    ) {
068        if (toggle(player, "worldedit")) {
069            player.sendMessage(
070                    TranslatableCaption.of("toggle.toggle_disabled"),
071                    Template.of("setting", command.toString())
072            );
073        } else {
074            player.sendMessage(
075                    TranslatableCaption.of("toggle.toggle_enabled"),
076                    Template.of("setting", command.toString())
077            );
078        }
079    }
080
081    @CommandDeclaration(command = "chat",
082            permission = "plots.toggle.chat")
083    public void chat(
084            Command command, PlotPlayer<?> player, String[] args,
085            RunnableVal3<Command, Runnable, Runnable> confirm,
086            RunnableVal2<Command, CommandResult> whenDone
087    ) {
088        if (toggle(player, "chat")) {
089            player.sendMessage(
090                    TranslatableCaption.of("toggle.toggle_disabled"),
091                    Template.of("setting", command.toString())
092            );
093        } else {
094            player.sendMessage(
095                    TranslatableCaption.of("toggle.toggle_enabled"),
096                    Template.of("setting", command.toString())
097            );
098        }
099    }
100
101    @CommandDeclaration(command = "clear-confirmation",
102            permission = "plots.admin.command.autoclear")
103    public void clearConfirmation(
104            Command command, PlotPlayer<?> player, String[] args,
105            RunnableVal3<Command, Runnable, Runnable> confirm,
106            RunnableVal2<Command, CommandResult> whenDone
107    ) {
108        if (toggle(player, "ignoreExpireTask")) {
109            player.sendMessage(
110                    TranslatableCaption.of("toggle.toggle_enabled"),
111                    Template.of("setting", command.toString())
112            );
113        } else {
114            player.sendMessage(
115                    TranslatableCaption.of("toggle.toggle_disabled"),
116                    Template.of("setting", command.toString())
117            );
118        }
119    }
120
121    @CommandDeclaration(command = "titles",
122            permission = "plots.toggle.titles")
123    public void titles(
124            Command command, PlotPlayer<?> player, String[] args,
125            RunnableVal3<Command, Runnable, Runnable> confirm,
126            RunnableVal2<Command, CommandResult> whenDone
127    ) {
128        if (toggle(player, "disabletitles")) {
129            player.sendMessage(
130                    TranslatableCaption.of("toggle.toggle_enabled"),
131                    Template.of("setting", command.toString())
132            );
133        } else {
134            player.sendMessage(
135                    TranslatableCaption.of("toggle.toggle_disabled"),
136                    Template.of("setting", command.toString())
137            );
138        }
139    }
140
141    @CommandDeclaration(command = "time",
142            permission = "plots.toggle.time")
143    public void time(
144            Command command, PlotPlayer<?> player, String[] args,
145            RunnableVal3<Command, Runnable, Runnable> confirm,
146            RunnableVal2<Command, CommandResult> whenDone
147    ) {
148        if (toggle(player, "disabletime")) {
149            player.sendMessage(
150                    TranslatableCaption.of("toggle.toggle_enabled"),
151                    Template.of("setting", command.toString())
152            );
153        } else {
154            player.sendMessage(
155                    TranslatableCaption.of("toggle.toggle_disabled"),
156                    Template.of("setting", command.toString())
157            );
158        }
159    }
160
161    @CommandDeclaration(command = "debug",
162            permission = "plots.toggle.debug")
163    public void debug(
164            Command command, PlotPlayer<?> player, String[] args,
165            RunnableVal3<Command, Runnable, Runnable> confirm,
166            RunnableVal2<Command, CommandResult> whenDone
167    ) {
168        if (toggle(player, "debug")) {
169            player.sendMessage(
170                    TranslatableCaption.of("toggle.toggle_disabled"),
171                    Template.of("setting", command.toString())
172            );
173        } else {
174            player.sendMessage(
175                    TranslatableCaption.of("toggle.toggle_enabled"),
176                    Template.of("setting", command.toString())
177            );
178        }
179        player.refreshDebug();
180    }
181
182    public boolean toggle(PlotPlayer<?> player, String key) {
183        if (player.getAttribute(key)) {
184            player.removeAttribute(key);
185            return true;
186        } else {
187            player.setAttribute(key);
188            return false;
189        }
190    }
191
192}