PlotSquared/Core/src/main/java/com/plotsquared/core/command/MainCommand.java

348 lines
15 KiB
Java
Raw Normal View History

/*
* PlotSquared, a land and world management plugin for Minecraft.
* Copyright (C) IntellectualSites <https://intellectualsites.com>
* Copyright (C) IntellectualSites team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-04-15 21:26:54 +02:00
package com.plotsquared.core.command;
2016-02-23 05:11:28 +01:00
2020-07-11 17:19:19 +02:00
import com.google.inject.Injector;
2020-07-10 17:32:07 +02:00
import com.plotsquared.core.PlotSquared;
2020-04-16 06:14:33 +02:00
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.location.Location;
import com.plotsquared.core.permissions.Permission;
2020-04-30 12:33:59 +02:00
import com.plotsquared.core.player.ConsolePlayer;
import com.plotsquared.core.player.MetaDataAccess;
import com.plotsquared.core.player.PlayerMetaDataKeys;
2020-04-30 12:33:59 +02:00
import com.plotsquared.core.player.PlotPlayer;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea;
2021-01-07 22:24:38 +01:00
import com.plotsquared.core.plot.world.SinglePlotArea;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.util.EconHandler;
import com.plotsquared.core.util.PlotExpression;
2020-04-30 12:33:59 +02:00
import com.plotsquared.core.util.task.RunnableVal2;
import com.plotsquared.core.util.task.RunnableVal3;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2016-02-23 05:11:28 +01:00
import java.util.Arrays;
2020-07-11 17:19:19 +02:00
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
2016-02-23 05:11:28 +01:00
/**
2016-03-23 02:41:37 +01:00
* PlotSquared command class.
2016-02-23 05:11:28 +01:00
*/
@CommandDeclaration(command = "plot",
aliases = {"plots", "p", "plotsquared", "plot2", "p2", "ps", "2", "plotme", "plotz", "ap"})
public class MainCommand extends Command {
2018-08-10 17:01:10 +02:00
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + MainCommand.class.getSimpleName());
2020-08-17 04:39:59 +02:00
2016-02-23 05:11:28 +01:00
private static MainCommand instance;
2016-03-26 06:44:38 +01:00
public Help help;
public Toggle toggle;
2016-02-23 05:11:28 +01:00
private MainCommand() {
super(null, true);
2016-02-23 05:11:28 +01:00
instance = this;
}
public static MainCommand getInstance() {
if (instance == null) {
instance = new MainCommand();
final Injector injector = PlotSquared.platform().injector();
2020-07-11 17:19:19 +02:00
final List<Class<? extends Command>> commands = new LinkedList<>();
commands.add(Caps.class);
commands.add(Buy.class);
if (Settings.Web.LEGACY_WEBINTERFACE) {
LOGGER.warn("Legacy webinterface is used. Please note that it will be removed in future.");
commands.add(Save.class);
}
2020-07-11 17:19:19 +02:00
commands.add(Load.class);
commands.add(Confirm.class);
commands.add(Template.class);
commands.add(Download.class);
commands.add(Setup.class);
commands.add(Area.class);
commands.add(DebugSaveTest.class);
commands.add(DebugLoadTest.class);
commands.add(CreateRoadSchematic.class);
commands.add(DebugAllowUnsafe.class);
commands.add(RegenAllRoads.class);
commands.add(Claim.class);
commands.add(Auto.class);
commands.add(HomeCommand.class);
commands.add(Visit.class);
commands.add(Set.class);
commands.add(Clear.class);
commands.add(Delete.class);
commands.add(Trust.class);
commands.add(Add.class);
commands.add(Leave.class);
commands.add(Deny.class);
commands.add(Remove.class);
commands.add(Info.class);
commands.add(Near.class);
commands.add(ListCmd.class);
commands.add(Debug.class);
commands.add(SchematicCmd.class);
commands.add(PluginCmd.class);
commands.add(Purge.class);
commands.add(Reload.class);
commands.add(Merge.class);
commands.add(DebugPaste.class);
commands.add(Unlink.class);
commands.add(Kick.class);
commands.add(Inbox.class);
commands.add(Comment.class);
commands.add(DatabaseCommand.class);
commands.add(Swap.class);
commands.add(Music.class);
commands.add(DebugRoadRegen.class);
commands.add(DebugExec.class);
commands.add(FlagCommand.class);
commands.add(Target.class);
commands.add(Move.class);
commands.add(Condense.class);
commands.add(Copy.class);
commands.add(Chat.class);
commands.add(Trim.class);
commands.add(Done.class);
commands.add(Continue.class);
commands.add(Middle.class);
commands.add(Grant.class);
commands.add(Owner.class);
commands.add(Desc.class);
commands.add(Biome.class);
commands.add(Alias.class);
commands.add(SetHome.class);
commands.add(Cluster.class);
commands.add(DebugImportWorlds.class);
commands.add(Backup.class);
2019-04-01 17:39:28 +02:00
if (Settings.Ratings.USE_LIKES) {
2020-07-11 17:19:19 +02:00
commands.add(Like.class);
commands.add(Dislike.class);
2019-04-01 17:39:28 +02:00
} else {
2020-07-11 17:19:19 +02:00
commands.add(Rate.class);
}
for (final Class<? extends Command> command : commands) {
2020-08-17 04:39:59 +02:00
try {
injector.getInstance(command);
} catch (final Exception e) {
LOGGER.error("Failed to register command {}", command.getCanonicalName());
2020-08-17 04:39:59 +02:00
e.printStackTrace();
}
2019-04-01 17:39:28 +02:00
}
2016-03-26 06:44:38 +01:00
// Referenced commands
2020-07-11 17:19:19 +02:00
instance.toggle = injector.getInstance(Toggle.class);
2016-03-26 06:44:38 +01:00
instance.help = new Help(instance);
2016-02-23 05:11:28 +01:00
}
return instance;
}
2020-07-10 17:32:07 +02:00
public static boolean onCommand(final PlotPlayer<?> player, String... args) {
final EconHandler econHandler = PlotSquared.platform().econHandler();
if (args.length >= 1 && args[0].contains(":")) {
String[] split2 = args[0].split(":");
if (split2.length == 2) {
// Ref: c:v, this will push value to the last spot in the array
// ex. /p h:2 SomeUsername
// > /p h SomeUsername 2
String[] tmp = new String[args.length + 1];
tmp[0] = split2[0];
tmp[args.length] = split2[1];
2016-03-28 14:04:08 +02:00
if (args.length >= 2) {
System.arraycopy(args, 1, tmp, 1, args.length - 1);
}
args = tmp;
2016-02-23 05:11:28 +01:00
}
}
try {
getInstance().execute(player, args, new RunnableVal3<>() {
@Override
public void run(final Command cmd, final Runnable success, final Runnable failure) {
if (cmd.hasConfirmation(player)) {
CmdConfirm.addPending(player, cmd.getUsage(), () -> {
PlotArea area = player.getApplicablePlotArea();
if (area != null && econHandler.isEnabled(area)) {
PlotExpression priceEval =
area.getPrices().get(cmd.getFullId());
double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
if (econHandler.getMoney(player) < price) {
if (failure != null) {
failure.run();
}
return;
}
}
if (success != null) {
success.run();
}
});
return;
}
PlotArea area = player.getApplicablePlotArea();
if (area != null && econHandler.isEnabled(area)) {
PlotExpression priceEval = area.getPrices().get(cmd.getFullId());
double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
if (price != 0d && econHandler.getMoney(player) < price) {
if (failure != null) {
failure.run();
}
return;
}
}
if (success != null) {
success.run();
}
2016-02-23 05:11:28 +01:00
}
}, new RunnableVal2<>() {
@Override
public void run(Command cmd, CommandResult result) {
// Post command stuff!?
}
}).thenAccept(result -> {
// TODO: Something with the command result
});
} catch (CommandException e) {
e.perform(player);
}
// Always true
return true;
2016-02-23 05:11:28 +01:00
}
2016-03-22 18:53:17 +01:00
2020-04-30 12:33:59 +02:00
@Override
public CompletableFuture<Boolean> execute(
final PlotPlayer<?> player, String[] args,
RunnableVal3<Command, Runnable, Runnable> confirm,
RunnableVal2<Command, CommandResult> whenDone
) {
// Optional command scope //
2019-09-08 20:02:45 +02:00
Location location = null;
2016-02-23 05:11:28 +01:00
Plot plot = null;
boolean tp = false;
if (args.length >= 2) {
PlotArea area = player.getApplicablePlotArea();
Plot newPlot = Plot.fromString(area, args[0]);
2018-08-10 17:01:10 +02:00
if (newPlot != null && (player instanceof ConsolePlayer || newPlot.getArea()
.equals(area) || player.hasPermission(Permission.PERMISSION_ADMIN)
|| player.hasPermission(Permission.PERMISSION_ADMIN_AREA_SUDO))
&& !newPlot.isDenied(player.getUUID())) {
2021-01-07 22:24:38 +01:00
final Location newLoc;
if (newPlot.getArea() instanceof SinglePlotArea) {
newLoc = newPlot.isLoaded() ? newPlot.getCenterSynchronous() : Location.at("", 0, 0, 0);
} else {
newLoc = newPlot.getCenterSynchronous();
}
if (player.canTeleport(newLoc)) {
// Save meta
try (final MetaDataAccess<Location> locationMetaDataAccess
= player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LOCATION)) {
location = locationMetaDataAccess.get().orElse(null);
locationMetaDataAccess.set(newLoc);
}
try (final MetaDataAccess<Plot> plotMetaDataAccess
= player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LAST_PLOT)) {
plot = plotMetaDataAccess.get().orElse(null);
plotMetaDataAccess.set(newPlot);
}
tp = true;
} else {
player.sendMessage(TranslatableCaption.of("border.denied"));
}
// Trim command
args = Arrays.copyOfRange(args, 1, args.length);
2016-02-23 05:11:28 +01:00
}
2016-12-09 22:58:32 +01:00
if (args.length >= 2 && !args[0].isEmpty() && args[0].charAt(0) == '-') {
if ("f".equals(args[0].substring(1))) {
confirm = new RunnableVal3<>() {
@Override
public void run(Command cmd, Runnable success, Runnable failure) {
if (area != null && PlotSquared.platform().econHandler().isEnabled(area)) {
PlotExpression priceEval =
area.getPrices().get(cmd.getFullId());
double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
if (price != 0d
&& PlotSquared.platform().econHandler().getMoney(player) < price) {
if (failure != null) {
failure.run();
2016-10-14 06:14:08 +02:00
}
return;
2016-10-14 06:14:08 +02:00
}
}
if (success != null) {
success.run();
}
}
};
args = Arrays.copyOfRange(args, 1, args.length);
} else {
player.sendMessage(TranslatableCaption.of("errors.invalid_command_flag"));
return CompletableFuture.completedFuture(false);
2016-10-14 06:14:08 +02:00
}
}
2016-02-23 05:11:28 +01:00
}
try {
super.execute(player, args, confirm, whenDone);
2016-06-29 13:22:55 +02:00
} catch (CommandException e) {
throw e;
} catch (Throwable e) {
e.printStackTrace();
String message = e.getMessage();
2016-06-29 13:22:55 +02:00
if (message != null) {
player.sendMessage(
TranslatableCaption.of("errors.error"),
net.kyori.adventure.text.minimessage.Template.of("value", message)
);
2016-06-29 13:22:55 +02:00
} else {
2020-08-17 23:21:18 +02:00
player.sendMessage(
TranslatableCaption.of("errors.error_console"));
2016-06-29 13:22:55 +02:00
}
}
// Reset command scope //
if (tp && !(player instanceof ConsolePlayer)) {
try (final MetaDataAccess<Location> locationMetaDataAccess
= player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LOCATION)) {
if (location == null) {
locationMetaDataAccess.remove();
} else {
locationMetaDataAccess.set(location);
}
2016-02-23 05:11:28 +01:00
}
try (final MetaDataAccess<Plot> plotMetaDataAccess
= player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LAST_PLOT)) {
if (plot == null) {
plotMetaDataAccess.remove();
} else {
plotMetaDataAccess.set(plot);
}
2016-02-23 05:11:28 +01:00
}
}
return CompletableFuture.completedFuture(true);
2016-02-23 05:11:28 +01:00
}
2016-03-22 18:53:17 +01:00
@Override
2021-05-01 18:33:02 +02:00
public boolean canExecute(PlotPlayer<?> player, boolean message) {
return true;
2016-02-23 05:11:28 +01:00
}
2016-02-23 05:11:28 +01:00
}