This commit is contained in:
walterhiggins 2013-01-07 23:59:42 +00:00
parent 588c7afe7e
commit 23794e985a
8 changed files with 198 additions and 0 deletions

View file

@ -0,0 +1,114 @@
//
// define these primitive methods used by drone.js (and potentiall others)
//
// getPlayerPos returns the player's x,y,z and yaw (direction)
// getMousePos returns the x,y,z of the current block being targeted.
// putBlock(x,y,z,blockId,metadata) puts a block at a location in current world
// getBlock(x,y,z) gets the block and metadata (returned as a string in form '35:15')
// putSign(texts,x,y,z,blockId,metadata) puts a sign at the given location
// notifyAdministrators(msg) sends a message to all admins/ops.
// echo(msg) prints a message on screen to current user.
//
(function(){
//
// only execute once
//
if (typeof getPlayerPos != "undefined"){
return;
}
importPackage(org.bukkit.command);
importPackage(org.bukkit.block);
importPackage(org.bukkit.entity);
var _getPlayerPos = function(){
if (typeof __self == "undefined")
return;
return __self.location;
};
var _getMousePos = function(){
if (typeof __self == "undefined")
return;
// __self might be CONSOLE or a CommandBlock
if (!__self.getTargetBlock)
return;
var targetedBlock = __self.getTargetBlock(null,5);
if (targetedBlock == null || targetedBlock.isEmpty()){
return null;
}
return targetedBlock.location;
};
var _putBlock = function(x,y,z,blockId,metadata){
if (typeof metadata == "undefined"){
metadata = 0;
}
var world = _getWorld();
if (world){
var block = world.getBlockAt(x,y,z);
block.setTypeId(blockId);
block.setData(metadata);
}
// todo add support for trees.
};
var _putSign = function(texts, x, y, z, blockId, meta){
putBlock(x,y,z,blockId,meta);
var block = _getBlockObject(x,y,z);
state = block.state;
if (state instanceof Sign){
for (var i = 0;i < texts.length; i++)
state.setLine(i%4,texts[i]);
state.update(true);
}
};
var _getBlock = function(x,y,z){
var block = _getBlockObject(x,y,z);
if (block)
return "" + block.typeId + ":" + block.data;
};
var _getBlockObject = function(x,y,z){
var world = _getWorld();
if (world)
return world.getBlockAt(x,y,z);
};
var _getWorld = function(){
if (typeof __self == "undefined")
return;
if (__self instanceof BlockCommandSender)
return __self.block.location.world;
if (__self instanceof Player)
return __self.location.world;
};
var _notifyAdministrators = function(msg){
var ops = __plugin.server.operators;
for (var i = 0; i < ops.size();i++){
if (ops[i].isOnline())
ops[i].player.chat(msg);
}
__plugin.logger.info(msg);
};
var _echo = function(msg){
__plugin.logger.info(msg);
if (typeof __self == "undefined"){
java.lang.System.out.println(msg);
return;
}
__self.sendMessage(msg);
};
getPlayerPos = _getPlayerPos;
getMousePos = _getMousePos;
putBlock = _putBlock;
getBlock = _getBlock;
putSign = _putSign;
notifyAdministrators = _notifyAdministrators;
echo = _echo;
}());

View file

@ -0,0 +1,84 @@
var global = this;
var ScriptCraft = ScriptCraft || {};
ScriptCraft.core = ScriptCraft.core || {};
//
// private implementation
//
(function(){
//
// don't execute this more than once
//
if (typeof load == "function")
return ;
var _originalScript = __script;
importPackage(java.io);
var _canonize = function(file){
return file.getCanonicalPath().replaceAll("\\\\","/");
};
var _load = function(filename){
print("loading " + filename);
var file = new File(filename);
if (file.exists()){
var parent = file.getParentFile();
var reader = new FileReader(file);
__engine.put("__script",_canonize(file));
__engine.put("__folder",(parent?_canonize(parent):"")+"/");
__engine.eval(reader);
}else{
print("Error: " + filename + " not found");
}
};
var _listJsFiles = function(store,dir)
{
if (typeof dir == "undefined"){
dir = new File(_originalScript).getParentFile().getParentFile();
}
var files = dir.listFiles();
for (var i = 0;i < files.length; i++){
if (files[i].isDirectory()){
_listJsFiles(store,files[i]);
}else{
if (files[i].getCanonicalPath().endsWith(".js") &&
!(files[i].getName().startsWith("_")))
{
store.push(files[i]);
}
}
}
};
var _reload = function(pluginDir)
{
var jsFiles = [];
_listJsFiles(jsFiles,pluginDir);
//
// script files whose name begins with _ (underscore)
// will not be loaded automatically at startup.
// These files are assumed to be dependencies/private to plugins
//
// E.g. If you have a plugin called myMiniGame.js in the myMiniGame directory
// and which in addition to myMiniGame.js also includes _myMiniGame_currency.js _myMiniGame_events.js etc.
// then it's assumed that _myMiniGame_currency.js and _myMiniGame_events.js will be loaded
// as dependencies by myMiniGame.js and do not need to be loaded via js reload
//
for (var i = 0;i < jsFiles.length; i++){
load(_canonize(jsFiles[i]));
}
};
ScriptCraft.core.load = _load;
ScriptCraft.core.reload = _reload;
for (var f in ScriptCraft.core){
global[f] = ScriptCraft.core[f];
}
ScriptCraft.core.initialized = true;
//
// assumes this was loaded from js-plugins/core/
//
reload(new File(__script).getParentFile().getParentFile());
}());