From 33fb2abb5443f5fc6069e7efc3af4b9a8636a304 Mon Sep 17 00:00:00 2001 From: sk89q Date: Sun, 3 Oct 2010 13:09:59 -0700 Subject: [PATCH] Added teleport functions to the player interface for scripts; renamed some script-related classes. --- ...tScriptContext.java => ScriptContext.java} | 6 +- ...ntext.java => ScriptMinecraftContext.java} | 4 +- ...ditScriptPlayer.java => ScriptPlayer.java} | 66 ++++++-- src/WorldEdit.java | 144 ++++++++++-------- 4 files changed, 136 insertions(+), 84 deletions(-) rename src/{EditScriptContext.java => ScriptContext.java} (90%) rename src/{EditScriptMinecraftContext.java => ScriptMinecraftContext.java} (93%) rename src/{EditScriptPlayer.java => ScriptPlayer.java} (66%) diff --git a/src/EditScriptContext.java b/src/ScriptContext.java similarity index 90% rename from src/EditScriptContext.java rename to src/ScriptContext.java index b0df3ffd1..c8cc52ed1 100644 --- a/src/EditScriptContext.java +++ b/src/ScriptContext.java @@ -21,15 +21,15 @@ * * @author sk89q */ -public class EditScriptContext { - public EditScriptPlayer player; +public class ScriptContext { + public ScriptPlayer player; /** * Constructs the context. A player object has to be passed. * * @param player */ - public EditScriptContext(EditScriptPlayer player) { + public ScriptContext(ScriptPlayer player) { this.player = player; } diff --git a/src/EditScriptMinecraftContext.java b/src/ScriptMinecraftContext.java similarity index 93% rename from src/EditScriptMinecraftContext.java rename to src/ScriptMinecraftContext.java index e743427ff..778b96358 100644 --- a/src/EditScriptMinecraftContext.java +++ b/src/ScriptMinecraftContext.java @@ -21,7 +21,7 @@ * * @author Albert */ -public class EditScriptMinecraftContext { +public class ScriptMinecraftContext { private EditSession editSession; /** @@ -29,7 +29,7 @@ public class EditScriptMinecraftContext { * * @param editSession */ - public EditScriptMinecraftContext(EditSession editSession) { + public ScriptMinecraftContext(EditSession editSession) { this.editSession = editSession; } diff --git a/src/EditScriptPlayer.java b/src/ScriptPlayer.java similarity index 66% rename from src/EditScriptPlayer.java rename to src/ScriptPlayer.java index 52f4b2bda..67c67b600 100644 --- a/src/EditScriptPlayer.java +++ b/src/ScriptPlayer.java @@ -21,7 +21,7 @@ * * @author Albert */ -public class EditScriptPlayer { +public class ScriptPlayer { private Player player; public String name; @@ -34,6 +34,24 @@ public class EditScriptPlayer { public int blockY; public int blockZ; + /** + * Constructs the player instance. + * + * @param player + */ + public ScriptPlayer(Player player) { + this.player = player; + name = player.getName(); + pitch = player.getPitch(); + yaw = player.getRotation(); + x = player.getX(); + y = player.getY(); + z = player.getZ(); + blockX = (int)Math.floor(player.getX()); + blockY = (int)Math.floor(player.getY()); + blockZ = (int)Math.floor(player.getZ()); + } + /** * Prints a message to the player. * @@ -53,20 +71,38 @@ public class EditScriptPlayer { } /** - * Constructs the player instance. - * - * @param player + * Teleport the player to a position. + * + * @param x + * @param y + * @param z */ - public EditScriptPlayer(Player player) { - this.player = player; - name = player.getName(); - pitch = player.getPitch(); - yaw = player.getRotation(); - x = player.getX(); - y = player.getY(); - z = player.getZ(); - blockX = (int)Math.floor(player.getX()); - blockY = (int)Math.floor(player.getY()); - blockZ = (int)Math.floor(player.getZ()); + public void teleporTo(double x, double y, double z) { + Location loc = new Location(); + loc.x = x; + loc.y = y; + loc.z = z; + loc.rotX = player.getRotation(); + loc.rotY = player.getPitch(); + player.teleportTo(loc); + } + + /** + * Teleport the player to a position with a certain rotation. + * + * @param x + * @param y + * @param z + * @param pitch + * @param yaw + */ + public void teleporTo(double x, double y, double z, float pitch, float yaw) { + Location loc = new Location(); + loc.x = x; + loc.y = y; + loc.z = z; + loc.rotX = yaw; + loc.rotY = pitch; + player.teleportTo(loc); } } diff --git a/src/WorldEdit.java b/src/WorldEdit.java index daf071c0a..388921536 100644 --- a/src/WorldEdit.java +++ b/src/WorldEdit.java @@ -65,7 +65,7 @@ public class WorldEdit extends Plugin { commands.put("/editload", "[Filename] - Load .schematic into clipboard"); commands.put("/editsave", "[Filename] - Save clipboard to .schematic"); commands.put("/editfill", " - Fill a hole"); - commands.put("/editscript", "[Filename] - Run an editscript"); + commands.put("/script", "[Filename] - Run a WorldEdit script"); } /** @@ -419,71 +419,12 @@ public class WorldEdit extends Plugin { return true; // Run an editscript - } else if (split[0].equalsIgnoreCase("/editscript")) { + } else if (split[0].equalsIgnoreCase("/script")) { checkArgs(split, 1); String filename = split[1].replace("\0", "") + ".js"; - 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)) { - player.sendMessage(Colors.Rose + "Editscript file does not exist."); - } 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 - Context cx = Context.enter(); - try { - ScriptableObject scope = cx.initStandardObjects(); - - // Add args - String[] args = new String[split.length - 2]; - System.arraycopy(split, 2, args, 0, split.length - 2); - ScriptableObject.putProperty(scope, "args", - Context.javaToJS(args, scope)); - - // Add context - EditScriptPlayer scriptPlayer = new EditScriptPlayer(player); - EditScriptContext context = new EditScriptContext( - scriptPlayer); - ScriptableObject.putProperty(scope, "context", - Context.javaToJS(context, scope)); - ScriptableObject.putProperty(scope, "player", - Context.javaToJS(scriptPlayer, scope)); - - // Add Minecraft context - EditScriptMinecraftContext minecraft = - new EditScriptMinecraftContext(editSession); - ScriptableObject.putProperty(scope, "minecraft", - Context.javaToJS(minecraft, scope)); - - cx.evaluateString(scope, code, filename, 1, null); - player.sendMessage(Colors.LightPurple + filename + " executed successfully."); - } catch (RhinoException re) { - player.sendMessage(Colors.Rose + "JS error: " + re.getMessage()); - re.printStackTrace(); - } finally { - Context.exit(); - session.remember(editSession); - } - - return true; - } - } catch (IOException e) { - player.sendMessage(Colors.Rose + "Editscript could not read or it does not exist."); - } + String[] args = new String[split.length - 2]; + System.arraycopy(split, 2, args, 0, split.length - 2); + runScript(player, session, editSession, filename, args); return true; } @@ -662,4 +603,79 @@ public class WorldEdit extends Plugin { return affected; } + + /** + * Execute a script. + * + * @param player + * @param filename + * @param args + */ + private boolean runScript(Player player, WorldEditSession session, + EditSession editSession, String filename, String[] args) { + 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)) { + player.sendMessage(Colors.Rose + "Script file does not exist."); + } else if (!f.exists()) { + player.sendMessage(Colors.Rose + "Script file does not exist."); + } 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 + Context cx = Context.enter(); + 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)); + + cx.evaluateString(scope, code, filename, 1, null); + player.sendMessage(Colors.LightPurple + filename + " executed successfully."); + + return true; + } catch (RhinoException re) { + player.sendMessage(Colors.Rose + "JS error: " + re.getMessage()); + re.printStackTrace(); + } finally { + Context.exit(); + session.remember(editSession); + } + } + } catch (IOException e) { + player.sendMessage(Colors.Rose + "Script could not read or it does not exist."); + } + + return false; + } }