From 0a3ec5dcd80e70c63b10da7127e1279319f8aec2 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Sun, 2 Aug 2015 02:12:49 +1000 Subject: [PATCH] Scripting stuff + remove PlotSquared-Null --- pom.xml | 17 +- .../com/intellectualcrafters/plot/PS.java | 20 ++- .../plot/commands/DebugExec.java | 149 +++++++++++++++++- .../plot/commands/Limit.java | 77 +++++++++ .../plot/commands/MainCommand.java | 1 + .../plot/object/Plot.java | 25 ++- .../plot/util/EconHandler.java | 2 + .../plot/util/ExpireManager.java | 2 +- .../plot/util/MainUtil.java | 29 +++- .../bukkit/listeners/PlayerEvents.java | 2 +- .../sponge/util/SpongeBlockManager.java | 3 + 11 files changed, 295 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/intellectualcrafters/plot/commands/Limit.java diff --git a/pom.xml b/pom.xml index d2267de9f..50027f1b3 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ UTF-8 PlotSquared - 3.0.4 + 3.0.5 PlotSquared jar @@ -67,21 +67,6 @@ - - api - - jar - - package - - PlotSquared-Null - - **/com/plotsquared/bukkit/** - **/com/intellectualcrafters/plot/api/* - **/com/plotsquared/sponge/** - - - diff --git a/src/main/java/com/intellectualcrafters/plot/PS.java b/src/main/java/com/intellectualcrafters/plot/PS.java index d442e8762..53d638dc0 100644 --- a/src/main/java/com/intellectualcrafters/plot/PS.java +++ b/src/main/java/com/intellectualcrafters/plot/PS.java @@ -7,6 +7,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.URI; import java.net.URL; import java.net.URLConnection; import java.nio.file.Files; @@ -117,6 +118,7 @@ public class PS { private LinkedHashMap> plots; private Database database; private Connection connection; + private Thread thread; /** * Initialize PlotSquared with the desired Implementation class @@ -125,14 +127,22 @@ public class PS { public PS(final IPlotMain imp_class) { try { instance = this; + this.thread = Thread.currentThread(); SetupUtils.generators = new HashMap<>(); IMP = imp_class; + URL url; try { - FILE = new File(PS.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()); + url = PS.class.getProtectionDomain().getCodeSource().getLocation(); + FILE = new File(new URL(url.toURI().toString().split("\\!")[0].replaceAll("jar:file", "file")).toURI().getPath()); } catch (Exception e) { - FILE = new File(IMP.getDirectory().getParentFile(), "PlotSquared.jar"); e.printStackTrace(); - log("Could not determine file path"); + FILE = new File(IMP.getDirectory().getParentFile(), "PlotSquared.jar"); + if (!FILE.exists()) { + FILE = new File(IMP.getDirectory().getParentFile(), "PlotSquared-Bukkit.jar"); + if (!FILE.exists()) { + FILE = new File(IMP.getDirectory().getParentFile(), "PlotSquared-Sponge.jar"); + } + } } VERSION = IMP.getPluginVersion(); EconHandler.manager = IMP.getEconomyHandler(); @@ -268,6 +278,10 @@ public class PS { } } + public boolean isMainThread(Thread thread) { + return this.thread == thread; + } + public boolean checkVersion(int[] version, int major, int minor, int minor2) { return (version[0] > major) || ((version[0] == major) && (version[1] > minor)) || ((version[0] == major) && (version[1] == minor) && (version[2] >= minor2)); } diff --git a/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java b/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java index 445c15ee7..ad4610b99 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java @@ -22,31 +22,53 @@ package com.intellectualcrafters.plot.commands; import java.io.File; import java.io.FileNotFoundException; +import java.io.IOException; import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.UUID; -import org.bukkit.Bukkit; +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; import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.generator.HybridUtils; import com.intellectualcrafters.plot.object.ChunkLoc; +import com.intellectualcrafters.plot.object.ConsolePlayer; +import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.OfflinePlotPlayer; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotAnalysis; +import com.intellectualcrafters.plot.object.PlotBlock; +import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.RunnableVal; +import com.intellectualcrafters.plot.util.AbstractTitle; import com.intellectualcrafters.plot.util.BlockManager; import com.intellectualcrafters.plot.util.ChunkManager; +import com.intellectualcrafters.plot.util.EventUtil; import com.intellectualcrafters.plot.util.ExpireManager; import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.MathMan; +import com.intellectualcrafters.plot.util.Permissions; +import com.intellectualcrafters.plot.util.SchematicHandler; +import com.intellectualcrafters.plot.util.SetupUtils; import com.intellectualcrafters.plot.util.StringMan; +import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.UUIDHandler; import com.plotsquared.bukkit.util.BukkitHybridUtils; import com.plotsquared.general.commands.CommandDeclaration; @@ -60,11 +82,71 @@ import com.plotsquared.general.commands.CommandDeclaration; ) public class DebugExec extends SubCommand { + private ScriptEngine engine; + private Bindings scope; + + public void init() { + if (engine != null) { + return; + } + engine = (new ScriptEngineManager()).getEngineByName("JavaScript"); + ScriptContext context = new SimpleScriptContext(); + scope = context.getBindings(ScriptContext.ENGINE_SCOPE); + + // stuff + scope.put("MainUtil", new MainUtil()); + scope.put("Settings", new Settings()); + scope.put("StringMan", new StringMan()); + scope.put("MathMan", new MathMan()); + + // 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); + + // 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); + scope.put("BlockManager", BlockManager.manager); + scope.put("SetupUtils", SetupUtils.manager); + scope.put("EventUtil", EventUtil.manager); + scope.put("UUIDHandler", UUIDHandler.implementation); + scope.put("DBFunc", DBFunc.dbManager); + scope.put("HybridUtils", HybridUtils.manager); + scope.put("HybridUtils", HybridUtils.manager); + scope.put("IMP", PS.get().IMP); + scope.put("MainCommand", MainCommand.getInstance()); + + // enums + for (Enum value : C.values()) { + scope.put("C_" + value.name(), value); + } + for (Enum value : Permissions.values()) { + scope.put("Permissions_" + value.name(), value); + } + addEnums(scope, C.values()); + } + + + private void addEnums(Bindings scope2, C[] values) { + // TODO Auto-generated method stub + } + + @Override public boolean onCommand(final PlotPlayer player, String[] args) { final List allowed_params = Arrays.asList("calibrate-analysis", "remove-flag", "stop-expire", "start-expire", "show-expired", "update-expired", "seen", "trim-check"); if (args.length > 0) { final String arg = args[0].toLowerCase(); + String script; + boolean async = false; switch (arg) { case "analyze": { Plot plot = MainUtil.getPlot(player.getLocation()); @@ -113,7 +195,7 @@ public class DebugExec extends SubCommand { } case "stop-expire": { if (ExpireManager.task != -1) { - Bukkit.getScheduler().cancelTask(ExpireManager.task); + PS.get().TASK.cancelTask(ExpireManager.task); } else { return MainUtil.sendMessage(player, "Task already halted"); } @@ -161,7 +243,7 @@ public class DebugExec extends SubCommand { return false; } ((BukkitHybridUtils)(HybridUtils.manager)).task = 0; - Bukkit.getScheduler().cancelTask(((BukkitHybridUtils)(HybridUtils.manager)).task); + PS.get().TASK.cancelTask(((BukkitHybridUtils)(HybridUtils.manager)).task); MainUtil.sendMessage(player, "&cCancelling task..."); while (BukkitHybridUtils.chunks.size() > 0) { ChunkLoc chunk = BukkitHybridUtils.chunks.get(0); @@ -272,9 +354,66 @@ public class DebugExec extends SubCommand { } return result; } + case "h": + case "he": + case "?": + case "help": { + MainUtil.sendMessage(player, "Possible sub commands: /plot debugexec <" + StringMan.join(allowed_params, "|") + ">"); + return false; + } + case "runasync": { + async = true; + } + case "run": { + try { + script = StringMan.join(Files.readLines(new File(PS.get().IMP.getDirectory(), args[1]), StandardCharsets.UTF_8), ""); + if (args.length > 2) { + HashMap replacements = new HashMap<>(); + for (int i = 2; i < args.length; i++) { + replacements.put("%s" + (i-2), args[i]); + } + script = StringMan.replaceFromMap(script, replacements); + } + } catch (IOException e) { + e.printStackTrace(); + return false; + } + break; + } + default: { + script = StringMan.join(args, " "); + } + } + init(); + scope.put("PlotPlayer", player); + PS.log("> " + script); + try { + if (async) { + final String toExec = script; + TaskManager.runTaskAsync(new Runnable() { + @Override + public void run() { + long start = System.currentTimeMillis(); + try { + engine.eval(toExec, scope); + } catch (ScriptException e) { + e.printStackTrace(); + } + PS.log("> " + (System.currentTimeMillis() - start) + "ms"); + } + }); + } + else { + long start = System.currentTimeMillis(); + engine.eval(script, scope); + PS.log("> " + (System.currentTimeMillis() - start) + "ms"); + } + return true; + } catch (ScriptException e) { + e.printStackTrace(); + return false; } } - MainUtil.sendMessage(player, "Possible sub commands: /plot debugexec <" + StringMan.join(allowed_params, "|") + ">"); - return true; + return false; } } diff --git a/src/main/java/com/intellectualcrafters/plot/commands/Limit.java b/src/main/java/com/intellectualcrafters/plot/commands/Limit.java new file mode 100644 index 000000000..fd8f129a6 --- /dev/null +++ b/src/main/java/com/intellectualcrafters/plot/commands/Limit.java @@ -0,0 +1,77 @@ +//////////////////////////////////////////////////////////////////////////////////////////////////// +// 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.BufferedReader; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.UUID; + +import com.intellectualcrafters.plot.PS; +import com.intellectualcrafters.plot.config.C; +import com.intellectualcrafters.plot.object.OfflinePlotPlayer; +import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.util.EconHandler; +import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.StringMan; +import com.intellectualcrafters.plot.util.TaskManager; +import com.intellectualcrafters.plot.util.UUIDHandler; +import com.plotsquared.general.commands.Argument; +import com.plotsquared.general.commands.CommandDeclaration; + +@CommandDeclaration( + command = "limit", + permission = "plots.limit", + description = "Set or increment player plot claim limits", + aliases = {"setlimit"}, + usage = "/plot limit ", + category = CommandCategory.DEBUG +) +public class Limit extends SubCommand { + + public Limit() { + requiredArguments = new Argument[] { + Argument.String, + Argument.String + }; + } + + @Override + public boolean onCommand(final PlotPlayer plr, final String[] args) { + UUID uuid = UUIDHandler.getUUID(args[0], null); + if (uuid == null) { + MainUtil.sendMessage(plr, C.INVALID_PLAYER, args[0]); + return false; + } + OfflinePlotPlayer op = UUIDHandler.getUUIDWrapper().getOfflinePlayer(uuid); + + // get current plot limit + // increase + +// EconHandler.manager.setPermission(op, perm, value); + plr.sendMessage("TODO"); + + + return true; + } +} diff --git a/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java b/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java index 2ca5340a5..e2a855dd2 100644 --- a/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java +++ b/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java @@ -60,6 +60,7 @@ public class MainCommand extends CommandManager { private MainCommand() { super(null, new ArrayList>()); + instance = this; createCommand(new Buy()); createCommand(new Save()); createCommand(new Load()); diff --git a/src/main/java/com/intellectualcrafters/plot/object/Plot.java b/src/main/java/com/intellectualcrafters/plot/object/Plot.java index 121ac1fc2..58c42bd53 100644 --- a/src/main/java/com/intellectualcrafters/plot/object/Plot.java +++ b/src/main/java/com/intellectualcrafters/plot/object/Plot.java @@ -27,12 +27,16 @@ import java.util.List; import java.util.Map.Entry; import java.util.UUID; +import javax.annotation.concurrent.ThreadSafe; + import com.intellectualcrafters.plot.PS; +import com.intellectualcrafters.plot.config.Configuration; import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.flag.Flag; import com.intellectualcrafters.plot.util.BlockManager; import com.intellectualcrafters.plot.util.ChunkManager; import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.TaskManager; /** * The plot class @@ -310,7 +314,7 @@ public class Plot { * Clear a plot * @see MainUtil#clear(Plot, boolean, Runnable) * @see MainUtil#clearAsPlayer(Plot, boolean, Runnable) - * @see #delete() to clear and delete a plot + * @see #deletePlot() to clear and delete a plot * @param whenDone A runnable to execute when clearing finishes, or null */ public void clear(Runnable whenDone) { @@ -334,13 +338,14 @@ public class Plot { * @see PS#removePlot(String, PlotId, boolean) * @see #clear(Runnable) to simply clear a plot */ - public void delete() { + public void deletePlot(final Runnable whenDone) { MainUtil.removeSign(this); MainUtil.clear(this, true, new Runnable() { @Override public void run() { if (PS.get().removePlot(world, id, true)) { DBFunc.delete(Plot.this); + TaskManager.runTask(whenDone); } } }); @@ -483,6 +488,22 @@ public class Plot { MainUtil.setBiome(this, biome, whenDone); } + /** + * Set components such as border, wall, floor + * (components are generator specific) + */ + public void setComponent(String component, PlotBlock... blocks) { + MainUtil.setComponent(this, component, blocks); + } + + /** + * Set components such as border, wall, floor + * (components are generator specific) + */ + public void setComponent(String component, String blocks) { + MainUtil.setComponent(this, component, Configuration.BLOCKLIST.parseString(blocks)); + } + /** * Get the biome (String) */ diff --git a/src/main/java/com/intellectualcrafters/plot/util/EconHandler.java b/src/main/java/com/intellectualcrafters/plot/util/EconHandler.java index c66a162b4..a340508c4 100644 --- a/src/main/java/com/intellectualcrafters/plot/util/EconHandler.java +++ b/src/main/java/com/intellectualcrafters/plot/util/EconHandler.java @@ -17,4 +17,6 @@ public abstract class EconHandler { public abstract void depositMoney(PlotPlayer player, double amount); public abstract void depositMoney(OfflinePlotPlayer player, double amount); public abstract void setPermission(PlotPlayer player, String perm, boolean value); +// public abstract void setPermission(OfflinePlotPlayer player, String perm, boolean value); +// public abstract void getPermission(OfflinePlotPlayer player, String perm); } diff --git a/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java b/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java index 53f2d1220..054bd1067 100644 --- a/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java +++ b/src/main/java/com/intellectualcrafters/plot/util/ExpireManager.java @@ -132,7 +132,7 @@ public class ExpireManager { if (plot.isMerged()) { MainUtil.unlinkPlot(plot); } - plot.delete(); + plot.deletePlot(null); expiredPlots.get(world).remove(plot); int complexity = changed == null ? 0 : changed.getComplexity(); int modified = changed == null ? 0 : changed.changes; diff --git a/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java b/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java index d09707c57..694717e77 100644 --- a/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java +++ b/src/main/java/com/intellectualcrafters/plot/util/MainUtil.java @@ -29,6 +29,7 @@ import java.util.UUID; import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.config.C; +import com.intellectualcrafters.plot.config.Configuration; import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.object.BlockLoc; @@ -765,6 +766,15 @@ public class MainUtil { } public static void removeSign(final Plot p) { + if (!PS.get().isMainThread(Thread.currentThread())) { + TaskManager.runTask(new Runnable() { + @Override + public void run() { + removeSign(p); + } + }); + return; + } final String world = p.world; final PlotManager manager = PS.get().getPlotManager(world); final PlotWorld plotworld = PS.get().getPlotWorld(world); @@ -783,16 +793,23 @@ public class MainUtil { setSign(UUIDHandler.getName(p.owner), p); } - public static void setSign(String name, final Plot p) { - if (name == null) { - name = "unknown"; + public static void setSign(final String name, final Plot p) { + if (!PS.get().isMainThread(Thread.currentThread())) { + TaskManager.runTask(new Runnable() { + @Override + public void run() { + setSign(name, p); + } + }); + return; } + String rename = name == null ? "unknown" : name; final PlotManager manager = PS.get().getPlotManager(p.world); final PlotWorld plotworld = PS.get().getPlotWorld(p.world); if (plotworld.ALLOW_SIGNS) { final Location loc = manager.getSignLoc(plotworld, p); final String id = p.id.x + ";" + p.id.y; - final String[] lines = new String[] { C.OWNER_SIGN_LINE_1.formatted().replaceAll("%id%", id), C.OWNER_SIGN_LINE_2.formatted().replaceAll("%id%", id).replaceAll("%plr%", name), C.OWNER_SIGN_LINE_3.formatted().replaceAll("%id%", id).replaceAll("%plr%", name), C.OWNER_SIGN_LINE_4.formatted().replaceAll("%id%", id).replaceAll("%plr%", name) }; + final String[] lines = new String[] { C.OWNER_SIGN_LINE_1.formatted().replaceAll("%id%", id), C.OWNER_SIGN_LINE_2.formatted().replaceAll("%id%", id).replaceAll("%plr%", rename), C.OWNER_SIGN_LINE_3.formatted().replaceAll("%id%", id).replaceAll("%plr%", rename), C.OWNER_SIGN_LINE_4.formatted().replaceAll("%id%", id).replaceAll("%plr%", rename) }; BlockManager.setSign(p.world, loc.getX(), loc.getY(), loc.getZ(), lines); } } @@ -1762,4 +1779,8 @@ public class MainUtil { } return ratings; } + + public static void setComponent(Plot plot, String component, PlotBlock[] blocks) { + PS.get().getPlotManager(plot.world).setComponent(PS.get().getPlotWorld(plot.world), plot.id, component, blocks); + } } diff --git a/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java b/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java index 3cec9dde9..87ec06512 100644 --- a/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java +++ b/src/main/java/com/plotsquared/bukkit/listeners/PlayerEvents.java @@ -1619,7 +1619,7 @@ public class PlayerEvents extends com.plotsquared.listener.PlotListener implemen if (Settings.DELETE_PLOTS_ON_BAN && event.getPlayer().isBanned()) { final Collection plots = PS.get().getPlots(pp.getName()).values(); for (final Plot plot : plots) { - plot.delete(); + plot.deletePlot(null); PS.debug(String.format("&cPlot &6%s &cwas deleted + cleared due to &6%s&c getting banned", plot.getId(), event.getPlayer().getName())); } } diff --git a/src/main/java/com/plotsquared/sponge/util/SpongeBlockManager.java b/src/main/java/com/plotsquared/sponge/util/SpongeBlockManager.java index 04e6a6266..31489f462 100644 --- a/src/main/java/com/plotsquared/sponge/util/SpongeBlockManager.java +++ b/src/main/java/com/plotsquared/sponge/util/SpongeBlockManager.java @@ -98,6 +98,9 @@ public class SpongeBlockManager extends BlockManager { @Override public int getHeighestBlock(String worldname, int x, int z) { World world = SpongeUtil.getWorld(worldname); + if (world == null) { + return 64; + } for (int y = 255; y > 0; y--) { BlockState block = world.getBlock(x, y, z); if (block != null && block.getType() != BlockTypes.AIR) {