From 23794e985a0dfba7ae43b19d64ef838f8fb8b02e Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Mon, 7 Jan 2013 23:59:42 +0000 Subject: [PATCH] reorg --- js-plugins/core/_primitives.js | 114 +++++++++++++++++++++++++++ js-plugins/core/_scriptcraft.js | 84 ++++++++++++++++++++ js-plugins/{ => drone}/castle.js | 0 js-plugins/{ => drone}/cottage.js | 0 js-plugins/{ => drone}/dancefloor.js | 0 js-plugins/{ => drone}/drone.js | 0 js-plugins/{ => drone}/fort.js | 0 js-plugins/{ => drone}/temple.js | 0 8 files changed, 198 insertions(+) create mode 100644 js-plugins/core/_primitives.js create mode 100644 js-plugins/core/_scriptcraft.js rename js-plugins/{ => drone}/castle.js (100%) rename js-plugins/{ => drone}/cottage.js (100%) rename js-plugins/{ => drone}/dancefloor.js (100%) rename js-plugins/{ => drone}/drone.js (100%) rename js-plugins/{ => drone}/fort.js (100%) rename js-plugins/{ => drone}/temple.js (100%) diff --git a/js-plugins/core/_primitives.js b/js-plugins/core/_primitives.js new file mode 100644 index 0000000..ba25031 --- /dev/null +++ b/js-plugins/core/_primitives.js @@ -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; + +}()); diff --git a/js-plugins/core/_scriptcraft.js b/js-plugins/core/_scriptcraft.js new file mode 100644 index 0000000..510ed0b --- /dev/null +++ b/js-plugins/core/_scriptcraft.js @@ -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()); + +}()); + + + + diff --git a/js-plugins/castle.js b/js-plugins/drone/castle.js similarity index 100% rename from js-plugins/castle.js rename to js-plugins/drone/castle.js diff --git a/js-plugins/cottage.js b/js-plugins/drone/cottage.js similarity index 100% rename from js-plugins/cottage.js rename to js-plugins/drone/cottage.js diff --git a/js-plugins/dancefloor.js b/js-plugins/drone/dancefloor.js similarity index 100% rename from js-plugins/dancefloor.js rename to js-plugins/drone/dancefloor.js diff --git a/js-plugins/drone.js b/js-plugins/drone/drone.js similarity index 100% rename from js-plugins/drone.js rename to js-plugins/drone/drone.js diff --git a/js-plugins/fort.js b/js-plugins/drone/fort.js similarity index 100% rename from js-plugins/fort.js rename to js-plugins/drone/fort.js diff --git a/js-plugins/temple.js b/js-plugins/drone/temple.js similarity index 100% rename from js-plugins/temple.js rename to js-plugins/drone/temple.js