Skip to content

Commit

Permalink
Removed scripting support.
Browse files Browse the repository at this point in the history
  • Loading branch information
sk89q committed Oct 11, 2010
1 parent a4eb772 commit aea6491
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 382 deletions.
2 changes: 1 addition & 1 deletion manifest.mf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Manifest-Version: 1.0
Class-Path: smalljs.jar jnbt.jar
Class-Path: jnbt.jar

53 changes: 0 additions & 53 deletions src/ScriptContext.java

This file was deleted.

65 changes: 0 additions & 65 deletions src/ScriptMinecraftContext.java

This file was deleted.

75 changes: 0 additions & 75 deletions src/ScriptPlayer.java

This file was deleted.

120 changes: 1 addition & 119 deletions src/WorldEdit.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.logging.Logger;
import java.util.Map;
import java.io.*;
import org.mozilla.javascript.*;
import com.sk89q.worldedit.*;

/**
Expand All @@ -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.
Expand Down Expand Up @@ -74,7 +72,6 @@ public WorldEdit() {
commands.put("/editsave", "[Filename] - Save clipboard to .schematic");
commands.put("/editfill", "[ID] [Radius] <Depth> - Fill a hole");
commands.put("/editdrain", "[Radius] - Drain nearby water/lava pools");
commands.put("/editscript", "[Filename] <Args...> - Run a WorldEdit script");
commands.put("/editlimit", "[Num] - See documentation");
commands.put("/unstuck", "Go up to the first free spot");
}
Expand Down Expand Up @@ -186,7 +183,6 @@ public void enable() {
}

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));

Expand Down Expand Up @@ -287,28 +283,6 @@ public boolean onCommand(Player modPlayer, String[] split) {
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;
Expand Down Expand Up @@ -540,18 +514,7 @@ private boolean performCommand(WorldEditPlayer player,

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;
Expand Down Expand Up @@ -675,87 +638,6 @@ private boolean performCommand(WorldEditPlayer player,
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.
Expand Down
Loading

0 comments on commit aea6491

Please sign in to comment.