PlotSquared/Core/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java

449 lines
23 KiB
Java
Raw Normal View History

2015-01-24 01:00:57 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// 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, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.commands;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;
import com.google.common.io.Files;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.database.DBFunc;
2015-06-24 22:33:43 +02:00
import com.intellectualcrafters.plot.flag.FlagManager;
import com.intellectualcrafters.plot.generator.HybridUtils;
import com.intellectualcrafters.plot.object.ConsolePlayer;
import com.intellectualcrafters.plot.object.Location;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.object.OfflinePlotPlayer;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotAnalysis;
2016-02-10 19:59:51 +01:00
import com.intellectualcrafters.plot.object.PlotArea;
import com.intellectualcrafters.plot.object.PlotBlock;
import com.intellectualcrafters.plot.object.PlotId;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.RunnableVal;
import com.intellectualcrafters.plot.util.AbstractTitle;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.util.ChunkManager;
2015-08-01 22:11:28 +02:00
import com.intellectualcrafters.plot.util.EconHandler;
import com.intellectualcrafters.plot.util.EventUtil;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.util.ExpireManager;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.MathMan;
import com.intellectualcrafters.plot.util.SchematicHandler;
import com.intellectualcrafters.plot.util.SetupUtils;
2015-07-30 19:24:01 +02:00
import com.intellectualcrafters.plot.util.StringMan;
import com.intellectualcrafters.plot.util.TaskManager;
2015-07-30 16:25:16 +02:00
import com.intellectualcrafters.plot.util.UUIDHandler;
2016-02-10 19:59:51 +01:00
import com.intellectualcrafters.plot.util.WorldUtil;
import com.plotsquared.general.commands.Command;
2015-07-27 19:50:04 +02:00
import com.plotsquared.general.commands.CommandDeclaration;
2015-07-28 08:06:19 +02:00
@CommandDeclaration(command = "debugexec", permission = "plots.admin", description = "Mutli-purpose debug command", aliases = "exec",
category = CommandCategory.DEBUG)
2015-09-13 06:04:31 +02:00
public class DebugExec extends SubCommand {
private ScriptEngine engine;
private Bindings scope;
2015-09-13 06:04:31 +02:00
public DebugExec() {
try {
if (PS.get() != null) {
final File file = new File(PS.get().IMP.getDirectory(), "scripts" + File.separator + "start.js");
if (file.exists()) {
init();
final String script = StringMan.join(Files
.readLines(new File(new File(PS.get().IMP.getDirectory() + File.separator + "scripts"), "start.js"),
StandardCharsets.UTF_8),
System.getProperty("line.separator"));
scope.put("THIS", this);
scope.put("PlotPlayer", ConsolePlayer.getConsole());
engine.eval(script, scope);
}
}
} catch (IOException | ScriptException e) {}
}
2015-09-13 06:04:31 +02:00
public ScriptEngine getEngine() {
2015-08-14 00:52:31 +02:00
return engine;
}
2015-09-13 06:04:31 +02:00
public Bindings getScope() {
2015-08-14 00:52:31 +02:00
return scope;
}
2015-09-13 06:04:31 +02:00
public void init() {
if (engine != null) {
return;
}
engine = new ScriptEngineManager(null).getEngineByName("nashorn");
2015-09-13 06:04:31 +02:00
if (engine == null) {
engine = new ScriptEngineManager(null).getEngineByName("JavaScript");
}
2015-09-11 12:09:22 +02:00
final ScriptContext context = new SimpleScriptContext();
scope = context.getBindings(ScriptContext.ENGINE_SCOPE);
2015-09-13 06:04:31 +02:00
// stuff
scope.put("MainUtil", new MainUtil());
scope.put("Settings", new Settings());
scope.put("StringMan", new StringMan());
scope.put("MathMan", new MathMan());
scope.put("FlagManager", new FlagManager());
2015-09-13 06:04:31 +02:00
// Classes
scope.put("Location", Location.class);
scope.put("PlotBlock", PlotBlock.class);
scope.put("Plot", Plot.class);
scope.put("PlotId", PlotId.class);
scope.put("Runnable", Runnable.class);
scope.put("RunnableVal", RunnableVal.class);
2015-09-13 06:04:31 +02:00
// Instances
scope.put("PS", PS.get());
scope.put("TaskManager", PS.get().TASK);
scope.put("TitleManager", AbstractTitle.TITLE_CLASS);
scope.put("ConsolePlayer", ConsolePlayer.getConsole());
scope.put("SchematicHandler", SchematicHandler.manager);
scope.put("ChunkManager", ChunkManager.manager);
2016-02-10 19:59:51 +01:00
scope.put("BlockManager", WorldUtil.IMP);
scope.put("SetupUtils", SetupUtils.manager);
scope.put("EventUtil", EventUtil.manager);
2015-08-01 22:11:28 +02:00
scope.put("EconHandler", EconHandler.manager);
scope.put("UUIDHandler", UUIDHandler.implementation);
scope.put("DBFunc", DBFunc.dbManager);
scope.put("HybridUtils", HybridUtils.manager);
scope.put("IMP", PS.get().IMP);
scope.put("MainCommand", MainCommand.getInstance());
2015-09-13 06:04:31 +02:00
// enums
2015-09-13 06:04:31 +02:00
for (final Enum<?> value : C.values()) {
scope.put("C_" + value.name(), value);
}
}
2015-09-13 06:04:31 +02:00
2015-01-24 01:00:57 +01:00
@Override
2015-09-13 06:04:31 +02:00
public boolean onCommand(final PlotPlayer player, final String... args) {
final List<String> allowed_params = Arrays.asList("calibrate-analysis", "remove-flag", "stop-expire", "start-expire", "show-expired", "update-expired", "seen");
2015-09-13 06:04:31 +02:00
if (args.length > 0) {
2015-02-20 07:34:19 +01:00
final String arg = args[0].toLowerCase();
String script;
boolean async = false;
2015-09-13 06:04:31 +02:00
switch (arg) {
case "analyze": {
2016-02-10 19:59:51 +01:00
final Plot plot = player.getCurrentPlot();
2015-09-13 06:04:31 +02:00
if (plot == null) {
MainUtil.sendMessage(player, C.NOT_IN_PLOT);
return false;
}
2015-09-11 12:09:22 +02:00
final PlotAnalysis analysis = plot.getComplexity();
2015-09-13 06:04:31 +02:00
if (analysis != null) {
2015-09-11 12:09:22 +02:00
final int complexity = analysis.getComplexity();
MainUtil.sendMessage(player, "Changes/column: " + analysis.changes / 1.0);
MainUtil.sendMessage(player, "Complexity: " + complexity);
return true;
}
MainUtil.sendMessage(player, "$1Starting task...");
2015-09-13 06:04:31 +02:00
HybridUtils.manager.analyzePlot(plot, new RunnableVal<PlotAnalysis>() {
@Override
2016-02-10 19:59:51 +01:00
public void run(PlotAnalysis value) {
MainUtil.sendMessage(player, "$1Done: $2use $3/plot debugexec analyze$2 for more information");
}
});
2015-06-26 11:46:06 +02:00
return true;
}
case "calibrate-analysis":
2015-09-13 06:04:31 +02:00
if (args.length != 2) {
MainUtil.sendMessage(player, C.COMMAND_SYNTAX, "/plot debugexec analyze <threshold>");
MainUtil.sendMessage(player, "$1<threshold> $2= $1The percentage of plots you want to clear (100 clears 100% of plots so no point calibrating it)");
return false;
}
double threshold;
2015-09-13 06:04:31 +02:00
try {
threshold = Integer.parseInt(args[1]) / 100d;
2015-09-13 06:04:31 +02:00
} catch (final NumberFormatException e) {
MainUtil.sendMessage(player, "$2Invalid threshold: " + args[1]);
MainUtil.sendMessage(player, "$1<threshold> $2= $1The percentage of plots you want to clear as a number between 0 - 100");
return false;
}
2015-09-13 06:04:31 +02:00
PlotAnalysis.calcOptimalModifiers(new Runnable() {
@Override
2015-09-13 06:04:31 +02:00
public void run() {
2015-07-30 16:25:16 +02:00
MainUtil.sendMessage(player, "$1Thank you for calibrating PlotSquared plot expiry");
}
}, threshold);
return true;
case "stop-expire":
2015-09-13 06:04:31 +02:00
if (ExpireManager.task != -1) {
PS.get().TASK.cancelTask(ExpireManager.task);
2015-09-13 06:04:31 +02:00
} else {
return MainUtil.sendMessage(player, "Task already halted");
2015-02-20 07:34:19 +01:00
}
ExpireManager.task = -1;
return MainUtil.sendMessage(player, "Cancelled task.");
case "remove-flag":
2015-09-13 06:04:31 +02:00
if (args.length != 2) {
MainUtil.sendMessage(player, C.COMMAND_SYNTAX, "/plot debugexec remove-flag <flag>");
return false;
}
2015-09-11 12:09:22 +02:00
final String flag = args[1];
2015-12-04 10:00:30 +01:00
for (final Plot plot : PS.get().getBasePlots()) {
2015-10-07 08:33:33 +02:00
if (FlagManager.getPlotFlagRaw(plot, flag) != null) {
FlagManager.removePlotFlag(plot, flag);
2015-06-24 22:33:43 +02:00
}
}
2015-09-11 12:09:22 +02:00
return MainUtil.sendMessage(player, "Cleared flag: " + flag);
2015-09-13 06:04:31 +02:00
case "start-rgar": {
if (args.length != 2) {
2015-07-30 16:25:16 +02:00
MainUtil.sendMessage(player, "&cInvalid syntax: /plot debugexec start-rgar <world>");
return false;
}
2016-02-10 19:59:51 +01:00
PlotArea area = PS.get().getPlotAreaByString(args[1]);
if (area == null) {
MainUtil.sendMessage(player, C.NOT_VALID_PLOT_WORLD, args[1]);
return false;
}
boolean result;
2015-09-13 06:04:31 +02:00
if (HybridUtils.regions != null) {
result = HybridUtils.manager.scheduleRoadUpdate(area, HybridUtils.regions, 0);
2015-09-13 06:04:31 +02:00
} else {
2016-02-10 19:59:51 +01:00
result = HybridUtils.manager.scheduleRoadUpdate(area, 0);
}
2015-09-13 06:04:31 +02:00
if (!result) {
2015-07-30 16:25:16 +02:00
MainUtil.sendMessage(player, "&cCannot schedule mass schematic update! (Is one already in progress?)");
return false;
}
return true;
}
case "stop-rgar":
2015-09-13 06:04:31 +02:00
if (!HybridUtils.UPDATE) {
2015-07-30 16:25:16 +02:00
MainUtil.sendMessage(player, "&cTASK NOT RUNNING!");
return false;
}
2015-08-02 13:56:18 +02:00
HybridUtils.UPDATE = false;
MainUtil.sendMessage(player, "&cCancelling task... (please wait)");
return true;
case "start-expire":
2015-09-13 06:04:31 +02:00
if (ExpireManager.task == -1) {
2015-02-20 07:34:19 +01:00
ExpireManager.runTask();
2015-09-13 06:04:31 +02:00
} else {
return MainUtil.sendMessage(player, "Plot expiry task already started");
2015-02-20 07:34:19 +01:00
}
return MainUtil.sendMessage(player, "Started plot expiry task");
case "update-expired":
2015-09-13 06:04:31 +02:00
if (args.length > 1) {
2016-02-10 19:59:51 +01:00
PlotArea area = PS.get().getPlotAreaByString(args[1]);
if (area == null || !WorldUtil.IMP.isWorld(area.worldname)) {
C.NOT_VALID_PLOT_WORLD.send(player, args[1]);
return false;
2015-09-13 06:04:31 +02:00
}
MainUtil.sendMessage(player, "Updating expired plot list");
2016-02-10 19:59:51 +01:00
ExpireManager.updateExpired(area);
2015-02-20 07:34:19 +01:00
return true;
}
return MainUtil.sendMessage(player, "Use /plot debugexec update-expired <world>");
case "show-expired":
2015-09-13 06:04:31 +02:00
if (args.length > 1) {
2015-02-22 07:42:52 +01:00
final String world = args[1];
2016-02-10 19:59:51 +01:00
if (!WorldUtil.IMP.isWorld(world)) {
2015-09-13 06:04:31 +02:00
return MainUtil.sendMessage(player, "Invalid world: " + args[1]);
}
if (!ExpireManager.expiredPlots.containsKey(args[1])) {
return MainUtil.sendMessage(player, "No task for world: " + args[1]);
}
MainUtil.sendMessage(player, "Expired plots (" + ExpireManager.expiredPlots.get(args[1]).size() + "):");
2015-09-13 06:04:31 +02:00
for (final Plot plot : ExpireManager.expiredPlots.get(args[1])) {
2016-02-10 19:59:51 +01:00
MainUtil.sendMessage(player,
2016-02-11 02:37:21 +01:00
" - " + plot.getArea() + ";" + plot.getId().x + ";" + plot.getId().y + ";" + UUIDHandler.getName(plot.owner) + " : " + ExpireManager.dates.get(plot.owner));
2015-02-20 07:34:19 +01:00
}
return true;
}
return MainUtil.sendMessage(player, "Use /plot debugexec show-expired <world>");
case "seen":
2015-09-13 06:04:31 +02:00
if (args.length != 2) {
return MainUtil.sendMessage(player, "Use /plot debugexec seen <player>");
}
2015-07-27 11:14:28 +02:00
final UUID uuid = UUIDHandler.getUUID(args[1], null);
2015-09-13 06:04:31 +02:00
if (uuid == null) {
return MainUtil.sendMessage(player, "player not found: " + args[1]);
}
2015-07-24 16:06:58 +02:00
final OfflinePlotPlayer op = UUIDHandler.getUUIDWrapper().getOfflinePlayer(uuid);
if (op == null || op.getLastPlayed() == 0) {
2015-09-13 06:04:31 +02:00
return MainUtil.sendMessage(player, "player hasn't connected before: " + args[1]);
}
2015-02-20 07:34:19 +01:00
final Timestamp stamp = new Timestamp(op.getLastPlayed());
final Date date = new Date(stamp.getTime());
MainUtil.sendMessage(player, "PLAYER: " + args[1]);
MainUtil.sendMessage(player, "UUID: " + uuid);
MainUtil.sendMessage(player, "Object: " + date.toGMTString());
MainUtil.sendMessage(player, "GMT: " + date.toGMTString());
MainUtil.sendMessage(player, "Local: " + date.toLocaleString());
2015-02-20 07:34:19 +01:00
return true;
case "h":
case "he":
case "?":
case "help":
MainUtil.sendMessage(player, "Possible sub commands: /plot debugexec <" + StringMan.join(allowed_params, "|") + ">");
return false;
case "addcmd":
2015-09-13 06:04:31 +02:00
try {
final String cmd = StringMan.join(Files
.readLines(new File(new File(PS.get().IMP.getDirectory() + File.separator + "scripts"), args[1]),
StandardCharsets.UTF_8),
System.getProperty("line.separator"));
2015-09-13 06:04:31 +02:00
final Command<PlotPlayer> subcommand = new Command<PlotPlayer>(args[1].split("\\.")[0]) {
2015-09-11 12:09:22 +02:00
@Override
2015-09-13 06:04:31 +02:00
public boolean onCommand(final PlotPlayer plr, final String[] args) {
try {
2015-09-11 12:09:22 +02:00
scope.put("PlotPlayer", plr);
scope.put("args", args);
engine.eval(cmd, scope);
return true;
2015-09-13 06:04:31 +02:00
} catch (final ScriptException e) {
2015-09-11 12:09:22 +02:00
e.printStackTrace();
MainUtil.sendMessage(player, C.COMMAND_WENT_WRONG);
return false;
}
}
2015-09-11 12:09:22 +02:00
};
MainCommand.getInstance().addCommand(subcommand);
return true;
} catch (IOException e) {
e.printStackTrace();
MainUtil.sendMessage(player, C.COMMAND_SYNTAX, "/plot debugexec addcmd <file>");
return false;
}
case "runasync":
async = true;
case "run":
2015-09-13 06:04:31 +02:00
try {
2015-09-11 12:09:22 +02:00
script = StringMan.join(Files.readLines(new File(new File(PS.get().IMP.getDirectory() + File.separator + "scripts"), args[1]), StandardCharsets.UTF_8),
System.getProperty("line.separator"));
2015-09-13 06:04:31 +02:00
if (args.length > 2) {
2015-09-11 12:09:22 +02:00
final 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]);
}
script = StringMan.replaceFromMap(script, replacements);
}
2015-09-13 06:04:31 +02:00
} catch (final IOException e) {
e.printStackTrace();
return false;
}
break;
case "allcmd":
2015-12-04 10:00:30 +01:00
if (args.length < 3) {
C.COMMAND_SYNTAX.send(player, "/plot debugexec allcmd <condition> <command>");
return false;
}
long start = System.currentTimeMillis();
Command<PlotPlayer> cmd = MainCommand.getInstance().getCommand(args[3]);
String[] params = Arrays.copyOfRange(args, 4, args.length);
if ("true".equals(args[1])) {
Location loc = player.getMeta("location");
Plot plot = player.getMeta("lastplot");
2015-12-04 10:00:30 +01:00
for (Plot current : PS.get().getBasePlots()) {
player.setMeta("location", current.getBottomAbs());
player.setMeta("lastplot", current);
cmd.onCommand(player, params);
}
if (loc == null) {
player.deleteMeta("location");
} else {
player.setMeta("location", loc);
}
if (plot == null) {
player.deleteMeta("lastplot");
} else {
player.setMeta("lastplot", plot);
}
player.sendMessage("&c> " + (System.currentTimeMillis() - start));
return true;
}
init();
scope.put("_2", params);
scope.put("_3", cmd);
script = "_1=PS.getBasePlots().iterator();while(_1.hasNext()){plot=_1.next();if(" + args[1] + "){PlotPlayer.setMeta(\"location\",plot.getBottomAbs());PlotPlayer.setMeta(\"lastplot\",plot);_3.onCommand(PlotPlayer,_2)}}";
2015-12-04 10:00:30 +01:00
break;
case "all":
2015-12-04 10:00:30 +01:00
if (args.length < 3) {
C.COMMAND_SYNTAX.send(player, "/plot debugexec all <condition> <code>");
return false;
}
script = "_1=PS.getBasePlots().iterator();while(_1.hasNext()){plot=_1.next();if(" + args[1] + "){" + StringMan
.join(Arrays.copyOfRange(args, 2, args.length), " ")
+ "}}";
2015-12-04 10:00:30 +01:00
break;
default:
script = StringMan.join(args, " ");
}
2015-09-13 06:04:31 +02:00
if (!ConsolePlayer.isConsole(player)) {
2015-08-01 20:01:41 +02:00
MainUtil.sendMessage(player, C.NOT_CONSOLE);
return false;
}
init();
scope.put("PlotPlayer", player);
PS.debug("> " + script);
2015-09-13 06:04:31 +02:00
try {
if (async) {
final String toExec = script;
2015-09-13 06:04:31 +02:00
TaskManager.runTaskAsync(new Runnable() {
@Override
2015-09-13 06:04:31 +02:00
public void run() {
2015-09-11 12:09:22 +02:00
final long start = System.currentTimeMillis();
2015-09-27 08:43:11 +02:00
Object result = null;
2015-09-13 06:04:31 +02:00
try {
2015-09-27 08:43:11 +02:00
result = engine.eval(toExec, scope);
2015-09-13 06:04:31 +02:00
} catch (final ScriptException e) {
e.printStackTrace();
}
2015-10-19 08:27:51 +02:00
ConsolePlayer.getConsole().sendMessage("> " + (System.currentTimeMillis() - start) + "ms -> " + result);
}
});
2015-09-13 06:04:31 +02:00
} else {
2015-09-11 12:09:22 +02:00
final long start = System.currentTimeMillis();
2015-09-27 08:43:11 +02:00
Object result = engine.eval(script, scope);
2015-10-19 08:27:51 +02:00
ConsolePlayer.getConsole().sendMessage("> " + (System.currentTimeMillis() - start) + "ms -> " + result);
}
return true;
2015-09-13 06:04:31 +02:00
} catch (final ScriptException e) {
e.printStackTrace();
return false;
2015-02-20 07:34:19 +01:00
}
2015-01-24 01:00:57 +01:00
}
return false;
2015-01-24 01:00:57 +01:00
}
}