PlotSquared/Core/src/main/java/com/plotsquared/commands/MainCommand.java

286 lines
11 KiB
Java
Raw Normal View History

2020-04-11 02:19:18 +02:00
package com.plotsquared.commands;
2016-02-23 05:11:28 +01:00
2020-04-11 02:19:18 +02:00
import com.plotsquared.config.Captions;
import com.plotsquared.config.Settings;
import com.plotsquared.player.ConsolePlayer;
import com.plotsquared.util.Expression;
import com.plotsquared.location.Location;
import com.plotsquared.plot.Plot;
import com.plotsquared.plot.PlotArea;
import com.plotsquared.player.PlotPlayer;
import com.plotsquared.util.tasks.RunnableVal2;
import com.plotsquared.util.tasks.RunnableVal3;
import com.plotsquared.util.EconHandler;
import com.plotsquared.util.Permissions;
2016-02-23 05:11:28 +01:00
import java.util.Arrays;
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
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();
new Buy();
new Save();
new Load();
new Confirm();
new Template();
new Download();
new Template();
new Setup();
new Area();
new DebugSaveTest();
new DebugLoadTest();
new CreateRoadSchematic();
new DebugAllowUnsafe();
new RegenAllRoads();
new Claim();
new Auto();
new Visit();
new Set();
new Clear();
new Delete();
new Trust();
new Add();
2017-07-24 22:39:59 +02:00
new Leave();
new Deny();
new Remove();
new Info();
2016-09-25 10:06:20 +02:00
new Near();
new ListCmd();
new Debug();
new SchematicCmd();
new PluginCmd();
new Purge();
new Reload();
2016-03-29 19:34:48 +02:00
new Relight();
new Merge();
new DebugPaste();
new Unlink();
new Kick();
new DebugClaimTest();
new Inbox();
new Comment();
Pull/2693 (#2694) * Commit WIP flag work. * More ported flag types, and additions to the flag API. * Make PlotFlag more generic to allow generic flag creation * Pull Captions methods into a Caption interface. * Port MusicFlag * Port flight flag * Port UntrustedVisitFlag * Port DenyExitFlag * Remove paper suggestion * Make ListFlag lists immutable * Update Flag containers. Add javadocs. Add missing methods. * Port description flag * Port greeting and farewell flags * Port weather flag * Move getExample implementation to BooleanFlag * Port reserved flags * Port all boolean flags. * Remove unused flag types * Invert liquid-flow flag * Find the real (legacy) flag name * Change NOITFY -> NOTIFY in Captions * Make IntegerFlag extendable * Port integer flags * Update Flag command to current API state * Begin remaking flag command * Create DoubleFlag + extract common parsing stuff * Supply arguments in flag parse exceptions * Implement missing flag subcommands * Update Flag command to current API state * Implement PriceFlag * Port deny-teleport * Port gamemode flags * Port BreakFlag * Port PlaceFlag * Port UseFlag * Remove old unused flag constants * Port blocked-cmds flag * Fix entity util * Port TimeFlag * Use CaptionUtility for formatting * Port keep flag * Fix imports * Reformat code * Remove unused classes * Fix MainUtil.java * Remove FlagCmd * Add flag info header and footer * Comment out flag related stuff in SchematicHandler * Remove FlagManager * Finalize Plot.java * Finalize PlotArea.java * Finalize PlotListener * Fix API issues * Fix a bunch of compile errors * Fix `/plot flag remove` * Fix initialization of GlobalFlagContainer * Apply API changes to events * Update SQLManager to new API * Invert default value for DenyExitFlag * Replace flag.getValue().toString() with flag.toString() * Make FlagContainer instance in Plot final * Fix various command issues * Clean up PlotSettings * Don't show internal flags in flag list * Fix `/plot flag add` * Remove the info inventory as it's 100% broken * Add plot info entries and fix up the default format * Fix default flag state in Captions * 781c200 part 2 * Fix odd grammar in captions * Fix odd grammar in captions v2 * Add create table statements for plot_flags * Remove old flag references in SQLManager * Use the new plot_flags table * Add tab completion to `/plot flag` * Improve parse error handling * Make flag permission check recognize parse exceptions * Initial progress towards flag conversion * Fix minor issues * Don't validate flags during flag conversion * Allow unrecognized flags to be parsed * Filter out internal flags from command sugguestions * Use the wrong caption when there's no plot description set * Limit command suggestions for boolean flags * Make blocktypelistflags accept blockcategories * Require categories to be prefixed with '#' and fix some minor display issues * Fix plot database conversion * Update PlotFlagEvent.java Updated return description * Fix command annotation wrapping * Add FLAG_UPDATE event for FlagContainer listeners * Make getParentContainer nullable * Document castUnsafe in FlagContainer * Document FlagContainer constructors * Add missing documentation to FlagContainer * Document FlagParseException * Fix wording in FlagContainer javadoc * Document InternalFlag * Document PlotFlag * Minor changes * Remove revisit comments Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com> Co-authored-by: NotMyFault <mc.cache@web.de> Co-authored-by: Matt <4009945+MattBDev@users.noreply.github.com>
2020-02-24 18:42:02 +01:00
new DatabaseCommand();
new Swap();
new Music();
new DebugRoadRegen();
new Trust();
new DebugExec();
Pull/2693 (#2694) * Commit WIP flag work. * More ported flag types, and additions to the flag API. * Make PlotFlag more generic to allow generic flag creation * Pull Captions methods into a Caption interface. * Port MusicFlag * Port flight flag * Port UntrustedVisitFlag * Port DenyExitFlag * Remove paper suggestion * Make ListFlag lists immutable * Update Flag containers. Add javadocs. Add missing methods. * Port description flag * Port greeting and farewell flags * Port weather flag * Move getExample implementation to BooleanFlag * Port reserved flags * Port all boolean flags. * Remove unused flag types * Invert liquid-flow flag * Find the real (legacy) flag name * Change NOITFY -> NOTIFY in Captions * Make IntegerFlag extendable * Port integer flags * Update Flag command to current API state * Begin remaking flag command * Create DoubleFlag + extract common parsing stuff * Supply arguments in flag parse exceptions * Implement missing flag subcommands * Update Flag command to current API state * Implement PriceFlag * Port deny-teleport * Port gamemode flags * Port BreakFlag * Port PlaceFlag * Port UseFlag * Remove old unused flag constants * Port blocked-cmds flag * Fix entity util * Port TimeFlag * Use CaptionUtility for formatting * Port keep flag * Fix imports * Reformat code * Remove unused classes * Fix MainUtil.java * Remove FlagCmd * Add flag info header and footer * Comment out flag related stuff in SchematicHandler * Remove FlagManager * Finalize Plot.java * Finalize PlotArea.java * Finalize PlotListener * Fix API issues * Fix a bunch of compile errors * Fix `/plot flag remove` * Fix initialization of GlobalFlagContainer * Apply API changes to events * Update SQLManager to new API * Invert default value for DenyExitFlag * Replace flag.getValue().toString() with flag.toString() * Make FlagContainer instance in Plot final * Fix various command issues * Clean up PlotSettings * Don't show internal flags in flag list * Fix `/plot flag add` * Remove the info inventory as it's 100% broken * Add plot info entries and fix up the default format * Fix default flag state in Captions * 781c200 part 2 * Fix odd grammar in captions * Fix odd grammar in captions v2 * Add create table statements for plot_flags * Remove old flag references in SQLManager * Use the new plot_flags table * Add tab completion to `/plot flag` * Improve parse error handling * Make flag permission check recognize parse exceptions * Initial progress towards flag conversion * Fix minor issues * Don't validate flags during flag conversion * Allow unrecognized flags to be parsed * Filter out internal flags from command sugguestions * Use the wrong caption when there's no plot description set * Limit command suggestions for boolean flags * Make blocktypelistflags accept blockcategories * Require categories to be prefixed with '#' and fix some minor display issues * Fix plot database conversion * Update PlotFlagEvent.java Updated return description * Fix command annotation wrapping * Add FLAG_UPDATE event for FlagContainer listeners * Make getParentContainer nullable * Document castUnsafe in FlagContainer * Document FlagContainer constructors * Add missing documentation to FlagContainer * Document FlagParseException * Fix wording in FlagContainer javadoc * Document InternalFlag * Document PlotFlag * Minor changes * Remove revisit comments Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com> Co-authored-by: NotMyFault <mc.cache@web.de> Co-authored-by: Matt <4009945+MattBDev@users.noreply.github.com>
2020-02-24 18:42:02 +01:00
new FlagCommand();
new Target();
new Move();
new Condense();
new Copy();
new Chat();
new Trim();
new Done();
new Continue();
new Middle();
new Grant();
2016-03-26 06:44:38 +01:00
// Set commands
new Owner();
new Desc();
new Biome();
new Alias();
new SetHome();
new Cluster();
2017-03-23 01:10:29 +01:00
new DebugImportWorlds();
2019-04-01 17:39:28 +02:00
if (Settings.Ratings.USE_LIKES) {
new Like();
new Dislike();
} else {
new Rate();
}
2016-03-26 06:44:38 +01:00
// Referenced commands
instance.toggle = new Toggle();
instance.help = new Help(instance);
2016-02-23 05:11:28 +01:00
}
return instance;
}
public static boolean onCommand(final PlotPlayer player, String... args) {
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<Command, Runnable, Runnable>() {
@Override
public void run(final Command cmd, final Runnable success, final Runnable failure) {
if (cmd.hasConfirmation(player)) {
CmdConfirm.addPending(player, cmd.getUsage(), () -> {
if (EconHandler.manager != null) {
PlotArea area = player.getApplicablePlotArea();
if (area != null) {
Expression<Double> priceEval = area.getPrices().get(cmd.getFullId());
Double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
if (price != null
&& EconHandler.manager.getMoney(player) < price) {
if (failure != null) {
failure.run();
}
return;
}
}
}
if (success != null) {
success.run();
}
});
return;
}
if (EconHandler.manager != null) {
PlotArea area = player.getApplicablePlotArea();
if (area != null) {
Expression<Double> priceEval = area.getPrices().get(cmd.getFullId());
2016-07-17 04:51:49 +02:00
Double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
2016-06-19 16:52:05 +02:00
if (price != 0d && EconHandler.manager.getMoney(player) < price) {
if (failure != null) {
failure.run();
}
return;
}
}
}
if (success != null) {
success.run();
}
2016-02-23 05:11:28 +01:00
}
}, new RunnableVal2<Command, CommandResult>() {
2018-08-10 17:01:10 +02:00
@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
@Override public CompletableFuture<Boolean> execute(final PlotPlayer player, String[] args,
2018-08-10 17:01:10 +02:00
RunnableVal3<Command, Runnable, Runnable> confirm,
RunnableVal2<Command, CommandResult> whenDone) {
2016-02-23 05:11:28 +01:00
// Clear perm caching //
player.deleteMeta("perm");
// 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()
Pull/2693 (#2694) * Commit WIP flag work. * More ported flag types, and additions to the flag API. * Make PlotFlag more generic to allow generic flag creation * Pull Captions methods into a Caption interface. * Port MusicFlag * Port flight flag * Port UntrustedVisitFlag * Port DenyExitFlag * Remove paper suggestion * Make ListFlag lists immutable * Update Flag containers. Add javadocs. Add missing methods. * Port description flag * Port greeting and farewell flags * Port weather flag * Move getExample implementation to BooleanFlag * Port reserved flags * Port all boolean flags. * Remove unused flag types * Invert liquid-flow flag * Find the real (legacy) flag name * Change NOITFY -> NOTIFY in Captions * Make IntegerFlag extendable * Port integer flags * Update Flag command to current API state * Begin remaking flag command * Create DoubleFlag + extract common parsing stuff * Supply arguments in flag parse exceptions * Implement missing flag subcommands * Update Flag command to current API state * Implement PriceFlag * Port deny-teleport * Port gamemode flags * Port BreakFlag * Port PlaceFlag * Port UseFlag * Remove old unused flag constants * Port blocked-cmds flag * Fix entity util * Port TimeFlag * Use CaptionUtility for formatting * Port keep flag * Fix imports * Reformat code * Remove unused classes * Fix MainUtil.java * Remove FlagCmd * Add flag info header and footer * Comment out flag related stuff in SchematicHandler * Remove FlagManager * Finalize Plot.java * Finalize PlotArea.java * Finalize PlotListener * Fix API issues * Fix a bunch of compile errors * Fix `/plot flag remove` * Fix initialization of GlobalFlagContainer * Apply API changes to events * Update SQLManager to new API * Invert default value for DenyExitFlag * Replace flag.getValue().toString() with flag.toString() * Make FlagContainer instance in Plot final * Fix various command issues * Clean up PlotSettings * Don't show internal flags in flag list * Fix `/plot flag add` * Remove the info inventory as it's 100% broken * Add plot info entries and fix up the default format * Fix default flag state in Captions * 781c200 part 2 * Fix odd grammar in captions * Fix odd grammar in captions v2 * Add create table statements for plot_flags * Remove old flag references in SQLManager * Use the new plot_flags table * Add tab completion to `/plot flag` * Improve parse error handling * Make flag permission check recognize parse exceptions * Initial progress towards flag conversion * Fix minor issues * Don't validate flags during flag conversion * Allow unrecognized flags to be parsed * Filter out internal flags from command sugguestions * Use the wrong caption when there's no plot description set * Limit command suggestions for boolean flags * Make blocktypelistflags accept blockcategories * Require categories to be prefixed with '#' and fix some minor display issues * Fix plot database conversion * Update PlotFlagEvent.java Updated return description * Fix command annotation wrapping * Add FLAG_UPDATE event for FlagContainer listeners * Make getParentContainer nullable * Document castUnsafe in FlagContainer * Document FlagContainer constructors * Add missing documentation to FlagContainer * Document FlagParseException * Fix wording in FlagContainer javadoc * Document InternalFlag * Document PlotFlag * Minor changes * Remove revisit comments Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com> Co-authored-by: NotMyFault <mc.cache@web.de> Co-authored-by: Matt <4009945+MattBDev@users.noreply.github.com>
2020-02-24 18:42:02 +01:00
.equals(area) || Permissions.hasPermission(player, Captions.PERMISSION_ADMIN)
|| Permissions.hasPermission(player, Captions.PERMISSION_ADMIN_SUDO_AREA))
2019-04-24 00:48:22 +02:00
&& !newPlot.isDenied(player.getUUID())) {
Location newLoc = newPlot.getCenterSynchronous();
if (player.canTeleport(newLoc)) {
// Save meta
2019-09-08 20:02:45 +02:00
location = player.getMeta(PlotPlayer.META_LOCATION);
plot = player.getMeta(PlotPlayer.META_LAST_PLOT);
tp = true;
// Set loc
player.setMeta(PlotPlayer.META_LOCATION, newLoc);
player.setMeta(PlotPlayer.META_LAST_PLOT, newPlot);
} else {
Captions.BORDER.send(player);
}
// 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<Command, Runnable, Runnable>() {
@Override public void run(Command cmd, Runnable success, Runnable failure) {
if (EconHandler.manager != null) {
PlotArea area = player.getApplicablePlotArea();
if (area != null) {
Expression<Double> priceEval = area.getPrices().get(cmd.getFullId());
Double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
if (price != 0d
&& EconHandler.manager.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 {
Captions.INVALID_COMMAND_FLAG.send(player);
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();
2016-06-29 13:22:55 +02:00
String message = e.getLocalizedMessage();
if (message != null) {
Captions.ERROR.send(player, message);
2016-06-29 13:22:55 +02:00
} else {
Captions.ERROR.send(player);
2016-06-29 13:22:55 +02:00
}
}
// Reset command scope //
if (tp && !(player instanceof ConsolePlayer)) {
2019-09-08 20:02:45 +02:00
if (location == null) {
player.deleteMeta(PlotPlayer.META_LOCATION);
2016-02-23 05:11:28 +01:00
} else {
2019-09-08 20:02:45 +02:00
player.setMeta(PlotPlayer.META_LOCATION, location);
2016-02-23 05:11:28 +01:00
}
if (plot == null) {
player.deleteMeta(PlotPlayer.META_LAST_PLOT);
2016-02-23 05:11:28 +01:00
} else {
player.setMeta(PlotPlayer.META_LAST_PLOT, 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 public boolean canExecute(PlotPlayer player, boolean message) {
return true;
2016-02-23 05:11:28 +01:00
}
}