2020-04-16 04:52:39 +02:00
|
|
|
/*
|
|
|
|
* _____ _ _ _____ _
|
|
|
|
* | __ \| | | | / ____| | |
|
|
|
|
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
|
|
|
|
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
|
|
|
|
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
|
|
|
|
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
|
|
|
|
* | |
|
|
|
|
* |_|
|
|
|
|
* PlotSquared plot management system for Minecraft
|
|
|
|
* Copyright (C) 2020 IntellectualSites
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-04-15 21:26:54 +02:00
|
|
|
package com.plotsquared.core.command;
|
2015-01-24 01:00:57 +01:00
|
|
|
|
2020-04-30 12:33:59 +02:00
|
|
|
import com.google.common.io.Files;
|
2020-07-14 18:49:40 +02:00
|
|
|
import com.google.inject.Inject;
|
2020-07-21 21:39:52 +02:00
|
|
|
import com.google.inject.internal.cglib.transform.$ClassTransformer;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.PlotSquared;
|
2020-04-16 06:14:33 +02:00
|
|
|
import com.plotsquared.core.configuration.Captions;
|
|
|
|
import com.plotsquared.core.configuration.Settings;
|
2020-07-21 21:39:52 +02:00
|
|
|
import com.plotsquared.core.configuration.caption.StaticCaption;
|
2020-07-18 15:06:51 +02:00
|
|
|
import com.plotsquared.core.configuration.caption.TranslatableCaption;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.database.DBFunc;
|
|
|
|
import com.plotsquared.core.events.PlotFlagRemoveEvent;
|
|
|
|
import com.plotsquared.core.events.Result;
|
|
|
|
import com.plotsquared.core.generator.HybridUtils;
|
|
|
|
import com.plotsquared.core.location.Location;
|
2020-04-30 12:33:59 +02:00
|
|
|
import com.plotsquared.core.player.ConsolePlayer;
|
|
|
|
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;
|
|
|
|
import com.plotsquared.core.plot.PlotId;
|
2020-04-30 12:33:59 +02:00
|
|
|
import com.plotsquared.core.plot.expiration.ExpireManager;
|
|
|
|
import com.plotsquared.core.plot.expiration.PlotAnalysis;
|
|
|
|
import com.plotsquared.core.plot.flag.GlobalFlagContainer;
|
|
|
|
import com.plotsquared.core.plot.flag.PlotFlag;
|
2020-07-10 17:32:07 +02:00
|
|
|
import com.plotsquared.core.plot.world.PlotAreaManager;
|
2020-04-30 12:33:59 +02:00
|
|
|
import com.plotsquared.core.queue.GlobalBlockQueue;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.util.ChunkManager;
|
|
|
|
import com.plotsquared.core.util.EconHandler;
|
2020-07-10 18:17:44 +02:00
|
|
|
import com.plotsquared.core.util.EventDispatcher;
|
2020-07-19 14:23:55 +02:00
|
|
|
import com.plotsquared.core.util.FileUtils;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.util.MathMan;
|
|
|
|
import com.plotsquared.core.util.SchematicHandler;
|
|
|
|
import com.plotsquared.core.util.SetupUtils;
|
|
|
|
import com.plotsquared.core.util.StringMan;
|
2020-04-30 12:33:59 +02:00
|
|
|
import com.plotsquared.core.util.WEManager;
|
|
|
|
import com.plotsquared.core.util.WorldUtil;
|
|
|
|
import com.plotsquared.core.util.task.RunnableVal;
|
|
|
|
import com.plotsquared.core.util.task.RunnableVal2;
|
|
|
|
import com.plotsquared.core.util.task.RunnableVal3;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.util.task.TaskManager;
|
2020-07-10 19:25:05 +02:00
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
2019-11-04 20:58:24 +01:00
|
|
|
import com.sk89q.worldedit.world.block.BlockState;
|
2020-07-21 21:39:52 +02:00
|
|
|
import net.kyori.adventure.text.minimessage.Template;
|
2020-07-06 17:17:46 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-08-10 17:01:10 +02:00
|
|
|
|
2020-07-17 17:24:45 +02:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
2019-05-18 00:49:09 +02:00
|
|
|
import javax.script.Bindings;
|
|
|
|
import javax.script.ScriptContext;
|
|
|
|
import javax.script.ScriptEngine;
|
|
|
|
import javax.script.ScriptEngineManager;
|
|
|
|
import javax.script.ScriptException;
|
|
|
|
import javax.script.SimpleScriptContext;
|
2016-03-01 20:56:50 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2020-04-30 12:33:59 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
2019-01-21 09:20:33 +01:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
2016-03-01 20:56:50 +01:00
|
|
|
|
2020-02-24 18:42:02 +01:00
|
|
|
@CommandDeclaration(command = "debugexec",
|
|
|
|
permission = "plots.admin",
|
|
|
|
description = "Mutli-purpose debug command",
|
|
|
|
aliases = {"exec", "$"},
|
|
|
|
category = CommandCategory.DEBUG)
|
|
|
|
public class DebugExec extends SubCommand {
|
2020-07-10 17:32:07 +02:00
|
|
|
|
2020-07-13 22:51:57 +02:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger("P2/" + DebugExec.class.getSimpleName());
|
2020-07-06 17:17:46 +02:00
|
|
|
|
2020-07-10 17:32:07 +02:00
|
|
|
private final PlotAreaManager plotAreaManager;
|
2020-07-10 18:17:44 +02:00
|
|
|
private final EventDispatcher eventDispatcher;
|
2020-07-10 19:25:05 +02:00
|
|
|
private final WorldEdit worldEdit;
|
2020-07-14 18:49:40 +02:00
|
|
|
private final GlobalBlockQueue blockQueue;
|
|
|
|
private final SchematicHandler schematicHandler;
|
|
|
|
private final EconHandler econHandler;
|
|
|
|
private final ChunkManager chunkManager;
|
|
|
|
private final WorldUtil worldUtil;
|
|
|
|
private final SetupUtils setupUtils;
|
|
|
|
private final HybridUtils hybridUtils;
|
|
|
|
|
2015-08-01 18:12:49 +02:00
|
|
|
private ScriptEngine engine;
|
|
|
|
private Bindings scope;
|
2016-03-22 18:53:17 +01:00
|
|
|
|
2020-07-14 18:49:40 +02:00
|
|
|
@Inject public DebugExec(@Nonnull final PlotAreaManager plotAreaManager,
|
|
|
|
@Nonnull final EventDispatcher eventDispatcher,
|
|
|
|
@Nullable final WorldEdit worldEdit,
|
|
|
|
@Nonnull final GlobalBlockQueue blockQueue,
|
|
|
|
@Nonnull final SchematicHandler schematicHandler,
|
|
|
|
@Nullable final EconHandler econHandler,
|
|
|
|
@Nonnull final ChunkManager chunkManager,
|
|
|
|
@Nonnull final WorldUtil worldUtil,
|
|
|
|
@Nonnull final SetupUtils setupUtils,
|
|
|
|
@Nonnull final HybridUtils hybridUtils) {
|
2020-07-10 17:32:07 +02:00
|
|
|
this.plotAreaManager = plotAreaManager;
|
2020-07-10 18:17:44 +02:00
|
|
|
this.eventDispatcher = eventDispatcher;
|
2020-07-10 19:25:05 +02:00
|
|
|
this.worldEdit = worldEdit;
|
2020-07-14 18:49:40 +02:00
|
|
|
this.blockQueue = blockQueue;
|
|
|
|
this.schematicHandler = schematicHandler;
|
|
|
|
this.econHandler = econHandler;
|
|
|
|
this.chunkManager = chunkManager;
|
|
|
|
this.worldUtil = worldUtil;
|
|
|
|
this.setupUtils = setupUtils;
|
|
|
|
this.hybridUtils = hybridUtils;
|
|
|
|
|
2019-03-06 18:47:38 +01:00
|
|
|
init();
|
2015-08-06 18:05:15 +02:00
|
|
|
}
|
2016-03-22 18:53:17 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public ScriptEngine getEngine() {
|
2016-06-24 07:41:27 +02:00
|
|
|
if (this.engine == null) {
|
|
|
|
init();
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
return this.engine;
|
2015-08-14 00:52:31 +02:00
|
|
|
}
|
2016-03-22 18:53:17 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public Bindings getScope() {
|
2016-03-23 02:41:37 +01:00
|
|
|
return this.scope;
|
2015-08-14 00:52:31 +02:00
|
|
|
}
|
2016-03-22 18:53:17 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public void init() {
|
2016-03-23 02:41:37 +01:00
|
|
|
if (this.engine != null) {
|
2015-09-13 06:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-03-15 02:49:33 +01:00
|
|
|
//create script engine manager
|
|
|
|
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
|
|
|
|
//create nashorn engine
|
|
|
|
this.engine = scriptEngineManager.getEngineByName("nashorn");
|
|
|
|
try {
|
2020-04-25 23:53:46 +02:00
|
|
|
engine.eval("print('PlotSquared scripting engine started');");
|
2019-03-15 02:49:33 +01:00
|
|
|
} catch (ScriptException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
if (this.engine == null) {
|
|
|
|
this.engine = new ScriptEngineManager(null).getEngineByName("JavaScript");
|
2015-08-03 16:54:35 +02:00
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
ScriptContext context = new SimpleScriptContext();
|
|
|
|
this.scope = context.getBindings(ScriptContext.ENGINE_SCOPE);
|
2016-03-22 18:53:17 +01:00
|
|
|
|
2015-08-01 18:12:49 +02:00
|
|
|
// stuff
|
2016-03-23 02:41:37 +01:00
|
|
|
this.scope.put("Settings", new Settings());
|
|
|
|
this.scope.put("StringMan", new StringMan());
|
|
|
|
this.scope.put("MathMan", new MathMan());
|
2016-03-22 18:53:17 +01:00
|
|
|
|
2015-08-01 18:12:49 +02:00
|
|
|
// Classes
|
2016-03-23 02:41:37 +01:00
|
|
|
this.scope.put("Location", Location.class);
|
2019-11-04 20:55:55 +01:00
|
|
|
this.scope.put("BlockState", BlockState.class);
|
2016-03-23 02:41:37 +01:00
|
|
|
this.scope.put("Plot", Plot.class);
|
|
|
|
this.scope.put("PlotId", PlotId.class);
|
|
|
|
this.scope.put("Runnable", Runnable.class);
|
|
|
|
this.scope.put("RunnableVal", RunnableVal.class);
|
2016-03-22 18:53:17 +01:00
|
|
|
|
2015-08-01 18:12:49 +02:00
|
|
|
// Instances
|
2018-11-14 14:19:56 +01:00
|
|
|
this.scope.put("PS", PlotSquared.get());
|
2020-07-14 18:49:40 +02:00
|
|
|
this.scope.put("GlobalBlockQueue", this.blockQueue);
|
2016-03-23 02:41:37 +01:00
|
|
|
this.scope.put("ExpireManager", ExpireManager.IMP);
|
2020-07-10 19:25:05 +02:00
|
|
|
if (this.worldEdit != null) {
|
2016-03-23 02:41:37 +01:00
|
|
|
this.scope.put("WEManager", new WEManager());
|
2016-03-07 07:34:57 +01:00
|
|
|
}
|
2020-07-17 16:38:07 +02:00
|
|
|
this.scope.put("TaskManager", TaskManager.getPlatformImplementation());
|
2016-03-23 02:41:37 +01:00
|
|
|
this.scope.put("ConsolePlayer", ConsolePlayer.getConsole());
|
2020-07-14 18:49:40 +02:00
|
|
|
this.scope.put("SchematicHandler", this.schematicHandler);
|
|
|
|
this.scope.put("ChunkManager", this.chunkManager);
|
|
|
|
this.scope.put("BlockManager", this.worldUtil);
|
|
|
|
this.scope.put("SetupUtils", this.setupUtils);
|
2020-07-10 18:17:44 +02:00
|
|
|
this.scope.put("EventUtil", this.eventDispatcher);
|
2020-07-14 18:49:40 +02:00
|
|
|
this.scope.put("EconHandler", this.econHandler);
|
2016-03-23 02:41:37 +01:00
|
|
|
this.scope.put("DBFunc", DBFunc.dbManager);
|
2020-07-14 18:49:40 +02:00
|
|
|
this.scope.put("HybridUtils", this.hybridUtils);
|
2020-07-07 12:56:43 +02:00
|
|
|
this.scope.put("IMP", PlotSquared.platform());
|
2016-03-23 02:41:37 +01:00
|
|
|
this.scope.put("MainCommand", MainCommand.getInstance());
|
2016-03-22 18:53:17 +01:00
|
|
|
|
2015-08-01 18:12:49 +02:00
|
|
|
// enums
|
2019-02-22 17:51:06 +01:00
|
|
|
for (Enum<?> value : Captions.values()) {
|
2016-03-23 02:41:37 +01:00
|
|
|
this.scope.put("C_" + value.name(), value);
|
2015-08-01 18:12:49 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-22 18:53:17 +01:00
|
|
|
|
2020-05-27 14:08:48 +02:00
|
|
|
@Override public boolean onCommand(final PlotPlayer<?> player, String[] args) {
|
2018-08-10 17:01:10 +02:00
|
|
|
List<String> allowed_params = Arrays
|
|
|
|
.asList("analyze", "calibrate-analysis", "remove-flag", "stop-expire", "start-expire",
|
|
|
|
"seen", "list-scripts", "start-rgar", "stop-rgar", "help", "addcmd", "runasync",
|
|
|
|
"run", "allcmd", "all");
|
2015-09-13 06:04:31 +02:00
|
|
|
if (args.length > 0) {
|
2016-03-23 02:41:37 +01:00
|
|
|
String arg = args[0].toLowerCase();
|
2015-08-01 18:12:49 +02:00
|
|
|
String script;
|
|
|
|
boolean async = false;
|
2015-09-13 06:04:31 +02:00
|
|
|
switch (arg) {
|
|
|
|
case "analyze": {
|
2016-03-23 02:41:37 +01:00
|
|
|
Plot plot = player.getCurrentPlot();
|
2015-09-13 06:04:31 +02:00
|
|
|
if (plot == null) {
|
2020-07-18 15:06:51 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
|
2015-07-18 07:02:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-05 09:28:34 +02:00
|
|
|
PlotAnalysis analysis = plot.getComplexity(null);
|
2015-09-13 06:04:31 +02:00
|
|
|
if (analysis != null) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("debugexec.changes_column"),
|
2020-07-22 00:08:51 +02:00
|
|
|
Template.of("value", String.valueOf(analysis.changes / 1.0))
|
2020-07-21 21:39:52 +02:00
|
|
|
);
|
2015-07-18 07:02:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("debugexec.starting_task"));
|
2020-07-14 18:49:40 +02:00
|
|
|
this.hybridUtils.analyzePlot(plot, new RunnableVal<PlotAnalysis>() {
|
2018-08-10 17:01:10 +02:00
|
|
|
@Override public void run(PlotAnalysis value) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(StaticCaption.of("&6Done: &7Use &6/plot debugexec analyze &7for more information."));
|
2015-06-28 02:54:57 +02:00
|
|
|
}
|
|
|
|
});
|
2015-06-26 11:46:06 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
case "calibrate-analysis":
|
2015-09-13 06:04:31 +02:00
|
|
|
if (args.length != 2) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("commandconfig.command_syntax"),
|
|
|
|
Template.of("value", "/plot debugexec analyze <threshold>")
|
|
|
|
);
|
|
|
|
player.sendMessage(TranslatableCaption.of("debugexec.threshold_default"));
|
2015-07-18 07:02:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
double threshold;
|
2015-09-13 06:04:31 +02:00
|
|
|
try {
|
2015-07-18 07:02:44 +02:00
|
|
|
threshold = Integer.parseInt(args[1]) / 100d;
|
2016-05-12 23:09:35 +02:00
|
|
|
} catch (NumberFormatException ignored) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("debugexec.invalid_threshold"),
|
|
|
|
Template.of("value", args[1])
|
|
|
|
);
|
|
|
|
player.sendMessage(TranslatableCaption.of("debugexec.threshold_default_double"));
|
2015-07-18 07:02:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-21 21:39:52 +02:00
|
|
|
PlotAnalysis.calcOptimalModifiers(() -> player.sendMessage(TranslatableCaption.of("debugexec.calibration_done")), threshold);
|
2015-07-18 07:02:44 +02:00
|
|
|
return true;
|
2016-02-14 02:01:18 +01:00
|
|
|
case "stop-expire":
|
2016-03-01 20:56:50 +01:00
|
|
|
if (ExpireManager.IMP == null || !ExpireManager.IMP.cancelTask()) {
|
2020-07-21 21:39:52 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("debugexec.task_halted"));
|
2015-02-20 07:34:19 +01:00
|
|
|
}
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("debugexec.task_cancelled"));
|
2016-02-14 02:01:18 +01:00
|
|
|
case "remove-flag":
|
2015-09-13 06:04:31 +02:00
|
|
|
if (args.length != 2) {
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("commandconfig.command_syntax"),
|
|
|
|
Template.of("value", "/plot debugexec remove-flag <flag>")
|
|
|
|
);
|
2015-06-29 21:30:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
String flag = args[1];
|
2020-02-24 18:42:02 +01:00
|
|
|
final PlotFlag<?, ?> flagInstance =
|
|
|
|
GlobalFlagContainer.getInstance().getFlagFromString(flag);
|
|
|
|
if (flagInstance != null) {
|
|
|
|
for (Plot plot : PlotSquared.get().getBasePlots()) {
|
2020-07-10 18:17:44 +02:00
|
|
|
PlotFlagRemoveEvent event = this.eventDispatcher
|
2020-03-15 12:22:49 +01:00
|
|
|
.callFlagRemove(flagInstance, plot);
|
|
|
|
if (event.getEventResult() != Result.DENY) {
|
|
|
|
plot.removeFlag(event.getFlag());
|
|
|
|
}
|
2015-06-24 22:33:43 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("debugexec.cleared_flag"),
|
|
|
|
Template.of("value", flag)
|
|
|
|
);
|
2015-09-13 06:04:31 +02:00
|
|
|
case "start-rgar": {
|
|
|
|
if (args.length != 2) {
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("commandconfig.command_syntax"),
|
|
|
|
Template.of("value", "Invalid syntax: /plot debugexec start-rgar <world>")
|
|
|
|
);
|
2015-06-07 21:37:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-07-10 17:32:07 +02:00
|
|
|
PlotArea area = this.plotAreaManager.getPlotAreaByString(args[1]);
|
2016-02-10 19:59:51 +01:00
|
|
|
if (area == null) {
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("errors.not_valid_plot_world"),
|
|
|
|
Template.of("value", args[1])
|
|
|
|
);
|
2015-06-11 02:56:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
boolean result;
|
2015-09-13 06:04:31 +02:00
|
|
|
if (HybridUtils.regions != null) {
|
2020-07-14 18:49:40 +02:00
|
|
|
result = this.hybridUtils.scheduleRoadUpdate(area, HybridUtils.regions, 0, new HashSet<>());
|
2015-09-13 06:04:31 +02:00
|
|
|
} else {
|
2020-07-14 18:49:40 +02:00
|
|
|
result = this.hybridUtils.scheduleRoadUpdate(area, 0);
|
2015-06-07 21:37:40 +02:00
|
|
|
}
|
2015-09-13 06:04:31 +02:00
|
|
|
if (!result) {
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("debugexec.mass_schematic_update_in_progress"));
|
2015-06-07 21:37:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
case "stop-rgar":
|
2015-09-13 06:04:31 +02:00
|
|
|
if (!HybridUtils.UPDATE) {
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("debugexec.task_not_running"));
|
2015-06-07 21:37:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-02 13:56:18 +02:00
|
|
|
HybridUtils.UPDATE = false;
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("debugexec.cancelling_task"));
|
2015-06-07 21:37:40 +02:00
|
|
|
return true;
|
2016-02-14 02:01:18 +01:00
|
|
|
case "start-expire":
|
2016-03-01 20:56:50 +01:00
|
|
|
if (ExpireManager.IMP == null) {
|
2020-07-10 18:17:44 +02:00
|
|
|
ExpireManager.IMP = new ExpireManager(this.eventDispatcher);
|
2015-02-20 07:34:19 +01:00
|
|
|
}
|
2016-06-05 09:28:34 +02:00
|
|
|
if (ExpireManager.IMP.runAutomatedTask()) {
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("debugexec.expiry_started"));
|
2016-03-01 20:56:50 +01:00
|
|
|
} else {
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("debugexec.expiry_already_started"));
|
2015-02-20 07:34:19 +01:00
|
|
|
}
|
2015-08-01 18:12:49 +02:00
|
|
|
case "h":
|
|
|
|
case "he":
|
|
|
|
case "?":
|
2016-02-14 02:01:18 +01:00
|
|
|
case "help":
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(StaticCaption.of("Possible sub commands: /plot debugexec <" + StringMan.join(allowed_params, "|") + ">"));
|
2015-08-01 18:12:49 +02:00
|
|
|
return false;
|
2016-02-14 02:01:18 +01:00
|
|
|
case "addcmd":
|
2015-09-13 06:04:31 +02:00
|
|
|
try {
|
2020-07-19 14:23:55 +02:00
|
|
|
final String cmd = StringMan.join(Files.readLines(FileUtils.getFile(new File(
|
2020-07-07 12:56:43 +02:00
|
|
|
PlotSquared.platform().getDirectory() + File.separator
|
2018-11-14 15:44:07 +01:00
|
|
|
+ Settings.Paths.SCRIPTS), args[1]), StandardCharsets.UTF_8),
|
2018-08-10 17:01:10 +02:00
|
|
|
System.getProperty("line.separator"));
|
|
|
|
new Command(MainCommand.getInstance(), true, args[1].split("\\.")[0], null,
|
|
|
|
RequiredType.NONE, CommandCategory.DEBUG) {
|
2020-04-30 12:33:59 +02:00
|
|
|
@Override
|
2020-05-27 14:08:48 +02:00
|
|
|
public CompletableFuture<Boolean> execute(PlotPlayer<?> player,
|
2020-02-24 18:42:02 +01:00
|
|
|
String[] args, RunnableVal3<Command, Runnable, Runnable> confirm,
|
2018-08-10 17:01:10 +02:00
|
|
|
RunnableVal2<Command, CommandResult> whenDone) {
|
2015-09-13 06:04:31 +02:00
|
|
|
try {
|
2016-03-24 10:57:59 +01:00
|
|
|
DebugExec.this.scope.put("PlotPlayer", player);
|
2016-03-23 02:41:37 +01:00
|
|
|
DebugExec.this.scope.put("args", args);
|
|
|
|
DebugExec.this.engine.eval(cmd, DebugExec.this.scope);
|
|
|
|
} catch (ScriptException e) {
|
2015-09-11 12:09:22 +02:00
|
|
|
e.printStackTrace();
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("error.command_went_wrong"));
|
2015-09-11 12:09:22 +02:00
|
|
|
}
|
2019-01-21 09:20:33 +01:00
|
|
|
return CompletableFuture.completedFuture(true);
|
2015-08-06 18:05:15 +02:00
|
|
|
}
|
2015-09-11 12:09:22 +02:00
|
|
|
};
|
|
|
|
return true;
|
2016-02-14 02:01:18 +01:00
|
|
|
} catch (IOException e) {
|
2015-08-06 18:05:15 +02:00
|
|
|
e.printStackTrace();
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("commandconfig.command_syntax"),
|
|
|
|
Template.of("value", "/plot debugexec addcmd <file>")
|
|
|
|
);
|
2015-08-06 18:05:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-02-14 02:01:18 +01:00
|
|
|
case "runasync":
|
2015-08-01 18:12:49 +02:00
|
|
|
async = true;
|
2016-02-14 02:01:18 +01:00
|
|
|
case "run":
|
2015-09-13 06:04:31 +02:00
|
|
|
try {
|
2020-07-19 14:23:55 +02:00
|
|
|
script = StringMan.join(Files.readLines(FileUtils.getFile(new File(
|
2020-07-07 12:56:43 +02:00
|
|
|
PlotSquared.platform().getDirectory() + File.separator
|
2018-11-14 15:44:07 +01:00
|
|
|
+ Settings.Paths.SCRIPTS), args[1]), StandardCharsets.UTF_8),
|
2018-08-10 17:01:10 +02:00
|
|
|
System.getProperty("line.separator"));
|
2015-09-13 06:04:31 +02:00
|
|
|
if (args.length > 2) {
|
2016-03-23 02:41:37 +01:00
|
|
|
HashMap<String, String> replacements = new HashMap<>();
|
2015-09-13 06:04:31 +02:00
|
|
|
for (int i = 2; i < args.length; i++) {
|
2015-09-11 12:09:22 +02:00
|
|
|
replacements.put("%s" + (i - 2), args[i]);
|
2015-08-01 18:12:49 +02:00
|
|
|
}
|
|
|
|
script = StringMan.replaceFromMap(script, replacements);
|
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
} catch (IOException e) {
|
2015-08-01 18:12:49 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2016-03-30 14:42:34 +02:00
|
|
|
case "list-scripts":
|
2020-07-07 12:56:43 +02:00
|
|
|
String path = PlotSquared.platform().getDirectory() + File.separator
|
2018-11-14 15:44:07 +01:00
|
|
|
+ Settings.Paths.SCRIPTS;
|
2016-03-30 14:42:34 +02:00
|
|
|
File folder = new File(path);
|
|
|
|
File[] filesArray = folder.listFiles();
|
|
|
|
|
|
|
|
int page;
|
|
|
|
switch (args.length) {
|
|
|
|
case 1:
|
|
|
|
page = 0;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (MathMan.isInteger(args[1])) {
|
|
|
|
page = Integer.parseInt(args[1]) - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("commandconfig.command_syntax"),
|
|
|
|
Template.of("value", "/plot debugexec list-scripts [#]")
|
|
|
|
);
|
2016-03-30 14:42:34 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<File> allFiles = Arrays.asList(filesArray);
|
2018-08-10 17:01:10 +02:00
|
|
|
paginate(player, allFiles, 8, page,
|
|
|
|
new RunnableVal3<Integer, File, PlotMessage>() {
|
2016-03-30 14:42:34 +02:00
|
|
|
|
2018-08-10 17:01:10 +02:00
|
|
|
@Override public void run(Integer i, File file, PlotMessage message) {
|
|
|
|
String name = file.getName();
|
|
|
|
message.text("[").color("$3").text(String.valueOf(i)).color("$1")
|
|
|
|
.text("]").color("$3").text(' ' + name).color("$1");
|
|
|
|
}
|
|
|
|
}, "/plot debugexec list-scripts", "List of scripts");
|
2016-03-30 14:42:34 +02:00
|
|
|
return true;
|
2016-02-14 02:01:18 +01:00
|
|
|
case "all":
|
2015-12-04 10:00:30 +01:00
|
|
|
if (args.length < 3) {
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("commandconfig.command_syntax"),
|
|
|
|
Template.of("value", "/plot debugexec all <condition> <code>")
|
|
|
|
);
|
2015-12-04 10:00:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-10 17:01:10 +02:00
|
|
|
script =
|
|
|
|
"_1=PS.getBasePlots().iterator();while(_1.hasNext()){plot=_1.next();if("
|
|
|
|
+ args[1] + "){" + StringMan
|
|
|
|
.join(Arrays.copyOfRange(args, 2, args.length), " ") + "}}";
|
2016-02-14 02:01:18 +01:00
|
|
|
|
2015-12-04 10:00:30 +01:00
|
|
|
break;
|
2016-02-14 02:01:18 +01:00
|
|
|
default:
|
2015-08-01 18:12:49 +02:00
|
|
|
script = StringMan.join(args, " ");
|
|
|
|
}
|
2016-04-02 01:14:46 +02:00
|
|
|
if (!(player instanceof ConsolePlayer)) {
|
2020-07-22 00:08:51 +02:00
|
|
|
player.sendMessage(TranslatableCaption.of("console.not_console"));
|
2015-08-01 20:01:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-01 18:12:49 +02:00
|
|
|
init();
|
2016-03-23 02:41:37 +01:00
|
|
|
this.scope.put("PlotPlayer", player);
|
2015-09-13 06:04:31 +02:00
|
|
|
try {
|
|
|
|
if (async) {
|
2015-08-01 18:12:49 +02:00
|
|
|
final String toExec = script;
|
2019-01-12 20:56:11 +01:00
|
|
|
TaskManager.runTaskAsync(() -> {
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
Object result = null;
|
|
|
|
try {
|
|
|
|
result = DebugExec.this.engine.eval(toExec, DebugExec.this.scope);
|
|
|
|
} catch (ScriptException e) {
|
|
|
|
e.printStackTrace();
|
2015-08-01 18:12:49 +02:00
|
|
|
}
|
2020-07-13 23:04:27 +02:00
|
|
|
logger.info("[P2] > {}ms -> {}", System.currentTimeMillis() - start, result);
|
2015-08-01 18:12:49 +02:00
|
|
|
});
|
2015-09-13 06:04:31 +02:00
|
|
|
} else {
|
2016-03-23 02:41:37 +01:00
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
Object result = this.engine.eval(script, this.scope);
|
2020-07-13 23:04:27 +02:00
|
|
|
logger.info("[P2] > {}ms -> {}", System.currentTimeMillis() - start, result);
|
2015-08-01 18:12:49 +02:00
|
|
|
}
|
|
|
|
return true;
|
2016-03-23 02:41:37 +01:00
|
|
|
} catch (ScriptException e) {
|
2015-08-01 18:12:49 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
2015-02-20 07:34:19 +01:00
|
|
|
}
|
2015-01-24 01:00:57 +01:00
|
|
|
}
|
2015-08-01 18:12:49 +02:00
|
|
|
return false;
|
2015-01-24 01:00:57 +01:00
|
|
|
}
|
|
|
|
}
|