Major reworking of MCP version of Mod and have added a new Bukkit Plugin version.

This commit is contained in:
walterhiggins 2013-01-04 23:38:47 +00:00
parent 165a2873b3
commit c9f30382d6
10 changed files with 571 additions and 0 deletions

View file

@ -0,0 +1,9 @@
name: ScriptCraftPlugin
main: net.walterhiggins.scriptcraft.ScriptCraftPlugin
version: 0.01
commands:
js:
description: Evaluate javascript.
usage: /<command> Javascript code
permission: ScriptCraftPlugin.js
permission-message: You don't have <permission>

View file

@ -0,0 +1,9 @@
name: ScriptCraft
main: net.walterhiggins.scriptcraft.ScriptCraft
version: 0.01
commands:
js:
description: Evaluate javascript.
usage: /<command> Javascript code
permission: ScriptCraft.js
permission-message: You don't have <permission>

View file

@ -0,0 +1,12 @@
package net.walterhiggins.scriptcraft;
public interface IScriptCraft
{
public double[] getPlayerPos();
public double[] getMousePos();
public void putSign(String[] texts,int x, int y, int z, int block, int meta);
public void putBlock(int x, int y, int z, int blockId, int meta);
public String getBlock(int x, int y, int z);
public void notifyAdministrators(String message);
public void setInvoker(Object invoker);
}

View file

@ -0,0 +1,20 @@
package net.walterhiggins.scriptcraft;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.*;
public class ScriptCraft extends JavaPlugin
{
@Override
public void onEnable(){
getLogger().info("ScriptCraft enabled.");
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if(cmd.getName().equalsIgnoreCase("js"))
{
this.getLogger().info(cmd.toString());
return true;
}
return false;
}
}

View file

@ -0,0 +1,22 @@
package net.walterhiggins.scriptcraft;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.*;
public class ScriptCraft extends JavaPlugin
{
@Override
public void onEnable(){
getLogger().info("ScriptCraft.onEnable has been invoked!");
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if(cmd.getName().equalsIgnoreCase("js")){
// If the player typed /basic then do the following...
// doSomething
this.getLogger().info("hellobukkit command");
return true;
} //If this has happened the function will return true.
// If this hasn't happened the a value of false will be returned.
return false;
}
}

View file

@ -0,0 +1,131 @@
package net.walterhiggins.scriptcraft;
import java.util.Set;
import org.bukkit.command.*;
import org.bukkit.entity.*;
import org.bukkit.block.*;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.plugin.java.JavaPlugin;
public class ScriptCraftBukkit implements IScriptCraft
{
/* (non-Javadoc)
* @see net.walterhiggins.scriptcraft.IScriptCraft#putSign(java.lang.String[], int, int, int, int, int)
*/
@Override
public void putSign(String[] texts, int x, int y, int z, int blockId, int meta) {
// TODO Auto-generated method stub
putBlock(x,y,z,blockId,meta);
Block block = this.getBlockObjectAt(x, y, z);
if (block instanceof Sign){
Sign sign = (Sign)block;
for (int i = 0; i < texts.length;i++){
sign.setLine(i%4, texts[i]);
}
sign.update(true);
}
}
private void _putBlock(World world,int x, int y, int z, int blockId, int meta){
Block block = world.getBlockAt(x, y, z);
block.setTypeId(blockId);
block.setData((byte)meta);
}
/* (non-Javadoc)
* @see net.walterhiggins.scriptcraft.IScriptCraft#putBlock(int, int, int, int, int)
*/
@Override
public void putBlock(int x, int y, int z, int blockId, int meta) {
World world = this.getInvokerWorld();
this._putBlock(world, x, y, z, blockId, meta);
}
private final World getInvokerWorld(){
if (this.invoker instanceof Player){
Player player = (Player)this.invoker;
return player.getLocation().getWorld();
}
if (this.invoker instanceof BlockCommandSender){
BlockCommandSender bcs = (BlockCommandSender)this.invoker;
return bcs.getBlock().getLocation().getWorld();
}
return null;
}
private final Block getBlockObjectAt(int x,int y, int z){
World world = getInvokerWorld();
if (world != null)
return world.getBlockAt(x, y, z);
return null;
}
/* (non-Javadoc)
* @see net.walterhiggins.scriptcraft.IScriptCraft#getBlock(int, int, int)
*/
@Override
public String getBlock(int x, int y, int z) {
Block block = this.getBlockObjectAt(x, y, z);
if (block !=null)
return "" + block.getTypeId() + ":" + block.getData();
return null;
}
/* (non-Javadoc)
* @see net.walterhiggins.scriptcraft.IScriptCraft#notifyAdministrators(java.lang.String)
*/
@Override
public void notifyAdministrators(String message) {
Set<OfflinePlayer> ops = this.plugin.getServer().getOperators();
for (OfflinePlayer op : ops){
if (op.isOnline()){
op.getPlayer().chat(message);
}
}
this.plugin.getLogger().info(message);
}
protected JavaPlugin plugin = null;
public CommandSender invoker = null;
public void setInvoker(Object invoker)
{
this.invoker = (CommandSender)invoker;
}
public ScriptCraftBukkit(JavaPlugin plugin){
this.plugin = plugin;
}
public double[] getPlayerPos()
{
double[] result = new double[4];
if (this.invoker instanceof Player){
Player player = (Player)this.invoker;
Location location = player.getLocation();
result[0] = location.getX();
result[1] = location.getY();
result[2] = location.getZ();
result[3] = location.getYaw();
return result;
}else{
return null;
}
}
public double[] getMousePos()
{
double[] result = new double[4];
if (this.invoker instanceof Player){
Player player = (Player)this.invoker;
Block targetedBlock = player.getTargetBlock(null,5);
if (targetedBlock == null || targetedBlock.isEmpty()){
return null;
}
Location location = targetedBlock.getLocation();
result[0] = location.getX();
result[1] = location.getY();
result[2] = location.getZ();
result[3] = location.getYaw();
return result;
}else{
return null;
}
}
}

View file

@ -0,0 +1,19 @@
package net.walterhiggins.scriptcraft;
import org.bukkit.command.*;
public class ScriptCraftBukkit implements IScriptCraft
{
public CommandSender invoker = null;
public void setInvoker(Object invoker)
{
this.invoker = (CommandSender)invoker;
}
public int[] getPlayerPos()
{
}
}

View file

@ -0,0 +1,277 @@
package net.walterhiggins.scriptcraft;
import org.mozilla.javascript.*;
import java.util.List;
import java.io.*;
import javax.swing.JFileChooser;
public class ScriptCraftEvaluator
{
protected static IScriptCraft sc = null;
protected Context ctx = null;
protected Scriptable scope = null;
public static class MCScope extends ImporterTopLevel{
/**
*
*/
private static final long serialVersionUID = 1L;
public MCScope(Context ctx){
super(ctx);
}
}
public ScriptCraftEvaluator(IScriptCraft scImpl)
{
ScriptCraftEvaluator.sc = scImpl;
this.ctx = Context.enter();
ScriptableObject importer = new ScriptCraftEvaluator.MCScope(ctx);
this.scope = this.ctx.initStandardObjects(importer);
//
// for mcp debug only
//ctx.evaluateString(scope,"importPackage(net.minecraft.src)","<cmd>",1,null);
//
String[] names = {
"print"
,"load"
,"help"
,"getPlayerPos"
,"getMousePos"
,"putBlock"
,"getBlock"
,"putSign"
};
importer.defineFunctionProperties(names,
ScriptCraftEvaluator.class,
ScriptableObject.DONTENUM);
}
/**
* So clients can add their own properties ...
*
* evaluator.getScope().defineProperty("jsVarName",javaObject);
*/
public ScriptableObject getScope()
{
return (ScriptableObject)this.scope;
}
public Object eval(String javascript, Object invoker)
{
ScriptCraftEvaluator.sc.setInvoker(invoker);
ScriptCraftEvaluator.sc.notifyAdministrators("js> " + javascript);
Object result = null;
try
{
result = ctx.evaluateString(this.scope, javascript, "<cmd>", 1, null);
}
catch(Exception e)
{
e.printStackTrace(System.err);
ScriptCraftEvaluator.sc.notifyAdministrators("Exception: " + e.getMessage());
}
if (result != null)
{
ScriptCraftEvaluator.sc.notifyAdministrators(Context.toString(result));
}
return result;
}
/**
* Load a javascript source file and evaluate its contents.
*/
public static Object load(Context cx, Scriptable thisObj, Object[] args, Function funObj)
{
Object result = null;
File scriptFile = null;
String filename = null;
if (args.length == 0)
{
JFileChooser fc = new javax.swing.JFileChooser();
int rc = fc.showOpenDialog(null);
if (rc ==JFileChooser.APPROVE_OPTION){
scriptFile = fc.getSelectedFile();
}else{
return result;
}
}else{
scriptFile = new File((String)args[0]);
}
FileReader in = null;
try {
in = new FileReader(scriptFile);
}
catch (FileNotFoundException ex)
{
ex.printStackTrace(System.err);
ScriptCraftEvaluator.sc.notifyAdministrators( "Error - File not found " + args[0]);
Context.reportError("Couldn't open file \"" + scriptFile + "\".");
return null;
}
filename = scriptFile.getAbsolutePath();
System.out.println("ScripCraftEvaluator: filename=" + filename);
File parentFile = scriptFile.getParentFile();
String filedir = null;
if (parentFile !=null){
filedir = parentFile.getAbsolutePath();
}
//
// setup the special script-context-only variables
//
((ScriptableObject)thisObj).defineProperty("$SCRIPT",filename,ScriptableObject.DONTENUM);
((ScriptableObject)thisObj).defineProperty("$SCRIPT_DIR",filedir==null?"":filedir,ScriptableObject.DONTENUM);
try {
// Here we evalute the entire contents of the file as
// a script. Text is printed only if the print() function
// is called.
ScriptCraftEvaluator.sc.notifyAdministrators( "Loading " + filename);
result = cx.evaluateReader(thisObj, in, filename, 1, null);
ScriptCraftEvaluator.sc.notifyAdministrators( "Successfully loaded " + filename);
}
catch (WrappedException we) {
we.printStackTrace(System.err);
String wes = we.getWrappedException().toString();
ScriptCraftEvaluator.sc.notifyAdministrators("WrappedException while loading " + filename + ": " + wes);
System.err.println(wes);
we.printStackTrace();
}
catch (EvaluatorException ee) {
ee.printStackTrace(System.err);
System.err.println("js: " + ee.getMessage());
ScriptCraftEvaluator.sc.notifyAdministrators( "EvaluatorException while loading " + filename + ": " + ee.getMessage());
}
catch (JavaScriptException jse) {
jse.printStackTrace(System.err);
System.err.println("js: " + jse.getMessage());
ScriptCraftEvaluator.sc.notifyAdministrators("JavascriptException while loading " + filename + ": " + jse.getMessage());
}
catch (IOException ioe) {
ioe.printStackTrace(System.err);
System.err.println(ioe.toString());
ScriptCraftEvaluator.sc.notifyAdministrators( "IOException while loading " + filename + ": " + ioe.getMessage());
}
finally {
try {
in.close();
}
catch (IOException ioe) {
System.err.println(ioe.toString());
}
}
return result;
}
public static void help(Context cx, Scriptable thisObj, Object[] args, Function funObj)
{
String cwd = java.lang.System.getProperty("user.dir");
String[] helpArgs = {"Current Working Directory: " + cwd,
"load('path-to-script.js')",
"load() (with no params) lets you choose a script file",
"getPlayerPos() returns player coords",
"getMousePos() returns mouse/crosshair coords",
"getBlock(x,y,z) returns the block and metadata e.g. '98' for a stone block or '98:2' for a mossy stone block",
"putBlock(x,y,z,blockId,meta) e.g. putBlock(100,2,50,44,2) puts a sandstone slab (44:2) at position 100,2,50. See http://www.minecraftinfo.com/idlist.htm for block ids"
};
print(cx,thisObj,helpArgs,funObj);
}
public static void print(Context cx, Scriptable thisObj,Object[] args, Function funObj)
{
for (int i=0; i < args.length; i++) {
if (i > 0){
System.out.print(" ");
}
// Convert the arbitrary JavaScript value into a string form.
String s = Context.toString(args[i]);
ScriptCraftEvaluator.sc.notifyAdministrators(s);
System.out.print(s);
}
System.out.println();
}
public static double[] getPlayerPos(Context cx, Scriptable thisObj,Object[] args, Function funObj)
{
return ScriptCraftEvaluator.sc.getPlayerPos();
}
public static double[] getMousePos(Context cx, Scriptable thisObj,Object[] args, Function funObj)
{
return ScriptCraftEvaluator.sc.getMousePos();
}
public static void putBlock(Context cx, Scriptable thisObj,Object[] args, Function funObj)
{
int x;
int y;
int z;
int b;
int m;
if (args.length == 2){
double[] mousePos = ScriptCraftEvaluator.sc.getMousePos();
if (mousePos != null){
x = (int)mousePos[0];
y = (int)mousePos[1];
z = (int)mousePos[2];
b = new Double(args[0].toString()).intValue();
m = new Double(args[1].toString()).intValue();
}else{
return;
}
}else {
x = new Double(args[0].toString()).intValue();
y = new Double(args[1].toString()).intValue();
z = new Double(args[2].toString()).intValue();
b = new Double(args[3].toString()).intValue();
m = new Double(args[4].toString()).intValue();
}
ScriptCraftEvaluator.sc.putBlock(x,y,z,b,m);
}
//
// gets the blockId and metadata at the given coords
// if no coords are provided then the mouse position is used instead.
//
public static String getBlock(Context cx, Scriptable thisObj,Object[] args, Function funObj){
int x;
int y;
int z;
if (args.length != 0){
x = new Double(args[0].toString()).intValue();
y = new Double(args[1].toString()).intValue();
z = new Double(args[2].toString()).intValue();
}else{
double[] mousePos = ScriptCraftEvaluator.sc.getMousePos();
if (mousePos != null){
x = (int)mousePos[0];
y = (int)mousePos[1];
z = (int)mousePos[2];
}else{
return null;
}
}
return ScriptCraftEvaluator.sc.getBlock(x,y,z);
}
@SuppressWarnings("unchecked")
public static void putSign(Context cx, Scriptable thisObj,Object[] args, Function funObj){
List<String> jsArray = (List<String>)args[0];
String[] texts = new String[4];
int i = 0;
for (String s : jsArray){
texts[i++] = s;
}
/*
for (int i = 0; i < jsArray.size() && i <= 3;i++){
texts[i] = (String)jsArray.get(i);
}
*/
int x = new Double(args[1].toString()).intValue();
int y = new Double(args[2].toString()).intValue();
int z = new Double(args[3].toString()).intValue();
int b = new Double(args[4].toString()).intValue();
int m = new Double(args[5].toString()).intValue();
ScriptCraftEvaluator.sc.putSign(texts,x,y,z,b,m);
}
}

View file

@ -0,0 +1,52 @@
package net.walterhiggins.scriptcraft;
import java.io.File;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.*;
import org.mozilla.javascript.*;
public class ScriptCraftPlugin extends JavaPlugin
{
// right now all ops share the same JS context/scope
// need to look at possibly having context/scope per operator
//protected Map<CommandSender,ScriptCraftEvaluator> playerContexts = new HashMap<CommandSender,ScriptCraftEvaluator>();
protected ScriptCraftEvaluator evaluator = null;
@Override
public void onEnable(){
getLogger().info("ScriptCraft enabled.");
if (this.evaluator == null){
this.evaluator = new ScriptCraftEvaluator(new ScriptCraftBukkit(this));
this.evaluator.getScope().defineProperty("plugin", this, ScriptableObject.READONLY);
//
// Auto-load Javascript plugins from the js-plugins directory
// in the current working directory
//
String userDir = System.getProperty("user.dir");
File jsPlugins = new File(userDir + System.getProperty("file.separator") + "js-plugins");
if (jsPlugins.exists()){
File[] files = jsPlugins.listFiles();
for (File f: files){
this.evaluator.eval("load(\"" + f.getAbsolutePath() + "\")", null);
}
}
}
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if(cmd.getName().equalsIgnoreCase("js"))
{
this.evaluator.getScope().defineProperty("self", sender, ScriptableObject.DONTENUM);
String javascriptCode = "";
for (int i = 0;i < args.length; i++){
javascriptCode = javascriptCode + args[i] + " ";
}
this.evaluator.eval(javascriptCode, sender);
return true;
}
return false;
}
}

View file

@ -0,0 +1,20 @@
package net.walterhiggins.scriptcraft;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.*;
public class ScriptCraftPlugin extends JavaPlugin
{
@Override
public void onEnable(){
getLogger().info("ScriptCraft enabled.");
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if(cmd.getName().equalsIgnoreCase("js"))
{
this.getLogger().info(cmd.toString());
return true;
}
return false;
}
}