From aea64916e3eec5db913afea5ce8f713e4224f44b Mon Sep 17 00:00:00 2001 From: sk89q Date: Mon, 11 Oct 2010 13:27:37 -0700 Subject: [PATCH] Removed scripting support. --- manifest.mf | 2 +- src/ScriptContext.java | 53 -------- src/ScriptMinecraftContext.java | 65 ---------- src/ScriptPlayer.java | 75 ----------- src/WorldEdit.java | 120 +----------------- .../sk89q/worldedit/ScriptContextFactory.java | 69 ---------- 6 files changed, 2 insertions(+), 382 deletions(-) delete mode 100644 src/ScriptContext.java delete mode 100644 src/ScriptMinecraftContext.java delete mode 100644 src/ScriptPlayer.java delete mode 100644 src/com/sk89q/worldedit/ScriptContextFactory.java diff --git a/manifest.mf b/manifest.mf index d5eced189..c6ce47339 100644 --- a/manifest.mf +++ b/manifest.mf @@ -1,3 +1,3 @@ Manifest-Version: 1.0 -Class-Path: smalljs.jar jnbt.jar +Class-Path: jnbt.jar diff --git a/src/ScriptContext.java b/src/ScriptContext.java deleted file mode 100644 index c8cc52ed1..000000000 --- a/src/ScriptContext.java +++ /dev/null @@ -1,53 +0,0 @@ -// $Id$ -/* - * WorldEdit - * Copyright (C) 2010 sk89q - * - * 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 . -*/ - -/** - * - * @author sk89q - */ -public class ScriptContext { - public ScriptPlayer player; - - /** - * Constructs the context. A player object has to be passed. - * - * @param player - */ - public ScriptContext(ScriptPlayer player) { - this.player = player; - } - - /** - * Prints a message to the player. - * - * @param msg - */ - public void print(String msg) { - player.print(msg); - } - - /** - * Prints an error message to the player. - * - * @param msg - */ - public void error(String msg) { - player.error(msg); - } -} diff --git a/src/ScriptMinecraftContext.java b/src/ScriptMinecraftContext.java deleted file mode 100644 index a57d7eede..000000000 --- a/src/ScriptMinecraftContext.java +++ /dev/null @@ -1,65 +0,0 @@ - -import com.sk89q.worldedit.MaxChangedBlocksException; - -// $Id$ -/* - * WorldEdit - * Copyright (C) 2010 sk89q - * - * 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 . -*/ - -/** - * - * @author Albert - */ -public class ScriptMinecraftContext { - private EditSession editSession; - - /** - * Construct this Minecraft object with an EditSession. - * - * @param editSession - */ - public ScriptMinecraftContext(EditSession editSession) { - this.editSession = editSession; - } - - /** - * Sets the block at position x, y, z with a block type. - * - * @param x - * @param y - * @param z - * @param blockType - * @throws MaxChangedBlocksException - * @return whether the block was changed - */ - public boolean setBlock(int x, int y, int z, int blockType) - throws MaxChangedBlocksException { - return editSession.setBlock(x, y, z, blockType); - } - - /** - * Gets the block type at a position x, y, z. - * - * @param x - * @param y - * @param z - * @return block type ID - */ - public int getBlock(int x, int y, int z) { - return editSession.getBlock(x, y, z); - } -} diff --git a/src/ScriptPlayer.java b/src/ScriptPlayer.java deleted file mode 100644 index 2937a5142..000000000 --- a/src/ScriptPlayer.java +++ /dev/null @@ -1,75 +0,0 @@ -// $Id$ -/* - * WorldEdit - * Copyright (C) 2010 sk89q - * - * 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 . -*/ - -import com.sk89q.worldedit.Point; - -/** - * - * @author sk89q - */ -public class ScriptPlayer { - private WorldEditPlayer player; - - public String name; - public double pitch; - public double yaw; - public double x; - public double y; - public double z; - public int blockX; - public int blockY; - public int blockZ; - - /** - * Constructs the player instance. - * - * @param player - */ - public ScriptPlayer(WorldEditPlayer player) { - this.player = player; - name = player.getName(); - pitch = player.getPitch(); - yaw = player.getYaw(); - Point pos = player.getPosition(); - x = pos.getX(); - y = pos.getY(); - z = pos.getZ(); - blockX = pos.getBlockX(); - blockY = pos.getBlockY(); - blockZ = pos.getBlockZ(); - } - - /** - * Prints a message to the player. - * - * @param msg - */ - public void print(String msg) { - player.print(msg); - } - - /** - * Prints an error message to the player. - * - * @param msg - */ - public void error(String msg) { - player.printError(msg); - } -} diff --git a/src/WorldEdit.java b/src/WorldEdit.java index 88be0dc45..c21d533bc 100644 --- a/src/WorldEdit.java +++ b/src/WorldEdit.java @@ -22,7 +22,6 @@ import java.util.logging.Level; import java.util.logging.Logger; import java.util.Map; import java.io.*; -import org.mozilla.javascript.*; import com.sk89q.worldedit.*; /** @@ -43,7 +42,6 @@ public class WorldEdit extends Plugin { private PropertiesFile properties; private String[] allowedBlocks; private int defaultMaxBlocksChanged; - private boolean mapScriptCommands = false; /** * Construct an instance of the plugin. @@ -74,7 +72,6 @@ public class WorldEdit extends Plugin { commands.put("/editsave", "[Filename] - Save clipboard to .schematic"); commands.put("/editfill", "[ID] [Radius] - Fill a hole"); commands.put("/editdrain", "[Radius] - Drain nearby water/lava pools"); - commands.put("/editscript", "[Filename] - Run a WorldEdit script"); commands.put("/editlimit", "[Num] - See documentation"); commands.put("/unstuck", "Go up to the first free spot"); } @@ -186,7 +183,6 @@ public class WorldEdit extends Plugin { } allowedBlocks = properties.getString("allowed-blocks", DEFAULT_ALLOWED_BLOCKS).split(","); - mapScriptCommands = properties.getBoolean("map-script-commands", true); defaultMaxBlocksChanged = Math.max(-1, properties.getInt("max-blocks-changed", -1)); @@ -287,28 +283,6 @@ public class WorldEdit extends Plugin { editSession.flushQueue(); } } - } else { - // See if there is a script by the same name - if (mapScriptCommands && modPlayer.canUseCommand("/editscript")) { - WorldEditPlayer player = new WorldEditPlayer(modPlayer); - WorldEditSession session = getSession(player); - EditSession editSession = - new EditSession(session.getBlockChangeLimit()); - editSession.enableQueue(); - - String filename = split[0].substring(1) + ".js"; - String[] args = new String[split.length - 1]; - System.arraycopy(split, 1, args, 0, split.length - 1); - - try { - return runScript(player, session, editSession, filename, args); - } catch (NoSuchScriptException nse) { - return false; - } finally { - session.remember(editSession); - editSession.flushQueue(); - } - } } return false; @@ -540,18 +514,7 @@ public class WorldEdit extends Plugin { return true; - // Run a script - } else if (split[0].equalsIgnoreCase("/editscript")) { - checkArgs(split, 1, -1, split[0]); - String filename = split[1].replace("\0", "") + ".js"; - String[] args = new String[split.length - 2]; - System.arraycopy(split, 2, args, 0, split.length - 2); - try { - runScript(player, session, editSession, filename, args); - } catch (NoSuchScriptException e) { - player.printError("Script file does not exist."); - } - return true; + // Get size } else if (split[0].equalsIgnoreCase("/editsize")) { player.print("# of blocks: " + session.getRegion().getSize()); return true; @@ -675,87 +638,6 @@ public class WorldEdit extends Plugin { return false; } - /** - * Execute a script. - * - * @param player - * @param filename - * @param args - * @return Whether the file was attempted execution - */ - private boolean runScript(WorldEditPlayer player, WorldEditSession session, - EditSession editSession, String filename, String[] args) throws - NoSuchScriptException { - File dir = new File("editscripts"); - File f = new File("editscripts", filename); - - try { - String filePath = f.getCanonicalPath(); - String dirPath = dir.getCanonicalPath(); - - if (!filePath.substring(0, dirPath.length()).equals(dirPath)) { - throw new NoSuchScriptException(); - } else if (!f.exists()) { - throw new NoSuchScriptException(); - } else { - // Read file - StringBuffer buffer = new StringBuffer(); - FileInputStream stream = new FileInputStream(f); - BufferedReader in = new BufferedReader(new InputStreamReader(stream, "UTF-8")); - int c; - while ((c = in.read()) > -1) { - buffer.append((char)c); - } - in.close(); - String code = buffer.toString(); - - // Evaluate - ScriptContextFactory factory = new ScriptContextFactory(); - Context cx = factory.enterContext(); - try { - ScriptableObject scope = cx.initStandardObjects(); - - // Add args - ScriptableObject.putProperty(scope, "args", - Context.javaToJS(args, scope)); - - // Add context - ScriptPlayer scriptPlayer = new ScriptPlayer(player); - ScriptContext context = new ScriptContext( - scriptPlayer); - ScriptableObject.putProperty(scope, "context", - Context.javaToJS(context, scope)); - ScriptableObject.putProperty(scope, "player", - Context.javaToJS(scriptPlayer, scope)); - - // Add Minecraft context - ScriptMinecraftContext minecraft = - new ScriptMinecraftContext(editSession); - ScriptableObject.putProperty(scope, "minecraft", - Context.javaToJS(minecraft, scope)); - - logger.log(Level.INFO, player.getName() + ": executing " + filename + "..."); - cx.evaluateString(scope, code, filename, 1, null); - logger.log(Level.INFO, player.getName() + ": script " + filename + " executed successfully."); - player.print(filename + " executed successfully."); - } catch (RhinoException re) { - player.printError(filename + ": JS error: " + re.getMessage()); - re.printStackTrace(); - } catch (Error err) { - player.printError(filename + ": execution error: " + err.getMessage()); - } finally { - Context.exit(); - } - } - - return true; - } catch (IOException e) { - player.printError("Script could not read or it does not exist."); - } - - return false; - } - /** * Gets the block type at a position x, y, z. Use an instance of * EditSession if possible. diff --git a/src/com/sk89q/worldedit/ScriptContextFactory.java b/src/com/sk89q/worldedit/ScriptContextFactory.java deleted file mode 100644 index 8e9a83ef0..000000000 --- a/src/com/sk89q/worldedit/ScriptContextFactory.java +++ /dev/null @@ -1,69 +0,0 @@ -// $Id$ -/* - * WorldEdit - * Copyright (C) 2010 sk89q - * - * 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 . -*/ - -package com.sk89q.worldedit; - -import org.mozilla.javascript.*; - -/** - * Context factory for the JavaScript engine. - * - * @author Albert - */ -public class ScriptContextFactory extends ContextFactory { - /** - * Context that will be used to store start time. - */ - private static class ScriptContext extends Context { - long startTime; - } - - static { - ContextFactory.initGlobal(new ScriptContextFactory()); - } - - @Override - protected Context makeContext() - { - ScriptContext ctx = new ScriptContext(); - ctx.setInstructionObserverThreshold(10000); - return ctx; - } - - @Override - protected void observeInstructionCount(Context ctx, int instructionCount) - { - ScriptContext sctx = (ScriptContext)ctx; - long currentTime = System.currentTimeMillis(); - if (currentTime - sctx.startTime > 3 * 1000) { - throw new Error("Exceeded 3 seconds"); - } - } - - @Override - protected Object doTopCall(Callable callable, - Context ctx, Scriptable scope, - Scriptable thisObj, Object[] args) - { - ScriptContext sctx = (ScriptContext)ctx; - sctx.startTime = System.currentTimeMillis(); - return super.doTopCall(callable, ctx, scope, thisObj, args); - } - -}