reorg
This commit is contained in:
parent
4a434dfdd2
commit
588c7afe7e
5 changed files with 0 additions and 517 deletions
|
@ -1,9 +0,0 @@
|
|||
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>
|
|
@ -1,12 +0,0 @@
|
|||
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);
|
||||
}
|
|
@ -1,132 +0,0 @@
|
|||
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);
|
||||
BlockState blockState = block.getState();
|
||||
if (blockState instanceof Sign){
|
||||
Sign sign = (Sign)blockState;
|
||||
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);
|
||||
// TODO - add support for trees.
|
||||
}
|
||||
/* (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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,300 +0,0 @@
|
|||
package net.walterhiggins.scriptcraft;
|
||||
|
||||
import org.mozilla.javascript.*;
|
||||
import java.util.List;
|
||||
import java.io.*;
|
||||
import javax.swing.JFileChooser;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
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;
|
||||
}
|
||||
//
|
||||
// wph 20130105 - Only the console user should be able to load a script
|
||||
// players should not be able to open scripts
|
||||
//
|
||||
private static Object invoker = null;
|
||||
|
||||
|
||||
public Object eval(String javascript, Object invoker)
|
||||
{
|
||||
ScriptCraftEvaluator.invoker = 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;
|
||||
}
|
||||
protected static Object loadJsFile(File scriptFile,Context cx, Scriptable thisObj)
|
||||
{
|
||||
Object result = null;
|
||||
FileReader in = null;
|
||||
try {
|
||||
in = new FileReader(scriptFile);
|
||||
}
|
||||
catch (FileNotFoundException ex)
|
||||
{
|
||||
ex.printStackTrace(System.err);
|
||||
return null;
|
||||
}
|
||||
String filename = null;
|
||||
String filedir = null;
|
||||
File parentFile = null;
|
||||
try{
|
||||
filename = scriptFile.getCanonicalPath().replaceAll("\\\\","/");
|
||||
parentFile = scriptFile.getParentFile();
|
||||
if (parentFile !=null){
|
||||
filedir = parentFile.getCanonicalPath().replaceAll("\\\\","/");
|
||||
}
|
||||
}catch(IOException ioe){
|
||||
ioe.printStackTrace(System.err);
|
||||
}
|
||||
//
|
||||
// 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.
|
||||
result = cx.evaluateReader(thisObj, in, filename, 1, null);
|
||||
|
||||
}
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* Load a javascript source file and evaluate its contents.
|
||||
*/
|
||||
public static Object load(Context cx, Scriptable thisObj, Object[] args, Function funObj)
|
||||
{
|
||||
if (ScriptCraftEvaluator.invoker instanceof Player){
|
||||
ScriptCraftEvaluator.sc.notifyAdministrators(invoker.toString() + " tried to load a script.\n" +
|
||||
"Only the console user can load scripts.\n" +
|
||||
"Players cannot load scripts.");
|
||||
return null;
|
||||
}
|
||||
|
||||
Object result = null;
|
||||
|
||||
File scriptFile = 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]);
|
||||
}
|
||||
|
||||
result = loadJsFile(scriptFile,cx,thisObj);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
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()){
|
||||
loadJsPlugins(jsPlugins);
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
// called recursively to load all js plugins in the js-plugins directory and
|
||||
// sub-directories
|
||||
//
|
||||
private void loadJsPlugins (File directory){
|
||||
File[] files = directory.listFiles();
|
||||
for (File f: files){
|
||||
if (f.isDirectory()){
|
||||
loadJsPlugins(f);
|
||||
}else{
|
||||
if (f.getAbsolutePath().endsWith(".js")){
|
||||
this.getLogger().info("Loading javascript source file " + f);
|
||||
ScriptCraftEvaluator.loadJsFile(f,this.evaluator.ctx,this.evaluator.scope);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue