Added teleport functions to the player interface for scripts; renamed some script-related classes.

This commit is contained in:
sk89q 2010-10-03 13:09:59 -07:00
parent c55799567a
commit 33fb2abb54
4 changed files with 136 additions and 84 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

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

View File

@ -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", "<ID> <Radius> <Depth> - Fill a hole");
commands.put("/editscript", "[Filename] <Args...> - Run an editscript");
commands.put("/script", "[Filename] <Args...> - 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;
}
}