diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..aa55a38 --- /dev/null +++ b/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/.gitignore b/.gitignore index d63443c..b6ba717 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,29 @@ target *.class *.js~ *.js# +*.md# +*.md~ +*.mdt~ +*.mdt# *.iml +*.#* *.idea .DS_Store build.local.properties +/src/main/javascript/lib/.#tabcomplete.js +/src/main/javascript/plugins/.#example-1.js +/nbproject/private/private.xml +/nbproject/project.xml +/src/docs/nbproject/private/ +/src/docs/build//#build.xml# +/#build.xml# +/src/docs/build/classes/generateApiDocs.js +/src/docs/build/classes/generateTOC.js +/src/docs/build/classes/hello.js +/src/docs/build/classes/.netbeans_automatic_build +/src/docs/build/classes/.netbeans_update_resources +build +nbproject +/manifest.mf +/platform.properties +CopyLibs \ No newline at end of file diff --git a/.project b/.project new file mode 100644 index 0000000..2709178 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + ScriptCraft + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b2168ad --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: java +jdk: + - oraclejdk8 + - oraclejdk7 + - openjdk7 diff --git a/README.md b/README.md index 3b0a5dd..809d43f 100644 --- a/README.md +++ b/README.md @@ -1,125 +1,266 @@ -ScriptCraft -=========== -A Minecraft mod that lets you create mods using Javascript. +# ScriptCraft - Modding Minecraft with Javascript -Description -=========== -ScriptCraft is a plugin for Minecraft Servers which lets -operators, administrators and plug-in authors customize the game using -Javascript. ScriptCraft makes it easier to create your own mods. Mods -can be written in Javscript and can use the full Bukkit API. The -ScriptCraft mod also lets you enter javascript commands at the in-game -prompt. To bring up the in-game prompt press the `/` key then type -`js ` followed by any javascript statement. E.g. `/js 1+1` will print -2. +[![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/walterhiggins/ScriptCraft?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -ScriptCraft also includes many objects and functions to make building and modding easier using Javascript. +ScriptCraft lets you write Minecraft Mods using Javascript - a +programming language that's relatively easy to learn and use. +ScriptCraft is a Minecraft Server plugin which means it must be used +with a Minecraft server. Once you've downloaded and installed the +Minecraft Server, then installed the ScriptCraft Plugin you can write +your own Minecraft mods using Javascript. - * echo( message ) - displays a message on the player's screen. e.g. `/js echo( 1 + 3 )` or `/js echo ("Hello World")` - * getMousePos() - A function which returns the current position of the cross-hairs (if a block is targeted) - * getPlayerPos() - A function which returns the current position of the player. - * putBlock( x, y, z, blockId, metaData ) - A function which lets you place a block anywhere (if no coordinates are given the block the player is currently looking at is replaced). - * getBlock( x, y, z ) - returns the blockId and metadata at the given location (if no coordinates are given the cross-hair location is used) - * putSign( String[] texts, x, y, z, blockId, metaData ) - A function which lets you place a sign. +I created ScriptCraft to make it easier for younger programmers to +create their own Minecraft Mods. Mods are written using the +JavaScript programming language. Once the ScriptCraft mod is +installed, you can add your own new Mods by adding JavaScript (.js) +files in a directory. -The above primitives can be used to create buildings which would -otherwise be time-consuming to create manually. It is highly -recommended using the attached [drone][drone] javascript plugin which -provides a fluent API for building. The Javascript `Drone` class -provides a much richer API which can be used to construct -buildings. See the attached [cottage.js][cottage] file for an example -of you can use the sample Drone plugin to create new buildings in + * If you're new to programming and want to start modding Minecraft, then [Start Here][yp]. + * If you've already used [Scratch][scr], have attended a few + [CoderDojo][cd] sessions, or have already dabbled with JavaScript, + then [Start Here][cda]. + * Watch some [demos][ytpl] of what you can do with ScriptCraft. + +This is a simple mod in a file called greet.js in the scriptcraft/plugins directory: + +```javascript +function greet( player ) { + echo( player, 'Hello ' + player.name ); +} +exports.greet = greet; +``` + +At the in-game prompt, type: + +```javascript +/js greet(self) +``` + +Anything you can do using the Spigot or CanaryMod APIs in Java, +you can do using ScriptCraft in JavaScript. + +# Description + +ScriptCraft is a plugin for Minecraft Servers which lets operators, +administrators and plug-in authors customize the game using +JavaScript. ScriptCraft makes it easier to create your own mods. Mods +can be written in Javscript and can use the full [SpigotMC +API][spigot] or [CanaryMod API][cm]. ScriptCraft works with all of the +following Minecraft Server software: + +* [SpigotMC][spigot] (Recommended) +* [GlowStone][gs] +* [CanaryMod][cm] + +[spigot]: http://www.spigotmc.org/ +[gs]: http://www.glowstone.net/ +[cm]: http://canarymod.net/ + +I recommend using SpigotMC because both CanaryMod and CraftBukkit are +no longer being actively developed. The ScriptCraft mod also lets you +enter javascript commands at the in-game prompt. To bring up the +in-game prompt press the `/` key then type `js ` followed by any +javascript statement. For example: `/js 1 + 1` will print 2. + +ScriptCraft also includes many objects and functions to make building +and modding easier using JavaScript. The JavaScript `Drone` object +bundled with ScriptCraft provides an easy way to build at-scale in +Minecraft. See the attached [temple.js][temple] file for an example +of how you can use the sample Drone plugin to create new buildings in Minecraft. [drone]: https://github.com/walterhiggins/ScriptCraft/tree/master/src/main/javascript/drone/drone.js -[cottage]: https://github.com/walterhiggins/ScriptCraft/tree/master/src/main/javascript//drone/cottage.js +[cottage]: https://github.com/walterhiggins/ScriptCraft/tree/master/src/main/js/plugins/drone/contrib/cottage.js +[temple]: https://github.com/walterhiggins/ScriptCraft/blob/master/src/main/js/plugins/drone/contrib/temple.js +[bukkit]: http://dl.bukkit.org/ -Prerequisites -============= -You will need to have Java version 6 or 7 installed on your -machine. Check the version by typing `java -version` at a command -prompt. You will need to [install Bukkit][ib] on your machine. Bukkit -is a version of Minecraft (server) that makes it easy to install -plugins and customize Minecraft. You can [download the CraftBukkit -server here.][cbdl] +# Prerequisites -Installation -============ -If you don't want to compile from source, you can [download the -compiled plugin here][dl] and copy it the craftbukkit's plugins -directory. +ScriptCraft is a Minecraft Server Mod which only works with Minecraft +for Personal computers (Windows, Mac and Linux). It does not work with +X-BOX, Playstation or WiiU versions of the game. You will need to have +Java version 7 or later installed. Check the version by typing `java +-version` at a command prompt. -Post Install -============ -Once installed, a new js-plugins directory is automatically created in -the same directory as the plugins folder. All files in the js-plugins -directory will be automatically loaded when CraftBukkit starts. *Only -players who are ops can use this plugin.* You can grant a player `op` -privileges by typing 'op ' at the server console prompt or -by adding the player's username to the ops.txt file in your -craftbukkit directory. +# Installation -Launch CraftBukkit, then launch the Minecraft client and create a new +Before installing ScriptCraft you must first install SpigotMC which is +a special version of Minecraft Server that makes it easy to customize +the game. + +## Installing and Running SpigotMC + +Follow these steps to download and install SpigotMC. + +1. Download Spigot's [BuildTools.jar][spigotdl] +2. Save the BuildTools.jar file to a new directory called spigotmc. +3. Open a terminal (Mac and Linux) or command prompt (windows) window and type `java -jar BuildTools.jar`. This will kick off a long series of commands to "build" SpigotMC. +4. When the build is done, there will be a new file beginning with `spigot` and ending in `.jar` in the spigotmc directory. Run this file by typing `java -jar spigot-1.10.2.jar` (it might not be that exact name - you can list files in the directory by typing `dir` (Windows) or `ls` (Mac and Linux). +5. The server will start up then shut down very shortly afterwards. You'll need to edit a file called `eula.txt` - change `eula=false` to `eula=true` and save the file. +6. Run the `java -jar spigot-1.10.2.jar` command again - this time the server will start up. Shut it down by typing `stop` at the server prompt. + +## Installing ScriptCraft + +Follow these steps to download and install ScriptCraft. + +1. Download the [scriptcraft.jar][dl] plugin and save it to the `plugins` directory and restart the server by typing `java -jar spigot-1.10.2.jar`. +2. At the server prompt type `js 1 + 1` and hit enter. The result `2` should be displayed. + +Congratulations - you've just installed your Custom Minecraft Server and are ready to begin writing your first mod! + +# Post Install + +Once installed, a new scriptcraft/plugins directory is automatically +created. All files in the scriptcraft/plugins directory will be +automatically loaded when the server starts. *Only players who are +ops can use this plugin.* You can grant a player `op` privileges by +typing 'op ' (replacing with your own Minecraft +user name) at the server console prompt or by adding the player's +username to the ops.txt file in your server directory. + +Launch the server, then launch the Minecraft client and create a new server connection. The IP address will be `localhost` . Once you've -connected to your bukkit server and have entered the game, look at a -ground-level block and type ... +connected to your server and have entered the game, look at a +ground-level block and type: - /js up().box('35:15', 4, 9, 1) + /js up().box( blocks.wool.black, 4, 9, 1 ) -... This will create a black monolith structure 4 blocks wide by 9 +… This will create a black monolith structure 4 blocks wide by 9 blocks high by 1 block long. Take a look at the src/main/javascript/drone/drone.js file to see what ScriptCraft's -drone can do. If you're interested in customizing minecraft beyond -just creating new buildings, take a look at [./homes/homes.js][homes] -and [./chat/color.js][chatcolor] for examples of how to create a -javascript plugin for Minecraft. +drone can do. -[ho]: blob/master/src/main/javascript/homes/homes.js -[ch]: blob/master/src/main/javascript/chat/color.js -[ar]: blob/master/src/main/javascript/arrows/arrows.js -[si]: blob/master/src/main/javascript/signs/menu.js +If you're interested in customizing minecraft beyond just creating new buildings, take a look at [the homes mod][homes] for an example of how to create a more fully-featured JavaScript plugin for Minecraft. -A Javascript mod for minecraft is just a javascript source file (.js) -located in the craftbukkit/js-plugins directory. All .js files in this -directory will be automatically loaded when the craftbukkit server -starts. To get started writing your own mod, first take a look at some -of the existing mods in the [homes][ho], [chat][ch], [arrows][ar] and -[signs][si] directories. The chat/color.js mod is probably the -simplest mod to get started with. +## Your first mod - Howling blocks +Listed below is a simple mod that will make blocks 'Howl' when they're broken. -Additional information -====================== -Because the Bukkit API is open, all of the Bukkit API is accessible -via javascript once the ScriptCraft plugin is loaded. For example, in -addition to the functions provided in the MCP version of ScriptCraft, -there are a couple of useful Java objects exposed via javascript in -the Bukkit ScriptCraft plugin... +``` javascript +// copy and paste this code to a new file named 'scriptcraft/plugins/howling-blocks.js' +var sounds = require('sounds'); +function howl(event){ + sounds.entityWolfHowl( event.block ); +} +events.blockBreak( howl ); +``` - * `__plugin` - the ScriptCraft Plugin itself. This is a useful starting point for accessing other Bukkit objects. The `__plugin` object is of type [org.bukkit.plugin.java.JavaPlugin][api] and all of its properties and methods are accessible. For example... `js __plugin.server.motd` returns the server's message of the day (javascript is more concise than the equivalent java code: __plugin.getServer().getMotd() ). - * `self` - The player/command-block or server console operator who invoked the js command. Again, this is a good jumping off point for diving into the Bukkit API. - * `server` - The top-level org.bukkit.Server object. See the [Bukkit API docs][bukapi] for reference. +If you're using CanaryMod instead of SpigotMC you can [download the equivalent code](https://gist.github.com/walterhiggins/69cddd15160d803fb096). -[dl]: http://scriptcraftjs.org/download -[api]: http://jd.bukkit.org/apidocs/org/bukkit/plugin/java/JavaPlugin.html -[ib]: http://wiki.bukkit.org/Setting_up_a_server -[cbdl]: http://dl.bukkit.org/downloads/craftbukkit/ -[bukapi]: http://jd.bukkit.org/apidocs/ +A JavaScript mod for minecraft is just a JavaScript source file (.js) +located in the scriptcraft/plugins directory. All .js files in this +directory will be automatically loaded when the server starts. -Further Reading -=============== +To get started writing your own mod, take a look at some of the +[examples][examples]. + +[homes]: src/main/js/plugins/homes/homes.js +[examples]: src/main/js/plugins/examples/ + +# Additional information + +Because the SpigotMC API is open, all of the SpigotMC API is accessible +via javascript once the ScriptCraft plugin is loaded. There are a +couple of useful Java objects exposed via javascript in the +ScriptCraft plugin: + + * `__plugin` – the ScriptCraft Plugin itself. This is a useful + starting point for accessing other SpigotMC objects. The `__plugin` + object is of type [org.bukkit.plugin.Plugin][api] and all + of its properties and methods are accessible. For example: `js + __plugin.name` returns the plugin's name + (JavaScript is more concise than the equivalent Java code: + `__plugin.getName()` ). + + * `server` – The top-level org.bukkit.Server object. See the [SpigotMC API docs][spigotapi] for reference. + + * `self` – The player/command-block or server console operator who + invoked the `/js` command. Again, this is a good jumping off point for + diving into the SpigotMC API. + +[dl]: http://scriptcraftjs.org/download/latest +[api]: https://hub.spigotmc.org/javadocs/spigot/ +[ic]: http://canarymod.net/releases +[spigotdl]: https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar +[cmapi]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/ +[spigotapi]: https://hub.spigotmc.org/javadocs/spigot/ + +# Contributing + +If you would like to contribute source code and/or documentation changes please [read contributing.md][contrib] + +## Status + +[![Travis Build Status](https://api.travis-ci.org/walterhiggins/ScriptCraft.png)](http://travis-ci.org/walterhiggins/ScriptCraft) + +# Bukkit Configuration +## (You can ignore this if using CanaryMod) + +ScriptCraft works with Bukkit Plugin and uses the Bukkit Configuration +API. On first loading, ScriptCraft will create a config.yml file in +the plugins/scriptcraft/ directory. This file looks like this: + + extract-js: + plugins: true + modules: true + lib: true + +This file allows scriptcraft admins to turn on or off re-unzipping of the `modules`, +`plugins` and `lib` folders when deploying a new version of +ScriptCraft. It's strongly recommended that the `lib` directory always +be set to true to get the latest core ScriptCraft code . The modules +and plugins directories are optional and not part of ScriptCraft core. + +# Further Reading ScriptCraft has [its own website][website] with further information. - * To get started using ScriptCraft to Learn Javascript I recommend [reading this][yp]. + * To get started using ScriptCraft to Learn JavaScript, read [The Young Person's Guide to Programming in Minecraft][yp]. * The ScriptCraft [API documentation][api]. - * To delve deeper into creating your own minecraft mod, I recommend [reading this][mm]. + * To delve deeper into creating your own minecraft mod for use by others, read [Creating a complete Minecraft Mod in JavaScript][mm]. + * Take a look at some [examples][ex]. + * Buy the Official ScriptCraft Book [A Beginner's Guide to Writing Minecraft Plugins in Javascript][book]. + + You can find more information about [ScriptCraft on my blog][blog]. -[blog]: http://walterhiggins.net/blog/cat-index-scriptcraft.html -[buk]: https://github.com/walterhiggins/ScriptCraft/blob/master/bukkit.md -[yp]: http://walterhiggins.net/blog/YoungPersonProgrammingMinecraft -[mm]: http://walterhiggins.net/blog/ScriptCraft-1-Month-later -[api]: https://github.com/walterhiggins/ScriptCraft/blob/master/docs/api.md -[website]: http://scriptcraftjs.org/ +# Additional Resources +CoderDojo Athenry have some [excellent tutorials][cda] for younger +programmers who have used [Scratch][scr] and are interested in Modding +Minecraft using JavaScript. In particular, they have an excellent +[Scratch - to - JavaScript][sj] tutorial which explains Scratch +programs and how to do the same thing in JavaScript. + +I highly recommend the series of [tutorials provided by CoderDojo Athenry][cda]. + +Developer Chris Cacciatore has created some interesting tools using ScriptCraft: + + * [A wolf-bot][wb] + * [L-Systems (Large-scale fractal structures in Minecraft)][ls] + +# Docker + +To launch a container with SpigotMC and ScriptCraft you can just do + + docker run -p 25565:25565 -it tclavier/scriptcraft + +You can find all files used to build this container in github project: [docker-scriptcraft](https://github.com/tclavier/docker-scriptcraft) + + +[wb]: https://github.com/cacciatc/wolfbot +[ls]: https://github.com/cacciatc/scriptcraft-lsystems + +[blog]: http://walterhiggins.net/blog/cat-index-scriptcraft.html +[yp]: docs/YoungPersonsGuideToProgrammingMinecraft.md +[mm]: docs/Anatomy-of-a-Plugin.md +[api]: docs/API-Reference.md +[website]: http://scriptcraftjs.org/ +[cd]: http://coderdojo.com/ +[scr]: http://scratch.mit.edu/ +[cda]: http://cdathenry.wordpress.com/category/modderdojo/ +[ytpl]: http://www.youtube.com/watch?v=DDp20SKm43Y&list=PL4Tw0AgXQZH5BiFHqD2hXyXQi0-qFbGp_ +[ex]: src/main/js/plugins/examples +[contrib]: contributing.md +[sj]: http://cdathenry.wordpress.com/2013/10/12/modderdojo-week-2-moving-from-scratch-to-javascript/ +[book]: http://www.peachpit.com/store/beginners-guide-to-writing-minecraft-plugins-in-javascript-9780133930146 diff --git a/build.properties b/build.properties index 27e9f1b..f6a95ea 100644 --- a/build.properties +++ b/build.properties @@ -1 +1 @@ -bukkit-version=1.4.7 +scriptcraft-version=3.2.1 diff --git a/build.xml b/build.xml index 98cb1eb..48d0e1b 100644 --- a/build.xml +++ b/build.xml @@ -1,11 +1,42 @@ - Builds the scriptcraft.jar file - a plugin for bukkit - - + Builds the scriptcraft.jar file - a plugin for bukkit + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -16,101 +47,216 @@ - + + - - + - Retrieving CraftBukkit artifact info - - - - - - - Retrieving CraftBukkit jar - - - - Creating default ops.txt for your user - - - - - Starting Bukkit with ScriptCraft - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
<!-- +IMPORTANT NOTE FOR CONTRIBUTORS +------------------------------- +Contributors: This file is generated from comments in javascript source files src/main/js/* +If you would like to make changes, change the comments in the src/main/js/* files instead. +--> +# ScriptCraft API Reference + +Walter Higgins + +[walter.higgins@gmail.com][email] + +[email]: mailto:walter.higgins@gmail.com?subject=ScriptCraft_API_Reference + +
+ + +
- - - - - - - - - - - - - + + + + - - Retrieving Coffeescript compiler - - - + + + + - - - - + + + +
<!-- + IMPORTANT NOTE FOR CONTRIBUTORS + ------------------------------- + Contributors: This file is generated from source file src/docs/templates/ypgpm.md + If you would like to make changes, change file src/docs/templates/ypgpm.md instead +--> +# The Young Person's Guide to Programming in Minecraft +
+ + +
+
+ + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + - - - [[version]] - + + + + + + [[version]] + + + [[version]] - + + + - + - +
diff --git a/build/bukkit-to-url.xsl b/build/bukkit-to-url.xsl deleted file mode 100644 index bb48b3c..0000000 --- a/build/bukkit-to-url.xsl +++ /dev/null @@ -1,13 +0,0 @@ - - - - - http://dl.bukkit.org - - - - - - \ No newline at end of file diff --git a/contributing.md b/contributing.md new file mode 100644 index 0000000..aad163c --- /dev/null +++ b/contributing.md @@ -0,0 +1,119 @@ +# Status + +[![Travis Build Status](https://api.travis-ci.org/walterhiggins/ScriptCraft.png)](http://travis-ci.org/walterhiggins/ScriptCraft) + +# Notes for Contributors + +This project uses a Maven-like directory structure... + + src + + main + + java + + net + + walterhiggins + + scriptcraft + + ScriptCraftPlugin.java + + js + + lib + + (core javascript code goes here. Modules in this directory + should not be 'require'd by plugin or module authors) + modules + + (this is where module authors should put modules for + use by others) + plugins + + (this is where plugins - scriptcraft extensions for use by + operators and players should go) + resources + + plugin.yml + docs + + templates + + (documentation templates go here. If you want to make + changes to the young persons guide should be made to ypgpm.md) + ypgpm.md + javascript + + (javascript source used to build the API reference and + table of contents for the API reference and Programming Guide + is located here) + docs + + (the following files should not be edited directly because they are constructed + during the build process - yeah I know they strictly shouldn't be under source control but + it's nice to have the markdown docs on github for reading by non-contributors) + + API-Reference.md + Young-Persons-Guide.md + + (this project is build using Ant, type `ant` at the command line to build.) + + build.xml + build.properties + +## Core javascript modules + +ScriptCraft's deployed core consists of a single Java source file (the +Bukkit Plugin) and a tiny set of javascript source files located in +the src/main/javascript/lib directory. All other javascript files are +optional modules and plugins. `scriptcraft.js` is the first file +loaded by the Java plugin. scriptcraft.js in turn loads the require.js +file which initializes the commonJS `require()` function and all other +files in the lib directory are then loaded. Finally all of the modules +in the plugins directory are automatically loaded and any exports are +automatically exported to the global namespace. For example a file +called `greet.js` located in the plugins folder... + + // plugins/greet.js contents + + exports.greet = function(sender){ + sender.sendMessage('hello') + } + +... will be loaded at startup and the `greet` function will be +global. Anyone with operator privileges can type `/js greet(self)` at +the in-game command prompt to execute the function. + +## Coding Conventions + +See for a recommended +approach to writing javascript code for ScriptCraft. ScriptCraft is +aimed at younger programmers so readability is important - moreso than +cleverness. If submitting new code for inclusion in ScriptCraft please +ensure it is documented using the guidelines below... + +## Documentation contributions + +The Young persons guide to programming source file is located at +/src/docs/templates/ypgpm.md . *Do not make changes to +/docs/YoungPersonsGuide.md* + +The API Reference is generated by the build from markdown comments +embedded in the javascript source. If you would like comments for +contributed code to be included in the API reference then enclose your +comment as follows: + + * Start the comment block with a `/**********` (a forward-slash + followed by 10 or more asterisk characters). *The start block must + be at the start of the line*. + + * End the comment block with a `***/` (3 asterisk followed by a + forward slash) at the start of a new line. + +This is an example of a comment which will be included in the API reference... + + /********************* + ## foo() function + + The foo() function performs foo-type operatations on all bars. + + ### Parameters + + * name : Name of the foo + * count: Number of foos to perform + + ### Returns + foo() returns a list of foos that were changed. + + ***/ + +Top level comments for a module should be a H2 heading `##`. Please +don't use a H1 heading ( `#` ) as this is used for the top-level API +document title. diff --git a/docs/API-Reference.md b/docs/API-Reference.md new file mode 100644 index 0000000..fbfd45d --- /dev/null +++ b/docs/API-Reference.md @@ -0,0 +1,7458 @@ + +# ScriptCraft API Reference + +Walter Higgins + +[walter.higgins@gmail.com][email] + +[email]: mailto:walter.higgins@gmail.com?subject=ScriptCraft_API_Reference + +## Table of Contents + * [Modules in Scriptcraft](#modules-in-scriptcraft) + * [Module Loading](#module-loading) + * [The plugins directory](#the-plugins-directory) + * [The modules directory](#the-modules-directory) + * [The lib directory](#the-lib-directory) + * [plugins sub-directories](#plugins-sub-directories) + * [Global variables](#global-variables) + * [__plugin variable](#__plugin-variable) + * [server variable](#server-variable) + * [self variable](#self-variable) + * [config variable](#config-variable) + * [events variable](#events-variable) + * [Module variables](#module-variables) + * [__filename variable](#__filename-variable) + * [__dirname variable](#__dirname-variable) + * [Global functions](#global-functions) + * [echo function](#echo-function) + * [require() function](#require-function) + * [scload() function](#scload-function) + * [scsave() function](#scsave-function) + * [plugin() function](#plugin-function) + * [command() function](#command-function) + * [setTimeout() function](#settimeout-function) + * [clearTimeout() function](#cleartimeout-function) + * [setInterval() function](#setinterval-function) + * [clearInterval() function](#clearinterval-function) + * [refresh() function](#refresh-function) + * [addUnloadHandler() function](#addunloadhandler-function) + * [isOp() function](#isop-function) + * [require - Node.js-style module loading in ScriptCraft](#require---nodejs-style-module-loading-in-scriptcraft) + * [math.js](#mathjs) + * [inc.js](#incjs) + * [program.js](#programjs) + * [Important](#important) + * [module name resolution](#module-name-resolution) + * [events Module](#events-module) + * [events.on() static method](#eventson-static-method) + * [Events Helper Module (CanaryMod version)](#events-helper-module-canarymod-version) + * [Usage](#usage) + * [events.minecartActivate()](#eventsminecartactivate) + * [events.villagerTradeUnlock()](#eventsvillagertradeunlock) + * [events.mobTarget()](#eventsmobtarget) + * [events.chickenLayEgg()](#eventschickenlayegg) + * [events.potionEffectFinish()](#eventspotioneffectfinish) + * [events.entityMove()](#eventsentitymove) + * [events.hangingEntityDestroy()](#eventshangingentitydestroy) + * [events.vehicleCollision()](#eventsvehiclecollision) + * [events.potionEffectApplied()](#eventspotioneffectapplied) + * [events.vehicleDestroy()](#eventsvehicledestroy) + * [events.vehicleEnter()](#eventsvehicleenter) + * [events.damage()](#eventsdamage) + * [events.entityMount()](#eventsentitymount) + * [events.slimeSplit()](#eventsslimesplit) + * [events.endermanDropBlock()](#eventsendermandropblock) + * [events.itemTouchGround()](#eventsitemtouchground) + * [events.entitySpawn()](#eventsentityspawn) + * [events.endermanPickupBlock()](#eventsendermanpickupblock) + * [events.vehicleDamage()](#eventsvehicledamage) + * [events.entityLightningStruck()](#eventsentitylightningstruck) + * [events.entityDespawn()](#eventsentitydespawn) + * [events.vehicleMove()](#eventsvehiclemove) + * [events.projectileHit()](#eventsprojectilehit) + * [events.entityDeath()](#eventsentitydeath) + * [events.entityTame()](#eventsentitytame) + * [events.vehicleExit()](#eventsvehicleexit) + * [events.dimensionSwitch()](#eventsdimensionswitch) + * [events.foodLevel()](#eventsfoodlevel) + * [events.bookEdit()](#eventsbookedit) + * [events.eat()](#eventseat) + * [events.playerList()](#eventsplayerlist) + * [events.playerIdle()](#eventsplayeridle) + * [events.enchant()](#eventsenchant) + * [events.playerArmSwing()](#eventsplayerarmswing) + * [events.teleport()](#eventsteleport) + * [events.anvilUse()](#eventsanviluse) + * [events.portalUse()](#eventsportaluse) + * [events.foodSaturation()](#eventsfoodsaturation) + * [events.connection()](#eventsconnection) + * [events.playerRespawned()](#eventsplayerrespawned) + * [events.armorBroken()](#eventsarmorbroken) + * [events.levelUp()](#eventslevelup) + * [events.blockRightClick()](#eventsblockrightclick) + * [events.itemDrop()](#eventsitemdrop) + * [events.itemFrameRotate()](#eventsitemframerotate) + * [events.playerRespawning()](#eventsplayerrespawning) + * [events.craft()](#eventscraft) + * [events.experience()](#eventsexperience) + * [events.signChange()](#eventssignchange) + * [events.healthChange()](#eventshealthchange) + * [events.disconnection()](#eventsdisconnection) + * [events.gameModeChange()](#eventsgamemodechange) + * [events.preConnection()](#eventspreconnection) + * [events.villagerTrade()](#eventsvillagertrade) + * [events.returnFromIdle()](#eventsreturnfromidle) + * [events.armorStandModify()](#eventsarmorstandmodify) + * [events.slotClick()](#eventsslotclick) + * [events.itemFrameSetItem()](#eventsitemframesetitem) + * [events.entityRightClick()](#eventsentityrightclick) + * [events.foodExhaustion()](#eventsfoodexhaustion) + * [events.chat()](#eventschat) + * [events.itemPickup()](#eventsitempickup) + * [events.bedExit()](#eventsbedexit) + * [events.blockPlace()](#eventsblockplace) + * [events.heldItemChange()](#eventshelditemchange) + * [events.toolBroken()](#eventstoolbroken) + * [events.kick()](#eventskick) + * [events.playerDeath()](#eventsplayerdeath) + * [events.blockLeftClick()](#eventsblockleftclick) + * [events.blockDestroy()](#eventsblockdestroy) + * [events.bedEnter()](#eventsbedenter) + * [events.signShow()](#eventssignshow) + * [events.inventory()](#eventsinventory) + * [events.playerMove()](#eventsplayermove) + * [events.itemUse()](#eventsitemuse) + * [events.ban()](#eventsban) + * [events.statGained()](#eventsstatgained) + * [events.smeltBegin()](#eventssmeltbegin) + * [events.treeGrow()](#eventstreegrow) + * [events.chunkCreated()](#eventschunkcreated) + * [events.liquidDestroy()](#eventsliquiddestroy) + * [events.chunkLoaded()](#eventschunkloaded) + * [events.pistonRetract()](#eventspistonretract) + * [events.smelt()](#eventssmelt) + * [events.blockUpdate()](#eventsblockupdate) + * [events.portalDestroy()](#eventsportaldestroy) + * [events.ignition()](#eventsignition) + * [events.redstoneChange()](#eventsredstonechange) + * [events.weatherChange()](#eventsweatherchange) + * [events.chunkCreation()](#eventschunkcreation) + * [events.hopperTransfer()](#eventshoppertransfer) + * [events.chunkUnload()](#eventschunkunload) + * [events.blockGrow()](#eventsblockgrow) + * [events.dispense()](#eventsdispense) + * [events.blockDropXp()](#eventsblockdropxp) + * [events.fireworkExplode()](#eventsfireworkexplode) + * [events.leafDecay()](#eventsleafdecay) + * [events.pistonExtend()](#eventspistonextend) + * [events.noteBlockPlay()](#eventsnoteblockplay) + * [events.lightningStrike()](#eventslightningstrike) + * [events.decorate()](#eventsdecorate) + * [events.explosion()](#eventsexplosion) + * [events.tNTActivate()](#eventstntactivate) + * [events.timeChange()](#eventstimechange) + * [events.flow()](#eventsflow) + * [events.portalCreate()](#eventsportalcreate) + * [events.blockPhysics()](#eventsblockphysics) + * [events.playerCommand()](#eventsplayercommand) + * [events.consoleCommand()](#eventsconsolecommand) + * [events.commandBlockCommand()](#eventscommandblockcommand) + * [events.loadWorld()](#eventsloadworld) + * [events.permissionCheck()](#eventspermissioncheck) + * [events.serverGuiStart()](#eventsserverguistart) + * [events.unloadWorld()](#eventsunloadworld) + * [events.pluginDisable()](#eventsplugindisable) + * [events.pluginEnable()](#eventspluginenable) + * [events.serverTick()](#eventsservertick) + * [events.serverListPing()](#eventsserverlistping) + * [events.serverShutdown()](#eventsservershutdown) + * [Events Helper Module (SpigotMC version)](#events-helper-module-spigotmc-version) + * [Usage](#usage-1) + * [events.blockFade()](#eventsblockfade) + * [events.blockFromTo()](#eventsblockfromto) + * [events.notePlay()](#eventsnoteplay) + * [events.blockPlace()](#eventsblockplace-1) + * [events.blockGrow()](#eventsblockgrow-1) + * [events.entityBlockForm()](#eventsentityblockform) + * [events.blockPistonExtend()](#eventsblockpistonextend) + * [events.blockPistonRetract()](#eventsblockpistonretract) + * [events.blockSpread()](#eventsblockspread) + * [events.blockBurn()](#eventsblockburn) + * [events.blockDamage()](#eventsblockdamage) + * [events.leavesDecay()](#eventsleavesdecay) + * [events.blockDispense()](#eventsblockdispense) + * [events.blockForm()](#eventsblockform) + * [events.blockMultiPlace()](#eventsblockmultiplace) + * [events.blockIgnite()](#eventsblockignite) + * [events.blockPhysics()](#eventsblockphysics-1) + * [events.blockRedstone()](#eventsblockredstone) + * [events.cauldronLevelChange()](#eventscauldronlevelchange) + * [events.blockCanBuild()](#eventsblockcanbuild) + * [events.signChange()](#eventssignchange-1) + * [events.blockExp()](#eventsblockexp) + * [events.blockExplode()](#eventsblockexplode) + * [events.blockBreak()](#eventsblockbreak) + * [events.prepareAnvil()](#eventsprepareanvil) + * [events.brew()](#eventsbrew) + * [events.inventoryClose()](#eventsinventoryclose) + * [events.inventoryDrag()](#eventsinventorydrag) + * [events.inventoryCreative()](#eventsinventorycreative) + * [events.prepareItemCraft()](#eventsprepareitemcraft) + * [events.craftItem()](#eventscraftitem) + * [events.inventoryClick()](#eventsinventoryclick) + * [events.brewingStandFuel()](#eventsbrewingstandfuel) + * [events.inventoryPickupItem()](#eventsinventorypickupitem) + * [events.furnaceExtract()](#eventsfurnaceextract) + * [events.inventory()](#eventsinventory-1) + * [events.furnaceBurn()](#eventsfurnaceburn) + * [events.furnaceSmelt()](#eventsfurnacesmelt) + * [events.inventoryOpen()](#eventsinventoryopen) + * [events.inventoryMoveItem()](#eventsinventorymoveitem) + * [events.hangingPlace()](#eventshangingplace) + * [events.hangingBreak()](#eventshangingbreak) + * [events.hangingBreakByEntity()](#eventshangingbreakbyentity) + * [events.entityToggleGlide()](#eventsentitytoggleglide) + * [events.itemSpawn()](#eventsitemspawn) + * [events.spawnerSpawn()](#eventsspawnerspawn) + * [events.foodLevelChange()](#eventsfoodlevelchange) + * [events.lingeringPotionSplash()](#eventslingeringpotionsplash) + * [events.entityShootBow()](#eventsentityshootbow) + * [events.entityTargetLivingEntity()](#eventsentitytargetlivingentity) + * [events.entityCombust()](#eventsentitycombust) + * [events.entityBreed()](#eventsentitybreed) + * [events.expBottle()](#eventsexpbottle) + * [events.slimeSplit()](#eventsslimesplit-1) + * [events.entityDeath()](#eventsentitydeath-1) + * [events.entityAirChange()](#eventsentityairchange) + * [events.projectileLaunch()](#eventsprojectilelaunch) + * [events.entityPortal()](#eventsentityportal) + * [events.horseJump()](#eventshorsejump) + * [events.fireworkExplode()](#eventsfireworkexplode-1) + * [events.entityExplode()](#eventsentityexplode) + * [events.itemMerge()](#eventsitemmerge) + * [events.entitySpawn()](#eventsentityspawn-1) + * [events.projectileHit()](#eventsprojectilehit-1) + * [events.creatureSpawn()](#eventscreaturespawn) + * [events.entityPortalExit()](#eventsentityportalexit) + * [events.entityTame()](#eventsentitytame-1) + * [events.entityPortalEnter()](#eventsentityportalenter) + * [events.playerLeashEntity()](#eventsplayerleashentity) + * [events.entityDamage()](#eventsentitydamage) + * [events.pigZap()](#eventspigzap) + * [events.entityCombustByEntity()](#eventsentitycombustbyentity) + * [events.entityChangeBlock()](#eventsentitychangeblock) + * [events.areaEffectCloudApply()](#eventsareaeffectcloudapply) + * [events.creeperPower()](#eventscreeperpower) + * [events.sheepDyeWool()](#eventssheepdyewool) + * [events.playerDeath()](#eventsplayerdeath-1) + * [events.villagerReplenishTrade()](#eventsvillagerreplenishtrade) + * [events.entityCombustByBlock()](#eventsentitycombustbyblock) + * [events.entityResurrect()](#eventsentityresurrect) + * [events.villagerAcquireTrade()](#eventsvillageracquiretrade) + * [events.enderDragonChangePhase()](#eventsenderdragonchangephase) + * [events.entityCreatePortal()](#eventsentitycreateportal) + * [events.sheepRegrowWool()](#eventssheepregrowwool) + * [events.entityRegainHealth()](#eventsentityregainhealth) + * [events.entityInteract()](#eventsentityinteract) + * [events.potionSplash()](#eventspotionsplash) + * [events.entityTarget()](#eventsentitytarget) + * [events.entityBreakDoor()](#eventsentitybreakdoor) + * [events.entityUnleash()](#eventsentityunleash) + * [events.entityDamageByBlock()](#eventsentitydamagebyblock) + * [events.entityTeleport()](#eventsentityteleport) + * [events.itemDespawn()](#eventsitemdespawn) + * [events.explosionPrime()](#eventsexplosionprime) + * [events.entityDamageByEntity()](#eventsentitydamagebyentity) + * [events.portalCreate()](#eventsportalcreate-1) + * [events.worldSave()](#eventsworldsave) + * [events.worldLoad()](#eventsworldload) + * [events.chunkLoad()](#eventschunkload) + * [events.chunkPopulate()](#eventschunkpopulate) + * [events.chunkUnload()](#eventschunkunload-1) + * [events.structureGrow()](#eventsstructuregrow) + * [events.worldInit()](#eventsworldinit) + * [events.spawnChange()](#eventsspawnchange) + * [events.worldUnload()](#eventsworldunload) + * [events.playerShearEntity()](#eventsplayershearentity) + * [events.playerVelocity()](#eventsplayervelocity) + * [events.playerBucketFill()](#eventsplayerbucketfill) + * [events.playerFish()](#eventsplayerfish) + * [events.playerGameModeChange()](#eventsplayergamemodechange) + * [events.playerCommandPreprocess()](#eventsplayercommandpreprocess) + * [events.playerInteract()](#eventsplayerinteract) + * [events.playerInteractAtEntity()](#eventsplayerinteractatentity) + * [events.playerPortal()](#eventsplayerportal) + * [events.playerPreLogin()](#eventsplayerprelogin) + * [events.playerMove()](#eventsplayermove-1) + * [events.playerQuit()](#eventsplayerquit) + * [events.playerToggleSneak()](#eventsplayertogglesneak) + * [events.playerPickupArrow()](#eventsplayerpickuparrow) + * [events.playerBedLeave()](#eventsplayerbedleave) + * [events.playerChangedMainHand()](#eventsplayerchangedmainhand) + * [events.playerEditBook()](#eventsplayereditbook) + * [events.playerAnimation()](#eventsplayeranimation) + * [events.playerResourcePackStatus()](#eventsplayerresourcepackstatus) + * [events.playerItemDamage()](#eventsplayeritemdamage) + * [events.playerBucketEmpty()](#eventsplayerbucketempty) + * [events.playerExpChange()](#eventsplayerexpchange) + * [events.asyncPlayerChat()](#eventsasyncplayerchat) + * [events.playerItemBreak()](#eventsplayeritembreak) + * [events.playerUnleashEntity()](#eventsplayerunleashentity) + * [events.playerRespawn()](#eventsplayerrespawn) + * [events.playerDropItem()](#eventsplayerdropitem) + * [events.playerItemHeld()](#eventsplayeritemheld) + * [events.playerTeleport()](#eventsplayerteleport) + * [events.playerInteractEntity()](#eventsplayerinteractentity) + * [events.playerEggThrow()](#eventsplayereggthrow) + * [events.playerChat()](#eventsplayerchat) + * [events.playerRegisterChannel()](#eventsplayerregisterchannel) + * [events.playerSwapHandItems()](#eventsplayerswaphanditems) + * [events.playerChangedWorld()](#eventsplayerchangedworld) + * [events.playerLevelChange()](#eventsplayerlevelchange) + * [events.playerPickupItem()](#eventsplayerpickupitem) + * [events.asyncPlayerPreLogin()](#eventsasyncplayerprelogin) + * [events.playerUnregisterChannel()](#eventsplayerunregisterchannel) + * [events.playerStatisticIncrement()](#eventsplayerstatisticincrement) + * [events.playerBedEnter()](#eventsplayerbedenter) + * [events.playerKick()](#eventsplayerkick) + * [events.playerToggleSprint()](#eventsplayertogglesprint) + * [events.playerAchievementAwarded()](#eventsplayerachievementawarded) + * [events.playerJoin()](#eventsplayerjoin) + * [events.playerToggleFlight()](#eventsplayertoggleflight) + * [events.playerArmorStandManipulate()](#eventsplayerarmorstandmanipulate) + * [events.playerLogin()](#eventsplayerlogin) + * [events.playerItemConsume()](#eventsplayeritemconsume) + * [events.playerChatTabComplete()](#eventsplayerchattabcomplete) + * [events.weatherChange()](#eventsweatherchange-1) + * [events.thunderChange()](#eventsthunderchange) + * [events.lightningStrike()](#eventslightningstrike-1) + * [events.vehicleMove()](#eventsvehiclemove-1) + * [events.vehicleDamage()](#eventsvehicledamage-1) + * [events.vehicleDestroy()](#eventsvehicledestroy-1) + * [events.vehicleUpdate()](#eventsvehicleupdate) + * [events.vehicleCreate()](#eventsvehiclecreate) + * [events.vehicleEntityCollision()](#eventsvehicleentitycollision) + * [events.vehicleBlockCollision()](#eventsvehicleblockcollision) + * [events.vehicleEnter()](#eventsvehicleenter-1) + * [events.vehicleExit()](#eventsvehicleexit-1) + * [events.prepareItemEnchant()](#eventsprepareitemenchant) + * [events.enchantItem()](#eventsenchantitem) + * [events.pluginDisable()](#eventsplugindisable-1) + * [events.serverListPing()](#eventsserverlistping-1) + * [events.serviceRegister()](#eventsserviceregister) + * [events.serverCommand()](#eventsservercommand) + * [events.remoteServerCommand()](#eventsremoteservercommand) + * [events.pluginEnable()](#eventspluginenable-1) + * [events.serviceUnregister()](#eventsserviceunregister) + * [events.tabComplete()](#eventstabcomplete) + * [events.mapInitialize()](#eventsmapinitialize) + * [console global variable](#console-global-variable) + * [Example](#example) + * [Using string substitutions](#using-string-substitutions) + * [Drone Plugin](#drone-plugin) + * [Constructing a Drone Object](#constructing-a-drone-object) + * [Drone.box() method](#dronebox-method) + * [Drone.box0() method](#dronebox0-method) + * [Drone.boxa() method](#droneboxa-method) + * [Chaining](#chaining) + * [Drone Properties](#drone-properties) + * [Extending Drone](#extending-drone) + * [Drone.extend() static method](#droneextend-static-method) + * [Drone Constants](#drone-constants) + * [Drone.times() Method](#dronetimes-method) + * [Drone.arc() method](#dronearc-method) + * [Drone.bed() method](#dronebed-method) + * [Drone.blocktype() method](#droneblocktype-method) + * [Copy & Paste using Drone](#copy--paste-using-drone) + * [Drone.copy() method](#dronecopy-method) + * [Drone.paste() method](#dronepaste-method) + * [Drone.cylinder() method](#dronecylinder-method) + * [Drone.cylinder0() method](#dronecylinder0-method) + * [Drone.door() method](#dronedoor-method) + * [Drone.door_iron() method](#dronedoor_iron-method) + * [Drone.door2() method](#dronedoor2-method) + * [Drone.door2_iron() method](#dronedoor2_iron-method) + * [Drone.firework() method](#dronefirework-method) + * [Drone.garden() method](#dronegarden-method) + * [Drone.ladder() method](#droneladder-method) + * [Drone Movement](#drone-movement) + * [Drone Positional Info](#drone-positional-info) + * [Drone Markers](#drone-markers) + * [Drone.prism() method](#droneprism-method) + * [Drone.prism0() method](#droneprism0-method) + * [Drone.rand() method](#dronerand-method) + * [Drone.wallsign() method](#dronewallsign-method) + * [Drone.signpost() method](#dronesignpost-method) + * [Drone.sign() method](#dronesign-method) + * [Drone.sphere() method](#dronesphere-method) + * [Drone.sphere0() method](#dronesphere0-method) + * [Drone.hemisphere() method](#dronehemisphere-method) + * [Drone.hemisphere0() method](#dronehemisphere0-method) + * [Drone.stairs() function](#dronestairs-function) + * [Drone Trees methods](#drone-trees-methods) + * [Drone.castle() method](#dronecastle-method) + * [Drone.chessboard() method](#dronechessboard-method) + * [Drone.cottage() method](#dronecottage-method) + * [Drone.cottage_road() method](#dronecottage_road-method) + * [Drone.dancefloor() method](#dronedancefloor-method) + * [Drone.fort() method](#dronefort-method) + * [Drone.hangtorch() method](#dronehangtorch-method) + * [Drone.lcdclock() method.](#dronelcdclock-method) + * [Drone.logojs() method](#dronelogojs-method) + * [Drone.maze() method](#dronemaze-method) + * [Drone.rainbow() method](#dronerainbow-method) + * [Drone.spiral_stairs() method](#dronespiral_stairs-method) + * [Drone.temple() method](#dronetemple-method) + * [The at Module](#the-at-module) + * [at() function](#at-function) + * [Blocks Module](#blocks-module) + * [Examples](#examples) + * [Fireworks Module](#fireworks-module) + * [Examples](#examples-1) + * [Classroom Plugin](#classroom-plugin) + * [jsp classroom command](#jsp-classroom-command) + * [classroom.allowScripting() function](#classroomallowscripting-function) + * [Inventory Module](#inventory-module) + * [Usage](#usage-2) + * [Asynchronous Input Module](#asynchronous-input-module) + * [Lightning module](#lightning-module) + * [Usage](#usage-3) + * [The recipes module](#the-recipes-module) + * [Example](#example-1) + * [Http Module](#http-module) + * [http.request() function](#httprequest-function) + * [sc-mqtt module](#sc-mqtt-module) + * [Usage](#usage-4) + * [Signs Module](#signs-module) + * [signs.menu() function](#signsmenu-function) + * [signs.getTargetedBy() function](#signsgettargetedby-function) + * [The slash Module](#the-slash-module) + * [The slash() function](#the-slash-function) + * [Sounds Module](#sounds-module) + * [Usage (Bukkit) :](#usage-bukkit-) + * [Spawn Module](#spawn-module) + * [Parameters](#parameters) + * [Example](#example-2) + * [Teleport Module](#teleport-module) + * [Parameters](#parameters-1) + * [Example](#example-3) + * [Utilities Module](#utilities-module) + * [utils.player() function](#utilsplayer-function) + * [utils.world( worldName ) function](#utilsworld-worldname--function) + * [utils.blockAt( Location ) function](#utilsblockat-location--function) + * [utils.locationToJSON() function](#utilslocationtojson-function) + * [utils.locationToString() function](#utilslocationtostring-function) + * [utils.locationFromJSON() function](#utilslocationfromjson-function) + * [utils.getPlayerPos() function](#utilsgetplayerpos-function) + * [utils.getMousePos() function](#utilsgetmousepos-function) + * [utils.foreach() function](#utilsforeach-function) + * [utils.nicely() function](#utilsnicely-function) + * [utils.time( world ) function](#utilstime-world--function) + * [utils.time24( world ) function](#utilstime24-world--function) + * [utils.find() function](#utilsfind-function) + * [utils.serverAddress() function](#utilsserveraddress-function) + * [utils.array() function](#utilsarray-function) + * [utils.players() function](#utilsplayers-function) + * [utils.playerNames() function](#utilsplayernames-function) + * [utils.stat() function](#utilsstat-function) + * [The watcher Module](#the-watcher-module) + * [watcher.watchFile() function](#watcherwatchfile-function) + * [watcher.watchDir() function](#watcherwatchdir-function) + * [watcher.unwatchFile() function](#watcherunwatchfile-function) + * [watcher.unwatchDir() function](#watcherunwatchdir-function) + * [Example Plugin #1 - A simple extension to Minecraft.](#example-plugin-1---a-simple-extension-to-minecraft) + * [Usage:](#usage-5) + * [Example Plugin #2 - Making extensions available for all players.](#example-plugin-2---making-extensions-available-for-all-players) + * [Usage:](#usage-6) + * [Example Plugin #3 - Limiting use of commands to operators only.](#example-plugin-3---limiting-use-of-commands-to-operators-only) + * [Usage:](#usage-7) + * [Example Plugin #4 - Using parameters in commands.](#example-plugin-4---using-parameters-in-commands) + * [Usage:](#usage-8) + * [Example Plugin #5 - Re-use - Using your own and others modules.](#example-plugin-5---re-use---using-your-own-and-others-modules) + * [Usage:](#usage-9) + * [Example Plugin #6 - Re-use - Using 'utils' to get Player objects.](#example-plugin-6---re-use---using-utils-to-get-player-objects) + * [Usage:](#usage-10) + * [Example Plugin #7 - Listening for events, Greet players when they join the game.](#example-plugin-7---listening-for-events-greet-players-when-they-join-the-game) + * [Arrows Plugin](#arrows-plugin) + * [Usage:](#usage-11) + * [Spawn Plugin](#spawn-plugin) + * [Usage](#usage-12) + * [alias Plugin](#alias-plugin) + * [Examples](#examples-2) + * [Commando Plugin](#commando-plugin) + * [Description](#description) + * [Example hi-command.js](#example-hi-commandjs) + * [Example - timeofday-command.js](#example---timeofday-commandjs) + * [Caveats](#caveats) + * [homes Plugin](#homes-plugin) + * [Basic options](#basic-options) + * [Social options](#social-options) + * [Administration options](#administration-options) + * [NumberGuess mini-game:](#numberguess-mini-game) + * [Description](#description-1) + * [Example](#example-4) + * [Cow Clicker Mini-Game](#cow-clicker-mini-game) + * [How to Play](#how-to-play) + * [Rules](#rules) + * [Gameplay Mechanics](#gameplay-mechanics) + * [Items module (SpigotMC version)](#items-module-spigotmc-version) + * [Usage](#usage-13) + * [Items module (CanaryMod version)](#items-module-canarymod-version) + * [Usage](#usage-14) + * [Entities module](#entities-module) + * [Usage](#usage-15) + +## Modules in Scriptcraft + +ScriptCraft has a simple module loading system. In ScriptCraft, files +and modules are in one-to-one correspondence. As an example, foo.js +loads the module circle.js in the same directory. +*ScriptCraft now uses the same module system as Node.js - see [Node.js Modules][njsmod] for more details.* + +[njsmod]: http://nodejs.org/api/modules.html + +The contents of foo.js: + +```javascript +var circle = require('./circle.js'); +console.log( 'The area of a circle of radius 4 is ' + + circle.area(4)); +``` + +The contents of circle.js: + +```javascript +var PI = Math.PI; +exports.area = function (r) { + return PI * r * r; +}; +exports.circumference = function (r) { + return 2 * PI * r; +}; +``` + +The module circle.js has exported the functions area() and +circumference(). To add functions and objects to the root of your +module, you can add them to the special exports object. + +Variables local to the module will be private, as though the module +was wrapped in a function. In this example the variable PI is private +to circle.js. + +If you want the root of your module's export to be a function (such as +a constructor) or if you want to export a complete object in one +assignment instead of building it one property at a time, assign it to +module.exports instead of exports. + +## Module Loading + +When the ScriptCraft Java plugin is first installed, a new +`scriptcraft` subdirectory is created. If your minecraft server +directory is called 'mcserver' then the new subdirectories will be ... + + * mcserver/scriptcraft/ + * mcserver/scriptcraft/plugins + * mcserver/scriptcraft/modules + * mcserver/scriptcraft/lib + +... The `plugins`, `modules` and `lib` directories each serve a different purpose. + +### The plugins directory + +At server startup the ScriptCraft Java plugin is loaded and begins +automatically loading and executing all of the modules (javascript +files with the extension `.js`) it finds in the `scriptcraft/plugins` +directory. All modules in the plugins directory are automatically +loaded into the `global` namespace. What this means is that anything a +module in the `plugins` directory exports becomes a global +variable. For example, if you have a module greeting.js in the plugins +directory.... + +```javascript +exports.greet = function(player) { + echo(player, 'Hello ' + player.name); +}; +``` + +... then `greet` becomes a global function and can be used at the +in-game (or server) command prompt like so... + + /js greet(self) + +... This differs from how modules (in NodeJS and commonJS +environments) normally work. If you want your module to be exported +globally, put it in the `plugins` directory. If you don't want your +module to be exported globally but only want it to be used by other +modules, then put it in the `modules` directory instead. If you've +used previous versions of ScriptCraft and have put your custom +javascript modules in the `js-plugins` directory, then put them in the +`scriptcraft/plugins` directory. To summarise, modules in this directory are ... + + * Automatically loaded and run at server startup. + * Anything exported by modules becomes a global variable. + +### The modules directory + +The module directory is where you should place your modules if you +don't want to export globally. In javascript, it's considered best +practice not to have too many global variables, so if you want to +develop modules for others to use, or want to develop more complex +mods then your modules should be placed in the `modules` directory. +*Modules in the `modules` directory are not automatically loaded at +startup*, instead, they are loaded and used by other modules/plugins +using the standard `require()` function. This is the key difference +between modules in the `plugins` directory and modules in the +`modules` directory. Modules in the `plugins` directory are +automatically loaded and exported in to the global namespace at server +startup, modules in the `modules` directory are not. + +### The lib directory + +Modules in the `lib` directory are for use by ScriptCraft and some +core functions for use by module and plugin developers are also +provided. The `lib` directory is for internal use by ScriptCraft. +Modules in this directory are not automatically loaded nor are they +globally exported. + +### plugins sub-directories + +As of December 24 2013, the `scriptcraft/plugins` directory has the following sub-directories... + + * drone - Contains the drone module and drone extensions. Drone was the first scriptcraft module. + * mini-games - Contains mini-games + * arrows - The arrows module - Changes the behaviour of Arrows: Explosive, Fireworks, Teleportation etc. + * signs - The signs module (includes example signs) - create interactive signs. + * chat - The chat plugin/module + * alias - The alias plugin/module - for creating custom aliases for commonly-used commands. + * home - The home module - for setting homes and visiting other homes. + +## Global variables + +There are a couple of special javascript variables available in ScriptCraft... + +### __plugin variable +The ScriptCraft JavaPlugin object. + +### server variable +The Minecraft Server object + +### self variable +The current player. (Note - this value should not be used in multi-threaded scripts or event-handling code - it's not thread-safe). This variable is only safe to use at the in-game prompt and should *never* be used in modules. For example you can use it here... + + /js console.log(self.name) + +... but not in any javascript module you create yourself or in any +event handling code. `self` is a temporary short-lived variable which +only exists in the context of the in-game or server command prompts. + +### config variable +ScriptCraft configuration - this object is loaded and saved at startup/shutdown. + +### events variable +The events object is used to add new event handlers to Minecraft. + +## Module variables +The following variables are available only within the context of Modules. (not available at in-game prompt). + +### __filename variable +The current file - this variable is only relevant from within the context of a Javascript module. + +### __dirname variable +The current directory - this variable is only relevant from within the context of a Javascript module. + +## Global functions + +ScripCraft provides some global functions which can be used by all plugins/modules... + +### echo function + +The `echo()` function displays a message on the in-game screen. + +#### Example + + /js echo( self, 'Hello World') + +For programmers familiar with Javascript web programming, an `alert` +function is also provided. `alert` works exactly the same as `echo` +e.g. `alert( self, 'Hello World')`. + +### require() function + +ScriptCraft's `require()` function is used to load modules. The `require()` function takes a module name as a parameter and will try to load the named module. + +#### Parameters + + * modulename - The name of the module to be loaded. Can be one of the following... + + - A relative file path (with or without `.js` suffix) + - An absolute file path (with or without `.js` suffix) + - A relative directory path (uses node.js rules for directories) + - An absolute directory path (uses node.js rules for directories) + - A name of the form `'events'` - in which case the `lib` directory and `modules` directories are searched for the module. + +#### Return + +require() will return the loaded module's exports. + +### scload() function + +#### No longer recommended for use by Plugin/Module developers (deprecated) + +scload() should only be used to load .json data. + +#### Parameters + + * filename - The name of the file to load. + * warnOnFileNotFound (optional - default: false) - warn if the file was not found. + +#### Returns + +scload() will return the result of the last statement evaluated in the file. + +#### Example + + scload("myFile.js"); // loads a javascript file and evaluates it. + + var myData = scload("myData.json"); // loads a javascript file and evaluates it - eval'd contents are returned. + +##### myData.json contents... + + { players: { + walterh: { + h: ["jsp home {1}"], + sunny:["time set 0", + "weather clear"] + } + } + } + +### scsave() function + +The scsave() function saves an in-memory javascript object to a +specified file. Under the hood, scsave() uses JSON (specifically +json2.js) to save the object. There will usually be no need to call +this function directly - If you want to have a javascript object +automatically loaded at startup and saved on shutdown then use the +`persist()` module. The `persist()` module uses scsave and scload +under the hood. Any in-memory object saved using the `scsave()` +function can later be restored using the `scload()` function. + +#### Parameters + + * objectToSave : The object you want to save. + * filename : The name of the file you want to save it to. + +#### Example + +```javascript +var myObject = { name: 'John Doe', + aliases: ['John Ray', 'John Mee'], + date_of_birth: '1982/01/31' }; +scsave(myObject, 'johndoe.json'); +``` + +##### johndoe.json contents... + + { "name": "John Doe", + "aliases": ["John Ray", "John Mee"], + "date_of_birth": "1982/01/31" + }; + +### plugin() function + +#### Update April 2015 +The `plugin()` function is deprecated. Please refer to [Anatomy of a +ScriptCraft Plugin][anatomy] for an up-to-date step-by-step guide to +creating a plugin which uses persistence (loading and saving data). + +#### Deprecated +The `plugin()` function should be used to declare a javascript module +whose state you want to have managed by ScriptCraft - that is - a +Module whose state will be loaded at start up and saved at shut down. +A plugin is just a regular javascript object whose state is managed by +ScriptCraft. The only member of the plugin which whose persistence is +managed by Scriptcraft is `store` - this special member will be +automatically saved at shutdown and loaded at startup by +ScriptCraft. This makes it easier to write plugins which need to +persist data. + +#### Parameters + + * pluginName (String) : The name of the plugin - this becomes a global variable. + * pluginDefinition (Object) : The various functions and members of the plugin object. + * isPersistent (boolean - optional) : Specifies whether or not the + plugin/object state should be loaded and saved by ScriptCraft. + +#### Example + +See chat/color.js for an example of a simple plugin - one which lets +players choose a default chat color. See also [Anatomy of a +ScriptCraft Plugin][anatomy]. + +[anatomy]: ./Anatomy-of-a-Plugin.md + +### command() function + +The `command()` function is used to expose javascript functions for use by non-operators (regular players). Only operators should be allowed use raw javascript using the `/js ` command because it is too powerful for use by regular players and can be easily abused. However, the `/jsp ` command lets you (the operator / server administrator / plugin author) safely expose javascript functions for use by players. + +#### Parameters + + * commandFunction: The named javascript function which will be invoked when the command is invoked by a player. The name of the function will be used as the command name so name this function accordingly. The callback function in turn takes 2 parameters... + + * params : An Array of type String - the list of parameters passed to the command. + * sender : The [CommandSender][bukcs] object that invoked the command (this is usually a Player object but can be a Block ([BlockCommandSender][bukbcs]). + + * options (Array|Function - optional) : An array of command options/parameters which the player can supply (It's useful to supply an array so that Tab-Completion works for the `/jsp ` commands. If a function is supplied instead of an array then the function will be invoked at TAB-completion time and should return an array of strings. + * intercepts (boolean - optional) : Indicates whether this command can intercept Tab-Completion of the `/jsp ` command - advanced usage - see alias/alias.js for example. + +#### Example + + // javascript code + function boo( params, sender) { + echo( sender, params[0] ); + } + command( boo ); + + # in-game execution + /jsp boo Hi! + > Hi! + +To use a callback for options (TAB-Completion) ... + + var utils = require('utils'); + function boo( params, sender ) { + var receiver = server.getPlayer( params[0] ); + if ( receiver ){ + echo( receiver, sender.name + ' says boo!'); + } + } + command( boo, utils.playerNames ); + +See chat/colors.js or alias/alias.js or homes/homes.js for more examples of how to use the `command()` function. + +### setTimeout() function + +This function mimics the setTimeout() function used in browser-based javascript. However, the function will only accept a function reference, not a string of javascript code. Where setTimeout() in the browser returns a numeric value which can be subsequently passed to clearTimeout(), This implementation returns an object which can be subsequently passed to ScriptCraft's own clearTimeout() implementation. + +If Node.js supports setTimeout() then it's probably good for ScriptCraft to support it too. + +#### Example + +```javascript +// +// start a storm in 5 seconds +// +setTimeout( function() { + var world = server.worlds.get(0); + world.setStorm(true); +}, 5000); +``` + +### clearTimeout() function + +A scriptcraft implementation of clearTimeout(). + +### setInterval() function + +This function mimics the setInterval() function used in browser-based javascript. However, the function will only accept a function reference, not a string of javascript code. Where setInterval() in the browser returns a numeric value which can be subsequently passed to clearInterval(), This implementation returns an object which can be subsequently passed to ScriptCraft's own clearInterval() implementation. + +### clearInterval() function + +A scriptcraft implementation of clearInterval(). + +### refresh() function + +The refresh() function can be used to only reload the ScriptCraft plugin (it's like the `reload` command except it only reloads ScriptCraft). The refresh() function will ... + +1. Disable the ScriptCraft plugin. +2. Unload all event listeners associated with the ScriptCraft plugin. +3. Cancel all timed tasks (created by `setInterval` & `setTimeout`) +3. Enable the ScriptCraft plugin. + +... refresh() can be used during development to reload only scriptcraft javascript files. +See [issue #69][issue69] for more information. + +By default, if `self` is defined at runtime, it checks, whether `self` is server operator, otherwise fails with message. This behavivor can be modified using `skipOpCheck` parameter (useful, if you are doing some custom premission checks before calling this function). + +#### Parameters + + * skipOpCheck (boolean - optional) : If true, the function won't check if `self` is server operator. + +[issue69]: https://github.com/walterhiggins/ScriptCraft/issues/69 + +### addUnloadHandler() function + +The addUnloadHandler() function takes a callback function as a parameter. The callback will be called when the ScriptCraft plugin is unloaded (usually as a result of a a `reload` command or server shutdown). + +This function provides a way for ScriptCraft modules to do any required cleanup/housekeeping just prior to the ScriptCraft Plugin unloading. + +### isOp() function + +This function takes a single parameter and returns true if it's an operator or has operator-level privileges. + +## require - Node.js-style module loading in ScriptCraft + +Node.js is a server-side javascript environment with an excellent +module loading system based on CommonJS. Modules in Node.js are really +simple. Each module is in its own javascript file and all variables +and functions within the file are private to that file/module only. +There is a very concise explanation of CommonJS modules at... + +[http://wiki.commonjs.org/wiki/Modules/1.1.1.][cjsmodules] + +Node.js also has good documentation on [Modules][njsmod]. + +If you want to export a variable or function you use the module.export +property. + +For example imagine you have 3 files program.js, inc.js and math.js ... + +### math.js + +```javascript +exports.add = function(a,b){ + return a + b; +} +``` + +### inc.js + +```javascript +var math = require('./math'); +exports.increment = function(n){ + return math.add(n, 1); +} +``` + +### program.js + +```javascript +var inc = require('./inc').increment; +var a = 7; +a = inc(a); +print(a); +``` + +You can see from the above sample code that programs can use modules +and modules themeselves can use other modules. Modules have full +control over what functions and properties they want to provide to +others. + +### Important + +Although ScriptCraft now supports Node.js style modules, it does not +support node modules. Node.js and Rhino are two very different +Javascript environments. ScriptCraft uses Rhino Javascript, not +Node.js. Standard Node.js modules such as `'fs'` are not available in ScriptCraft. + +Modules can be loaded using relative or absolute paths. Per the CommonJS +module specification, the '.js' suffix is optional. + +[cjsmodules]: http://wiki.commonjs.org/wiki/Modules/1.1.1. + +### module name resolution + +When resolving module names to file paths, ScriptCraft uses the following rules... + + 1. if the module does not begin with './' or '/' then ... + + 1.1 Look in the 'scriptcraft/lib' directory. If it's not there then... + 1.2 Look in the 'scriptcraft/modules' directory. If it's not there then + Throw an Error. + + 2. If the module begins with './' or '/' then ... + + 2.1 if the module begins with './' then it's treated as a file path. File paths are + always relative to the module from which the require() call is being made. + + 2.2 If the module begins with '/' then it's treated as an absolute path. + + If the module does not have a '.js' suffix, and a file with the same name and a .js sufix exists, + then the file will be loaded. + + 3. If the module name resolves to a directory then... + + 3.1 look for a package.json file in the directory and load the `main` property e.g. + + // package.json located in './some-library/' + { + "main": './some-lib.js', + "name": 'some-library' + } + + 3.2 if no package.json file exists then look for an index.js file in the directory + +## events Module + +The Events module provides a thin wrapper around CanaryMod's or +Bukkit's Event-handling API. The Java-based CanaryMod and Bukkit +Events APIs make use of Java Annotations which are not available in +Javascript, so this module provides a simple way to listen to +minecraft events in javascript. + +### events.on() static method + +This method is used to register event listeners. This method is called by all of the Event Helper methods. +The `events` object has functions for registering listeners for each type of event. For example, you can register a block-break listener using events.on: + +```javascript +events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt, cancel ) { + echo(evt.player, evt.player.name + ' broke a block!'); +} ); +``` + +or you can (and probably should) use the more succinct: + +```javascript +events.blockDestroy( function( evt, cancel ) { + echo(evt.player, evt.player.name + ' broke a block!'); +} ); +``` + +The events.on method can be used to register standard CanaryMod/Bukkit +events and can also be used to register non-standard events - that is +- events provided by plugins. + +#### Parameters + + * eventType - A Java class. See the [CanaryMod Hook API][cmEvtApi] or [Bukkit Event API][buk] for details of the many event types. + + * callback - A function which will be called whenever the event + fires. The callback in turn takes 2 parameters: + + - event : the event fired + - cancel : a function which if invoked will cancel the event - not all event types are cancelable; this function only cancels cancelable events). + + * priority (optional - default: "CRITICAL" for CanaryMod or "HIGHEST" for Bukkit) - + The priority the listener/callback takes over other listeners to the same event. + Possible values for CanaryMod are "CRITICAL", "HIGH", "LOW", "NORMAL" and "PASSIVE". + For an explanation of what the different CanaryMod Hook priorities + mean, refer to CanaryMod's [Hook Priority class][cmPriority]. + Possible values for Bukkit are "HIGH", "HIGHEST", "LOW", "LOWEST", "NORMAL", "MONITOR". + For an explanation of what the different Bukkit Event priorities + mean, refer to bukkit's [Event API Reference][buk2]. + +#### Returns + +An object which can be used to unregister the listener. + +#### Example: + +The following code will print a message on screen every time a block is broken in the game + +```javascript +events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt, cancel ) { + echo(evt.player, evt.player.name + ' broke a block!'); +} ); +``` + +To handle an event only once and unregister from further events... + +```javascript +events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt, cancel ) { + echo( evt.player, evt.player.name + ' broke a block!'); + this.unregister(); +} ); +``` + +The `this` keyword when used inside the callback function refers to +the Listener object created by ScriptCraft. It has 2 methods +`unregister()` which can be used to stop listening and `cancel()` +which can be used to cancel the current event. The object returned by +`events.on()` only has the `unregister()` method, the `cancel()` +method is only available from within the event handling function. + +To unregister a listener *outside* of the listener function... + +```javascript +var myBlockBreakListener = events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt ) { ... } ); +... +myBlockBreakListener.unregister(); +``` + +[buk2]: http://wiki.bukkit.org/Event_API_Reference +[buk]: http://jd.bukkit.org/dev/apidocs/index.html?org/bukkit/event/Event.html +[cmEvtApi]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/Hook.html +[cmPriority]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/plugin/Priority.html + +## Events Helper Module (CanaryMod version) +The Events helper module provides a suite of functions - one for each possible event. +For example, the events.blockDestroy() function is just a wrapper function which calls events.on(net.canarymod.hook.player.BlockDestroyHook, callback, priority) +This module is a convenience wrapper for easily adding new event handling functions in Javascript. +At the in-game or server-console prompt, players/admins can type `events.` and use TAB completion +to choose from any of the approx. 160 different event types to listen to. + +### Usage + + events.blockDestroy( function( event ) { + echo( event.player, 'You broke a block!'); + }); + +The crucial difference is that the events module now has functions for each of the built-in events. The functions are accessible via TAB-completion so will help beginning programmers to explore the events at the server console window. + +### events.minecartActivate() + +#### Parameters + + * callback - A function which is called whenever the [entity.MinecartActivateHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/MinecartActivateHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.villagerTradeUnlock() + +#### Parameters + + * callback - A function which is called whenever the [entity.VillagerTradeUnlockHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/VillagerTradeUnlockHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.mobTarget() + +#### Parameters + + * callback - A function which is called whenever the [entity.MobTargetHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/MobTargetHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.chickenLayEgg() + +#### Parameters + + * callback - A function which is called whenever the [entity.ChickenLayEggHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/ChickenLayEggHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.potionEffectFinish() + +#### Parameters + + * callback - A function which is called whenever the [entity.PotionEffectFinishHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/PotionEffectFinishHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityMove() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityMoveHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/EntityMoveHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.hangingEntityDestroy() + +#### Parameters + + * callback - A function which is called whenever the [entity.HangingEntityDestroyHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/HangingEntityDestroyHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.vehicleCollision() + +#### Parameters + + * callback - A function which is called whenever the [entity.VehicleCollisionHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/VehicleCollisionHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.potionEffectApplied() + +#### Parameters + + * callback - A function which is called whenever the [entity.PotionEffectAppliedHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/PotionEffectAppliedHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.vehicleDestroy() + +#### Parameters + + * callback - A function which is called whenever the [entity.VehicleDestroyHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/VehicleDestroyHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.vehicleEnter() + +#### Parameters + + * callback - A function which is called whenever the [entity.VehicleEnterHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/VehicleEnterHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.damage() + +#### Parameters + + * callback - A function which is called whenever the [entity.DamageHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/DamageHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityMount() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityMountHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/EntityMountHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.slimeSplit() + +#### Parameters + + * callback - A function which is called whenever the [entity.SlimeSplitHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/SlimeSplitHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.endermanDropBlock() + +#### Parameters + + * callback - A function which is called whenever the [entity.EndermanDropBlockHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/EndermanDropBlockHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.itemTouchGround() + +#### Parameters + + * callback - A function which is called whenever the [entity.ItemTouchGroundHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/ItemTouchGroundHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entitySpawn() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntitySpawnHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/EntitySpawnHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.endermanPickupBlock() + +#### Parameters + + * callback - A function which is called whenever the [entity.EndermanPickupBlockHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/EndermanPickupBlockHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.vehicleDamage() + +#### Parameters + + * callback - A function which is called whenever the [entity.VehicleDamageHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/VehicleDamageHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityLightningStruck() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityLightningStruckHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/EntityLightningStruckHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityDespawn() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityDespawnHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/EntityDespawnHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.vehicleMove() + +#### Parameters + + * callback - A function which is called whenever the [entity.VehicleMoveHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/VehicleMoveHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.projectileHit() + +#### Parameters + + * callback - A function which is called whenever the [entity.ProjectileHitHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/ProjectileHitHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityDeath() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityDeathHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/EntityDeathHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityTame() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityTameHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/EntityTameHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.vehicleExit() + +#### Parameters + + * callback - A function which is called whenever the [entity.VehicleExitHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/VehicleExitHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.dimensionSwitch() + +#### Parameters + + * callback - A function which is called whenever the [entity.DimensionSwitchHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/entity/DimensionSwitchHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.foodLevel() + +#### Parameters + + * callback - A function which is called whenever the [player.FoodLevelHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/FoodLevelHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.bookEdit() + +#### Parameters + + * callback - A function which is called whenever the [player.BookEditHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/BookEditHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.eat() + +#### Parameters + + * callback - A function which is called whenever the [player.EatHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/EatHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerList() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerListHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/PlayerListHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerIdle() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerIdleHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/PlayerIdleHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.enchant() + +#### Parameters + + * callback - A function which is called whenever the [player.EnchantHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/EnchantHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerArmSwing() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerArmSwingHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/PlayerArmSwingHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.teleport() + +#### Parameters + + * callback - A function which is called whenever the [player.TeleportHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/TeleportHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.anvilUse() + +#### Parameters + + * callback - A function which is called whenever the [player.AnvilUseHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/AnvilUseHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.portalUse() + +#### Parameters + + * callback - A function which is called whenever the [player.PortalUseHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/PortalUseHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.foodSaturation() + +#### Parameters + + * callback - A function which is called whenever the [player.FoodSaturationHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/FoodSaturationHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.connection() + +#### Parameters + + * callback - A function which is called whenever the [player.ConnectionHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/ConnectionHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerRespawned() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerRespawnedHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/PlayerRespawnedHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.armorBroken() + +#### Parameters + + * callback - A function which is called whenever the [player.ArmorBrokenHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/ArmorBrokenHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.levelUp() + +#### Parameters + + * callback - A function which is called whenever the [player.LevelUpHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/LevelUpHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockRightClick() + +#### Parameters + + * callback - A function which is called whenever the [player.BlockRightClickHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/BlockRightClickHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.itemDrop() + +#### Parameters + + * callback - A function which is called whenever the [player.ItemDropHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/ItemDropHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.itemFrameRotate() + +#### Parameters + + * callback - A function which is called whenever the [player.ItemFrameRotateHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/ItemFrameRotateHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerRespawning() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerRespawningHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/PlayerRespawningHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.craft() + +#### Parameters + + * callback - A function which is called whenever the [player.CraftHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/CraftHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.experience() + +#### Parameters + + * callback - A function which is called whenever the [player.ExperienceHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/ExperienceHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.signChange() + +#### Parameters + + * callback - A function which is called whenever the [player.SignChangeHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/SignChangeHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.healthChange() + +#### Parameters + + * callback - A function which is called whenever the [player.HealthChangeHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/HealthChangeHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.disconnection() + +#### Parameters + + * callback - A function which is called whenever the [player.DisconnectionHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/DisconnectionHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.gameModeChange() + +#### Parameters + + * callback - A function which is called whenever the [player.GameModeChangeHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/GameModeChangeHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.preConnection() + +#### Parameters + + * callback - A function which is called whenever the [player.PreConnectionHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/PreConnectionHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.villagerTrade() + +#### Parameters + + * callback - A function which is called whenever the [player.VillagerTradeHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/VillagerTradeHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.returnFromIdle() + +#### Parameters + + * callback - A function which is called whenever the [player.ReturnFromIdleHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/ReturnFromIdleHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.armorStandModify() + +#### Parameters + + * callback - A function which is called whenever the [player.ArmorStandModifyHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/ArmorStandModifyHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.slotClick() + +#### Parameters + + * callback - A function which is called whenever the [player.SlotClickHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/SlotClickHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.itemFrameSetItem() + +#### Parameters + + * callback - A function which is called whenever the [player.ItemFrameSetItemHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/ItemFrameSetItemHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityRightClick() + +#### Parameters + + * callback - A function which is called whenever the [player.EntityRightClickHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/EntityRightClickHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.foodExhaustion() + +#### Parameters + + * callback - A function which is called whenever the [player.FoodExhaustionHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/FoodExhaustionHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.chat() + +#### Parameters + + * callback - A function which is called whenever the [player.ChatHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/ChatHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.itemPickup() + +#### Parameters + + * callback - A function which is called whenever the [player.ItemPickupHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/ItemPickupHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.bedExit() + +#### Parameters + + * callback - A function which is called whenever the [player.BedExitHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/BedExitHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockPlace() + +#### Parameters + + * callback - A function which is called whenever the [player.BlockPlaceHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/BlockPlaceHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.heldItemChange() + +#### Parameters + + * callback - A function which is called whenever the [player.HeldItemChangeHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/HeldItemChangeHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.toolBroken() + +#### Parameters + + * callback - A function which is called whenever the [player.ToolBrokenHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/ToolBrokenHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.kick() + +#### Parameters + + * callback - A function which is called whenever the [player.KickHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/KickHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerDeath() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerDeathHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/PlayerDeathHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockLeftClick() + +#### Parameters + + * callback - A function which is called whenever the [player.BlockLeftClickHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/BlockLeftClickHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockDestroy() + +#### Parameters + + * callback - A function which is called whenever the [player.BlockDestroyHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/BlockDestroyHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.bedEnter() + +#### Parameters + + * callback - A function which is called whenever the [player.BedEnterHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/BedEnterHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.signShow() + +#### Parameters + + * callback - A function which is called whenever the [player.SignShowHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/SignShowHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.inventory() + +#### Parameters + + * callback - A function which is called whenever the [player.InventoryHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/InventoryHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerMove() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerMoveHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/PlayerMoveHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.itemUse() + +#### Parameters + + * callback - A function which is called whenever the [player.ItemUseHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/ItemUseHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.ban() + +#### Parameters + + * callback - A function which is called whenever the [player.BanHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/BanHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.statGained() + +#### Parameters + + * callback - A function which is called whenever the [player.StatGainedHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/player/StatGainedHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.smeltBegin() + +#### Parameters + + * callback - A function which is called whenever the [world.SmeltBeginHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/SmeltBeginHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.treeGrow() + +#### Parameters + + * callback - A function which is called whenever the [world.TreeGrowHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/TreeGrowHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.chunkCreated() + +#### Parameters + + * callback - A function which is called whenever the [world.ChunkCreatedHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/ChunkCreatedHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.liquidDestroy() + +#### Parameters + + * callback - A function which is called whenever the [world.LiquidDestroyHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/LiquidDestroyHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.chunkLoaded() + +#### Parameters + + * callback - A function which is called whenever the [world.ChunkLoadedHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/ChunkLoadedHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.pistonRetract() + +#### Parameters + + * callback - A function which is called whenever the [world.PistonRetractHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/PistonRetractHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.smelt() + +#### Parameters + + * callback - A function which is called whenever the [world.SmeltHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/SmeltHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockUpdate() + +#### Parameters + + * callback - A function which is called whenever the [world.BlockUpdateHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/BlockUpdateHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.portalDestroy() + +#### Parameters + + * callback - A function which is called whenever the [world.PortalDestroyHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/PortalDestroyHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.ignition() + +#### Parameters + + * callback - A function which is called whenever the [world.IgnitionHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/IgnitionHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.redstoneChange() + +#### Parameters + + * callback - A function which is called whenever the [world.RedstoneChangeHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/RedstoneChangeHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.weatherChange() + +#### Parameters + + * callback - A function which is called whenever the [world.WeatherChangeHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/WeatherChangeHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.chunkCreation() + +#### Parameters + + * callback - A function which is called whenever the [world.ChunkCreationHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/ChunkCreationHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.hopperTransfer() + +#### Parameters + + * callback - A function which is called whenever the [world.HopperTransferHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/HopperTransferHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.chunkUnload() + +#### Parameters + + * callback - A function which is called whenever the [world.ChunkUnloadHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/ChunkUnloadHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockGrow() + +#### Parameters + + * callback - A function which is called whenever the [world.BlockGrowHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/BlockGrowHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.dispense() + +#### Parameters + + * callback - A function which is called whenever the [world.DispenseHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/DispenseHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockDropXp() + +#### Parameters + + * callback - A function which is called whenever the [world.BlockDropXpHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/BlockDropXpHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.fireworkExplode() + +#### Parameters + + * callback - A function which is called whenever the [world.FireworkExplodeHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/FireworkExplodeHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.leafDecay() + +#### Parameters + + * callback - A function which is called whenever the [world.LeafDecayHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/LeafDecayHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.pistonExtend() + +#### Parameters + + * callback - A function which is called whenever the [world.PistonExtendHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/PistonExtendHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.noteBlockPlay() + +#### Parameters + + * callback - A function which is called whenever the [world.NoteBlockPlayHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/NoteBlockPlayHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.lightningStrike() + +#### Parameters + + * callback - A function which is called whenever the [world.LightningStrikeHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/LightningStrikeHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.decorate() + +#### Parameters + + * callback - A function which is called whenever the [world.DecorateHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/DecorateHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.explosion() + +#### Parameters + + * callback - A function which is called whenever the [world.ExplosionHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/ExplosionHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.tNTActivate() + +#### Parameters + + * callback - A function which is called whenever the [world.TNTActivateHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/TNTActivateHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.timeChange() + +#### Parameters + + * callback - A function which is called whenever the [world.TimeChangeHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/TimeChangeHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.flow() + +#### Parameters + + * callback - A function which is called whenever the [world.FlowHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/FlowHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.portalCreate() + +#### Parameters + + * callback - A function which is called whenever the [world.PortalCreateHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/PortalCreateHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockPhysics() + +#### Parameters + + * callback - A function which is called whenever the [world.BlockPhysicsHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/world/BlockPhysicsHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerCommand() + +#### Parameters + + * callback - A function which is called whenever the [command.PlayerCommandHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/command/PlayerCommandHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.consoleCommand() + +#### Parameters + + * callback - A function which is called whenever the [command.ConsoleCommandHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/command/ConsoleCommandHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.commandBlockCommand() + +#### Parameters + + * callback - A function which is called whenever the [command.CommandBlockCommandHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/command/CommandBlockCommandHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.loadWorld() + +#### Parameters + + * callback - A function which is called whenever the [system.LoadWorldHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/system/LoadWorldHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.permissionCheck() + +#### Parameters + + * callback - A function which is called whenever the [system.PermissionCheckHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/system/PermissionCheckHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.serverGuiStart() + +#### Parameters + + * callback - A function which is called whenever the [system.ServerGuiStartHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/system/ServerGuiStartHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.unloadWorld() + +#### Parameters + + * callback - A function which is called whenever the [system.UnloadWorldHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/system/UnloadWorldHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.pluginDisable() + +#### Parameters + + * callback - A function which is called whenever the [system.PluginDisableHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/system/PluginDisableHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.pluginEnable() + +#### Parameters + + * callback - A function which is called whenever the [system.PluginEnableHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/system/PluginEnableHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.serverTick() + +#### Parameters + + * callback - A function which is called whenever the [system.ServerTickHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/system/ServerTickHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.serverListPing() + +#### Parameters + + * callback - A function which is called whenever the [system.ServerListPingHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/system/ServerListPingHook.html) is fired + + * priority - optional - see events.on() for more information. + +### events.serverShutdown() + +#### Parameters + + * callback - A function which is called whenever the [system.ServerShutdownHook event](https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/system/ServerShutdownHook.html) is fired + + * priority - optional - see events.on() for more information. + +## Events Helper Module (SpigotMC version) +The Events helper module provides a suite of functions - one for each possible event. +For example, the events.blockBreak() function is just a wrapper function which calls events.on(org.bukkit.event.block.BlockBreakEvent, callback, priority) +This module is a convenience wrapper for easily adding new event handling functions in Javascript. +At the in-game or server-console prompt, players/admins can type `events.` and use TAB completion +to choose from any of the approx. 160 different event types to listen to. + +### Usage + + events.blockBreak( function( event ) { + echo( event.player, 'You broke a block!'); + }); + +The crucial difference is that the events module now has functions for each of the built-in events. The functions are accessible via TAB-completion so will help beginning programmers to explore the events at the server console window. + +### events.blockFade() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockFadeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockFadeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockFromTo() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockFromToEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockFromToEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.notePlay() + +#### Parameters + + * callback - A function which is called whenever the [block.NotePlayEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/NotePlayEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockPlace() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockPlaceEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockPlaceEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockGrow() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockGrowEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockGrowEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityBlockForm() + +#### Parameters + + * callback - A function which is called whenever the [block.EntityBlockFormEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/EntityBlockFormEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockPistonExtend() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockPistonExtendEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockPistonExtendEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockPistonRetract() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockPistonRetractEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockPistonRetractEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockSpread() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockSpreadEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockSpreadEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockBurn() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockBurnEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockBurnEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockDamage() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockDamageEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockDamageEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.leavesDecay() + +#### Parameters + + * callback - A function which is called whenever the [block.LeavesDecayEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/LeavesDecayEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockDispense() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockDispenseEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockDispenseEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockForm() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockFormEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockFormEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockMultiPlace() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockMultiPlaceEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockMultiPlaceEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockIgnite() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockIgniteEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockIgniteEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockPhysics() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockPhysicsEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockPhysicsEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockRedstone() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockRedstoneEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockRedstoneEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.cauldronLevelChange() + +#### Parameters + + * callback - A function which is called whenever the [block.CauldronLevelChangeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/CauldronLevelChangeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockCanBuild() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockCanBuildEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockCanBuildEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.signChange() + +#### Parameters + + * callback - A function which is called whenever the [block.SignChangeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/SignChangeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockExp() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockExpEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockExpEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockExplode() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockExplodeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockExplodeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.blockBreak() + +#### Parameters + + * callback - A function which is called whenever the [block.BlockBreakEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockBreakEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.prepareAnvil() + +#### Parameters + + * callback - A function which is called whenever the [inventory.PrepareAnvilEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/PrepareAnvilEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.brew() + +#### Parameters + + * callback - A function which is called whenever the [inventory.BrewEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/BrewEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.inventoryClose() + +#### Parameters + + * callback - A function which is called whenever the [inventory.InventoryCloseEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/InventoryCloseEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.inventoryDrag() + +#### Parameters + + * callback - A function which is called whenever the [inventory.InventoryDragEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/InventoryDragEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.inventoryCreative() + +#### Parameters + + * callback - A function which is called whenever the [inventory.InventoryCreativeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/InventoryCreativeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.prepareItemCraft() + +#### Parameters + + * callback - A function which is called whenever the [inventory.PrepareItemCraftEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/PrepareItemCraftEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.craftItem() + +#### Parameters + + * callback - A function which is called whenever the [inventory.CraftItemEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/CraftItemEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.inventoryClick() + +#### Parameters + + * callback - A function which is called whenever the [inventory.InventoryClickEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/InventoryClickEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.brewingStandFuel() + +#### Parameters + + * callback - A function which is called whenever the [inventory.BrewingStandFuelEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/BrewingStandFuelEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.inventoryPickupItem() + +#### Parameters + + * callback - A function which is called whenever the [inventory.InventoryPickupItemEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/InventoryPickupItemEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.furnaceExtract() + +#### Parameters + + * callback - A function which is called whenever the [inventory.FurnaceExtractEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/FurnaceExtractEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.inventory() + +#### Parameters + + * callback - A function which is called whenever the [inventory.InventoryEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/InventoryEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.furnaceBurn() + +#### Parameters + + * callback - A function which is called whenever the [inventory.FurnaceBurnEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/FurnaceBurnEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.furnaceSmelt() + +#### Parameters + + * callback - A function which is called whenever the [inventory.FurnaceSmeltEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/FurnaceSmeltEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.inventoryOpen() + +#### Parameters + + * callback - A function which is called whenever the [inventory.InventoryOpenEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/InventoryOpenEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.inventoryMoveItem() + +#### Parameters + + * callback - A function which is called whenever the [inventory.InventoryMoveItemEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/inventory/InventoryMoveItemEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.hangingPlace() + +#### Parameters + + * callback - A function which is called whenever the [hanging.HangingPlaceEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/hanging/HangingPlaceEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.hangingBreak() + +#### Parameters + + * callback - A function which is called whenever the [hanging.HangingBreakEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/hanging/HangingBreakEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.hangingBreakByEntity() + +#### Parameters + + * callback - A function which is called whenever the [hanging.HangingBreakByEntityEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/hanging/HangingBreakByEntityEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityToggleGlide() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityToggleGlideEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityToggleGlideEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.itemSpawn() + +#### Parameters + + * callback - A function which is called whenever the [entity.ItemSpawnEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/ItemSpawnEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.spawnerSpawn() + +#### Parameters + + * callback - A function which is called whenever the [entity.SpawnerSpawnEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/SpawnerSpawnEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.foodLevelChange() + +#### Parameters + + * callback - A function which is called whenever the [entity.FoodLevelChangeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/FoodLevelChangeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.lingeringPotionSplash() + +#### Parameters + + * callback - A function which is called whenever the [entity.LingeringPotionSplashEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/LingeringPotionSplashEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityShootBow() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityShootBowEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityShootBowEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityTargetLivingEntity() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityTargetLivingEntityEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityTargetLivingEntityEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityCombust() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityCombustEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityCombustEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityBreed() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityBreedEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityBreedEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.expBottle() + +#### Parameters + + * callback - A function which is called whenever the [entity.ExpBottleEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/ExpBottleEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.slimeSplit() + +#### Parameters + + * callback - A function which is called whenever the [entity.SlimeSplitEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/SlimeSplitEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityDeath() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityDeathEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityDeathEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityAirChange() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityAirChangeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityAirChangeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.projectileLaunch() + +#### Parameters + + * callback - A function which is called whenever the [entity.ProjectileLaunchEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/ProjectileLaunchEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityPortal() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityPortalEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityPortalEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.horseJump() + +#### Parameters + + * callback - A function which is called whenever the [entity.HorseJumpEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/HorseJumpEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.fireworkExplode() + +#### Parameters + + * callback - A function which is called whenever the [entity.FireworkExplodeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/FireworkExplodeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityExplode() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityExplodeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityExplodeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.itemMerge() + +#### Parameters + + * callback - A function which is called whenever the [entity.ItemMergeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/ItemMergeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entitySpawn() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntitySpawnEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntitySpawnEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.projectileHit() + +#### Parameters + + * callback - A function which is called whenever the [entity.ProjectileHitEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/ProjectileHitEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.creatureSpawn() + +#### Parameters + + * callback - A function which is called whenever the [entity.CreatureSpawnEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/CreatureSpawnEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityPortalExit() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityPortalExitEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityPortalExitEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityTame() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityTameEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityTameEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityPortalEnter() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityPortalEnterEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityPortalEnterEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerLeashEntity() + +#### Parameters + + * callback - A function which is called whenever the [entity.PlayerLeashEntityEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/PlayerLeashEntityEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityDamage() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityDamageEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityDamageEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.pigZap() + +#### Parameters + + * callback - A function which is called whenever the [entity.PigZapEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/PigZapEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityCombustByEntity() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityCombustByEntityEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityCombustByEntityEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityChangeBlock() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityChangeBlockEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityChangeBlockEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.areaEffectCloudApply() + +#### Parameters + + * callback - A function which is called whenever the [entity.AreaEffectCloudApplyEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/AreaEffectCloudApplyEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.creeperPower() + +#### Parameters + + * callback - A function which is called whenever the [entity.CreeperPowerEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/CreeperPowerEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.sheepDyeWool() + +#### Parameters + + * callback - A function which is called whenever the [entity.SheepDyeWoolEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/SheepDyeWoolEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerDeath() + +#### Parameters + + * callback - A function which is called whenever the [entity.PlayerDeathEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/PlayerDeathEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.villagerReplenishTrade() + +#### Parameters + + * callback - A function which is called whenever the [entity.VillagerReplenishTradeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/VillagerReplenishTradeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityCombustByBlock() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityCombustByBlockEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityCombustByBlockEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityResurrect() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityResurrectEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityResurrectEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.villagerAcquireTrade() + +#### Parameters + + * callback - A function which is called whenever the [entity.VillagerAcquireTradeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/VillagerAcquireTradeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.enderDragonChangePhase() + +#### Parameters + + * callback - A function which is called whenever the [entity.EnderDragonChangePhaseEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EnderDragonChangePhaseEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityCreatePortal() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityCreatePortalEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityCreatePortalEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.sheepRegrowWool() + +#### Parameters + + * callback - A function which is called whenever the [entity.SheepRegrowWoolEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/SheepRegrowWoolEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityRegainHealth() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityRegainHealthEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityRegainHealthEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityInteract() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityInteractEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityInteractEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.potionSplash() + +#### Parameters + + * callback - A function which is called whenever the [entity.PotionSplashEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/PotionSplashEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityTarget() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityTargetEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityTargetEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityBreakDoor() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityBreakDoorEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityBreakDoorEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityUnleash() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityUnleashEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityUnleashEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityDamageByBlock() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityDamageByBlockEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityDamageByBlockEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityTeleport() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityTeleportEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityTeleportEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.itemDespawn() + +#### Parameters + + * callback - A function which is called whenever the [entity.ItemDespawnEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/ItemDespawnEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.explosionPrime() + +#### Parameters + + * callback - A function which is called whenever the [entity.ExplosionPrimeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/ExplosionPrimeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.entityDamageByEntity() + +#### Parameters + + * callback - A function which is called whenever the [entity.EntityDamageByEntityEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityDamageByEntityEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.portalCreate() + +#### Parameters + + * callback - A function which is called whenever the [world.PortalCreateEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/world/PortalCreateEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.worldSave() + +#### Parameters + + * callback - A function which is called whenever the [world.WorldSaveEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/world/WorldSaveEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.worldLoad() + +#### Parameters + + * callback - A function which is called whenever the [world.WorldLoadEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/world/WorldLoadEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.chunkLoad() + +#### Parameters + + * callback - A function which is called whenever the [world.ChunkLoadEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/world/ChunkLoadEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.chunkPopulate() + +#### Parameters + + * callback - A function which is called whenever the [world.ChunkPopulateEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/world/ChunkPopulateEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.chunkUnload() + +#### Parameters + + * callback - A function which is called whenever the [world.ChunkUnloadEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/world/ChunkUnloadEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.structureGrow() + +#### Parameters + + * callback - A function which is called whenever the [world.StructureGrowEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/world/StructureGrowEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.worldInit() + +#### Parameters + + * callback - A function which is called whenever the [world.WorldInitEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/world/WorldInitEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.spawnChange() + +#### Parameters + + * callback - A function which is called whenever the [world.SpawnChangeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/world/SpawnChangeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.worldUnload() + +#### Parameters + + * callback - A function which is called whenever the [world.WorldUnloadEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/world/WorldUnloadEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerShearEntity() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerShearEntityEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerShearEntityEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerVelocity() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerVelocityEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerVelocityEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerBucketFill() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerBucketFillEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerBucketFillEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerFish() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerFishEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerFishEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerGameModeChange() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerGameModeChangeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerGameModeChangeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerCommandPreprocess() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerCommandPreprocessEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerCommandPreprocessEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerInteract() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerInteractEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerInteractEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerInteractAtEntity() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerInteractAtEntityEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerInteractAtEntityEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerPortal() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerPortalEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerPortalEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerPreLogin() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerPreLoginEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerPreLoginEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerMove() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerMoveEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerMoveEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerQuit() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerQuitEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerQuitEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerToggleSneak() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerToggleSneakEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerToggleSneakEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerPickupArrow() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerPickupArrowEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerPickupArrowEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerBedLeave() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerBedLeaveEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerBedLeaveEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerChangedMainHand() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerChangedMainHandEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerChangedMainHandEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerEditBook() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerEditBookEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerEditBookEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerAnimation() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerAnimationEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerAnimationEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerResourcePackStatus() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerResourcePackStatusEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerResourcePackStatusEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerItemDamage() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerItemDamageEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerItemDamageEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerBucketEmpty() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerBucketEmptyEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerBucketEmptyEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerExpChange() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerExpChangeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerExpChangeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.asyncPlayerChat() + +#### Parameters + + * callback - A function which is called whenever the [player.AsyncPlayerChatEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/AsyncPlayerChatEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerItemBreak() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerItemBreakEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerItemBreakEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerUnleashEntity() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerUnleashEntityEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerUnleashEntityEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerRespawn() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerRespawnEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerRespawnEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerDropItem() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerDropItemEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerDropItemEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerItemHeld() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerItemHeldEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerItemHeldEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerTeleport() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerTeleportEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerTeleportEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerInteractEntity() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerInteractEntityEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerInteractEntityEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerEggThrow() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerEggThrowEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerEggThrowEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerChat() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerChatEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerChatEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerRegisterChannel() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerRegisterChannelEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerRegisterChannelEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerSwapHandItems() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerSwapHandItemsEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerSwapHandItemsEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerChangedWorld() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerChangedWorldEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerChangedWorldEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerLevelChange() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerLevelChangeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerLevelChangeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerPickupItem() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerPickupItemEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerPickupItemEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.asyncPlayerPreLogin() + +#### Parameters + + * callback - A function which is called whenever the [player.AsyncPlayerPreLoginEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/AsyncPlayerPreLoginEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerUnregisterChannel() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerUnregisterChannelEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerUnregisterChannelEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerStatisticIncrement() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerStatisticIncrementEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerStatisticIncrementEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerBedEnter() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerBedEnterEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerBedEnterEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerKick() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerKickEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerKickEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerToggleSprint() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerToggleSprintEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerToggleSprintEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerAchievementAwarded() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerAchievementAwardedEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerAchievementAwardedEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerJoin() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerJoinEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerJoinEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerToggleFlight() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerToggleFlightEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerToggleFlightEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerArmorStandManipulate() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerArmorStandManipulateEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerArmorStandManipulateEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerLogin() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerLoginEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerLoginEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerItemConsume() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerItemConsumeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerItemConsumeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.playerChatTabComplete() + +#### Parameters + + * callback - A function which is called whenever the [player.PlayerChatTabCompleteEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerChatTabCompleteEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.weatherChange() + +#### Parameters + + * callback - A function which is called whenever the [weather.WeatherChangeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/weather/WeatherChangeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.thunderChange() + +#### Parameters + + * callback - A function which is called whenever the [weather.ThunderChangeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/weather/ThunderChangeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.lightningStrike() + +#### Parameters + + * callback - A function which is called whenever the [weather.LightningStrikeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/weather/LightningStrikeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.vehicleMove() + +#### Parameters + + * callback - A function which is called whenever the [vehicle.VehicleMoveEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/vehicle/VehicleMoveEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.vehicleDamage() + +#### Parameters + + * callback - A function which is called whenever the [vehicle.VehicleDamageEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/vehicle/VehicleDamageEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.vehicleDestroy() + +#### Parameters + + * callback - A function which is called whenever the [vehicle.VehicleDestroyEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/vehicle/VehicleDestroyEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.vehicleUpdate() + +#### Parameters + + * callback - A function which is called whenever the [vehicle.VehicleUpdateEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/vehicle/VehicleUpdateEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.vehicleCreate() + +#### Parameters + + * callback - A function which is called whenever the [vehicle.VehicleCreateEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/vehicle/VehicleCreateEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.vehicleEntityCollision() + +#### Parameters + + * callback - A function which is called whenever the [vehicle.VehicleEntityCollisionEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/vehicle/VehicleEntityCollisionEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.vehicleBlockCollision() + +#### Parameters + + * callback - A function which is called whenever the [vehicle.VehicleBlockCollisionEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/vehicle/VehicleBlockCollisionEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.vehicleEnter() + +#### Parameters + + * callback - A function which is called whenever the [vehicle.VehicleEnterEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/vehicle/VehicleEnterEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.vehicleExit() + +#### Parameters + + * callback - A function which is called whenever the [vehicle.VehicleExitEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/vehicle/VehicleExitEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.prepareItemEnchant() + +#### Parameters + + * callback - A function which is called whenever the [enchantment.PrepareItemEnchantEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/enchantment/PrepareItemEnchantEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.enchantItem() + +#### Parameters + + * callback - A function which is called whenever the [enchantment.EnchantItemEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/enchantment/EnchantItemEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.pluginDisable() + +#### Parameters + + * callback - A function which is called whenever the [server.PluginDisableEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/server/PluginDisableEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.serverListPing() + +#### Parameters + + * callback - A function which is called whenever the [server.ServerListPingEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/server/ServerListPingEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.serviceRegister() + +#### Parameters + + * callback - A function which is called whenever the [server.ServiceRegisterEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/server/ServiceRegisterEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.serverCommand() + +#### Parameters + + * callback - A function which is called whenever the [server.ServerCommandEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/server/ServerCommandEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.remoteServerCommand() + +#### Parameters + + * callback - A function which is called whenever the [server.RemoteServerCommandEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/server/RemoteServerCommandEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.pluginEnable() + +#### Parameters + + * callback - A function which is called whenever the [server.PluginEnableEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/server/PluginEnableEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.serviceUnregister() + +#### Parameters + + * callback - A function which is called whenever the [server.ServiceUnregisterEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/server/ServiceUnregisterEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.tabComplete() + +#### Parameters + + * callback - A function which is called whenever the [server.TabCompleteEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/server/TabCompleteEvent.html) is fired + + * priority - optional - see events.on() for more information. + +### events.mapInitialize() + +#### Parameters + + * callback - A function which is called whenever the [server.MapInitializeEvent event](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/server/MapInitializeEvent.html) is fired + + * priority - optional - see events.on() for more information. + +## console global variable + +ScriptCraft provides a `console` global variable with the followng methods... + + * log() + * info() + * warn() + * error() + +The ScriptCraft console methods work like the [Web API implementation][webcons]. + +### Example + + console.log('Hello %s', 'world'); + +Basic variable substitution is supported (ScriptCraft's implementation +of console uses the Bukkit Plugin [Logger][lgr] or Canary Plugin [Logman][cmlgr] under the hood and +uses [java.lang.String.format()][strfmt] for variable +substitution. All output will be sent to the server console (not +in-game). + +### Using string substitutions + +ScriptCraft uses Java's [String.format()][strfmt] so any string substitution identifiers supported by +`java.lang.String.format()` are supported (e.g. %s , %d etc). + + for (var i=0; i<5; i++) { + console.log("Hello, %s. You've called me %d times.", "Bob", i+1); + } + +[lgr]: http://jd.bukkit.org/beta/apidocs/org/bukkit/plugin/PluginLogger.html +[cmlgr]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/logger/Logman.html +[strfmt]: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#format(java.lang.String, java.lang.Object...) +[webcons]: https://developer.mozilla.org/en-US/docs/Web/API/console + +## Drone Plugin + +The Drone is a convenience class for building. + +It uses a fluent interface which means all of the Drone's methods return `this` and can be chained together like so... + + var theDrone = new Drone(self); + theDrone.up().left().box(blocks.oak).down().fwd(3).cylinder0(blocks.lava,8); + +### Constructing a Drone Object + +Drones can be created in any of the following ways... + + 1. Calling any one of the methods listed below will return a Drone object. For example... + + var d = box( blocks.oak ) + + ... creates a 1x1x1 wooden block at the cross-hairs or player's location and returns a Drone object. This might look odd (if you're familiar with Java's Object-dot-method syntax) but all of the Drone class's methods are also global functions that return new Drone objects. This is short-hand for creating drones and is useful for playing around with Drones at the in-game command prompt. It's shorter than typing ... + + var d = new Drone(self).box( blocks.oak ) + + ... All of the Drone's methods return `this` so you can chain operations together like this... + + var d = box( blocks.oak ) + .up() + .box( blocks.oak ,3,1,3) + .down() + .fwd(2) + .box( blocks.oak ) + .turn() + .fwd(2) + .box( blocks.oak ) + .turn() + .fwd(2) + .box( blocks.oak ); + + 2. Using the following form... + + d = new Drone(self) + + ...will create a new Drone taking the current player as the parameter. If the player's cross-hairs are pointing at a block at the time then, that block's location becomes the drone's starting point. If the cross-hairs are _not_ pointing at a block, then the drone's starting location will be 2 blocks directly in front of the player. TIP: Building always happens right and front of the drone's position... + + Plan View: + + ^ + | + | + D----> + + For convenience you can use a _corner stone_ to begin building. The corner stone should be located just above ground level. If the cross-hair is point at or into ground level when you create a new Drone() with either a player or location given as a parameter, then building begins at the location the player was looking at or at the location. You can get around this by pointing at a 'corner stone' just above ground level or alternatively use the following statement... + + d = new Drone(self).up(); + + ... which will move the drone up one block as soon as it's created. + + ![corner stone](img/cornerstone1.png) + + 3. Or by using the following form... + + d = new Drone(x,y,z,direction,world); + + This will create a new Drone at the location you specified using x, y, z In minecraft, the X axis runs west to east and the Z axis runs north to south. The direction parameter says what direction you want the drone to face: 0 = east, 1 = south, 2 = west, 3 = north. If the direction parameter is omitted, the player's direction is used instead. Both the `direction` and `world` parameters are optional. + + 4. Create a new Drone based on a Location object... + + d = new Drone(location); + + This is useful when you want to create a drone at a given `org.bukkit.Location` . The `Location` class is used throughout the bukkit API. For example, if you want to create a drone when a block is broken at the block's location you would do so like this... + + events.blockBreak( function( event ) { + var location = event.block.location; + var drone = new Drone(location); + // do more stuff with the drone here... + }); + +#### Parameters + + * Player : If a player reference is given as the sole parameter then the block the player was looking at will be used as the starting point for the drone. If the player was not looking at a block then the player's location will be used as the starting point. If a `Player` object is provided as a paramter then it should be the only parameter. + * location : *NB* If a `Location` object is provided as a parameter, then it should be the only parameter. + * x : The x coordinate of the Drone (x,y,z,direction and world are not needed if either a player or location parameter is provided) + * y : The y coordinate of the Drone + * z : The z coordinate of the Drone + * direction : The direction in which the Drone is facing. Possible values are 0 (east), 1 (south), 2 (west) or 3 (north) + * world : The world in which the drone is created. + +### Drone.box() method + +the box() method is a convenience method for building things. (For the more performance-oriented method - see cuboid) + +#### parameters + + * b - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` + * w (optional - default 1) - the width of the structure + * h (optional - default 1) - the height of the structure + * d (optional - default 1) - the depth of the structure - NB this is not how deep underground the structure lies - this is how far away (depth of field) from the drone the structure will extend. + +#### Example + +To create a black structure 4 blocks wide, 9 blocks tall and 1 block long... + + box(blocks.wool.black, 4, 9, 1); + +... or the following code does the same but creates a variable that can be used for further methods... + + var drone = new Drone(self); + drone.box(blocks.wool.black, 4, 9, 1); + +![box example 1](img/boxex1.png) + +### Drone.box0() method + +Another convenience method - this one creates 4 walls with no floor or ceiling. + +#### Parameters + + * block - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` + * width (optional - default 1) - the width of the structure + * height (optional - default 1) - the height of the structure + * length (optional - default 1) - the length of the structure - how far + away (depth of field) from the drone the structure will extend. + +#### Example + +To create a stone building with the insided hollowed out 7 wide by 3 tall by 6 long... + + box0( blocks.stone, 7, 3, 6); + +![example box0](img/box0ex1.png) + +### Drone.boxa() method + +Construct a cuboid using an array of blocks. As the drone moves first along the width axis, then the height (y axis) then the length, each block is picked from the array and placed. + +#### Parameters + + * blocks - An array of blocks - each block in the array will be placed in turn. + * width + * height + * length + +#### Example + +Construct a rainbow-colored road 100 blocks long... + + var rainbowColors = [blocks.wool.red, blocks.wool.orange, blocks.wool.yellow, blocks.wool.lime, + blocks.wool.lightblue, blocks.wool.blue, blocks.wool.purple]; + + boxa(rainbowColors,7,1,30); + +![boxa example](img/boxaex1.png) + +### Chaining + +All of the Drone methods return a Drone object, which means methods can be 'chained' together so instead of writing this... + + drone = new Drone( self ); + drone.fwd( 3 ); + drone.left( 2 ); + drone.box( blocks.grass ); // create a grass block + drone.up(); + drone.box( blocks.grass ); // create another grass block + drone.down(); + +...you could simply write ... + + var drone = new Drone(self).fwd(3).left(2).box(blocks.grass).up().box(blocks.grass).down(); + +... since each Drone method is also a global function that constructs a drone if none is supplied, you can shorten even further to just... + + fwd(3).left(2).box(blocks.grass).up().box(blocks.grass).down() + +The Drone object uses a [Fluent Interface][fl] to make ScriptCraft scripts more concise and easier to write and read. Minecraft's in-game command prompt is limited to about 80 characters so chaining drone commands together means more can be done before hitting the command prompt limit. For complex building you should save your commands in a new script file and load it using /js load() + +[fl]: http://en.wikipedia.org/wiki/Fluent_interface + +### Drone Properties + + * x - The Drone's position along the west-east axis (x increases as you move east) + * y - The Drone's position along the vertical axis (y increses as you move up) + * z - The Drone's position along the north-south axis (z increases as you move south) + * dir - The Drone's direction 0 is east, 1 is south , 2 is west and 3 is north. + +### Extending Drone + +The Drone object can be easily extended - new buidling recipes/blueprints can be added and can become part of a Drone's chain using the *static* method `Drone.extend`. + +### Drone.extend() static method + +Use this method to add new methods (which also become chainable global functions) to the Drone object. + +#### Parameters + + * name - The name of the new method e.g. 'pyramid'. + * function - The method body. + +Alternatively if you provide just a function as a parameter, then the function name will be used as the new method name. For example the following two approaches are both valid. + +#### Example 1 Using name and function as parameters + + // submitted by [edonaldson][edonaldson] + var Drone = require('drone'); + Drone.extend('pyramid', function( block, height ) { + this.chkpt('pyramid'); + for ( var i = height; i > 0; i -= 2) { + this.box(block, i, 1, i).up().right().fwd(); + } + return this.move('pyramid'); + }); + +#### Example 2 Using just a named function as a parameter + + var Drone = require('drone'); + function pyramid( block, height ) { + this.chkpt('pyramid'); + for ( var i = height; i > 0; i -= 2) { + this.box(block, i, 1, i).up().right().fwd(); + } + return this.move('pyramid'); + } + Drone.extend( pyramid ); + +Once the method is defined (it can be defined in a new pyramid.js file) it can be used like so... + + var d = new Drone(self); + d.pyramid(blocks.brick.stone, 12); + +... or simply ... + + pyramid(blocks.brick.stone, 12); + +[edonaldson]: https://github.com/edonaldson + +### Drone Constants + +#### Drone.PLAYER_STAIRS_FACING + +An array which can be used when constructing stairs facing in the Drone's direction... + + var d = new Drone(self); + d.box(blocks.stairs.oak + ':' + Drone.PLAYER_STAIRS_FACING[d.dir]); + +... will construct a single oak stair block facing the drone. + +#### Drone.PLAYER_SIGN_FACING + +An array which can be used when placing signs so they face in a given direction. This is used internally by the Drone.sign() method. It should also be used for placing any of the following blocks... + + * chest + * ladder + * furnace + * dispenser + +By default, chests, dispensers, signs, ladders and furnaces are placed facing towards the drone so to place a chest facing the Drone just use: + + drone.box( blocks.chest ); + +To place a chest facing _away_ from the Drone: + + drone.box( blocks.chest + ':' + Drone.PLAYER_SIGN_FACING[(drone.dir + 2) % 4]); + +#### Drone.PLAYER_TORCH_FACING + +Used when placing torches. By default torches will be placed facing up. If you want to place a torch so that it faces towards the drone: + + drone.box( blocks.torch + ':' + Drone.PLAYER_TORCH_FACING[drone.dir]); + +If you want to place a torch so it faces _away_ from the drone: + + drone.box( blocks.torch + ':' + Drone.PLAYER_TORCH_FACING[(drone.dir + 2) % 4]); + +#### Drone.MAX_SIDE + +Specifies the maximum length (in any dimension) when calling the Drone.cuboidX (box) method. +The default value is 1,000 blocks. + +If you see an error message in the console `Build too big!` It's because the width, height or length paramete was greater than the Drone.MAX_SIDE value. + +#### Drone.MAX_VOLUME + +Specifies the maximum value for any call to Drone.cuboidX (box) method. +The default value is 1,000,000 (1 million) blocks. + +If the volume (width X height X length) of any single call to the Drone.cuboidX() method exceeds this value, you will see an error message in the console `Build too big!` . + +The values of both the `Drone.MAX_SiDE` and `Drone.MAX_VOLUME` variables _can_ be overridden but it's not recommended. + +### Drone.times() Method + +The `times()` method makes building multiple copies of buildings +easy. It's possible to create rows or grids of buildings without +resorting to `for` or `while` loops. + +#### Parameters + + * numTimes : The number of times you want to repeat the preceding statements. + +#### Limitation + +For now, don't use `times()` inside a Drone method implementation – only use it at the in-game prompt as a short-hand workaround for loops. + +#### Example + +Say you want to do the same thing over and over. You have a couple of options: + + * You can use a `for` loop … + + d = new Drone(); for ( var i = 0; i < 4; i++ ) { d.cottage().right(8); } + +While this will fit on the in-game prompt, it's awkward. You need to +declare a new Drone object first, then write a `for` loop to create the +4 cottages. It's also error prone – even the `for` loop is too much +syntax for what should really be simple. + + * You can use a `while` loop … + + d = new Drone(); var i=4; while (i--) { d.cottage().right(8); } + +… which is slightly shorter but still too much syntax. Each of the +above statements is fine for creating a 1-dimensional array of +structures. But what if you want to create a 2-dimensional or +3-dimensional array of structures? Enter the `times()` method. + +The `times()` method lets you repeat commands in a chain any number of +times. So to create 4 cottages in a row you would use the following +statement: + + cottage().right(8).times(4); + +… which will build a cottage, then move right 8 blocks, then do it +again 4 times over so that at the end you will have 4 cottages in a +row. What's more, the `times()` method can be called more than once in +a chain. So if you wanted to create a *grid* of 20 houses ( 4 x 5 ), +you would do so using the following statement: + + cottage().right(8).times(4).fwd(8).left(32).times(5); + +… breaking it down … + + 1. The first 3 calls in the chain ( `cottage()`, `right(8)`, `times(4)` ) build a single row of 4 cottages. + + 2. The last 3 calls in the chain ( `fwd(8)`, `left(32)`, `times(5)` ) move the drone forward 8 then left 32 blocks (4 x 8) to return to the original X coordinate, then everything in the chain is repeated again 5 times so that in the end, we have a grid of 20 cottages, 4 x 5. Normally this would require a nested loop but the `times()` method does away with the need for loops when repeating builds. + +Another example: This statement creates a row of trees 2 by 3: + + oak().right(10).times(2).left(20).fwd(10).times(3) + +… You can see the results below. + +![times example 1](img/times-trees.png) + +### Drone.arc() method + +The arc() method can be used to create 1 or more 90 degree arcs in the +horizontal or vertical planes. This method is called by cylinder() and +cylinder0() and the sphere() and sphere0() methods. + +#### Parameters + +arc() takes a single parameter - an object with the following named properties... + + * radius - The radius of the arc. + * blockType - The type of block to use - this is the block Id only (no meta). See [Data Values][dv]. + * meta - The metadata value. See [Data Values][dv]. + * orientation (default: 'horizontal' ) - the orientation of the arc - can be 'vertical' or 'horizontal'. + * stack (default: 1 ) - the height or length of the arc (depending on the orientation - if orientation is horizontal then this parameter refers to the height, if vertical then it refers to the length ). + * strokeWidth (default: 1 ) - the width of the stroke (how many blocks) - if drawing nested arcs it's usually a good idea to set strokeWidth to at least 2 so that there are no gaps between each arc. The arc method uses a [bresenham algorithm][bres] to plot points along the circumference. + * fill - If true (or present) then the arc will be filled in. + * quadrants (default: `{topleft:true,topright:true,bottomleft:true,bottomright:true}` - An object with 4 properties indicating which of the 4 quadrants of a circle to draw. If the quadrants property is absent then all 4 quadrants are drawn. + +#### Examples + +To draw a 1/4 circle (top right quadrant only) with a radius of 10 and +stroke width of 2 blocks ... + + arc({blockType: blocks.iron, + meta: 0, + radius: 10, + strokeWidth: 2, + quadrants: { topright: true }, + orientation: 'vertical', + stack: 1, + fill: false + } ); + +![arc example 1](img/arcex1.png) + +[bres]: http://en.wikipedia.org/wiki/Midpoint_circle_algorithm +[dv]: http://www.minecraftwiki.net/wiki/Data_values + +### Drone.bed() method + +Creates a bed. The foot of the bed will be at the drone's location and +the head of the bed will extend away from the drone. + +#### Example +To create a bed at the in-game prompt, look at a block then type: + +```javascript +/js bed() +``` + +Like most Drone methods, this returns the drone so it can be chained like so: + +```javascript +this + .fwd(3) + .bed() + .back(3) +``` +### Drone.blocktype() method + +Creates the text out of blocks. Useful for large-scale in-game signs. + +#### Parameters + + * message - The message to create - (use `\n` for newlines) + * foregroundBlock (default: black wool) - The block to use for the foreground + * backgroundBlock (default: none) - The block to use for the background + +#### Example + +To create a 2-line high message using glowstone... + + blocktype('Hello\nWorld', blocks.glowstone); + +![blocktype example][imgbt1] + +[imgbt1]: img/blocktype1.png + +### Copy & Paste using Drone + +A drone can be used to copy and paste areas of the game world. + +#### Deprecated +As of January 10 2015 the copy-paste functions in Drone are no longer +supported. Copy/Paste is: + +1. Difficult to do correctly in a way which works for both Minecraft 1.7 and 1.8 + due to how blocks changed in 1.8 +2. Not aligned with the purpose of ScriptCraft's Drone module which is to provide + a simple set of functions for scripting and in-game building. + +### Drone.copy() method + +Copies an area so it can be pasted elsewhere. The name can be used for +pasting the copied area elsewhere... + +#### Parameters + + * name - the name to be given to the copied area (used by `paste`) + * width - the width of the area to copy + * height - the height of the area to copy + * length - the length of the area (extending away from the drone) to copy + +#### Example + + drone.copy('somethingCool',10,5,10 ).right(12 ).paste('somethingCool' ); + +### Drone.paste() method + +Pastes a copied area to the current location. + +#### Example + +To copy a 10x5x10 area (using the drone's coordinates as the starting +point) into memory. the copied area can be referenced using the name +'somethingCool'. The drone moves 12 blocks right then pastes the copy. + + drone.copy('somethingCool',10,5,10 ) + .right(12 ) + .paste('somethingCool' ); + +### Drone.cylinder() method + +A convenience method for building cylinders. Building begins radius blocks to the right and forward. + +#### Parameters + + * block - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` + * radius + * height + +#### Example + +To create a cylinder of Iron 7 blocks in radius and 1 block high... + + cylinder(blocks.iron, 7 , 1); + +![cylinder example](img/cylinderex1.png) + +### Drone.cylinder0() method + +A version of cylinder that hollows out the middle. + +#### Example + +To create a hollow cylinder of Iron 7 blocks in radius and 1 block high... + + cylinder0(blocks.iron, 7, 1); + +![cylinder0 example](img/cylinder0ex1.png) + +### Drone.door() method + +create a door - if a parameter is supplied an Iron door is created otherwise a wooden door is created. + +#### Parameters + + * doorType (optional - default wood) - If a parameter is provided then the door is Iron. + +#### Example + +To create a wooden door at the crosshairs/drone's location... + + var drone = new Drone(self); + drone.door(); + +To create an iron door... + + drone.door( blocks.door_iron ); + +![iron door](img/doorex1.png) + +### Drone.door_iron() method + +create an Iron door. + +### Drone.door2() method + +Create double doors (left and right side) + +#### Parameters + + * doorType (optional - default wood) - If a parameter is provided then the door is Iron. + +#### Example + +To create double-doors at the cross-hairs/drone's location... + + drone.door2(); + +![double doors](img/door2ex1.png) + +### Drone.door2_iron() method + +Create double iron doors + +### Drone.firework() method + +Launches a firework at the drone's location. + +#### Example + +To launch a firework: + + var drone = new Drone(self); + drone.firework(); + +### Drone.garden() method + +places random flowers and long grass (similar to the effect of placing bonemeal on grass) + +#### Parameters + + * width - the width of the garden + * length - how far from the drone the garden extends + +#### Example + +To create a garden 10 blocks wide by 5 blocks long... + + garden(10,5); + +![garden example](img/gardenex1.png) + +### Drone.ladder() method + +Creates a ladder extending skyward. + +#### Parameters + + * height (optional - default 1) + +#### Example + +To create a ladder extending 10 blocks high: + + var drone = new Drone(self); + drone.ladder(10) + +At the in-game prompt, look at a block and then type: + + /js ladder(10) + +A ladder 10 blocks high will be created at the point you were looking at. + +#### Since +##### 3.0.3 +### Drone Movement + +Drones can move freely in minecraft's 3-D world. You control the +Drone's movement using any of the following methods.. + + * up() + * down() + * left() + * right() + * fwd() + * back() + * turn() + +... Each of these methods takes a single optional parameter +`numBlocks` - the number of blocks to move in the given direction. If +no parameter is given, the default is 1. + +To change direction use the `turn()` method which also takes a single +optional parameter (numTurns) - the number of 90 degree turns to +make. Turns are always clock-wise. If the drone is facing north, then +drone.turn() will make the turn face east. If the drone is facing east +then drone.turn(2) will make the drone turn twice so that it is facing +west. + +### Drone Positional Info + + * getLocation() - Returns a native Java Location object for the drone + +### Drone Markers + +Markers are useful when your Drone has to do a lot of work. You can +set a check-point and return to the check-point using the move() +method. If your drone is about to undertake a lot of work - +e.g. building a road, skyscraper or forest you should set a +check-point before doing so if you want your drone to return to its +current location. + +A 'start' checkpoint is automatically created when the Drone is first created. + +Markers are created and returned to using the followng two methods... + + * chkpt - Saves the drone's current location so it can be returned to later. + * move - moves the drone to a saved location. Alternatively you can provide a Java Location object or x,y,z and direction parameters. + +#### Parameters + + * name - the name of the checkpoint to save or return to. + +#### Example + + drone.chkpt('town-square'); + // + // the drone can now go off on a long excursion + // + for ( i = 0; i< 100; i++) { + drone.fwd(12).box(6); + } + // + // return to the point before the excursion + // + drone.move('town-square'); + +### Drone.prism() method + +Creates a prism. This is useful for roofs on houses. + +#### Parameters + + * block - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. + Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` + * width - the width of the prism + * length - the length of the prism (will be 2 time its height) + +#### Example + + prism(blocks.oak,3,12); + +![prism example](img/prismex1.png) + +### Drone.prism0() method + +A variation on `prism` which hollows out the inside of the prism. It +uses the same parameters as `prism`. + +### Drone.rand() method + +rand takes either an array (if each blockid has the same chance of occurring) or an object where each property is a blockid and the value is it's weight (an integer) + +#### Example + +place random blocks stone, mossy stone and cracked stone (each block has the same chance of being picked) + + rand( [blocks.brick.stone, blocks.brick.mossy, blocks.brick.cracked ],w,d,h) + +to place random blocks stone has a 50% chance of being picked, + + var distribution = {}; + distribution[ blocks.brick.stone ] = 5; + distribution[ blocks.brick.mossy ] = 3; + distribution[ blocks.brick.cracked ] = 2; + + rand( distribution, width, height, depth) + +regular stone has a 50% chance, mossy stone has a 30% chance and cracked stone has just a 20% chance of being picked. + +### Drone.wallsign() method + +Creates a wall sign (A sign attached to a wall) + +#### Parameters + + * message - can be a string or an array of strings + +#### Example + + drone.wallsign(['Welcome','to','Scriptopia']); + +![wall sign](img/signex2.png) + +### Drone.signpost() method + +Creates a free-standing signpost + +#### Parameters + + * message - can be a string or an array of strings + +#### Example + + drone.signpost(['Hello','World']); + +![ground sign](img/signex1.png) + +### Drone.sign() method + +Deprecated: Use signpost() or wallsign() methods instead. + +Signs must use block 63 (stand-alone signs) or 68 (signs on walls) + +#### Parameters + + * message - can be a string or an array of strings. + * block - can be 63 or 68 + +#### Example + +To create a free-standing sign... + + drone.sign(["Hello","World"], blocks.sign_post); + +![ground sign](img/signex1.png) + +... to create a wall mounted sign... + + drone.sign(["Welcome","to","Scriptopia"], blocks.sign ); + +![wall sign](img/signex2.png) + +### Drone.sphere() method + +Creates a sphere. + +#### Parameters + + * block - The block the sphere will be made of. + * radius - The radius of the sphere. + +#### Example + +To create a sphere of Iron with a radius of 10 blocks... + + sphere( blocks.iron, 10); + +![sphere example](img/sphereex1.png) + +Spheres are time-consuming to make. You *can* make large spheres (250 radius) but expect the +server to be very busy for a couple of minutes while doing so. + +### Drone.sphere0() method + +Creates an empty sphere. + +#### Parameters + + * block - The block the sphere will be made of. + * radius - The radius of the sphere. + +#### Example + +To create a sphere of Iron with a radius of 10 blocks... + + sphere0( blocks.iron, 10); + +Spheres are time-consuming to make. You *can* make large spheres (250 radius) but expect the +server to be very busy for a couple of minutes while doing so. + +### Drone.hemisphere() method + +Creates a hemisphere. Hemispheres can be either north or south. + +#### Parameters + + * block - the block the hemisphere will be made of. + * radius - the radius of the hemisphere + * northSouth - whether the hemisphere is 'north' or 'south' + +#### Example + +To create a wood 'north' hemisphere with a radius of 7 blocks... + + hemisphere(blocks.oak, 7, 'north'); + +![hemisphere example](img/hemisphereex1.png) + +### Drone.hemisphere0() method + +Creates a hollow hemisphere. Hemispheres can be either north or south. + +#### Parameters + + * block - the block the hemisphere will be made of. + * radius - the radius of the hemisphere + * northSouth - whether the hemisphere is 'north' or 'south' + +#### Example + +To create a glass 'north' hemisphere with a radius of 20 blocks... + + hemisphere0(blocks.glass, 20, 'north'); + +![hemisphere example](img/hemisphereex2.png) + +### Drone.stairs() function + +The stairs() function will build a flight of stairs + +#### Parameters + + * blockType - should be one of the following: + + * blocks.stairs.oak + * blocks.stairs.cobblestone + * blocks.stairs.brick + * blocks.stairs.stone + * blocks.stairs.nether + * blocks.stairs.sandstone + * blocks.stairs.spruce + * blocks.stairs.birch + * blocks.stairs.jungle + * blocks.stairs.quartz + + * width - The width of the staircase - default is 1 + * height - The height of the staircase - default is 1 + +#### Example + +To build an oak staircase 3 blocks wide and 5 blocks tall: + + /js stairs(blocks.stairs.oak, 3, 5) + +Staircases do not have any blocks beneath them. + +### Drone Trees methods + + * oak() + * spruce() + * birch() + * jungle() + +#### Example + +To create 4 trees in a row, point the cross-hairs at the ground then type `/js ` and ... + + up( ).oak( ).right(8 ).spruce( ).right(8 ).birch( ).right(8 ).jungle( ); + +Trees won't always generate unless the conditions are right. You +should use the tree methods when the drone is directly above the +ground. Trees will usually grow if the drone's current location is +occupied by Air and is directly above an area of grass (That is why +the `up()` method is called first). + +![tree example](img/treeex1.png) + +None of the tree methods require parameters. Tree methods will only be +successful if the tree is placed on grass in a setting where trees can +grow. + +### Drone.castle() method + +Creates a Castle. A castle is just a big wide fort with 4 taller forts at each corner. +See also Drone.fort() method. + +#### Parameters + + * side - How many blocks wide and long the castle will be (default: 24. Must be greater than 19) + * height - How tall the castle will be (default: 10. Must be geater than 7) + +#### Example + +At the in-game prompt you can create a castle by looking at a block and typing: + +```javascript +/js castle() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the castle() method. + +```javascript +var d = new Drone(player); +d.castle(); +``` +![castle example](img/castleex1.png) + +### Drone.chessboard() method + +Creates a tile pattern of given block types and size + +#### Parameters + + * whiteBlock - (optional: default blocks.wool.white) + * blackBlock - (optional: default blocks.wool.black) + * width - width of the chessboard + * length - length of the chessboard + +#### Example + +At the in-game prompt you can create a chessboard by looking at a block and typing: + +```javascript +/js chessboard() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the chessboard() method. + +```javascript +var d = new Drone(player); +d.chessboard(); +``` +![chessboard example](img/chessboardex1.png) + +### Drone.cottage() method + +Creates a simple but cosy dwelling. + +#### Example + +At the in-game prompt you can create a cottage by looking at a block and typing: + +```javascript +/js cottage() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the cottage() method. + +```javascript +var d = new Drone(player); +d.cottage(); +``` +![cottage example](img/cottageex1.png) + +### Drone.cottage_road() method + +Creates a tree-lined avenue with cottages on both sides. + +#### Parameters + + * numberOfCottages: The number of cottages to build in total (optional: default 6) + +#### Example + +At the in-game prompt you can create a cottage road by looking at a block and typing: + +```javascript +/js cottage_road() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the cottage_road() method. + +```javascript +var d = new Drone(player); +d.cottage_road(); +``` +![cottage_road example](img/cottageroadex1.png) + +### Drone.dancefloor() method +Create an animated dance floor of colored tiles some of which emit light. +The tiles change color every second creating a strobe-lit dance-floor effect. +See it in action here [http://www.youtube.com/watch?v=UEooBt6NTFo][ytdance] + +#### Parameters + + * width - how wide the dancefloor should be (optional: default 5) + * length - how long the dancefloor should be (optional: default 5) + * duration - the time duration for which the lights should change (optional: default 30 seconds) + +#### Example + +At the in-game prompt you can create a dancefloor by looking at a block and typing: + +```javascript +/js dancefloor() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the dancefloor() method. + +```javascript +var d = new Drone(player); +d.dancefloor(); +``` + +[ytdance]: http://www.youtube.com/watch?v=UEooBt6NTFo +![dancefloor example](img/dancefloorex1.png) +### Drone.fort() method + +Constructs a medieval fort. + +#### Parameters + + * side - How many blocks whide and long the fort will be (default: 18 . Must be greater than 9) + * height - How tall the fort will be (default: 6 . Must be greater than 3) + +#### Example + +At the in-game prompt you can create a fort by looking at a block and typing: + +```javascript +/js fort() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the fort() method. + +```javascript +var d = new Drone(player); +d.fort(); +``` +![fort example](img/fortex1.png) + +### Drone.hangtorch() method + +Adds a hanging torch to a wall. This method will try to hang a torch +against a wall. It will traverse backwards until it finds a block +adjacent to air and hang the torch. If it can't find a block next to +air it will log a message in the server. + +#### Example + +At the in-game prompt you can create a hanging torch by looking at a +block and typing: + +```javascript +/js hangtorch() +``` + +Alternatively you can create a new Drone object from a Player or +Location object and call the hangtorch() method. + +```javascript +var d = new Drone(player); +d.hangtorch(); +``` + +### Drone.lcdclock() method. + +Constructs a large LCD Clock. The clock will display the current time of day. +The clock can be stopped by calling the stopLCD() method of the Drone which created the clock. + +#### Parameters + + * foregroundBlock (Optional - default is blocks.glowstone) + * backgroundBlock (Optional - default is blocks.wool.black) + * borderBlock (Optional - a border around the LCD display - default none) + +#### Example + +At the in-game prompt you can create a LCD clock by looking at a block and typing: + +```javascript +/js var clock = lcdclock() +/js clock.stopLCD() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the lcdclock() method. + +```javascript +var d = new Drone(player); +d.lcdclock(); +d.stopLCD(); +``` +![lcdclock example](img/lcdclockex1.png) +### Drone.logojs() method + +Constructs a large Javascript Logo (black JS on Yellow background) +See: https://raw.github.com/voodootikigod/logo.js/master/js.png + +#### Parameters + + * foregroundBlock (Optional - default is blocks.wool.gray) + * backgroundBlock (Optional - default is blocks.gold) + +### Drone.maze() method + +Maze generation based on http://rosettacode.org/wiki/Maze_generation#JavaScript + +#### Parameters + + * width (optional - default 10) + * length (optional - default 10) + +#### Example + +At the in-game prompt you can create a maze by looking at a block and typing: + +```javascript +/js maze() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the maze() method. + +```javascript +var d = new Drone(player); +d.maze(); +``` +![maze example](img/mazeex1.png) + +### Drone.rainbow() method + +Creates a Rainbow. + +#### Parameters + + * radius (optional - default:18) - The radius of the rainbow + +#### Example + +At the in-game prompt you can create a rainbow by looking at a block and typing: +```javascript +/js rainbow() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the rainbow() method. + +```javascript +var d = new Drone(player); +d.rainbow(30); +``` + +![rainbow example](img/rainbowex1.png) + +### Drone.spiral_stairs() method + +Constructs a spiral staircase with slabs at each corner. + +#### Parameters + + * stairBlock - The block to use for stairs, should be one of the following... + - 'oak' + - 'spruce' + - 'birch' + - 'jungle' + - 'cobblestone' + - 'brick' + - 'stone' + - 'nether' + - 'sandstone' + - 'quartz' + * flights - The number of flights of stairs to build. + +![Spiral Staircase](img/spiralstair1.png) + +#### Example + +To construct a spiral staircase 5 floors high made of oak... + + spiral_stairs('oak', 5); + +### Drone.temple() method + +Constructs a mayan temple. + +#### Parameters + + * side - How many blocks wide and long the temple will be (default: 20) + +#### Example + +At the in-game prompt you can create a temple by looking at a block and typing: + +```javascript +/js temple() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the temple() method. + +```javascript +var d = new Drone(player); +d.temple(); +``` +![temple example](img/templeex1.png) + +## The at Module + +The at module provides a single function `at()` which can be used to schedule +repeating (or non-repeating) tasks to be done at a particular time. + +### at() function + +The utils.at() function will perform a given task at a given time in the +(minecraft) day. + +#### Parameters + + * time24hr : The time in 24hr form - e.g. 9:30 in the morning is '09:30' while + 9:30 pm is '21:30', midnight is '00:00' and midday is '12:00' + * callback : A javascript function which will be invoked at the given time. + * worlds : (optional) An array of worlds. Each world has its own clock. If no array of worlds is specified, all the server's worlds are used. + * repeat : (optional) true or false, default is true (repeat the task every day) + +#### Example + +To warn players when night is approaching: + +```javascript +var utils = require('utils'), + at = require('at'); +function warning(){ + utils.players(function( player ) { + echo( player, 'The night is dark and full of terrors!' ); + }); +} +at('19:00', warning); +``` +To run a task only once at the next given time: +```javascript +var utils = require('utils'), + at = require('at'); +function wakeup(){ + utils.players(function( player ) { + echo( player, "Wake Up Folks!" ); + }); +} +at('06:00', wakeup, null, false); +``` + +## Blocks Module + +You hate having to lookup [Data Values][dv] when you use ScriptCraft's +Drone() functions. So do I. So I created this blocks object which is +a helper object for use in construction. + +### Examples + + box( blocks.oak ); // creates a single oak wood block + box( blocks.sand, 3, 2, 1 ); // creates a block of sand 3 wide x 2 high x 1 long + box( blocks.wool.green, 2 ); // creates a block of green wool 2 blocks wide + +Color aliased properties that were a direct descendant of the blocks +object are no longer used to avoid confusion with carpet and stained +clay blocks. In addition, there's a convenience array `blocks.rainbow` +which is an array of the 7 colors of the rainbow (or closest +approximations). + +The blocks module is globally exported by the Drone module. + +## Fireworks Module + +The fireworks module makes it easy to create fireworks using +ScriptCraft. The module has a single function `firework` which takes +a `org.bukkit.Location` as its 1 and only parameter. + +### Examples + +The module also extends the `Drone` object adding a `firework` method +so that fireworks can be created as a part of a Drone chain. For +Example.... + + /js firework() + +... creates a single firework, while .... + + /js firework().fwd(3).times(5) + +... creates 5 fireworks in a row. Fireworks have also been added as a +possible option for the `arrow` module. To have a firework launch +where an arrow strikes... + + /js arrows.firework() + +To call the fireworks.firework() function directly, you must provide a +location. For example... + + /js var fireworks = require('fireworks'); + /js fireworks.firework( self.location ); + +![firework example](img/firework.png) + +## Classroom Plugin + +The `classroom` object contains a couple of utility functions for use +in a classroom setting. The goal of these functions is to make it +easier for tutors to facilitate ScriptCraft for use by students in a +classroom environment. Although granting ScriptCraft access to +students on a shared server is potentially risky (Students can +potentially abuse it), it is slighlty less risky than granting +operator privileges to each student. (Enterprising students will +quickly realise how to grant themselves and others operator privileges +once they have access to ScriptCraft). + +The goal of this module is not so much to enforce restrictions +(security or otherwise) but to make it easier for tutors to setup a +shared server so students can learn Javascript. When scripting is +turned on, every player who joins the server will have a dedicated +directory into which they can save scripts. All scripts in such +directories are automatically watched and loaded into a global +variable named after the player. + +So for example, if player 'walterh' joins the server, a `walterh` +global variable is created. If a file `greet.js` with the following +content is dropped into the `scriptcraft/players/walterh` +directory... + +```javascript +exports.hi = function( player ){ + echo( player, 'Hi ' + player.name); +}; +``` + +... then it can be invoked like this: `/js walterh.hi( self )` . This +lets every player/student create their own functions without having +naming collisions. + +It's strongly recommended that the +`scriptcraft/players/` directory is shared so that +others can connect to it and drop .js files into their student +directories. On Ubuntu, select the folder in Nautilus (the default +file browser) then right-click and choose *Sharing Options*, check the +*Share this folder* checkbox and the *Allow others to create and +delete files* and *Guest access* checkboxes. Click *Create Share* +button to close the sharing options dialog. Students can then access +the shared folder as follows... + + * Windows: Open Explorer, Go to \\{serverAddress}\players\ + * Macintosh: Open Finder, Go to smb://{serverAddress}/players/ + * Linux: Open Nautilus, Go to smb://{serverAddress}/players/ + +... where {serverAddress} is the ip address of the server (this is +displayed to whoever invokes the classroom.allowScripting() function.) + +### jsp classroom command +The `jsp classroom` command makes it easy for tutors to turn on or off +classroom mode. This command can only be used by server operators. To +turn on classroom mode (enable scripting for all players): + + jsp classroom on + +To turn off classroom mode (disable scripting for all players): + + jsp classroom off + +The `jsp classroom` command is provided as an easier way to turn on or +off classroom mode. This should be used in preference to the +classroom.allowScripting() function which is provided only for +programmatically enabling or disabling classroom mode. + +### classroom.allowScripting() function + +Allow or disallow anyone who connects to the server (or is already +connected) to use ScriptCraft. This function is preferable to granting 'ops' privileges +to every student in a Minecraft classroom environment. + +Whenever any file is added/edited or removed from any of the players/ +directories the contents are automatically reloaded. This is to +facilitate quick turnaround time for students getting to grips with +Javascript. + +#### Parameters + + * canScript : true or false + +#### Example + +To allow all players (and any players who connect to the server) to +use the `js` and `jsp` commands... + + /js classroom.allowScripting( true, self ) + +To disallow scripting (and prevent players who join the server from using the commands)... + + /js classroom.allowScripting( false, self ) + +Only ops users can run the classroom.allowScripting() function - this is so that students +don't try to bar themselves and each other from scripting. + +## Inventory Module +This module provides functions to add items to, remove items from and check the +contents of a player or NPC's inventory. + +### Usage +The inventory module is best used in conjunction with the items module. See below for examples of usage. + +```javascript +var inventory = require('inventory'); +var items = require('items'); +var utils = require('utils'); + +// gives every player a cookie and a baked potatoe +utils.players(function(player){ + inventory(player) + .add( items.cookie(1) ) + .add( items.bakedPotato(1) ) +}); + +// give a player 6 cookies then take away 4 of them + +inventory(player) + .add( items.cookie(6) ) + .remove ( items.cookie(4) ) + +// check if a player has any cookies + +var hasCookies = inventory(player).contains( items.cookie(1) ); + +``` +The inventory module exposes a single function which when passed a player or NPC will return an object with 3 methods: + +* add : Adds items to the inventory (Expects parameters of type `net.canarymod.api.inventory.Item` - I strongly recommend using the `items` module for constructing items) +* remove : removes items from the inventory (Expects parameters of type `net.canarymod.api.inventory.Item` - I strongly recommend using the `items` module for constructing items) +* contains : checks to see if there is the specified type and amount of item in the inventory (Expects parameters of type `net.canarymod.api.inventory.Item` - I strongly recommend using the `items` module for constructing items) + +## Asynchronous Input Module + +The `input` module provides a simple way to prompt players for input at the +in-game prompt. In Javascript browser environments the `prompt()` function provides +a way to block execution and ask the user for input. Execution is blocked until the user +provides input using the modal dialog and clicks OK. Unfortunately Minecraft provides no +equivalent modal dialog which can be used to gather player text input. The only way to gather text +input from the player in Minecraft is to do so asynchronously. That is - a prompt message can be +sent to the player but the player is not obliged to provide input immediately, nor does the program +execution block until the player does so. + +So ScriptCraft has no `prompt()` implementation because `prompt()` is a synchronous function and +Minecraft's API provides no equivalent functions or classes which can be used to implement this synchronously. +The Minecraft API does however have a 'Conversation' API which allows for prompting of the player and asynchronously gathering text input from the player. + +This new `input()` function is best illustrated by example. The following code is for a number-guessing game: + +```javascript +var input = require('input'); +exports.numberguess = function(player){ + var randomNumber = Math.ceil(Math.random() * 10); + input( player, 'Think of a number between 1 and 10 (q to quit)', function( guess, guesser, repeat ) { + if ( guess == 'q'){ + return; + } + if ( +guess !== randomNumber ) { + if (+guess < randomNumber ) { + echo( guesser, 'Too low - guess again'); + } + if (+guess > randomNumber ) { + echo( guesser, 'Too high - guess again'); + } + repeat(); + } else { + echo( guesser, 'You guessed correctly'); + } + }); +}; +``` + +The `input()` function takes 3 parameters, the player, a prompt message and a callback which will be invoked when the player has entered some text at the in-game command prompt. +The callback is bound to an object which has the following properties: + + * sender : The player who input the text + * value : The value of the text which has been input. + * message: The message prompt. + * repeat: A function which when invoked will repeat the original prompt. (this is for flow control) + +The callback function as well as being bound to an object with the above properties (so you can use this.value inside your callback to get the value which has just been input), can also take the following parameters (in exact order): + + * value + * sender + * repeat + +The `value` parameter will be the same as `this.value`, the `repeat` parameter will be the same as `this.repeat` and so on. + +## Lightning module + +Causes a bolt of lightning to strike. + +### Usage +```javascript +// strike lightning wherever a player's arrow lands +var lightning = require('lightning'); +events.projectileHit( function( event ){ + if ( entities.arrow( event.projectile ) // it's an arrow + && entities.player( event.projectile.owner ) // it was shot by a player + ) { + lightning( event.projectile ); // strike lightning at the arrow location + } +}); +``` + +## The recipes module + +The Recipes module provides convenience functions for adding and removing recipes +from the game. + +### Example +To add an EnderBow to the game (assumes there's an enchanted Item variable called enderBow)... + + var recipes = require('recipes'); + var items = require('items'); + ... + var enderBowRecipe = recipes.create( { + result: enderBow, + ingredients: { + E: items.enderPearl(1), + S: items.stick(1), + W: items.string(1) + }, + shape: [ 'ESW', + 'SEW', + 'ESW' ] + } ); + // add to server + var addedRecipe = server.addRecipe( enderBowRecipe ); + // to remove... + server.removeRemove( addedRecipe ); + +## Http Module + +For handling http requests. Not to be confused with the more robust +and functional 'http' module bundled with Node.js. + +### http.request() function + +The http.request() function will fetch a web address asynchronously (on a +separate thread)and pass the URL's response to a callback function +which will be executed synchronously (on the main thread). In this +way, http.request() can be used to fetch web content without blocking the +main thread of execution. + +#### Parameters + + * request: The request details either a plain URL e.g. "http://scriptcraft.js/sample.json" or an object with the following properties... + + - url: The URL of the request. + - method: Should be one of the standard HTTP methods, GET, POST, PUT, DELETE (defaults to GET). + - params: A Javascript object with name-value pairs. This is for supplying parameters to the server. + + * callback: The function to be called when the Web request has completed. This function takes the following parameters... + - responseCode: The numeric response code from the server. If the server did not respond with 200 OK then the response parameter will be undefined. + - response: A string (if the response is of type text) or object containing the HTTP response body. + +#### Example + +The following example illustrates how to use http.request to make a request to a JSON web service and evaluate its response... + +```javascript +var jsResponse; +var http = require('http'); +http.request('http://scriptcraftjs.org/sample.json',function(responseCode, responseBody){ + jsResponse = JSON.parse( responseBody ); +}); +``` +The following example illustrates a more complex use-case POSTing parameters to a CGI process on a server... + +```javascript +var http = require('http'); +http.request( { + url: 'http://pixenate.com/pixenate/pxn8.pl', + method: 'POST', + params: {script: '[]'} + }, + function( responseCode, responseBody ) { + var jsObj = JSON.parse( responseBody ); + }); +``` + +## sc-mqtt module + +This module provides a simple way to communicate with devices (such as Arduino) +using the popular lightweight [MQTT protocol][mqtt]. + +### Usage + +This module can only be used if the separate `sc-mqtt.jar` file is +present in the CraftBukkit classpath. To use this module, you should +... + + 1. Download sc-mqtt.jar from + 2. Save the file to the same directory where craftbukkit.jar resides. + 3. Create a new batch file (windows-only) called + craftbukkit-sc-mqtt.bat and edit it to include the following + command... + + ```sh + java -classpath sc-mqtt.jar;craftbukkit.jar org.bukkit.craftbukkit.Main + ``` + + If you're using Mac OS, create a new craftbukkit-sc-mqtt.command + file and edit it (using TextWrangler or another text editor) ... + + ```sh + java -classpath sc-mqtt.jar:craftbukkit.jar org.bukkit.craftbukkit.Main + ``` + + 4. Execute the craftbukkit-sc-mqtt batch file / command file to start + Craftbukkit. You can now begin using this module to send and receive + messages to/from a Net-enabled Arduino or any other device which uses + the [MQTT protocol][mqtt] + + ```javascript + var mqtt = require('sc-mqtt'); + // create a new client + var client = mqtt.client( 'tcp://localhost:1883', 'uniqueClientId' ); + // connect to the broker + client.connect( { keepAliveInterval: 15 } ); + // publish a message to the broker + client.publish( 'minecraft', 'loaded' ); + // subscribe to messages on 'arduino' topic + client.subscribe( 'arduino' ); + // do something when an incoming message arrives... + client.onMessageArrived( function( topic, message ) { + console.log( 'Message arrived: topic=' + topic + ', message=' + message ); + }); + + ``` + +The `sc-mqtt` module provides a very simple minimal wrapper around the +[Eclipse Paho MQTT Version 3 Client][pahodocs] java-based MQTT +library. + +[pahodocs]: http://pic.dhe.ibm.com/infocenter/wmqv7/v7r5/index.jsp?topic=/com.ibm.mq.javadoc.doc/WMQMQxrClasses/org/eclipse/paho/client/mqttv3/package-summary.html +[mqtt]: http://mqtt.org/ + +## Signs Module + +The Signs Module can be used by plugin authors to create interactive +signs - that is - signs which display a list of choices which can be +changed by interacting (right-clicking) with the sign. + +### signs.menu() function + +This function is used to construct a new interactive menu on top of an +existing sign in the game world. + +#### Parameters + + * Label : A string which will be displayed in the topmost line of the + sign. This label is not interactive. + * options : An array of strings which can be selected on the sign by + right-clicking/interacting. + * callback : A function which will be called whenever a player + interacts (changes selection) on a sign. This callback in turn + takes as its parameter, an object with the following properties... + + * player : The player who interacted with the sign. + * sign : The [org.bukkit.block.Sign][buksign] which the player interacted with. + * text : The text for the currently selected option on the sign. + * number : The index of the currently selected option on the sign. + + * selectedIndex : optional: A number (starting at 0) indicating which + of the options should be selected by default. 0 is the default. + +#### Returns +This function does not itself do much. It does however return a +function which when invoked with a given +[org.bukkit.block.Sign][buksign] object, will convert that sign into +an interactive sign. + +#### Example: Create a sign which changes the time of day. + +##### plugins/signs/time-of-day.js + +```javascript +var utils = require('utils'), + signs = require('signs'); + +var onTimeChoice = function(event){ + var selectedIndex = event.number; + // convert to Minecraft time 0 = Dawn, 6000 = midday, 12000 = dusk, 18000 = midnight + var time = selectedIndex * 6000; + event.player.location.world.setTime(time); +}; + +// signs.menu returns a function which can be called for one or more signs in the game. +var convertToTimeMenu = signs.menu('Time of Day', + ['Dawn', 'Midday', 'Dusk', 'Midnight'], + onTimeChoice); + +exports.time_sign = function( player ){ + var sign = signs.getTargetedBy(player); + if ( !sign ) { + throw new Error('You must look at a sign'); + } + convertToTimeMenu(sign); +}; +``` + +To use the above function at the in-game prompt, look at an existing +sign and type... + + /js time_sign(self); + +... and the sign you're looking at will become an interactive sign +which changes the time each time you interact (right-click) with it. + +### signs.getTargetedBy() function + +This function takes a [org.bukkit.entity.LivingEntity][bukle] as a +parameter and returns a [org.bukkit.block.Sign][buksign] object which +the entity has targeted. It is a utility function for use by plugin authors. + +#### Example + +```javascript +var signs = require('signs'), + utils = require('utils'); +var player = utils.player('tom1234'); +var sign = signs.getTargetedBy( player ); +if ( !sign ) { + echo( player, 'Not looking at a sign'); +} +``` + +[buksign]: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/block/Sign.html +[bukle]: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/LivingEntity.html + +## The slash Module + +This module provides a single function which makes it easy to execute +minecraft commands via javascript. + +### The slash() function + +This function makes it easy to execute one or more minecraft commands. + +#### Parameters + + * commands : A String or Array of strings - each string is a command to be executed. + * sender: (optional) The player on whose behalf the commands should be executed. If not specified the commands will be executed as the server console user. + +#### Examples + +Invoke the `/defaultgamemode creative` command (as server). + +```javascript +var slash = require('slash'); +slash('defaultgamemode creative'); +``` + +Set the time of day to Midday and toggle downfall (as player 'JohnDoe'): + +```javascript +var slash = require('slash'), + utils = require('utils'); +var johnDoe = utils.player('John_Doe'); + +slash([ + 'time set 6000', + 'toggledownfall' +], johnDoe); +``` + +## Sounds Module + +This module is a simple wrapper around the Bukkit Sound class and provides +a simpler way to play sounds. All of the org.bukkit.Sound Enum values are attached. + +### Usage (Bukkit) : + + var sounds = require('sounds'); + sounds.play( bukkit.sound.VILLAGER_NO , self, 1, 0); // plays VILLAGER_NO sound at full volume and medium pitch + sounds.play( bukkit.sound.VILLAGER_NO , self ); // same as previous statement + +The play() function takes either a Location object or any object which has a location. +The volume parameter is in the range 0 to 1 and the pitch parameter is in the range 0 to 4. + +In addition, a play function is provided for each possible sound using the following rules: + +1. The sound is converted from ALL_CAPS_UNDERSCORE to camelCase so for example there is a sounds.villagerNo() function which will play the VILLAGER_NO sound. +2. Each such function can take 3 parameters: location (which can be either an actual Location object or an object which has a location), volume and pitch +3. Or... each such function can be called without parameters meaning the sound will be played for all online players to hear. + + sounds.villagerNo(self, 1, 0); // plays VILLAGER_NO sound at full volume and medium pitch at invoker's location + + sounds.villagerNo(); // plays VILLAGER_NO sound for all players online. + +These methods are provided for convenience to help beginners explore sounds using TAB completion. +## Spawn Module + +Provides a single function to 'spawn' an entity at a given location. + +### Parameters + + * entityType - The type of entity to spawn. This can be a string (see entities module for reference) or a framework-specific object type (see https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/EntityType.html). A list of [all possible entities][ents] functions (equivalent to the EntityType enum). + + * location - where the entity should be spawned. + +[ents]: #entities-module + +### Example + +Using the entities module as a helper, spawn a new polar bear at the world's default spawn location: + +```javascript +var entities = require('entities'), + spawn = require('spawn'); +... +var spawnLocation = world.spawnLocation; +spawn(entities.polar_bear(), spawnLocation); +``` + +This module is in turn used by the Drone's `spawn()` method and the `jsp spawn` command. +String class extensions +----------------------- +The following chat-formatting methods are added to the javascript String class.. + + * aqua() + * black() + * blue() + * bold() + * brightgreen() + * darkaqua() + * darkblue() + * darkgray() + * darkgreen() + * purple() + * darkpurple() + * darkred() + * gold() + * gray() + * green() + * italic() + * lightpurple() + * indigo() + * green() + * red() + * pink() + * yellow() + * white() + * strike() + * random() + * magic() + * underline() + * reset() + +Example +------- + + /js var boldGoldText = "Hello World".bold().gold(); + /js echo(self, boldGoldText ); + +

Hello World

+ +## Teleport Module + +This module provides a function to teleport entities (Players or NPCs). + +### Parameters + + * entity - The player or NPC to be teleported. If of type String, then a player with that name will be teleported. + * destination - The location to which they should be teleported. If not of type Location but is a Player, Block or any + object which has a `location` property then that works too. If of type String, then it's assumed that the destination is the player with that name. + +### Example + +The following code will teleport each player back to their spawn position. + +```javascript +var teleport = require('teleport'), + utils = require('utils'), + players = utils.players(), + i = 0; +for ( ; i < players.length; i++ ) { + teleport( players[i], players[i].spawnPosition ); +} +``` + +The following code will teleport 'tom' to 'jane's location. + +```javascript +var teleport = require('teleport'); +teleport('tom' , 'jane'); +``` +## Utilities Module + +The `utils` module is a storehouse for various useful utility +functions which can be used by plugin and module authors. It contains +miscellaneous utility functions and classes to help with programming. + +### utils.player() function + +The utils.player() function will return a [Player][cmpl] object +with the given name. This function takes a single parameter +`playerName` which can be either a String or a [Player][cmpl] object - +if it's a Player object, then the same object is returned. If it's a +String, then it tries to find the player with that name. + +#### Parameters + + * playerName : A String or Player object. If no parameter is provided + then player() will try to return the `self` variable . It is + strongly recommended to provide a parameter. + +#### Example + +```javascript +var utils = require('utils'); +var name = 'walterh'; +var player = utils.player(name); +if ( player ) { + echo(player, 'Got ' + name); +} else { + console.log('No player named ' + name); +} +``` + +[bkpl]: http://jd.bukkit.org/dev/apidocs/org/bukkit/entity/Player.html +[cmpl]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/entity/living/humanoid/Player.html +[cmloc]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/world/position/Location.html +[bkloc]: http://jd.bukkit.org/dev/apidocs/org/bukkit/Location.html + +### utils.world( worldName ) function + +Returns a World object matching the given name + +### utils.blockAt( Location ) function + +Returns the Block at the given location. + +### utils.locationToJSON() function + +utils.locationToJSON() returns a [Location][cmloc] object in JSON form... + + { world: 'world5', + x: 56.9324, + y: 103.9954, + z: 43.1323, + yaw: 0.0, + pitch: 0.0 + } + +This can be useful if you write a plugin that needs to store location data since bukkit's Location object is a Java object which cannot be serialized to JSON by default. + +#### Parameters + + * location: An object of type [Location][cmloc] + +#### Returns + +A JSON object in the above form. + +### utils.locationToString() function + +The utils.locationToString() function returns a +[Location][cmloc] object in string form... + + '{"world":"world5",x:56.9324,y:103.9954,z:43.1323,yaw:0.0,pitch:0.0}' + +... which can be useful if you write a plugin which uses Locations as +keys in a lookup table. + +#### Example + +```javascript +var utils = require('utils'); +... +var key = utils.locationToString(player.location); +lookupTable[key] = player.name; +``` + +### utils.locationFromJSON() function + +This function reconstructs an [Location][cmloc] object from +a JSON representation. This is the counterpart to the +`locationToJSON()` function. It takes a JSON object of the form +returned by locationToJSON() and reconstructs and returns a bukkit +Location object. + +### utils.getPlayerPos() function + +This function returns the player's [Location][cmloc] (x, y, z, pitch +and yaw) for a named player. If the "player" is in fact a +[BlockCommand][bkbcs] then the attached Block's location is returned. + +#### Parameters + + * player : A [org.bukkit.command.CommandSender][bkbcs] (Player or BlockCommandSender) or player name (String). + +#### Returns + +A [Location][cmloc] object. + +[bkbcs]: http://jd.bukkit.org/dev/apidocs/org/bukkit/command/BlockCommandSender.html +[bksndr]: http://jd.bukkit.org/dev/apidocs/index.html?org/bukkit/command/CommandSender.html +### utils.getMousePos() function + +This function returns a [Location][cmloc] object (the +x,y,z) of the current block being targeted by the named player. This +is the location of the block the player is looking at (targeting). + +#### Parameters + + * player : The player whose targeted location you wish to get. + +#### Example + +The following code will strike lightning at the location the player is looking at... + +```javascript +var utils = require('utils'); +var playerName = 'walterh'; +var targetPos = utils.getMousePos(playerName); +if (targetPos){ + if (__plugin.canary){ + targetPos.world.makeLightningBolt(targetPos); + } + if (__plugin.bukkit){ + targetPos.world.strikeLightning(targetPos); + } +} +``` + +### utils.foreach() function + +The utils.foreach() function is a utility function for iterating over +an array of objects (or a java.util.Collection of objects) and +processing each object in turn. Where utils.foreach() differs from +other similar functions found in javascript libraries, is that +utils.foreach can process the array immediately or can process it +*nicely* by processing one item at a time then delaying processing of +the next item for a given number of server ticks (there are 20 ticks +per second on the minecraft main thread). This method relies on +Bukkit's [org.bukkit.scheduler][sched] package for scheduling +processing of arrays. + +[sched]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/package-summary.html + +#### Parameters + + * array : The array to be processed - It can be a javascript array, a java array or java.util.Collection + * callback : The function to be called to process each item in the + array. The callback function should have the following signature + `callback(item, index, object, array)`. That is the callback will + be called with the following parameters.... + + - item : The item in the array + - index : The index at which the item can be found in the array. + - object : Additional (optional) information passed into the foreach method. + - array : The entire array. + + * context (optional) : An object which may be used by the callback. + * delayInMilliseconds (optional, numeric) : If a delay is specified then the processing will be scheduled so that + each item will be processed in turn with a delay between the completion of each + item and the start of the next. This is recommended for any CPU-intensive process. + * onDone (optional, function) : A function to be executed when all processing + is complete. This parameter is only used when the processing is delayed. (It's optional even if a + delay parameter is supplied). + +If called with a delay parameter then foreach() will return +immediately after processing just the first item in the array (all +subsequent items are processed later). If your code relies on the +completion of the array processing, then provide an `onDone` parameter +and put the code there. + +#### Example + +The following example illustrates how to use foreach for immediate processing of an array... + +```javascript +var utils = require('utils'); +var players = utils.players(); +utils.foreach (players, function( player ) { + echo( player , 'Hi ' + player); +}); +``` + +... The `utils.foreach()` function can work with Arrays or any +Java-style collection. This is important because many objects in the +CanaryMod and Bukkit APIs use Java-style collections. +### utils.nicely() function + +The utils.nicely() function is for performing background processing. utils.nicely() lets you +process with a specified delay between the completion of each `next()` +function and the start of the next `next()` function. +`utils.nicely()` is a recursive function - that is - it calls itself +(schedules itself actually) repeatedly until `hasNext` returns false. + +#### Parameters + + * next : A function which will be called if processing is to be done. + * hasNext : A function which is called to determine if the `next` + callback should be invoked. This should return a boolean value - + true if the `next` function should be called (processing is not + complete), false otherwise. + * onDone : A function which is to be called when all processing is complete (hasNext returned false). + * delayInMilliseconds : The delay between each call. + +#### Example + +See the source code to utils.foreach for an example of how utils.nicely is used. + +### utils.time( world ) function + +Returns the timeofday (in minecraft ticks) for the given world. This function is necessary because +canarymod and bukkit differ in how the timeofday is calculated. + +See http://minecraft.gamepedia.com/Day-night_cycle#Conversions + +### utils.time24( world ) function + +Returns the timeofday for the given world using 24 hour notation. (number of minutes) + +See http://minecraft.gamepedia.com/Day-night_cycle#Conversions + +#### Parameters + + * world : the name of the world or world object for which you want to get time + +### utils.find() function + +The utils.find() function will return a list of all files starting at +a given directory and recursiving trawling all sub-directories. + +#### Parameters + + * dir : The starting path. Must be a string. + * filter : (optional) A [FilenameFilter][fnfltr] object to return only files matching a given pattern. + +[fnfltr]: http://docs.oracle.com/javase/6/docs/api/java/io/FilenameFilter.html + +#### Example + +```javascript +var utils = require('utils'); +var jsFiles = utils.find('./', function(dir,name){ + return name.match(/\.js$/); +}); +``` +### utils.serverAddress() function + +The utils.serverAddress() function returns the IP(v4) address of the server. + +```javascript +var utils = require('utils'); +var serverAddress = utils.serverAddress(); +console.log(serverAddress); +``` +### utils.array() function + +Converts Java collection objects to type Javascript array so they can avail of +all of Javascript's Array goodness. + +#### Example + + var utils = require('utils'); + var worlds = utils.array(server.worldManager.getAllWorlds()); + +### utils.players() function + +This function returns a javascript array of all online players on the +server. You can optionally provide a function which will be invoked +with each player as a parameter. For example, to give each player the +ability to shoot arrows which launch fireworks: + +```javascript +require('utils').players( arrows.firework ) +``` + +Any players with a bow will be able to launch fireworks by shooting. + +### utils.playerNames() function + +This function returns a javascript array of player names (as javascript strings) + +### utils.stat() function + +This function returns a numeric value for a given player statistic. + +#### Parameters + + * Player - The player object (optional - if only the statistic name parameter is provided then the statistic object is returned) + * Statistic - A string whose value should be one of the following (CanaryMod) + * ANIMALSBRED + * BOATONECM + * CLIMBONECM + * CROUCHONECM + * DAMAGEDEALT + * DAMAGETAKEN + * DEATHS + * DRIVEONECM + * DROP + * FALLONECM + * FISHCAUGHT + * FLYONECM + * HORSEONECM + * JUMP + * JUNKFISHED + * LEAVEGAME + * MINECARTONECM + * MOBKILLS + * PIGONECM + * PLAYERKILLS + * PLAYONEMINUTE + * SPRINTONECM + * SWIMONECM + * TALKEDTOVILLAGER + * TIMESINCEDEATH + * TRADEDWITHVILLAGER + * TREASUREFISHED + * WALKONECM + +See [CanaryMod's Statistic][cmstat] class for an up-to-date list of possible stat values + +[cmstat]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/statistics/Statistics.html + +#### Example 1 Getting stats for a player + + var utils = require('utils'); + var jumpCount = utils.stat( player, 'jump'); + +#### Example 2 Getting the JUMP statistic object (which can be used elsewhere) + + var utils = require('utils'); + var JUMPSTAT = utils.stat('jump'); + var jumpCount = player.getStat( JUMPSTAT ); // canary-specific code + +This function also contains values for each possible stat so you can get at stats like this... + + var utils = require('utils'); + var JUMPSTAT = utils.stat.JUMP; // Accessing the value + var jumpCount = player.getStat ( JUMPSTAT ); // canary-specific code +## The watcher Module + +This module exposes functions for watching for changes to files or directories. + +### watcher.watchFile() function + +Watches for changes to the given file or directory and calls the function provided +when the file changes. + +#### Parameters + + * File - the file to watch (can be a file or directory) + * Callback - The callback to invoke when the file has changed. The callback takes the + changed file as a parameter. + +#### Example + +```javascript +var watcher = require('watcher'); +watcher.watchFile( 'test.txt', function( file ) { + console.log( file + ' has changed'); +}); +``` +### watcher.watchDir() function + +Watches for changes to the given directory and calls the function provided +when the directory changes. It works by calling watchFile/watchDir for each +file/subdirectory. + +#### Parameters + + * Dir - the file to watch (can be a file or directory) + * Callback - The callback to invoke when the directory has changed. + The callback takes the changed file as a parameter. + For each change inside the directory the callback will also + be called. + +#### Example + +```javascript +var watcher = require('watcher'); +watcher.watchDir( 'players/_ial', function( dir ) { + console.log( dir + ' has changed'); +}); +``` +### watcher.unwatchFile() function + +Removes a file from the watch list. + +#### Example +```javascript +var watcher = require('watcher'); +watcher.unwatchFile('test.txt'); +``` + +### watcher.unwatchDir() function + +Removes a directory from the watch list and all files inside the directory +are also "unwatched" + +#### Example +```javascript +var watcher = require('watcher'); +watcher.unwatchDir ('players/_ial'); +``` +Would cause also +```javascript +watcher.unwatchFile (file); +``` +for each file inside directory (and unwatchDir for each directory inside it) + +## Example Plugin #1 - A simple extension to Minecraft. + +A simple minecraft plugin. The most basic module. + +### Usage: + +At the in-game prompt type ... + + /js hello(self) + +... and a message `Hello {player-name}` will appear (where + {player-name} is replaced by your own name). + +This example demonstrates the basics of adding new functionality which +is only usable by server operators or users with the +scriptcraft.evaluate permission. By default, only ops are granted this +permission. + +The `hello` function below is only usable by players with the scriptcraft.evaluate +permission since it relies on the `/js` command to execute. + + exports.hello = function(player){ + echo( player, 'Hello ' + player.name); + }; + +## Example Plugin #2 - Making extensions available for all players. + +A simple minecraft plugin. Commands for other players. + +### Usage: + +At the in-game prompt type ... + + /jsp hello + +... and a message `Hello {player-name}` will appear (where {player-name} is +replaced by your own name). + +This example demonstrates the basics of adding new functionality +which is usable all players or those with the scriptcraft.proxy +permission. By default, all players are granted this permission. + +This differs from example 1 in that a new 'jsp ' command extension +is defined. Since all players can use the `jsp` command, all players +can use the new extension. Unlike the previous example, the `jsp hello` +command does not evaluate javascript code so this command is much more secure. + + command('hello', function (parameters, player) { + echo( player, 'Hello ' + player.name); + }); + +## Example Plugin #3 - Limiting use of commands to operators only. + +A simple minecraft plugin. Commands for operators only. + +### Usage: + +At the in-game prompt type ... + + /jsp op-hello + +... and a message `Hello {player-name}` will appear (where {player-name} is +replaced by your own name). + +This example demonstrates the basics of adding new functionality +which is usable all players or those with the scriptcraft.proxy +permission. By default, all players are granted this permission. In +this command though, the function checks to see if the player is an +operator and if they aren't will return immediately. + +This differs from example 2 in that the function will only print a +message for operators. + + command('op-hello', function (parameters, player) { + if ( !isOp(player) ){ + echo( player, 'Only operators can do this.'); + return; + } + echo( player, 'Hello ' + player.name); + }); +## Example Plugin #4 - Using parameters in commands. + +A simple minecraft plugin. Handling parameters. + +### Usage: + +At the in-game prompt type ... + + /jsp hello-params Hi + /jsp hello-params Saludos + /jsp hello-params Greetings + +... and a message `Hi {player-name}` or `Saludos {player-name}` etc +will appear (where {player-name} is replaced by your own name). + +This example demonstrates adding and using parameters in commands. + +This differs from example 3 in that the greeting can be changed from +a fixed 'Hello ' to anything you like by passing a parameter. + + command( 'hello-params', function ( parameters, player ) { + var salutation = parameters[0] ; + echo( player, salutation + ' ' + player.name ); + }); + +## Example Plugin #5 - Re-use - Using your own and others modules. + +A simple minecraft plugin. Using Modules. + +### Usage: + +At the in-game prompt type ... + + /jsp hello-module + +... and a message `Hello {player-name}` will appear (where {player-name} is +replaced by your own name). + +This example demonstrates the use of modules. In +example-1-hello-module.js we created a new javascript module. In +this example, we use that module... + + * We load the module using the `require()` function. Because this + module and the module we require are n the same directory, we + specify `'./example-1-hello-module'` as the path (when loading a + module from the same directory, `./` at the start of the path + indicates that the file should be searched for in the same + directory. + + * We assign the loaded module to a variable (`greetings`) and then + use the module's `hello` method to display the message. + +Source Code... + + var greetings = require('./example-1-hello-module'); + command( 'hello-module', function( parameters, player ) { + greetings.hello( player ); + }); + +## Example Plugin #6 - Re-use - Using 'utils' to get Player objects. + +A simple minecraft plugin. Finding players by name. + +### Usage: + +At the in-game prompt type ... + + /jsp hello-byname {player-name} + +... substituting {player-name} with the name of a player currently +online and a message `Hello ...` will be sent to the named +player. + +This example builds on example-5 and also introduces a new concept - +use of shared modules. That is : modules which are not specific to +any one plugin or set of plugins but which can be used by all +plugins. Shared modules should be placed in the +`scriptcraft/modules` directory. + + * The utils module is used. Because the 'utils' module is + located in the modules folder we don't need to specify an exact + path, just 'utils' will do. + + * The `utils.player()` function is used to obtain a player object + matching the player name. Once a player object is obtained, a + message is sent to that player. + +Source Code ... + + var utils = require('utils'); + var greetings = require('./example-1-hello-module'); + + command( 'hello-byname', function( parameters, sender ) { + var playerName = parameters[0]; + var recipient = utils.player( playerName ); + if ( recipient ) { + greetings.hello( recipient ); + } else { + echo( sender, 'Player ' + playerName + ' not found.' ); + } + }); + +## Example Plugin #7 - Listening for events, Greet players when they join the game. + +A simple event-driven minecraft plugin. How to handle Events. + +This example demonstrates event-driven programming. The code below +will display the version of ScriptCraft every time an operator joins +the game. This module is notable from previous modules for the +following reasons... + + 1. It does not export any functions or variables. That's fine. Not + all modules need export stuff. Code in this module will be + executed when the module is first loaded. Because it is in the + `/scriptcraft/plugins` directory, it will be loaded automatically + when the server starts up. + + 2. It uses ScriptCraft's `events` module to add a new *Event + Handler*. An *Event Handler* is a function that gets + called whenever a particular *event* happens in the game. The + function defined below will only be executed whenever a player + joins the game. This style of program is sometimes refered to as + *Event-Driven Programming*. + +Adding new *Event Handlers* in ScriptCraft is relatively easy. Use one +of the `events` module's functions to add a new event handler. The +events module has many functions - one for each type of event. Each +function takes a single parameter: + + * The event handling function (also sometimes refered to as a + 'callback'). In ScriptCraft, this function takes a single + parameter, an event object. All of the information about the event + is in the event object. + +In the example below, if a player joins the server and is an operator, +then the ScriptCraft plugin information will be displayed to that +player. + +```javascript +function onJoin( event ){ + if ( isOp(event.player) ) { + echo( event.player, 'Welcome to ' + __plugin ); + } +} +events.connection( onJoin ); +``` +First the onJoin() function is defined, this is our event handler - +the function we wish to be called every time some new player joins the +game. Then we hook up - or register - that function using the +events.connection() function. The events.connection function is the +function responsible for adding new *connection* event handlers - that +is - functions which should be invoked when there's a new *connection* +event in the game. A new *connection* event is fired whenever a player +joins the game. There are many other types of events you can handle in +Minecraft. You can see [a full list of events here][cmEvtList]. + +[cmEvtList]: #events-helper-module-canary-version +## Arrows Plugin + +The arrows mod adds fancy arrows to the game. Arrows which ... + + * Launch fireworks. + * Explode on impact. + * Force Lightning to strike where they land. + * Teleport the player to the landing spot. + * Spawn Trees at the landing spot. + +### Usage: + + * `/js arrows.firework(self)` - A firework launches where the the arrow lands. + * `/js arrows.lightning(self)` - lightning strikes where the arrow lands. + * `/js arrows.teleport(self)` - makes player teleport to where arrow has landed. + * `/js arrows.flourish(self)` - makes a tree grow where the arrow lands. + * `/js arrows.explosive(self)` - makes arrows explode. + * `/js arrows.normal(self)` sets arrow type to normal. + * `/js arrows.sign(self)` turns a targeted sign into an Arrows menu + +All of the above functions can take an optional player object or name +as a parameter. For example: `/js arrows.explosive('player23')` makes +player23's arrows explosive. + +## Spawn Plugin + +Allows in-game operators to easily spawn creatures at current location. + +### Usage + + /jsp spawn cow + /jsp spawn sheep + /jsp spawn wolf + +This command supports TAB completion so to see a list of possible +entitities, type `/jsp spawn ' at the in-game command prompt, then +press TAB. Visit + (Bukkit/SpigotMC) +or (CanaryMod) + +for a list of possible entities (creatures) which can be spawned. + +## alias Plugin + +The alias module lets players and server admins create their own +per-player or global custom in-game command aliases. + +### Examples + +To set a command alias which is only visible to the current player +(per-player alias)... + + /jsp alias set cw = time set {1} ; weather {2} + +... Creates a new custom command only usable by the player who set +it called `cw` (short for set Clock and Weather) which when invoked... + + /cw 4000 sun + +... will perform the following commands... + + /time set 4000 + /weather sun + +Aliases can use paramters as above. On the right hand side of the `=`, the +`{1}` refers to the first parameter provided with the `cw` alias, `{2}` +refers to the second parameter and so on. So `cw 4000 sun` is converted to +`time set 4000` and `weather sun`. + +To set a global command alias usable by all (only operators can create +such an alias)... + + /jsp alias global stormy = time 18000; weather storm + +To remove an alias ... + + /jsp alias remove cw + +... removes the 'cw' alias from the appropriate alias map. + +To get a list of aliases currently defined... + + /jsp alias list + +To get help on the `jsp alias` command: + + /jsp alias help + +Aliases can be used at the in-game prompt by players or in the server +console. Aliases will not be able to avail of command autocompletion +(pressing the TAB key will have no effect). + +## Commando Plugin + +### Description + +commando is a plugin which can be used to add completely new commands +to Minecraft. Normally ScriptCraft only allows for provision of new +commands as extensions to the jsp command. For example, to create a +new simple command for use by all players... + + /js command('hi', function(args,player){ echo( player, 'Hi ' + player.name); }); + +... then players can use this command by typing... + + /jsp hi + +... A couple of ScriptCraft users have asked for the ability to take +this a step further and allow the global command namespace to be +populated so that when a developer creates a new command using the +'command' function, then the command is added to the global command +namespace so that players can use it simply like this... + + /hi + +... There are good reasons why ScriptCraft's core `command()` function +does not do this. Polluting the global namespace with commands would +make ScriptCraft a bad citizen in that Plugins should be able to work +together in the same server and - as much as possible - not step on +each others' toes. The CraftBukkit team have very good reasons for +forcing Plugins to declare their commands in the plugin.yml +configuration file. It makes approving plugins easier and ensures that +craftbukkit plugins behave well together. While it is possible to +override other plugins' commands, the CraftBukkit team do not +recommend this. However, as ScriptCraft users have suggested, it +should be at the discretion of server administrators as to when +overriding or adding new commands to the global namespace is good. + +So this is where `commando()` comes in. It uses the exact same +signature as the core `command()` function but will also make the +command accessible without the `jsp` prefix so instead of having to +type `/jsp hi` for the above command example, players simply type +`/hi` . This functionality is provided as a plugin rather than as part +of the ScriptCraft core. + +### Example hi-command.js + + var commando = require('../commando'); + commando('hi', function(args,player){ + echo( player, 'Hi ' + player.name); + }); + +...Displays a greeting to any player who issues the `/hi` command. + +### Example - timeofday-command.js + + var times = {Dawn: 0, Midday: 6000, Dusk: 12000, Midnight:18000}; + commando('timeofday', function(params,player){ + player.location.world.setTime(times[params[0]]); + }, + ['Dawn','Midday','Dusk','Midnight']); + +... changes the time of day using a new `/timeofday` command (options are Dawn, Midday, Dusk, Midnight) + +### Caveats + +Since commands registered using commando are really just appendages to +the `/jsp` command and are not actually registered globally (it just +looks like that to the player), you won't be able to avail of tab +completion for the command itself or its parameters (unless you go the +traditional route of adding the `jsp` prefix). This plugin uses the +[PlayerCommandPreprocessEvent][pcppevt] which allows plugins to +intercepts all commands and inject their own commands instead. If +anyone reading this knows of a better way to programmatically add new +global commands for a plugin, please let me know. + +[pcppevt]: http://jd.bukkit.org/dev/apidocs/org/bukkit/event/player/PlayerCommandPreprocessEvent.html + +## homes Plugin + +The homes plugin lets players set a location as home and return to the +location, invite other players to their home and also visit other +player's homes. + +This module is a good example of how to create a javascript-based +minecraft mod which provides... + + * A programmatic interface (API) and + * A command extension which uses that API to provide new functionality for players. + +The module uses the `plugin()` function to specify an object and +methods, and the `command()` function to expose functionality to +players through a new `jsp home` command. This module also +demonstrates how to enable autocompletion for custom commands (to see +this in action, at the in-game prompt or server console prompt type +`jsp home ` then press the TAB key - you should see a list of further +possible options). + +The `jsp home` command has the following options... + +### Basic options + + * `/jsp home set` Will set your current location as your + 'home' location to which you can return at any time using the ... + + * `/jsp home` ..command will return you to your home, if you have set one. + + * `/jsp home {player}` Will take you to the home of {player} (where + {player} is the name of the player whose home you wish to visit. + + * `/jsp home delete` Deletes your home location from the location + database. This does not actually remove the home from the world or + change the world in any way. This command is completely + non-destructive and cannot be used for griefing. No blocks will be + destroyed by this command. + +### Social options +The following options allow players to open their homes to all or some +players, invite players to their home and see a list of homes they can +visit. + + * `/jsp home list` Lists home which you can visit. + * `/jsp home ilist` Lists players who can visit your home. + * `/jsp home invite {player}` Invites the named player to your home. + * `/jsp home uninvite {player}` Uninvites (revokes invitation) the named player to your home. + * `/jsp home public` Opens your home to all players (all players can visit your home). + * `/jsp home private` Makes your home private (no longer visitable by all). + +### Administration options +The following administration options can only be used by server operators... + + * `/jsp home listall` List all of the homes + * `/jsp home clear {player}` Removes the player's home + location. Again, this command does not destroy any structures in + the world, it simply removes the location from the database. No + blocks are destroyed by this command. + +## NumberGuess mini-game: + +### Description +This is a very simple number guessing game. Minecraft will ask you to +guess a number between 1 and 10 and you will tell you if you're too +hight or too low when you guess wrong. The purpose of this mini-game +code is to demonstrate use of Bukkit's Conversation API. + +### Example + + /js Game_NumberGuess.start(self) + +Once the game begins, guess a number by typing the `/` character +followed by a number between 1 and 10. + +## Cow Clicker Mini-Game + +### How to Play + +At the in-game prompt type `jsp cowclicker` to start or stop +playing. Right-Click on Cows to score points. No points for killing +cows (hint: use the same keyboard keys you'd use for opening doors). + +Every time you click a cow your score increases by 1 point. Your score +is displayed in a side-bar along the right edge of of the screen. + +![cow clicker](img/cowclicker.png) + +### Rules + + * You can join and leave the Cow Clicker game at any time by typing + `/jsp cowclicker` at the in-game prompt. + + * Once you leave the game, your score is reset to zero. + + * When you disconnect from the server, your score will be reset to zero. + +### Gameplay Mechanics + +This is meant as a trivially simple use of the [Bukkit Scoreboard +API][bukscore]. There are many things you'll want to consider when constructing +your own mini-game... + + * Is the game itself a long-lived game - that is - should players and + scores be persisted (stored) between server restarts? + + * What should happen when a player quits the server - should this also be + understood as quitting the mini-game? + + * What should happen when a player who was previously playing the + mini-game, joins the server - should they automatically resume the + mini-game? + +[bukscore]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scoreboard/package-summary.html + + +## Items module (SpigotMC version) +The Items module provides a suite of functions - one for each possible item. +See https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html for a list of possible items + +### Usage + + items.book(); // returns org.bukkit.Material.BOOK + items.book(2); // returns a new org.bukkit.Material object with an amount 2 (2 books) + items.book( itemType ); // compares itemType parameter to org.bukkit.Material.BOOK or an Item of type book + +The following functions are provided: + + * acaciaDoor() + * acaciaDoorItem() + * acaciaFence() + * acaciaFenceGate() + * acaciaStairs() + * activatorRail() + * air() + * anvil() + * apple() + * armorStand() + * arrow() + * bakedPotato() + * banner() + * barrier() + * beacon() + * bed() + * bedBlock() + * bedrock() + * beetroot() + * beetrootBlock() + * beetrootSeeds() + * beetrootSoup() + * birchDoor() + * birchDoorItem() + * birchFence() + * birchFenceGate() + * birchWoodStairs() + * blackShulkerBox() + * blazePowder() + * blazeRod() + * blueShulkerBox() + * boat() + * boatAcacia() + * boatBirch() + * boatDarkOak() + * boatJungle() + * boatSpruce() + * bone() + * boneBlock() + * book() + * bookAndQuill() + * bookshelf() + * bow() + * bowl() + * bread() + * brewingStand() + * brewingStandItem() + * brick() + * brickStairs() + * brownMushroom() + * brownShulkerBox() + * bucket() + * burningFurnace() + * cactus() + * cake() + * cakeBlock() + * carpet() + * carrot() + * carrotItem() + * carrotStick() + * cauldron() + * cauldronItem() + * chainmailBoots() + * chainmailChestplate() + * chainmailHelmet() + * chainmailLeggings() + * chest() + * chorusFlower() + * chorusFruit() + * chorusFruitPopped() + * chorusPlant() + * clay() + * clayBall() + * clayBrick() + * coal() + * coalBlock() + * coalOre() + * cobbleWall() + * cobblestone() + * cobblestoneStairs() + * cocoa() + * command() + * commandChain() + * commandMinecart() + * commandRepeating() + * compass() + * cookedBeef() + * cookedChicken() + * cookedFish() + * cookedMutton() + * cookedRabbit() + * cookie() + * crops() + * cyanShulkerBox() + * darkOakDoor() + * darkOakDoorItem() + * darkOakFence() + * darkOakFenceGate() + * darkOakStairs() + * daylightDetector() + * daylightDetectorInverted() + * deadBush() + * detectorRail() + * diamond() + * diamondAxe() + * diamondBarding() + * diamondBlock() + * diamondBoots() + * diamondChestplate() + * diamondHelmet() + * diamondHoe() + * diamondLeggings() + * diamondOre() + * diamondPickaxe() + * diamondSpade() + * diamondSword() + * diode() + * diodeBlockOff() + * diodeBlockOn() + * dirt() + * dispenser() + * doublePlant() + * doubleStep() + * doubleStoneSlab2() + * dragonEgg() + * dragonsBreath() + * dropper() + * egg() + * elytra() + * emerald() + * emeraldBlock() + * emeraldOre() + * emptyMap() + * enchantedBook() + * enchantmentTable() + * endBricks() + * endCrystal() + * endGateway() + * endRod() + * enderChest() + * enderPearl() + * enderPortal() + * enderPortalFrame() + * enderStone() + * expBottle() + * explosiveMinecart() + * eyeOfEnder() + * feather() + * fence() + * fenceGate() + * fermentedSpiderEye() + * fire() + * fireball() + * firework() + * fireworkCharge() + * fishingRod() + * flint() + * flintAndSteel() + * flowerPot() + * flowerPotItem() + * frostedIce() + * furnace() + * ghastTear() + * glass() + * glassBottle() + * glowingRedstoneOre() + * glowstone() + * glowstoneDust() + * goldAxe() + * goldBarding() + * goldBlock() + * goldBoots() + * goldChestplate() + * goldHelmet() + * goldHoe() + * goldIngot() + * goldLeggings() + * goldNugget() + * goldOre() + * goldPickaxe() + * goldPlate() + * goldRecord() + * goldSpade() + * goldSword() + * goldenApple() + * goldenCarrot() + * grass() + * grassPath() + * gravel() + * grayShulkerBox() + * greenRecord() + * greenShulkerBox() + * grilledPork() + * hardClay() + * hayBlock() + * hopper() + * hopperMinecart() + * hugeMushroom1() + * hugeMushroom2() + * ice() + * inkSack() + * ironAxe() + * ironBarding() + * ironBlock() + * ironBoots() + * ironChestplate() + * ironDoor() + * ironDoorBlock() + * ironFence() + * ironHelmet() + * ironHoe() + * ironIngot() + * ironLeggings() + * ironNugget() + * ironOre() + * ironPickaxe() + * ironPlate() + * ironSpade() + * ironSword() + * ironTrapdoor() + * itemFrame() + * jackOLantern() + * jukebox() + * jungleDoor() + * jungleDoorItem() + * jungleFence() + * jungleFenceGate() + * jungleWoodStairs() + * ladder() + * lapisBlock() + * lapisOre() + * lava() + * lavaBucket() + * leash() + * leather() + * leatherBoots() + * leatherChestplate() + * leatherHelmet() + * leatherLeggings() + * leaves() + * leaves2() + * lever() + * lightBlueShulkerBox() + * limeShulkerBox() + * lingeringPotion() + * log() + * log2() + * longGrass() + * magentaShulkerBox() + * magma() + * magmaCream() + * map() + * melon() + * melonBlock() + * melonSeeds() + * melonStem() + * milkBucket() + * minecart() + * mobSpawner() + * monsterEgg() + * monsterEggs() + * mossyCobblestone() + * mushroomSoup() + * mutton() + * mycel() + * nameTag() + * netherBrick() + * netherBrickItem() + * netherBrickStairs() + * netherFence() + * netherStalk() + * netherStar() + * netherWartBlock() + * netherWarts() + * netherrack() + * noteBlock() + * observer() + * obsidian() + * orangeShulkerBox() + * packedIce() + * painting() + * paper() + * pinkShulkerBox() + * pistonBase() + * pistonExtension() + * pistonMovingPiece() + * pistonStickyBase() + * poisonousPotato() + * pork() + * portal() + * potato() + * potatoItem() + * potion() + * poweredMinecart() + * poweredRail() + * prismarine() + * prismarineCrystals() + * prismarineShard() + * pumpkin() + * pumpkinPie() + * pumpkinSeeds() + * pumpkinStem() + * purpleShulkerBox() + * purpurBlock() + * purpurDoubleSlab() + * purpurPillar() + * purpurSlab() + * purpurStairs() + * quartz() + * quartzBlock() + * quartzOre() + * quartzStairs() + * rabbit() + * rabbitFoot() + * rabbitHide() + * rabbitStew() + * rails() + * rawBeef() + * rawChicken() + * rawFish() + * record10() + * record11() + * record12() + * record3() + * record4() + * record5() + * record6() + * record7() + * record8() + * record9() + * redMushroom() + * redNetherBrick() + * redRose() + * redSandstone() + * redSandstoneStairs() + * redShulkerBox() + * redstone() + * redstoneBlock() + * redstoneComparator() + * redstoneComparatorOff() + * redstoneComparatorOn() + * redstoneLampOff() + * redstoneLampOn() + * redstoneOre() + * redstoneTorchOff() + * redstoneTorchOn() + * redstoneWire() + * rottenFlesh() + * saddle() + * sand() + * sandstone() + * sandstoneStairs() + * sapling() + * seaLantern() + * seeds() + * shears() + * shield() + * shulkerShell() + * sign() + * signPost() + * silverShulkerBox() + * skull() + * skullItem() + * slimeBall() + * slimeBlock() + * smoothBrick() + * smoothStairs() + * snow() + * snowBall() + * snowBlock() + * soil() + * soulSand() + * speckledMelon() + * spectralArrow() + * spiderEye() + * splashPotion() + * sponge() + * spruceDoor() + * spruceDoorItem() + * spruceFence() + * spruceFenceGate() + * spruceWoodStairs() + * stainedClay() + * stainedGlass() + * stainedGlassPane() + * standingBanner() + * stationaryLava() + * stationaryWater() + * step() + * stick() + * stone() + * stoneAxe() + * stoneButton() + * stoneHoe() + * stonePickaxe() + * stonePlate() + * stoneSlab2() + * stoneSpade() + * stoneSword() + * storageMinecart() + * string() + * structureBlock() + * structureVoid() + * sugar() + * sugarCane() + * sugarCaneBlock() + * sulphur() + * thinGlass() + * tippedArrow() + * tnt() + * torch() + * totem() + * trapDoor() + * trappedChest() + * tripwire() + * tripwireHook() + * vine() + * wallBanner() + * wallSign() + * watch() + * water() + * waterBucket() + * waterLily() + * web() + * wheat() + * whiteShulkerBox() + * wood() + * woodAxe() + * woodButton() + * woodDoor() + * woodDoubleStep() + * woodHoe() + * woodPickaxe() + * woodPlate() + * woodSpade() + * woodStairs() + * woodStep() + * woodSword() + * woodenDoor() + * wool() + * workbench() + * writtenBook() + * yellowFlower() + * yellowShulkerBox() + + +## Items module (CanaryMod version) +The Items module provides a suite of functions - one for each possible item. +See https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/inventory/ItemType.html for a list of possible items + +### Usage + + items.book(); // returns net.canarymod.api.inventory.ItemType.Book + items.book(2); // returns a new net.canarymod.api.inventory.Item object with an amount 2 (2 books) + items.book( itemType ); // compares itemType parameter to ItemType.Book or an Item of type book + +The following functions are provided: + + * acaciaDoor() + * acaciaFence() + * acaciaFenceGate() + * acaciaLeaves() + * acaciaLog() + * acaciaSapling() + * acaciaStairs() + * acaciaWood() + * acaciaWoodSlab() + * activatorRail() + * allium() + * andesite() + * anvil() + * apple() + * armorStand() + * arrow() + * azureBluet() + * bakedPotato() + * banner() + * beacon() + * bed() + * bedrock() + * birchDoor() + * birchFence() + * birchFenceGate() + * birchLeaves() + * birchLog() + * birchSapling() + * birchWood() + * birchWoodSlab() + * birchWoodStairs() + * blackCarpet() + * blackGlass() + * blackGlassPane() + * blackStainedClay() + * blazePowder() + * blazeRod() + * blocksRecord() + * blueCarpet() + * blueGlass() + * blueGlassPane() + * blueOrchid() + * blueStainedClay() + * boat() + * bone() + * bonemeal() + * book() + * bookAndQuill() + * bookshelf() + * bottleOEnchanting() + * bow() + * bowl() + * bread() + * brewingStand() + * brickBlock() + * brickSlab() + * brickStairs() + * brownCarpet() + * brownGlass() + * brownGlassPane() + * brownMushroom() + * brownStainedClay() + * bucket() + * burningFurnace() + * cactus() + * cactusGreen() + * cake() + * carrot() + * carrotOnAStick() + * carrots() + * cauldron() + * chainmailBoots() + * chainmailChestplate() + * chainmailHelmet() + * chainmailLeggings() + * charcoal() + * chest() + * chirpRecord() + * clay() + * clayBall() + * clayBrick() + * clownFish() + * coal() + * coalBlock() + * coalOre() + * coarseDirt() + * cobble() + * cobbleSilverFishBlock() + * cobbleSlab() + * cobbleStairs() + * cobblestoneWall() + * cocoaBeans() + * cocoaPlant() + * commandBlock() + * compass() + * cookedChicken() + * cookedClownFish() + * cookedFish() + * cookedMutton() + * cookedPufferFish() + * cookedRabbit() + * cookedSalmon() + * cookie() + * crackedSilverFishBlock() + * crackedStoneBrick() + * creeperHead() + * cyanCarpet() + * cyanDye() + * cyanGlass() + * cyanGlassPane() + * cyanStainedClay() + * dandelionYellow() + * darkOakDoor() + * darkOakFence() + * darkOakFenceGate() + * darkOakLeaves() + * darkOakLog() + * darkOakSapling() + * darkOakStairs() + * darkOakWood() + * darkOakWoodSlab() + * daylightSensor() + * deadBush() + * detectorRail() + * diamond() + * diamondAxe() + * diamondBlock() + * diamondBoots() + * diamondChestplate() + * diamondHelmet() + * diamondHoe() + * diamondHorseArmor() + * diamondLeggings() + * diamondOre() + * diamondPickaxe() + * diamondSpade() + * diamondSword() + * diorite() + * dirt() + * dispenser() + * doubleAcaciaWoodSlab() + * doubleBirchWoodSlab() + * doubleBrickBlockSlab() + * doubleCobbleSlab() + * doubleDarkOakWoodSlab() + * doubleGrass() + * doubleJungleWoodSlab() + * doubleNetherBrickSlab() + * doubleOakWoodSlab() + * doubleOrnateStoneSlab() + * doubleQuartzSlab() + * doubleRedSandstoneSlab() + * doubleSandStoneTrimSlab() + * doubleSandstoneSlab() + * doubleSpruceWoodSlab() + * doubleStoneBricksSlab() + * doubleStoneSlab() + * doubleWoodSlab() + * dropper() + * egg() + * elevenRecord() + * emerald() + * emeraldBlock() + * emeraldOre() + * emptyMap() + * enchantedBook() + * enchantmentTable() + * endPortal() + * endPortalFrame() + * endStone() + * enderChest() + * enderDragonEgg() + * enderPearl() + * eyeofEnder() + * farRecord() + * feather() + * fence() + * fenceGate() + * fermentedSpiderEye() + * fireBlock() + * fireCharge() + * fireworkRocket() + * fireworkStar() + * fishingRod() + * flint() + * flintAndSteel() + * flowerPot() + * furnace() + * ghastTear() + * glass() + * glassBottle() + * glassPane() + * glisteringMelon() + * glowStone() + * glowstoneDust() + * goldAxe() + * goldBlock() + * goldBoots() + * goldChestplate() + * goldHelmet() + * goldHoe() + * goldHorseArmor() + * goldIngot() + * goldLeggings() + * goldNugget() + * goldOre() + * goldPickaxe() + * goldRecord() + * goldSpade() + * goldSword() + * goldenApple() + * goldenCarrot() + * granite() + * grass() + * gravel() + * grayCarpet() + * grayDye() + * grayGlass() + * grayGlassPane() + * grayStainedClay() + * greenCarpet() + * greenGlass() + * greenGlassPane() + * greenRecord() + * greenStainedClay() + * grilledPork() + * gunpowder() + * hardenedClay() + * hayBale() + * heavyWeightedPressurePlate() + * hopper() + * hugeBrownMushroom() + * hugeRedMushroom() + * humanHead() + * ice() + * inkSack() + * ironAxe() + * ironBars() + * ironBlock() + * ironBoots() + * ironChestplate() + * ironDoor() + * ironHelmet() + * ironHoe() + * ironHorseArmor() + * ironIngot() + * ironLeggings() + * ironOre() + * ironPickaxe() + * ironSpade() + * ironSword() + * itemFrame() + * jackOLantern() + * jukebox() + * jungleDoor() + * jungleFence() + * jungleFenceGate() + * jungleLeaves() + * jungleLog() + * jungleSapling() + * jungleWood() + * jungleWoodSlab() + * jungleWoodStairs() + * ladder() + * lapisBlock() + * lapisLazuli() + * lapislazuliOre() + * largeFern() + * lava() + * lavaBucket() + * lavaFlowing() + * lead() + * leather() + * leatherBoots() + * leatherChestplate() + * leatherHelmet() + * leatherLeggings() + * lever() + * lightBlueCarpet() + * lightBlueDye() + * lightBlueGlass() + * lightBlueGlassPane() + * lightBlueStainedClay() + * lightGrayCarpet() + * lightGrayDye() + * lightGrayGlass() + * lightGrayGlassPane() + * lightGrayStainedClay() + * lightWeightedPressurePlate() + * lilac() + * lilypad() + * limeCarpet() + * limeDye() + * limeGlass() + * limeGlassPane() + * limeStainedClay() + * magentaCarpet() + * magentaDye() + * magentaGlass() + * magentaGlassPane() + * magentaStainedClay() + * magmaCream() + * mallRecord() + * map() + * mellohiRecord() + * melon() + * melonSeeds() + * melonSlice() + * milkBucket() + * minecart() + * minecartCommandBlock() + * minecartHopper() + * minecartTNT() + * mobSpawner() + * mossyBrickSilverFishBlock() + * mossyCobble() + * mossyCobbleWall() + * mossyStoneBrick() + * mushroomSoup() + * mycelium() + * nameTag() + * netherBrick() + * netherBrickFence() + * netherBrickStairs() + * netherBricks() + * netherBricksSlab() + * netherQuartz() + * netherQuartzOre() + * netherStar() + * netherWart() + * netherrack() + * noteBlock() + * oakLeaves() + * oakLog() + * oakSapling() + * oakWood() + * oakWoodSlab() + * obsidian() + * orangeCarpet() + * orangeDye() + * orangeGlass() + * orangeGlassPane() + * orangeStainedClay() + * orangeTulip() + * ornateQuartzBlock() + * ornateSilverFishBlock() + * ornateStoneBrick() + * ornateStoneSlab() + * oxeyeDaisy() + * packedIce() + * painting() + * paper() + * peony() + * pineLeaves() + * pineLog() + * pineWoodStairs() + * pinkCarpet() + * pinkDye() + * pinkGlass() + * pinkGlassPane() + * pinkStainedClay() + * pinkTulip() + * piston() + * podzol() + * poisonousPotato() + * polishedAndesite() + * polishedDiorite() + * polishedGranite() + * poppy() + * pork() + * portal() + * potato() + * potatoes() + * potion() + * poweredMinecart() + * poweredRail() + * prismarineCrystals() + * prismarineShard() + * pufferFish() + * pumpkin() + * pumpkinPie() + * pumpkinSeeds() + * purpleCarpet() + * purpleDye() + * purpleGlass() + * purpleGlassPane() + * purpleStainedClay() + * quartzBlock() + * quartzPillarCap() + * quartzPillarHorizontal() + * quartzPillarVertical() + * quartzSlab() + * quartzStairs() + * rabbitFoot() + * rabbitHide() + * rabbitStew() + * rail() + * rawBeef() + * rawChicken() + * rawFish() + * rawMutton() + * rawRabbit() + * rawSalmon() + * redCarpet() + * redGlass() + * redGlassPane() + * redMushroom() + * redSandstone() + * redSandstoneBlank() + * redSandstoneOrnate() + * redSandstoneSlab() + * redSandstoneStairs() + * redStainedClay() + * redStone() + * redTulip() + * redstoneBlock() + * redstoneComparator() + * redstoneLampOff() + * redstoneOre() + * redstoneRepeater() + * redstoneTorchOn() + * reed() + * roseBush() + * roseRed() + * rottenFlesh() + * saddle() + * sand() + * sandStoneTrimSlab() + * sandstone() + * sandstoneBlank() + * sandstoneOrnate() + * sandstoneSlab() + * sandstoneStairs() + * seeds() + * shears() + * shrub() + * sign() + * skeletonHead() + * slimeBall() + * snow() + * snowBall() + * snowBlock() + * soil() + * soulSand() + * spawnEgg() + * spiderEye() + * spiderWeb() + * sponge() + * spruceDoor() + * spruceFence() + * spruceFenceGate() + * spruceSapling() + * spruceWood() + * spruceWoodSlab() + * stalRecord() + * steak() + * stick() + * stickyPiston() + * stone() + * stoneAxe() + * stoneBrick() + * stoneBrickSilverFishBlock() + * stoneBrickStairs() + * stoneBricksSlab() + * stoneButton() + * stoneHoe() + * stonePickaxe() + * stonePlate() + * stoneSilverFishBlock() + * stoneSlab() + * stoneSpade() + * stoneSword() + * storageMinecart() + * stradRecord() + * string() + * sugar() + * sunflower() + * tallFern() + * tallGrass() + * tnt() + * torch() + * trapdoor() + * trappedChest() + * tripwireHook() + * vines() + * waitRecord() + * wardRecord() + * watch() + * water() + * waterBucket() + * waterFlowing() + * wheat() + * whiteCarpet() + * whiteGlass() + * whiteGlassPane() + * whiteStainedClay() + * whiteTulip() + * witherSkeletonHead() + * woodAxe() + * woodDoor() + * woodHoe() + * woodPickaxe() + * woodPlate() + * woodSlab() + * woodSpade() + * woodSword() + * woodenButton() + * woodenStairs() + * woolBlack() + * woolBlue() + * woolBrown() + * woolCyan() + * woolDarkGreen() + * woolGray() + * woolLightBlue() + * woolLightGray() + * woolLightGreen() + * woolMagenta() + * woolOrange() + * woolPink() + * woolPurple() + * woolRed() + * woolWhite() + * woolYellow() + * workbench() + * writtenBook() + * yellowCarpet() + * yellowFlower() + * yellowGlass() + * yellowGlassPane() + * yellowStainedClay() + * zombieHead() + + +## Entities module +The Entities module provides a suite of functions - one for each possible entity type. +It acts as a helper or enumerated module to assist in use with the `spawn` module and command. +This module is useful for TAB-completion at the in-game prompt. + +When each function is called with no parameters, it will return the appropriate EntityType object. +For example `entities.polar_bear()` will return an `EntityType.POLAR_BEAR` object. + +When each function is called with a single parameter - an entity - the entity's type will be compared and return true or false. + +### Usage + + entities.zombie(); // returns a SpigotMC/CanaryMod EntityType.ZOMBIE enum value + entities.zombie( mob ); // compares the entity's type to a zombie, returns true if mob type is zombie, false otherwise + entities.player( self ); // at the in-game prompt this should return true (compares self to a player entity type) + entities.rabbit( self ); // at the in-game prompt this should return false (compares self to a rabbit entity type) + +The following functions are provided: + + * area_effect_cloud() + * armor_stand() + * arrow() + * bat() + * blaze() + * boat() + * cave_spider() + * chicken() + * complex_part() + * cow() + * creeper() + * donkey() + * dragon_fireball() + * dropped_item() + * egg() + * elder_guardian() + * ender_crystal() + * ender_dragon() + * ender_pearl() + * ender_signal() + * enderman() + * endermite() + * evoker() + * evoker_fangs() + * experience_orb() + * falling_block() + * fireball() + * firework() + * fishing_hook() + * ghast() + * giant() + * guardian() + * horse() + * husk() + * iron_golem() + * item_frame() + * leash_hitch() + * lightning() + * lingering_potion() + * llama() + * llama_spit() + * magma_cube() + * minecart() + * minecart_chest() + * minecart_command() + * minecart_furnace() + * minecart_hopper() + * minecart_mob_spawner() + * minecart_tnt() + * mule() + * mushroom_cow() + * ocelot() + * painting() + * pig() + * pig_zombie() + * player() + * polar_bear() + * primed_tnt() + * rabbit() + * sheep() + * shulker() + * shulker_bullet() + * silverfish() + * skeleton() + * skeleton_horse() + * slime() + * small_fireball() + * snowball() + * snowman() + * spectral_arrow() + * spider() + * splash_potion() + * squid() + * stray() + * thrown_exp_bottle() + * tipped_arrow() + * unknown() + * vex() + * villager() + * vindicator() + * weather() + * witch() + * wither() + * wither_skeleton() + * wither_skull() + * wolf() + * zombie() + * zombie_horse() + * zombie_villager() + diff --git a/docs/Anatomy-of-a-Plugin.md b/docs/Anatomy-of-a-Plugin.md new file mode 100644 index 0000000..125b8e8 --- /dev/null +++ b/docs/Anatomy-of-a-Plugin.md @@ -0,0 +1,175 @@ +# Anatomy of a ScriptCraft Plugin + +Anything you can do using a Java-based plugin, you can do it +faster and easier in JavaScript with the ScriptCraft plugin. To +demonstrate this, I've recreated a commonly-used mod (homes) in +javascript. The [homes][homes] JavaScript plugin lets players set their current +location as home and return to that location using in-game commands. +They can also visit other players' homes. It's a simple plugin that +demonstrates a couple of new features in ScriptCraft … + + * Persistence + * Adding Player (non-operator) commands + +[homes]: /src/main/js/plugins/homes/homes.js + +Here, I walk you through another useful plugin which lets players modify the color of the in-game chat. + +## Persistence +… First persistence. Persistence is the ability to retain state +after the server has shutdown and started up again. You can create a +JavaScript object which will be saved at shutdown and reloaded at +startup by using the built-in `persist()` function. + +```javascript +// file: scriptcraft/plugins/my-first-plugin.js +var prefs = persist('myprefs', {}); +... +prefs.color = 'black'; +``` +In the example above, a new empty object is created and stored in a file called `myprefs-store.json`. The empty object is returned (if data is not already present in that file or the file does not exist) and any changes to the object's contents are written to the file when the server is shutdown. + +The data is persisted in JSON form so it's even somewhat +human-readable. Declaring a new plugin is easy. I'm going to create a +new plugin called "chat" that will let players change the default +color of their messages in the in-game chat window … + +```javascript +var store = persist('chat-colors', {players: {}}); +exports.chat = { + setColor: function(player,chatColor) { + store.players[player.name] = chatColor; + } +} +``` +The above code doesn't do a whole lot other than let operators set a +player's color choice ( `/js chat.setColor(self, 'green')` ). A little +bit more code has to be added so that the player's text color will +change when chatting with other players, but the above code will ensure +the player's color setting is at least saved. The following code just +ensures that when a player chats, the text will be displayed in their +chosen color … + +```javascript +var colors = ['black', 'blue', 'darkgreen', 'darkaqua', 'darkred', + 'purple', 'gold', 'gray', 'darkgray', 'indigo', + 'brightgreen', 'aqua', 'red', 'pink', + 'yellow', 'white']; +var colorCodes = {}; +var COLOR_CHAR = '\u00a7'; +for (var i =0;i < colors.length;i++) + colorCodes[colors[i]] = i.toString(16); + +var addColor = function( evt ) { + var player = evt.player; + var playerChatColor = store.players[ player.name ]; + if ( playerChatColor ) { + evt.message = COLOR_CHAR + colorCodes[ playerChatColor ] + evt.message; + } +}; + +if (__plugin.bukkit) { + events.asyncPlayerChat(addColor); +} else if (__plugin.canary) { + events.chat(addColor); +}; +``` + +The next step is to declare a lookup table of colors / names and add an event +handler which intercepts and inserts color codes into player's text +messages. + +## Adding new Player Commands +The other command in ScriptCraft is the `/jsp` command – this lets +operators expose plugins for use by regular players. To be clear, `/jsp` +does not do any JavaScript evaluation, it just accepts parameters which +are then passed on to the appropriate JavaScript plugin. So far in this +example plugin we haven't provided any way for regular players to – you +know – actually set their text color of choice – only operators can do +this for a player using the `js chat.setColor(...)` JavaScript +expression. Let's be clear – giving your players access to the whole API +via JavaScript isn't a good idea. So how do you safely let players +choose their text color? If you've written a JavaScript function and +want players to be able to use that function, you expose it using the +new `command()` function like so … + +```javascript +function chat_color( params, sender ){ + var color = params[0]; + if (colorCodes[color]){ + chat.setColor(sender,color); + }else{ + echo(sender, color + ' is not a valid color'); + echo(sender, 'valid colors: ' + colors.join(', ')); + } +} +command(chat_color, colors); +``` + +… The above code adds a new *subcommand* to the `/jsp` command and +also specifies autocomplete options (the last parameter – `colors`) for +that command when the player presses the `TAB` key. Now the player +themselves can change their chosen chat color like so … + + /jsp chat_color yellow + +… What I've done here is create a new plugin which lets players choose +a chat color and saves that preference when the server shuts down and +starts up. I've also added a new `jsp` sub-command – `chat_color` that +players use to change their chat color setting. The full plugin source +code is just a couple of lines of code but is a fully working plugin … + +```javascript +var store = persist('chat-colors', {players: {}}); +exports.chat = { + setColor: function(player,chatColor) { + store.players[player.name] = chatColor; + } +} +var colors = ['black', 'blue', 'darkgreen', 'darkaqua', 'darkred', + 'purple', 'gold', 'gray', 'darkgray', 'indigo', + 'brightgreen', 'aqua', 'red', 'pink', + 'yellow', 'white']; +var colorCodes = {}; +var COLOR_CHAR = '\u00a7'; +for (var i =0;i < colors.length;i++) + colorCodes[colors[i]] = i.toString(16); + +var addColor = function( evt ) { + var player = evt.player; + var playerChatColor = store.players[ player.name ]; + if ( playerChatColor ) { + evt.message = COLOR_CHAR + colorCodes[ playerChatColor ] + evt.message; + } +}; + +if (__plugin.bukkit) { + events.asyncPlayerChat(addColor); +} else if (__plugin.canary) { + events.chat(addColor); +}; + +function chat_color( params, sender ){ + var color = params[0]; + if (colorCodes[color]){ + chat.setColor(sender,color); + }else{ + echo(sender, color + ' is not a valid color'); + echo(sender, 'valid colors: ' + colors.join(', ')); + } +} + +command(chat_color, colors); +``` + +![Chat Color plugin][1] + +… this is what I would call a minimum viable plugin and it +demonstrates some of the new features of ScriptCraft – persistence +(automatic), event handling, and exposing new functionality to players +using the `/jsp` command. I hope this will give potential Minecraft +modders a feel for just how easy it can be to change the game to suit +their needs. + +[1]: img/scriptcraft-chat-color.png + diff --git a/docs/Frequently-Asked-Questions.md b/docs/Frequently-Asked-Questions.md new file mode 100644 index 0000000..792143a --- /dev/null +++ b/docs/Frequently-Asked-Questions.md @@ -0,0 +1,46 @@ +## Using Other Plugins from ScriptCraft +The following question gets asked a lot so I'm going to try to answer it here: + +> How to use other bukkit plugins API? +> Like PermissionEX API. +> I can check permission group by java code: +> ru.tehkode.permissions.bukkit.PermissionsEx.getUser(player).inGroup("moderator"); +> But I can't run this code in JavaScript. +> -- [Bukkit forum question][1] + +[1]: http://dev.bukkit.org/bukkit-plugins/scriptcraft/?page=2#c48 + +The above question refers to using ScriptCraft for CraftBukkit so I'll answer that first: + +You can get the permissionsEx (or any other Bukkit plugin) like this... +```javascript +var pex = server.pluginManager.getPlugin('PermissionsEx'); +if (pex.getUser(player).inGroup('moderator') ) { +... +} +``` +Generally if you want to use another plugin's API, then get the plugin object by name and then call its methods. In the above example the `pex` variable refers to the aforementioned `PermissionsEx` Plugin. Once you have that reference you can call any of the plugin's methods just as you would in Java. The tricky part is getting the reference and that's where `server.pluginManager.getPlugin()` comes in. + +To get a reference to and work with another plugin's API using ScriptCraft for CanaryMod the same principle applies. Say you've installed ScriptCraft and the dConomy plugin: + +```javascript +var Canary = Packages.net.canarymod.Canary; +var pluginMgr = Canary.pluginManager(); +var dConomy = pluginMgr.getPlugin('dConomy'); +var dConomyServer = dConomy.modServer; +// from here on in you can access all of the dConomyServer object's calls +// e.g. dConomyServer.newTransaction() +``` + +The only difference between CanaryMod and Bukkit is how you get the plugin reference. In Bukkit it's: + +```javascript +var otherPlugin = server.pluginManager.getPlugin('PLUGIN_NAME_GOES_HERE'); +``` + +whereas in CanaryMod it's: + +```javascript +var Canary = Packages.net.canarymod.Canary; +var otherPlugin = Canary.pluginManager().getPlugin('PLUGIN_NAME_GOES_HERE'); +``` diff --git a/docs/Using-Java-APIs-In-Javascript.md b/docs/Using-Java-APIs-In-Javascript.md new file mode 100644 index 0000000..cfc2cc4 --- /dev/null +++ b/docs/Using-Java-APIs-In-Javascript.md @@ -0,0 +1,126 @@ +# Using Java APIs in Javascript + +ScriptCraft uses the Javascript Engine bundled with Java 6 and later +versions. This means that all of the core Java classes can be used +from within ScriptCraft. In addition, all of the Bukkit API can be +used from Javascript too. There are some things to consider when using +Java classes in Javascript... + +## Using Java Beans + +The Javascript Engine bundled with Java comes with a handy notation +for accessing and modifying Java Beans. A Java Bean is any Java class +which uses a `get{Property}()` method to retrieve an object's property +and a `set{Property}()` method to set the object's property. One +example of a Java Bean in the [Bukkit API][bukapi] is the +[org.bukkit.entity.Player][bukpl] Class which has many methods which +conform to the JavaBean specification. + +For example the [Player.getWalkSpeed()][bukplws] can be used to get a +player's walking speed. In Java you would have to write code like this +to obtain the walking speed... + + float walkingSpeed = player.getWalkSpeed(); + +... however, in Javascript you can access the walking-speed property +using the more succinct... + + var walkingspeed = player.walkSpeed; + +... or if you prefer to use Java-style access... + + var walkingspeed = player.getWalkSpeed(); + +... I personally prefer to use the simpler `player.walkSpeed` because +it is easier to read. The important thing to remember when using the +Bukkit (or any Java API) from Javascript is that for any Java Bean, a +property called `propertyName` will have a getter called +`getPropertyName()` and a setter called `setPropertyName()`. From this +rule you can infer what any Bukkit class properties are. For example, +the [Bukkit Player][bukpl] object has the following methods... + + * float getWalkSpeed() + * void setWalkSpeed(float speed) + +... so from this you can infer that every Player object has a +`walkSpeed` property which can be read and changed. For example you +can triple your own walking speed (from the default 0.2) at the +in-game prompt using the following command... + + /js self.walkSpeed = self.walkSpeed * 3; + +... If we were limited to using Java's notation we would have had to +write `/js self.setWalkSpeed( self.getWalkSpeed() * 3 )` . Since +almost every class in the Bukkit API is also a JavaBean you can access +properties of properties and so on. For example, to get the name of +the world in which a player is located... + + /js self.location.world.name + +... is more concise than `/js self.getLocation().getWorld().getName()`. +If you're new to Java and the [Bukkit API][bukapi] is the first time +you've browsed Java documentation, you may be wondering where the +`location` property came from - the `location` property is "inherited" +from one of the Player class's super-classes (it's ancestors). You'll see the +`getLocation()` method listed under a section titled **Methods +inherited from interface org.bukkit.entity.Entity** in the +[Player][bukpl] javadoc page. + +## Using java.lang package classes + +In Java the following code will print out the `user.dir` and +`user.timezone` properties... + + System.out.println( System.getProperty( "user.dir" ) ); + System.out.println( System.getProperty( "user.timezone" ) ); + +... In Java, any classes in the `java.lang` package don't need to be +prefixed with the package so the `java.lang.System` class can simply +be written as `System`. However, in Javascript classes in the +`java.lang` package need to be fully qualified so you need to write... + + println( java.lang.System.getProperty( "user.dir" ) ); + println( java.lang.System.getProperty( "user.timezone" ) ); + +... the `println()` function is one of the default functions provided +by the JS Engine in Java so there is no need to add the class name +prefix, but for other System class methods you need to explicitly +include the package name e.g. `java.lang.`. If you are using the +System class in a number of statements you can save yourself some +typing by declaring a System variable and using that instead of the +fully-qualified package and class name... + + var System = java.lang.System; + println( System.getProperty( "user.dir" ) ); + println( System.getProperty( "user.timezone" ) ); + +The JS Engine provides an `importPackage()` function which can be used +to import packages. This also saves you having to type full package +names before classes. For example... + + importPackage(java.util); + var hMap = new HashMap(); + hMap.put('name','Walter'); + +... makes all of the classes in the Java Library's `java.util` package +available for use without having to use the `java.util` +prefix. However, importing the `java.lang` package is not recommended +as some of the java.lang classes (e.g. String, Object) conflict with +Javascript Object types. + +## Summary + +When writing modules or plugins in ScriptCraft, you can access and +change JavaBean properties using a simple .{propertyName} notation +instead of using the Java .get{PropertyName}() and .set{PropertyName() +methods. This results in more concise code. This simpler notation is +provided by the Javascript Engine embedded in Java 6 and later +versions. Javascript does not have access to private members, the +.{propertyName} notation is automagically converted to the appropriate +.get{PropertyName}() or .set{PropertyName}() method by Java. + +[bukapi]: http://jd.bukkit.org/beta/apidocs/ +[bukpl]: http://jd.bukkit.org/beta/apidocs/org/bukkit/entity/Player.html +[bukplws]: http://jd.bukkit.org/beta/apidocs/org/bukkit/entity/Player.html#getWalkSpeed() +[buksrv]: http://jd.bukkit.org/beta/apidocs/org/bukkit/Server.html + diff --git a/docs/YoungPersonsGuideToProgrammingMinecraft.md b/docs/YoungPersonsGuideToProgrammingMinecraft.md index db7890b..a41d973 100644 --- a/docs/YoungPersonsGuideToProgrammingMinecraft.md +++ b/docs/YoungPersonsGuideToProgrammingMinecraft.md @@ -1,7 +1,49 @@ + # The Young Person's Guide to Programming in Minecraft -## 2013/01/08 17:26 - -### Introduction +## Table of Contents + * [Introduction](#introduction) + * [Installing and Running SpigotMC](#installing-and-running-spigotmc) + * [Installing ScriptCraft](#installing-scriptcraft) + * [Configuring your Server (optional)](#configuring-your-server-optional) + * [Learning Javascript](#learning-javascript) + * [First Steps](#first-steps) + * [Variables](#variables) + * [Functions](#functions) + * [Building stuff in Minecraft](#building-stuff-in-minecraft) + * [Common Block Materials](#common-block-materials) + * [Dimensions](#dimensions) + * [More shapes](#more-shapes) + * [The Drone Object](#the-drone-object) + * [Movement](#movement) + * [Chaining - combining building and movement.](#chaining---combining-building-and-movement) + * [Exercise - Build a simple dwelling](#exercise---build-a-simple-dwelling) + * [Remembering where you started.](#remembering-where-you-started) + * [Saving your work](#saving-your-work) + * [Your First Minecraft Mod!](#your-first-minecraft-mod) + * [Parameters](#parameters) + * [true or false](#true-or-false) + * [More fun with `true` or `false`](#more-fun-with-true-or-false) + * [Booleans and JavaBeans](#booleans-and-javabeans) + * [SIDENOTE](#sidenote) + * [...and Again, and Again, and Again,...](#and-again-and-again-and-again) + * [Counting to 100](#counting-to-100) + * [Saying "Hi!" to every player](#saying-hi-to-every-player) + * [While Loops](#while-loops) + * [`utils.foreach()` - Yet another way to process Arrays](#utilsforeach---yet-another-way-to-process-arrays) + * [Exercise](#exercise) + * [Putting `for` loops to use - Building a Skyscraper](#putting-for-loops-to-use---building-a-skyscraper) + * [Making Decisions](#making-decisions) + * [Event-Driven programming](#event-driven-programming) + * [Stop listening to events.](#stop-listening-to-events) + * [Keeping Score - Lookup tables in Javascript](#keeping-score---lookup-tables-in-javascript) + * [Counting block break events for each player](#counting-block-break-events-for-each-player) + * [Next Steps](#next-steps) +## Introduction Minecraft is an open-ended 3D game where you can build and craft anything you like. Minecraft can be extended and enhanced using 'Mods' @@ -20,40 +62,52 @@ players connect to a Minecraft Server on the internet or locally ![Cottages created using ScriptCraft in MineCraft][img_cr] -### Installation +# Installation -CraftBukkit is a version of the Minecraft server software which allows -easy addition of 'Mods' and extensions to Minecraft. ScriptCraft is a -'Mod' for use with CraftBukkit. Adding Mods to Minecraft can be -difficult but CraftBukkit makes it easy. Follow these steps to -Install ScriptCraft on your computer... +Before installing ScriptCraft you must first install SpigotMC which is +a special version of Minecraft Server that makes it easy to customize +the game. -1. [Download and install CraftBukkit][dlbuk]. +## Installing and Running SpigotMC -2. [Download the ScriptCraft Mod][sc-plugin]. Then copy it to the -`craftbukkit/plugins` folder you created in step 1. +Follow these steps to download and install SpigotMC. -3. Start the CraftBukkit server. +1. Download Spigot's [BuildTools.jar][spigotdl] +2. Save the BuildTools.jar file to a new directory called spigotmc. +3. Open a terminal (Mac and Linux) or command prompt (windows) window and type `java -jar BuildTools.jar`. This will kick off a long series of commands to "build" SpigotMC. +4. When the build is done, there will be a new file beginning with `spigot` and ending in `.jar` in the spigotmc directory. Run this file by typing `java -jar spigot-1.10.2.jar` (it might not be that exact name - you can list files in the directory by typing `dir` (Windows) or `ls` (Mac and Linux). +5. The server will start up then shut down very shortly afterwards. You'll need to edit a file called `eula.txt` - change `eula=false` to `eula=true` and save the file. +6. Run the `java -jar spigot-1.10.2.jar` command again - this time the server will start up. Shut it down by typing `stop` at the server prompt. -4. In the CraftBukkit command window type `op {your_username}` and hit -enter, replacing {your_username} with your own minecraft -username. This will give you `operator` access meaning you can perform -more commands than are normally available in Minecraft. +## Installing ScriptCraft -5. In the CraftBukkit command window type `js 1 + 1` and hit enter. You should see `> 2` . +Follow these steps to download and install ScriptCraft. -... Congratulations! You just installed your own Minecraft Server with -the ScriptCraft Mod and are now ready to begin programming in Minecraft. +1. Download the [scriptcraft.jar][dl] plugin and save it to the `plugins` directory and restart the server by typing `java -jar spigot-1.10.2.jar`. +2. At the server prompt type `js 1 + 1` and hit enter. The result `2` should be displayed. -Normally, Minecraft Mods are written in Java. This makes writing your -own extension or game rules difficult because you must first learn Java. -Java is different enough from Javascript. With the ScriptCraft plug-in -installed, you don't have to learn Java, you can extend and customize -Minecraft your way using Javascript. Javascript is easier to learn than -Java but it's also more flexible and powerful and is used for creating -interactive web sites and many other applications. +Congratulations - you've just installed your Custom Minecraft Server and are ready to begin writing your first mod! -### Learning Javascript +## Configuring your Server (optional) + +Once you've installed SpigotMC, depending on your specific needs, +you might want to consider setting the following properties in the `server.properties` file: + + # completely flat worlds are best for building from scratch + # bukkit/spigotmc + level-type=FLAT + generate-structures=false + + # creative mode + gamemode=1 + pvp=false + + # turns off authentication (for classroom environments) + online-mode=false + spawn-npcs=false + spawn-monsters=false + +## Learning Javascript To begin creating cool stuff in Minecraft using ScriptCraft, you don't *have* to know much JavaScript. ScriptCraft comes with lots of functions @@ -65,19 +119,25 @@ your friends. If you want to get started learning JavaScript, check out this [fun Javascript Tutorial][ce]. If you want to dive right in to ScriptCraft, read on... -### First Steps +## First Steps If you don't already know Javascript, don't worry, you'll learn a little about Programming and Javascript along the way. You've set up a Minecraft server and are ready to connect ... -1. Launch Minecraft (keep the Bukkit Command window open). +1. Launch Minecraft. 2. Click 'Multi-Player' 3. Click 'Add Server' 4. Type any name you like in the name field then type `localhost` in the address field. `localhost` is a special internet address that points to your own computer. -5. Click 'Join Server' to join the craftbukkit server. +5. Click 'Join Server' to join the server. If the version +of Minecraft is incompatible with the version of the server you will +not be able to connect to the server. To fix this, you can create a +Minecraft profile in your client. Profiles let you decide which +version of Minecraft client you want to run so that your client and +server are compatible. + 6. Once you've joined the game, press the `/` key located at the bottom right of your keyboard. A prompt will appear. Type the following then press enter: `js 1 + 1` The number 2 should be displayed. @@ -85,13 +145,13 @@ press enter: `js 1 + 1` The number 2 should be displayed. ... Well Done! You've just confirmed you can run Javascript code from within the Minecraft Console. -### Variables +## Variables A variable is how you name something for the computer (and you the programmer) to remember. You create a new variable in Javascript using the `var` keyword... - /js var location = "Blackrock Castle" + /js var location = 'Blackrock Castle' ... creates a new variable called `location` and stores the text `Blackrock Castle` in it. Now the computer has a new item in its memory @@ -103,9 +163,9 @@ called `location`. We can use that name like this... Blackrock Castle -...You might be wondering where the `""` (called double-quotes) went. -When telling the computer to store some text, you have to put `"` -(that's the double-quote character - press Shift+2) at the start and end +...You might be wondering why there's no enclosing `'` single quotes. +When telling the computer to store some text, you have to put `'` +(that's the single-quote character) at the start and end of the text. The computer doesn't store these quote characters, only the text between them. The computer will store the variables while the Minecraft Server is running. Repeat the last command you entered by @@ -114,13 +174,13 @@ pressing enter. You can repeat that statement as many times as you like and the computer will always display the same value. You can change the value like this... - /js location = "Mahon Point" + /js location = 'Mahon Point' ...notice this time I didn't use the `var` keyword. I didn't need to. The `var` keyword is only needed when you first create the variable. Now execute this command... - /js echo( location ) + /js echo( self, location ) ...and it displays... @@ -128,18 +188,18 @@ execute this command... Variables can be created and changed easily in Javascript. Along with the variables you'll create in your in-game commands and scripts, there -are handy variables created for you by ScriptCraft. One such variable is -`self`, it contains information about the current player... +are handy *free* variables created for you by ScriptCraft. One such variable is +`self`, it contains information about the current player (that's you)... - /js echo ( self ) + /js echo ( self, self.name ) -... displays the following... +... displays something like the following... - CraftPlayer{name=walterh} + walterh ... for me but the message displayed will be different for every player. -### Functions +## Functions ScriptCraft comes with a couple of extra functions not normally found in Javascript. These functions will help you build new structures and @@ -149,7 +209,7 @@ looking at the building functions let's look at the `echo()` function. `echo()` - as its name implies - will echo back at you whatever you tell it. For example, type ... - /js echo('Hello') + /js echo( self, 'Hello') ... and the game will display... @@ -157,7 +217,7 @@ tell it. For example, type ... ... type ... - /js echo( 5 + 7 ) + /js echo( self, 5 + 7 ) ... and the game will display... @@ -167,7 +227,7 @@ tell it. For example, type ... don't recommend it. Homework and Minecraft don't mix! The `echo()` function will display anything you tell it to - Text, Numbers and other types... - /js echo( new Date() ) + /js echo( self, new Date() ) ... prints today's date. If the statement above looks confusing - don't worry - `new Date()` creates a new date object - I'll talk about objects @@ -216,7 +276,7 @@ called. You'll notice the above statement didn't actually do anything ... The current time is displayed. Congrats! You've just written your first Javascript function - you're well on your way to becoming a -Minecraft Modder :-) There are many functions for working with Text, +Minecraft Modder. There are many functions for working with Text, numbers and dates in Javascript... /js Math.random() @@ -227,7 +287,7 @@ in-game console. You'll see the number displayed is different each time. Think of Math.random() as a Dice with many many sides. You can rely on it to never return the same value twice. -### Building stuff in Minecraft +## Building stuff in Minecraft Now we get to the fun stuff - creating structures and buildings in Minecraft. Building by hand is fun but tedious when you want to build @@ -247,38 +307,53 @@ function. You must tell the function what material you want the shape to be made of. For example, in the game, point the cross hairs at the ground, then type the following and hit enter... - /js box("5") + /js box( blocks.oak ) -... This will change the targeted block to wood. What's happened here is -the `box()` function has created a single new wooden block. The text -`"5"` is taken by Minecraft to mean Wood. You can see many more -materials and the number Minecraft uses for them by visiting the -[Minecraft Data Values][mcdv] site. +... This will change the targeted block to wood. What's happened here +is the `box()` function has created a single new wooden +block. `blocks` is another one of those *free* variables you get in +ScriptCraft, you can see a list of block materials by typing ... -### Common Block Materials + /js blocks. + +... then pressing the `TAB` key. Repeatedly pressing the `TAB` key +will cycle through all of the block materials. Alternatively, you can +see many more current materials and the numbers Minecraft uses for +them by visiting the [Minecraft Data Values][mcdv] site. + +## Common Block Materials In Minecraft Programming, Materials aren't known by their name, instead numbers (sometimes 2 numbers) are used to indicate which material should be used. For example the number 2 is grass, 1 is cobblestone etc, while 5 is wood (oak). There are different types of -wood so the text "5:1" means Spruce, "5:2" means Birch and "5:3" means +wood so the text '5:1' means Spruce, '5:2' means Birch and '5:3' means Jungle wood. There are many different materials in the Minecraft world, the most commonly used materials for building are: - * "4" - Cobblestone - * "5" - Wooden Planks - * "5:2" - Birch wood Planks (light wood) - * "98" - Stone bricks - * "45" - Red bricks - * "68" - Doors - * "102" - Glass panes (for windows) - -For reference, here is a chart of all of the blocks (not items) in the Minecraft -world... + * '4' - Cobblestone or `blocks.cobblestone` + * '5' - Wooden Planks or `blocks.oak` + * '5:2' - Birch wood Planks (light wood) or `blocks.birch` + * '98' - Stone bricks or `blocks.brick.stone` + * '45' - Red bricks or `blocks.brick.red` + * '68' - Sign or `blocks.sign` + * '102' - Glass panes (for windows) or `blocks.glass_pane` + +You can create a single wooden block using the numeric values or the `blocks` variable. For example... + + /js box( '5' ) + +... and ... + + /js box( blocks.oak ) + +... both do exactly the same thing but I personally prefer `/js box( +blocks.oak )` because it's easier to remember. For reference, here is +a chart of all of the blocks (not items) in the Minecraft world... ![Minecraft Data Values][img_dv] -### Dimensions +## Dimensions `box()` can do more than just create single blocks - it can create cubes and cuboids of any @@ -293,7 +368,7 @@ away you want something to extend. ![Width, Height and Depth][img_whd] -### More shapes +## More shapes * `box0( block, width, height, depth )` - creates an empty box (with the insides hollowed out - perfect for dwellings. `box0` will remove both @@ -306,7 +381,7 @@ away you want something to extend. * `prism( block, width, depth )` - creates a Prism - good for roofs. * `prism0( block, width, depth )` - creates an empty prism. -### The Drone Object +## The Drone Object ScriptCraft is a Minecraft Mod that lets you execute Javascript code in the game. It also lets you write your own Mod in Javacript. One @@ -336,24 +411,24 @@ Drone is easy... make the Drone turn twice so that it is facing in the opposite direction. -#### Chaining - combining bulding and movement. +### Chaining - combining building and movement. You can make a Drone move around before and after building by *daisy-chaining* the building and movement functions together. In the game, point at the ground then type the following... - /js up(1).box(5).fwd(3).box(5) + /js up(1).box( blocks.oak ).fwd(3).box( blocks.oak ) A series of 2 boxes is created 3 blocks apart. ![Two Boxes 3 blocks apart][img_2boxes] -### Excercise - Build a simple dwelling +### Exercise - Build a simple dwelling OK. You know enough now about the `Drone` functions to be able to build a simple dwelling. The dwelling should be a hollow building with a sloped roof. *Don't worry about doors or windows for now*. The walls -should be made of Cobblestone ("4") and the roof made of wood ("5"). You can use +should be made of Cobblestone ('4') and the roof made of wood ('5'). You can use the following `Drone` functions to create a dwelling 7 blocks wide by 3 blocks high by 6 blocks long with a wooden sloped roof. It's up to you to figure out how. @@ -364,21 +439,21 @@ to you to figure out how. Your dwelling should end up looking something like this... -![Excercise Dwelling][img_ed] +![Exercise Dwelling][img_ed] ### Remembering where you started. Sometimes when you're building something big that requires lots of manoeuvering by your Drone, you need to leave breadcrumbs as you go so your `Drone` can return to where it started. Every new Drone has a -`"start"` checkpoint that it can return to by executing -`move("start")` ... +`'start'` checkpoint that it can return to by executing +`move('start')` ... - /js box("5").up(3).left(4).box("1").turn(3).fwd(5).right().box("1").move("start") + /js box('5').up(3).left(4).box('1').turn(3).fwd(5).right().box('1').move('start') ... A genius would have trouble figuring out how to get back to where they started. Fortunately, they don't have to - the -`move("start")` function will take the Drone back to its starting +`move('start')` function will take the Drone back to its starting point. * `chkpt( breadCrumb )` - Leaves a mark at your Drone's current @@ -395,7 +470,7 @@ Both `chkpt()` and `mark()` are useful for when you want to build complex things that require your Drone to move about a lot ( for example, Castles, mansions, palaces, etc). -### Saving your work +## Saving your work You can build cool things using the in-game command-prompt and the `/js` command but sooner or later you'll probably want to build @@ -406,31 +481,36 @@ again when you quit the game and start it up again. installed on every Windows machine) that is well suited for writing code. If you don't already have it on your machine, you can [install Notepad++ here][np]. I recommend using NotePad++ rather than plain old -Notepad because it understands Javascript. +Notepad because it understands Javascript. If you prefer coding on a +Macintosh, then [TextWrangler][twl] is a good programming editor which +also understands Javascript code. -### Your First Minecraft Mod! +## Your First Minecraft Mod! So, You've learnt a little bit about Javascript and what the Drone() object can do, let's use that knowledge to create a Minecraft Mod! Once you've installed Notepad++, Launch it, create a new file and type the following... - function greet(){ - echo("Hi " + self.name); - } +```javascript +exports.greet = function( player ) { + echo( player, 'Hi ' + player.name); +} +``` ... then save the file in a new directory -`craftbukkit/js-plugins/{your_name}` (replace {your_name} with your -own name) and call the file `greet.js` (be sure to change the file-type -option to '*.* All Files' when saving or NotePad++ will add a '.txt' -extension to the filename. Now switch back to the Minecraft game and -type... +`scriptcraft/plugins/{your_name}` (replace +{your_name} with your own name) and call the file `greet.js` (be sure +to change the file-type option to '*.* All Files' when saving or +NotePad++ will add a '.txt' extension to the filename. Now switch back +to the Minecraft game and type... - /reload + /js refresh() -... to reload all of the server plugins. Your mod has just been loaded. Try it out by typing this command... +... to reload all of the server plugins. Your mod has just been +loaded. Try it out by typing this command... - /js greet() + /js greet(self) ... it should display ... @@ -440,12 +520,36 @@ type... minecraft username. Congratulations - You've just written your very first Minecraft Mod! With ScriptCraft installed, writing Minecraft Mods is as simple as writing a new javascript function and saving it -in a file in the js-plugins directory. This function will now be -avaible every time you launch minecraft. +in a file in the scriptcraft/plugins +directory. This function will now be avaible every time you launch +minecraft. This is a deliberately trivial minecraft mod but the +principles are the same when creating more complex mods. -#### Parameters +The `exports` variable is a special variable you can use in your mod +to provide functions, objects and variables for others to use. If you +want to provide something for other programmers to use, you should +*export* it using the special `exports` variable. The syntax is +straightforward and you can use the same `exports` variable to export +one or more functions, objects or variables. For example... + +#### thrower.js + +```javascript +exports.boo = function(player){ + echo( player, 'Boo!'); +} +exports.yo = function(player){ + echo( player, 'Yo!'); +} +``` + +... is a plugin which provides 2 javascript functions called `boo()` +and `yo()` which can be invoked from the in-game prompt like +this `/js boo(self)` or `/js yo(self)`. + +## Parameters If you want to change the `greet()` function so that it displays a -greeting other than "Hi " you can change the code in the `greet()` +greeting other than 'Hi ' you can change the code in the `greet()` function, or better still, you can use *Parameters*. Parameters are values you provide to a function so that the function behaves differently each time it is called. @@ -454,18 +558,20 @@ differently each time it is called. Change the `greet()` function so that it looks like this... - function greet( greeting ) { - echo( greeting + self.name ); - } +```javascript +exports.greet = function ( greeting , player) { + echo( player, greeting + player.name ); +} +``` -... Save your greet.js file and issue the /reload command in +... Save your greet.js file and issue the `/js refresh()` command in minecraft. Now enter the following command in Minecraft... - greet("Hello "); + greet('Hello ',self); ... Now try ... - greet("Dia Dhuit "); + greet('Dia Dhuit ',self); ... you should see the following messages in your chat window... @@ -476,7 +582,7 @@ minecraft. Now enter the following command in Minecraft... they're called. As you'll see later, Parameters are very useful when changing the behaviour of MineCraft. -### true or false +## true or false Try entering each of the following statements and make a note of the answers given by minecraft... @@ -520,63 +626,88 @@ things... ... try comparing some more numbers yourself - say for example, compare the ages of your friends or siblings to your own age. -#### More fun with `true` or `false` -You can find out if you can Fly in minecraft by typing the following statement... +## More fun with `true` or `false` - /js self.allowFlight +You can find out if you can Fly in minecraft by typing the following statement ... + + /js self.allowFlight ... the result will be `true` or `false` depending on whether you can fly or not. You can turn on and off your ability to fly by setting -your `allowFlight` property to `true` or `false`. Try it... +your `allowFlight` property to `true` or `false`. Try it ... - /js self.allowFlight = true + /js self.allowFlight = true; -... Now you can fly! To turn off flight... +... Now you can fly! Double-press the space bar key to start flying. To turn off flight ... - /js self.allowFlight = false + /js self.allowFlight = false; ... and you come crashing down to earth. This is just one example of -how `true` and `false` are used throughout ScriptCraft - these are -called `boolean` values - named after [George Boole][boole], a 19th Century +how `true` and `false` are used throughout ScriptCraft – these are +called `boolean` values – named after [George Boole][boole], a 19th Century Maths Professor at University College Cork. There are plenty more -examples of boolean values in Minecraft. You can find out if monsters -are allowed in your minecraft world by typing the following -statement... +examples of boolean values in Minecraft. You can find out if it's +raining in your minecraft world by typing the following statement ... - /js self.location.world.allowMonsters + /js self.world.hasStorm() -... The result of this statement will be either `false` (Phew!) or -`true` (Yikes!) depending on how your server has been -configured. However, typing the following statement doesn't work as -expected... +... The result of this statement will be either `false` (if it's not raining) or +`true` (if it *is* raining). If it's raining, you can make it stop raining typing the following command: - /js self.location.world.allowMonsters = true + /js self.world.setStorm(false) -... This statement won't work as expected - it will give an Error -message. This is because sometimes we can read variables but we can't -change them the same way we read them (this is because of how -Javascript, Java and the CraftBukkit API work together). To turn on or -off the spawning of monsters, type the following... +... Similarly, to make it start raining you can issue the following command: - /js self.location.world.setSpawnFlags(false, true) + /js self.world.setStorm( true ) -... the `setSpawnFlags()` method takes 2 parameters, the first -parameter says whether or not monsters can spawn, and the 2nd says -whether or not Animals can spawn. (SIDENOTE: You may be wondering how -to change other aspects of the Minecraft game - pretty much all +### Booleans and JavaBeans + +There are many *boolean* properties you can use to turn on or off +certain game behaviours. For example, the *thundering* behavior is turned +on or off using the World's `thundering` property. The World object's +properties and methods are [documented on the SpigotMC JavaDocs World +page][spworld]. When browsing the SpigotMC JavaDoc pages, whenever +you see a method whose name begins with `is` such as `isThundering()` and +a companion method `setThundering()`, these methods are called *JavaBean* +methods - the *thundering* property is a *JavaBean* property and there +are two ways you can use JavaBean properties in Javascript. You can +*get* and *set* the property using the methods provided by Java. To +*get* the thundering property you can call the JavaBean Method: + + /js self.world.isThundering() + +... or you can get the property like this: + + /js self.world.thundering + +To *set* the thundering property, you can call the JavaBean method: + + /js self.world.setThundering( true ) + +... or you can set the property like this: + + /js self.world.thundering = true + +Whatever approach you use, the result will be the same. + +[cmworld]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/world/World.html +[spworld]: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/World.html + +### SIDENOTE +You may be wondering how to change other aspects of the Minecraft game - pretty much all aspects of the game can be changed. Changes are made using what are called `API` calls - these are calls to functions and methods in -Minecraft - you can read more about these on the [CraftBukkit API -Reference][cbapi].) +Minecraft - you can read more about these on the [SpigotMC API +Reference][spigotapi]. -### ...and Again, and Again, and Again,... +## ...and Again, and Again, and Again,... One of the things Computers are really good at is repetition. Computers don't get tired or bored of doing the same thing over and over again. Loops are handy, if you want to run the same code over and over again, each time with a different value. -#### Counting to 100 +### Counting to 100 At the in-game command prompt (hint: press 't') type the following then hit Enter... @@ -604,26 +735,28 @@ The `for` statement is useful when you want to repeat something over and over. I remember, an Array is just a list of things, for example - the players connnected to a server, the worlds of a server and so on. -#### Saying "Hi!" to every player +### Saying "Hi!" to every player At the in-game command prompt type the following then hit Enter... - /js for (var i = 0;i < server.onlinePlayers.length; i++){ server.onlinePlayers[i].sendMessage("Hi!"); } + /js var utils = require('utils'); + /js var players = utils.players(); + /js for (var i = 0;i < players.length; i++){ echo(players[i], 'Hi!'); } ... Lets look at these statements in more detail. We had to enter the statements on a single line at the in-game command prompt but the statements could be written like this... - var players = server.onlinePlayers; - for (var i = 0; i < players.length; i++) { - var player = players[i]; - player.sendMessage("Hi!"); - } +```javascript +var utils = require('utils'); +var players = utils.players(); +for (var i = 0;i < players.length; i++) { + echo(players[i], 'Hi!'); +} +``` -... On the first line, a new variable `players` is created from the -server object's onlinePlayers property. `players` is more concise and -easier to type than the long-winded `server.onlinePlayers`. On the -second line, the for loop is declared, a counter variable `i` is set +... On the 2nd line, a new variable `players` is created and assigned a value by calling utils.players(). +On the next line, the for loop is declared, a counter variable `i` is set to 0 (zero - arrays in javascript start at 0 not 1) and each time around the loop is tested to see if it's less than the number of players online. At the end of each run around the loop the `i` @@ -647,31 +780,38 @@ function. Open the `hi.js` file you created earlier (using NotePad++ , TextWrangler or your editor of choice) and add the following code at the bottom of the file... - function hiAll(){ - var players = server.onlinePlayers; - for (var i = 0; i < players.length; i++) { - var player = players[i]; - player.sendMessage("Hi!"); - } - } +```javascript +var utils = require('utils'); +exports.hiAll = function () { + var players = utils.players(); + player, + i; + for ( i = 0; i < players.length; i++) { + player = players[i]; + echo( player, 'Hi!' ); + } +} +``` -... save the file, at the in-game command prompt type `reload` and +... save the file, at the in-game command prompt type `/js refresh()` and then type `/js hiAll()`. This will send the message `Hi!` to all of the players connected to your server. You've done this using a `for` loop and arrays. Arrays and `for` loops are used heavily in all types of software, in fact there probably isn't any software that doesn't use `for` loops and Arrays to get things done. -#### While Loops +## While Loops Another way to repeat things over and over is to use a `while` loop. The following `while` loop counts to 100... - var i = 1; - while (i <= 100){ - echo( i ); - i = i + 1; - } +```javascript +var i = 1; +while ( i <= 100 ) { + console.log( i ); + i = i + 1; +} +``` A `while` loop will repeat until its condition is `false` - the condition in the above example is `i <= 100` so while i is less than @@ -693,19 +833,22 @@ Just like `for` loops, `while` loops can be also be used to loop through arrays. The following loop prints out all of the players on the server... - var players = server.onlinePlayers; - var i = 0; - while ( i < players.length ) { - echo( players[i] ); - i = i + 1; - } +```javascript +var utils = require('utils'); +var players = utils.players(); +var i = 0; +while ( i < players.length ) { + console.log( players[i] ); + i = i + 1; +} +``` ... whether you chose to use a `for` loop or a `while` loop is largely a matter of personal taste, `for` loops are more commonly used with Arrays but as you see from the example above, `while` loops can also loop over Arrays. -#### `utils.foreach()` - Yet another way to process Arrays +## `utils.foreach()` - Yet another way to process Arrays Both the `for` statement and `while` statement are standard commonly used javascript statements used for looping. ScriptCraft also comes @@ -718,56 +861,66 @@ loops. utils.foreach() takes two parameters... 2. A function which will be called for each item in the array. ...that's right, you can pass functions as parameters in javascript! -Let's see it in action, the following code will `echo()` (print) the -name of each online player... +Let's see it in action, the following code will `console.log()` (print) the +name of each online player in the server console window... - utils.foreach( server.onlinePlayers, echo ); + var utils = require('utils'); + var players = utils.players; + utils.foreach( players, console.log ); ... in the above example, the list of online players is processed one -at a time and each item (player) is passed to the `echo` -function. Note here that I used `echo` not `echo()`. The round braces +at a time and each item (player) is passed to the `console.log` +function. Note here that I used `console.log` not `console.log()`. The round braces () are used to call the function. If I want to pass the function as a parameter, I just use the function name without the round braces. The -above example uses a named function which already exists ( `echo` ), +above example uses a named function which already exists ( `console.log` ), you can also create new functions on-the-fly and pass them to the utils.foreach() function... - /* - give every player the ability to fly. - */ - utils.foreach( server.onlinePlayers, - function (player) { - player.setAllowFlight(true); - } - ); +```javascript +/* + give every player the ability to fly. +*/ +var utils = require('utils'); +var players = utils.players(); +utils.foreach( players, function( player ) { + player.capabilities.flying = true; + player.updateCapabilities(); +} ); +``` ... Another example, this time each player will hear a Cat's Meow... - /* - Play a Cat's Meow sound for each player. - */ - utils.foreach( server.onlinePlayers, - function (player) { - player.playSound(player.location, - org.bukkit.Sound.CAT_MEOW, - 1, - 1); +```javascript +/* + Play a Cat's Meow sound for each player. +*/ +var utils = require('utils'); +var players = utils.players(); +var sounds = require('sounds'); +utils.foreach( players, function( player ) { + sounds.entityCatAmbient( player ); // spigot 1.9 + /* canarymod only + sounds.catMeow( player ); + */ +} ); +``` - } - ); - -#### Exercise +### Exercise Try changing the above function so that different sounds are played -instead of a Cat's Meow. You'll need to lookup the [CraftBukkit API's -Sound class][soundapi] to see all of the possible sounds that can be -played. +instead of a Cat's Meow. To see all of the possible sounds that can be +played, load the sounds module at the in-game prompt using the following statement: + + /js var sounds = require('sounds'); + +... then type `/js sounds.` and press the TAB key to see a list of all possible sounds. Loops are a key part of programming in any language. Javascript provides `for` and `while` statements for looping and many javascript libraries also provide their own custom looping functions. You should use what you feel most comfortable with. -#### Putting `for` loops to use - Building a Skyscraper +## Putting `for` loops to use - Building a Skyscraper For loops can be used to build enormous structures. In this next exercise I'm going to use a for loop to build a skyscraper. This @@ -786,29 +939,37 @@ pointing at the block, type the following into the in-game prompt... ... A skyscraper with just a single floor isn't much of a skyscraper so the next step is to repeat this over and over. This is where `for` loops come in. Open your favorite text editor and create a new file in -your js-plugins/{your-name} directory called `myskyscraper.js`, then -type the following... +your scriptcraft/plugins/{your-name} directory, name the file `myskyscraper.js`, then +type the following code and save: - function skyscraper(floors) - { - floors = floors || 10; // default number of floors is 10 - this.chkpt('skyscraper'); // saves the drone position so it can return there later - for (var i = 0; i < floors; i++) - { - this.box(blocks.iron,20,1,20).up().box0(blocks.glass_pane,20,3,20).up(3); - } - return this.move('skyscraper'); // return to where we started - }; +```javascript +function myskyscraper( floors ) { + var i ; + if ( typeof floors == 'undefined' ) { + floors = 10; + } + // bookmark the drone's position so it can return there later + this.chkpt('myskyscraper'); + for ( i = 0; i < floors; i++ ) { + this + .box(blocks.iron,20,1,20) + .up() + .box0(blocks.glass_pane,20,3,20) + .up(3); + } + // return the drone to where it started + this.move('myskyscraper'); +}; +var Drone = require('drone'); +Drone.extend( myskyscraper ); +``` - load("../drone/drone.js"); - Drone.extend('skyscraper',skyscraper); - -... so this takes a little explaining. First I create a new function -called skyscraper that will take a single parameter `floors` so that -when you eventually call the `skyscraper()` function you can tell it +So this takes a little explaining. First I create a new function +called myskyscraper that will take a single parameter `floors` so that +when you eventually call the `myskyscraper()` function you can tell it how many floors you want built. The first statement in the function -`floors = floors || 10;` just sets floors to 10 if no parameter is -supplied. The next statement `this.chkpt('myskyscraper')` just saves +`if (typeof floors == 'undefined'){ floors = 10; }` sets floors to 10 if no parameter is +supplied. The next statement `this.chkpt('myskyscraper')` saves the position of the Drone so it can eventually return to where it started when finished building (I don't want the drone stranded atop the skyscraper when it's finished). Then comes the `for` loop. I loop @@ -817,12 +978,12 @@ floor. When the loop is done I return the drone to where it started. The last 2 lines load the drone module (it must be loaded before I can add new features to it) and the last line extends the 'Drone' object so that now it can build skyscrapers among other things. Once you've -typed in the above code and saved the file, type `reload` in your -in-game prompt, then type ... +typed in the above code and saved the file, type `/js refresh()` in your +in-game prompt, then type: - /js skyscraper(2); + /js myskyscraper(2); -... A two-story skyscraper should appear. If you're feeling +A two-story skyscraper should appear. If you're feeling adventurous, try a 10 story skyscraper! Or a 20 story skyscraper! Minecraft has a height limit (256 blocks from bedrock) beyond which you can't build. If you try to build higher than this then building @@ -836,71 +997,282 @@ that out, creating an entire city of blocks of skyscrapers is the next logical step. Of course, Minecraft doesn't have the same constraints as real-world densely populated areas so let your imagination go wild. - -### Making Decisions +## Making Decisions All the programs we have seen so far have been fairly predictable - they went straight through the statements, and then went back to the beginning again. This is not very useful. In practice the computer would be expected to make decisions and act accordingly. The javascript statement used for making decisions is `if`. -While standing on the ground in-game, type the following at the command prompt... +While standing on the ground in-game, type the following at the command prompt: - /js if ( self.flying ) { echo("Hey, You are flying!"); } + /js if ( self.onGround ) { echo('You are not flying!'); } -... No message should appear on screen. That is - `Hey, You are -flying!` should *not* appear on screen. Now double-tap the `space` -bar to start flying in-game (tap the space bar twice in rapid +the following message should have appeared on your screen: + + You are not flying! + +Now double-tap the `space` bar to start flying in-game (tap the space bar twice in rapid succession), then press and hold space to rise above the ground. Now enter the same statement again (If you don't want to type the same statement again, just press `/` then press the `UP` cursor key on your keyboard, the statement you entered previously should reappear. - /js if ( self.flying ) { echo("Hey, You are flying!"); } + /js if ( self.onGround ) { echo('You are not flying!'); } -... this time the following message should have appeared on your screen... - - Hey, You are flying! +This time no message should appear on your screen. The `if` statement tests to see if something is `true` or `false` and if `true` then the block of code between the curly braces ( `{` and `}` ) is executed - but only if the condition is true. The condition -in the above example is `self.flying` which will be `true` if you are -currently flying or `false` if you aren't. +in the above example is `!self.onGround` (self is _not_ on ground) which +will be `true` if you are currently flying or `false` if you aren't. What if you wanted to display a message only if a condition is *not* -true ? For example to only display a message if the player is *not* -flying... +true ? For example to only display a message if the player is *not* on the ground: - /js if ( ! self.flying ) { echo ("You are not flying."); } + /js if ( !self.onGround ) { echo ('You are flying!'); } -... This code differs in that now there's a `!` (the exclamation mark) -before `self.flying`. The `!` symbol negates (returns the opposite of) +This code differs in that now there's a `!` (the exclamation mark) +before `self.onGround`. The `!` symbol negates (returns the opposite of) whatever follows it. What if you want to display a message in both cases - whether you're flying or not? This is where the `if - else` construct comes in handy. Open your favorite editor and type the following code into a new file -in your js-plugins directory... +in your scriptcraft/plugins directory... - function flightStatus() - { - if ( self.flying ) - { - echo( "Hey, You are flying!" ); - } - else - { - echo( "You are not flying." ); - } - } +```javascript +exports.flightStatus = function( player ) { + if ( player.onGround ) { + echo(player, 'You are not flying!' ); + } else { + echo(player, 'Hey, You are flying!' ); + } +} +``` -... now type `/reload` at the in-game prompt then type `/js -flightStatus()` and an appropriate message will appear based on +... now type `/js refresh()` at the in-game prompt then type `/js +flightStatus(self)` and an appropriate message will appear based on whether or not you're currently flying. Type the `/js flightStatus()` command while on the ground and while flying. The message displayed in each case should be different. -### Next Steps +## Event-Driven programming + +So far we've written code which executes when you invoke the `/js ` +command. What if - for example - you want to have some special +behaviour which occurs when a player joins the game? What if you +wanted to display a custom welcome message (in addition to the MotD - +message-of-the-day which is configurable in your server.properties +file) ? This is where *Event-Driven Programming* comes +in. Event-Driven Programming is just a fancy way of saying 'Do this +when that happens' where 'this' is a function you define, and 'that' +is some event which occurs. There are hundreds of events in the +minecraft game... + + * Every time someone joins the server - that's an event! + * Every time someone breaks a block - that's an event! + * Every time someone shoots an arrow - that's an event! and so on... + +You can write a function which will be called whenever a specific type +of event occurs, it's probably best to illustrate this by example. The +following code sends a message to any player who breaks a block in the +game... + +```javascript +function myBlockBreakHook( event ){ + var breaker = event.player; + echo( breaker, 'You broke a block'); +} +events.blockBreak( myBlockBreakHook ); +``` + +The `events.blockBreak()` function is just one of the many `events` functions which can be used to *register* a function to be called whenever a particular type of event occurs. In the +above code the blockBreak function takes as a parameter a function +I want to be called when that event occurs. The function I want called +in turn takes 1 parameter. The `event` object has all the information +about the event which just occurred. I can tell who broke the block +and send a message to the player. The important thing to note is that +the `myBlockBreakHook` function defined above will not be called until a player breaks a +block. Try it - save the above code in a new file in the +`scriptcraft/plugins` directory then type `/js refresh()` to reload +scriptcraft. Then break a block in the game and you should see the +message 'You broke a block'. + +There are many types of events you can listen for in Minecraft. You can +browse [all possible event registration functions][spevts2] in the API Reference. + +For custom events (events which aren't in the org.bukkit.event tree) +just specify the fully qualified class name instead. E.g. ... + + events.on ( net.yourdomain.events.YourEvent, function( event ) { + ... + }); + +### Stop listening to events. + +If you want an event handler to only execute once, you can remove the handler like this... + +```javascript +function myBlockBreakHook( evt ) { + var breaker = evt.player; + echo( breaker, 'You broke a block'); + this.unregister(); +} +events.blockBreak( myBlockBreakHook ); +``` + +The `this.unregister();` statement will remove this function from the +list of listeners for the event. The `this` keyword when used inside +an event handling function refers to a Listener object provided by +ScriptCraft, it has a single method `unregister()` which can be used +to stop listening for events. + +To unregister a listener *outside* of the listener function... + +```javascript +function myBlockBreakHook( evt ){ + var breaker = evt.player; + echo( breaker, 'You broke a block'); +} +var myBlockBreakListener = events.blockBreak( myBlockBreakHook ); +... +myBlockBreakListener.unregister(); +``` +## Keeping Score - Lookup tables in Javascript + +In the *Event-Driven Programming* section, I defined a function which +displayed a message to players every time they broke a block. Imagine +if I wanted to keep a count of how many blocks each player has broken? +This is where Javascript's Objecct literals come in handy. An object +literal in javascript is simply a way of creating a new Object +on-the-fly in your code. This is an example... + + var myNewObject = { name: 'walter', country: 'Ireland' }; + +... I created a new object with two properties 'name' and +'country'. The notation used to create this object is called JSON +which is short for JavaScript Object Notation. If I want to find out +the 'country' property of the myNewObject variable there are a few +ways I can do it... + + var playerCountry = myNewObject.country; + +... or ... + + var playerCountry = myNewObject['country'] + +... JavaScript lets you access any object property using either +dot-notation ( `object.property` ) or by index ( `object['property']` +). The result in both cases is the same - playerCountry will be +'Ireland'. When accessing the object by indexing, the property doesn't +even have to be a string literal - it can be a variable like this... + + var propertyName = 'country'; + var propertyValue = myNewObject[propertyName]; + +... in the above example, the propertyName variable is used when +indexing. What this means is that every object in JavaScript can act +like a lookup table. What's a lookup table? A table you 'look up' of +course. This is a table of names and scores... + + Name Score + -------- ----- + walter 5 + tom 6 + jane 8 + bart 7 + +... If I want to find Jane's score, I look *down* the list of names in +the name column until I find 'jane' then look *across* to get her +score. In Javascript, an object which stored such a table would look +like this... + +```javascript +var scoreboard = { + walter: 5, + tom: 6, + jane: 8, + bart: 7 +}; +``` + +... and if I wanted to write a function which took a player name as a +parameter and returned their score, I'd do it like this... + +```javascript +function getScore(player){ + return scoreboard[ player ]; +} +``` + +... I might call such a function like this... + +```javascript +var janesScore = getScore('jane'); // returns 8 +``` + +... putting it all together, a hypothetical scoreboard.js mdoule might +look something like this... + +```javascript +var utils = require('utils'); +var scores = {}; + +exports.initialise = function(names){ + scores = {}; + utils.foreach(names, function(name){ + scores[name] = 0; + }); +}; + +/* + changes score by diff e.g. to add 6 to the player's current score + updateScore('walter',6); // walter's new score = 5 + 6 = 11. +*/ +exports.updateScore = function(name, diff){ + scores[name] += diff; +}; + +exports.getScore = function(name){ + return scores[name]; +}; +``` + +## Counting block break events for each player + +I can use a Javascript lookup table (a plain old Javascript object) to +keep a count of how many blocks each player has broken ... + +#### block-break-counter.js + +```javascript +var breaks = {}; + +/* + every time a player joins the game reset their block-break-count to 0 +*/ +function initializeBreakCount( event ){ + breaks[event.player.name] = 0; +} +events.playerJoin( initializeBreakCount ); + +/* + every time a player breaks a block increase their block-break-count +*/ +function incrementBreakCount( event ){ + breaks[event.player.name] += 1; // add 1 + var breakCount = breaks[event.player.name]; + echo( event.player, 'You broke ' + breakCount + ' blocks'); +} +events.blockBreak( incrementBreakCount ); +``` + +With a little more work, you could turn this into a game where players +compete against each other to break as many blocks as possible within +a given time period. + +## Next Steps This guide is meant as a gentle introduction to programming and modding Minecraft using the Javascript Programming Language. @@ -910,25 +1282,32 @@ covered here. If you want to dive deeper into programming and modding minecraft, I recommend reading the accompanying [ScriptCraft API reference][api] which covers all of the ScriptCraft functions, objects and methods. I also recommend reading the source code to some of the -existing scriptcraft add-ons, the *chat* module ( -`js-plugins/chat/chat.js` ) is a good place to start, followed by -[Anatomy of a ScriptCraft Plug-in][ap]. The online [Craftbukkit API -Reference][cbapi] provides lots of valuable information about the +existing scriptcraft plugins, followed by +[Anatomy of a ScriptCraft Plug-in][ap]. The online [SpigotMC API +Reference][spigotapi] provides lots of valuable information about the different objects and methods available for use by ScriptCraft. -[buk]: http://wiki.bukkit.org/Setting_up_a_server -[dlbuk]: http://dl.bukkit.org/ -[sc-plugin]: files/scriptcraft/ +[cmadmin]: https://github.com/walterhiggins/canarymod-admin-guide/ +[dlbuk2]: http://dl.bukkit.org/downloads/craftbukkit/ +[dlcm]: http://canarymod.net/releases +[bii]: http://wiki.bukkit.org/Setting_up_a_server +[sc-plugin]: http://scriptcraftjs.org/download/ [ce]: http://www.codecademy.com/ [mcdv]: http://www.minecraftwiki.net/wiki/Data_values [np]: http://notepad-plus-plus.org/ [cbapi]: http://jd.bukkit.org/beta/apidocs/ +[cmapi]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/ +[spigotapi]: https://hub.spigotmc.org/javadocs/spigot/ [boole]: http://en.wikipedia.org/wiki/George_Boole -[soundapi]: http://jd.bukkit.org/beta/apidocs/org/bukkit/Sound.html -[ap]: http://walterhiggins.net/blog/ScriptCraft-1-Month-later -[api]: api.md - +[soundapi]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/world/effects/SoundEffect.Type.html +[ap]: Anatomy-of-a-Plugin.md +[api]: API-Reference.md +[twl]: http://www.barebones.com/products/textwrangler/ +[bkevts]: http://jd.bukkit.org/dev/apidocs/org/bukkit/event/package-summary.html +[cmevts]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/package-summary.html +[cmevts2]: API-Reference.md#events-helper-module-canary-version +[spevts2]: API-Reference.md#events-helper-module-spigotmc-version [img_echo_date]: img/ypgpm_echo_date.png [img_3d_shapes]: img/ypgpm_3dshapes.jpg [img_whd]: img/ypgpm_whd.jpg @@ -940,5 +1319,3 @@ different objects and methods available for use by ScriptCraft. [img_ssf]: img/skyscraper_floor.png [img_ss]: img/skyscraper.png -## Categories -Minecraft, Programming, ScriptCraft diff --git a/docs/api.md b/docs/api.md deleted file mode 100644 index 6125c04..0000000 --- a/docs/api.md +++ /dev/null @@ -1,1466 +0,0 @@ -ScriptCraft API Reference -========================= - -Walter Higgins - -[walter.higgins@gmail.com][email] - -[email]: mailto:walter.higgins@gmail.com?subject=ScriptCraft_API_Reference - -Module Loading -============== -At server startup the ScriptCraft Java plugin is loaded and once -loaded the Java plugin will in turn begin loading all of the -javascript (.js) files it finds in the js-plugins directory (in the -current working directory). If this is the first time the ScriptCraft -plugin is loaded, then the js-plugins directory will not yet exist, it -will be created and all of the bundled javascript files will be -unzipped into it from a bundled resource within the Java plugin. The -very first javascript file to load will always be -js-plugins/core/_scriptcraft.js. Then all other javascript files are -loaded. - -Directory structure -------------------- -The js-plugins directory is loosely organised into subdirectories - -one for each module. Each subdirectory in turn can contain one or more -javascript files. Within each directory, a javascript file with the -same filename as the directory will always be loaded before all other -files in the same directory. So for example, drone/drone.js will -always load before any other files in the drone/ directory. Similarly -utils/utils.js will always load before any other files in the utils/ -directory. - -Directories ------------ -As of February 10 2013, the js-plugins directory has the following sub-directories... - - * core - Contains javascript files containing Core functionality crucial to ScriptCraft and modules which use it. - * drone - Contains the drone module and drone extensions. Drone was the first scriptcraft module. - * ext - Contains external 3rd party javascript libraries (e.g. json2.js - the JSON lib) - * mini-games - Contains mini-games - * arrows - The arrows module - * signs - The signs module - * chat - The chat plugin/module - * alias - The alias plugin/module - -Core Module -=========== -This module defines commonly used functions by all plugins... - - * load (filename,warnOnFileNotFound) - loads and evaluates a javascript file, returning the evaluated object. - - * save (object, filename) - saves an object to a file. - - * plugin (name, interface, isPersistent) - defines a new plugin. If - isPersistent is true then the plugin doesn't have to worry about - loading and saving state - that will be done by the framework. Just - make sure that anything you want to save (and restore) is in the - 'store' property - this will be created automatically if not - already defined. (its type is object {} ) - - * ready (function) - specifies code to be executed only when all the plugins have loaded. - - * command (name, function) - defines a command that can be used by non-operators. - -load() function ---------------- -The load() function is used by ScriptCraft at startup to load all of -the javascript modules and data. You normally wouldn't need to call -this function directly. If you put a javascript file anywhere in the -craftbukkit/js-plugins directory tree it will be loaded automatically -when craftbukkit starts up. The exception is files whose name begins -with an underscore `_` character. These files will not be -automatically loaded at startup as they are assumed to be files -managed / loaded by plugins. - -Parameters ----------- - - * filenames - An array of file names or a single file name. - * warnOnFileNotFound (optional - default: false) - warn if the file was not found. - -Return ------- -load() will return the result of the last statement evaluated in the file. - -Example -------- - - load(__folder + "myFile.js"); // loads a javascript file and evaluates it. - - var myData = load("myData.json"); // loads a javascript file and evaluates it - eval'd contents are returned. - -myData.json contents... - - __data = {players:{ - walterh:{ - h: ["jsp home {1}"], - sunny:["time set 0", - "weather clear"] - } - } - } - -save() function ---------------- -The save() function saves an in-memory javascript object to a -specified file. Under the hood, save() uses JSON (specifically -json2.js) to save the object. Again, there will usually be no need to -call this function directly as all javascript plugins' state are saved -automatically if they are declared using the `plugin()` function. Any -in-memory object saved using the `save()` function can later be -restored using the `load()` function. - -Parameters ----------- - - * objectToSave : The object you want to save. - * filename : The name of the file you want to save it to. - -Example -------- - - var myObject = { name: 'John Doe', - aliases: ['John Ray', 'John Mee'], - date_of_birth: '1982/01/31' }; - save(myObject, 'johndoe.json'); - -johndoe.json contents... - - var __data = { "name": "John Doe", - "aliases": ["John Ray", "John Mee"], - "date_of_birth": "1982/01/31" }; - -plugin() function ------------------ -The `plugin()` function should be used to declare a javascript module -whose state you want to have managed by ScriptCraft - that is - a -Module whose state will be loaded at start up and saved at shut down. -A plugin is just a regular javascript object whose state is managed by -ScriptCraft. The only member of the plugin which whose persistence is -managed by Scriptcraft is `state` - this special member will be -automatically saved at shutdown and loaded at startup by -ScriptCraft. This makes it easier to write plugins which need to -persist data. - -Parameters ----------- - - * pluginName (String) : The name of the plugin - this becomes a global variable. - * pluginDefinition (Object) : The various functions and members of the plugin object. - * isPersistent (boolean - optional) : Specifies whether or not the plugin/object state should be loaded and saved by ScriptCraft. - -Example -------- -See chat/color.js for an example of a simple plugin - one which lets -players choose a default chat color. See also [Anatomy of a -ScriptCraft Plugin][anatomy]. - -[anatomy]: http://walterhiggins.net/blog/ScriptCraft-1-Month-later - -command() function ------------------- -The `command()` function is used to expose javascript functions for -use by non-operators (regular players). Only operators should be -allowed use raw javascript using the `/js ` command because it is too -powerful for use by regular players and can be easily abused. However, -the `/jsp ` command lets you (the operator / server administrator / -plugin author) safely expose javascript functions for use by players. - -Parameters ----------- - - * commandName : The name to give your command - the command will be invoked like this by players `/jsp commandName` - * commandFunction: The javascript function which will be invoked when the command is invoked by a player. - * options (Array - optional) : An array of command options/parameters - which the player can supply (It's useful to supply an array so that - Tab-Completion works for the `/jsp ` commands. - * intercepts (boolean - optional) : Indicates whether this command - can intercept Tab-Completion of the `/jsp ` command - advanced - usage - see alias/alias.js for example. - -Example -------- -See chat/colors.js or alias/alias.js or homes/homes.js for examples of how to use the `command()` function. - -ready() function ----------------- -The `ready()` function provides a way for plugins to do additional -setup once all of the other plugins/modules have loaded. For example, -event listener registration can only be done after the -events/events.js module has loaded. A plugin author could load the -file explicilty like this... - - load(__folder + "../events/events.js"); - - // event listener registration goes here - -... or better still, just do event regristration using the `ready()` -handler knowing that by the time the `ready()` callback is invoked, -all of the scriptcraft modules have been loaded... - - ready(function(){ - // event listener registration goes here - // code that depends on other plugins/modules also goes here - }); - -The execution of the function object passed to the `ready()` function -is *deferred* until all of the plugins/modules have loaded. That way -you are guaranteed that when the function is invoked, all of the -plugins/modules have been loaded and evaluated and are ready to use. -setTimeout() function ---------------------- - -This function mimics the setTimeout() function used in browser-based javascript. -However, the function will only accept a function reference, not a string of javascript code. -Where setTimeout() in the browser returns a numeric value which can be subsequently passed to -clearTimeout(), This implementation returns a [BukkitTask][btdoc] object which can be subsequently passed to ScriptCraft's own clearTimeout() implementation. - -If Node.js supports setTimeout() then it's probably good for ScriptCraft to support it too. - -[btdoc]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/BukkitTask.html - -clearTimeout() function ---------------------- -A scriptcraft implementation of clearTimeout(). - -setInterval() function ---------------------- - -This function mimics the setInterval() function used in browser-based javascript. -However, the function will only accept a function reference, not a string of javascript code. -Where setInterval() in the browser returns a numeric value which can be subsequently passed to -clearInterval(), This implementation returns a [BukkitTask][btdoc] object which can be subsequently passed to ScriptCraft's own clearInterval() implementation. - -If Node.js supports setInterval() then it's probably good for ScriptCraft to support it too. - -[btdoc]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/BukkitTask.html - -clearInterval() function ---------------------- -A scriptcraft implementation of clearInterval(). - -Core Module - Special Variables -=============================== -There are a couple of special javascript variables available in ScriptCraft... - - * __folder - The current working directory - this variable is only to be used within the main body of a .js file. - * __plugin - The ScriptCraft JavaPlugin object. - * server - The Minecraft Server object. - * self - the current player. (Note - this value should not be used in multi-threaded scripts - it's not thread-safe) - -refresh() function ------------------- -The refresh() function will ... - -1. Disable the ScriptCraft plugin. -2. Unload all event listeners associated with the ScriptCraft plugin. -3. Enable the ScriptCraft plugin. - -... refresh() can be used during development to reload only scriptcraft javascript files. -See [issue #69][issue69] for more information. - -[issue69]: https://github.com/walterhiggins/ScriptCraft/issues/69 - -Drone Module -============ -The Drone is a convenience class for building. It can be used for... - - 1. Building - 2. Copying and Pasting - -It uses a fluent interface which means all of the Drone's methods return `this` and can -be chained together like so... - - var theDrone = new Drone(); - theDrone.up().left().box(blocks.oak).down().fwd(3).cylinder0(blocks.lava,8); - -TLDNR; (Just read this if you're impatient) -=========================================== -At the in-game command prompt type... - - /js box(blocks.oak) - -... creates a single wooden block at the cross-hairs or player location - - /js box(5).right(2).box('35:15',4,9,1) - -... creates a single wooden block and a 2001 black obelisk that is 4 -wide x 9 tall x 1 long in size. If you want to see what else -ScriptCraft's Drone can do, read on... - -Constructing a Drone Object -=========================== - -Drones can be created in any of the following ways... - - 1. Calling any one of the methods listed below will return a Drone object. For example... - - var d = box(blocks.oak) - - ... creates a 1x1x1 wooden block at the cross-hairs or player's location and returns a Drone - object. This might look odd (if you're familiar with Java's Object-dot-method syntax) but all - of the Drone class's methods are also global functions that return new Drone objects. - This is short-hand for creating drones and is useful for playing around with Drones at the in-game - command prompt. It's shorter than typing ... - - var d = new Drone().box(5) - - ... All of the Drone's methods return `this` (self) so you can chain operations together like this... - - var d = box(5).up().box(5,3,1,3).down().fwd(2).box(5).turn().fwd(2).box(5).turn().fwd(2).box(5) - - 2. Using the following form... - - d = new Drone() - - ...will create a new Drone. If the cross-hairs are pointing at a - block at the time then, that block's location becomes the drone's - starting point. If the cross-hairs are _not_ pointing at a block, - then the drone's starting location will be 2 blocks directly in - front of the player. TIP: Building always happens right and front - of the drone's position... - - Plan View: - - ^ - | - | - D----> - - For convenience you can use a _corner stone_ to begin building. - The corner stone should be located just above ground level. If - the cross-hair is point at or into ground level when you create a - new Drone(), then building begins at that point. You can get - around this by pointing at a 'corner stone' just above ground - level or alternatively use the following statement... - - d = new Drone().up(); - - ... which will move the drone up one block as soon as it's created. - - ![corner stone](img/cornerstone1.png) - - 3. Or by using the following form... - - d = new Drone(x,y,z,direction,world); - - This will create a new Drone at the location you specified using - x, y, z In minecraft, the X axis runs west to east and the Z axis runs - north to south. The direction parameter says what direction you want - the drone to face: 0 = east, 1 = south, 2 = west, 3 = north. If the - direction parameter is omitted, the player's direction is used - instead. - - Both the `direction` and `world` parameters are optional. - - 4. Create a new Drone based on a Bukkit Location object... - - d = new Drone(location); - - This is useful when you want to create a drone at a given - `org.bukkit.Location` . The `Location` class is used throughout - the bukkit API. For example, if you want to create a drone when a - block is broken at the block's location you would do so like - this... - - events.on('block.BlockBreakEvent',function(listener,event){ - var location = event.block.location; - var drone = new Drone(location); - // do more stuff with the drone here... - }); - -Parameters ----------- - * location (optional) : *NB* If an `org.bukkit.Location` object is provided as a parameter, then it should be the only parameter. - * x (optional) : The x coordinate of the Drone - * y (optional) : The y coordinate of the Drone - * z (optional) : The z coordinate of the Drone - * direction (optional) : The direction in which the Drone is - facing. Possible values are 0 (east), 1 (south), 2 (west) or 3 (north) - * world (optional) : The world in which the drone is created. - -Drone.box() method -================== -the box() method is a convenience method for building things. (For the more performance-oriented method - see cuboid) - -parameters ----------- - * b - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. - Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` - * w (optional - default 1) - the width of the structure - * h (optional - default 1) - the height of the structure - * d (optional - default 1) - the depth of the structure - NB this is - not how deep underground the structure lies - this is how far - away (depth of field) from the drone the structure will extend. - -Example -------- -To create a black structure 4 blocks wide, 9 blocks tall and 1 block long... - - box(blocks.wool.black, 4, 9, 1); - -... or the following code does the same but creates a variable that can be used for further methods... - - var drone = new Drone(); - drone.box(blocks.wool.black, 4, 9, 1); - -![box example 1](img/boxex1.png) - -Drone.box0() method -=================== -Another convenience method - this one creates 4 walls with no floor or ceiling. - -Parameters ----------- - * block - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. - Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` - * width (optional - default 1) - the width of the structure - * height (optional - default 1) - the height of the structure - * length (optional - default 1) - the length of the structure - how far - away (depth of field) from the drone the structure will extend. - -Example -------- -To create a stone building with the insided hollowed out 7 wide by 3 tall by 6 long... - - box0( blocks.stone, 7, 3, 6); - -![example box0](img/box0ex1.png) - -Drone.boxa() method -=================== -Construct a cuboid using an array of blocks. As the drone moves first along the width axis, -then the height (y axis) then the length, each block is picked from the array and placed. - -Parameters ----------- - * blocks - An array of blocks - each block in the array will be placed in turn. - * width - * height - * length - -Example -------- -Construct a rainbow-colored road 100 blocks long... - - var rainbowColors = [blocks.wool.red, blocks.wool.orange, blocks.wool.yellow, blocks.wool.lime, - blocks.wool.lightblue, blocks.wool.blue, blocks.wool.purple]; - - boxa(rainbowColors,7,1,30); - -![boxa example](img/boxaex1.png) - -Drone Movement -============== -Drones can move freely in minecraft's 3-D world. You control the -Drone's movement using any of the following methods.. - - * up() - * down() - * left() - * right() - * fwd() - * back() - * turn() - -... Each of these methods takes a single optional parameter -`numBlocks` - the number of blocks to move in the given direction. If -no parameter is given, the default is 1. - -to change direction use the `turn()` method which also takes a single -optional parameter (numTurns) - the number of 90 degree turns to make. -Turns are always clock-wise. If the drone is facing north, then -drone.turn() will make the turn face east. If the drone is facing east -then drone.turn(2) will make the drone turn twice so that it is facing -west. - -Drone Positional Info -===================== - - * getLocation() - Returns a Bukkit Location object for the drone - -Drone Markers -============= -Markers are useful when your Drone has to do a lot of work. You can -set a check-point and return to the check-point using the move() -method. If your drone is about to undertake a lot of work - -e.g. building a road, skyscraper or forest you should set a -check-point before doing so if you want your drone to return to its -current location. - -A 'start' checkpoint is automatically created when the Drone is first created. - -Markers are created and returned to using the followng two methods... - - * chkpt - Saves the drone's current location so it can be returned to later. - * move - moves the drone to a saved location. Alternatively you can provide an - org.bukkit.Location object or x,y,z and direction parameters. - -Parameters ----------- - * name - the name of the checkpoint to save or return to. - -Example -------- - - drone.chkpt('town-square'); - // - // the drone can now go off on a long excursion - // - for (i = 0; i< 100; i++){ - drone.fwd(12).box(6); - } - // - // return to the point before the excursion - // - drone.move('town-square'); - -Drone.prism() method -==================== -Creates a prism. This is useful for roofs on houses. - -Parameters ----------- - - * block - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. - Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` - * width - the width of the prism - * length - the length of the prism (will be 2 time its height) - -Example -------- - - prism(blocks.oak,3,12); - -![prism example](img/prismex1.png) - -Drone.prism0() method -===================== -A variation on `prism` which hollows out the inside of the prism. It uses the same parameters as `prism`. - -Drone.cylinder() method -======================= -A convenience method for building cylinders. Building begins radius blocks to the right and forward. - -Parameters ----------- - - * block - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. - Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` - * radius - * height - -Example -------- -To create a cylinder of Iron 7 blocks in radius and 1 block high... - - cylinder(blocks.iron, 7 , 1); - -![cylinder example](img/cylinderex1.png) - -Drone.cylinder0() method -======================== -A version of cylinder that hollows out the middle. - -Example -------- -To create a hollow cylinder of Iron 7 blocks in radius and 1 block high... - - cylinder0(blocks.iron, 7, 1); - -![cylinder0 example](img/cylinder0ex1.png) - -Drone.arc() method -================== -The arc() method can be used to create 1 or more 90 degree arcs in the horizontal or vertical planes. -This method is called by cylinder() and cylinder0() and the sphere() and sphere0() methods. - -Parameters ----------- -arc() takes a single parameter - an object with the following named properties... - - * radius - The radius of the arc. - * blockType - The type of block to use - this is the block Id only (no meta). See [Data Values][dv]. - * meta - The metadata value. See [Data Values][dv]. - * orientation (default: 'horizontal') - the orientation of the arc - can be 'vertical' or 'horizontal'. - * stack (default: 1) - the height or length of the arc (depending on - the orientation - if orientation is horizontal then this parameter - refers to the height, if vertical then it refers to the length). - * strokeWidth (default: 1) - the width of the stroke (how many - blocks) - if drawing nested arcs it's usually a good idea to set - strokeWidth to at least 2 so that there are no gaps between each - arc. The arc method uses a [bresenham algorithm][bres] to plot - points along the circumference. - * fill - If true (or present) then the arc will be filled in. - * quadrants (default: - `{topleft:true,topright:true,bottomleft:true,bottomright:true}` - An - object with 4 properties indicating which of the 4 quadrants of a - circle to draw. If the quadrants property is absent then all 4 - quadrants are drawn. - -Examples --------- -To draw a 1/4 circle (top right quadrant only) with a radius of 10 and stroke width of 2 blocks ... - - arc({blockType: blocks.iron, - meta: 0, - radius: 10, - strokeWidth: 2, - quadrants: { topright: true }, - orientation: 'vertical', - stack: 1, - fill: false - }); - -![arc example 1](img/arcex1.png) - -[bres]: http://en.wikipedia.org/wiki/Midpoint_circle_algorithm -[dv]: http://www.minecraftwiki.net/wiki/Data_values -Drone.door() method -=================== -create a door - if a parameter is supplied an Iron door is created otherwise a wooden door is created. - -Parameters ----------- - * doorType (optional - default wood) - If a parameter is provided then the door is Iron. - -Example -------- -To create a wooden door at the crosshairs/drone's location... - - var drone = new Drone(); - drone.door(); - -To create an iron door... - - drone.door( blocks.door_iron ); - -![iron door](img/doorex1.png) - -Drone.door2() method -==================== -Create double doors (left and right side) - -Parameters ----------- - * doorType (optional - default wood) - If a parameter is provided then the door is Iron. - -Example -------- -To create double-doors at the cross-hairs/drone's location... - - drone.door2(); - -![double doors](img/door2ex1.png) - -Drone.sign() method -=================== -Signs must use block 63 (stand-alone signs) or 68 (signs on walls) - -Parameters ----------- - * message - can be a string or an array of strings. - * block - can be 63 or 68 - -Example -------- -To create a free-standing sign... - - drone.sign(["Hello","World"],63); - -![ground sign](img/signex1.png) - -... to create a wall mounted sign... - - drone.sign(["Welcome","to","Scriptopia"], 68); - -![wall sign](img/signex2.png) - -Drone Trees methods -=================== - - * oak() - * spruce() - * birch() - * jungle() - -Example -------- -To create 4 trees in a row, point the cross-hairs at the ground then type `/js ` and ... - - up().oak().right(8).spruce().right(8).birch().right(8).jungle(); - -Trees won't always generate unless the conditions are right. You -should use the tree methods when the drone is directly above the -ground. Trees will usually grow if the drone's current location is -occupied by Air and is directly above an area of grass (That is why -the `up()` method is called first). - -![tree example](img/treeex1.png) - - -None of the tree methods require parameters. Tree methods will only be successful -if the tree is placed on grass in a setting where trees can grow. -Drone.garden() method -===================== -places random flowers and long grass (similar to the effect of placing bonemeal on grass) - -Parameters ----------- - - * width - the width of the garden - * length - how far from the drone the garden extends - -Example -------- -To create a garden 10 blocks wide by 5 blocks long... - - garden(10,5); - -![garden example](img/gardenex1.png) - -Drone.rand() method -=================== -rand takes either an array (if each blockid has the same chance of occurring) -or an object where each property is a blockid and the value is it's weight (an integer) - -Example -------- -place random blocks stone, mossy stone and cracked stone (each block has the same chance of being picked) - - rand( [blocks.brick.stone, blocks.brick.mossy, blocks.brick.cracked ],w,d,h) - -to place random blocks stone has a 50% chance of being picked, - - rand({blocks.brick.stone: 5, blocks.brick.mossy: 3, blocks.brick.cracked: 2},w,d,h) - -regular stone has a 50% chance, mossy stone has a 30% chance and cracked stone has just a 20% chance of being picked. - -Copy & Paste using Drone -======================== -A drone can be used to copy and paste areas of the game world. - -Drone.copy() method -=================== -Copies an area so it can be pasted elsewhere. The name can be used for -pasting the copied area elsewhere... - -Parameters ----------- - - * name - the name to be given to the copied area (used by `paste`) - * width - the width of the area to copy - * height - the height of the area to copy - * length - the length of the area (extending away from the drone) to copy - -Example -------- - - drone.copy('somethingCool',10,5,10).right(12).paste('somethingCool'); - -Drone.paste() method -==================== -Pastes a copied area to the current location. - -Example -------- -To copy a 10x5x10 area (using the drone's coordinates as the starting -point) into memory. the copied area can be referenced using the name -'somethingCool'. The drone moves 12 blocks right then pastes the copy. - - drone.copy('somethingCool',10,5,10) - .right(12) - .paste('somethingCool'); - -Chaining -======== - -All of the Drone methods return a Drone object, which means methods -can be 'chained' together so instead of writing this... - - drone = new Drone(); - drone.fwd(3); - drone.left(2); - drone.box(2); // create a grass block - drone.up(); - drone.box(2); // create another grass block - drone.down(); - -...you could simply write ... - - var drone = new Drone().fwd(3).left(2).box(2).up().box(2).down(); - -... since each Drone method is also a global function that constructs -a drone if none is supplied, you can shorten even further to just... - - fwd(3).left(2).box(2).up().box(2).down() - -The Drone object uses a [Fluent Interface][fl] to make ScriptCraft -scripts more concise and easier to write and read. Minecraft's -in-game command prompt is limited to about 80 characters so chaining -drone commands together means more can be done before hitting the -command prompt limit. For complex building you should save your -commands in a new script file and load it using /js load() - -[fl]: http://en.wikipedia.org/wiki/Fluent_interface - -Drone Properties -================ - - * x - The Drone's position along the west-east axis (x increases as you move east) - * y - The Drone's position along the vertical axis (y increses as you move up) - * z - The Drone's position along the north-south axis (z increases as you move south) - * dir - The Drone's direction 0 is east, 1 is south , 2 is west and 3 is north. - -Extending Drone -=============== -The Drone object can be easily extended - new buidling recipes/blue-prints can be added and can -become part of a Drone's chain using the *static* method `Drone.extend`. - -Drone.extend() static method -============================ -Use this method to add new methods (which also become chainable global functions) to the Drone object. - -Parameters ----------- - * name - The name of the new method e.g. 'pyramid' - * function - The method body. - -Example -------- - - // submitted by [edonaldson][edonaldson] - Drone.extend('pyramid', function(block,height){ - this.chkpt('pyramid'); - for (var i = height; i > 0; i -= 2) { - this.box(block, i, 1, i).up().right().fwd(); - } - return this.move('pyramid'); - }); - -Once the method is defined (it can be defined in a new pyramid.js file) it can be used like so... - - var d = new Drone(); - d.pyramid(blocks.brick.stone, 12); - -... or simply ... - - pyramid(blocks.brick.stone, 12); - -[edonaldson]: https://github.com/edonaldson - -Drone Constants -=============== - -Drone.PLAYER_STAIRS_FACING --------------------------- -An array which can be used when constructing stairs facing in the Drone's direction... - - var d = new Drone(); - d.box(blocks.stairs.oak + ':' + Drone.PLAYER_STAIRS_FACING[d.dir]); - -... will construct a single oak stair block facing the drone. - -Drone.PLAYER_SIGN_FACING ------------------------- -An array which can be used when placing signs so they face in a given direction. -This is used internally by the Drone.sign() method. It should also be used for placing -any of the following blocks... - - * chest - * ladder - * furnace - * dispenser - -To place a chest facing the Drone ... - - drone.box( blocks.chest + ':' + Drone.PLAYER_SIGN_FACING[drone.dir]); - -Drone.PLAYER_TORCH_FACING -------------------------- -Used when placing torches so that they face towards the drone. - - drone.box( blocks.torch + ':' + Drone.PLAYER_TORCH_FACING[drone.dir]); - -Drone.times() Method -==================== -The times() method makes building multiple copies of buildings easy. It's possible to create rows or grids of buildings without resorting to `for` or `while` loops. - -Parameters ----------- - * numTimes (optional - default 2) : The number of times you want to repeat the preceding statements. - -Example -------- -Say you want to do the same thing over and over. You have a couple of options... - - * You can use a for loop... - - d = new Drone(); for (var i =0;i < 4; i++){ d.cottage().right(8); } - -While this will fit on the in-game prompt, it's awkward. You need to -declare a new Drone object first, then write a for loop to create the -4 cottages. It's also error prone, even the `for` loop is too much -syntax for what should really be simple. - - * You can use a while loop... - - d = new Drone(); var i=4; while (i--){ d.cottage().right(8); } - -... which is slightly shorter but still too much syntax. Each of the -above statements is fine for creating a 1-dimensional array of -structures. But what if you want to create a 2-dimensional or -3-dimensional array of structures? Enter the `times()` method. - -The `times()` method lets you repeat commands in a chain any number of -times. So to create 4 cottages in a row you would use the following -statement... - - cottage().right(8).times(4); - -...which will build a cottage, then move right 8 blocks, then do it -again 4 times over so that at the end you will have 4 cottages in a -row. What's more the `times()` method can be called more than once in -a chain. So if you wanted to create a *grid* of 20 houses ( 4 x 5 ), -you would do so using the following statement... - - cottage().right(8).times(4).fwd(8).left(32).times(5); - -... breaking it down... - - 1. The first 3 calls in the chain ( `cottage()`, `right(8)`, - `times(4)` ) build a single row of 4 cottages. - - 2. The last 3 calls in the chain ( `fwd(8)`, `left(32)`, `times(5)` ) - move the drone forward 8 then left 32 blocks (4 x 8) to return to - the original x coordinate, then everything in the chain is - repeated again 5 times so that in the end, we have a grid of 20 - cottages, 4 x 5. Normally this would require a nested loop but - the `times()` method does away with the need for loops when - repeating builds. - -Another example: This statement creates a row of trees 2 by 3 ... - - oak().right(10).times(2).left(20).fwd(10).times(3) - -... You can see the results below. - -![times example 1](img/times-trees.png) - -Drone.blocktype() method -======================== -Creates the text out of blocks. Useful for large-scale in-game signs. - -Parameters ----------- - - * message - The message to create - (use `\n` for newlines) - * foregroundBlock (default: black wool) - The block to use for the foreground - * backgroundBlock (default: none) - The block to use for the background - -Example -------- -To create a 2-line high message using glowstone... - - blocktype("Hello\nWorld",blocks.glowstone); - -![blocktype example][imgbt1] - -[imgbt1]: img/blocktype1.png - -Blocks Module -============= -You hate having to lookup [Data Values][dv] when you use ScriptCraft's Drone() functions. So do I. -So I created this blocks object which is a helper object for use in construction. - -Examples --------- - - box( blocks.oak ); // creates a single oak wood block - box( blocks.sand, 3, 2, 1 ); // creates a block of sand 3 wide x 2 high x 1 long - box( blocks.wool.green, 2 ); // creates a block of green wool 2 blocks wide - -Color aliased properties that were a direct descendant of the blocks object are no longer used to avoid confusion with carpet and stained clay blocks. In addition, there's a convenience array `blocks.rainbow` which is an array of the 7 colors of the rainbow (or closest approximations). - -Drone.sphere() method -===================== -Creates a sphere. - -Parameters ----------- - - * block - The block the sphere will be made of. - * radius - The radius of the sphere. - -Example -------- -To create a sphere of Iron with a radius of 10 blocks... - - sphere( blocks.iron, 10); - -![sphere example](img/sphereex1.png) - -Spheres are time-consuming to make. You *can* make large spheres (250 radius) but expect the -server to be very busy for a couple of minutes while doing so. - -Drone.sphere0() method -====================== -Creates an empty sphere. - -Parameters ----------- - - * block - The block the sphere will be made of. - * radius - The radius of the sphere. - -Example -------- -To create a sphere of Iron with a radius of 10 blocks... - - sphere0( blocks.iron, 10); - -Spheres are time-consuming to make. You *can* make large spheres (250 radius) but expect the -server to be very busy for a couple of minutes while doing so. - -Drone.hemisphere() method -========================= -Creates a hemisphere. Hemispheres can be either north or south. - -Parameters ----------- - - * block - the block the hemisphere will be made of. - * radius - the radius of the hemisphere - * northSouth - whether the hemisphere is 'north' or 'south' - -Example -------- -To create a wood 'north' hemisphere with a radius of 7 blocks... - - hemisphere(blocks.oak, 7, 'north'); - -![hemisphere example](img/hemisphereex1.png) - -Drone.hemisphere0() method -========================= -Creates a hollow hemisphere. Hemispheres can be either north or south. - -Parameters ----------- - - * block - the block the hemisphere will be made of. - * radius - the radius of the hemisphere - * northSouth - whether the hemisphere is 'north' or 'south' - -Example -------- -To create a glass 'north' hemisphere with a radius of 20 blocks... - - hemisphere0(blocks.glass, 20, 'north'); - -![hemisphere example](img/hemisphereex2.png) - -Drone.rainbow() method -====================== -Creates a Rainbow. - -Parameters ----------- - - * radius (optional - default:18) - The radius of the rainbow - -Example -------- - - var d = new Drone(); - d.rainbow(30); - -![rainbow example](img/rainbowex1.png) - -Drone.spiral_stairs() method -============================ -Constructs a spiral staircase with slabs at each corner. - -Parameters ----------- - - * stairBlock - The block to use for stairs, should be one of the following... - - 'oak' - - 'spruce' - - 'birch' - - 'jungle' - - 'cobblestone' - - 'brick' - - 'stone' - - 'nether' - - 'sandstone' - - 'quartz' - * flights - The number of flights of stairs to build. - -![Spiral Staircase](img/spiralstair1.png) - -Example -------- -To construct a spiral staircase 5 floors high made of oak... - - spiral_stairs('oak', 5); - -Classroom Module -================ -The `classroom` object contains a couple of utility functions for use -in a classroom setting. The goal of these functions is to make it -easier for tutors to facilitate ScriptCraft for use by students in a -classroom environment. Although granting ScriptCraft access to -students on a shared server is potentially risky (Students can -potentially abuse it), it is slighlty less risky than granting -operator privileges to each student. (Enterprising students will -quickly realise how to grant themselves and others operator privileges -once they have access to ScriptCraft). - -The goal of this module is not so much to enforce restrictions -(security or otherwise) but to make it easier for tutors to setup a shared server -so students can learn Javascript. - -classroom.allowScripting() function -=================================== -Allow or disallow anyone who connects to the server (or is already -connected) to use ScriptCraft. This function is preferable to granting 'ops' privileges -to every student in a Minecraft classroom environment. - -Parameters ----------- - - * canScript : true or false - -Example -------- -To allow all players (and any players who connect to the server) to -use the `js` and `jsp` commands... - - /js classroom.allowScripting(true) - -To disallow scripting (and prevent players who join the server from using the commands)... - - /js classroom.allowScripting(false) - -Only ops users can run the classroom.allowScripting() function - this is so that students -don't try to bar themselves and each other from scripting. - -events Module -============= -The Events module provides a thin wrapper around Bukkit's -Event-handling API. Bukkit's Events API makes use of Java Annotations -which are not available in Javascript, so this module provides a -simple way to listen to minecraft events in javascript. - -events.on() static method -========================= -This method is used to register event listeners. - -Parameters ----------- - - * eventName - A string or java class. If a string is supplied it must - be part of the Bukkit event class name. See [Bukkit API][buk] for - details of the many bukkit event types. When a string is supplied - there is no need to provide the full class name - you should omit - the 'org.bukkit.event' prefix. e.g. if the string - "block.BlockBreakEvent" is supplied then it's converted to the - org.bukkit.event.block.BlockBreakEvent class . - - If a java class is provided (say in the case where you've defined - your own custom event) then provide the full class name (without - enclosing quotes). - - * callback - A function which will be called whenever the event - fires. The callback should take 2 parameters, listener (the Bukkit - registered listener for this callback) and event (the event fired). - - * priority (optional - default: "HIGHEST") - The priority the - listener/callback takes over other listeners to the same - event. Possible values are "HIGH", "HIGHEST", "LOW", "LOWEST", - "NORMAL", "MONITOR". For an explanation of what the different - priorities mean refer to bukkit's [Event API Reference][buk2]. - -Returns -------- -An org.bukkit.plugin.RegisteredListener object which can be used to -unregister the listener. This same object is passed to the callback -function each time the event is fired. - -Example: ------- -The following code will print a message on screen every time a block is broken in the game - - events.on("block.BlockBreakEvent", function(listener, evt){ - echo (evt.player.name + " broke a block!"); - }); - -To handle an event only once and unregister from further events... - - events.on("block.BlockBreakEvent", function(listener, evt){ - print (evt.player.name + " broke a block!"); - evt.handlers.unregister(listener); - }); - -To unregister a listener *outside* of the listener function... - - var myBlockBreakListener = events.on("block.BlockBreakEvent",function(l,e){ ... }); - ... - var handlers = org.bukkit.event.block.BlockBreakEvent.getHandlerList(); - handlers.unregister(myBlockBreakListener); - -[buk2]: http://wiki.bukkit.org/Event_API_Reference -[buk]: http://jd.bukkit.org/dev/apidocs/index.html?org/bukkit/event/Event.html - -Fireworks Module -================ -The fireworks module makes it easy to create fireworks using -ScriptCraft. The module has a single function `firework` which takes -a `org.bukkit.Location` as its 1 and only parameter. - -Examples --------- -The module also extends the `Drone` object adding a `firework` method -so that fireworks can be created as a part of a Drone chain. For -Example.... - - /js firework() - -... creates a single firework, while .... - - /js firework.fwd(3).times(5) - -... creates 5 fireworks in a row. Fireworks have also been added as a -possible option for the `arrow` module. To have a firework launch -where an arrow strikes... - - /js arrows.firework() - -To call the fireworks.firework() function directly, you must provide a -location. For example... - - /js fireworks.firework(self.location); - -![firework example](img/firework.png) - -http.request() function -==================== -The http.request() function will fetch a web address asynchronously (on a -separate thread)and pass the URL's response to a callback function -which will be executed synchronously (on the main thread). In this -way, http.request() can be used to fetch web content without blocking the -main thread of execution. - -Parameters ----------- - - * request: The request details either a plain URL e.g. "http://scriptcraft.js/sample.json" or an object with the following properties... - - - url: The URL of the request. - - method: Should be one of the standard HTTP methods, GET, POST, PUT, DELETE (defaults to GET). - - params: A Javascript object with name-value pairs. This is for supplying parameters to the server. - - * callback: The function to be called when the Web request has completed. This function takes the following parameters... - - responseCode: The numeric response code from the server. If the server did not respond with 200 OK then the response parameter will be undefined. - - response: A string (if the response is of type text) or object containing the HTTP response body. - -Example -------- -The following example illustrates how to use http.request to make a request to a JSON web service and evaluate its response... - - var jsResponse; - http.request("http://scriptcraftjs.org/sample.json",function(responseCode, responseBody){ - jsResponse = eval("(" + responseBody + ")"); - }); - -... The following example illustrates a more complex use-case POSTing parameters to a CGI process on a server... - - http.request({ url: "http://pixenate.com/pixenate/pxn8.pl", - method: "POST", - params: {script: "[]"} - }, function( responseCode, responseBody){ - var jsObj = eval("(" + responseBody + ")"); - }); - -Utilities Module -================ -Miscellaneous utility functions and classes to help with programming. - - * locationToString(Location) - returns a bukkit Location object in string form. - - * getPlayerObject(playerName) - returns the Player object for a named - player or `self` if no name is provided. - -utils.foreach() function -======================== -The utils.foreach() function is a utility function for iterating over -an array of objects (or a java.util.Collection of objects) and processing each object in turn. Where -utils.foreach() differs from other similar functions found in -javascript libraries, is that utils.foreach can process the array -immediately or can process it *nicely* by processing one item at a -time then delaying processing of the next item for a given number of -server ticks (there are 20 ticks per second on the minecraft main -thread). This method relies on Bukkit's [org.bukkit.scheduler][sched] -package for scheduling processing of arrays. - -[sched]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/package-summary.html - -Parameters ----------- - - * array : The array to be processed - It can be a javascript array, a java array or java.util.Collection - * callback : The function to be called to process each item in the - array. The callback function should have the following signature - `callback(item, index, object, array)`. That is the callback will - be called with the following parameters.... - - - item : The item in the array - - index : The index at which the item can be found in the array. - - object : Additional (optional) information passed into the foreach method. - - array : The entire array. - - * object (optional) : An object which may be used by the callback. - * delay (optional, numeric) : If a delay is specified (in ticks - 20 - ticks = 1 second), then the processing will be scheduled so that - each item will be processed in turn with a delay between the completion of each - item and the start of the next. This is recommended for big builds (say 200 x 200 x 200 - blocks) or any CPU-intensive process. - * onDone (optional, function) : A function to be executed when all processing - is complete. This parameter is only used when the processing is delayed. (It's optional even if a - delay parameter is supplied). - -If called with a delay parameter then foreach() will return -immediately after processing just the first item in the array (all -subsequent items are processed later). If your code relies on the -completion of the array processing, then provide an `onDone` parameter -and put the code there. - -Example -------- -The following example illustrates how to use foreach for immediate processing of an array... - - var players = ["moe", "larry", "curly"]; - utils.foreach (players, function(item){ - server.getPlayer(item).sendMessage("Hi " + item); - }); - -... The `utils.foreach()` function can work with Arrays or any Java-style collection. This is important -because many objects in the Bukkit API use Java-style collections... - - utils.foreach( server.onlinePlayers, function(player){ - player.chat("Hello!"); - }); - -... the above code sends a "Hello!" to every online player. - -The following example is a more complex use case - The need to build an enormous structure -without hogging CPU usage... - - // build a structure 200 wide x 200 tall x 200 long - // (That's 8 Million Blocks - enough to tax any machine!) - - var a = []; - a.length = 200; - var drone = new Drone(); - var processItem = function(item, index, object, array){ - // build a box 200 wide by 200 long then move up - drone.box(blocks.wood, 200, 1, 200).up(); - }; - // by the time the job's done 'self' might be someone else - // assume this code is within a function/closure - var player = self; - var onDone = function(){ - player.sendMessage("Job Done!"); - }; - utils.foreach (a, processItem, null, 10, onDone); - -utils.nicely() function -======================= -The utils.nicely() function is for performing processing using the -[org.bukkit.scheduler][sched] package/API. utils.nicely() lets you -process with a specified delay between the completion of each `next()` -function and the start of the next `next()` function. -`utils.nicely()` is a recursive function - that is - it calls itself -(schedules itself actually) repeatedly until `hasNext` returns false. - -Parameters ----------- - - * next : A function which will be called if processing is to be done. - * hasNext : A function which is called to determine if the `next` - callback should be invoked. This should return a boolean value - - true if the `next` function should be called (processing is not - complete), false otherwise. - * onDone : A function which is to be called when all processing is complete (hasNext returned false). - * delay : The delay (in server ticks - 20 per second) between each call. - -Example -------- -See the source code to utils.foreach for an example of how utils.nicely is used. - -utils.at() function -=================== -The utils.at() function will perform a given task at a given time every -(minecraft) day. - -Parameters ----------- - - * time24hr : The time in 24hr form - e.g. 9:30 in the morning is "09:30" while - 9:30 pm is "21:30", midnight is "00:00" and midday is "12:00" - * callback : A javascript function which will be invoked at the given time. - * world : (optional) Each world has its own clock. If no world is specified, the server's first world is used. - -Example -------- - -To warn players when night is approaching... - - utils.at( "19:00", function() { - - utils.foreach( server.onlinePlayers, function(player){ - player.chat("The night is dark and full of terrors!"); - }); - - }, self.world); - -String class extensions ------------------------ -The following chat-formatting methods are added to the javascript String class.. - - * black() - * darkblue() - * blue() - * darkgreen() - * darkaqua() - * darkred() - * purple() - * gold() - * gray() - * darkgray() - * indigo() - * brightgreen() - * green() - * aqua() - * red() - * pink() - * yellow() - * white() - * bold() - * random() - * strike() - * underline() - * italic() - * reset() - -Example -------- - - var boldGoldText = "Hello World".bold().gold(); - self.sendMessage(boldGoldText); - -

Hello World

- diff --git a/docs/img/castleex1.png b/docs/img/castleex1.png new file mode 100644 index 0000000..d1efaf7 Binary files /dev/null and b/docs/img/castleex1.png differ diff --git a/docs/img/chessboardex1.png b/docs/img/chessboardex1.png new file mode 100644 index 0000000..24bc8c9 Binary files /dev/null and b/docs/img/chessboardex1.png differ diff --git a/docs/img/cottageex1.png b/docs/img/cottageex1.png new file mode 100644 index 0000000..bc6a077 Binary files /dev/null and b/docs/img/cottageex1.png differ diff --git a/docs/img/cottageroadex1.png b/docs/img/cottageroadex1.png new file mode 100644 index 0000000..8e7f2ad Binary files /dev/null and b/docs/img/cottageroadex1.png differ diff --git a/docs/img/cowclicker.png b/docs/img/cowclicker.png new file mode 100644 index 0000000..43e2fe9 Binary files /dev/null and b/docs/img/cowclicker.png differ diff --git a/docs/img/dancefloorex1.png b/docs/img/dancefloorex1.png new file mode 100644 index 0000000..dd8c449 Binary files /dev/null and b/docs/img/dancefloorex1.png differ diff --git a/docs/img/fortex1.png b/docs/img/fortex1.png new file mode 100644 index 0000000..e204ebc Binary files /dev/null and b/docs/img/fortex1.png differ diff --git a/docs/img/lcdclockex1.png b/docs/img/lcdclockex1.png new file mode 100644 index 0000000..df17c48 Binary files /dev/null and b/docs/img/lcdclockex1.png differ diff --git a/docs/img/mazeex1.png b/docs/img/mazeex1.png new file mode 100644 index 0000000..1db678e Binary files /dev/null and b/docs/img/mazeex1.png differ diff --git a/docs/img/scriptcraft-chat-color.png b/docs/img/scriptcraft-chat-color.png new file mode 100644 index 0000000..65208e8 Binary files /dev/null and b/docs/img/scriptcraft-chat-color.png differ diff --git a/docs/img/templeex1.png b/docs/img/templeex1.png new file mode 100644 index 0000000..059ca4c Binary files /dev/null and b/docs/img/templeex1.png differ diff --git a/lib/.gitignore b/lib/.gitignore new file mode 100644 index 0000000..a2e0b10 --- /dev/null +++ b/lib/.gitignore @@ -0,0 +1 @@ +/canarymod.jar diff --git a/lib/canarymod-1.8.0.jar b/lib/canarymod-1.8.0.jar new file mode 100644 index 0000000..c3b025f Binary files /dev/null and b/lib/canarymod-1.8.0.jar differ diff --git a/lib/spigot-1.11.2.jar b/lib/spigot-1.11.2.jar new file mode 100644 index 0000000..6a05525 Binary files /dev/null and b/lib/spigot-1.11.2.jar differ diff --git a/license.txt b/license.txt new file mode 100644 index 0000000..e0e06da --- /dev/null +++ b/license.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Walter Higgins + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/mcp-deprecated/CommandScript.java b/mcp-deprecated/CommandScript.java deleted file mode 100644 index 1775c0d..0000000 --- a/mcp-deprecated/CommandScript.java +++ /dev/null @@ -1,43 +0,0 @@ -package net.minecraft.src; -/** - * This mod lets you load and run javascript to build structures which - * would otherwise be tedious. Build road networks, rows of houses, - * factories and sky-scrapers. ScriptCraft takes building to a whole - * new level by making it easy to create javascript scripts that do - * the building for you. The following code creates a simple cottage - * at the crosshair location or the player's current location... - * - * load("./drone.js"); - * var drone = new Drone().chkpt('cornerstone'); - * drone.box0(48,7,2,6) // 4 walls - * .right(3).door() // a door front-center - * .left(2).box(102) // windows left and right of door - * .right(4).box(102) // - * .move('cornerstone').up(2).prism0(53,7,6); // a gable roof - * - */ - -public class CommandScript extends CommandBase -{ - ScriptCraftEvaluator evaluator = null; - - public String getCommandName() { return "js"; } - - public int getRequiredPermissionLevel() { return 0; } - - public void processCommand(ICommandSender par1ICommandSender, String[] args) - { - if (this.evaluator == null) - this.evaluator = new ScriptCraftEvaluator(new ScriptCraftMCP(this)); - - // Collect the arguments into a single string. - String s = ""; - for (int i=0; i < args.length; i++) { - s += args[i] + " "; - } - // Now evaluate the string we've colected. - this.evaluator.eval(s,par1ICommandSender); - - return; - } -} diff --git a/mcp-deprecated/IScriptCraft.java b/mcp-deprecated/IScriptCraft.java deleted file mode 100644 index 7d9de18..0000000 --- a/mcp-deprecated/IScriptCraft.java +++ /dev/null @@ -1,12 +0,0 @@ -package net.minecraft.src; - -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); -} diff --git a/mcp-deprecated/ScriptCraftEvaluator.java b/mcp-deprecated/ScriptCraftEvaluator.java deleted file mode 100644 index cf810db..0000000 --- a/mcp-deprecated/ScriptCraftEvaluator.java +++ /dev/null @@ -1,265 +0,0 @@ -package net.minecraft.src; - -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{ - 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)","",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, "", 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); - } - public static void putSign(Context cx, Scriptable thisObj,Object[] args, Function funObj){ - List jsArray = (List)args[0]; - - String[] texts = new String[4]; - 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); - } - -} diff --git a/mcp-deprecated/ScriptCraftMCP.java b/mcp-deprecated/ScriptCraftMCP.java deleted file mode 100644 index 88b9429..0000000 --- a/mcp-deprecated/ScriptCraftMCP.java +++ /dev/null @@ -1,109 +0,0 @@ -package net.minecraft.src; -import net.minecraft.client.*; -/** - * An implementation of the IScriptCraft interface for - * Minecraft Coder Pack-style install - * - */ -public class ScriptCraftMCP implements IScriptCraft -{ - protected CommandBase command = null; - - public ScriptCraftMCP(CommandBase mcpCommand){ - this.command = mcpCommand; - } - - protected ICommandSender invoker; - // - // following code depends on MCP - // - public double[] getPlayerPos() - { - double[] result = new double[4]; - if (this.invoker instanceof EntityPlayer){ - EntityPlayer player = (EntityPlayer)this.invoker; - result[0] = player.posX; - result[1] = player.posY; - result[2] = player.posZ; - result[3] = player.rotationYaw; - return result; - } - return null; - } - public double[] getMousePos() - { - Minecraft mc = net.minecraft.client.Minecraft.getMinecraft(); - MovingObjectPosition omo = mc.objectMouseOver; - if (omo == null){ - return null; - } - double[] result = new double[4]; - result[0] = omo.blockX; - result[1] = omo.blockY; - result[2] = omo.blockZ; - return result; - } - public void putSign(String[] texts, int x, int y, int z, int block, int meta) - { - this.putBlock(x,y,z,block,meta); - EntityPlayer player = (EntityPlayer)this.invoker; - World world = player.worldObj; - TileEntitySign sign = (TileEntitySign)world.getBlockTileEntity(x,y,z); - for (int i=0 ; i < texts.length && i <= 3;i++){ - String text = texts[i]; - if (text != null){ - if (text.length() > 15){ - text = text.substring(0,15); - } - sign.signText[i] = text; - } - } - sign.onInventoryChanged(); - world.markBlockForUpdate(x,y,z); - } - - public void putBlock(int x, int y, int z, int blockId, int meta) - { - World world = null; - if (this.invoker instanceof EntityPlayer) - { - world = ((EntityPlayer)(this.invoker)).worldObj; - } - else if (this.invoker instanceof TileEntity) - { - world = ((TileEntity)(this.invoker)).getWorldObj(); - } - world.setBlockAndMetadata(x,y,z,blockId,meta); - - switch (blockId) - { - case 6: - ((BlockSapling)Block.sapling).growTree(world,x,y,z,world.rand); - break; - } - } - // - // returns the block id and metadata at a given location in the world - // e.g. returns "98" for a stone block or "98:2" for a mossy stone block. - // - public String getBlock(int x, int y, int z) - { - EntityPlayer player = (EntityPlayer)this.invoker; - World world = player.worldObj; - int blockId = world.getBlockId(x,y,z); - int metadata = world.getBlockMetadata(x,y,z); - if (metadata !=0){ - return "" + blockId + ":" + metadata; - }else{ - return "" + blockId; - } - } - public void notifyAdministrators(String message) - { - this.command.notifyAdmins(this.invoker,message,(Object[])null); - } - public void setInvoker(Object invoker) - { - this.invoker = (ICommandSender)invoker; - } -} diff --git a/mcp-deprecated/ServerCommandManager.patch b/mcp-deprecated/ServerCommandManager.patch deleted file mode 100644 index 231096f..0000000 --- a/mcp-deprecated/ServerCommandManager.patch +++ /dev/null @@ -1,2 +0,0 @@ -9a10 -> this.registerCommand(new CommandScript()); diff --git a/mcp-deprecated/optional/GuiIngame.patch b/mcp-deprecated/optional/GuiIngame.patch deleted file mode 100644 index 2ef70ac..0000000 --- a/mcp-deprecated/optional/GuiIngame.patch +++ /dev/null @@ -1,10 +0,0 @@ -466a467,475 -> // -> // wph 20121231 show the block id and metadata for in-focus block -> // -> MovingObjectPosition omo = this.mc.objectMouseOver; -> if (omo != null){ -> int bi = this.mc.theWorld.getBlockId(omo.blockX,omo.blockY,omo.blockZ); -> int md = this.mc.theWorld.getBlockMetadata(omo.blockX,omo.blockY,omo.blockZ); -> this.drawString(var8,"Mouse:" + omo.blockX + ", " + omo.blockY + ", " + omo.blockZ + " block data value: " + bi + ":" + md,2,112,14737632); -> } diff --git a/release-notes.md b/release-notes.md new file mode 100644 index 0000000..5d19238 --- /dev/null +++ b/release-notes.md @@ -0,0 +1,594 @@ +RELEASE NOTES +============= + +3.2.1 Release (2016 12 23) +-------------------------- + +Bug fixes and updated from Spigot 1.9 to Spigot 1.11.2 + +3.2.0 Release (2016 03 20) +-------------------------- + +Bug fixes and updated from Spigot 1.8.8 to Spigot 1.9 + +Fixed issues #256 and #287 + +3.1.12 Release (2015 12 30) +--------------------------- + +Added new modules + +* lightning + +The entities module and lightning module are now documented. + +To make lightning strikes when and where any arrow lands: + + var lightning = require('lightning'); + events.projectileHit( function( event ) { + if (entities.arrow( event.projectile ) + lightning( event.projectile ); + }); + +3.1.11 Release (2015 11 21) +--------------------------- + +Added new modules + +* entities +* spawn + +And new Drone function `spawn()` + +To use: +Point at a block then type... +``` +/js spawn('ZOMBIE').fwd().times(5).right().back(5).times(6) +``` + +... unleash a horde of zombies (in 5x6 grid formation). + +3.1.10 Release (2015 08 16) +--------------------------- +Bug fix: modules/bukkit/sounds.js now works (fixed for Bukkit/SpigotMC/Glowstone) + +3.1.9 Release (2015 08 01) +-------------------------- +Bug fix: minigames/scoreboard.js module's updatePlayerScore() function did not work with latest version of CanaryMod. +Using /scoreboard command instead. See https://github.com/walterhiggins/ScriptCraft/issues/261 + +3.1.8 Release (2015 06 07) +-------------------------- +Bug fix: Fixes drone on Spigot 1.8.7 with JDK 7 see +https://github.com/walterhiggins/ScriptCraft/issues/254 + + +3.1.7 Release (2015 06 07) +-------------------------- +Added workaround for https://bugs.openjdk.java.net/browse/JDK-8072596 to recipes module. + +3.1.6 Release (2015 05 31) +-------------------------- +Provide more helpful error messages when trying to require modules which don't exist e.g. +require('greetings') should fail but indicate if there's a 'greeting' module present instead. + +Fixes problem with Fireworks module on Mac OS. + +3.1.5 Release (2015 05 31) +-------------------------- +CanaryMod version : Add events.connect as synonym for events.connnection for backward-compatibility +with 1.7.9 and book listings. + +3.1.4 Release (2015 05 09) +-------------------------- +Various bug fixes and new 'inventory' module. + +3.1.3 Release (2015 03 02) +-------------------------- +Various bug fixes. + +3.1.2 Release (2015 02 16) +-------------------------- +Bug fix release. Fixes bug #213 (http.request fixed for CanaryMod) +New blocks.slime and other block types. +Various other fixes. See https://github.com/walterhiggins/ScriptCraft/compare/3.1.1...3.1.2 + +3.1.1 Release (2015 01 24) +-------------------------- +This is a bug fix release. +See https://github.com/walterhiggins/ScriptCraft/compare/3.1.0...master for bug fix details. + +3.1.0 Release (2015 01 11) +-------------------------- +Fixes issue #197 + +Extending Drone has been made easier. Drone is now a module so it can be required like this: + + var Drone = require('drone'); + Drone.extend(function myExtension(){ } ); + +There have been a number of documentation updates. +The Drone.copy() and Drone.paste() methods are deprecated. + +New utils methods: + +* utils.time(world) returns the time of day (in minecraft ticks) for a world +* utils.time24(world) returns the time of day (in minutes) for a world + +The Arrows and Signs plugins have now been updated to support CanaryMod. + +3.0.3 Release (2015 01 03) +-------------------------- +Additional support for Drone methods in 1.7.10 and 1.8. +Fixes issues: + +* 177: https://github.com/walterhiggins/ScriptCraft/issues/177 +* 185: https://github.com/walterhiggins/ScriptCraft/issues/185 +* 188: https://github.com/walterhiggins/ScriptCraft/issues/188 + +3.0.2 Release (2014 12 28) +-------------------------- +This version includes fixes for CraftBukkit (1.7.X) and also fixes +problems on Mac OS X and includes some support for Drone methods in +Minecraft 1.8 (CanaryMod 1.2) + +3.0.0 Release (2014 11 09) +---------------------------------- +In September 2014, development of CraftBukkit was discontinued due to +a DMCA takedown notice. ScriptCraft has since switched from +CraftBukkit to CanaryMod as the underlying framework. ScriptCraft +continues to run on CraftBukkit but - for the immediate future - +future development efforts will be to ensure it works primarily on +CanaryMod. When other frameworks like SpongePowered become available, + +I hope to eventually support those too. + +The biggest change in 3.0.0 is the removal of the short name event +registration function. + +This will no longer work: + + events.on('block.BlockBreakEvent', function( event ) { ... }); + +Instead use this: + + events.blockDestroy( function( event ) { ... }); + +or + + events.on(net.canarymod.hook.block.BlockDestroyHook, function( event ) { ... }); + + +2014 08 23 +---------- +Chessboard was broken, is now fixed. +Rainbows are now made from stained glass. Full range of stained_glass +colors is available in blocks variable. +SnowballFight mini-game has been removed. An improved version will be +available soon online. +chat plugin has been removed - will be available in another github repo soon. + +2014 06 14 +---------- +Fix issue #140 - fails to build for JRE7 +Changed command() documentation to conform with new way of using (passing a named function) + +2014 05 31 +---------- +Fix bug in persistence module. Private load function wasn't returning result of scload. + +2014 05 29 +---------- +Fix tab completion for /jsp command so that it conforms with tab completion norms in minecraft. +/jsp ice completes to /jsp icecream +Hitting TAB again has no effect. Player must type space then hit TAB to get list of flavors. +This is consistent with how MC treats other commands for tab completion. + +2014 05 19 +---------- +Improved Tab Completion to work with Java Enums too. + +2014 05 12 +---------- +Turn off modality for conversations which are started via the 'input' module. +(with modality on, player.sendMessage() is suppressed but player.sendRawMessage() isn't. + turning modality off as devs would expect player.sendMessage() to work - I did anyway) + + +2014 05 10 +---------- +Further simplification of events handling. The events.on() function +can still be used but additional functions are now provided for each +type of event. For example, to register a custom player-join event +handler... + + events.playerJoin(function(event){ + event.player.sendMessage('welcome!'); + }); + +Added new sounds module for simpler sounds playback and in-game tab completion. +All of the org.bukkit.Sound enum values are attached to the sounds module. + +2014 04 13 +---------- +Added asynchronous `input()` function module. + +Version 2.0.6 (2014 03 15) +-------------------------- +## Simplified Event handling code. + +The callback function for event handling now only takes 1 single +parameter, the event which triggered the callback. The listener object +is bound to the callback function so within the callback function +`this` refers to the listener object. Unregistering listeners has also +been greatly simplified. You can have an event handler which fires +only once by unregistering itself within the callback like this... + + events.on('player.PlayerJoinEvent', function( event ) { + + // do something + event.player.sendMessage( "You're the first player to join" ); + + // unregister so this function is called only once ever. + this.unregister(); + + }); + +The `events.on()` function also returns the same listener object as +`this` refered to inside the callback. The listener object has a +single method `unregister()` which can be called to stop listening for +the event. + +# 2014 03 12 + +Added Drone.MAX_VOLUME and Drone.MAX_SIDE properties to specify limits +on size of Drone ops. This is to stop individual players from hogging +the CPU in a classrom environment. + +# 2014 03 08 + +Fixed issues #115 #122 #123 + +Improved background processing of Drone build commands. + +# 2014 02 19 + +## Version 2.0.5 + +Asynchronous building. Drone now builds asynchronously. +Fixed Issue #119 (exceptions on reload/stop) + +# 2014 02 11 + +## Version 2.0.4 + +Various bug fixes, enhanced classroom module and improved error logging on startup. + +# 2014 01 17 + +Added support for communication with Arduino and other devices which +use the [MQTT protocol][mqtt] via a new `sc-mqtt` module. This module +requires a separate sc-mqtt.jar file downloadable from + which must be included +in the CraftBukkit classpath. If using MQTT in ScriptCraft, then +Craftbukkit should be launched like this... + + java -classpath craftbukkit.jar;sc-mqtt.jar org.bukkit.craftbukkit.Main + +You can use the new `sc-mqtt` module like this to send and receive +messages between minecraft and an Arduino. For example to send a +message to the MQTT broker whenever a player breaks a block... + + var mqtt = require('sc-mqtt'); + var client = mqtt.client('tcp://localhost:1883','scripcraft'); + client.connect(); + + events.on('block.BlockBreakEvent', function(listener, event){ + client.publish('minecraft','block-broken'); + }); + +To have minecraft react to inputs from an MQTT broker... + + var mqtt = require('sc-mqtt'); + var client = mqtt.client('tcp://localhost:1883','scripcraft'); + client.connect(); + client.onMessageArrived(function(topic, message){ + var payload = message.payload; + if (topic == 'arduino'){ + // do something with payload. + } + }); + +[mqtt]: http://mqtt.org/ +# 2014 01 14 + +Added config.yml file for configuration options. This file allows +scriptcraft admins to turn on or off re-unzipping of the modules, +plugins and lib folders when deploying a new version of +scriptcraft. It's strongly recommended that the lib directory always +be set to true to get the latest core scriptcraft code . The modules +and plugins directories are optional and not part of scriptcraft core. +The config.yml file looks like this... + + extract-js: + plugins: true + modules: true + lib: true + +# 2014 01 13 + +Bug Fix: Make ScriptCraft work with Nashorn javascript engine bundled with Java 8 + + +# 2014 01 12 + +## Important +The ScriptCraft.jar file has been renamed scriptcraft.jar (see bug fix +details below). This means that you will have to remove the existing +`plugins/ScriptCraft.jar` file if present. + +Bug Fix: On Mac OS, the plugins/scriptcraft directory is copied to +plugins/ScriptCraftPlugin the 2nd time ScriptCraftPlugin is loaded. +This has been fixed by changing the plugin name from ScriptCraftPlugin +to scriptcraft. The jar file has also been rename from +ScriptCraft.jar to scriptcraft.jar. + +New command: `jsp spawn` lets in-game operators spawn any type of +entity. For example `/jsp spawn cow` will spawn a cow at the in-game +operator's current location. + +New minigame: Cow Clicker. A simple demonstration of using Bukkit's +Scoreboard API. Players click cows to score points. Scores are +displayed in a side bar on screen. Players join or leave the game by +typing `/jsp cowclicker` at the in-game prompt. + +# 2014 01 05 + +Bug Fix: On Mac OS, alias plugin caused Exceptions due to missing +.trim() function on String. + +Changed target for compilation to 1.6 so that ScriptCraft will work +with both java 1.6 and 1.7. + +Added documentation for the Signs module. + +# 2014 01 02 + +Added a warning in console at start-up if legacy directories are detected. +Added 'use strict' to core modules. +Bug Fix; Signs were not being saved. (introduced with recent change to JSONifying Location) + +# 2014 01 01 + +'Buddha' Release - towards a total annihilation of the 'self' variable. +The 'self' variable should only be used at the in-game or server +console command prompts and should not be used in modules or in +multi-threaded code. + +Moved scriptcraft directory from {craftbukkit-root}/scriptcraft to +{craftbukkit-root}/plugins/scriptcraft because this seems to be where +other plugins create plugin-specific directories on the filesystem. + +Documentation updates. Added new sections to the Young Persons Guide +to Modding Minecraft. + +# 2013 12 30 + +Removing coffeescript support because coffeescript.js will not +compile/evaluate (generated bytecode exceeds 64K limit). + +Updated plugin to remove `self` variable once the `js` command +evaluation has completed (limits the scope of the `self` variable to +just command-prompt evaluation). + +# 2013 12 29 + +Bug Fix: [Can't get Scriptcraft core libraries working][bug103]. + +[bug103]: https://github.com/walterhiggins/ScriptCraft/issues/103 + +Bug Fix; Server console errors when empty commands submitted. + +Added more example modules. + +# 2013 12 28 + +Documented the 'homes' module other tweaks to documentation. + +# 2013 12 27 + +## Updated 'jsp alias' command. + +The 'jsp alias' command now lets players define their own shortcuts +which don't require the 'jsp ' prefix. + +### Example + +At the in-game prompt use the following command to create a new alias +`cw` (short for change Clock & Weather) which will change the time and +weather using a single statement. + + /jsp alias set cw = time set {1} ; weather {2} + +This creates a new cw alias which takes 2 parameters, time and weather +and passes them to the 'time set' and 'weather' commands. You use the +alias like this... + + /cw 4000 sun + +... which in turn gets converted into these 2 commands... + + /time set 4000 + /weather sun + +Aliases can be set on a per-player basis or can be set globally (for +all players). Aliases are automatically saved and restore on server +shutdown/startup. + +## Added console global variable. + +ScriptCraft now has a `console` global variable which can be used for +logging (to the server console). The `console` variable uses the +ScriptCraft plugin Logger object. You use the console object in your +javascript modules like this... + + console.info('Hello %s, %s', 'World', 'Universe'); + +... or simply... + + console.warn('Hello World'); + +# 2013 12 26 + +Made the `events` variable global because it is use by modules and +plugins. This means there is no longer any need to explicitly +`require('events')` since `events` is now a free variable in the +global namespace. + +# 2013 12 25 + +Added the 'commando' module. + +# 2013 12 24 + +## 'Modules' release + +### Modules in ScriptCraft + +ScriptCraft now has a simple module loading system. ScriptCraft now +uses the [CommonJS module contract][cjsmod] - that is - the same +module system used by Node.js. All of the javascript code which comes +bundled with ScriptCraft has been modified so that it conforms to the +CommonJS module system. + +### What this means for plugins you've developed using ScriptCraft + +If you have written plugins using a previous version of ScriptCraft then you have 2 options... + + 1. Continue using the previous version of ScriptCraft. + 2. Update your plugins to work with the ScriptCraft 'Modules' release. + +... Option 2 should be relatively straightforward if you follow these steps... + + 1. Copy your own custom plugins from the `js-plugins` directory to the new `scriptcraft/plugins` directory. + 2. In your javascript code any functions, objects or variables which + you want to expose for use by others should be exposed using the + special `exports` variable. All other code within your .js files will + now be private by default. See below for details on how + CommonJS/Node.js modules work. + +If you have any questions or concerns or need help converting your +existing javascript plugin, contact please post questions on the +[ScriptCraft forum][scforum] or open an issue on the [Github +project][github] + +[github]: http://github.com/walterhiggins/ScriptCraft +[scforum]: https://groups.google.com/forum/?fromgroups=#!forum/scriptcraft---scripting-minecraft + +In ScriptCraft, files and modules are in one-to-one correspondence. As +an example, foo.js loads the module circle.js in the same directory. +*ScriptCraft now uses the same module system as Node.js - see [Node.js +Modules][njsmod] for more details.* + +[njsmod]: http://nodejs.org/api/modules.html +[cjsmod]: http://wiki.commonjs.org/wiki/Modules/1.1.1 + +The contents of foo.js: + + var circle = require('./circle.js'); + echo( 'The area of a circle of radius 4 is ' + + circle.area(4)); + +The contents of circle.js: + + var PI = Math.PI; + + exports.area = function (r) { + return PI * r * r; + }; + + exports.circumference = function (r) { + return 2 * PI * r; + }; + +The module circle.js has exported the functions area() and +circumference(). To add functions and objects to the root of your +module, you can add them to the special exports object. + +Variables local to the module will be private, as though the module +was wrapped in a function. In this example the variable PI is private +to circle.js. + +If you want the root of your module's export to be a function (such as +a constructor) or if you want to export a complete object in one +assignment instead of building it one property at a time, assign it to +module.exports instead of exports. + +#### Module Loading + +When the ScriptCraft Java plugin is first installed, a new +subdirectory is created in the craftbukkit directory. If your +craftbukkit directory is called 'craftbukkit' then the new +subdirectories will be ... + + * craftbukkit/scriptcraft/ + * craftbukkit/scriptcraft/plugins + * craftbukkit/scriptcraft/modules + * craftbukkit/scriptcraft/lib + +... The `plugins`, `modules` and `lib` directories each serve a different purpose. + +##### The plugins directory + +At server startup the ScriptCraft Java plugin is loaded and begins +automatically loading and executing all of the modules (javascript +files with the extension `.js`) it finds in the `scriptcraft/plugins` +directory. All modules in the plugins directory are automatically +loaded into the `global` namespace. What this means is that anything a +module in the `plugins` directory exports becomes a global +variable. For example, if you have a module greeting.js in the plugins +directory.... + + exports.greet = function() { + echo('Hello ' + self.name); + }; + +... then `greet` becomes a global function and can be used at the +in-game (or server) command prompt like so... + + /js greet() + +... This differs from how modules (in NodeJS and commonJS +environments) normally work. If you want your module to be exported +globally, put it in the `plugins` directory. If you don't want your +module to be exported globally but only want it to be used by other +modules, then put it in the `modules` directory instead. If you've +used previous versions of ScriptCraft and have put your custom +javascript modules in the `js-plugins` directory, then put them in the +`scriptcraft/plugins` directory. To summarise, modules in this directory are ... + + * Automatically loaded and run at server startup. + * Anything exported by modules becomes a global variable. + +##### The modules directory + +The module directory is where you should place your modules if you +don't want to export globally. In javascript, it's considered best +practice not to have too many global variables, so if you want to +develop modules for others to use, or want to develop more complex +mods then your modules should be placed in the `modules` directory. +*Modules in the `modules` directory are not automatically loaded at +startup*, instead, they are loaded and used by other modules/plugins +using the standard `require()` function. This is the key difference +between modules in the `plugins` directory and modules in the +`modules` directory. Modules in the `plugins` directory are +automatically loaded and exported in to the global namespace at server +startup, modules in the `modules` directory are not. + +##### The lib directory + +Modules in the `lib` directory are for use by ScriptCraft and some +core functions for use by module and plugin developers are also +provided. The `lib` directory is for internal use by ScriptCraft. +Modules in this directory are not automatically loaded nor are they +globally exported. diff --git a/src/docs/java/jscript.java b/src/docs/java/jscript.java index 777fe7d..53334a1 100644 --- a/src/docs/java/jscript.java +++ b/src/docs/java/jscript.java @@ -1,5 +1,6 @@ import javax.script.*; import java.io.FileReader; +import net.canarymod.api.inventory.ItemType; public class jscript { @@ -10,6 +11,7 @@ public class jscript java.io.File file = new java.io.File(args[0]); engine.put("engine",engine); engine.put("args",args); + engine.put("cmItemTypeClass",ItemType.class); FileReader fr = new java.io.FileReader(file); engine.eval(fr); fr.close(); diff --git a/src/docs/javascript/generateApiDocs.js b/src/docs/javascript/generateApiDocs.js deleted file mode 100644 index 81de2a3..0000000 --- a/src/docs/javascript/generateApiDocs.js +++ /dev/null @@ -1,123 +0,0 @@ -/* - This script is run at build time to generate api.md - a single Markdown document containing documentation for ScriptCraft's API -*/ -args = args.slice(1); -var dir = args[0]; -var foreach = function(array, func){ - for (var i =0; i < array.length; i++){ - func(array[i],i,array); - } -}; - -importPackage(java.io); -/* - find - a (very) basic implementation of the unix command line tool. -*/ -var find = function(dir,store,re) -{ - var files = dir.listFiles(); - foreach (files, function(filename){ - filename = "" + filename; - var file = new File(filename); - if (file.isDirectory()) { - find(file,store,re); - } else { - if (typeof re == "undefined") - store.push(filename); - else if (filename.match(re)) - store.push(filename); - } - }); -}; -/* - the main module file for a given directory - (assuming the main module is in a file with the same name as the parent - directory) - e.g. drone/drone.js -*/ -var sorter = function( precedence ){ - return function(a,b) - { - // convert from Java string to JS string - a = "" + a; - b = "" + b; - var aparts = a.split(/\//); - var bparts = b.split(/\//); - var adir = aparts.slice(3,aparts.length-1).join("/"); - var afile = aparts[aparts.length-1]; - var bdir = bparts.slice(3,bparts.length-1).join("/"); - var bfile = bparts[bparts.length-1]; - - for (var i = 0;i < precedence.length; i++){ - var re = precedence[i]; - if (a.match(re)) - return -1; - if (b.match(re)) - return 1; - } - if(adirbdir) return 1; - afile = afile.replace(/\.js$/,""); - if (afile == adir) - return -1; - else - return 1; - }; -}; -var sortByModule = function(a,b) -{ - var aparts = (""+a).split(/\//); - var bparts = (""+b).split(/\//); - var adir = aparts[aparts.length-2]; - var afile = aparts[aparts.length-1]; - var bdir = bparts[bparts.length-2]; - var bfile = bparts[bparts.length-1]; - if (afile == "_scriptcraft.js") - return -1; - if (bfile == "_scriptcraft.js") - return 1; - if(adirbdir) return 1; - if (afile.indexOf(adir) == 0) - return -1; - else - return 1; -}; -var store = []; -find(new File(dir),store,/\/[a-zA-Z0-9_\-]+\.js$/); - -store.sort(sorter([ - /_scriptcraft\.js$/, - /core/, - /drone\.js/, - /drone/ -])); - -var contents = []; -foreach(store, function(filename){ - var br = new BufferedReader(new FileReader(filename)); - var line ; - while ( (line = br.readLine()) != null){ - contents.push(line); - } - br.close(); -}); - -var len = contents.length; -var writeComment = false; -var startComment = /^\/\*{10}/; -var endComment = /^\*{3}\//; - -for (var i = 0;i < contents.length; i++){ - var line = contents[i]; - if (line.match(startComment)){ - writeComment = true; - i++; - } - if (line.match(endComment)){ - writeComment = false; - } - if (writeComment){ - println(contents[i]); - } -} - diff --git a/src/docs/js/generateApiDocs.js b/src/docs/js/generateApiDocs.js new file mode 100644 index 0000000..467dbf7 --- /dev/null +++ b/src/docs/js/generateApiDocs.js @@ -0,0 +1,136 @@ +/*global load, args, Packages*/ +/* + This script is run at build time to generate api.md - a single Markdown document containing documentation for ScriptCraft's API + */ +function foreach(array, func){ + for (var i =0; i < array.length; i++){ + func(array[i],i,array); + } +} +/* + find - a (very) basic implementation of the unix command line tool. + */ +function find(dir,store,re) { + var files = dir.listFiles(); + foreach (files, function(filename){ + filename = "" + filename; + var file = new File(filename); + if (file.isDirectory()) { + find(file,store,re); + } else { + if (typeof re == "undefined") + store.push(filename); + else if (filename.match(re)) + store.push(filename); + } + }); +} +/* + the main module file for a given directory + (assuming the main module is in a file with the same name as the parent + directory) - e.g. drone/drone.js + */ +function sorter( precedence ){ + return function(a,b) + { + // convert from Java string to JS string + a = '' + a; + b = '' + b; + var aparts = a.split(/\//); + var bparts = b.split(/\//); + var adir = aparts.slice(3,aparts.length-1).join('/'); + var afile = aparts[aparts.length-1]; + var bdir = bparts.slice(3,bparts.length-1).join('/'); + var bfile = bparts[bparts.length-1]; + + for (var i = 0;i < precedence.length; i++){ + var re = precedence[i]; + if (a.match(re) && b.match(re)){ + if (afile < bfile) + return -1; + if (afile > bfile) + return 1; + } + if (a.match(re)) + return -1; + if (b.match(re)) + return 1; + } + if(adirbdir) return 1; + afile = afile.replace(/\.js$/,''); + if (afile == adir){ + return -1; + } + else { + var result = 0; + if (afile < bfile){ + result = -1; + } + if (afile > bfile){ + result = 1; + } + //err.println("afile: " + afile + ", bfile:" + bfile + ",result=" + result); + + return result; + } + }; +} +var err = java.lang.System.err; +args = Array.prototype.slice.call(args,1); + +if (typeof importPackage == 'undefined'){ + // load compatibility script + load('nashorn:mozilla_compat.js'); +} +var dir = args[0]; + +var io = Packages.java.io; +var File = io.File; +var store = []; +find(new io.File(dir),store,/\/[a-zA-Z0-9_\-]+\.js$/); + +store.sort(sorter([ + /lib\/scriptcraft\.js$/, + /lib\/require\.js$/, + /lib\/plugin\.js$/, + /lib\/events\.js$/, + /lib\/events\-helper\-canary/, + /lib\/events\-helper\-bukkit/, + /lib\//, + /modules\/drone\/index\.js/, + /modules\/drone\//, + /plugins\/drone\//, + /modules\//, + /examples\// +])); + +var contents = []; +foreach(store, function(filename){ + var br = new io.BufferedReader(new io.FileReader(filename)); + var line ; + while ( (line = br.readLine()) != null){ + contents.push(line); + } + br.close(); +}); + +var len = contents.length; +var writeComment = false; +var startComment = /^\/\*{10}/; +var endComment = /^\*{3}\//; + +for (var i = 0;i < contents.length; i++){ + var line = contents[i]; + if (line.match(startComment)){ + writeComment = true; + i++; + } + if (line.match(endComment)){ + writeComment = false; + } + if (writeComment){ + java.lang.System.out.println(contents[i]); + } +} + diff --git a/src/docs/js/generateEntitiesDoc.js b/src/docs/js/generateEntitiesDoc.js new file mode 100644 index 0000000..f24e368 --- /dev/null +++ b/src/docs/js/generateEntitiesDoc.js @@ -0,0 +1,46 @@ +args = Array.prototype.slice.call(args,1); +// [0] = type, [1] = lib.jar [2] = blockX, [3] = classX +var out = java.lang.System.out, + err = java.lang.System.err, + entry = null; +var content = [ + '', + '## Entities module', + 'The Entities module provides a suite of functions - one for each possible entity type. ', + 'It acts as a helper or enumerated module to assist in use with the `spawn` module and command. ', + 'This module is useful for TAB-completion at the in-game prompt. ', + '', + 'When each function is called with no parameters, it will return the appropriate EntityType object. ', + 'For example `entities.polar_bear()` will return an `EntityType.POLAR_BEAR` object. ', + '', + 'When each function is called with a single parameter - an entity - the entity\'s type will be compared and return true or false. ', + '', + '### Usage', + '', + ' entities.zombie(); // returns a SpigotMC/CanaryMod EntityType.ZOMBIE enum value', + ' entities.zombie( mob ); // compares the entity\'s type to a zombie, returns true if mob type is zombie, false otherwise', + ' entities.player( self ); // at the in-game prompt this should return true (compares self to a player entity type)', + ' entities.rabbit( self ); // at the in-game prompt this should return false (compares self to a rabbit entity type)', + '', + 'The following functions are provided:', + '' +]; + +var enumVals = [], t, i, name; +var entitytypes = org.bukkit.entity.EntityType.values(); +for (t in entitytypes) { + if (entitytypes[t] && entitytypes[t].ordinal) { + name = entitytypes[t].name(); + name = ('' + name).replace(/^(.*)/,function(a){ return a.toLowerCase(); }); + enumVals.push(' * ' + name + '()'); + } +} +enumVals.sort(); +content = content.concat(enumVals); +content.push(''); +for (i = 0; i< content.length; i++){ + out.println(content[i]); +} + + + diff --git a/src/docs/js/generateEventsHelper.js b/src/docs/js/generateEventsHelper.js new file mode 100644 index 0000000..d32fcb8 --- /dev/null +++ b/src/docs/js/generateEventsHelper.js @@ -0,0 +1,103 @@ +args = Array.prototype.slice.call(args,1); +// [0] = type, [1] = lib.jar [2] = blockX, [3] = classX +var File = java.io.File, + FileReader = java.io.FileReader, + FileInputStream = java.io.FileInputStream, + FRAMEWORK = args[0], + out = java.lang.System.out, + err = java.lang.System.err, + Modifier = java.lang.reflect.Modifier, + clz, + ZipInputStream = java.util.zip.ZipInputStream, + zis = new ZipInputStream(new FileInputStream(args[1])), + entry = null; +var content = [ + '/*********************', + '## Events Helper Module (' + FRAMEWORK + ' version)', + 'The Events helper module provides a suite of functions - one for each possible event.', + 'For example, the events.' + args[2] + '() function is just a wrapper function which calls events.on(' + args[3] + ', callback, priority)', + 'This module is a convenience wrapper for easily adding new event handling functions in Javascript. ', + 'At the in-game or server-console prompt, players/admins can type `events.` and use TAB completion ', + 'to choose from any of the approx. 160 different event types to listen to.', + '', + '### Usage', + '', + ' events.' + args[2] + '( function( event ) { ', + ' echo( event.player, \'You broke a block!\'); ', + ' });', + '', + 'The crucial difference is that the events module now has functions for each of the built-in events. The functions are accessible via TAB-completion so will help beginning programmers to explore the events at the server console window.', + '', + '***/' +]; +var canary = false; +if (FRAMEWORK == 'CanaryMod'){ + canary = true; +} + +for (var i = 0; i< content.length; i++){ + out.println(content[i]); +} +while ( ( entry = zis.nextEntry) != null) { + var name = new String( entry.name ); + var re1 = /org\/bukkit\/event\/.+Event\.class$/; + if (canary){ + re1 = /net\/canarymod\/hook\/.+Hook\.class$/; + } + if ( re1.test(name) ) { + name = name.replace(/\//g,'.').replace('.class',''); + try { + clz = java.lang.Class.forName(name); + }catch ( e) { + err.println('Warning: could not Class.forName("' + name + '")'); + clz = engine.eval(name); + } + var isAbstract = Modifier.isAbstract(clz.getModifiers()); + if ( isAbstract ) { + continue; + } + var parts = name.split('.'); + var shortName = null; + if (canary){ + shortName = name.replace('net.canarymod.hook.',''); + } + if (!canary){ + shortName = name.replace('org.bukkit.event.',''); + } + var fname = parts.reverse().shift().replace(/^(.)/,function(a){ + return a.toLowerCase();}); + if (!canary){ + fname = fname.replace(/Event$/,''); + } + if (canary){ + fname = fname.replace(/Hook$/,''); + } + var javaDoc = canary ? 'https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/' : 'https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/'; + var comment = [ + '/*********************', + '### events.' + fname + '()', + '', + '#### Parameters ', + '', + ' * callback - A function which is called whenever the ['+ shortName + ' event](' + javaDoc + shortName.replace('.','/') + '.html) is fired', + '', + ' * priority - optional - see events.on() for more information.', + '', + '***/' +//http://jd.bukkit.org/rb/apidocs/org/bukkit/event/player/PlayerJoinEvent.html + ]; + for (var i = 0; i < comment.length; i++){ + out.println(comment[i]); + } + out.println('exports.' + fname + ' = function(callback,priority){ '); + if (canary){ + out.println(' return events.on(Packages.' + name + ',callback,priority);'); + } else { + out.println(' return events.on(' + name + ',callback,priority);'); + } + out.println('};'); + } +} + + + diff --git a/src/docs/js/generateItemsDoc.js b/src/docs/js/generateItemsDoc.js new file mode 100644 index 0000000..fe5715f --- /dev/null +++ b/src/docs/js/generateItemsDoc.js @@ -0,0 +1,77 @@ +args = Array.prototype.slice.call(args,1); +// [0] = type, [1] = lib.jar [2] = blockX, [3] = classX +var out = java.lang.System.out, + err = java.lang.System.err, + entry = null; +var content = [ + '', + '## Items module (SpigotMC version)', + 'The Items module provides a suite of functions - one for each possible item.', + 'See https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html for a list of possible items', + '', + '### Usage', + '', + ' items.book(); // returns org.bukkit.Material.BOOK', + ' items.book(2); // returns a new org.bukkit.Material object with an amount 2 (2 books)', + ' items.book( itemType ); // compares itemType parameter to org.bukkit.Material.BOOK or an Item of type book', + '', + 'The following functions are provided:', + '' +]; + +var enumVals = [], t, i, name; +var types = org.bukkit.Material.values(); +for (t in types) { + if (types[t] && types[t].ordinal) { + name = ('' + types[t].name()).toLowerCase(); + name = name.replace(/(_.)/g,function(a){ return a.replace(/_/,'').toUpperCase(); }); + enumVals.push(' * ' + name + '()'); + } +} +enumVals.sort(); +content = content.concat(enumVals); +content.push(''); +for (i = 0; i< content.length; i++){ + out.println(content[i]); +} + +content = [ + '', + '## Items module (CanaryMod version)', + 'The Items module provides a suite of functions - one for each possible item.', + 'See https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/inventory/ItemType.html for a list of possible items', + '', + '### Usage', + '', + ' items.book(); // returns net.canarymod.api.inventory.ItemType.Book', + ' items.book(2); // returns a new net.canarymod.api.inventory.Item object with an amount 2 (2 books)', + ' items.book( itemType ); // compares itemType parameter to ItemType.Book or an Item of type book', + '', + 'The following functions are provided:', + '' +]; + +//var ItemType = java.lang.Class.forName('net.canarymod.api.inventory.ItemType'); +var materials = cmItemTypeClass.getDeclaredFields(); + +enumVals = []; +for ( i = 0;i < materials.length; i++ ){ + + if (materials[i].type != cmItemTypeClass) { + continue; + } + var materialField = materials[i]; + name = (''+materialField.name).replace(/^(.)/,function(a){ + return a.toLowerCase() ; + }); + enumVals.push(' * ' + name + '()'); +} +enumVals.sort(); +content = content.concat(enumVals); +content.push(''); +for (var i = 0; i< content.length; i++){ + out.println(content[i]); +} + + + diff --git a/src/docs/js/generateTOC.js b/src/docs/js/generateTOC.js new file mode 100644 index 0000000..22e35df --- /dev/null +++ b/src/docs/js/generateTOC.js @@ -0,0 +1,50 @@ +args = Array.prototype.slice.call(args,1); + +// wph 20140105 trim not availabe in String on Mac OS. +if (typeof String.prototype.trim == 'undefined'){ + String.prototype.trim = function(){ + return this.replace(/^\s+|\s+$/g,''); + }; +} + +var template = args[0]; + +var BufferedReader = java.io.BufferedReader; +var FileReader = java.io.FileReader; + +var contents = [], line = undefined; +var br = new BufferedReader( new FileReader(template) ); + +while ( (line = br.readLine()) != null){ + contents.push(line); +} +br.close(); + +var anchors = {}; + +var createLink = function(text){ + var result = text.replace(/_/g,'_'); + result = result.replace(/[^a-zA-Z0-9 _\-]/g,''); + result = result.replace(/ /g,'-'); + var result = result.toLowerCase(); + if (anchors[result]){ + result = result + '-' + (anchors[result]++); + } + anchors[result] = 1; + return result; +}; +java.lang.System.out.println('## Table of Contents'); + +for (var i = 0; i < contents.length; i++){ + line = contents[i]; + if (line.match(/^##\s+/)){ + var h2 = line.match(/^##\s+(.*)/)[1].trim(); + var link = createLink(h2); + java.lang.System.out.println(' * [' + h2 + '](#' + link + ')'); + } + if (line.match(/^###\s+/)){ + var h3 = line.match(/^###\s+(.*)/)[1].trim(); + var link = createLink(h3); + java.lang.System.out.println(' * [' + h3 + '](#' + link + ')'); + } +} diff --git a/src/docs/templates/ypgpm.md b/src/docs/templates/ypgpm.md new file mode 100644 index 0000000..445166c --- /dev/null +++ b/src/docs/templates/ypgpm.md @@ -0,0 +1,1276 @@ +## Introduction + +Minecraft is an open-ended 3D game where you can build and craft +anything you like. Minecraft can be extended and enhanced using 'Mods' +(short for 'modifications') - additional bits of code that are added +to the Game. ScriptCraft is one such Mod - it lets you program in +Javacript right within the game, making it possible to ... + + * Build using simple javascript statements. + * Extend the game in other interesting ways - add new Items, change + the game behaviour and create mini-games. + +Minecraft can be played in single-player or multi-player mode (with +friends). Most people play Minecraft in Multi-player mode where +players connect to a Minecraft Server on the internet or locally +(running on the player's computer). + +![Cottages created using ScriptCraft in MineCraft][img_cr] + +# Installation + +Before installing ScriptCraft you must first install SpigotMC which is +a special version of Minecraft Server that makes it easy to customize +the game. + +## Installing and Running SpigotMC + +Follow these steps to download and install SpigotMC. + +1. Download Spigot's [BuildTools.jar][spigotdl] +2. Save the BuildTools.jar file to a new directory called spigotmc. +3. Open a terminal (Mac and Linux) or command prompt (windows) window and type `java -jar BuildTools.jar`. This will kick off a long series of commands to "build" SpigotMC. +4. When the build is done, there will be a new file beginning with `spigot` and ending in `.jar` in the spigotmc directory. Run this file by typing `java -jar spigot-1.10.2.jar` (it might not be that exact name - you can list files in the directory by typing `dir` (Windows) or `ls` (Mac and Linux). +5. The server will start up then shut down very shortly afterwards. You'll need to edit a file called `eula.txt` - change `eula=false` to `eula=true` and save the file. +6. Run the `java -jar spigot-1.10.2.jar` command again - this time the server will start up. Shut it down by typing `stop` at the server prompt. + +## Installing ScriptCraft + +Follow these steps to download and install ScriptCraft. + +1. Download the [scriptcraft.jar][dl] plugin and save it to the `plugins` directory and restart the server by typing `java -jar spigot-1.10.2.jar`. +2. At the server prompt type `js 1 + 1` and hit enter. The result `2` should be displayed. + +Congratulations - you've just installed your Custom Minecraft Server and are ready to begin writing your first mod! + +## Configuring your Server (optional) + +Once you've installed SpigotMC, depending on your specific needs, +you might want to consider setting the following properties in the `server.properties` file: + + # completely flat worlds are best for building from scratch + # bukkit/spigotmc + level-type=FLAT + generate-structures=false + + # creative mode + gamemode=1 + pvp=false + + # turns off authentication (for classroom environments) + online-mode=false + spawn-npcs=false + spawn-monsters=false + +## Learning Javascript + +To begin creating cool stuff in Minecraft using ScriptCraft, you don't +*have* to know much JavaScript. ScriptCraft comes with lots of functions +to help you create buildings of any size, and lets you experiment while +you play. However, as you learn Javascript you will be able to create +cooler stuff in Minecraft - not just buildings, you'll be able to add +new rules and items to the game - even create mini-games for you and +your friends. If you want to get started learning JavaScript, check out +this [fun Javascript Tutorial][ce]. If you want to dive right in to +ScriptCraft, read on... + +## First Steps + +If you don't already know Javascript, don't worry, you'll learn a little +about Programming and Javascript along the way. You've set up a +Minecraft server and are ready to connect ... + +1. Launch Minecraft. +2. Click 'Multi-Player' +3. Click 'Add Server' +4. Type any name you like in the name field then type `localhost` in the +address field. `localhost` is a special internet address that points to +your own computer. +5. Click 'Join Server' to join the server. If the version +of Minecraft is incompatible with the version of the server you will +not be able to connect to the server. To fix this, you can create a +Minecraft profile in your client. Profiles let you decide which +version of Minecraft client you want to run so that your client and +server are compatible. + +6. Once you've joined the game, press the `/` key located at the bottom +right of your keyboard. A prompt will appear. Type the following then +press enter: `js 1 + 1` The number 2 should be displayed. + +... Well Done! You've just confirmed you can run Javascript code from +within the Minecraft Console. + +## Variables + +A variable is how you name something for the computer (and you the +programmer) to remember. You create a new variable in Javascript using +the `var` keyword... + + /js var location = 'Blackrock Castle' + +... creates a new variable called `location` and stores the text +`Blackrock Castle` in it. Now the computer has a new item in its memory +called `location`. We can use that name like this... + + /js echo( location ) + +... and the following is displayed... + + Blackrock Castle + +...You might be wondering why there's no enclosing `'` single quotes. +When telling the computer to store some text, you have to put `'` +(that's the single-quote character) at the start and end +of the text. The computer doesn't store these quote characters, only the +text between them. The computer will store the variables while the +Minecraft Server is running. Repeat the last command you entered by +pressing the `/` key then the UP arrow key on your keyboard, then +pressing enter. You can repeat that statement as many times as you like +and the computer will always display the same value. You can change the +value like this... + + /js location = 'Mahon Point' + +...notice this time I didn't use the `var` keyword. I didn't need to. +The `var` keyword is only needed when you first create the variable. Now +execute this command... + + /js echo( self, location ) + +...and it displays... + + Mahon Point + +Variables can be created and changed easily in Javascript. Along with +the variables you'll create in your in-game commands and scripts, there +are handy *free* variables created for you by ScriptCraft. One such variable is +`self`, it contains information about the current player (that's you)... + + /js echo ( self, self.name ) + +... displays something like the following... + + walterh + +... for me but the message displayed will be different for every player. + +## Functions + +ScriptCraft comes with a couple of extra functions not normally found in +Javascript. These functions will help you build new structures and +buildings which would otherwise take hours to build by hand. Before +looking at the building functions let's look at the `echo()` function. + +`echo()` - as its name implies - will echo back at you whatever you +tell it. For example, type ... + + /js echo( self, 'Hello') + +... and the game will display... + + Hello + +... type ... + + /js echo( self, 5 + 7 ) + +... and the game will display... + + 12 + +... While you can now use Minecraft to help with Maths homework - I +don't recommend it. Homework and Minecraft don't mix! The `echo()` +function will display anything you tell it to - Text, Numbers and other types... + + /js echo( self, new Date() ) + +... prints today's date. If the statement above looks confusing - don't +worry - `new Date()` creates a new date object - I'll talk about objects +later ... + + Tue Jan 08 2013 20:53:37 GMT-0000 (GMT) + +![Today's Date][img_echo_date] + +`echo()` is a very useful function but it is not part of the +Javascript Language. You can't use it outside of Minecraft. There are +many other functions in Javascript all of which you can also +use in Minecraft. For example... + + /js Math.max( 6, 11 ) + +... returns the larger of the 2 numbers you give it (max is short for +maximum). While... + + /js Math.min( 6, 11 ) + +... returns the smaller of the 2 numbers. That's another thing - +functions can `return` stuff. You can store the result of a function +(what it returns) in a variable like this... + + /js var biggest = Math.max( 6, 11 ) + +... Now type... + + /js biggest + +... Not all Javascript functions return data but most do. As well as +the functions provided to you by the Javascript Language and +ScriptCraft, you can write your own functions like this... + + /js function whatTimeIsIt () { return new Date() } + +... Here you've created a new `function` called `whatTimeIsIt` and +told the function it should return a new `Date` object every time it's +called. You'll notice the above statement didn't actually do anything +- it certainly didn't display the current time. That's because all + you've done is is say what the function should do when it's called, + you haven't called it yet. To call the function... + + /js whatTimeIsIt() + +... The current time is displayed. Congrats! You've just written your +first Javascript function - you're well on your way to becoming a +Minecraft Modder. There are many functions for working with Text, +numbers and dates in Javascript... + + /js Math.random() + +... prints out a random number every time you call it. Try it! Then press +the `/` key then the UP Arrow key to repeat the last statement in your +in-game console. You'll see the number displayed is different each +time. Think of Math.random() as a Dice with many many sides. You can +rely on it to never return the same value twice. + +## Building stuff in Minecraft + +Now we get to the fun stuff - creating structures and buildings in +Minecraft. Building by hand is fun but tedious when you want to build +big - Towers, Castles and Fortresses. That's where ScriptCraft comes in. +ScriptCraft comes with a couple of javascript functions that can be +combined to build interesting things. Let's start small though to get a +feel for how ScriptCraft's building functions work. The function you'll +probably use most for building is called `box()` and - as its name +implies - it is used to create cubes and cuboids of any size. A cube is +a 3D shape whose sides are all the same length. A cuboid is a 3D shape +whose width, height and length can differ. + +![3D Shapes][img_3d_shapes] + +You can create a Cube or a Cuboid in ScriptCraft using the `box()` +function. You must tell the function what material you want the shape to +be made of. For example, in the game, point the cross hairs at the +ground, then type the following and hit enter... + + /js box( blocks.oak ) + +... This will change the targeted block to wood. What's happened here +is the `box()` function has created a single new wooden +block. `blocks` is another one of those *free* variables you get in +ScriptCraft, you can see a list of block materials by typing ... + + /js blocks. + +... then pressing the `TAB` key. Repeatedly pressing the `TAB` key +will cycle through all of the block materials. Alternatively, you can +see many more current materials and the numbers Minecraft uses for +them by visiting the [Minecraft Data Values][mcdv] site. + +## Common Block Materials + +In Minecraft Programming, Materials aren't known by their name, +instead numbers (sometimes 2 numbers) are used to indicate which +material should be used. For example the number 2 is grass, 1 is +cobblestone etc, while 5 is wood (oak). There are different types of +wood so the text '5:1' means Spruce, '5:2' means Birch and '5:3' means +Jungle wood. There are many different materials in the Minecraft world, the most +commonly used materials for building are: + + * '4' - Cobblestone or `blocks.cobblestone` + * '5' - Wooden Planks or `blocks.oak` + * '5:2' - Birch wood Planks (light wood) or `blocks.birch` + * '98' - Stone bricks or `blocks.brick.stone` + * '45' - Red bricks or `blocks.brick.red` + * '68' - Sign or `blocks.sign` + * '102' - Glass panes (for windows) or `blocks.glass_pane` + +You can create a single wooden block using the numeric values or the `blocks` variable. For example... + + /js box( '5' ) + +... and ... + + /js box( blocks.oak ) + +... both do exactly the same thing but I personally prefer `/js box( +blocks.oak )` because it's easier to remember. For reference, here is +a chart of all of the blocks (not items) in the Minecraft world... + +![Minecraft Data Values][img_dv] + +## Dimensions + +`box()` can do more than just +create single blocks - it can create cubes and cuboids of any +size. Take a look at the following picture which shows how shapes are +measured in 3D space. There are 3 dimensions (or sizes) to consider. + +1. Width +2. Height +3. Depth (or length) - not to be confused with how deep underground a +mine-shaft can go. Think of Depth (or length if you prefer) as how far +away you want something to extend. + +![Width, Height and Depth][img_whd] + +## More shapes + + * `box0( block, width, height, depth )` - creates an empty box (with the + insides hollowed out - perfect for dwellings. `box0` will remove both + the floor and ceiling too. + * `cylinder( block, radius, height )` - creates cylinders, perfect for + Chimneys. + * `cylinder0( block, radius, height )` - creates empty cylinders - + perfect for Towers. `cylinder0` will remove both the floor and + ceiling too. + * `prism( block, width, depth )` - creates a Prism - good for roofs. + * `prism0( block, width, depth )` - creates an empty prism. + +## The Drone Object + +ScriptCraft is a Minecraft Mod that lets you execute Javascript code +in the game. It also lets you write your own Mod in Javacript. One +such mod that comes bundled with ScriptCraft is called the `Drone` +mod. The `Drone` is an (invsible) object you create every time you +execute any of the building or movement functions. When you execute... + + /js box(5,3,2,4) + +... a new `Drone` object is created and does the work of building on +your behalf. Think of a `Drone` as something like a remote control +plane that can move about freely and build things for you. Moving the +Drone is easy... + +### Movement + + * `up( numberOfBlocks )` - moves the Drone Up. For example: `up()` + will move the Drone 1 block up. You can tell it how many blocks to + move if you want it to move more than one block. + * `down( numberOfBlocks )` - moves the Drone Down. + * `left( numberOfBlocks )` - moves the Drone Left. + * `right( numberOfBlocs )` - moves the Drone Right. + * `fwd( numberOfBlocs )` - moves the Drone Forward (away from the player). + * `back( numberOfBlocs )` - moves the Drone Back (towards the player) + * `turn( numberOfTurns )` - Turns the Drone Clock-wise (right). For example: + `turn()` will make the Drone turn right 90 degrees. `turn(2)` will + make the Drone turn twice so that it is facing in the opposite + direction. + +### Chaining - combining building and movement. + +You can make a Drone move around before and after building by +*daisy-chaining* the building and movement functions together. In the +game, point at the ground then type the following... + + /js up(1).box( blocks.oak ).fwd(3).box( blocks.oak ) + +A series of 2 boxes is created 3 blocks apart. + +![Two Boxes 3 blocks apart][img_2boxes] + +### Exercise - Build a simple dwelling + +OK. You know enough now about the `Drone` functions to be able to +build a simple dwelling. The dwelling should be a hollow building with +a sloped roof. *Don't worry about doors or windows for now*. The walls +should be made of Cobblestone ('4') and the roof made of wood ('5'). You can use +the following `Drone` functions to create a dwelling 7 blocks wide by +3 blocks high by 6 blocks long with a wooden sloped roof. It's up +to you to figure out how. + + * `up()` + * `box0()` + * `prism0()` + +Your dwelling should end up looking something like this... + +![Exercise Dwelling][img_ed] + +### Remembering where you started. + +Sometimes when you're building something big that requires lots of +manoeuvering by your Drone, you need to leave breadcrumbs as you go so +your `Drone` can return to where it started. Every new Drone has a +`'start'` checkpoint that it can return to by executing +`move('start')` ... + + /js box('5').up(3).left(4).box('1').turn(3).fwd(5).right().box('1').move('start') + +... A genius would have trouble figuring out how to get back +to where they started. Fortunately, they don't have to - the +`move('start')` function will take the Drone back to its starting +point. + + * `chkpt( breadCrumb )` - Leaves a mark at your Drone's current + location so it can return there later. Think of it as giving a name + to the place where your Drone is located. `chkpt` is short for + Check-Point - a place in a game where you usually save your + progress. + + * `move( breadCrumb )` - Moves your Drone to a location you named + using `chkpt()` . It brings your Drone back to the place where you + saved it. + +Both `chkpt()` and `mark()` are useful for when you want to build +complex things that require your Drone to move about a lot ( for +example, Castles, mansions, palaces, etc). + +## Saving your work + +You can build cool things using the in-game command-prompt and the +`/js` command but sooner or later you'll probably want to build +something more complex and save your commands so you can run them +again when you quit the game and start it up again. + +[Notepad++][np] Is a special text editor (like Notepad which comes +installed on every Windows machine) that is well suited for writing +code. If you don't already have it on your machine, you can [install +Notepad++ here][np]. I recommend using NotePad++ rather than plain old +Notepad because it understands Javascript. If you prefer coding on a +Macintosh, then [TextWrangler][twl] is a good programming editor which +also understands Javascript code. + +## Your First Minecraft Mod! + +So, You've learnt a little bit about Javascript and what the Drone() +object can do, let's use that knowledge to create a Minecraft Mod! + +Once you've installed Notepad++, Launch it, create a new file and type the following... + +```javascript +exports.greet = function( player ) { + echo( player, 'Hi ' + player.name); +} +``` + +... then save the file in a new directory +`scriptcraft/plugins/{your_name}` (replace +{your_name} with your own name) and call the file `greet.js` (be sure +to change the file-type option to '*.* All Files' when saving or +NotePad++ will add a '.txt' extension to the filename. Now switch back +to the Minecraft game and type... + + /js refresh() + +... to reload all of the server plugins. Your mod has just been +loaded. Try it out by typing this command... + + /js greet(self) + +... it should display ... + + Hi {your-username-here} + +... where {your-username-here} will be replaced with your own +minecraft username. Congratulations - You've just written your very +first Minecraft Mod! With ScriptCraft installed, writing Minecraft +Mods is as simple as writing a new javascript function and saving it +in a file in the scriptcraft/plugins +directory. This function will now be avaible every time you launch +minecraft. This is a deliberately trivial minecraft mod but the +principles are the same when creating more complex mods. + +The `exports` variable is a special variable you can use in your mod +to provide functions, objects and variables for others to use. If you +want to provide something for other programmers to use, you should +*export* it using the special `exports` variable. The syntax is +straightforward and you can use the same `exports` variable to export +one or more functions, objects or variables. For example... + +#### thrower.js + +```javascript +exports.boo = function(player){ + echo( player, 'Boo!'); +} +exports.yo = function(player){ + echo( player, 'Yo!'); +} +``` + +... is a plugin which provides 2 javascript functions called `boo()` +and `yo()` which can be invoked from the in-game prompt like +this `/js boo(self)` or `/js yo(self)`. + +## Parameters +If you want to change the `greet()` function so that it displays a +greeting other than 'Hi ' you can change the code in the `greet()` +function, or better still, you can use *Parameters*. Parameters are +values you provide to a function so that the function behaves +differently each time it is called. + +![greeting][img_greet] + +Change the `greet()` function so that it looks like this... + +```javascript +exports.greet = function ( greeting , player) { + echo( player, greeting + player.name ); +} +``` + +... Save your greet.js file and issue the `/js refresh()` command in +minecraft. Now enter the following command in Minecraft... + + greet('Hello ',self); + +... Now try ... + + greet('Dia Dhuit ',self); + +... you should see the following messages in your chat window... + + Hello {your name} + Dia Dhuit {your name} + +... Parameters let you provide different values to functions each time +they're called. As you'll see later, Parameters are very useful when +changing the behaviour of MineCraft. + +## true or false + +Try entering each of the following statements and make a note of the +answers given by minecraft... + + /js 1 < 2 + + /js 1 > 2 + +... the answer given by the first statement ( `1 < 2` ) should be +`true` since 1 is less than 2. The `<` symbol - usually found near the +bottom right of your keyboard - means test to see if something is less +than another so `1 < 2` is a way of asking the computer "is 1 less +than 2 ?". This is a silly example of course since we know 1 is less +than 2 but when dealing with variables we might not know in advance +what its value is or whether it's greater than (bigger) or less than +(smaller) another number or value. The result of the 2nd statement (`1 > 2`) +should be `false` since 1 is not greater than 2. Now try this... + + /js 1 = 2 + +... The result won't be what you expected. You'll see an Error message +- that's OK. What's happened here is I've tried to test to see if 1 is +equal to 2 but I've made one of the most common mistakes even +experienced programmers make. If you want to test to see if two things +are the same, you use `==` that's two equals signs right next to each +other. Let's try again... + + /js 1 == 2 + +... this time you should get an answer `false` since 1 obviously isn't +equal to 2. These are the different *operators* used when comparing +things... + + * `<` Is less than ? + * `>` Is greater than ? + * `==` Is equal to ? + * `<=` Is less than or equal to ? + * `>=` Is greather than or equal to ? + * `!=` Is not equal to ? + +... try comparing some more numbers yourself - say for example, +compare the ages of your friends or siblings to your own age. + +## More fun with `true` or `false` + +You can find out if you can Fly in minecraft by typing the following statement ... + + /js self.allowFlight + +... the result will be `true` or `false` depending on whether you can +fly or not. You can turn on and off your ability to fly by setting +your `allowFlight` property to `true` or `false`. Try it ... + + /js self.allowFlight = true; + +... Now you can fly! Double-press the space bar key to start flying. To turn off flight ... + + /js self.allowFlight = false; + +... and you come crashing down to earth. This is just one example of +how `true` and `false` are used throughout ScriptCraft – these are +called `boolean` values – named after [George Boole][boole], a 19th Century +Maths Professor at University College Cork. There are plenty more +examples of boolean values in Minecraft. You can find out if it's +raining in your minecraft world by typing the following statement ... + + /js self.world.hasStorm() + +... The result of this statement will be either `false` (if it's not raining) or +`true` (if it *is* raining). If it's raining, you can make it stop raining typing the following command: + + /js self.world.setStorm(false) + +... Similarly, to make it start raining you can issue the following command: + + /js self.world.setStorm( true ) + +### Booleans and JavaBeans + +There are many *boolean* properties you can use to turn on or off +certain game behaviours. For example, the *thundering* behavior is turned +on or off using the World's `thundering` property. The World object's +properties and methods are [documented on the SpigotMC JavaDocs World +page][spworld]. When browsing the SpigotMC JavaDoc pages, whenever +you see a method whose name begins with `is` such as `isThundering()` and +a companion method `setThundering()`, these methods are called *JavaBean* +methods - the *thundering* property is a *JavaBean* property and there +are two ways you can use JavaBean properties in Javascript. You can +*get* and *set* the property using the methods provided by Java. To +*get* the thundering property you can call the JavaBean Method: + + /js self.world.isThundering() + +... or you can get the property like this: + + /js self.world.thundering + +To *set* the thundering property, you can call the JavaBean method: + + /js self.world.setThundering( true ) + +... or you can set the property like this: + + /js self.world.thundering = true + +Whatever approach you use, the result will be the same. + +[cmworld]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/world/World.html +[spworld]: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/World.html + +### SIDENOTE +You may be wondering how to change other aspects of the Minecraft game - pretty much all +aspects of the game can be changed. Changes are made using what are +called `API` calls - these are calls to functions and methods in +Minecraft - you can read more about these on the [SpigotMC API +Reference][spigotapi]. + +## ...and Again, and Again, and Again,... + +One of the things Computers are really good at is +repetition. Computers don't get tired or bored of doing the same thing +over and over again. Loops are handy, if you want to run the same +code over and over again, each time with a different value. + +### Counting to 100 + +At the in-game command prompt (hint: press 't') type the following then hit Enter... + + /js for (var i = 1 ; i <= 100 ; i = i + 1) { echo( i ); } + +... The above code will count from 1 to 100. The first thing you'll +notice if you run the above code is how quickly the count +happened. You're probably curious how long it would take to count to +1000. Try it out for yourself. Change the above line of code so that +it counts to 1000 instead of 100. If you're feeling adventurous, see +how long it takes to count to ten thousand, one hundred thousand or even one million. + +The `for` statement is useful when you want to repeat something over and over. It has 4 parts... + + 1. The initialiser: `var i = 1` - this happens once at the start of the loop. + 2. The test: `i <= 100` - this happens at the start of each run around the loop. If the test fails, then the loop ends. + 3. The increment: `i = i + 1` - this happens at the end of each run + around the loop. If you didn't have a statement here, the loop might + never finish. `i = i + 1` is often written as `i++` - it's shorter + and does basically the same thing. + 4. The body - everything that appears between the `{` and `}` (opening and closing curly braces). + + +`for` loops becomes very useful when you combine it with Arrays - +remember, an Array is just a list of things, for example - the players +connnected to a server, the worlds of a server and so on. + +### Saying "Hi!" to every player + +At the in-game command prompt type the following then hit Enter... + + /js var utils = require('utils'); + /js var players = utils.players(); + /js for (var i = 0;i < players.length; i++){ echo(players[i], 'Hi!'); } + +... Lets look at these statements in more detail. We had to enter the +statements on a single line at the in-game command prompt but the +statements could be written like this... + +```javascript +var utils = require('utils'); +var players = utils.players(); +for (var i = 0;i < players.length; i++) { + echo(players[i], 'Hi!'); +} +``` + +... On the 2nd line, a new variable `players` is created and assigned a value by calling utils.players(). +On the next line, the for loop is declared, a counter variable `i` is set +to 0 (zero - arrays in javascript start at 0 not 1) and each time +around the loop is tested to see if it's less than the number of +players online. At the end of each run around the loop the `i` +variable is incremented (increased by 1) so that the next player can +be messaged. Inside the body of the for loop (everything between the +opening `{` and closing `}` curly braces) the `players[i]` expression +refers to the player in the players array at position[i]. Imagine +there are 4 players online on a minecraft server, the `players` array +might look like this... + + * players[0] = 'CrafterJohn' + * players[1] = 'MinerPaul' + * players[2] = 'ExplorerRingo' + * players[3] = 'TraderGeorge' + +... in this case `players.length` will be 4 (since there are 4 online +players), the for-loop will go around 4 times starting from position 0 +and going all the way up to position 3, sending a message to each of +the players in the array. It's time for a new scriptcraft +function. Open the `hi.js` file you created earlier (using NotePad++ , +TextWrangler or your editor of choice) and add the following code at +the bottom of the file... + +```javascript +var utils = require('utils'); +exports.hiAll = function () { + var players = utils.players(); + player, + i; + for ( i = 0; i < players.length; i++) { + player = players[i]; + echo( player, 'Hi!' ); + } +} +``` + +... save the file, at the in-game command prompt type `/js refresh()` and +then type `/js hiAll()`. This will send the message `Hi!` to all of +the players connected to your server. You've done this using a `for` +loop and arrays. Arrays and `for` loops are used heavily in all types +of software, in fact there probably isn't any software that doesn't +use `for` loops and Arrays to get things done. + +## While Loops + +Another way to repeat things over and over is to use a `while` +loop. The following `while` loop counts to 100... + +```javascript +var i = 1; +while ( i <= 100 ) { + console.log( i ); + i = i + 1; +} +``` + +A `while` loop will repeat until its condition is `false` - the +condition in the above example is `i <= 100` so while i is less than +or equal to 100 the code within the `while` block (everything between +the starting `{` and ending `}` curly braces) will run. It's important +that you change the variable being tested in a while loop, otherwise +the while loop will never it - it will run forever. Try running the +following code... + + /js var i = 1; while (i <= 100){ echo( i ); } + +The code above will contine printing out the number 1 until the end of +time (or until you unplug your computer). That's because the `i` +variable is never incremented (remember - incrementing just means +adding 1 to it) so i will always be 1 and never changes meaning the +loop goes on forever. Again - this is a mistake even experienced programmers sometimes make. + +Just like `for` loops, `while` loops can be also be used to loop +through arrays. The following loop prints out all of the players on +the server... + +```javascript +var utils = require('utils'); +var players = utils.players(); +var i = 0; +while ( i < players.length ) { + console.log( players[i] ); + i = i + 1; +} +``` + +... whether you chose to use a `for` loop or a `while` loop is largely +a matter of personal taste, `for` loops are more commonly used with +Arrays but as you see from the example above, `while` loops can also +loop over Arrays. + +## `utils.foreach()` - Yet another way to process Arrays + +Both the `for` statement and `while` statement are standard commonly +used javascript statements used for looping. ScriptCraft also comes +with a special function for looping called `utils.foreach()`. +utils.foreach() is a convenience function, you don't have to use it if +you prefer the syntax of javascript's `for` and `while` +loops. utils.foreach() takes two parameters... + + 1. An array + 2. A function which will be called for each item in the array. + +...that's right, you can pass functions as parameters in javascript! +Let's see it in action, the following code will `console.log()` (print) the +name of each online player in the server console window... + + var utils = require('utils'); + var players = utils.players; + utils.foreach( players, console.log ); + +... in the above example, the list of online players is processed one +at a time and each item (player) is passed to the `console.log` +function. Note here that I used `console.log` not `console.log()`. The round braces +() are used to call the function. If I want to pass the function as a +parameter, I just use the function name without the round braces. The +above example uses a named function which already exists ( `console.log` ), +you can also create new functions on-the-fly and pass them to the +utils.foreach() function... + +```javascript +/* + give every player the ability to fly. +*/ +var utils = require('utils'); +var players = utils.players(); +utils.foreach( players, function( player ) { + player.capabilities.flying = true; + player.updateCapabilities(); +} ); +``` + +... Another example, this time each player will hear a Cat's Meow... + +```javascript +/* + Play a Cat's Meow sound for each player. +*/ +var utils = require('utils'); +var players = utils.players(); +var sounds = require('sounds'); +utils.foreach( players, function( player ) { + sounds.entityCatAmbient( player ); // spigot 1.9 + /* canarymod only + sounds.catMeow( player ); + */ +} ); +``` + +### Exercise +Try changing the above function so that different sounds are played +instead of a Cat's Meow. To see all of the possible sounds that can be +played, load the sounds module at the in-game prompt using the following statement: + + /js var sounds = require('sounds'); + +... then type `/js sounds.` and press the TAB key to see a list of all possible sounds. + +Loops are a key part of programming in any language. Javascript +provides `for` and `while` statements for looping and many javascript +libraries also provide their own custom looping functions. You should +use what you feel most comfortable with. + +## Putting `for` loops to use - Building a Skyscraper + +For loops can be used to build enormous structures. In this next +exercise I'm going to use a for loop to build a skyscraper. This +skyscraper will be made of Glass and Steel (just like most skyscrapers +in real-life). The first thing to do is see what a single floor of +the skyscraper will look like. Place a block (of any type) where you +want to eventually build the skyscraper, then while your cursor is +pointing at the block, type the following into the in-game prompt... + + /js var drone = box(blocks.iron,20,1,20).up().box0(blocks.glass_pane,20,3,20).up(3) + +... you should a large (20x20) iron floor with 3 block high glass all around. + +![skyscraper-floor.png][img_ssf] + +... A skyscraper with just a single floor isn't much of a skyscraper +so the next step is to repeat this over and over. This is where `for` +loops come in. Open your favorite text editor and create a new file in +your scriptcraft/plugins/{your-name} directory, name the file `myskyscraper.js`, then +type the following code and save: + +```javascript +function myskyscraper( floors ) { + var i ; + if ( typeof floors == 'undefined' ) { + floors = 10; + } + // bookmark the drone's position so it can return there later + this.chkpt('myskyscraper'); + for ( i = 0; i < floors; i++ ) { + this + .box(blocks.iron,20,1,20) + .up() + .box0(blocks.glass_pane,20,3,20) + .up(3); + } + // return the drone to where it started + this.move('myskyscraper'); +}; +var Drone = require('drone'); +Drone.extend( myskyscraper ); +``` + +So this takes a little explaining. First I create a new function +called myskyscraper that will take a single parameter `floors` so that +when you eventually call the `myskyscraper()` function you can tell it +how many floors you want built. The first statement in the function +`if (typeof floors == 'undefined'){ floors = 10; }` sets floors to 10 if no parameter is +supplied. The next statement `this.chkpt('myskyscraper')` saves +the position of the Drone so it can eventually return to where it +started when finished building (I don't want the drone stranded atop +the skyscraper when it's finished). Then comes the `for` loop. I loop +from 0 to `floors` and each time through the loop I build a single +floor. When the loop is done I return the drone to where it started. +The last 2 lines load the drone module (it must be loaded before I can +add new features to it) and the last line extends the 'Drone' object +so that now it can build skyscrapers among other things. Once you've +typed in the above code and saved the file, type `/js refresh()` in your +in-game prompt, then type: + + /js myskyscraper(2); + +A two-story skyscraper should appear. If you're feeling +adventurous, try a 10 story skyscraper! Or a 20 story skyscraper! +Minecraft has a height limit (256 blocks from bedrock) beyond which +you can't build. If you try to build higher than this then building +will stop at that height. + +![skyscraper][img_ss] + +I'll leave it as an exercise to the reader to create a city block of +skyscrapers, 5 blocks apart using a for loop. Once you've figured +that out, creating an entire city of blocks of skyscrapers is the next +logical step. Of course, Minecraft doesn't have the same constraints +as real-world densely populated areas so let your imagination go wild. + +## Making Decisions + +All the programs we have seen so far have been fairly predictable - they went +straight through the statements, and then went back to the beginning again. This is +not very useful. In practice the computer would be expected to make decisions and +act accordingly. The javascript statement used for making decisions is `if`. +While standing on the ground in-game, type the following at the command prompt: + + /js if ( self.onGround ) { echo('You are not flying!'); } + +the following message should have appeared on your screen: + + You are not flying! + +Now double-tap the `space` bar to start flying in-game (tap the space bar twice in rapid +succession), then press and hold space to rise above the ground. Now +enter the same statement again (If you don't want to type the same +statement again, just press `/` then press the `UP` cursor key on your +keyboard, the statement you entered previously should reappear. + + /js if ( self.onGround ) { echo('You are not flying!'); } + +This time no message should appear on your screen. + +The `if` statement tests to see if something is `true` or `false` and +if `true` then the block of code between the curly braces ( `{` and +`}` ) is executed - but only if the condition is true. The condition +in the above example is `!self.onGround` (self is _not_ on ground) which +will be `true` if you are currently flying or `false` if you aren't. + +What if you wanted to display a message only if a condition is *not* +true ? For example to only display a message if the player is *not* on the ground: + + /js if ( !self.onGround ) { echo ('You are flying!'); } + +This code differs in that now there's a `!` (the exclamation mark) +before `self.onGround`. The `!` symbol negates (returns the opposite of) +whatever follows it. + +What if you want to display a message in both cases - whether you're +flying or not? This is where the `if - else` construct comes in handy. +Open your favorite editor and type the following code into a new file +in your scriptcraft/plugins directory... + +```javascript +exports.flightStatus = function( player ) { + if ( player.onGround ) { + echo(player, 'You are not flying!' ); + } else { + echo(player, 'Hey, You are flying!' ); + } +} +``` + +... now type `/js refresh()` at the in-game prompt then type `/js +flightStatus(self)` and an appropriate message will appear based on +whether or not you're currently flying. Type the `/js flightStatus()` +command while on the ground and while flying. The message displayed in +each case should be different. + +## Event-Driven programming + +So far we've written code which executes when you invoke the `/js ` +command. What if - for example - you want to have some special +behaviour which occurs when a player joins the game? What if you +wanted to display a custom welcome message (in addition to the MotD - +message-of-the-day which is configurable in your server.properties +file) ? This is where *Event-Driven Programming* comes +in. Event-Driven Programming is just a fancy way of saying 'Do this +when that happens' where 'this' is a function you define, and 'that' +is some event which occurs. There are hundreds of events in the +minecraft game... + + * Every time someone joins the server - that's an event! + * Every time someone breaks a block - that's an event! + * Every time someone shoots an arrow - that's an event! and so on... + +You can write a function which will be called whenever a specific type +of event occurs, it's probably best to illustrate this by example. The +following code sends a message to any player who breaks a block in the +game... + +```javascript +function myBlockBreakHook( event ){ + var breaker = event.player; + echo( breaker, 'You broke a block'); +} +events.blockBreak( myBlockBreakHook ); +``` + +The `events.blockBreak()` function is just one of the many `events` functions which can be used to *register* a function to be called whenever a particular type of event occurs. In the +above code the blockBreak function takes as a parameter a function +I want to be called when that event occurs. The function I want called +in turn takes 1 parameter. The `event` object has all the information +about the event which just occurred. I can tell who broke the block +and send a message to the player. The important thing to note is that +the `myBlockBreakHook` function defined above will not be called until a player breaks a +block. Try it - save the above code in a new file in the +`scriptcraft/plugins` directory then type `/js refresh()` to reload +scriptcraft. Then break a block in the game and you should see the +message 'You broke a block'. + +There are many types of events you can listen for in Minecraft. You can +browse [all possible event registration functions][spevts2] in the API Reference. + +For custom events (events which aren't in the org.bukkit.event tree) +just specify the fully qualified class name instead. E.g. ... + + events.on ( net.yourdomain.events.YourEvent, function( event ) { + ... + }); + +### Stop listening to events. + +If you want an event handler to only execute once, you can remove the handler like this... + +```javascript +function myBlockBreakHook( evt ) { + var breaker = evt.player; + echo( breaker, 'You broke a block'); + this.unregister(); +} +events.blockBreak( myBlockBreakHook ); +``` + +The `this.unregister();` statement will remove this function from the +list of listeners for the event. The `this` keyword when used inside +an event handling function refers to a Listener object provided by +ScriptCraft, it has a single method `unregister()` which can be used +to stop listening for events. + +To unregister a listener *outside* of the listener function... + +```javascript +function myBlockBreakHook( evt ){ + var breaker = evt.player; + echo( breaker, 'You broke a block'); +} +var myBlockBreakListener = events.blockBreak( myBlockBreakHook ); +... +myBlockBreakListener.unregister(); +``` +## Keeping Score - Lookup tables in Javascript + +In the *Event-Driven Programming* section, I defined a function which +displayed a message to players every time they broke a block. Imagine +if I wanted to keep a count of how many blocks each player has broken? +This is where Javascript's Objecct literals come in handy. An object +literal in javascript is simply a way of creating a new Object +on-the-fly in your code. This is an example... + + var myNewObject = { name: 'walter', country: 'Ireland' }; + +... I created a new object with two properties 'name' and +'country'. The notation used to create this object is called JSON +which is short for JavaScript Object Notation. If I want to find out +the 'country' property of the myNewObject variable there are a few +ways I can do it... + + var playerCountry = myNewObject.country; + +... or ... + + var playerCountry = myNewObject['country'] + +... JavaScript lets you access any object property using either +dot-notation ( `object.property` ) or by index ( `object['property']` +). The result in both cases is the same - playerCountry will be +'Ireland'. When accessing the object by indexing, the property doesn't +even have to be a string literal - it can be a variable like this... + + var propertyName = 'country'; + var propertyValue = myNewObject[propertyName]; + +... in the above example, the propertyName variable is used when +indexing. What this means is that every object in JavaScript can act +like a lookup table. What's a lookup table? A table you 'look up' of +course. This is a table of names and scores... + + Name Score + -------- ----- + walter 5 + tom 6 + jane 8 + bart 7 + +... If I want to find Jane's score, I look *down* the list of names in +the name column until I find 'jane' then look *across* to get her +score. In Javascript, an object which stored such a table would look +like this... + +```javascript +var scoreboard = { + walter: 5, + tom: 6, + jane: 8, + bart: 7 +}; +``` + +... and if I wanted to write a function which took a player name as a +parameter and returned their score, I'd do it like this... + +```javascript +function getScore(player){ + return scoreboard[ player ]; +} +``` + +... I might call such a function like this... + +```javascript +var janesScore = getScore('jane'); // returns 8 +``` + +... putting it all together, a hypothetical scoreboard.js mdoule might +look something like this... + +```javascript +var utils = require('utils'); +var scores = {}; + +exports.initialise = function(names){ + scores = {}; + utils.foreach(names, function(name){ + scores[name] = 0; + }); +}; + +/* + changes score by diff e.g. to add 6 to the player's current score + updateScore('walter',6); // walter's new score = 5 + 6 = 11. +*/ +exports.updateScore = function(name, diff){ + scores[name] += diff; +}; + +exports.getScore = function(name){ + return scores[name]; +}; +``` + +## Counting block break events for each player + +I can use a Javascript lookup table (a plain old Javascript object) to +keep a count of how many blocks each player has broken ... + +#### block-break-counter.js + +```javascript +var breaks = {}; + +/* + every time a player joins the game reset their block-break-count to 0 +*/ +function initializeBreakCount( event ){ + breaks[event.player.name] = 0; +} +events.playerJoin( initializeBreakCount ); + +/* + every time a player breaks a block increase their block-break-count +*/ +function incrementBreakCount( event ){ + breaks[event.player.name] += 1; // add 1 + var breakCount = breaks[event.player.name]; + echo( event.player, 'You broke ' + breakCount + ' blocks'); +} +events.blockBreak( incrementBreakCount ); +``` + +With a little more work, you could turn this into a game where players +compete against each other to break as many blocks as possible within +a given time period. + +## Next Steps + +This guide is meant as a gentle introduction to programming and +modding Minecraft using the Javascript Programming Language. +Javascript is a very powerful and widely-used programming language and +there are many more aspects and features of the language which are not +covered here. If you want to dive deeper into programming and modding +minecraft, I recommend reading the accompanying [ScriptCraft API +reference][api] which covers all of the ScriptCraft functions, objects +and methods. I also recommend reading the source code to some of the +existing scriptcraft plugins, followed by +[Anatomy of a ScriptCraft Plug-in][ap]. The online [SpigotMC API +Reference][spigotapi] provides lots of valuable information about the +different objects and methods available for use by ScriptCraft. + + +[cmadmin]: https://github.com/walterhiggins/canarymod-admin-guide/ +[dlbuk2]: http://dl.bukkit.org/downloads/craftbukkit/ +[dlcm]: http://canarymod.net/releases +[bii]: http://wiki.bukkit.org/Setting_up_a_server +[sc-plugin]: http://scriptcraftjs.org/download/ +[ce]: http://www.codecademy.com/ +[mcdv]: http://www.minecraftwiki.net/wiki/Data_values +[np]: http://notepad-plus-plus.org/ +[cbapi]: http://jd.bukkit.org/beta/apidocs/ +[cmapi]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/ +[spigotapi]: https://hub.spigotmc.org/javadocs/spigot/ +[boole]: http://en.wikipedia.org/wiki/George_Boole +[soundapi]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/world/effects/SoundEffect.Type.html +[ap]: Anatomy-of-a-Plugin.md +[api]: API-Reference.md +[twl]: http://www.barebones.com/products/textwrangler/ +[bkevts]: http://jd.bukkit.org/dev/apidocs/org/bukkit/event/package-summary.html +[cmevts]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/package-summary.html +[cmevts2]: API-Reference.md#events-helper-module-canary-version +[spevts2]: API-Reference.md#events-helper-module-spigotmc-version +[img_echo_date]: img/ypgpm_echo_date.png +[img_3d_shapes]: img/ypgpm_3dshapes.jpg +[img_whd]: img/ypgpm_whd.jpg +[img_dv]: img/ypgpm_datavalues.png +[img_ed]: img/ypgpm_ex_dwell.png +[img_2boxes]: img/ypgpm_2boxes.png +[img_cr]: img/ypgpm_mc_cr.png +[img_greet]: img/ypgpm_greet.png +[img_ssf]: img/skyscraper_floor.png +[img_ss]: img/skyscraper.png + diff --git a/src/main/java/bukkit/org/scriptcraftjs/bukkit/ScriptCraftPlugin.java b/src/main/java/bukkit/org/scriptcraftjs/bukkit/ScriptCraftPlugin.java new file mode 100644 index 0000000..9713500 --- /dev/null +++ b/src/main/java/bukkit/org/scriptcraftjs/bukkit/ScriptCraftPlugin.java @@ -0,0 +1,87 @@ +package org.scriptcraftjs.bukkit; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; + +import javax.script.Invocable; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; + +public class ScriptCraftPlugin extends JavaPlugin implements Listener +{ + public boolean canary = false; + public boolean bukkit = true; + // right now all ops share the same JS context/scope + // need to look at possibly having context/scope per operator + //protected Map playerContexts = new HashMap(); + private String NO_JAVASCRIPT_MESSAGE = "No JavaScript Engine available. ScriptCraft will not work without Javascript."; + protected ScriptEngine engine = null; + + @Override public void onEnable() + { + Thread currentThread = Thread.currentThread(); + ClassLoader previousClassLoader = currentThread.getContextClassLoader(); + currentThread.setContextClassLoader(getClassLoader()); + try { + ScriptEngineManager factory = new ScriptEngineManager(); + this.engine = factory.getEngineByName("JavaScript"); + if (this.engine == null) { + this.getLogger().severe(NO_JAVASCRIPT_MESSAGE); + } else { + Invocable inv = (Invocable) this.engine; + this.engine.eval(new InputStreamReader(this.getResource("boot.js"))); + inv.invokeFunction("__scboot", this, engine); + } + } catch (Exception e) { + e.printStackTrace(); + this.getLogger().severe(e.getMessage()); + } finally { + currentThread.setContextClassLoader(previousClassLoader); + } + } + + public List onTabComplete(CommandSender sender, Command cmd, + String alias, + String[] args) + { + List result = new ArrayList(); + if (this.engine == null) { + this.getLogger().severe(NO_JAVASCRIPT_MESSAGE); + return null; + } + try { + Invocable inv = (Invocable)this.engine; + inv.invokeFunction("__onTabComplete", result, sender, cmd, alias, args); + } catch (Exception e) { + sender.sendMessage(e.getMessage()); + e.printStackTrace(); + } + return result; + } + + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) + { + boolean result = false; + Object jsResult = null; + if (this.engine == null) { + this.getLogger().severe(NO_JAVASCRIPT_MESSAGE); + return false; + } + try { + jsResult = ((Invocable)this.engine).invokeFunction("__onCommand", sender, cmd, label, args); + } catch (Exception se) { + this.getLogger().severe(se.toString()); + se.printStackTrace(); + sender.sendMessage(se.getMessage()); + } + if (jsResult != null){ + return ((Boolean)jsResult).booleanValue(); + } + return result; + } +} diff --git a/src/main/java/canary/org/scriptcraftjs/canarymod/ScriptCraftPlugin.java b/src/main/java/canary/org/scriptcraftjs/canarymod/ScriptCraftPlugin.java new file mode 100644 index 0000000..6c16cc1 --- /dev/null +++ b/src/main/java/canary/org/scriptcraftjs/canarymod/ScriptCraftPlugin.java @@ -0,0 +1,164 @@ +package org.scriptcraftjs.canarymod; + +import java.io.InputStreamReader; +import javax.script.ScriptEngineManager; +import javax.script.ScriptEngine; +import javax.script.Invocable; +import java.util.List; +import java.util.ArrayList; + +import net.canarymod.plugin.Plugin; +import net.canarymod.plugin.PluginListener; +import net.canarymod.tasks.ServerTask; +import net.canarymod.tasks.TaskOwner; +import net.canarymod.commandsys.CommandListener; +import net.canarymod.commandsys.Command; +import net.canarymod.commandsys.TabComplete; +import net.canarymod.chat.MessageReceiver; +import net.canarymod.Canary; +// event help stuff +import net.canarymod.hook.Dispatcher; +import net.canarymod.hook.Hook; + +public class ScriptCraftPlugin extends Plugin implements PluginListener, CommandListener +{ + public boolean canary = true; + public boolean bukkit = false; + private String NO_JAVASCRIPT_MESSAGE = "No JavaScript Engine available. " + + "ScriptCraft will not work without Javascript."; + protected ScriptEngine engine = null; + + @Override + public void disable(){ + try { + ((Invocable)this.engine).invokeFunction("__onDisable", this.engine, this); + }catch ( Exception e) { + this.getLogman().error(e.getMessage()); + } + } + + @Override + public boolean enable() + { + try{ + ScriptEngineManager factory = new ScriptEngineManager(); + this.engine = factory.getEngineByName("JavaScript"); + if (this.engine == null){ + this.getLogman().error(NO_JAVASCRIPT_MESSAGE); + } else { + Invocable inv = (Invocable)this.engine; + //File f = new File(this.getJarPath()); + InputStreamReader reader = new InputStreamReader(getClass() + .getClassLoader() + .getResourceAsStream("boot.js")); + this.engine.eval(reader); + inv.invokeFunction("__scboot", this, engine, getClass().getClassLoader()); + } + + Canary.commands().registerCommands(this, this, false); + }catch(Exception e){ + e.printStackTrace(); + this.getLogman().error(e.getMessage()); + } + + + return true; + } + + public static interface IDispatcher { + public void execute(PluginListener listener, Hook hook); + } + + public Dispatcher getDispatcher(final IDispatcher impl){ + return new Dispatcher(){ + public void execute(PluginListener listener, Hook hook){ + impl.execute(listener, hook); + } + }; + } + + static class ScriptCraftTask extends ServerTask { + private Runnable runnable = null; + public ScriptCraftTask(Runnable runnable, TaskOwner owner, long delay, boolean continuous){ + super(owner, delay, continuous); + this.runnable = runnable; + } + @Override + public void run(){ + this.runnable.run(); + } + } + + public ServerTask createServerTask(Runnable runnable, long delay, boolean continuous){ + return new ScriptCraftTask(runnable, this, delay, continuous); + } + + private void executeCommand( MessageReceiver sender, String[] args) { + Object jsResult = null; + if (this.engine == null){ + this.getLogman().error(NO_JAVASCRIPT_MESSAGE); + return; + } + try { + jsResult = ((Invocable)this.engine).invokeFunction("__onCommand", sender, args); + }catch (Exception se){ + this.getLogman().error(se.toString()); + se.printStackTrace(); + sender.message(se.getMessage()); + } + if (jsResult != null){ + return ; + } + return; + } + + @Command( + aliases = { "js" }, + description = "Execute Javascript code", + permissions = { "scriptcraft.evaluate" }, + toolTip = "/js javascript expression") + public void jsCommand(MessageReceiver sender, String[] args) { + + executeCommand(sender, args); + } + + /* + groupmod permission add visitors canary.jsp + groupmod permission add visitors canary.command.jsp + */ + @Command( + aliases = { "jsp" }, + description = "Run javascript-provided command", + permissions = { "" }, + toolTip = "/jsp command") + public void jspCommand(MessageReceiver sender, String[] args) { + + executeCommand(sender, args); + } + + private List complete(MessageReceiver sender, String[] args, String cmd){ + List result = new ArrayList(); + if (this.engine == null){ + this.getLogman().error(NO_JAVASCRIPT_MESSAGE); + return null; + } + try { + Invocable inv = (Invocable)this.engine; + inv.invokeFunction("__onTabComplete", result, sender, args, cmd); + }catch (Exception e){ + sender.message(e.getMessage()); + e.printStackTrace(); + } + return result; + } + + @TabComplete (commands = { "js" }) + public List jsComplete(MessageReceiver sender, String[] args){ + return complete(sender, args, "js"); + } + + @TabComplete (commands = { "jsp" }) + public List jspComplete(MessageReceiver sender, String[] args){ + return complete(sender, args, "jsp"); + } +} diff --git a/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java b/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java deleted file mode 100644 index 2df8324..0000000 --- a/src/main/java/net/walterhiggins/scriptcraft/ScriptCraftPlugin.java +++ /dev/null @@ -1,185 +0,0 @@ -package net.walterhiggins.scriptcraft; - -import java.io.File; -import java.io.FileReader; -import java.io.FileOutputStream; -import java.io.IOException; -import javax.script.*; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; -import java.util.Collection; -import java.util.Arrays; -import java.util.List; -import java.util.ArrayList; - -import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.command.*; -import org.bukkit.Bukkit; -import org.bukkit.event.Listener; - -public class ScriptCraftPlugin extends JavaPlugin implements Listener -{ - // right now all ops share the same JS context/scope - // need to look at possibly having context/scope per operator - //protected Map playerContexts = new HashMap(); - protected ScriptEngine engine = null; - private static final String JS_PLUGINS_DIR = "js-plugins"; - - /** - * Unzips bundled javascript code. - */ - private void unzipJS() - { - // - // does the js-plugins directory exist? - // - File jsPlugins = new File(JS_PLUGINS_DIR); - if (!jsPlugins.exists()) - { - getLogger().finest("Directory " + JS_PLUGINS_DIR + " does not exist."); - getLogger().finest("Initializing " + JS_PLUGINS_DIR + " directory with contents from plugin archive."); - jsPlugins.mkdir(); - } - - ZipInputStream zis = new ZipInputStream(getResource(JS_PLUGINS_DIR + ".zip")); - ZipEntry entry; - try { - while ( ( entry = zis.getNextEntry() ) != null) - { - String filename = entry.getName(); - File newFile = new File(jsPlugins.getName() + File.separator + filename); - - //create all non exists folders - //else you will hit FileNotFoundException for compressed folder - if (entry.isDirectory()){ - newFile.mkdirs(); - }else{ - // - // only write out to file if zip entry is newer than file - // - long zTime = entry.getTime(); - boolean unzip = false; - if (!newFile.exists()) - unzip = true; - else{ - long fTime = newFile.lastModified(); - if (zTime > fTime) - unzip = true; - } - if (unzip){ - getLogger().info("Unzipping " + filename); - FileOutputStream fout = new FileOutputStream(newFile); - for (int c = zis.read(); c != -1; c = zis.read()) { - fout.write(c); - } - fout.close(); - } - - } - zis.closeEntry(); - } - zis.close(); - }catch (IOException ioe){ - getLogger().warning(ioe.getMessage()); - ioe.printStackTrace(); - } - } - - @Override - public void onEnable() - { - unzipJS(); - FileReader reader = null; - try{ - ScriptEngineManager factory = new ScriptEngineManager(); - File boot = new File(JS_PLUGINS_DIR + "/core/_scriptcraft.js"); - this.engine = factory.getEngineByName("JavaScript"); - this.engine.put("__engine",engine); - this.engine.put("__plugin",this); - this.engine.put("__script",boot.getCanonicalPath().replaceAll("\\\\","/")); - reader = new FileReader(boot); - this.engine.eval(reader); - /* - wph 20130811 Need to disable coffeescript support until issues loading and evaluating it are resolved. - See issue #92 - // Load the CoffeeScript compiler - File coffeescript = new File(JS_PLUGINS_DIR + "/core/_coffeescript.js"); - this.engine.eval(new FileReader(coffeescript)); - */ - - }catch(Exception e){ - e.printStackTrace(); - }finally { - if (reader != null){ - try { - reader.close(); - }catch(IOException ioe){ - // fail silently - } - } - } - } - public List onTabComplete(CommandSender sender, Command cmd, - String alias, - String[] args) - { - // - // delegate to javascript - // - List result = new ArrayList(); - try { - this.engine.put("__onTC_result",result); - this.engine.put("__onTC_sender",sender); - this.engine.put("__onTC_cmd",cmd); - this.engine.put("__onTC_alias",alias); - this.engine.put("__onTC_args",args); - this.engine.eval("_onTabComplete()"); - }catch (Exception e){ - sender.sendMessage(e.getMessage()); - e.printStackTrace(); - } - return result; - } - - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) - { - boolean result = false; - String javascriptCode = ""; - - if(cmd.getName().equalsIgnoreCase("js")){ - for (int i = 0;i < args.length; i++){ - javascriptCode = javascriptCode + args[i] + " "; - } - result = true; - } else if (cmd.getName().equalsIgnoreCase("jsp")){ - javascriptCode = "command()"; - this.engine.put("__cmdArgs",args); - result = true; - } else if (cmd.getName().equalsIgnoreCase("coffee")) { - for (int i = 0;i < args.length; i++) - javascriptCode += args[i] + " "; - javascriptCode = "eval(CoffeeScript.compile(\""+javascriptCode+"\", {bare: true}))"; - result = true; - } - - if (result){ - this.engine.put("self",sender); - try{ - Object resultObj = this.engine.eval(javascriptCode); - if (resultObj != null){ - if (resultObj instanceof java.util.Collection){ - java.util.Collection collection = (java.util.Collection)resultObj; - sender.sendMessage(Arrays.toString(collection.toArray())); - }else{ - sender.sendMessage(resultObj.toString()); - } - } - }catch (Exception e){ - sender.sendMessage(e.getMessage()); - e.printStackTrace(); - } - } - return result; - } - -} diff --git a/src/main/javascript/alias/alias.js b/src/main/javascript/alias/alias.js deleted file mode 100644 index c4cf0d7..0000000 --- a/src/main/javascript/alias/alias.js +++ /dev/null @@ -1,75 +0,0 @@ -plugin("alias", { - help: function(){ - return [ - "/jsp alias set : Set a shortcut/alias for one or more commands (separated by ';')\n" + - "For example: '/jsp alias set sunny time set 4000; weather clear'\n" + - "/jsp sunny (is the same as..\n/time set 4000\n/weather clear", - "/jsp alias delete : Removes a shortcut/alias", - "/jsp alias list : shows a list of the player's command aliases", - "/jsp alias help : Shows this message" - ]; - }, - set: function(player, alias, commands){ - var aliases = this.store.players; - var name = player.name; - aliases[name] = aliases[name] || {}; - aliases[name][alias] = commands; - }, - remove: function(player, alias){ - var aliases = this.store.players; - if (aliases[player.name]) - delete aliases[player.name][alias]; - }, - list: function(player){ - var result = []; - var aliases = this.store.players[player.name]; - for (var a in aliases) - result.push(a + " = " + aliases[a].join(";")); - return result; - } -},true); - -alias.store.players = alias.store.players || {}; - -command("alias",function(params){ - /* - this function also intercepts command options for /jsp - */ - if (params[0] === "help"){ - self.sendMessage(alias.help()); - return; - } - if (params[0] === "set"){ - var aliasCmd = params[1]; - var cmdStr = params.slice(2).join(' '); - var cmds = cmdStr.split(';'); - alias.set(self,aliasCmd,cmds); - return; - } - if (params[0] === "delete"){ - alias.remove(self,params[1]); - return ; - } - if (params[0] === "list"){ - self.sendMessage(alias.list(self)); - return; - } - if (params.length == 0) - return self.sendMessage(alias.help()); - - var playerHasAliases = alias.store.players[self.name]; - if (!playerHasAliases) - return false; - // is it an alias? - var commands = playerHasAliases[params[0]]; - if (!commands) - return false; - for (var i = 0;i < commands.length; i++){ - // fill in template - var cmd = commands[i]; - cmd = cmd.replace(/{([0-9]*)}/g,function(dummy,index){ return params[index] || "";}) - self.performCommand(cmd); - } - return true; - -},["help","set","delete","list"],true); diff --git a/src/main/javascript/arrows/arrows.js b/src/main/javascript/arrows/arrows.js deleted file mode 100644 index b00cdd0..0000000 --- a/src/main/javascript/arrows/arrows.js +++ /dev/null @@ -1,138 +0,0 @@ -/* - - The arrows mod adds fancy arrows to the game. - - Usage: - - /js arrows.sign() turns a targeted sign into a Arrows menu - /js arrows.normal() sets arrow type to normal. - /js arrows.explosive() - makes arrows explode. - /js arrows.teleport() - makes player teleport to where arrow has landed. - /js arrows.flourish() - makes a tree grow where the arrow lands. - /js arrows.lightning() - lightning strikes where the arrow lands. - /js arrows.firework() - A firework launches where the the arrow lands. - - All of the above functions can take an optional player object or name as - a parameter. E.g. - - /js arrows.explosive('player23') makes player23's arrows explosive. - -*/ - -var arrows = arrows || plugin("arrows",{ - /* - turn a sign into a menu of arrow choices - */ - sign: function(sign){}, - /* - change player's arrows to normal - */ - normal: function(player){}, - /* - change player's arrows to explode on impact - */ - explosive: function(player){}, - /* - change player's arrows to teleporting - */ - teleport: function(player){}, - /* - change player's arrows to plant trees where they land - */ - flourish: function(player){}, - /* - change player's arrows to strike lightning where they land - */ - lightning: function(player){}, - - /* - launch a firework where the arrow lands - */ - explosiveYield: 2.5 - -},true); -/* - initialize data -*/ -arrows.store.players = arrows.store.players || {}; - -/* - private implementation of normal, explosive, teleport, flourish and lightning functions -*/ -(function(){ - // - // setup functions for the arrow types - // - var _types = {normal: 0, explosive: 1, teleport: 2, flourish: 3, lightning: 4, firework: 5}; - for (var type in _types) - { - arrows[type] = (function(n){ - return function(player){ - if (typeof player == "undefined") - player = self; - var playerName = null; - if (typeof player == "string") - playerName = player; - else - playerName = player.name; - arrows.store.players[playerName] = n; - }; - })(_types[type]); - } -}()); -/* - Arrows depends on 2 other modules: 'signs' and 'events' so the following code - can't execute until all modules have loaded (ready). -*/ -ready(function() -{ - /* - called when the player chooses an arrow option from a menu sign - */ - var _onMenuChoice = function(event){ - arrows.store.players[event.player.name] = event.number; - }; - arrows.sign = signs.menu("Arrow", - ["Normal","Explosive","Teleport","Flourish","Lightning","Firework"], - _onMenuChoice ); - - /* - event handler called when a projectile hits something - */ - var _onArrowHit = function(listener,event) - { - var projectile = event.entity; - var world = projectile.world; - var shooter = projectile.shooter; - if (projectile instanceof org.bukkit.entity.Arrow && - shooter instanceof org.bukkit.entity.Player) - { - var arrowType = arrows.store.players[shooter.name]; - switch (arrowType){ - case 1: - projectile.remove(); - world.createExplosion(projectile.location,arrows.explosiveYield); - break; - case 2: - projectile.remove(); - var teleportCause =org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; - shooter.teleport(projectile.location, - teleportCause.PLUGIN); - break; - case 3: - projectile.remove(); - world.generateTree(projectile.location, org.bukkit.TreeType.BIG_TREE); - break; - case 4: - projectile.remove(); - world.strikeLightning(projectile.location); - break; - case 5: - projectile.remove(); - fireworks.firework(projectile.location); - break; - } - } - }; - events.on("entity.ProjectileHitEvent",_onArrowHit); -}); diff --git a/src/main/javascript/chat/color.js b/src/main/javascript/chat/color.js deleted file mode 100644 index 2e28bdc..0000000 --- a/src/main/javascript/chat/color.js +++ /dev/null @@ -1,53 +0,0 @@ -/* - declare a new javascript plugin for changing chat text color -*/ -var chat = chat || plugin("chat", { - /* - set the color of text for a given player - */ - setColor: function(player, color){ - this.store.players[player.name] = color; - } -},true); -/* - initialize the store -*/ -chat.store.players = chat.store.players || {}; - -ready(function() -{ - var colors = [ - "black", "blue", "darkgreen", "darkaqua", "darkred", - "purple", "gold", "gray", "darkgray", "indigo", - "brightgreen", "aqua", "red", "pink", "yellow", "white" - ]; - var colorCodes = {}; - for (var i =0;i < colors.length;i++) { - var hexCode = i.toString(16); - colorCodes[colors[i]] = hexCode; - } - - events.on("player.AsyncPlayerChatEvent",function(l,e){ - var player = e.player; - var playerChatColor = chat.store.players[player.name]; - if (playerChatColor){ - e.message = "§" + colorCodes[playerChatColor] + e.message; - } - }); - var listColors = function(params){ - var colorNamesInColor = []; - for (var i = 0;i < colors.length;i++) - colorNamesInColor[i] = "§"+colorCodes[colors[i]] + colors[i]; - self.sendMessage("valid chat colors are " + colorNamesInColor.join(", ")); - }; - command("list_colors", listColors); - command("chat_color",function(params){ - var color = params[0]; - if (colorCodes[color]){ - chat.setColor(self,color); - }else{ - self.sendMessage(color + " is not a valid color"); - listColors(); - } - },colors); -}); diff --git a/src/main/javascript/classroom/classroom.js b/src/main/javascript/classroom/classroom.js deleted file mode 100644 index 57f9fbc..0000000 --- a/src/main/javascript/classroom/classroom.js +++ /dev/null @@ -1,78 +0,0 @@ -/************************************************************************ -Classroom Module -================ -The `classroom` object contains a couple of utility functions for use -in a classroom setting. The goal of these functions is to make it -easier for tutors to facilitate ScriptCraft for use by students in a -classroom environment. Although granting ScriptCraft access to -students on a shared server is potentially risky (Students can -potentially abuse it), it is slighlty less risky than granting -operator privileges to each student. (Enterprising students will -quickly realise how to grant themselves and others operator privileges -once they have access to ScriptCraft). - -The goal of this module is not so much to enforce restrictions -(security or otherwise) but to make it easier for tutors to setup a shared server -so students can learn Javascript. - -classroom.allowScripting() function -=================================== -Allow or disallow anyone who connects to the server (or is already -connected) to use ScriptCraft. This function is preferable to granting 'ops' privileges -to every student in a Minecraft classroom environment. - -Parameters ----------- - - * canScript : true or false - -Example -------- -To allow all players (and any players who connect to the server) to -use the `js` and `jsp` commands... - - /js classroom.allowScripting(true) - -To disallow scripting (and prevent players who join the server from using the commands)... - - /js classroom.allowScripting(false) - -Only ops users can run the classroom.allowScripting() function - this is so that students -don't try to bar themselves and each other from scripting. - -***/ -var classroom = { - allowScripting: function(/* boolean: true or false */ canScript){} -}; - -ready(function(){ - classroom.allowScripting = function(canScript) - { - /* - only operators should be allowed run this function - */ - if (!self.isOp()) - return; - if (canScript){ - utils.foreach( server.onlinePlayers, function (player) { - player.addAttachment(__plugin, "scriptcraft.*", true); - }); - }else{ - utils.foreach( server.onlinePlayers, function(player) { - utils.foreach(player.getEffectivePermissions(), function(perm) { - if ((""+perm.permission).indexOf("scriptcraft.") == 0){ - if (perm.attachment) - perm.attachment.remove(); - } - }); - }); - } - classroom.canScript = canScript; - }; - events.on("player.PlayerLoginEvent", function(listener, event) { - var player = event.player; - if (classroom.canScript){ - player.addAttachment(__plugin, "scriptcraft.*", true); - } - }, "HIGHEST"); -}); diff --git a/src/main/javascript/core/_primitives.js b/src/main/javascript/core/_primitives.js deleted file mode 100644 index 1ba6f78..0000000 --- a/src/main/javascript/core/_primitives.js +++ /dev/null @@ -1,135 +0,0 @@ -var global = this; -// -// Define these primitive methods used by drone.js (and potentially 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; - } - - 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 pl = org.bukkit.entity.Player; - var cs = org.bukkit.command.BlockCommandSender; - var world = (self instanceof pl)?self.location.world:(self instanceof cs)?self.block.location.world:null; - var block = world.getBlockAt(x,y,z); - if (block.typeId != blockId || block.data != metadata) - block.setTypeIdAndData(blockId,metadata,false); - - }; - - var _putSign = function(texts, x, y, z, blockId, meta){ - if (blockId != 63 && blockId != 68) - throw new Error("Invalid Parameter: blockId must be 63 or 68"); - putBlock(x,y,z,blockId,meta); - var block = _getBlockObject(x,y,z); - var state = block.state; - if (state instanceof org.bukkit.block.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){ - if (typeof z == "undefined"){ - var loc = _getMousePos() - if (loc) - return world.getBlockAt(loc); - }else{ - return world.getBlockAt(x,y,z); - } - } - }; - - var _getWorld = function(){ - if (self instanceof org.bukkit.entity.Player) - return self.location.world; - if (typeof self == "undefined") - return; - if (self instanceof org.bukkit.command.BlockCommandSender) - return self.block.location.world; - }; - - var _notifyAdministrators = function(msg){ - var ops = __plugin.server.operators.toArray(); - for (var i = 0; i < ops.length;i++){ - var op = ops[i]; - if (op.isOnline()) - op.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); - }; - - global.getPlayerPos = _getPlayerPos; - global.getMousePos = _getMousePos; - global.putBlock = _putBlock; - global.getBlock = _getBlock; - global.putSign = _putSign; - global.notifyAdministrators = _notifyAdministrators; - global.echo = _echo; - -}()); diff --git a/src/main/javascript/core/_scriptcraft.js b/src/main/javascript/core/_scriptcraft.js deleted file mode 100644 index e012e8b..0000000 --- a/src/main/javascript/core/_scriptcraft.js +++ /dev/null @@ -1,798 +0,0 @@ -/************************************************************************ -ScriptCraft API Reference -========================= - -Walter Higgins - -[walter.higgins@gmail.com][email] - -[email]: mailto:walter.higgins@gmail.com?subject=ScriptCraft_API_Reference - -Module Loading -============== -At server startup the ScriptCraft Java plugin is loaded and once -loaded the Java plugin will in turn begin loading all of the -javascript (.js) files it finds in the js-plugins directory (in the -current working directory). If this is the first time the ScriptCraft -plugin is loaded, then the js-plugins directory will not yet exist, it -will be created and all of the bundled javascript files will be -unzipped into it from a bundled resource within the Java plugin. The -very first javascript file to load will always be -js-plugins/core/_scriptcraft.js. Then all other javascript files are -loaded. - -Directory structure -------------------- -The js-plugins directory is loosely organised into subdirectories - -one for each module. Each subdirectory in turn can contain one or more -javascript files. Within each directory, a javascript file with the -same filename as the directory will always be loaded before all other -files in the same directory. So for example, drone/drone.js will -always load before any other files in the drone/ directory. Similarly -utils/utils.js will always load before any other files in the utils/ -directory. - -Directories ------------ -As of February 10 2013, the js-plugins directory has the following sub-directories... - - * core - Contains javascript files containing Core functionality crucial to ScriptCraft and modules which use it. - * drone - Contains the drone module and drone extensions. Drone was the first scriptcraft module. - * ext - Contains external 3rd party javascript libraries (e.g. json2.js - the JSON lib) - * mini-games - Contains mini-games - * arrows - The arrows module - * signs - The signs module - * chat - The chat plugin/module - * alias - The alias plugin/module - -Core Module -=========== -This module defines commonly used functions by all plugins... - - * load (filename,warnOnFileNotFound) - loads and evaluates a javascript file, returning the evaluated object. - - * save (object, filename) - saves an object to a file. - - * plugin (name, interface, isPersistent) - defines a new plugin. If - isPersistent is true then the plugin doesn't have to worry about - loading and saving state - that will be done by the framework. Just - make sure that anything you want to save (and restore) is in the - 'store' property - this will be created automatically if not - already defined. (its type is object {} ) - - * ready (function) - specifies code to be executed only when all the plugins have loaded. - - * command (name, function) - defines a command that can be used by non-operators. - -load() function ---------------- -The load() function is used by ScriptCraft at startup to load all of -the javascript modules and data. You normally wouldn't need to call -this function directly. If you put a javascript file anywhere in the -craftbukkit/js-plugins directory tree it will be loaded automatically -when craftbukkit starts up. The exception is files whose name begins -with an underscore `_` character. These files will not be -automatically loaded at startup as they are assumed to be files -managed / loaded by plugins. - -Parameters ----------- - - * filenames - An array of file names or a single file name. - * warnOnFileNotFound (optional - default: false) - warn if the file was not found. - -Return ------- -load() will return the result of the last statement evaluated in the file. - -Example -------- - - load(__folder + "myFile.js"); // loads a javascript file and evaluates it. - - var myData = load("myData.json"); // loads a javascript file and evaluates it - eval'd contents are returned. - -myData.json contents... - - __data = {players:{ - walterh:{ - h: ["jsp home {1}"], - sunny:["time set 0", - "weather clear"] - } - } - } - -save() function ---------------- -The save() function saves an in-memory javascript object to a -specified file. Under the hood, save() uses JSON (specifically -json2.js) to save the object. Again, there will usually be no need to -call this function directly as all javascript plugins' state are saved -automatically if they are declared using the `plugin()` function. Any -in-memory object saved using the `save()` function can later be -restored using the `load()` function. - -Parameters ----------- - - * objectToSave : The object you want to save. - * filename : The name of the file you want to save it to. - -Example -------- - - var myObject = { name: 'John Doe', - aliases: ['John Ray', 'John Mee'], - date_of_birth: '1982/01/31' }; - save(myObject, 'johndoe.json'); - -johndoe.json contents... - - var __data = { "name": "John Doe", - "aliases": ["John Ray", "John Mee"], - "date_of_birth": "1982/01/31" }; - -plugin() function ------------------ -The `plugin()` function should be used to declare a javascript module -whose state you want to have managed by ScriptCraft - that is - a -Module whose state will be loaded at start up and saved at shut down. -A plugin is just a regular javascript object whose state is managed by -ScriptCraft. The only member of the plugin which whose persistence is -managed by Scriptcraft is `state` - this special member will be -automatically saved at shutdown and loaded at startup by -ScriptCraft. This makes it easier to write plugins which need to -persist data. - -Parameters ----------- - - * pluginName (String) : The name of the plugin - this becomes a global variable. - * pluginDefinition (Object) : The various functions and members of the plugin object. - * isPersistent (boolean - optional) : Specifies whether or not the plugin/object state should be loaded and saved by ScriptCraft. - -Example -------- -See chat/color.js for an example of a simple plugin - one which lets -players choose a default chat color. See also [Anatomy of a -ScriptCraft Plugin][anatomy]. - -[anatomy]: http://walterhiggins.net/blog/ScriptCraft-1-Month-later - -command() function ------------------- -The `command()` function is used to expose javascript functions for -use by non-operators (regular players). Only operators should be -allowed use raw javascript using the `/js ` command because it is too -powerful for use by regular players and can be easily abused. However, -the `/jsp ` command lets you (the operator / server administrator / -plugin author) safely expose javascript functions for use by players. - -Parameters ----------- - - * commandName : The name to give your command - the command will be invoked like this by players `/jsp commandName` - * commandFunction: The javascript function which will be invoked when the command is invoked by a player. - * options (Array - optional) : An array of command options/parameters - which the player can supply (It's useful to supply an array so that - Tab-Completion works for the `/jsp ` commands. - * intercepts (boolean - optional) : Indicates whether this command - can intercept Tab-Completion of the `/jsp ` command - advanced - usage - see alias/alias.js for example. - -Example -------- -See chat/colors.js or alias/alias.js or homes/homes.js for examples of how to use the `command()` function. - -ready() function ----------------- -The `ready()` function provides a way for plugins to do additional -setup once all of the other plugins/modules have loaded. For example, -event listener registration can only be done after the -events/events.js module has loaded. A plugin author could load the -file explicilty like this... - - load(__folder + "../events/events.js"); - - // event listener registration goes here - -... or better still, just do event regristration using the `ready()` -handler knowing that by the time the `ready()` callback is invoked, -all of the scriptcraft modules have been loaded... - - ready(function(){ - // event listener registration goes here - // code that depends on other plugins/modules also goes here - }); - -The execution of the function object passed to the `ready()` function -is *deferred* until all of the plugins/modules have loaded. That way -you are guaranteed that when the function is invoked, all of the -plugins/modules have been loaded and evaluated and are ready to use. -***/ - -var global = this; - -/************************************************************************* -setTimeout() function ---------------------- - -This function mimics the setTimeout() function used in browser-based javascript. -However, the function will only accept a function reference, not a string of javascript code. -Where setTimeout() in the browser returns a numeric value which can be subsequently passed to -clearTimeout(), This implementation returns a [BukkitTask][btdoc] object which can be subsequently passed to ScriptCraft's own clearTimeout() implementation. - -If Node.js supports setTimeout() then it's probably good for ScriptCraft to support it too. - -[btdoc]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/BukkitTask.html - -***/ - global.setTimeout = function( callback, delayInMillis){ - // - // javascript programmers familiar with setTimeout know that it expects - // a delay in milliseconds. However, bukkit's scheduler expects a delay in ticks - // (where 1 tick = 1/20th second) - // - var bukkitTask = server.scheduler.runTaskLater(__plugin, callback, delayInMillis/50); - return bukkitTask; - }; - -/************************************************************************* -clearTimeout() function ---------------------- -A scriptcraft implementation of clearTimeout(). - -***/ - global.clearTimeout = function(bukkitTask){ - bukkitTask.cancel(); - }; - -/************************************************************************* -setInterval() function ---------------------- - -This function mimics the setInterval() function used in browser-based javascript. -However, the function will only accept a function reference, not a string of javascript code. -Where setInterval() in the browser returns a numeric value which can be subsequently passed to -clearInterval(), This implementation returns a [BukkitTask][btdoc] object which can be subsequently passed to ScriptCraft's own clearInterval() implementation. - -If Node.js supports setInterval() then it's probably good for ScriptCraft to support it too. - -[btdoc]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/BukkitTask.html - -***/ - global.setInterval = function(callback, intervalInMillis){ - var delay = intervalInMillis/ 50; - var bukkitTask = server.scheduler.runTaskTimer(__plugin, callback, delay, delay); - return bukkitTask; - }; -/************************************************************************* -clearInterval() function ---------------------- -A scriptcraft implementation of clearInterval(). - -***/ - global.clearInterval = function(bukkitTask){ - bukkitTask.cancel(); - }; - -/************************************************************************* -Core Module - Special Variables -=============================== -There are a couple of special javascript variables available in ScriptCraft... - - * __folder - The current working directory - this variable is only to be used within the main body of a .js file. - * __plugin - The ScriptCraft JavaPlugin object. - * server - The Minecraft Server object. - * self - the current player. (Note - this value should not be used in multi-threaded scripts - it's not thread-safe) - -***/ -var verbose = verbose || false; -/* - wph 20130124 - make self, plugin and server public - these are far more useful now that tab-complete works. -*/ -var server = org.bukkit.Bukkit.server; -// -// private implementation -// -(function(){ - // - // don't execute this more than once - // - if (typeof load == "function") - return ; - - var _canonize = function(file){ return file.getCanonicalPath().replaceAll("\\\\","/"); }; - - var _originalScript = __script; - var parentFileObj = new java.io.File(__script).getParentFile(); - var jsPluginsRootDir = parentFileObj.getParentFile(); - var jsPluginsRootDirName = _canonize(jsPluginsRootDir); - - - var _loaded = {}; - /* - Load the contents of the file and evaluate as javascript - */ - var _load = function(filename,warnOnFileNotFound) - { - var filenames = []; - if (filename.constructor == Array) - filenames = filename; - else - filenames = [filename]; - - var result = null; - - for (var i =0;i < filenames.length; i++) { - - var file = new java.io.File(filenames[0]); - var canonizedFilename = _canonize(file); - // - // wph 20130123 don't load the same file more than once. - // - if (_loaded[canonizedFilename]) - continue; - - if (verbose) - print("loading " + canonizedFilename); - - if (file.exists()) { - var parent = file.getParentFile(); - var reader = new java.io.FileReader(file); - var br = new java.io.BufferedReader(reader); - __engine.put("__script",canonizedFilename); - __engine.put("__folder",(parent?_canonize(parent):"")+"/"); - - var code = ""; - try{ - if (file.getCanonicalPath().endsWith(".coffee")) { - var r = undefined; - while ((r = br.readLine()) !== null) code += "\"" + r + "\" +\n"; - code += "\"\""; - var code = "load(__folder + \"../core/_coffeescript.js\"); var ___code = "+code+"; eval(CoffeeScript.compile(___code, {bare: true}))"; - } else { - while ((r = br.readLine()) !== null) code += r + "\n"; - } - - result = __engine.eval(code); - _loaded[canonizedFilename] = true; - reader.close(); - }catch (e){ - __plugin.logger.severe("Error evaluating " + canonizedFilename + ", " + e ); - } - }else{ - if (warnOnFileNotFound) - __plugin.logger.warning(canonizedFilename + " not found"); - } - } - - return result; - }; - /* - recursively walk the given directory and return a list of all .js files - */ - var _listSourceFiles = function(store,dir) - { - if (typeof dir == "undefined"){ - dir = new java.io.File(_originalScript).getParentFile().getParentFile(); - } - var files = dir.listFiles(); - for (var i = 0;i < files.length; i++){ - var file = files[i]; - if (file.isDirectory()){ - _listSourceFiles(store,file); - }else{ - if ((file.getCanonicalPath().endsWith(".js") || file.getCanonicalPath().endsWith(".coffee")) && - !(file.getName().startsWith("_")) && - file.exists()) - { - store.push(file); - } - } - } - }; - /* - sort so that .js files with same name as parent directory appear before - other files in the same directory - */ - var sortByModule = function(a,b){ - a = _canonize(a); - b = _canonize(b); - var aparts = (""+a).split(/\//); - var bparts = (""+b).split(/\//); - //var adir = aparts[aparts.length-2]; - var adir = aparts.slice(0,aparts.length-1).join("/"); - var afile = aparts[aparts.length-1]; - //var bdir = bparts[bparts.length-2]; - var bdir = bparts.slice(0,bparts.length-1).join("/"); - var bfile = bparts[bparts.length-1]; - - if(adirbdir) return 1; - - afile = afile.match(/[a-zA-Z0-9\-_]+/)[0]; - - if (adir.match(new RegExp(afile + "$"))) - return -1; - else - return 1; - }; - /* - Reload all of the .js files in the given directory - */ - var _reload = function(pluginDir) - { - _loaded = []; - var sourceFiles = []; - _listSourceFiles(sourceFiles,pluginDir); - - sourceFiles.sort(sortByModule); - - // - // 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 - // - var len = sourceFiles.length; - for (var i = 0;i < len; i++){ - load(_canonize(sourceFiles[i]),true); - } - }; - - /* - Save a javascript object to a file (saves using JSON notation) - */ - var _save = function(object, filename){ - var objectToStr = null; - try{ - objectToStr = JSON.stringify(object); - }catch(e){ - print("ERROR: " + e.getMessage() + " while saving " + filename); - return; - } - var f = new java.io.File(filename); - var out = new java.io.PrintWriter(new java.io.FileWriter(f)); - out.println("__data = " + objectToStr); - out.close(); - }; - /* - plugin management - */ - var _plugins = {}; - var _plugin = function(/* String */ moduleName, /* Object */ moduleObject, isPersistent) - { - // - // don't load plugin more than once - // - if (typeof _plugins[moduleName] != "undefined") - return; - - var pluginData = {persistent: isPersistent, module: moduleObject}; - moduleObject.store = moduleObject.store || {}; - _plugins[moduleName] = pluginData; - - if (isPersistent) - moduleObject.store = load(jsPluginsRootDirName + "/" + moduleName + "-store.txt") || {}; - - global[moduleName] = moduleObject; - return moduleObject; - }; - /* - allow for deferred execution (once all modules have loaded) - */ - var _deferred = []; - var _ready = function( func ){ - _deferred.push(func); - }; - var _cmdInterceptors = []; - /* - command management - allow for non-ops to execute approved javascript code. - */ - var _commands = {}; - var _command = function(name,func,options,intercepts) - { - if (typeof name == "undefined"){ - // it's an invocation from the Java Plugin! - if (__cmdArgs.length === 0) - throw new Error("Usage: jsp command-name command-parameters"); - var name = __cmdArgs[0]; - var cmd = _commands[name]; - if (typeof cmd === "undefined"){ - // it's not a global command - pass it on to interceptors - var intercepted = false; - for (var i = 0;i < _cmdInterceptors.length;i++){ - if (_cmdInterceptors[i](__cmdArgs)) - intercepted = true; - } - if (!intercepted) - self.sendMessage("Command '" + name + "' is not recognised"); - }else{ - func = cmd.callback; - var params = []; - for (var i =1; i < __cmdArgs.length;i++){ - params.push("" + __cmdArgs[i]); - } - return func(params); - } - }else{ - if (typeof options == "undefined") - options = []; - _commands[name] = {callback: func, options: options}; - if (intercepts) - _cmdInterceptors.push(func); - return func; - } - }; - var _rmCommand = function(name){ - delete _commands[name]; - }; - /* - Tab Completion of the /js and /jsp commands - */ - var _isJavaObject = function(o){ - var result = false; - try { - o.hasOwnProperty("testForJava"); - }catch (e){ - // java will throw an error when an attempt is made to access the - // hasOwnProperty method. (it won't exist for Java objects) - result = true; - } - return result; - }; - var _javaLangObjectMethods = ["equals","getClass","class","getClass","hashCode","notify","notifyAll","toString","wait","clone","finalize"]; - var _getProperties = function(o) - { - var result = []; - if (_isJavaObject(o)) - { - propertyLoop: - for (var i in o) - { - // - // don't include standard Object methods - // - var isObjectMethod = false; - for (var j = 0;j < _javaLangObjectMethods.length; j++) - if (_javaLangObjectMethods[j] == i) - continue propertyLoop; - if (typeof o[i] == "function" ) - result.push(i+"()"); - else - result.push(i); - } - }else{ - if (o.constructor == Array) - return result; - - for (var i in o){ - if (i.match(/^[^_]/)){ - if (typeof o[i] == "function") - result.push(i+"()"); - else - result.push(i); - } - } - } - return result.sort(); - }; - /* - Tab completion for the /jsp commmand - */ - var __onTabCompleteJSP = function() { - var result = global.__onTC_result; - var args = global.__onTC_args; - var cmdInput = args[0]; - var cmd = _commands[cmdInput]; - if (cmd){ - var opts = cmd.options; - var len = opts.length; - if (args.length == 1){ - for (var i = 0;i < len; i++) - result.add(opts[i]); - }else{ - // partial e.g. /jsp chat_color dar - for (var i = 0;i < len; i++){ - if (opts[i].indexOf(args[1]) == 0){ - result.add(opts[i]); - } - } - } - }else{ - if (args.length == 0){ - for (var i in _commands) - result.add(i); - }else{ - // partial e.g. /jsp al - // should tabcomplete to alias - // - for (var c in _commands){ - if (c.indexOf(cmdInput) == 0){ - result.add(c); - } - } - } - } - return result; - }; - /* - Tab completion for the /js command - */ - var __onTabCompleteJS = function() - { - if (__onTC_cmd.name == "jsp") - return __onTabCompleteJSP() - - var _globalSymbols = _getProperties(global) - var result = global.__onTC_result; - var args = global.__onTC_args; - var lastArg = args.length?args[args.length-1]+"":null; - var propsOfLastArg = []; - var statement = args.join(" "); - - statement = statement.replace(/^\s+/,"").replace(/\s+$/,""); - - - if (statement.length == 0) - propsOfLastArg = _globalSymbols; - else{ - var statementSyms = statement.split(/[^\$a-zA-Z0-9_\.]/); - var lastSymbol = statementSyms[statementSyms.length-1]; - //print("DEBUG: lastSymbol=[" + lastSymbol + "]"); - // - // try to complete the object ala java IDEs. - // - var parts = lastSymbol.split(/\./); - var name = parts[0]; - var symbol = global[name]; - var lastGoodSymbol = symbol; - if (typeof symbol != "undefined") - { - for (var i = 1; i < parts.length;i++){ - name = parts[i]; - symbol = symbol[name]; - if (typeof symbol == "undefined") - break; - lastGoodSymbol = symbol; - } - //print("debug:name["+name+"]lastSymbol["+lastSymbol+"]symbol["+symbol+"]"); - if (typeof symbol == "undefined"){ - // - // look up partial matches against last good symbol - // - var objectProps = _getProperties(lastGoodSymbol); - if (name == ""){ - // if the last symbol looks like this.. - // ScriptCraft. - // - - for (var i =0;i < objectProps.length;i++){ - var candidate = lastSymbol + objectProps[i]; - var re = new RegExp(lastSymbol + "$","g"); - propsOfLastArg.push(lastArg.replace(re,candidate)); - } - - }else{ - // it looks like this.. - // ScriptCraft.co - // - //print("debug:case Y: ScriptCraft.co"); - - var li = statement.lastIndexOf(name); - for (var i = 0; i < objectProps.length;i++){ - if (objectProps[i].indexOf(name) == 0) - { - var candidate = lastSymbol.substring(0,lastSymbol.lastIndexOf(name)); - candidate = candidate + objectProps[i]; - var re = new RegExp(lastSymbol+ "$","g"); - //print("DEBUG: re=" + re + ",lastSymbol="+lastSymbol+",lastArg=" + lastArg + ",candidate=" + candidate); - propsOfLastArg.push(lastArg.replace(re,candidate)); - } - } - - } - }else{ - //print("debug:case Z:ScriptCraft"); - var objectProps = _getProperties(symbol); - for (var i = 0; i < objectProps.length; i++){ - var re = new RegExp(lastSymbol+ "$","g"); - propsOfLastArg.push(lastArg.replace(re,lastSymbol + "." + objectProps[i])); - } - } - }else{ - //print("debug:case AB:ScriptCr"); - // loop thru globalSymbols looking for a good match - for (var i = 0;i < _globalSymbols.length; i++){ - if (_globalSymbols[i].indexOf(lastSymbol) == 0){ - var possibleCompletion = _globalSymbols[i]; - var re = new RegExp(lastSymbol+ "$","g"); - propsOfLastArg.push(lastArg.replace(re,possibleCompletion)); - } - } - - } - } - for (var i = 0;i < propsOfLastArg.length; i++) - result.add(propsOfLastArg[i]); - }; - - /* - Unload Handlers - */ - var unloadHandlers = []; - var _addUnloadHandler = function(f) { - unloadHandlers.push(f); - }; - var _runUnloadHandlers = function() { - for (var i = 0; i < unloadHandlers.length; i++) { - unloadHandlers[i](); - } - }; - -/************************************************************************* -refresh() function ------------------- -The refresh() function will ... - -1. Disable the ScriptCraft plugin. -2. Unload all event listeners associated with the ScriptCraft plugin. -3. Enable the ScriptCraft plugin. - -... refresh() can be used during development to reload only scriptcraft javascript files. -See [issue #69][issue69] for more information. - -[issue69]: https://github.com/walterhiggins/ScriptCraft/issues/69 - -***/ - global.refresh = function(){ - __plugin.pluginLoader.disablePlugin(__plugin); - __plugin.pluginLoader.enablePlugin(__plugin); - }; - - - global.load = _load; - global.save = _save; - global.plugin = _plugin; - global.ready = _ready; - global.command = _command; - global._onTabComplete = __onTabCompleteJS; - global.addUnloadHandler = _addUnloadHandler; - - - // - // assumes this was loaded from js-plugins/core/ - // load all of the plugins. - // - _reload(jsPluginsRootDir); - - // - // all modules have loaded - // - for (var i =0;i < _deferred.length;i++) - _deferred[i](); - - events.on("server.PluginDisableEvent",function(l,e){ - // - // save all plugins which have persistent data - // - for (var moduleName in _plugins){ - var pluginData = _plugins[moduleName]; - if (pluginData.persistent) - save(pluginData.module.store, jsPluginsRootDirName + "/" + moduleName + "-store.txt"); - } - _runUnloadHandlers(); - org.bukkit.event.HandlerList["unregisterAll(org.bukkit.plugin.Plugin)"](__plugin); - }); - - -}()); - - - diff --git a/src/main/javascript/drone/blocks.js b/src/main/javascript/drone/blocks.js deleted file mode 100644 index c265d17..0000000 --- a/src/main/javascript/drone/blocks.js +++ /dev/null @@ -1,274 +0,0 @@ -/************************************************************************ -Blocks Module -============= -You hate having to lookup [Data Values][dv] when you use ScriptCraft's Drone() functions. So do I. -So I created this blocks object which is a helper object for use in construction. - -Examples --------- - - box( blocks.oak ); // creates a single oak wood block - box( blocks.sand, 3, 2, 1 ); // creates a block of sand 3 wide x 2 high x 1 long - box( blocks.wool.green, 2 ); // creates a block of green wool 2 blocks wide - -Color aliased properties that were a direct descendant of the blocks object are no longer used to avoid confusion with carpet and stained clay blocks. In addition, there's a convenience array `blocks.rainbow` which is an array of the 7 colors of the rainbow (or closest approximations). - -***/ -var blocks = { - air: 0, - stone: 1, - grass: 2, - dirt: 3, - cobblestone: 4, - oak: 5, - spruce: '5:1', - birch: '5:2', - jungle: '5:3', - sapling: { - oak: 6, - spruce: '6:1', - birch: '62:2', - jungle: '6:3' - }, - bedrock: 7, - water: 8, - water_still: 9, - lava: 10, - lava_still: 11, - sand: 12, - gravel: 13, - gold_ore: 14, - iron_ore: 15, - coal_ore: 16, - wood: 17, - leaves: 18, - sponge: 19, - glass: 20, - lapis_lazuli_ore: 21, - lapis_lazuli_block: 22, - dispenser: 23, - sandstone: 24, - note: 25, - bed: 26, - powered_rail: 27, - detector_rail: 28, - sticky_piston: 29, - cobweb: 30, - grass_tall: 31, - dead_bush: 32, - piston: 33, - piston_extn: 34, - wool: { - white: 35 // All other colors added below - }, - dandelion: 37, - flower_yellow: 37, - rose: 38, - flower_red: 38, - mushroom_brown: 39, - mushroom_red: 40, - gold: 41, - iron: 42, - tnt: 46, - bookshelf: 47, - moss_stone: 48, - obsidian: 49, - torch: 50, - fire: 51, - monster_spawner: 52, - stairs: { - oak: 53, - cobblestone: 67, - brick: 108, - stone: 109, - nether: 114, - sandstone: 128, - spruce: 134, - birch: 135, - jungle: 136, - quartz: 156 - }, - chest: 54, - redstone_wire: 55, - diamond_ore: 56, - diamond: 57, - crafting_table: 58, - wheat_seeds: 59, - farmland: 60, - furnace: 61, - furnace_burning: 62, - sign_post: 63, - door_wood: 64, - ladder: 65, - rail: 66, - sign: 68, - lever: 69, - pressure_plate_stone: 70, - door_iron: 71, - pressure_plate_wood: 72, - redstone_ore: 73, - redstone_ore_glowing: 74, - torch_redstone: 75, - torch_redstone_active: 76, - stone_button: 77, - ice: 79, - snow: 80, - cactus: 81, - clay: 82, - sugar_cane: 83, - jukebox: 84, - fence: 85, - pumpkin: 86, - netherrack: 87, - soulsand: 88, - glowstone: 89, - netherportal: 90, - jackolantern: 91, - cake: 92, - redstone_repeater: 93, - redeston_repeater_active: 94, - chest_locked: 95, - trapdoor: 96, - monster_egg: 97, - brick: { - stone: 98, - mossy: '98:1', - cracked: '98:2', - chiseled: '98:3', - red: 45 - }, - mushroom_brown_huge: 99, - mushroom_red_huge: 100, - iron_bars: 101, - glass_pane: 102, - melon: 103, - pumpkin_stem: 104, - melon_stem: 105, - vines: 106, - fence_gate: 107, - mycelium: 110, - lily_pad: 111, - nether: 112, - nether_fence: 113, - netherwart: 115, - table_enchantment: 116, - brewing_stand: 117, - cauldron: 118, - endportal: 119, - endportal_frame: 120, - endstone: 121, - dragon_egg: 122, - redstone_lamp: 123, - redstone_lamp_active: 124, - slab: { - snow: 78, - stone: 44, - sandstone: '44:1', - wooden: '44:2', - cobblestone: '44:3', - brick: '44:4', - stonebrick: '44:5', - netherbrick:'44:6', - quartz: '44:7', - oak: 126, - spruce: '126:1', - birch: '126:2', - jungle: '126:3', - upper: { - stone: '44:8', - sandstone: '44:9', - wooden: '44:10', - cobblestone: '44:11', - brick: '44:12', - stonebrick: '44:13', - netherbrick:'44:14', - quartz: '44:15', - oak: '126:8', - spruce: '126:9', - birch: '126:10', - jungle: '126:11', - } - }, - cocoa: 127, - emerald_ore: 129, - enderchest: 130, - tripwire_hook: 131, - tripwire: 132, - emerald: 133, - command: 137, - beacon: 138, - cobblestone_wall: 139, - flowerpot: 140, - carrots: 141, - potatoes: 142, - button_wood: 143, - mobhead: 144, - anvil: 145, - chest_trapped: 146, - pressure_plate_weighted_light: 147, - pressure_plate_weighted_heavy: 148, - redstone_comparator: 149, - redstone_comparator_active: 150, - daylight_sensor: 151, - redstone: 152, - netherquartzore: 153, - hopper: 154, - quartz: 155, - rail_activator: 157, - dropper: 158, - stained_clay: { - white: 159 // All other colors added below - }, - hay: 170, - carpet: { - white: 171 // All other colors added below - }, - hardened_clay: 172, - coal_block: 173 -}; - -(function() { - // Add all available colors to colorized block collections - - var colors = { - orange: ':1', - magenta: ':2', - lightblue: ':3', - yellow: ':4', - lime: ':5', - pink: ':6', - gray: ':7', - lightgray: ':8', - cyan: ':9', - purple: ':10', - blue: ':11', - brown: ':12', - green: ':13', - red: ':14', - black: ':15' - }; - var colorized_blocks = ["wool", "stained_clay", "carpet"]; - - for (var i = 0, len = colorized_blocks.length; i < len; i++) { - var block = colorized_blocks[i], - data_value = blocks[block].white; - - for (var color in colors) { - blocks[block][color] = data_value + colors[color]; - } - }; - - /* - rainbow colors - a convenience - Color aliased properties that were a direct descendant of the blocks - object are no longer used to avoid confusion with carpet and stained - clay blocks. - */ - blocks.rainbow = [blocks.wool.red, - blocks.wool.orange, - blocks.wool.yellow, - blocks.wool.lime, - blocks.wool.lightblue, - blocks.wool.blue, - blocks.wool.purple]; -})(); diff --git a/src/main/javascript/drone/blocktype.js b/src/main/javascript/drone/blocktype.js deleted file mode 100644 index c1ca783..0000000 --- a/src/main/javascript/drone/blocktype.js +++ /dev/null @@ -1,373 +0,0 @@ -/************************************************************************ -Drone.blocktype() method -======================== -Creates the text out of blocks. Useful for large-scale in-game signs. - -Parameters ----------- - - * message - The message to create - (use `\n` for newlines) - * foregroundBlock (default: black wool) - The block to use for the foreground - * backgroundBlock (default: none) - The block to use for the background - -Example -------- -To create a 2-line high message using glowstone... - - blocktype("Hello\nWorld",blocks.glowstone); - -![blocktype example][imgbt1] - -[imgbt1]: img/blocktype1.png - -***/ -(function(){ - - var bitmaps = { - raw: { - '0':' ### '+ - ' # # '+ - ' # # '+ - ' # # '+ - ' ### ', - - '1':' # '+ - ' ## '+ - ' # '+ - ' # '+ - ' ### ', - - '2':' ### '+ - ' # '+ - ' ### '+ - ' # '+ - ' ### ', - - '3':' ### '+ - ' # '+ - ' ## '+ - ' # '+ - ' ### ', - - '4':' # '+ - ' ## '+ - ' # # '+ - ' ### '+ - ' # ', - - '5':' ### '+ - ' # '+ - ' ### '+ - ' # '+ - ' ### ', - - '6':' ### '+ - ' # '+ - ' ### '+ - ' # # '+ - ' ### ', - - '7':' ### '+ - ' # '+ - ' # '+ - ' # '+ - ' # ', - - '8':' ### '+ - ' # # '+ - ' ### '+ - ' # # '+ - ' ### ', - - '9':' ### '+ - ' # # '+ - ' ### '+ - ' # '+ - ' ### ', - - 'a':' ### '+ - ' # # '+ - ' ### '+ - ' # # '+ - ' # # ', - - 'b':' ## '+ - ' # # '+ - ' ## '+ - ' # # '+ - ' ## ', - - 'c':' ## '+ - ' # '+ - ' # '+ - ' # '+ - ' ## ', - - 'd':' ## '+ - ' # # '+ - ' # # '+ - ' # # '+ - ' ## ', - - 'e':' ### '+ - ' # '+ - ' ## '+ - ' # '+ - ' ### ', - - 'f':' ### '+ - ' # '+ - ' ## '+ - ' # '+ - ' # ', - - 'g':' ### '+ - ' # '+ - ' # '+ - ' # # '+ - ' ### ', - - 'h':' # # '+ - ' # # '+ - ' ### '+ - ' # # '+ - ' # # ', - - 'i':' ### '+ - ' # '+ - ' # '+ - ' # '+ - ' ### ', - - 'j':' ### '+ - ' # '+ - ' # '+ - ' # '+ - ' # ', - - 'k':' # '+ - ' # # '+ - ' ## '+ - ' # # '+ - ' # # ', - - 'l':' # '+ - ' # '+ - ' # '+ - ' # '+ - ' ### ', - - 'm':' # # '+ - ' ### '+ - ' # # '+ - ' # # '+ - ' # # ', - - 'n':' ## '+ - ' # # '+ - ' # # '+ - ' # # '+ - ' # # ', - - 'o':' # '+ - ' # # '+ - ' # # '+ - ' # # '+ - ' # ', - - 'p':' ### '+ - ' # # '+ - ' ### '+ - ' # '+ - ' # ', - - 'q':' ### '+ - ' # # '+ - ' # # '+ - ' ### '+ - ' # ', - - 'r':' ## '+ - ' # # '+ - ' ## '+ - ' # # '+ - ' # # ', - - 's':' ## '+ - ' # '+ - ' ### '+ - ' # '+ - ' ## ', - - 't':' ### '+ - ' # '+ - ' # '+ - ' # '+ - ' # ', - - 'u':' # # '+ - ' # # '+ - ' # # '+ - ' # # '+ - ' ### ', - - 'v':' # # '+ - ' # # '+ - ' # # '+ - ' # # '+ - ' # ', - - 'w':' # # '+ - ' # # '+ - ' # # '+ - ' ### '+ - ' # # ', - - 'x':' # # '+ - ' # # '+ - ' # '+ - ' # # '+ - ' # # ', - - 'y':' # # '+ - ' # # '+ - ' # # '+ - ' # '+ - ' # ', - - 'z':' ### '+ - ' # '+ - ' # '+ - ' # '+ - ' ### ', - - '!':' # '+ - ' # '+ - ' # '+ - ' '+ - ' # ', - - ':':' '+ - ' # '+ - ' '+ - ' # '+ - ' ', - - ';':' '+ - ' # '+ - ' '+ - ' # '+ - ' # ', - - ',':' '+ - ' '+ - ' '+ - ' # '+ - ' # ', - - '/':' # '+ - ' # '+ - ' # '+ - ' # '+ - ' # ', - - '+':' '+ - ' # '+ - ' ### '+ - ' # '+ - ' ', - - '-':' '+ - ' '+ - ' ### '+ - ' '+ - ' ', - - '.':' '+ - ' '+ - ' '+ - ' '+ - ' # ', - - "'":' # '+ - ' # '+ - ' '+ - ' '+ - ' ', - - ' ':' '+ - ' '+ - ' '+ - ' '+ - ' ' - }, - computed: {} - }; - /* - wph 20130121 compute the width, and x,y coords of pixels ahead of time - */ - for (var c in bitmaps.raw){ - var bits = bitmaps.raw[c]; - var width = bits.length/5; - var bmInfo = {"width": width,"pixels":[]} - bitmaps.computed[c] = bmInfo; - for (var j = 0; j < bits.length; j++){ - if (bits.charAt(j) != ' '){ - bmInfo.pixels.push([j%width,Math.ceil(j/width)]); - } - } - } - - - // - // message - // string with text to be displayed - // fg - // foreground material. The material the text will be in. - // bg - // background material, optional. The negative space within the bounding box of the text. - // - Drone.extend('blocktype', function(message,fg,bg){ - - this.chkpt('blocktext'); - - if (typeof fg == "undefined") - fg = blocks.wool.black; - - var bmfg = this._getBlockIdAndMeta(fg); - var bmbg = null; - if (typeof bg != "undefined") - bmbg = this._getBlockIdAndMeta(bg); - var lines = message.split("\n"); - var lineCount = lines.length; - for (var h = 0;h < lineCount; h++) { - var line = lines[h]; - line = line.toLowerCase().replace(/[^0-9a-z \.\-\+\/\;\'\:\!]/g,""); - this.up(7*(lineCount-(h+1))); - - for (var i =0;i < line.length; i++) { - var ch = line.charAt(i) - var bits = bitmaps.computed[ch]; - if (typeof bits == "undefined"){ - bits = bitmaps.computed[' ']; - } - var charWidth = bits.width; - if (typeof bg != "undefined") - this.cuboidX(bmbg[0],bmbg[1],charWidth,7,1); - for (var j = 0;j < bits.pixels.length;j++){ - this.chkpt('btbl'); - var x = bits.pixels[j][0]; - var y = bits.pixels[j][1]; - this.up(6-y).right(x).cuboidX(bmfg[0],bmfg[1]); - this.move('btbl'); - } - this.right(charWidth-1); - } - this.move('blocktext'); - } - - return this.move('blocktext'); - }); -}()); - - diff --git a/src/main/javascript/drone/contrib/castle.js b/src/main/javascript/drone/contrib/castle.js deleted file mode 100644 index 9cb9d46..0000000 --- a/src/main/javascript/drone/contrib/castle.js +++ /dev/null @@ -1,49 +0,0 @@ -// -// a castle is just a big wide fort with 4 taller forts at each corner -// -Drone.extend('castle', function(side, height) -{ - // - // use sensible default parameter values - // if no parameters are supplied - // - if (typeof side == "undefined") - side = 24; - if (typeof height == "undefined") - height = 10; - if (height < 8 || side < 20) - throw new java.lang.RuntimeException("Castles must be at least 20 wide X 8 tall"); - // - // remember where the drone is so it can return 'home' - // - this.chkpt('castle'); - // - // how big the towers at each corner will be... - // - var towerSide = 10; - var towerHeight = height+4; - - // - // the main castle building will be front and right of the first tower - // - this.fwd(towerSide/2).right(towerSide/2); - // - // the castle is really just a big fort with 4 smaller 'tower' forts at each corner - // - this.fort(side,height); - // - // move back to start position - // - this.move('castle'); - // - // now place 4 towers at each corner (each tower is another fort) - // - for (var corner = 0; corner < 4; corner++) - { - // construct a 'tower' fort - this.fort(towerSide,towerHeight); - // move forward the length of the castle then turn right - this.fwd(side+towerSide-1).turn(); - } - return this.move('castle'); -}); diff --git a/src/main/javascript/drone/contrib/chessboard.js b/src/main/javascript/drone/contrib/chessboard.js deleted file mode 100644 index 8c985bb..0000000 --- a/src/main/javascript/drone/contrib/chessboard.js +++ /dev/null @@ -1,30 +0,0 @@ -/** -* Creates a tile pattern of given block types and size -* -* Paramters: -* whiteBlock - blockId used for the traditional white portion of the chessboard -* blackBlock - blockId used for the traditional black portion of the chessboard -* width - width of the chessboard -* height - height of the chessboard -*/ -Drone.extend("chessboard", function(whiteBlock, blackBlock, width, depth) { - this.chkpt('chessboard-start'); - width = width || 8; - depth = depth || width; - blackBlock = blackBlock || blocks.wool.black; - whiteBlock = whiteBlock || blocks.wool.white; - - for(var i = 0; i < width; ++i) { - for(var j = 0; j < depth; ++j) { - var block = blackBlock; - if((i+j)%2 == 1) { - block = whiteBlock; - } - this.box(block); - this.right(); - } - this.move('chessboard-start').fwd(i+1); - } - - return this.move('chessboard-start'); -}); diff --git a/src/main/javascript/drone/contrib/cottage.js b/src/main/javascript/drone/contrib/cottage.js deleted file mode 100644 index e5a2321..0000000 --- a/src/main/javascript/drone/contrib/cottage.js +++ /dev/null @@ -1,83 +0,0 @@ -// -// need to use the drone module to create buildings easily -// it can be done using calls to putBlock(), putSign(), getPlayerPos() and getMousePos() -// but it's easier to use the Drone class -// __folder is a special javascript variable whose value is the directory where the -// current script resides. -// -// usage: -// [1] to build a cottage at the player's current location or the cross-hairs location... -// -// /js cottage(); -// -// [2] to build a cottage using an existing drone... -// -// /js drone.cottage(); -// - -Drone.extend('cottage',function () -{ - this.chkpt('cottage') - .box0(48,7,2,6) // 4 walls - .right(3).door() // door front and center - .up(1).left(2).box(102) // windows to left and right - .right(4).box(102) - .left(5).up().prism0(53,7,6); - // - // put up a sign near door. - // - this.down().right(4).sign(["Home","Sweet","Home"],68); - - return this.move('cottage'); -}); -// -// a more complex script that builds an tree-lined avenue with -// cottages on both sides. -// -Drone.extend('cottage_road', function(numberCottages) -{ - if (typeof numberCottages == "undefined"){ - numberCottages = 6; - } - var i=0, distanceBetweenTrees = 11; - // - // step 1 build the road. - // - var cottagesPerSide = Math.floor(numberCottages/2); - this - .chkpt("cottage_road") // make sure the drone's state is saved. - .box(43,3,1,cottagesPerSide*(distanceBetweenTrees+1)) // build the road - .up().right() // now centered in middle of road - .chkpt("cr"); // will be returning to this position later - - // - // step 2 line the road with trees - // - for (; i < cottagesPerSide+1;i++){ - this - .left(5).oak() - .right(10).oak() - .left(5) // return to middle of road - .fwd(distanceBetweenTrees+1); // move forward. - } - this.move("cr").back(6); // move back 1/2 the distance between trees - - // this function builds a path leading to a cottage. - function pathAndCottage(d){ - return d.down().box(43,1,1,5).fwd(5).left(3).up().cottage(); - }; - // - // step 3 build cottages on each side - // - for (i = 0;i < cottagesPerSide; i++) - { - this.fwd(distanceBetweenTrees+1).chkpt("r"+i); - // build cottage on left - pathAndCottage(this.turn(3)).move("r"+i); - // build cottage on right - pathAndCottage(this.turn()).move("r"+i); - } - // return drone to where it was at start of function - return this.move("cottage_road"); -}); - diff --git a/src/main/javascript/drone/contrib/dancefloor.js b/src/main/javascript/drone/contrib/dancefloor.js deleted file mode 100644 index b961a19..0000000 --- a/src/main/javascript/drone/contrib/dancefloor.js +++ /dev/null @@ -1,36 +0,0 @@ -// -// Create a floor of colored tiles some of which emit light. -// The tiles change color every second creating a strobe-lit dance-floor. -// -// See it in action here => http://www.youtube.com/watch?v=UEooBt6NTFo -// -Drone.extend('dancefloor',function(width,length) -{ - if (typeof width == "undefined") - width = 5; - if (typeof length == "undefined") - length = width; - // - // create a separate Drone object to lay down disco tiles - // - var disco = new Drone(this.x, this.y, this.z, this.dir, this.world); - // - // under-floor lighting - // - disco.down().box(89,width,1,length).up(); - var floorTiles = [35,35,'35:1','35:2','35:3','35:4','35:4','35:4','35:6',20,20]; - // - // strobe gets called in a java thread - disco only lasts 30 seconds. - // - var discoTicks = 30; - var task = null; - var strobe = function() { - disco.rand(floorTiles,width,1,length); - if (!discoTicks--) - task.cancel(); - }; - var now = 0; - var everySecond = 20; - task = server.scheduler.runTaskTimer(__plugin,strobe,now,everySecond); - return this; -}); diff --git a/src/main/javascript/drone/contrib/fort.js b/src/main/javascript/drone/contrib/fort.js deleted file mode 100644 index 49aec69..0000000 --- a/src/main/javascript/drone/contrib/fort.js +++ /dev/null @@ -1,66 +0,0 @@ -// -// constructs a medieval fort -// -Drone.extend('fort', function(side, height) -{ - if (typeof side == "undefined") - side = 18; - if (typeof height == "undefined") - height = 6; - // make sure side is even - if (side%2) - side++; - if (height < 4 || side < 10) - throw new java.lang.RuntimeException("Forts must be at least 10 wide X 4 tall"); - var brick = 98; - // - // build walls. - // - this.chkpt('fort').box0(brick,side,height-1,side); - // - // build battlements - // - this.up(height-1); - for (i = 0;i <= 3;i++){ - var turret = []; - this.box(brick) // solid brick corners - .up().box('50:5').down() // light a torch on each corner - .fwd(); - turret.push('109:'+ Drone.PLAYER_STAIRS_FACING[this.dir]); - turret.push('109:'+ Drone.PLAYER_STAIRS_FACING[(this.dir+2)%4]); - try{ - this.boxa(turret,1,1,side-2).fwd(side-2).turn(); - }catch(e){ - self.sendMessage("ERROR: " + e.toString()); - } - } - // - // build battlement's floor - // - this.move('fort'); - this.up(height-2).fwd().right().box('126:0',side-2,1,side-2); - var battlementWidth = 3; - if (side <= 12) - battlementWidth = 2; - - this.fwd(battlementWidth).right(battlementWidth) - .box(0,side-((1+battlementWidth)*2),1,side-((1+battlementWidth)*2)); - // - // add door - // - var torch = '50:' + Drone.PLAYER_TORCH_FACING[this.dir]; - this.move('fort').right((side/2)-1).door2() // double doors - .back().left().up() - .box(torch) // left torch - .right(3) - .box(torch); // right torch - // - // add ladder up to battlements - // - var ladder = '65:' + Drone.PLAYER_SIGN_FACING[(this.dir+2)%4]; - this.move('fort').right((side/2)-3).fwd(1) // move inside fort - .box(ladder, 1,height-1,1); - return this.move('fort'); - -}); - diff --git a/src/main/javascript/drone/contrib/rainbow.js b/src/main/javascript/drone/contrib/rainbow.js deleted file mode 100644 index a9da6e4..0000000 --- a/src/main/javascript/drone/contrib/rainbow.js +++ /dev/null @@ -1,41 +0,0 @@ -/************************************************************************ -Drone.rainbow() method -====================== -Creates a Rainbow. - -Parameters ----------- - - * radius (optional - default:18) - The radius of the rainbow - -Example -------- - - var d = new Drone(); - d.rainbow(30); - -![rainbow example](img/rainbowex1.png) - -***/ -Drone.extend('rainbow', function(radius){ - if (typeof radius == "undefined") - radius = 18; - - this.chkpt('rainbow'); - this.down(radius); - // copy blocks.rainbow and add air at end (to compensate for strokewidth) - var colors = blocks.rainbow.slice(0); - colors.push(blocks.air); - for (var i = 0;i < colors.length; i++) { - var bm = this._getBlockIdAndMeta(colors[i]); - this.arc({ - blockType: bm[0], - meta: bm[1], - radius: radius-i, - strokeWidth: 2, - quadrants: {topright: true, - topleft: true}, - orientation: 'vertical'}).right().up(); - } - return this.move('rainbow'); -}); diff --git a/src/main/javascript/drone/contrib/skyscraper-example.js b/src/main/javascript/drone/contrib/skyscraper-example.js deleted file mode 100644 index b5b70cb..0000000 --- a/src/main/javascript/drone/contrib/skyscraper-example.js +++ /dev/null @@ -1,12 +0,0 @@ -Drone.extend('skyscraper',function(floors){ - floors = floors || 10; - this.chkpt('skyscraper'); - for (var i = 0;i < floors; i++) - { - this.box(blocks.iron,20,1,20) - .up() - .box0(blocks.glass_pane,20,3,20); - this.up(3); - } - return this.move('skyscraper'); -}); diff --git a/src/main/javascript/drone/contrib/spiral_stairs.js b/src/main/javascript/drone/contrib/spiral_stairs.js deleted file mode 100644 index c41bda6..0000000 --- a/src/main/javascript/drone/contrib/spiral_stairs.js +++ /dev/null @@ -1,44 +0,0 @@ -/************************************************************************ -Drone.spiral_stairs() method -============================ -Constructs a spiral staircase with slabs at each corner. - -Parameters ----------- - - * stairBlock - The block to use for stairs, should be one of the following... - - 'oak' - - 'spruce' - - 'birch' - - 'jungle' - - 'cobblestone' - - 'brick' - - 'stone' - - 'nether' - - 'sandstone' - - 'quartz' - * flights - The number of flights of stairs to build. - -![Spiral Staircase](img/spiralstair1.png) - -Example -------- -To construct a spiral staircase 5 floors high made of oak... - - spiral_stairs('oak', 5); - -***/ -Drone.extend("spiral_stairs",function(stairBlock, flights){ - this.chkpt('spiral_stairs'); - - for (var i = 0; i < flights; i++){ - this - .box(blocks.stairs[stairBlock] + ':' + Drone.PLAYER_STAIRS_FACING[this.dir]) - .up().fwd() - .box(blocks.stairs[stairBlock] + ':' + Drone.PLAYER_STAIRS_FACING[this.dir]) - .up().fwd() - .box(blocks.slab[stairBlock]) - .turn().fwd(); - } - return this.move('spiral_stairs'); -}); diff --git a/src/main/javascript/drone/contrib/streamer.js b/src/main/javascript/drone/contrib/streamer.js deleted file mode 100644 index ffc3a53..0000000 --- a/src/main/javascript/drone/contrib/streamer.js +++ /dev/null @@ -1,19 +0,0 @@ -/** -* Creates a stream of blocks in a given direction until it hits something other than air -* -* Parameters: -* block - blockId -* dir - "up", "down", "left", "right", "fwd", "back -* maxIterations - (Optional) maximum number of cubes to generate, defaults to 1000 -*/ -Drone.extend("streamer", function(block, dir, maxIterations) { - for(var i = 0; i < maxIterations||1000; ++i) { - this.box(block); - this[dir].call(this); - if(getBlock(this.x, this.y, this.z) !== "0:0") { - break; - } - } - - return this; -}); diff --git a/src/main/javascript/drone/contrib/temple.js b/src/main/javascript/drone/contrib/temple.js deleted file mode 100644 index 90ba27e..0000000 --- a/src/main/javascript/drone/contrib/temple.js +++ /dev/null @@ -1,23 +0,0 @@ -// -// constructs a mayan temple -// -Drone.extend('temple', function(side) { - if (!side) { - side = 20; - } - var stone = '98:1'; - var stair = '109:' + Drone.PLAYER_STAIRS_FACING[this.dir]; - - this.chkpt('temple'); - - while (side > 4) { - var middle = Math.round((side-2)/2); - this.chkpt('corner') - .box(stone, side, 1, side) - .right(middle).box(stair).right().box(stair) - .move('corner').up().fwd().right(); - side = side - 2; - } - - return this.move('temple'); -}); diff --git a/src/main/javascript/drone/drone.js b/src/main/javascript/drone/drone.js deleted file mode 100644 index 590ec11..0000000 --- a/src/main/javascript/drone/drone.js +++ /dev/null @@ -1,1800 +0,0 @@ -var global = this; -load(__folder + "../core/_primitives.js",true); - -/********************************************************************* -Drone Module -============ -The Drone is a convenience class for building. It can be used for... - - 1. Building - 2. Copying and Pasting - -It uses a fluent interface which means all of the Drone's methods return `this` and can -be chained together like so... - - var theDrone = new Drone(); - theDrone.up().left().box(blocks.oak).down().fwd(3).cylinder0(blocks.lava,8); - -TLDNR; (Just read this if you're impatient) -=========================================== -At the in-game command prompt type... - - /js box(blocks.oak) - -... creates a single wooden block at the cross-hairs or player location - - /js box(5).right(2).box('35:15',4,9,1) - -... creates a single wooden block and a 2001 black obelisk that is 4 -wide x 9 tall x 1 long in size. If you want to see what else -ScriptCraft's Drone can do, read on... - -Constructing a Drone Object -=========================== - -Drones can be created in any of the following ways... - - 1. Calling any one of the methods listed below will return a Drone object. For example... - - var d = box(blocks.oak) - - ... creates a 1x1x1 wooden block at the cross-hairs or player's location and returns a Drone - object. This might look odd (if you're familiar with Java's Object-dot-method syntax) but all - of the Drone class's methods are also global functions that return new Drone objects. - This is short-hand for creating drones and is useful for playing around with Drones at the in-game - command prompt. It's shorter than typing ... - - var d = new Drone().box(5) - - ... All of the Drone's methods return `this` (self) so you can chain operations together like this... - - var d = box(5).up().box(5,3,1,3).down().fwd(2).box(5).turn().fwd(2).box(5).turn().fwd(2).box(5) - - 2. Using the following form... - - d = new Drone() - - ...will create a new Drone. If the cross-hairs are pointing at a - block at the time then, that block's location becomes the drone's - starting point. If the cross-hairs are _not_ pointing at a block, - then the drone's starting location will be 2 blocks directly in - front of the player. TIP: Building always happens right and front - of the drone's position... - - Plan View: - - ^ - | - | - D----> - - For convenience you can use a _corner stone_ to begin building. - The corner stone should be located just above ground level. If - the cross-hair is point at or into ground level when you create a - new Drone(), then building begins at that point. You can get - around this by pointing at a 'corner stone' just above ground - level or alternatively use the following statement... - - d = new Drone().up(); - - ... which will move the drone up one block as soon as it's created. - - ![corner stone](img/cornerstone1.png) - - 3. Or by using the following form... - - d = new Drone(x,y,z,direction,world); - - This will create a new Drone at the location you specified using - x, y, z In minecraft, the X axis runs west to east and the Z axis runs - north to south. The direction parameter says what direction you want - the drone to face: 0 = east, 1 = south, 2 = west, 3 = north. If the - direction parameter is omitted, the player's direction is used - instead. - - Both the `direction` and `world` parameters are optional. - - 4. Create a new Drone based on a Bukkit Location object... - - d = new Drone(location); - - This is useful when you want to create a drone at a given - `org.bukkit.Location` . The `Location` class is used throughout - the bukkit API. For example, if you want to create a drone when a - block is broken at the block's location you would do so like - this... - - events.on('block.BlockBreakEvent',function(listener,event){ - var location = event.block.location; - var drone = new Drone(location); - // do more stuff with the drone here... - }); - -Parameters ----------- - * location (optional) : *NB* If an `org.bukkit.Location` object is provided as a parameter, then it should be the only parameter. - * x (optional) : The x coordinate of the Drone - * y (optional) : The y coordinate of the Drone - * z (optional) : The z coordinate of the Drone - * direction (optional) : The direction in which the Drone is - facing. Possible values are 0 (east), 1 (south), 2 (west) or 3 (north) - * world (optional) : The world in which the drone is created. - -***/ -var Drone = function(/* number */ x, /* number */ y, /* number */ z, /* number */ direction){}; -/************************************************************************ -Drone.box() method -================== -the box() method is a convenience method for building things. (For the more performance-oriented method - see cuboid) - -parameters ----------- - * b - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. - Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` - * w (optional - default 1) - the width of the structure - * h (optional - default 1) - the height of the structure - * d (optional - default 1) - the depth of the structure - NB this is - not how deep underground the structure lies - this is how far - away (depth of field) from the drone the structure will extend. - -Example -------- -To create a black structure 4 blocks wide, 9 blocks tall and 1 block long... - - box(blocks.wool.black, 4, 9, 1); - -... or the following code does the same but creates a variable that can be used for further methods... - - var drone = new Drone(); - drone.box(blocks.wool.black, 4, 9, 1); - -![box example 1](img/boxex1.png) - -Drone.box0() method -=================== -Another convenience method - this one creates 4 walls with no floor or ceiling. - -Parameters ----------- - * block - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. - Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` - * width (optional - default 1) - the width of the structure - * height (optional - default 1) - the height of the structure - * length (optional - default 1) - the length of the structure - how far - away (depth of field) from the drone the structure will extend. - -Example -------- -To create a stone building with the insided hollowed out 7 wide by 3 tall by 6 long... - - box0( blocks.stone, 7, 3, 6); - -![example box0](img/box0ex1.png) - -Drone.boxa() method -=================== -Construct a cuboid using an array of blocks. As the drone moves first along the width axis, -then the height (y axis) then the length, each block is picked from the array and placed. - -Parameters ----------- - * blocks - An array of blocks - each block in the array will be placed in turn. - * width - * height - * length - -Example -------- -Construct a rainbow-colored road 100 blocks long... - - var rainbowColors = [blocks.wool.red, blocks.wool.orange, blocks.wool.yellow, blocks.wool.lime, - blocks.wool.lightblue, blocks.wool.blue, blocks.wool.purple]; - - boxa(rainbowColors,7,1,30); - -![boxa example](img/boxaex1.png) - -***/ -Drone.prototype.box = function(block,width,height,depth){}; -Drone.prototype.box0 = function(block,width,height,length){}; -Drone.prototype.boxa = function(/* [string] */ width, height, length){}; - -/************************************************************************ -Drone Movement -============== -Drones can move freely in minecraft's 3-D world. You control the -Drone's movement using any of the following methods.. - - * up() - * down() - * left() - * right() - * fwd() - * back() - * turn() - -... Each of these methods takes a single optional parameter -`numBlocks` - the number of blocks to move in the given direction. If -no parameter is given, the default is 1. - -to change direction use the `turn()` method which also takes a single -optional parameter (numTurns) - the number of 90 degree turns to make. -Turns are always clock-wise. If the drone is facing north, then -drone.turn() will make the turn face east. If the drone is facing east -then drone.turn(2) will make the drone turn twice so that it is facing -west. - -***/ -Drone.prototype.up = function(numBlocks){}; -Drone.prototype.down = function(numBlocks){}; -Drone.prototype.left = function(numBlocks){}; -Drone.prototype.right = function(numBlocks){}; -Drone.prototype.fwd = function(numBlocks){}; -Drone.prototype.back = function(numBlocks){}; -Drone.prototype.turn = function(numTurns){}; - -/************************************************************************ -Drone Positional Info -===================== - - * getLocation() - Returns a Bukkit Location object for the drone - -***/ -Drone.prototype.getLocation = function(){}; - -/************************************************************************ -Drone Markers -============= -Markers are useful when your Drone has to do a lot of work. You can -set a check-point and return to the check-point using the move() -method. If your drone is about to undertake a lot of work - -e.g. building a road, skyscraper or forest you should set a -check-point before doing so if you want your drone to return to its -current location. - -A 'start' checkpoint is automatically created when the Drone is first created. - -Markers are created and returned to using the followng two methods... - - * chkpt - Saves the drone's current location so it can be returned to later. - * move - moves the drone to a saved location. Alternatively you can provide an - org.bukkit.Location object or x,y,z and direction parameters. - -Parameters ----------- - * name - the name of the checkpoint to save or return to. - -Example -------- - - drone.chkpt('town-square'); - // - // the drone can now go off on a long excursion - // - for (i = 0; i< 100; i++){ - drone.fwd(12).box(6); - } - // - // return to the point before the excursion - // - drone.move('town-square'); - -***/ -Drone.prototype.chkpt = function(checkpoint_name){}; -Drone.prototype.move = function(checkpoint_name){}; - -/************************************************************************ -Drone.prism() method -==================== -Creates a prism. This is useful for roofs on houses. - -Parameters ----------- - - * block - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. - Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` - * width - the width of the prism - * length - the length of the prism (will be 2 time its height) - -Example -------- - - prism(blocks.oak,3,12); - -![prism example](img/prismex1.png) - -Drone.prism0() method -===================== -A variation on `prism` which hollows out the inside of the prism. It uses the same parameters as `prism`. - -***/ -Drone.prototype.prism = function(block,width,depth){}; -Drone.prototype.prism0 = function(block,width,depth){}; -/************************************************************************ -Drone.cylinder() method -======================= -A convenience method for building cylinders. Building begins radius blocks to the right and forward. - -Parameters ----------- - - * block - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. - Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` - * radius - * height - -Example -------- -To create a cylinder of Iron 7 blocks in radius and 1 block high... - - cylinder(blocks.iron, 7 , 1); - -![cylinder example](img/cylinderex1.png) - -Drone.cylinder0() method -======================== -A version of cylinder that hollows out the middle. - -Example -------- -To create a hollow cylinder of Iron 7 blocks in radius and 1 block high... - - cylinder0(blocks.iron, 7, 1); - -![cylinder0 example](img/cylinder0ex1.png) - -***/ -Drone.prototype.cylinder = function(block,radius,height){}; -Drone.prototype.cylinder0 = function(block,radius,height){}; -/************************************************************************ -Drone.arc() method -================== -The arc() method can be used to create 1 or more 90 degree arcs in the horizontal or vertical planes. -This method is called by cylinder() and cylinder0() and the sphere() and sphere0() methods. - -Parameters ----------- -arc() takes a single parameter - an object with the following named properties... - - * radius - The radius of the arc. - * blockType - The type of block to use - this is the block Id only (no meta). See [Data Values][dv]. - * meta - The metadata value. See [Data Values][dv]. - * orientation (default: 'horizontal') - the orientation of the arc - can be 'vertical' or 'horizontal'. - * stack (default: 1) - the height or length of the arc (depending on - the orientation - if orientation is horizontal then this parameter - refers to the height, if vertical then it refers to the length). - * strokeWidth (default: 1) - the width of the stroke (how many - blocks) - if drawing nested arcs it's usually a good idea to set - strokeWidth to at least 2 so that there are no gaps between each - arc. The arc method uses a [bresenham algorithm][bres] to plot - points along the circumference. - * fill - If true (or present) then the arc will be filled in. - * quadrants (default: - `{topleft:true,topright:true,bottomleft:true,bottomright:true}` - An - object with 4 properties indicating which of the 4 quadrants of a - circle to draw. If the quadrants property is absent then all 4 - quadrants are drawn. - -Examples --------- -To draw a 1/4 circle (top right quadrant only) with a radius of 10 and stroke width of 2 blocks ... - - arc({blockType: blocks.iron, - meta: 0, - radius: 10, - strokeWidth: 2, - quadrants: { topright: true }, - orientation: 'vertical', - stack: 1, - fill: false - }); - -![arc example 1](img/arcex1.png) - -[bres]: http://en.wikipedia.org/wiki/Midpoint_circle_algorithm -[dv]: http://www.minecraftwiki.net/wiki/Data_values -***/ -Drone.prototype.arc = function(params) {}; - -/************************************************************************ -Drone.door() method -=================== -create a door - if a parameter is supplied an Iron door is created otherwise a wooden door is created. - -Parameters ----------- - * doorType (optional - default wood) - If a parameter is provided then the door is Iron. - -Example -------- -To create a wooden door at the crosshairs/drone's location... - - var drone = new Drone(); - drone.door(); - -To create an iron door... - - drone.door( blocks.door_iron ); - -![iron door](img/doorex1.png) - -Drone.door2() method -==================== -Create double doors (left and right side) - -Parameters ----------- - * doorType (optional - default wood) - If a parameter is provided then the door is Iron. - -Example -------- -To create double-doors at the cross-hairs/drone's location... - - drone.door2(); - -![double doors](img/door2ex1.png) - -***/ -Drone.prototype.door = function(b){}; -Drone.prototype.door2 = function(b){}; -/************************************************************************ -Drone.sign() method -=================== -Signs must use block 63 (stand-alone signs) or 68 (signs on walls) - -Parameters ----------- - * message - can be a string or an array of strings. - * block - can be 63 or 68 - -Example -------- -To create a free-standing sign... - - drone.sign(["Hello","World"],63); - -![ground sign](img/signex1.png) - -... to create a wall mounted sign... - - drone.sign(["Welcome","to","Scriptopia"], 68); - -![wall sign](img/signex2.png) - -***/ -Drone.prototype.sign = function(s,b){}; -/************************************************************************ -Drone Trees methods -=================== - - * oak() - * spruce() - * birch() - * jungle() - -Example -------- -To create 4 trees in a row, point the cross-hairs at the ground then type `/js ` and ... - - up().oak().right(8).spruce().right(8).birch().right(8).jungle(); - -Trees won't always generate unless the conditions are right. You -should use the tree methods when the drone is directly above the -ground. Trees will usually grow if the drone's current location is -occupied by Air and is directly above an area of grass (That is why -the `up()` method is called first). - -![tree example](img/treeex1.png) - - -None of the tree methods require parameters. Tree methods will only be successful -if the tree is placed on grass in a setting where trees can grow. -***/ -Drone.prototype.oak= function(){}; -Drone.prototype.spruce = function(){}; -Drone.prototype.birch = function(){}; -Drone.prototype.jungle = function(){}; -/************************************************************************ -Drone.garden() method -===================== -places random flowers and long grass (similar to the effect of placing bonemeal on grass) - -Parameters ----------- - - * width - the width of the garden - * length - how far from the drone the garden extends - -Example -------- -To create a garden 10 blocks wide by 5 blocks long... - - garden(10,5); - -![garden example](img/gardenex1.png) - -***/ -Drone.prototype.garden = function(w,d){}; -/************************************************************************ -Drone.rand() method -=================== -rand takes either an array (if each blockid has the same chance of occurring) -or an object where each property is a blockid and the value is it's weight (an integer) - -Example -------- -place random blocks stone, mossy stone and cracked stone (each block has the same chance of being picked) - - rand( [blocks.brick.stone, blocks.brick.mossy, blocks.brick.cracked ],w,d,h) - -to place random blocks stone has a 50% chance of being picked, - - rand({blocks.brick.stone: 5, blocks.brick.mossy: 3, blocks.brick.cracked: 2},w,d,h) - -regular stone has a 50% chance, mossy stone has a 30% chance and cracked stone has just a 20% chance of being picked. - -***/ -Drone.prototype.rand = function(distribution,w,h,d){}; -/************************************************************************ -Copy & Paste using Drone -======================== -A drone can be used to copy and paste areas of the game world. - -Drone.copy() method -=================== -Copies an area so it can be pasted elsewhere. The name can be used for -pasting the copied area elsewhere... - -Parameters ----------- - - * name - the name to be given to the copied area (used by `paste`) - * width - the width of the area to copy - * height - the height of the area to copy - * length - the length of the area (extending away from the drone) to copy - -Example -------- - - drone.copy('somethingCool',10,5,10).right(12).paste('somethingCool'); - -Drone.paste() method -==================== -Pastes a copied area to the current location. - -Example -------- -To copy a 10x5x10 area (using the drone's coordinates as the starting -point) into memory. the copied area can be referenced using the name -'somethingCool'. The drone moves 12 blocks right then pastes the copy. - - drone.copy('somethingCool',10,5,10) - .right(12) - .paste('somethingCool'); - -***/ -Drone.prototype.copy = function(name,w,h,d){}; -Drone.prototype.paste = function(name){}; -/************************************************************************ -Chaining -======== - -All of the Drone methods return a Drone object, which means methods -can be 'chained' together so instead of writing this... - - drone = new Drone(); - drone.fwd(3); - drone.left(2); - drone.box(2); // create a grass block - drone.up(); - drone.box(2); // create another grass block - drone.down(); - -...you could simply write ... - - var drone = new Drone().fwd(3).left(2).box(2).up().box(2).down(); - -... since each Drone method is also a global function that constructs -a drone if none is supplied, you can shorten even further to just... - - fwd(3).left(2).box(2).up().box(2).down() - -The Drone object uses a [Fluent Interface][fl] to make ScriptCraft -scripts more concise and easier to write and read. Minecraft's -in-game command prompt is limited to about 80 characters so chaining -drone commands together means more can be done before hitting the -command prompt limit. For complex building you should save your -commands in a new script file and load it using /js load() - -[fl]: http://en.wikipedia.org/wiki/Fluent_interface - -Drone Properties -================ - - * x - The Drone's position along the west-east axis (x increases as you move east) - * y - The Drone's position along the vertical axis (y increses as you move up) - * z - The Drone's position along the north-south axis (z increases as you move south) - * dir - The Drone's direction 0 is east, 1 is south , 2 is west and 3 is north. - -Extending Drone -=============== -The Drone object can be easily extended - new buidling recipes/blue-prints can be added and can -become part of a Drone's chain using the *static* method `Drone.extend`. - -Drone.extend() static method -============================ -Use this method to add new methods (which also become chainable global functions) to the Drone object. - -Parameters ----------- - * name - The name of the new method e.g. 'pyramid' - * function - The method body. - -Example -------- - - // submitted by [edonaldson][edonaldson] - Drone.extend('pyramid', function(block,height){ - this.chkpt('pyramid'); - for (var i = height; i > 0; i -= 2) { - this.box(block, i, 1, i).up().right().fwd(); - } - return this.move('pyramid'); - }); - -Once the method is defined (it can be defined in a new pyramid.js file) it can be used like so... - - var d = new Drone(); - d.pyramid(blocks.brick.stone, 12); - -... or simply ... - - pyramid(blocks.brick.stone, 12); - -[edonaldson]: https://github.com/edonaldson - -Drone Constants -=============== - -Drone.PLAYER_STAIRS_FACING --------------------------- -An array which can be used when constructing stairs facing in the Drone's direction... - - var d = new Drone(); - d.box(blocks.stairs.oak + ':' + Drone.PLAYER_STAIRS_FACING[d.dir]); - -... will construct a single oak stair block facing the drone. - -Drone.PLAYER_SIGN_FACING ------------------------- -An array which can be used when placing signs so they face in a given direction. -This is used internally by the Drone.sign() method. It should also be used for placing -any of the following blocks... - - * chest - * ladder - * furnace - * dispenser - -To place a chest facing the Drone ... - - drone.box( blocks.chest + ':' + Drone.PLAYER_SIGN_FACING[drone.dir]); - -Drone.PLAYER_TORCH_FACING -------------------------- -Used when placing torches so that they face towards the drone. - - drone.box( blocks.torch + ':' + Drone.PLAYER_TORCH_FACING[drone.dir]); - -***/ - -// -// Implementation -// ============== -// -// There is no need to read any further unless you want to understand how the Drone object works. -// -(function(){ - Drone = function(x,y,z,dir,world) - { - this.record = false; - var usePlayerCoords = false; - var playerPos = getPlayerPos(); - if (typeof x == "undefined") - { - var mp = getMousePos(); - if (mp){ - this.x = mp.x; - this.y = mp.y; - this.z = mp.z; - if (playerPos) - this.dir = _getDirFromRotation(playerPos.yaw); - this.world = mp.world; - }else{ - // base it on the player's current location - usePlayerCoords = true; - // - // it's possible that drone.js could be loaded by a non-playing op - // (from the server console) - // - if (!playerPos){ - return null; - } - this.x = playerPos.x; - this.y = playerPos.y; - this.z = playerPos.z; - this.dir = _getDirFromRotation(playerPos.yaw); - this.world = playerPos.world; - } - }else{ - if (arguments[0] instanceof org.bukkit.Location){ - this.x = arguments[0].x; - this.y = arguments[0].y; - this.z = arguments[0].z; - this.dir = _getDirFromRotation(arguments[0].yaw); - this.world = arguments[0].world; - }else{ - this.x = x; - this.y = y; - this.z = z; - if (typeof dir == "undefined"){ - this.dir = _getDirFromRotation(playerPos.yaw); - }else{ - this.dir = dir%4; - } - if (typeof world == "undefined"){ - this.world = _getWorld(); - }else{ - this.world = world; - } - } - } - - if (usePlayerCoords){ - this.fwd(3); - } - this.chkpt('start'); - this.record = true; - this.history = []; - // for debugging - // self.sendMessage("New Drone " + this.toString()); - return this; - }; - // - // add custom methods to the Drone object using this function - // - Drone.extend = function(name, func) - { - // Drone.prototype[name] = func; - Drone.prototype['_' + name] = func; - Drone.prototype[name] = function(){ - if (this.record) - this.history.push([name,arguments]); - var oldVal = this.record; - this.record = false; - this['_' + name].apply(this,arguments); - this.record = oldVal; - return this; - }; - - global[name] = function(){ - var result = new Drone(); - result[name].apply(result,arguments); - return result; - }; - }; -/************************************************************************** -Drone.times() Method -==================== -The times() method makes building multiple copies of buildings easy. It's possible to create rows or grids of buildings without resorting to `for` or `while` loops. - -Parameters ----------- - * numTimes (optional - default 2) : The number of times you want to repeat the preceding statements. - -Example -------- -Say you want to do the same thing over and over. You have a couple of options... - - * You can use a for loop... - - d = new Drone(); for (var i =0;i < 4; i++){ d.cottage().right(8); } - -While this will fit on the in-game prompt, it's awkward. You need to -declare a new Drone object first, then write a for loop to create the -4 cottages. It's also error prone, even the `for` loop is too much -syntax for what should really be simple. - - * You can use a while loop... - - d = new Drone(); var i=4; while (i--){ d.cottage().right(8); } - -... which is slightly shorter but still too much syntax. Each of the -above statements is fine for creating a 1-dimensional array of -structures. But what if you want to create a 2-dimensional or -3-dimensional array of structures? Enter the `times()` method. - -The `times()` method lets you repeat commands in a chain any number of -times. So to create 4 cottages in a row you would use the following -statement... - - cottage().right(8).times(4); - -...which will build a cottage, then move right 8 blocks, then do it -again 4 times over so that at the end you will have 4 cottages in a -row. What's more the `times()` method can be called more than once in -a chain. So if you wanted to create a *grid* of 20 houses ( 4 x 5 ), -you would do so using the following statement... - - cottage().right(8).times(4).fwd(8).left(32).times(5); - -... breaking it down... - - 1. The first 3 calls in the chain ( `cottage()`, `right(8)`, - `times(4)` ) build a single row of 4 cottages. - - 2. The last 3 calls in the chain ( `fwd(8)`, `left(32)`, `times(5)` ) - move the drone forward 8 then left 32 blocks (4 x 8) to return to - the original x coordinate, then everything in the chain is - repeated again 5 times so that in the end, we have a grid of 20 - cottages, 4 x 5. Normally this would require a nested loop but - the `times()` method does away with the need for loops when - repeating builds. - -Another example: This statement creates a row of trees 2 by 3 ... - - oak().right(10).times(2).left(20).fwd(10).times(3) - -... You can see the results below. - -![times example 1](img/times-trees.png) - -***/ - Drone.prototype.times = function(numTimes,commands) - { - if (typeof numTimes == "undefined") - numTimes = 2; - if (typeof commands == "undefined") - commands = this.history.concat(); - - this.history = [['times',[numTimes+1,commands]]]; - var oldVal = this.record; - this.record = false; - for (var j = 1; j < numTimes; j++) - { - for (var i = 0;i < commands.length; i++){ - var command = commands[i]; - var methodName = command[0]; - var args = command[1]; - print ("command=" + JSON.stringify(command) + ",methodName=" + methodName); - this[methodName].apply(this,args); - } - } - this.record = oldVal; - return this; - }; - - Drone.prototype._checkpoints = {}; - - Drone.extend('chkpt',function(name){ - this._checkpoints[name] = {x:this.x,y:this.y,z:this.z,dir:this.dir}; - }); - - Drone.extend('move', function() { - if (arguments[0] instanceof org.bukkit.Location){ - this.x = arguments[0].x; - this.y = arguments[0].y; - this.z = arguments[0].z; - this.dir = _getDirFromRotation(arguments[0].yaw); - this.world = arguments[0].world; - }else if (typeof arguments[0] === "string"){ - var coords = this._checkpoints[arguments[0]]; - if (coords){ - this.x = coords.x; - this.y = coords.y; - this.z = coords.z; - this.dir = coords.dir%4; - } - }else{ - // expect x,y,z,dir - switch(arguments.length){ - case 4: - this.dir = arguments[3]; - case 3: - this.z = arguments[2]; - case 2: - this.y = arguments[1]; - case 1:n - this.x = arguments[0]; - } - } - }); - - Drone.extend('turn',function(n){ - if (typeof n == "undefined") - n = 1; - this.dir += n; - this.dir %=4; - }); - Drone.extend('right',function(n){ - if (typeof n == "undefined") - n = 1; - _movements[this.dir].right(this,n); - }); - Drone.extend('left',function(n){ - if (typeof n == "undefined") - n = 1; - _movements[this.dir].left(this,n); - }); - Drone.extend('fwd',function(n){ - if (typeof n == "undefined") - n = 1; - _movements[this.dir].fwd(this,n); - }); - Drone.extend('back',function(n){ - if (typeof n == "undefined") - n = 1; - _movements[this.dir].back(this,n); - }); - Drone.extend('up',function(n){ - if (typeof n == "undefined") - n = 1; - this.y+= n; - }); - Drone.extend('down',function(n){ - if (typeof n == "undefined") - n = 1; - this.y-= n; - }); - // - // position - // - Drone.prototype.getLocation = function() { - return new org.bukkit.Location(this.world, this.x, this.y, this.z); - }; - // - // building - // - Drone.extend('sign',function(message,block){ - if (message.constructor == Array){ - }else{ - message = [message]; - } - var bm = this._getBlockIdAndMeta(block); - block = bm[0]; - var meta = bm[1]; - if (block != 63 && block != 68){ - print("ERROR: Invalid block id for use in signs"); - return; - } - if (block == 68){ - meta = Drone.PLAYER_SIGN_FACING[this.dir%4]; - this.back(); - } - if (block == 63){ - meta = (12 + ((this.dir+2)*4)) % 16; - } - putSign(message,this.x,this.y,this.z,block,meta); - if (block == 68){ - this.fwd(); - } - }); - Drone.prototype.cuboida = function(/* Array */ blocks,w,h,d){ - var properBlocks = []; - var len = blocks.length; - for (var i = 0;i < len;i++){ - var bm = this._getBlockIdAndMeta(blocks[i]); - properBlocks.push([bm[0],bm[1]]); - } - if (typeof h == "undefined") - h = 1; - if (typeof d == "undefined") - d = 1; - if (typeof w == "undefined") - w = 1; - var that = this; - var dir = this.dir; - var pl = org.bukkit.entity.Player; - var cs = org.bukkit.command.BlockCommandSender; - var bi = 0; - /* - - */ - _traverse[dir].depth(that,d,function(){ - _traverseHeight(that,h,function(){ - _traverse[dir].width(that,w,function(){ - var block = that.world.getBlockAt(that.x,that.y,that.z); - var properBlock = properBlocks[bi%len]; - block.setTypeIdAndData(properBlock[0],properBlock[1],false); - bi++; - }); - }); - }); - return this; - - }; - /* - faster cuboid because blockid, meta and world must be provided - use this method when you need to repeatedly place blocks - */ - Drone.prototype.cuboidX = function(blockType, meta, w, h, d){ - - if (typeof h == "undefined") - h = 1; - if (typeof d == "undefined") - d = 1; - if (typeof w == "undefined") - w = 1; - var that = this; - var dir = this.dir; - - var depthFunc = function(){ - var block = that.world.getBlockAt(that.x,that.y,that.z); - block.setTypeIdAndData(blockType,meta,false); - // wph 20130210 - dont' know if this is a bug in bukkit but for chests, - // the metadata is ignored (defaults to 2 - south facing) - // only way to change data is to set it using property/bean. - block.data = meta; - }; - var heightFunc = function(){ - _traverse[dir].depth(that,d,depthFunc); - }; - var widthFunc = function(){ - _traverseHeight(that,h,heightFunc); - }; - - _traverse[dir].width(that,w,widthFunc); - return this; - - }; - - Drone.prototype.cuboid = function(block,w,h,d){ - var bm = this._getBlockIdAndMeta(block); - return this.cuboidX(bm[0],bm[1], w,h,d); - }; - Drone.prototype.cuboid0 = function(block,w,h,d){ - this.chkpt('start_point'); - - // Front wall - this.cuboid(block, w, h, 1); - // Left wall - this.cuboid(block, 1, h, d); - // Right wall - this.right(w-1).cuboid(block, 1, h, d).left(w-1); - // Back wall - this.fwd(d-1).cuboid(block, w, h, 1); - - return this.move('start_point'); - }; - Drone.extend('door',function(door){ - if (typeof door == "undefined"){ - door = 64; - }else{ - door = 71; - } - this.cuboid(door+':' + this.dir).up().cuboid(door+':8').down(); - }); - Drone.extend('door2',function(door){ - if (typeof door == "undefined"){ - door = 64; - }else{ - door = 71; - } - this - .box(door+':' + this.dir).up() - .box(door+':8').right() - .box(door+':9').down() - .box(door+':' + this.dir).left(); - }); - // player dirs: 0 = east, 1 = south, 2 = west, 3 = north - // block dirs: 0 = east, 1 = west, 2 = south , 3 = north - // sign dirs: 5 = east, 3 = south, 4 = west, 2 = north - Drone.PLAYER_STAIRS_FACING = [0,2,1,3]; - // for blocks 68 (wall signs) 65 (ladders) 61,62 (furnaces) 23 (dispenser) and 54 (chest) - Drone.PLAYER_SIGN_FACING = [4,2,5,3]; - Drone.PLAYER_TORCH_FACING = [2,4,1,3]; - - var _getWorld = function(){ - var pl = org.bukkit.entity.Player; - var cs = org.bukkit.command.BlockCommandSender; - var world = (self instanceof pl)?self.location.world:(self instanceof cs)?self.block.location.world:null; - return world; - }; - - var _STAIRBLOCKS = {53: '5:0' // oak wood - ,67: 4 // cobblestone - ,108: 45 // brick - ,109: 98 // stone brick - ,114: 112 // nether brick - ,128: 24 // sandstone - ,134: '5:1' // spruce wood - ,135: '5:2' // birch wood - ,136: '5:3' // jungle wood - }; - // - // prism private implementation - // - var _prism = function(block,w,d) - { - var stairEquiv = _STAIRBLOCKS[block]; - if (stairEquiv){ - this.fwd().prism(stairEquiv,w,d-2).back(); - var d2 = 0; - var middle = Math.floor(d/2); - var uc = 0,dc = 0; - while (d2 < d) - { - var di = (d2 < middle?this.dir:(this.dir+2)%4); - var bd = block + ':' + Drone.PLAYER_STAIRS_FACING[di]; - var putStep = true; - if (d2 == middle){ - if (d % 2 == 1){ - putStep = false; - } - } - if (putStep) - this.cuboid(bd,w); - if (d2 < middle-1){ - this.up(); - uc++; - } - var modulo = d % 2; - if (modulo == 1){ - if (d2 > middle && d2= middle && d2= 1){ - this.cuboid(block,w,1,d2); - d2 -= 2; - this.fwd().up(); - c++; - } - this.down(c).back(c); - } - return this; - }; - // - // prism0 private implementation - // - var _prism0 = function(block,w,d){ - this.prism(block,w,d) - .fwd().right() - .prism(0,w-2,d-2) - .left().back(); - var se = _STAIRBLOCKS[block]; - if (d % 2 == 1 && se){ - // top of roof will be open - need repair - var f = Math.floor(d/2); - this.fwd(f).up(f).cuboid(se,w).down(f).back(f); - } - }; - Drone.extend('prism0',_prism0); - Drone.extend('prism',_prism); - Drone.extend('box',Drone.prototype.cuboid); - Drone.extend('box0',Drone.prototype.cuboid0); - Drone.extend('boxa',Drone.prototype.cuboida); - // - // show the Drone's position and direction - // - Drone.prototype.toString = function(){ - var dirs = ["east","south","west","north"]; - return "x: " + this.x + " y: "+this.y + " z: " + this.z + " dir: " + this.dir + " "+dirs[this.dir]; - }; - Drone.prototype.debug = function(){ - print(this.toString()); - return this; - }; - /* - do the bresenham thing - */ - var _bresenham = function(x0,y0,radius, setPixel, quadrants){ - // - // credit: Following code is copied almost verbatim from - // http://en.wikipedia.org/wiki/Midpoint_circle_algorithm - // Bresenham's circle algorithm - // - var f = 1 - radius; - var ddF_x = 1; - var ddF_y = -2 * radius; - var x = 0; - var y = radius; - quadrants = quadrants || {topleft: true, - topright: true, - bottomleft: true, - bottomright: true}; - /* - II | I - ------------ - III | IV - */ - if (quadrants.topleft || quadrants.topright) - setPixel(x0, y0 + radius); // quadrant I/II topmost - if (quadrants.bottomleft || quadrants.bottomright) - setPixel(x0, y0 - radius); // quadrant III/IV bottommost - if (quadrants.topright || quadrants.bottomright) - setPixel(x0 + radius, y0); // quadrant I/IV rightmost - if (quadrants.topleft || quadrants.bottomleft) - setPixel(x0 - radius, y0); // quadrant II/III leftmost - - while(x < y) - { - // ddF_x == 2 * x + 1; - // ddF_y == -2 * y; - // f == x*x + y*y - radius*radius + 2*x - y + 1; - if(f >= 0) - { - y--; - ddF_y += 2; - f += ddF_y; - } - x++; - ddF_x += 2; - f += ddF_x; - if (quadrants.topright){ - setPixel(x0 + x, y0 + y); // quadrant I - setPixel(x0 + y, y0 + x); // quadrant I - } - if (quadrants.topleft){ - setPixel(x0 - x, y0 + y); // quadrant II - setPixel(x0 - y, y0 + x); // quadrant II - } - if (quadrants.bottomleft){ - setPixel(x0 - x, y0 - y); // quadrant III - setPixel(x0 - y, y0 - x); // quadrant III - } - if (quadrants.bottomright){ - setPixel(x0 + x, y0 - y); // quadrant IV - setPixel(x0 + y, y0 - x); // quadrant IV - } - } - }; - var _getStrokeDir = function(x,y){ - var absY = Math.abs(y); - var absX = Math.abs(x); - var strokeDir = 0; - if (y > 0 && absY >= absX) - strokeDir = 0 ; //down - else if (y < 0 && absY >= absX) - strokeDir = 1 ; // up - else if (x > 0 && absX >= absY) - strokeDir = 2 ; // left - else if (x < 0 && absX >= absY) - strokeDir = 3 ; // right - return strokeDir; - }; - /* - The daddy of all arc-related API calls - - if you're drawing anything that bends it ends up here. - */ - var _arc2 = function( params ) { - - var drone = params.drone; - var orientation = params.orientation || "horizontal"; - var quadrants = params.quadrants || { - topright:1, - topleft:2, - bottomleft:3, - bottomright:4 - }; - var stack = params.stack || 1; - var radius = params.radius; - var strokeWidth = params.strokeWidth || 1; - drone.chkpt('arc2'); - var x0, y0, gotoxy,setPixel; - - if (orientation == "horizontal"){ - gotoxy = function(x,y){ return drone.right(x).fwd(y);}; - drone.right(radius).fwd(radius).chkpt('center'); - switch (drone.dir) { - case 0: // east - case 2: // west - x0 = drone.z; - y0 = drone.x; - break; - case 1: // south - case 3: // north - x0 = drone.x; - y0 = drone.z; - } - setPixel = function(x,y) { - x = (x-x0); - y = (y-y0); - if (params.fill){ - // wph 20130114 more efficient esp. for large cylinders/spheres - if (y < 0){ - drone - .fwd(y).right(x) - .cuboidX(params.blockType,params.meta,1,stack,Math.abs(y*2)+1) - .back(y).left(x); - } - }else{ - if (strokeWidth == 1){ - gotoxy(x,y) - .cuboidX(params.blockType, - params.meta, - 1, // width - stack, // height - strokeWidth // depth - ) - .move('center'); - } else { - var strokeDir = _getStrokeDir(x,y); - var width = 1, depth = 1; - switch (strokeDir){ - case 0: // down - y = y-(strokeWidth-1); - depth = strokeWidth; - break; - case 1: // up - depth = strokeWidth; - break; - case 2: // left - width = strokeWidth; - x = x-(strokeWidth-1); - break; - case 3: // right - width = strokeWidth; - break; - } - gotoxy(x,y) - .cuboidX(params.blockType, params.meta, width, stack, depth) - .move('center'); - - } - } - }; - }else{ - // vertical - gotoxy = function(x,y){ return drone.right(x).up(y);}; - drone.right(radius).up(radius).chkpt('center'); - switch (drone.dir) { - case 0: // east - case 2: // west - x0 = drone.z; - y0 = drone.y; - break; - case 1: // south - case 3: // north - x0 = drone.x; - y0 = drone.y; - } - setPixel = function(x,y) { - x = (x-x0); - y = (y-y0); - if (params.fill){ - // wph 20130114 more efficient esp. for large cylinders/spheres - if (y < 0){ - drone - .up(y).right(x) - .cuboidX(params.blockType,params.meta,1,Math.abs(y*2)+1,stack) - .down(y).left(x); - } - }else{ - if (strokeWidth == 1){ - gotoxy(x,y) - .cuboidX(params.blockType,params.meta,strokeWidth,1,stack) - .move('center'); - }else{ - var strokeDir = _getStrokeDir(x,y); - var width = 1, height = 1; - switch (strokeDir){ - case 0: // down - y = y-(strokeWidth-1); - height = strokeWidth; - break; - case 1: // up - height = strokeWidth; - break; - case 2: // left - width = strokeWidth; - x = x-(strokeWidth-1); - break; - case 3: // right - width = strokeWidth; - break; - } - gotoxy(x,y) - .cuboidX(params.blockType, params.meta, width, height, stack) - .move('center'); - - } - } - }; - } - /* - setPixel assumes a 2D plane - need to put a block along appropriate plane - */ - _bresenham(x0,y0,radius,setPixel,quadrants); - - params.drone.move('arc2'); - }; - - - Drone.extend('arc',function(params) { - params.drone = this; - _arc2(params); - }); - // ======================================================================== - // Private variables and functions - // ======================================================================== - var _cylinder0 = function(block,radius,height,exactParams){ - var arcParams = { - radius: radius, - fill: false, - orientation: 'horizontal', - stack: height, - }; - - if (exactParams){ - arcParams.blockType = exactParams.blockType; - arcParams.meta = exactParams.meta; - }else{ - var md = this._getBlockIdAndMeta(block); - arcParams.blockType = md[0]; - arcParams.meta = md[1]; - } - return this.arc(arcParams); - }; - var _cylinder1 = function(block,radius,height,exactParams){ - var arcParams = { - radius: radius, - fill: true, - orientation: 'horizontal', - stack: height, - }; - - if (exactParams){ - arcParams.blockType = exactParams.blockType; - arcParams.meta = exactParams.meta; - }else{ - var md = this._getBlockIdAndMeta(block); - arcParams.blockType = md[0]; - arcParams.meta = md[1]; - } - return this.arc(arcParams); - }; - var _getDirFromRotation = function(r){ - // 0 = east, 1 = south, 2 = west, 3 = north - // 46 to 135 = west - // 136 to 225 = north - // 226 to 315 = east - // 316 to 45 = south - - r = (r + 360) % 360; // east could be 270 or -90 - - if (r > 45 && r <= 135) - return 2; // west - if (r > 135 && r <= 225) - return 3; // north - if (r > 225 && r <= 315) - return 0; // east - if (r > 315 || r < 45) - return 1; // south - }; - var _getBlockIdAndMeta = function(b){ - var defaultMeta = 0; - if (typeof b == 'string'){ - var bs = b; - var sp = bs.indexOf(':'); - if (sp == -1){ - b = parseInt(bs); - // wph 20130414 - use sensible defaults for certain blocks e.g. stairs - // should face the drone. - for (var i in blocks.stairs){ - if (blocks.stairs[i] === b){ - defaultMeta = Drone.PLAYER_STAIRS_FACING[this.dir]; - break; - } - } - return [b,defaultMeta]; - } - b = parseInt(bs.substring(0,sp)); - var md = parseInt(bs.substring(sp+1,bs.length)); - return [b,md]; - }else{ - // wph 20130414 - use sensible defaults for certain blocks e.g. stairs - // should face the drone. - for (var i in blocks.stairs){ - if (blocks.stairs[i] === b){ - defaultMeta = Drone.PLAYER_STAIRS_FACING[this.dir]; - break; - } - } - return [b,defaultMeta]; - } - }; - // - // movement - // - var _movements = [{},{},{},{}]; - // east - _movements[0].right = function(that,n){ that.z +=n; return that;}; - _movements[0].left = function(that,n){ that.z -=n; return that;}; - _movements[0].fwd = function(that,n){ that.x +=n; return that;}; - _movements[0].back = function(that,n){ that.x -= n; return that;}; - // south - _movements[1].right = _movements[0].back; - _movements[1].left = _movements[0].fwd; - _movements[1].fwd = _movements[0].right; - _movements[1].back = _movements[0].left; - // west - _movements[2].right = _movements[0].left; - _movements[2].left = _movements[0].right; - _movements[2].fwd = _movements[0].back; - _movements[2].back = _movements[0].fwd; - // north - _movements[3].right = _movements[0].fwd; - _movements[3].left = _movements[0].back; - _movements[3].fwd = _movements[0].left; - _movements[3].back = _movements[0].right; - var _traverse = [{},{},{},{}]; - // east - _traverse[0].width = function(that,n,callback){ - var s = that.z, e = s + n; - for (; that.z < e; that.z++){ - callback(that.z-s); - } - that.z = s; - }; - _traverse[0].depth = function(that,n,callback){ - var s = that.x, e = s+n; - for (;that.x < e;that.x++){ - callback(that.x-s); - } - that.x = s; - }; - // south - _traverse[1].width = function(that,n,callback){ - var s = that.x, e = s-n; - for (;that.x > e;that.x--){ - callback(s-that.x); - } - that.x = s; - }; - _traverse[1].depth = _traverse[0].width; - // west - _traverse[2].width = function(that,n,callback){ - var s = that.z, e = s-n; - for (;that.z > e;that.z--){ - callback(s-that.z); - } - that.z = s; - }; - _traverse[2].depth = _traverse[1].width; - // north - _traverse[3].width = _traverse[0].depth; - _traverse[3].depth = _traverse[2].width; - var _traverseHeight = function(that,n,callback){ - var s = that.y, e = s + n; - for (; that.y < e; that.y++){ - callback(that.y-s); - } - that.y = s; - }; - // - // standard fisher-yates shuffle algorithm - // - var _fisherYates = function( myArray ) { - var i = myArray.length; - if ( i == 0 ) return false; - while ( --i ) { - var j = Math.floor( Math.random() * ( i + 1 ) ); - var tempi = myArray[i]; - var tempj = myArray[j]; - myArray[i] = tempj; - myArray[j] = tempi; - } - }; - - var _rand = function(blockDistribution){ - if (!(blockDistribution.constructor == Array)){ - var a = []; - for (var p in blockDistribution){ - var n = blockDistribution[p]; - for (var i = 0;i < n;i++){ - a.push(p); - } - } - blockDistribution = a; - } - while (blockDistribution.length < 1000){ - // make array bigger so that it's more random - blockDistribution = blockDistribution.concat(blockDistribution); - } - _fisherYates(blockDistribution); - return blockDistribution; - }; - Drone.extend('rand',function(dist,w,h,d){ - var randomized = _rand(dist); - this.boxa(randomized,w,h,d); - }); - var _trees = { - oak: org.bukkit.TreeType.BIG_TREE , - birch: org.bukkit.TreeType.BIRCH , - jungle: org.bukkit.TreeType.JUNGLE, - spruce: org.bukkit.TreeType.REDWOOD - }; - for (var p in _trees) - { - Drone.extend(p, function(v) { - return function() { - var block = this.world.getBlockAt(this.x,this.y,this.z); - if (block.typeId == 2){ - this.up(); - } - var treeLoc = new org.bukkit.Location(this.world,this.x,this.y,this.z); - var successful = treeLoc.world.generateTree(treeLoc,v); - if (block.typeId == 2){ - this.down(); - } - }; - }(_trees[p])); - } - - Drone.extend('garden',function(w,d) - { - // make sure grass is present first - this.down().box(2,w,1,d).up(); - - // make flowers more common than long grass - var dist = {37: 3, // red flower - 38: 3, // yellow flower - '31:1': 2, // long grass - 0: 1 - }; - - return this.rand(dist,w,1,d); - }); - // - // Drone's clipboard - // - Drone.clipBoard = {}; - Drone.extend('copy', function(name, w, h, d) - { - var that = this; - var ccContent = []; - _traverse[this.dir].width(that,w,function(ww){ - ccContent.push([]); - _traverseHeight(that,h,function(hh){ - ccContent[ww].push([]); - _traverse[that.dir].depth(that,d,function(dd){ - var b = getBlock(that.x,that.y,that.z); - ccContent[ww][hh][dd] = b; - }); - }); - }); - Drone.clipBoard[name] = {dir: this.dir, blocks: ccContent}; - }); - - Drone.extend('paste',function(name) - { - var ccContent = Drone.clipBoard[name]; - var srcBlocks = ccContent.blocks; - var srcDir = ccContent.dir; // direction player was facing when copied. - var dirOffset = (4 + (this.dir - srcDir)) %4; - var that = this; - _traverse[this.dir].width(that,srcBlocks.length,function(ww){ - var h = srcBlocks[ww].length; - _traverseHeight(that,h,function(hh){ - var d = srcBlocks[ww][hh].length; - _traverse[that.dir].depth(that,d,function(dd){ - var b = srcBlocks[ww][hh][dd]; - var bm = that._getBlockIdAndMeta(b); - var cb = bm[0]; - var md = bm[1]; - // - // need to adjust blocks which face a direction - // - switch (cb) - { - // - // doors - // - case 64: // wood - case 71: // iron - // top half of door doesn't need to change - if (md < 8) { - md = (md + dirOffset) % 4; - } - break; - // - // stairs - // - case 53: // oak - case 67: // cobblestone - case 108: // red brick - case 109: // stone brick - case 114: // nether brick - case 128: // sandstone - case 134: // spruce - case 135: // birch - case 136: // junglewood - var dir = md & 0x3; - var a = Drone.PLAYER_STAIRS_FACING; - var len = a.length; - for (var c=0;c < len;c++){ - if (a[c] == dir){ - break; - } - } - c = (c + dirOffset) %4; - var newDir = a[c]; - md = (md >>2<<2) + newDir; - break; - // - // signs , ladders etc - // - case 23: // dispenser - case 54: // chest - case 61: // furnace - case 62: // burning furnace - case 65: // ladder - case 68: // wall sign - var a = Drone.PLAYER_SIGN_FACING; - var len = a.length; - for (var c=0;c < len;c++){ - if (a[c] == md){ - break; - } - } - c = (c + dirOffset) %4; - var newDir = a[c]; - md = newDir; - break; - } - putBlock(that.x,that.y,that.z,cb,md); - }); - }); - }); - }); - Drone.extend('cylinder0',_cylinder0); - Drone.extend('cylinder', _cylinder1); - - // - // make all Drone's methods available also as standalone functions - // which return a drone object - // this way drones can be created and used as follows... - // - // /js box(5,7,3,4) - // - // ... which is a short-hand way to create a wooden building 7x3x4 - // -/* - var ops = ['up','down','left','right','fwd','back','turn', - 'chkpt','move', - 'box','box0','boxa','prism','prism0','cylinder','cylinder0','arc', - 'door','door2','sign','oak','spruce','birch','jungle', - 'rand','garden', - 'copy','paste' - ]; - - for (var i = 0;i < ops.length; i++){ - global[ops[i]] = function(op){ - return function(){ - var result = new Drone(); - result[op].apply(result,arguments); - return result; - }; - }(ops[i]); - } -*/ - // - // wph 20130130 - make this a method - extensions can use it. - // - Drone.prototype._getBlockIdAndMeta = _getBlockIdAndMeta; - -}()); diff --git a/src/main/javascript/drone/partial.js b/src/main/javascript/drone/partial.js deleted file mode 100644 index 8adbeac..0000000 --- a/src/main/javascript/drone/partial.js +++ /dev/null @@ -1,14 +0,0 @@ -/** -* Create a partial function -* -* Parameters: -* func - base function -* [remaining arguments] - arguments bound to the partial function -*/ -function partial(func /*, 0..n args */) { - var args = Array.prototype.slice.call(arguments, 1); - return function() { - var allArguments = args.concat(Array.prototype.slice.call(arguments)); - return func.apply(this, allArguments); - }; -} diff --git a/src/main/javascript/drone/rboxcall.js b/src/main/javascript/drone/rboxcall.js deleted file mode 100644 index 15b05e0..0000000 --- a/src/main/javascript/drone/rboxcall.js +++ /dev/null @@ -1,32 +0,0 @@ -/** -* Iterates over each cube in a cubic region. For each cube has a chance to callback your -* function and provide a new drone to it. -* -* Parameters: -* callback - any function that accepts a drone as its first argument -* probability - chance to invoke your callback on each iteration -* width - width of the region -* height - (Optional) height of the region, defaults to width -* depth - (Optional) depth of the cube, defaults to width -*/ - -Drone.extend("rboxcall", function(callback, probability, width, height, depth) { - this.chkpt('rboxcall-start'); - - for(var i = 0; i < width; ++i) { - this.move('rboxcall-start').right(i); - for(var j = 0; j < depth; ++j) { - this.move('rboxcall-start').right(i).fwd(j); - for(var k = 0; k < height; ++k) { - if(Math.random()*100 < probability) { - callback.call(null, new Drone(this.x, this.y, this.z)); - } - this.up(); - } - } - } - - this.move('rboxcall-start'); - - return this; -}); diff --git a/src/main/javascript/drone/sphere.js b/src/main/javascript/drone/sphere.js deleted file mode 100644 index eaf6b8f..0000000 --- a/src/main/javascript/drone/sphere.js +++ /dev/null @@ -1,265 +0,0 @@ -/************************************************************************ -Drone.sphere() method -===================== -Creates a sphere. - -Parameters ----------- - - * block - The block the sphere will be made of. - * radius - The radius of the sphere. - -Example -------- -To create a sphere of Iron with a radius of 10 blocks... - - sphere( blocks.iron, 10); - -![sphere example](img/sphereex1.png) - -Spheres are time-consuming to make. You *can* make large spheres (250 radius) but expect the -server to be very busy for a couple of minutes while doing so. - -***/ -Drone.extend('sphere', function(block,radius) -{ - var lastRadius = radius; - var slices = [[radius,0]]; - var diameter = radius*2; - var bm = this._getBlockIdAndMeta(block); - - var r2 = radius*radius; - for (var i = 0; i <= radius;i++){ - var newRadius = Math.round(Math.sqrt(r2 - i*i)); - if (newRadius == lastRadius) - slices[slices.length-1][1]++; - else - slices.push([newRadius,1]); - lastRadius = newRadius; - } - this.chkpt('sphere'); - // - // mid section - // - this.up(radius - slices[0][1]) - .cylinder(block,radius,(slices[0][1]*2)-1,{blockType: bm[0],meta: bm[1]}) - .down(radius-slices[0][1]); - - var yOffset = -1; - for (var i = 1; i < slices.length;i++) - { - yOffset += slices[i-1][1]; - var sr = slices[i][0]; - var sh = slices[i][1]; - var v = radius + yOffset, h = radius-sr; - // northern hemisphere - this.up(v).fwd(h).right(h) - .cylinder(block,sr,sh,{blockType: bm[0],meta: bm[1]}) - .left(h).back(h).down(v); - - // southern hemisphere - v = radius - (yOffset+sh+1); - this.up(v).fwd(h).right(h) - .cylinder(block,sr,sh,{blockType: bm[0],meta: bm[1]}) - .left(h).back(h). down(v); - } - return this.move('sphere'); -}); -/************************************************************************ -Drone.sphere0() method -====================== -Creates an empty sphere. - -Parameters ----------- - - * block - The block the sphere will be made of. - * radius - The radius of the sphere. - -Example -------- -To create a sphere of Iron with a radius of 10 blocks... - - sphere0( blocks.iron, 10); - -Spheres are time-consuming to make. You *can* make large spheres (250 radius) but expect the -server to be very busy for a couple of minutes while doing so. - -***/ -Drone.extend('sphere0', function(block,radius) -{ -/* - this.sphere(block,radius) - .fwd().right().up() - .sphere(0,radius-1) - .back().left().down(); - -*/ - - var lastRadius = radius; - var slices = [[radius,0]]; - var diameter = radius*2; - var bm = this._getBlockIdAndMeta(block); - - var r2 = radius*radius; - for (var i = 0; i <= radius;i++){ - var newRadius = Math.round(Math.sqrt(r2 - i*i)); - if (newRadius == lastRadius) - slices[slices.length-1][1]++; - else - slices.push([newRadius,1]); - lastRadius = newRadius; - } - this.chkpt('sphere0'); - // - // mid section - // - //.cylinder(block,radius,(slices[0][1]*2)-1,{blockType: bm[0],meta: bm[1]}) - this.up(radius - slices[0][1]) - .arc({blockType: bm[0], - meta: bm[1], - radius: radius, - strokeWidth: 2, - stack: (slices[0][1]*2)-1, - fill: false - }) - .down(radius-slices[0][1]); - - var yOffset = -1; - var len = slices.length; - for (var i = 1; i < len;i++) - { - yOffset += slices[i-1][1]; - var sr = slices[i][0]; - var sh = slices[i][1]; - var v = radius + yOffset, h = radius-sr; - // northern hemisphere - // .cylinder(block,sr,sh,{blockType: bm[0],meta: bm[1]}) - this.up(v).fwd(h).right(h) - .arc({ - blockType: bm[0], - meta: bm[1], - radius: sr, - stack: sh, - fill: false, - strokeWidth: i : Go to player's home", - "/jsp home set : Set your current location as home", - "/jsp home delete : Delete your home location", - - /* social */ - "/jsp home list : List homes you can visit", - "/jsp home ilist : List players who can visit your home", - "/jsp home invite : Invite to your home", - "/jsp home uninvite : Uninvite to your home", - "/jsp home public : Open your home to all players", - "/jsp home private : Make your home private", - - /* administration */ - "/jsp home listall : Show all houses (ops only)", - "/jsp home clear : Clears player's home location (ops only)" - ]; - }, - /* ======================================================================== - basic functions - ======================================================================== */ - - go: function(guest, host){ - if (typeof host == "undefined") - host = guest; - guest = utils.getPlayerObject(guest); - host = utils.getPlayerObject(host); - var loc = this.store.houses[host.name]; - if (!loc){ - guest.sendMessage(host.name + " has no home"); - return; - } - if (!this._canVisit(guest,host)){ - guest.sendMessage("You can't visit " + host.name + "'s home yet"); - return; - } - var worldName = loc[0], x = loc[1], y = loc[2], z=loc[3], yaw=loc[4]; - var teleportCause = org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; - var homeLoc = new org.bukkit.Location(org.bukkit.Bukkit.getWorld(worldName),x,y,z,yaw,0); - guest.teleport(homeLoc, teleportCause.PLUGIN); - }, - /* - determine whether a guest is allow visit a host's home - */ - _canVisit: function(guest, host){ - if (guest == host) - return true; - if (this.store.openHouses[host.name]) - return true; - var invitations = this.store.invites[host.name]; - if (invitations) - for (var i = 0;i < invitations.length;i++) - if (invitations[i] == guest.name) - return true; - return false; - }, - set: function(player){ - player = utils.getPlayerObject(player); - var loc = player.location; - this.store.houses[player.name] = [""+loc.world.name - ,Math.floor(loc.x) - ,Math.floor(loc.y) - ,Math.floor(loc.z) - ,Math.floor(loc.yaw) - ,Math.floor(loc.pitch)]; - }, - remove: function(player){ - player = utils.getPlayerObject(player); - delete this.store.houses[player.name]; - }, - /* ======================================================================== - social functions - ======================================================================== */ - - /* - list homes which the player can visit - */ - list: function(player){ - var result = []; - for (var ohp in this.store.openHouses) - result.push(ohp); - player = utils.getPlayerObject(player); - for (var host in this.store.invites){ - var guests = this.store.invites[host]; - for (var i = 0;i < guests.length; i++) - if (guests[i] == player.name) - result.push(host); - } - return result; - }, - /* - list who can visit the player's home - */ - ilist: function(player){ - player = utils.getPlayerObject(player); - var result = []; - // if home is public - all players - if (this.store.openHouses[player.name]){ - var online = org.bukkit.Bukkit.getOnlinePlayers(); - for (var i = 0;i < online.length; i++) - if (online[i].name != player.name) - result.push(online[i].name); - }else{ - result = this.store.invites[player.name] || []; - } - return result; - }, - /* - Invite a player to the home - */ - invite: function(host, guest){ - host = utils.getPlayerObject(host); - guest = utils.getPlayerObject(guest); - var invitations = this.store.invites[host.name] || []; - invitations.push(guest.name); - this.store.invites[host.name] = invitations; - guest.sendMessage(host.name + " has invited you to their home."); - guest.sendMessage("type '/jsp home " + host.name + "' to accept"); - }, - /* - Uninvite someone to the home - */ - uninvite: function(host, guest){ - host = utils.getPlayerObject(host); - guest = utils.getPlayerObject(guest); - var invitations = this.store.invites[host.name]; - if (!invitations) - return; - var revisedInvites = []; - for (var i =0;i < invitations.length; i++) - if (invitations[i] != guest.name) - revisedInvites.push(invitations[i]); - this.store.invites[host.name] = revisedInvites; - }, - /* - make the player's house public - */ - open: function(player, optionalMsg){ - player = utils.getPlayerObject(player); - this.store.openHouses[player.name] = true; - if (typeof optionalMsg != "undefined") - __plugin.server.broadcastMessage(optionalMsg); - }, - /* - make the player's house private - */ - close: function(player){ - player = utils.getPlayerObject(player); - delete this.store.openHouses[player.name]; - }, - /* ======================================================================== - admin functions - ======================================================================== */ - listall: function(){ - var result = []; - for (var home in this.store.houses) - result.push(home); - return result; - }, - clear: function(player){ - player = utils.getPlayerObject(player); - delete this.store.houses[player.name]; - delete this.store.openHouses[player.name]; - } - -}, true); -/* - private implementation -*/ -(function(){ - /* - define a set of command options that can be used by players - */ - var options = { - set: function(){homes.set();}, - 'delete': function(){ homes.remove();}, - help: function(){ self.sendMessage(homes.help());}, - list: function(){ - var visitable = homes.list(); - if (visitable.length == 0){ - self.sendMessage("There are no homes to visit"); - return; - }else{ - self.sendMessage([ - "You can visit any of these " + visitable.length + " homes" - ,visitable.join(", ") - ]); - } - }, - ilist: function(){ - var potentialVisitors = homes.ilist(); - if (potentialVisitors.length == 0) - self.sendMessage("No one can visit your home"); - else - self.sendMessage([ - "These " + potentialVisitors.length + "players can visit your home", - potentialVisitors.join(", ")]); - }, - invite: function(params){ - if (params.length == 1){ - self.sendMessage("You must provide a player's name"); - return; - } - var playerName = params[1]; - var guest = utils.getPlayerObject(playerName); - if (!guest) - self.sendMessage(playerName + " is not here"); - else - homes.invite(self,guest); - }, - uninvite: function(params){ - if (params.length == 1){ - self.sendMessage("You must provide a player's name"); - return; - } - var playerName = params[1]; - var guest = utils.getPlayerObject(playerName); - if (!guest) - self.sendMessage(playerName + " is not here"); - else - homes.uninvite(self,guest); - }, - 'public': function(params){ - homes.open(self,params.slice(1).join(' ')); - self.sendMessage("Your home is open to the public"); - }, - 'private': function(){ - homes.close(); - self.sendMessage("Your home is closed to the public"); - }, - listall: function(){ - if (!self.isOp()) - self.sendMessage("Only operators can do this"); - else - self.sendMessage(homes.listall().join(", ")); - }, - clear: function(params){ - if (!self.isOp()) - self.sendMessage("Only operators can do this"); - else - homes.clear(params[1]); - } - }; - var optionList = []; - for (var o in options) - optionList.push(o); - /* - Expose a set of commands that players can use at the in-game command prompt - */ - command("home", function(params){ - if (params.length == 0){ - homes.go(); - return; - } - var option = options[params[0]]; - if (option) - option(params); - else{ - var host = utils.getPlayerObject(params[0]); - if (!host) - self.sendMessage(params[0] + " is not here"); - else - homes.go(self,host); - } - },optionList); - - /* - initialize the store - */ - homes.store.houses = homes.store.houses || {}; - homes.store.openHouses = homes.store.openHouses || {}; - homes.store.invites = homes.store.invites || {}; -}()); diff --git a/src/main/javascript/http/request.js b/src/main/javascript/http/request.js deleted file mode 100644 index 3d25136..0000000 --- a/src/main/javascript/http/request.js +++ /dev/null @@ -1,102 +0,0 @@ -/************************************************************************* -http.request() function -==================== -The http.request() function will fetch a web address asynchronously (on a -separate thread)and pass the URL's response to a callback function -which will be executed synchronously (on the main thread). In this -way, http.request() can be used to fetch web content without blocking the -main thread of execution. - -Parameters ----------- - - * request: The request details either a plain URL e.g. "http://scriptcraft.js/sample.json" or an object with the following properties... - - - url: The URL of the request. - - method: Should be one of the standard HTTP methods, GET, POST, PUT, DELETE (defaults to GET). - - params: A Javascript object with name-value pairs. This is for supplying parameters to the server. - - * callback: The function to be called when the Web request has completed. This function takes the following parameters... - - responseCode: The numeric response code from the server. If the server did not respond with 200 OK then the response parameter will be undefined. - - response: A string (if the response is of type text) or object containing the HTTP response body. - -Example -------- -The following example illustrates how to use http.request to make a request to a JSON web service and evaluate its response... - - var jsResponse; - http.request("http://scriptcraftjs.org/sample.json",function(responseCode, responseBody){ - jsResponse = eval("(" + responseBody + ")"); - }); - -... The following example illustrates a more complex use-case POSTing parameters to a CGI process on a server... - - http.request({ url: "http://pixenate.com/pixenate/pxn8.pl", - method: "POST", - params: {script: "[]"} - }, function( responseCode, responseBody){ - var jsObj = eval("(" + responseBody + ")"); - }); - -***/ -var http = http || {}; - -http.request = function( request, callback) -{ - var paramsToString = function(params){ - var result = ""; - var paramNames = []; - for (var i in params){ - paramNames.push(i); - } - for (var i = 0;i < paramNames.length;i++){ - result += paramNames[i] + "=" + encodeURI(params[paramNames[i]]); - if (i < paramNames.length-1) - result += "&"; - } - return result; - }; - - server.scheduler.runTaskAsynchronously(__plugin,function() - { - var url, paramsAsString, conn, requestMethod; - if (typeof request === "string"){ - url = request; - requestMethod = "GET"; - }else{ - paramsAsString = paramsToString(request.params); - requestMethod = request.method || "GET"; - if (requestMethod == "GET" && request.params){ - // append each parameter to the URL - url = request.url + "?" + paramsAsString; - } - } - conn = new java.net.URL(url).openConnection(); - conn.requestMethod = requestMethod; - conn.doOutput = true; - conn.instanceFollowRedirects = false; - - if (conn.requestMethod == "POST"){ - conn.doInput = true; - // put each parameter in the outputstream - conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - conn.setRequestProperty("charset", "utf-8"); - conn.setRequestProperty("Content-Length", "" + paramsAsString.length); - conn.useCaches =false ; - wr = new java.io.DataOutputStream(conn.getOutputStream ()); - wr.writeBytes(paramsAsString); - wr.flush(); - wr.close(); - } - var rc = conn.responseCode; - var response; - var stream; - if (rc == 200){ - stream = conn.getInputStream(); - response = new java.util.Scanner(stream).useDelimiter("\\A").next(); - } - server.scheduler.runTask(__plugin,function(){ - callback(rc,response); - }); - }); -}; diff --git a/src/main/javascript/minigames/NumberGuess.js b/src/main/javascript/minigames/NumberGuess.js deleted file mode 100644 index b70d931..0000000 --- a/src/main/javascript/minigames/NumberGuess.js +++ /dev/null @@ -1,48 +0,0 @@ -/* - A basic number-guessing game that uses the Bukkit Conversation API. - */ -ready(function(){ - - global.GuessTheNumber = function() - { - importPackage(org.bukkit.conversations); - - var number = Math.ceil(Math.random() * 10); - - var prompt = new Prompt() - { - getPromptText: function(ctx){ - var hint = ""; - var h = ctx.getSessionData("hint"); - if (h){ - hint = h; - } - return hint + "Think of a number between 1 and 10"; - }, - acceptInput: function(ctx, s) - { - s = s.replace(/^[^0-9]+/,""); // strip leading non-numeric characters (e.g. '/' ) - s = parseInt(s); - if (s == number){ - setTimeout(function(){ - ctx.forWhom.sendRawMessage("You guessed Correct!"); - },100); - return null; - }else{ - if (s < number) - ctx.setSessionData("hint","too low\n"); - if (s > number) - ctx.setSessionData("hint","too high\n"); - return prompt; - } - }, - blocksForInput: function(ctx){ return true; } - }; - var cf = new ConversationFactory(__plugin); - var conv = cf.withModality(true) - .withFirstPrompt(prompt) - .withPrefix(new ConversationPrefix(){ getPrefix: function(ctx){ return "[1-10] ";} }) - .buildConversation(self); - conv.begin(); - }; -}); diff --git a/src/main/javascript/minigames/SnowBallFight.js b/src/main/javascript/minigames/SnowBallFight.js deleted file mode 100644 index 0876b1a..0000000 --- a/src/main/javascript/minigames/SnowBallFight.js +++ /dev/null @@ -1,180 +0,0 @@ -load(__folder + "../events/events.js"); -/* - OK - this is a rough and ready prototype of a simple multi-player shoot-em-up. - Get a bunch of players in close proximity and issue the following commands... - - /js var redTeam = ['','',...etc] - /js var blueTeam = ['',',...etc] - /js var greenTeam = ['',',...etc] - /js new SnowBallFight({red: redTeam,blue: blueTeam,green: greenTeam},60).start(); - - Alternatively you can just have all players play against each other... - - /js new SnowBallFight(['player1','player2','player3'],60).start(); - - (where etc are the names of actual players) - - You specify the teams in the game as an object where each property's name is a team name and - each property's value is the list of players on that team. - You specify the duration of the game (in seconds) - You kick off the game with the start() method. - I need to work on a better in-game mechanism for players to choose teams and start the game - but this will do for now. - - When the game starts, each player is put in survival mode and given snowballs. The aim of the - game is to hit players on opposing teams. If you hit a player on your own team, you lose a point. - - At the end of the game the scores for each team are broadcast. Create a small arena - with a couple of small buildings for cover to make the game more fun :-) - -*/ - -var SnowBallFight = function(teams, duration){}; -SnowBallFight.prototype.start = function(){}; - -(function(){ - - /* - setup game - */ - var _startGame = function(gameState){ - // don't let game start if already in progress (wait for game to finish) - if (gameState.inProgress){ - return; - } - gameState.inProgress = true; - // reset timer - gameState.duration = gameState.originalDuration; - // put all players in survival mode and give them each 200 snowballs - // 64 snowballs for every 30 seconds should be more than enough - for (var i = 10;i < gameState.duration;i+=10) - gameState.ammo.push(gameState.ammo[0]); - - for (var teamName in gameState.teams) - { - gameState.teamScores[teamName] = 0; - var team = gameState.teams[teamName]; - for (var i = 0;i < team.length;i++) { - var player = server.getPlayer(team[i]); - gameState.savedModes[player.name] = player.gameMode; - player.gameMode = org.bukkit.GameMode.SURVIVAL; - player.inventory.addItem(gameState.ammo); - } - } - }; - /* - end the game - */ - var _endGame = function(gameState){ - var scores = []; - - var leaderBoard = []; - for (var tn in gameState.teamScores){ - leaderBoard.push([tn,gameState.teamScores[tn]]); - } - leaderBoard.sort(function(a,b){ return b[1] - a[1];}); - - for (var i = 0;i < leaderBoard.length; i++){ - scores.push("Team " + leaderBoard[i][0] + " scored " + leaderBoard[i][1]); - } - - for (var teamName in gameState.teams) { - var team = gameState.teams[teamName]; - for (var i = 0;i < team.length;i++) { - // restore player's previous game mode and take back snowballs - var player = server.getPlayer(team[i]); - player.gameMode = gameState.savedModes[player.name]; - player.inventory.removeItem(gameState.ammo); - player.sendMessage("GAME OVER."); - player.sendMessage(scores); - } - } - var handlerList = org.bukkit.event.entity.EntityDamageByEntityEvent.getHandlerList(); - handlerList.unregister(gameState.listener); - gameState.inProgress = false; - }; - /* - get the team the player belongs to - */ - var _getTeam = function(player,pteams) { - for (var teamName in pteams) { - var team = pteams[teamName]; - for (var i = 0;i < team.length; i++) - if (team[i] == player.name) - return teamName; - } - return null; - }; - /* - construct a new game - */ - var _constructor = function(duration, teams) { - - var _snowBalls = new org.bukkit.inventory.ItemStack(org.bukkit.Material.SNOW_BALL, 64); - - var _gameState = { - teams: teams, - duration: duration, - originalDuration: duration, - inProgress: false, - teamScores: {}, - listener: null, - savedModes: {}, - ammo: [_snowBalls] - }; - if (typeof duration == "undefined"){ - duration = 60; - } - if (typeof teams == "undefined"){ - /* - wph 20130511 use all players - */ - teams = []; - var players = server.onlinePlayers; - for (var i = 0;i < players.length; i++){ - teams.push(players[i].name); - } - } - // - // allow for teams param to be either {red:['player1','player2'],blue:['player3']} or - // ['player1','player2','player3'] if all players are against each other (no teams) - // - if (teams instanceof Array){ - _gameState.teams = {}; - for (var i = 0;i < teams.length; i++) - _gameState.teams[teams[i]] = [teams[i]]; - } - /* - this function is called every time a player is damaged by another entity/player - */ - var _onSnowballHit = function(l,event){ - var snowball = event.damager; - if (!snowball || !(snowball instanceof org.bukkit.entity.Snowball)) - return; - var throwersTeam = _getTeam(snowball.shooter,_gameState.teams); - var damageeTeam = _getTeam(event.entity,_gameState.teams); - if (!throwersTeam || !damageeTeam) - return; // thrower/damagee wasn't in game - if (throwersTeam != damageeTeam) - _gameState.teamScores[throwersTeam]++; - else - _gameState.teamScores[throwersTeam]--; - }; - - return { - start: function() { - _startGame(_gameState); - _gameState.listener = events.on("entity.EntityDamageByEntityEvent",_onSnowballHit); - new java.lang.Thread(function(){ - while (_gameState.duration--) - java.lang.Thread.sleep(1000); // sleep 1,000 millisecs (1 second) - _endGame(_gameState); - }).start(); - } - }; - }; - SnowBallFight = _constructor; - -}()); - - diff --git a/src/main/javascript/signs/menu.js b/src/main/javascript/signs/menu.js deleted file mode 100644 index 03729b9..0000000 --- a/src/main/javascript/signs/menu.js +++ /dev/null @@ -1,218 +0,0 @@ -/* - Define the signs module - signs are persistent - (that is - a menu sign will still be a menu after th - server has shut down and started up) plugins now have persistent state - Yay! -*/ -var signs = signs || plugin("signs", { - /* - construct an interactive menu which can then be attached to a Sign. - */ - menu: function( - /* String */ label, - /* Array */ options, - /* Function */ onInteract, - /* Number */ defaultSelection ){} - /* - more to come - clocks - */ -},true); -/* - private implementation -*/ -(function(){ - /* - redraw a menu sign - */ - var _redrawMenuSign = function(p_sign,p_selectedIndex,p_displayOptions) - { - var optLen = p_displayOptions.length; - // the offset is where the menu window begins - var offset = Math.max(0, Math.min(optLen-3, Math.floor(p_selectedIndex/3) * 3)); - for (var i = 0;i < 3; i++){ - var text = ""; - if (offset+i < optLen) - text = p_displayOptions[offset+i]; - if (offset+i == p_selectedIndex) - text = ("" + text).replace(/^ /,">"); - p_sign.setLine(i+1,text); - } - p_sign.update(true); - }; - signs._updaters = {}; - - /* - construct an interactive menu to be subsequently attached to - one or more Signs. - */ - signs.menu = function( - /* String */ label, - /* Array */ options, - /* Function */ callback, - /* Number */ selectedIndex) - { - - if (typeof selectedIndex == "undefined") - selectedIndex = 0; - - // - // variables common to all instances of this menu can go here - // - var labelPadding = "---------------"; - var optionPadding = " "; - - var paddedLabel = (labelPadding+label+labelPadding).substr(((label.length+30)/2)-7,15); - var optLen = options.length; - var displayOptions = []; - for (var i =0;i < options.length;i++){ - displayOptions[i] = (" " + options[i] + optionPadding).substring(0,15); - } - - var theSigns = this; - - /* - this function is returned by signs.menu and when it is invoked it will - attach menu behaviour to an existing sign in the world. - signs.menu is for use by Plugin Authors. - The function returned by signs.menu is for use by admins/ops. - */ - var convertToMenuSign = function(/* Sign */ sign, save) - { - if (typeof save == "undefined") - save = true; - - if (typeof sign == "undefined"){ - var mouseLoc = getMousePos(); - if (mouseLoc){ - sign = mouseLoc.block.state; - }else{ - throw new Exception("You must provide a sign!"); - } - } - // - // per-sign variables go here - // - var cSelectedIndex = selectedIndex; - sign.setLine(0,paddedLabel.bold()); - var _updateSign = function(p_player,p_sign) { - cSelectedIndex = (cSelectedIndex+1) % optLen; - _redrawMenuSign(p_sign,cSelectedIndex,displayOptions); - var signSelectionEvent = {player: p_player, - sign: p_sign, - text: options[cSelectedIndex], - number:cSelectedIndex}; - - callback(signSelectionEvent); - }; - - /* - get a unique ID for this particular sign instance - */ - var signLoc = sign.block.location; - var menuSignSaveData = [""+signLoc.world.name, signLoc.x,signLoc.y,signLoc.z]; - var menuSignUID = JSON.stringify(menuSignSaveData); - /* - keep a reference to the update function for use by the event handler - */ - theSigns._updaters[menuSignUID] = _updateSign; - - // initialize the sign - _redrawMenuSign(sign,cSelectedIndex,displayOptions); - - /* - whenever a sign is placed somewhere in the world - (which is what this function does) - save its location for loading and initialization - when the server starts up again. - */ - if (save){ - if (typeof theSigns.store.menus == "undefined") - theSigns.store.menus = {}; - var signLocations = theSigns.store.menus[label]; - if (typeof signLocations == "undefined") - signLocations = theSigns.store.menus[label] = []; - signLocations.push(menuSignSaveData); - } - return sign; - }; - - /* - a new sign definition - need to store (in-memory only) - it's behaviour and bring back to life other signs of the - same type in the world. Look for other static signs in the - world with this same label and make dynamic again. - */ - - if (this.store.menus && this.store.menus[label]) - { - var signsOfSameLabel = this.store.menus[label]; - var defragged = []; - var len = signsOfSameLabel.length; - for (var i = 0; i < len ; i++) - { - var loc = signsOfSameLabel[i]; - var world = org.bukkit.Bukkit.getWorld(loc[0]); - if (!world) - continue; - var block = world.getBlockAt(loc[1],loc[2],loc[3]); - if (block.state instanceof org.bukkit.block.Sign){ - convertToMenuSign(block.state,false); - defragged.push(loc); - } - } - /* - remove data for signs which no longer exist. - */ - if (defragged.length != len){ - this.store.menus[label] = defragged; - } - } - return convertToMenuSign; - }; - - /* - All dependecies ( 'events' module ) have loaded - */ - ready(function(){ - // - // Usage: - // In game, create a sign , target it and type /js signs.testMenu() - // - signs.testMenu = signs.menu( - "Dinner", - ["Lamb","Pork","Chicken","Duck","Beef"], - function(event){ - event.player.sendMessage("You chose " + event.text); - }); - // - // This is an example sign that displays a menu of times of day - // interacting with the sign will change the time of day accordingly. - // - // In game, create a sign , target it and type /js signs.timeOfDay() - // - signs.timeOfDay = signs.menu( - "Time", - ["Dawn","Midday","Dusk","Midnight"], - function(event){ - event.player.location.world.setTime( event.number * 6000 ); - }); - - // - // update it every time player interacts with it. - // - events.on("player.PlayerInteractEvent",function(listener, event) { - /* - look up our list of menu signs. If there's a matching location and there's - a sign, then update it. - */ - - if (! event.clickedBlock.state instanceof org.bukkit.block.Sign) - return; - var evtLocStr = utils.locationToString(event.clickedBlock.location); - var signUpdater = signs._updaters[evtLocStr] - if (signUpdater) - signUpdater(event.player, event.clickedBlock.state); - }); - }); -}()); - - diff --git a/src/main/javascript/utils/text.js b/src/main/javascript/utils/text.js deleted file mode 100644 index 32b66c8..0000000 --- a/src/main/javascript/utils/text.js +++ /dev/null @@ -1,72 +0,0 @@ -/************************************************************************ -String class extensions ------------------------ -The following chat-formatting methods are added to the javascript String class.. - - * black() - * darkblue() - * blue() - * darkgreen() - * darkaqua() - * darkred() - * purple() - * gold() - * gray() - * darkgray() - * indigo() - * brightgreen() - * green() - * aqua() - * red() - * pink() - * yellow() - * white() - * bold() - * random() - * strike() - * underline() - * italic() - * reset() - -Example -------- - - var boldGoldText = "Hello World".bold().gold(); - self.sendMessage(boldGoldText); - -

Hello World

- -***/ -(function(){ - var formattingCodes = { - black: 0, - darkblue: 1, - blue: 1, - darkgreen: 2, - darkaqua: 3, - darkred: 4, - purple: 5, - gold: 6, - gray: 7, - darkgray: 8, - indigo: 9, - brightgreen: 'a', - green: 'a', - aqua: 'b', - red: 'c', - pink: 'd', - yellow: 'e', - white: 'f', - bold: 'l', - random:'k', - strike: 'm', - underline: 'n', - italic: 'o', - reset: 'r' - }; - for (var method in formattingCodes){ - String.prototype[method] = function(m,c){ - return function(){ return "§"+c + this;}; - }(method,formattingCodes[method]); - } -}()); diff --git a/src/main/javascript/utils/utils.js b/src/main/javascript/utils/utils.js deleted file mode 100644 index f2165fe..0000000 --- a/src/main/javascript/utils/utils.js +++ /dev/null @@ -1,209 +0,0 @@ -/************************************************************************ -Utilities Module -================ -Miscellaneous utility functions and classes to help with programming. - - * locationToString(Location) - returns a bukkit Location object in string form. - - * getPlayerObject(playerName) - returns the Player object for a named - player or `self` if no name is provided. - -***/ -var utils = utils || { - locationToString: function(location){ - return JSON.stringify([""+location.world.name,location.x, location.y, location.z]); - }, - - getPlayerObject: function(playerName){ - if (typeof playerName == "undefined") - return self; - if (typeof playerName == "string") - return org.bukkit.Bukkit.getPlayer(playerName); - return player; - }, -/************************************************************************ -utils.foreach() function -======================== -The utils.foreach() function is a utility function for iterating over -an array of objects (or a java.util.Collection of objects) and processing each object in turn. Where -utils.foreach() differs from other similar functions found in -javascript libraries, is that utils.foreach can process the array -immediately or can process it *nicely* by processing one item at a -time then delaying processing of the next item for a given number of -server ticks (there are 20 ticks per second on the minecraft main -thread). This method relies on Bukkit's [org.bukkit.scheduler][sched] -package for scheduling processing of arrays. - -[sched]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/package-summary.html - -Parameters ----------- - - * array : The array to be processed - It can be a javascript array, a java array or java.util.Collection - * callback : The function to be called to process each item in the - array. The callback function should have the following signature - `callback(item, index, object, array)`. That is the callback will - be called with the following parameters.... - - - item : The item in the array - - index : The index at which the item can be found in the array. - - object : Additional (optional) information passed into the foreach method. - - array : The entire array. - - * object (optional) : An object which may be used by the callback. - * delay (optional, numeric) : If a delay is specified (in ticks - 20 - ticks = 1 second), then the processing will be scheduled so that - each item will be processed in turn with a delay between the completion of each - item and the start of the next. This is recommended for big builds (say 200 x 200 x 200 - blocks) or any CPU-intensive process. - * onDone (optional, function) : A function to be executed when all processing - is complete. This parameter is only used when the processing is delayed. (It's optional even if a - delay parameter is supplied). - -If called with a delay parameter then foreach() will return -immediately after processing just the first item in the array (all -subsequent items are processed later). If your code relies on the -completion of the array processing, then provide an `onDone` parameter -and put the code there. - -Example -------- -The following example illustrates how to use foreach for immediate processing of an array... - - var players = ["moe", "larry", "curly"]; - utils.foreach (players, function(item){ - server.getPlayer(item).sendMessage("Hi " + item); - }); - -... The `utils.foreach()` function can work with Arrays or any Java-style collection. This is important -because many objects in the Bukkit API use Java-style collections... - - utils.foreach( server.onlinePlayers, function(player){ - player.chat("Hello!"); - }); - -... the above code sends a "Hello!" to every online player. - -The following example is a more complex use case - The need to build an enormous structure -without hogging CPU usage... - - // build a structure 200 wide x 200 tall x 200 long - // (That's 8 Million Blocks - enough to tax any machine!) - - var a = []; - a.length = 200; - var drone = new Drone(); - var processItem = function(item, index, object, array){ - // build a box 200 wide by 200 long then move up - drone.box(blocks.wood, 200, 1, 200).up(); - }; - // by the time the job's done 'self' might be someone else - // assume this code is within a function/closure - var player = self; - var onDone = function(){ - player.sendMessage("Job Done!"); - }; - utils.foreach (a, processItem, null, 10, onDone); - -***/ - foreach: function(array, callback, object, delay, onCompletion) { - if (array instanceof java.util.Collection) - array = array.toArray(); - var i = 0; - var len = array.length; - if (delay){ - var next = function(){ callback(array[i],i,object,array); i++;}; - var hasNext = function(){return i < len;}; - utils.nicely(next,hasNext,onCompletion,delay); - }else{ - for (;i < len; i++){ - callback(array[i],i,object,array); - } - } - }, -/************************************************************************ -utils.nicely() function -======================= -The utils.nicely() function is for performing processing using the -[org.bukkit.scheduler][sched] package/API. utils.nicely() lets you -process with a specified delay between the completion of each `next()` -function and the start of the next `next()` function. -`utils.nicely()` is a recursive function - that is - it calls itself -(schedules itself actually) repeatedly until `hasNext` returns false. - -Parameters ----------- - - * next : A function which will be called if processing is to be done. - * hasNext : A function which is called to determine if the `next` - callback should be invoked. This should return a boolean value - - true if the `next` function should be called (processing is not - complete), false otherwise. - * onDone : A function which is to be called when all processing is complete (hasNext returned false). - * delay : The delay (in server ticks - 20 per second) between each call. - -Example -------- -See the source code to utils.foreach for an example of how utils.nicely is used. - -***/ - nicely: function(next, hasNext, onDone, delay){ - if (hasNext()){ - next(); - server.scheduler.runTaskLater(__plugin,function(){ - utils.nicely(next,hasNext,onDone,delay); - },delay); - }else{ - if (onDone) - onDone(); - } - }, -/************************************************************************ -utils.at() function -=================== -The utils.at() function will perform a given task at a given time every -(minecraft) day. - -Parameters ----------- - - * time24hr : The time in 24hr form - e.g. 9:30 in the morning is "09:30" while - 9:30 pm is "21:30", midnight is "00:00" and midday is "12:00" - * callback : A javascript function which will be invoked at the given time. - * world : (optional) Each world has its own clock. If no world is specified, the server's first world is used. - -Example -------- - -To warn players when night is approaching... - - utils.at( "19:00", function() { - - utils.foreach( server.onlinePlayers, function(player){ - player.chat("The night is dark and full of terrors!"); - }); - - }, self.world); - -***/ - at: function(time24hr, callback, world){ - var forever = function(){ return true;}; - var timeParts = time24hr.split(":"); - var hrs = ((timeParts[0] * 1000) + 18000) % 24000; - var mins; - if (timeParts.length > 1) - mins = (timeParts[1] / 60) * 1000; - - var timeMc = hrs + mins; - if (typeof world == "undefined"){ - world = server.worlds.get(0); - } - utils.nicely(function(){ - var time = world.getTime(); - var diff = timeMc - time; - if (diff > 0 && diff < 100){ - callback(); - } - },forever, null, 100); - }, -}; diff --git a/src/main/js/lib/.gitignore b/src/main/js/lib/.gitignore new file mode 100644 index 0000000..455bdfa --- /dev/null +++ b/src/main/js/lib/.gitignore @@ -0,0 +1,2 @@ +/events-helper-bukkit.js +/events-helper-canary.js diff --git a/src/main/js/lib/command.js b/src/main/js/lib/command.js new file mode 100644 index 0000000..2ba6a4d --- /dev/null +++ b/src/main/js/lib/command.js @@ -0,0 +1,64 @@ +'use strict'; +/* + command management - allow for non-ops to execute approved javascript code. +*/ +var _commands = {}, + _cmdInterceptors = []; +/* + execute a JSP command. +*/ +var executeCmd = function( args, player ) { + var name, + cmd, + intercepted, + result = null; + + if ( args.length === 0 ) { + throw new Error('Usage: jsp command-name command-parameters'); + } + name = args[0]; + cmd = _commands[name]; + if ( typeof cmd === 'undefined' ) { + // it's not a global command - pass it on to interceptors + intercepted = false; + for ( var i = 0; i < _cmdInterceptors.length; i++ ) { + if ( _cmdInterceptors[i]( args, player ) ) + intercepted = true; + } + if ( !intercepted ) { + console.warn( 'Command %s is not recognised', name ); + } + }else{ + try { + result = cmd.callback( args.slice(1), player ); + } catch ( e ) { + console.error( 'Error while trying to execute command: ' + JSON.stringify( args ) ); + throw e; + } + } + return result; +}; +/* + define a new JSP command. +*/ +var defineCmd = function( name, func, options, intercepts ) { + + if ( typeof name == 'function'){ + intercepts = options; + options = func; + func = name; + name = func.name; + } + + if ( typeof options == 'undefined' ) { + options = []; + } + _commands[name] = { callback: func, options: options }; + if ( intercepts ) { + _cmdInterceptors.push(func); + } + return func; +}; +exports.command = defineCmd; +exports.commands = _commands; +exports.exec = executeCmd; diff --git a/src/main/js/lib/console.js b/src/main/js/lib/console.js new file mode 100644 index 0000000..854aa34 --- /dev/null +++ b/src/main/js/lib/console.js @@ -0,0 +1,79 @@ +'use strict'; +/************************************************************************* +## console global variable + +ScriptCraft provides a `console` global variable with the followng methods... + + * log() + * info() + * warn() + * error() + +The ScriptCraft console methods work like the [Web API implementation][webcons]. + +### Example + + console.log('Hello %s', 'world'); + +Basic variable substitution is supported (ScriptCraft's implementation +of console uses the Bukkit Plugin [Logger][lgr] or Canary Plugin [Logman][cmlgr] under the hood and +uses [java.lang.String.format()][strfmt] for variable +substitution. All output will be sent to the server console (not +in-game). + +### Using string substitutions + +ScriptCraft uses Java's [String.format()][strfmt] so any string substitution identifiers supported by +`java.lang.String.format()` are supported (e.g. %s , %d etc). + + for (var i=0; i<5; i++) { + console.log("Hello, %s. You've called me %d times.", "Bob", i+1); + } + +[lgr]: http://jd.bukkit.org/beta/apidocs/org/bukkit/plugin/PluginLogger.html +[cmlgr]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/logger/Logman.html +[strfmt]: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#format(java.lang.String, java.lang.Object...) +[webcons]: https://developer.mozilla.org/en-US/docs/Web/API/console + +***/ +function argsToArray( args ) { + var result = []; + for ( var i =0; i < args.length; i++ ) { + result.push(args[i]); + } + return result; +} +function consMsg(params){ + var args = argsToArray(params); + if ( args.length > 1 ) { + return java.lang.String.format( args[0], args.slice(1) ); + } else { + return args[0]; + } +} + +module.exports = function(logger){ + + function bukkitLog( level, restOfArgs ) { + logger['log(java.util.logging.Level,java.lang.String)']( + java.util.logging.Level[level], + consMsg(restOfArgs) + ); + } + + if (__plugin.canary){ + return { + log: function( ) { logger.info( consMsg(arguments) ); }, + info: function( ) { logger.info( consMsg(arguments) ); }, + warn: function( ) { logger.warn( consMsg(arguments) ); }, + error: function( ) { logger.error( consMsg(arguments) ); } + }; + } else { + return { + log: function() { bukkitLog('INFO', arguments ); }, + info: function() { bukkitLog('INFO', arguments ); }, + warn: function( ) { bukkitLog('WARNING', arguments ); }, + error: function( ) { bukkitLog('SEVERE', arguments ); } + }; + } +}; diff --git a/src/main/js/lib/events-bukkit.js b/src/main/js/lib/events-bukkit.js new file mode 100644 index 0000000..875d91c --- /dev/null +++ b/src/main/js/lib/events-bukkit.js @@ -0,0 +1,78 @@ +/*global Java, exports, org, __plugin */ +var bkEventPriority = org.bukkit.event.EventPriority, + bkEventExecutor = org.bukkit.plugin.EventExecutor, + bkRegisteredListener = org.bukkit.plugin.RegisteredListener, + bkEventPackage = 'org.bukkit.event.'; + +var nashorn = (typeof Java != 'undefined'); + +function getHandlerListForEventType( eventType ){ + var result = null; + var clazz = null; + if (nashorn) { + + //Nashorn doesn't like when getHandlerList is in a superclass of your event + //so to avoid this problem, call getHandlerList using java.lang.reflect + //methods + clazz = eventType['class']; + result = clazz.getMethod("getHandlerList").invoke(null); + + } else { + result = eventType.getHandlerList(); + } + + return result; +} +exports.on = function( + /* Java Class */ + eventType, + /* function( registeredListener, event) */ + handler, + /* (optional) String (HIGH, HIGHEST, LOW, LOWEST, NORMAL, MONITOR), */ + priority ) { + var handlerList, + regd, + eventExecutor; + + if ( typeof priority == 'undefined' ) { + priority = bkEventPriority.HIGHEST; + } else { + priority = bkEventPriority[priority.toUpperCase().trim()]; + } + handlerList = getHandlerListForEventType (eventType); + + var result = { }; + eventExecutor = new bkEventExecutor( { + execute: function( l, evt ) { + function cancel(){ + if (evt instanceof org.bukkit.event.Cancellable){ + evt.setCancelled(true); + } + } + /* + let handlers use this.cancel() to cancel the current event + or this.unregister() to unregister from future events. + */ + var bound = {}; + for (var i in result){ + bound[i] = result[i]; + } + bound.cancel = cancel; + handler.call( bound, evt, cancel ); + } + } ); + /* + wph 20130222 issue #64 bad interaction with Essentials plugin + if another plugin tries to unregister a Listener (not a Plugin or a RegisteredListener) + then BOOM! the other plugin will throw an error because Rhino can't coerce an + equals() method from an Interface. + The workaround is to make the ScriptCraftPlugin java class a Listener. + Should only unregister() registered plugins in ScriptCraft js code. + */ + regd = new bkRegisteredListener( __plugin, eventExecutor, priority, __plugin, false ); + handlerList.register( regd ); + result.unregister = function(){ + handlerList.unregister( regd ); + }; + return result; +}; diff --git a/src/main/js/lib/events-canary.js b/src/main/js/lib/events-canary.js new file mode 100644 index 0000000..59e983b --- /dev/null +++ b/src/main/js/lib/events-canary.js @@ -0,0 +1,68 @@ +/*global nashorn, exports, require, Packages, __plugin*/ +var cmPriority = Packages.net.canarymod.plugin.Priority, + cmCanary = Packages.net.canarymod.Canary, + cmDispatcher = Packages.net.canarymod.hook.Dispatcher, + cmRegisteredPluginListener = Packages.net.canarymod.plugin.RegisteredPluginListener, + cmPluginListener = Packages.net.canarymod.plugin.PluginListener; +var cmHookExecutor = cmCanary.hooks(); + +exports.on = function( + /* Java Class */ + eventType, + /* function( registeredListener, event) */ + handler, + /* (optional) String (CRITICAL, HIGH, NORMAL, LOW, PASSIVE), */ + priority ) { + var handlerList, + regd, + eventExecutor; + + if ( typeof priority == 'undefined' ) { + priority = cmPriority.NORMAL; + } else { + priority = cmPriority[priority.toUpperCase().trim()]; + } + + var result = { }; + eventExecutor = __plugin.getDispatcher( function(l,e){ + function cancel(){ + if (e.setCanceled){ + e.setCanceled(); + } + } + /* + let handlers use this.cancel() to cancel the current event + or this.unregister() to unregister from future events. + */ + var bound = {}; + for (var i in result){ + bound[i] = result[i]; + } + bound.cancel = cancel; + try { + handler.call(bound, e, cancel); + } catch ( error ){ + console.log('Error while executing handler:' + handler + + ' for event type:' + eventType + + ' error: ' + error); + } + }); + /* + wph 20130222 issue #64 bad interaction with Essentials plugin + if another plugin tries to unregister a Listener (not a Plugin or a RegisteredListener) + then BOOM! the other plugin will throw an error because Rhino can't coerce an + equals() method from an Interface. + The workaround is to make the ScriptCraftPlugin java class a Listener. + Should only unregister() registered plugins in ScriptCraft js code. + */ + if (nashorn){ + // nashorn + eventType = require('nashorn-type')(eventType); + } + regd = new cmPluginListener({}); + cmHookExecutor.registerHook(regd, __plugin, eventType, eventExecutor, priority); + result.unregister = function(){ + cmHookExecutor.unregisterPluginListener(regd); + }; + return result; +}; diff --git a/src/main/js/lib/events.js b/src/main/js/lib/events.js new file mode 100644 index 0000000..f66fd11 --- /dev/null +++ b/src/main/js/lib/events.js @@ -0,0 +1,112 @@ +'use strict'; +/************************************************************************ +## events Module + +The Events module provides a thin wrapper around CanaryMod's or +Bukkit's Event-handling API. The Java-based CanaryMod and Bukkit +Events APIs make use of Java Annotations which are not available in +Javascript, so this module provides a simple way to listen to +minecraft events in javascript. + +### events.on() static method + +This method is used to register event listeners. This method is called by all of the Event Helper methods. +The `events` object has functions for registering listeners for each type of event. For example, you can register a block-break listener using events.on: + +```javascript +events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt, cancel ) { + echo(evt.player, evt.player.name + ' broke a block!'); +} ); +``` + +or you can (and probably should) use the more succinct: + +```javascript +events.blockDestroy( function( evt, cancel ) { + echo(evt.player, evt.player.name + ' broke a block!'); +} ); +``` + +The events.on method can be used to register standard CanaryMod/Bukkit +events and can also be used to register non-standard events - that is +- events provided by plugins. + +#### Parameters + + * eventType - A Java class. See the [CanaryMod Hook API][cmEvtApi] or [Bukkit Event API][buk] for details of the many event types. + + * callback - A function which will be called whenever the event + fires. The callback in turn takes 2 parameters: + + - event : the event fired + - cancel : a function which if invoked will cancel the event - not all event types are cancelable; this function only cancels cancelable events). + + * priority (optional - default: "CRITICAL" for CanaryMod or "HIGHEST" for Bukkit) - + The priority the listener/callback takes over other listeners to the same event. + Possible values for CanaryMod are "CRITICAL", "HIGH", "LOW", "NORMAL" and "PASSIVE". + For an explanation of what the different CanaryMod Hook priorities + mean, refer to CanaryMod's [Hook Priority class][cmPriority]. + Possible values for Bukkit are "HIGH", "HIGHEST", "LOW", "LOWEST", "NORMAL", "MONITOR". + For an explanation of what the different Bukkit Event priorities + mean, refer to bukkit's [Event API Reference][buk2]. + +#### Returns + +An object which can be used to unregister the listener. + +#### Example: + +The following code will print a message on screen every time a block is broken in the game + +```javascript +events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt, cancel ) { + echo(evt.player, evt.player.name + ' broke a block!'); +} ); +``` + +To handle an event only once and unregister from further events... + +```javascript +events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt, cancel ) { + echo( evt.player, evt.player.name + ' broke a block!'); + this.unregister(); +} ); +``` + +The `this` keyword when used inside the callback function refers to +the Listener object created by ScriptCraft. It has 2 methods +`unregister()` which can be used to stop listening and `cancel()` +which can be used to cancel the current event. The object returned by +`events.on()` only has the `unregister()` method, the `cancel()` +method is only available from within the event handling function. + +To unregister a listener *outside* of the listener function... + +```javascript +var myBlockBreakListener = events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt ) { ... } ); +... +myBlockBreakListener.unregister(); +``` + +[buk2]: http://wiki.bukkit.org/Event_API_Reference +[buk]: http://jd.bukkit.org/dev/apidocs/index.html?org/bukkit/event/Event.html +[cmEvtApi]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/hook/Hook.html +[cmPriority]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/plugin/Priority.html + +***/ +var helper; +/*global __plugin, module, require*/ +if (__plugin.canary){ + module.exports = require('events-canary'); + helper = require('events-helper-canary'); + // backwards-compatibility with canarymod 1.7.9 for book listings + if (helper.connection && !helper.connect){ + helper.connect = helper.connection; + } +} else { + module.exports = require('events-bukkit'); + helper = require('events-helper-bukkit'); +} +for ( var func in helper ) { + module.exports[func] = helper[func]; +}; diff --git a/src/main/js/lib/find.js b/src/main/js/lib/find.js new file mode 100644 index 0000000..2f686ff --- /dev/null +++ b/src/main/js/lib/find.js @@ -0,0 +1,29 @@ +'use strict'; +var File = java.io.File; +module.exports = function find(dir, filter) { + var result = []; + function recurse( dir, store ) { + var files, + len, + i, + file, + dirfile = new File( dir ); + + if ( typeof filter == 'undefined' ) { + files = dirfile.list(); + } else { + files = dirfile.list(filter); + } + len = files.length; i = 0; + for (; i < len; i++){ + file = new File( dir + '/' + files[i] ); + if ( file.isDirectory() ) { + recurse( file.canonicalPath, store ); + } else { + store.push( ('' + file.canonicalPath).replace(/\\\\/g,'/') ); + } + } + } + recurse( dir, result ); + return result; +}; diff --git a/src/main/js/lib/java-utils.js b/src/main/js/lib/java-utils.js new file mode 100644 index 0000000..aaa065e --- /dev/null +++ b/src/main/js/lib/java-utils.js @@ -0,0 +1,29 @@ +exports.isJavaObject = function( o ) { + if (o === global){ + return false; + } + if (o !== undefined && o !== null){ + try { + // this throws error for java objects in jre7 + if (typeof o.constructor === 'function'){ + return false; + } + } catch (e){ + return true; + } + try { + var result = o.getClass ? true : false; // throws error for Enums/Class in jre7 + if (result == true){ + return result; + } + }catch (e2){ + // fail silently and move on to next test + } + // java classes don't have a getClass so just because .getClass isn't present + // doesn't mean it's not a Java Enum or Class (.getClass only works for object instances?) + if (o instanceof java.lang.Object){ + return true; + } + } + return o instanceof java.lang.Object; +}; diff --git a/src/main/js/lib/js-patch.js b/src/main/js/lib/js-patch.js new file mode 100644 index 0000000..20cd7fa --- /dev/null +++ b/src/main/js/lib/js-patch.js @@ -0,0 +1,77 @@ +module.exports = function( $ ) { + + // wph 20140105 trim not availabe in String on Mac OS. + if ( typeof String.prototype.trim == 'undefined' ) { + String.prototype.trim = function( ) { + return this.replace( /^\s+|\s+$/g, '' ); + }; + } + + // wph 20140316 Java 1.6.0_65 on mac does not have Function.prototype.bind + // code from http://webreflection.blogspot.ie/2010/02/functionprototypebind.html + if (typeof Function.prototype.bind == 'undefined' ) { + Function.prototype.bind = (function (slice){ + // (C) WebReflection - Mit Style License + function bind(context) { + var self = this; // "trapped" function reference + // only if there is more than an argument + // we are interested into more complex operations + // this will speed up common bind creation + // avoiding useless slices over arguments + if (1 < arguments.length) { + // extra arguments to send by default + var $arguments = slice.call(arguments, 1); + return function () { + return self.apply( + context, + // thanks @kangax for this suggestion + arguments.length ? + // concat arguments with those received + $arguments.concat(slice.call(arguments)) : + // send just arguments, no concat, no slice + $arguments + ); + }; + } + // optimized callback + return function () { + // speed up when function is called without arguments + return arguments.length ? self.apply(context, arguments) : self.call(context); + }; + } + // the named function + return bind; + }(Array.prototype.slice)); + } + + if (__plugin.canary){ + require('task-canary')($); + } else { + require('task-bukkit')($); + } + + return function unitTest( console ) { + /* + sanity tests + */ + $.setTimeout(function(){ + console.log('js-patch setTimeout() test complete'); + },100); + var clearMe = $.setTimeout(function(){ + console.error('js-patch clearTimeout() test failed'); + },100); + $.clearTimeout( clearMe ); + + var runs = 3; + var clearAfterRuns = $.setInterval(function(){ + runs --; + if (runs == 0){ + $.clearInterval(clearAfterRuns); + } + if (runs < 0){ + console.error('js-patch clearInterval test failed.'); + } + },100); + }; +}; + diff --git a/src/main/javascript/ext/json2.js b/src/main/js/lib/json2.js similarity index 99% rename from src/main/javascript/ext/json2.js rename to src/main/js/lib/json2.js index c7745df..d89ecc7 100644 --- a/src/main/javascript/ext/json2.js +++ b/src/main/js/lib/json2.js @@ -1,6 +1,6 @@ /* json2.js - 2012-10-08 + 2013-05-26 Public Domain. @@ -173,7 +173,7 @@ if (typeof JSON !== 'object') { if (typeof Date.prototype.toJSON !== 'function') { - Date.prototype.toJSON = function (key) { + Date.prototype.toJSON = function () { return isFinite(this.valueOf()) ? this.getUTCFullYear() + '-' + @@ -187,7 +187,7 @@ if (typeof JSON !== 'object') { String.prototype.toJSON = Number.prototype.toJSON = - Boolean.prototype.toJSON = function (key) { + Boolean.prototype.toJSON = function () { return this.valueOf(); }; } diff --git a/src/main/js/lib/legacy-check.js b/src/main/js/lib/legacy-check.js new file mode 100644 index 0000000..21bf89c --- /dev/null +++ b/src/main/js/lib/legacy-check.js @@ -0,0 +1,32 @@ +var File = java.io.File; +/* + wph 20140102 - warn if legacy 'mcserver/js-plugins' or + 'mcserver/plugins/scriptcraft' directories are present + */ +module.exports = function( jsPluginsRootDir ) { + var mcServerDir = new File(jsPluginsRootDir.canonicalPath).parentFile; + if (mcServerDir == null){ + console.warn('Could not find parent directory for ' + jsPluginsRootDir.canonicalPath); + return; + } + var legacyExists = false, + legacyDirs = [ + new File( mcServerDir, 'js-plugins' ) + ]; + + for ( var i = 0; i < legacyDirs.length; i++ ) { + if ( legacyDirs[i].exists() + && legacyDirs[i].isDirectory() ) { + + legacyExists = true; + + console.warn('Legacy ScriptCraft directory %s was found. This directory is no longer used.', + legacyDirs[i].canonicalPath); + console.warn('Please put plugins in the ' + jsPluginsRootDir.canonicalPath + '/plugins directory'); + } + } + if ( legacyExists ) { + console.info( 'The working directory for %s is %s', + __plugin, jsPluginsRootDir.canonicalPath ); + } +}; diff --git a/src/main/js/lib/nashorn-type.js b/src/main/js/lib/nashorn-type.js new file mode 100644 index 0000000..ad3f731 --- /dev/null +++ b/src/main/js/lib/nashorn-type.js @@ -0,0 +1,8 @@ +/* + The .class operator causes problems for non-nashorn Java on Mac OS X and some other + environments. So need to have it in a separate module which should only be loaded in + nashorn environment. +*/ +module.exports = function(t){ + return t.class; +}; diff --git a/src/main/js/lib/persistence.js b/src/main/js/lib/persistence.js new file mode 100644 index 0000000..36eabb4 --- /dev/null +++ b/src/main/js/lib/persistence.js @@ -0,0 +1,52 @@ +var _dataDir = null, + _persistentData = {}; + +module.exports = function( rootDir, $ ) { + + var _load = function( name ) { + var result = $.scloadJSON( _dataDir.canonicalPath + '/' + name + '-store.json' ); + return result; + }; + + var _save = function( name, objToSave ) { + $.scsave( objToSave, _dataDir.canonicalPath + '/' + name + '-store.json' ); + }; + + _dataDir = new java.io.File( rootDir, 'data' ); + + $.persist = function( name, data, write ) { + var i, + dataFromFile; + if ( typeof data == 'undefined' ) { + data = {}; + } + if ( typeof write == 'undefined' ) { + write = false; + } + if ( !write ) { + dataFromFile = _load( name ); + if ( typeof dataFromFile != 'undefined') { + for ( i in dataFromFile ) { + data[i] = dataFromFile[i]; + } + } + } else { + // flush data to file + _save( name, data ); + } + _persistentData[name] = data; + return data; + }; + /* + persist on shutdown + */ + $.addUnloadHandler( function( ) { + var name, + data; + for ( name in _persistentData ) { + data = _persistentData[name]; + _save( name, data ); + } + }); +}; + diff --git a/src/main/js/lib/plugin.js b/src/main/js/lib/plugin.js new file mode 100644 index 0000000..ac64be9 --- /dev/null +++ b/src/main/js/lib/plugin.js @@ -0,0 +1,72 @@ +'use strict'; +/*global persist,exports,config,__plugin,require*/ +var File = java.io.File, + FileWriter = java.io.FileWriter, + PrintWriter = java.io.PrintWriter, + find = require('./find'); +/* + plugin management +*/ +var _plugins = {}; + +function _plugin(/* String */ moduleName, /* Object */ moduleObject, isPersistent ) { + // + // don't load plugin more than once + // + if ( typeof _plugins[moduleName] != 'undefined' ) { + return _plugins[moduleName].module; + } + + var pluginData = { persistent: isPersistent, module: moduleObject }; + if ( typeof moduleObject.store == 'undefined' ) { + moduleObject.store = {}; + } + _plugins[moduleName] = pluginData; + + if ( isPersistent ) { + moduleObject.store = persist( moduleName, moduleObject.store ); + } + return moduleObject; +} + +function _autoload( context, pluginDir, options ) { + /* + Reload all of the .js files in the given directory + */ + var sourceFiles = [], + property, + module, + pluginPath; + sourceFiles = find(pluginDir); + + var len = sourceFiles.length; + if ( config && config.verbose ) { + console.info( len + ' scriptcraft plugins found in ' + pluginDir ); + } + + for ( var i = 0; i < len; i++ ) { + + pluginPath = sourceFiles[i]; + if (!pluginPath.match(/\.js$/)){ + continue; + } + module = {}; + + try { + module = require( pluginPath , options); + for ( property in module ) { + /* + all exports in plugins become members of context object + */ + context[property] = module[property]; + } + } catch ( e ) { + var msg = 'Plugin ' + pluginPath + ' ' + e ; + console.error( msg ); + } + } + +} +exports.plugin = _plugin; +exports.autoload = _autoload; + diff --git a/src/main/js/lib/readme.md b/src/main/js/lib/readme.md new file mode 100644 index 0000000..972a966 --- /dev/null +++ b/src/main/js/lib/readme.md @@ -0,0 +1,12 @@ +# lib directory + +This directory contains core scriptcraft files and modules. + + * plugin.js - A module which provides support for persistent plugins (plugins which need to save state) + * require.js - The require() function implementation. See [Node.js modules] documentation. + * scriptcraft.js - The core scriptcraft code. + * events.js - Event handling module for use by plugin/module developers. + +When `require('modulename')` is called, if a file in the current working directory called 'modulename' is not found then the `lib` folder is the first location that `require()` looks for modules. + +[njsmod]: http://nodejs.org/api/modules.html diff --git a/src/main/js/lib/require.js b/src/main/js/lib/require.js new file mode 100644 index 0000000..c75fba2 --- /dev/null +++ b/src/main/js/lib/require.js @@ -0,0 +1,321 @@ +/************************************************************************* +## require - Node.js-style module loading in ScriptCraft + +Node.js is a server-side javascript environment with an excellent +module loading system based on CommonJS. Modules in Node.js are really +simple. Each module is in its own javascript file and all variables +and functions within the file are private to that file/module only. +There is a very concise explanation of CommonJS modules at... + +[http://wiki.commonjs.org/wiki/Modules/1.1.1.][cjsmodules] + +Node.js also has good documentation on [Modules][njsmod]. + +If you want to export a variable or function you use the module.export +property. + +For example imagine you have 3 files program.js, inc.js and math.js ... + +### math.js + +```javascript +exports.add = function(a,b){ + return a + b; +} +``` + +### inc.js + +```javascript +var math = require('./math'); +exports.increment = function(n){ + return math.add(n, 1); +} +``` + +### program.js + +```javascript +var inc = require('./inc').increment; +var a = 7; +a = inc(a); +print(a); +``` + +You can see from the above sample code that programs can use modules +and modules themeselves can use other modules. Modules have full +control over what functions and properties they want to provide to +others. + +### Important + +Although ScriptCraft now supports Node.js style modules, it does not +support node modules. Node.js and Rhino are two very different +Javascript environments. ScriptCraft uses Rhino Javascript, not +Node.js. Standard Node.js modules such as `'fs'` are not available in ScriptCraft. + +Modules can be loaded using relative or absolute paths. Per the CommonJS +module specification, the '.js' suffix is optional. + +[cjsmodules]: http://wiki.commonjs.org/wiki/Modules/1.1.1. + +***/ +(function ( rootDir, modulePaths, hooks, evaluate ) { + + var File = java.io.File, + FileReader = java.io.FileReader, + BufferedReader = java.io.BufferedReader; + + function fileExists( file ) { + if ( file.isDirectory() ) { + return readModuleFromDirectory( file ); + } else { + return file; + } + } + + function _canonize(file){ + return "" + file.canonicalPath.replaceAll("\\\\","/"); + } + + function readModuleFromDirectory( dir ) { + + // look for a package.json file + var pkgJsonFile = new File( dir, './package.json' ); + if ( pkgJsonFile.exists() ) { + var pkg = scload( pkgJsonFile ); + var mainFile = new File( dir, pkg.main ); + if ( mainFile.exists() ) { + return mainFile; + } else { + return null; + } + } else { + // look for an index.js file + var indexJsFile = new File( dir, './index.js' ); + if ( indexJsFile.exists() ) { + return indexJsFile; + } else { + return null; + } + } + } + + +/********************************************************************** +### module name resolution + +When resolving module names to file paths, ScriptCraft uses the following rules... + + 1. if the module does not begin with './' or '/' then ... + + 1.1 Look in the 'scriptcraft/lib' directory. If it's not there then... + 1.2 Look in the 'scriptcraft/modules' directory. If it's not there then + Throw an Error. + + 2. If the module begins with './' or '/' then ... + + 2.1 if the module begins with './' then it's treated as a file path. File paths are + always relative to the module from which the require() call is being made. + + 2.2 If the module begins with '/' then it's treated as an absolute path. + + If the module does not have a '.js' suffix, and a file with the same name and a .js sufix exists, + then the file will be loaded. + + 3. If the module name resolves to a directory then... + + 3.1 look for a package.json file in the directory and load the `main` property e.g. + + // package.json located in './some-library/' + { + "main": './some-lib.js', + "name": 'some-library' + } + + 3.2 if no package.json file exists then look for an index.js file in the directory + +***/ + function resolveModuleToFile( moduleName, parentDir ) { + var file = new File(moduleName), + i = 0, + pathWithJSExt, + resolvedFile; + if ( file.exists() ) { + return fileExists(file); + } + if ( moduleName.match( /^[^\.\/]/ ) ) { + // it's a module named like so ... 'events' , 'net/http' + // + for ( ; i < modulePaths.length; i++ ) { + resolvedFile = new File(modulePaths[i] + moduleName); + if ( resolvedFile.exists() ) { + return fileExists(resolvedFile); + } else { + // try appending a .js to the end + resolvedFile = new File(modulePaths[i] + moduleName + '.js'); + if ( resolvedFile.exists() ) { + return resolvedFile; + } + } + } + } else { + if ((file = new File(parentDir, moduleName)).exists()) { + return fileExists(file); + } else if ((file = new File(parentDir, moduleName + ".js")).exists()) { // try .js extension + return file; + } else if ((file = new File(parentDir, moduleName + ".json")).exists()) { // try .json extension + return file; + } + } + return null; + } + /* + require() function implementation + */ + function _require( parentFile, path, options ) { + var file, + canonizedFilename, + moduleInfo, + buffered, + head = '(function(exports,module,require,__filename,__dirname){ ', + code = '', + line = null; + + if ( typeof options == 'undefined' ) { + options = { cache: true }; + } else { + if ( typeof options.cache == 'undefined' ) { + options.cache = true; + } + } + + file = resolveModuleToFile(path, parentFile); + if ( !file ) { + var errMsg = '' + _format("require() failed to find matching file for module '%s' " + + "in working directory '%s' ", [path, parentFile.canonicalPath]); + if (! ( (''+path).match( /^\./ ) ) ) { + errMsg = errMsg + ' and not found in paths ' + JSON.stringify(modulePaths); + } + var find = _require(parentFile, 'find').exports; + var allJS = []; + for (var i = 0;i < modulePaths.length; i++){ + var js = find( modulePaths[i] ); + for (var j = 0;j < js.length; j++){ + if (js[j].match(/\.js$/)){ + allJS.push( js[j].replace(modulePaths[i],'') ); + } + } + } + var pathL = path.toLowerCase(); + var candidates = []; + for (i = 0;i < allJS.length;i++){ + var filenameparts = allJS[i]; + var candidate = filenameparts.replace(/\.js/,'') ; + var lastpart = candidate.toLowerCase(); + if (pathL.indexOf(lastpart) > -1 || lastpart.indexOf(pathL) > -1){ + candidates.push(candidate); + } + } + if (candidates.length > 0){ + errMsg += '\nBut found module/s named: ' + candidates.join(',') + ' - is this what you meant?'; + } + throw new Error(errMsg); + } + canonizedFilename = _canonize(file); + + moduleInfo = _loadedModules[canonizedFilename]; + if ( moduleInfo ) { + if ( options.cache ) { + return moduleInfo; + } + } + if ( hooks ) { + hooks.loading( canonizedFilename ); + } + buffered = new BufferedReader(new FileReader(file)); + while ( (line = buffered.readLine()) !== null ) { + code += line + '\n'; + } + buffered.close(); // close the stream so there's no file locks + + if(canonizedFilename.toLowerCase().substring(canonizedFilename.length - 5) === ".json") // patch code when it is json + code = "module.exports = (" + code + ");"; + + moduleInfo = { + loaded: false, + id: canonizedFilename, + exports: {}, + require: _requireClosure(file.parentFile) + }; + var tail = '})'; + code = head + code + tail; + + if ( options.cache ) { + _loadedModules[canonizedFilename] = moduleInfo; + } + var compiledWrapper = null; + try { + compiledWrapper = evaluate(code); + } catch (e) { + /* + wph 20140313 JRE8 (nashorn) gives misleading linenumber of evaluating code not evaluated code. + This can be fixed by instead using __engine.eval + */ + throw new Error( "Error evaluating module " + path + + " line #" + e.lineNumber + + " : " + e.message, canonizedFilename, e.lineNumber ); + } + var __dirname = '' + file.parentFile.canonicalPath; + var parameters = [ + moduleInfo.exports, /* exports */ + moduleInfo, /* module */ + moduleInfo.require, /* require */ + canonizedFilename, /* __filename */ + __dirname /* __dirname */ + ]; + try { + compiledWrapper + .apply(moduleInfo.exports, /* this */ + parameters); + } catch (e) { + var snippet = ''; + if ((''+e.lineNumber).match(/[0-9]/)){ + var lines = code.split(/\n/); + if (e.lineNumber > 1){ + snippet = ' ' + lines[e.lineNumber-2] + '\n'; + } + snippet += '> ' + lines[e.lineNumber-1] + '\n'; + if (e.lineNumber < lines.length){ + snippet += ' ' + lines[e.lineNumber] + '\n'; + } + } + throw new Error( "Error executing module " + path + + " line #" + e.lineNumber + + " : " + e.message + (snippet?('\n' + snippet):''), canonizedFilename, e.lineNumber ); + } + if ( hooks ) { + hooks.loaded( canonizedFilename ); + } + moduleInfo.loaded = true; + return moduleInfo; + } + + function _requireClosure( parentFile ) { + var _boundRequire = function requireBoundToParent( path, options ) { + var module = _require( parentFile, path , options); + return module.exports; + }; + + _boundRequire.resolve = function resolveBoundToParent ( path ) { + return resolveModuleToFile(path, parentFile); + }; + _boundRequire.cache = _loadedModules; + + return _boundRequire; + } + var _loadedModules = {}; + var _format = java.lang.String.format; + return _requireClosure( new java.io.File(rootDir) ); + // last line deliberately has no semicolon! +}) diff --git a/src/main/js/lib/scriptcraft.js b/src/main/js/lib/scriptcraft.js new file mode 100644 index 0000000..369f7c0 --- /dev/null +++ b/src/main/js/lib/scriptcraft.js @@ -0,0 +1,785 @@ +'use strict'; +/*global require*/ +/************************************************************************ + +## Modules in Scriptcraft + +ScriptCraft has a simple module loading system. In ScriptCraft, files +and modules are in one-to-one correspondence. As an example, foo.js +loads the module circle.js in the same directory. +*ScriptCraft now uses the same module system as Node.js - see [Node.js Modules][njsmod] for more details.* + +[njsmod]: http://nodejs.org/api/modules.html + +The contents of foo.js: + +```javascript +var circle = require('./circle.js'); +console.log( 'The area of a circle of radius 4 is ' + + circle.area(4)); +``` + +The contents of circle.js: + +```javascript +var PI = Math.PI; +exports.area = function (r) { + return PI * r * r; +}; +exports.circumference = function (r) { + return 2 * PI * r; +}; +``` + +The module circle.js has exported the functions area() and +circumference(). To add functions and objects to the root of your +module, you can add them to the special exports object. + +Variables local to the module will be private, as though the module +was wrapped in a function. In this example the variable PI is private +to circle.js. + +If you want the root of your module's export to be a function (such as +a constructor) or if you want to export a complete object in one +assignment instead of building it one property at a time, assign it to +module.exports instead of exports. + +## Module Loading + +When the ScriptCraft Java plugin is first installed, a new +`scriptcraft` subdirectory is created. If your minecraft server +directory is called 'mcserver' then the new subdirectories will be ... + + * mcserver/scriptcraft/ + * mcserver/scriptcraft/plugins + * mcserver/scriptcraft/modules + * mcserver/scriptcraft/lib + +... The `plugins`, `modules` and `lib` directories each serve a different purpose. + +### The plugins directory + +At server startup the ScriptCraft Java plugin is loaded and begins +automatically loading and executing all of the modules (javascript +files with the extension `.js`) it finds in the `scriptcraft/plugins` +directory. All modules in the plugins directory are automatically +loaded into the `global` namespace. What this means is that anything a +module in the `plugins` directory exports becomes a global +variable. For example, if you have a module greeting.js in the plugins +directory.... + +```javascript +exports.greet = function(player) { + echo(player, 'Hello ' + player.name); +}; +``` + +... then `greet` becomes a global function and can be used at the +in-game (or server) command prompt like so... + + /js greet(self) + +... This differs from how modules (in NodeJS and commonJS +environments) normally work. If you want your module to be exported +globally, put it in the `plugins` directory. If you don't want your +module to be exported globally but only want it to be used by other +modules, then put it in the `modules` directory instead. If you've +used previous versions of ScriptCraft and have put your custom +javascript modules in the `js-plugins` directory, then put them in the +`scriptcraft/plugins` directory. To summarise, modules in this directory are ... + + * Automatically loaded and run at server startup. + * Anything exported by modules becomes a global variable. + +### The modules directory + +The module directory is where you should place your modules if you +don't want to export globally. In javascript, it's considered best +practice not to have too many global variables, so if you want to +develop modules for others to use, or want to develop more complex +mods then your modules should be placed in the `modules` directory. +*Modules in the `modules` directory are not automatically loaded at +startup*, instead, they are loaded and used by other modules/plugins +using the standard `require()` function. This is the key difference +between modules in the `plugins` directory and modules in the +`modules` directory. Modules in the `plugins` directory are +automatically loaded and exported in to the global namespace at server +startup, modules in the `modules` directory are not. + +### The lib directory + +Modules in the `lib` directory are for use by ScriptCraft and some +core functions for use by module and plugin developers are also +provided. The `lib` directory is for internal use by ScriptCraft. +Modules in this directory are not automatically loaded nor are they +globally exported. + +### plugins sub-directories + +As of December 24 2013, the `scriptcraft/plugins` directory has the following sub-directories... + + * drone - Contains the drone module and drone extensions. Drone was the first scriptcraft module. + * mini-games - Contains mini-games + * arrows - The arrows module - Changes the behaviour of Arrows: Explosive, Fireworks, Teleportation etc. + * signs - The signs module (includes example signs) - create interactive signs. + * chat - The chat plugin/module + * alias - The alias plugin/module - for creating custom aliases for commonly-used commands. + * home - The home module - for setting homes and visiting other homes. + +## Global variables + +There are a couple of special javascript variables available in ScriptCraft... + +### __plugin variable +The ScriptCraft JavaPlugin object. + +### server variable +The Minecraft Server object + +### self variable +The current player. (Note - this value should not be used in multi-threaded scripts or event-handling code - it's not thread-safe). This variable is only safe to use at the in-game prompt and should *never* be used in modules. For example you can use it here... + + /js console.log(self.name) + +... but not in any javascript module you create yourself or in any +event handling code. `self` is a temporary short-lived variable which +only exists in the context of the in-game or server command prompts. + +### config variable +ScriptCraft configuration - this object is loaded and saved at startup/shutdown. + +### events variable +The events object is used to add new event handlers to Minecraft. + +## Module variables +The following variables are available only within the context of Modules. (not available at in-game prompt). + +### __filename variable +The current file - this variable is only relevant from within the context of a Javascript module. + +### __dirname variable +The current directory - this variable is only relevant from within the context of a Javascript module. + +## Global functions + +ScripCraft provides some global functions which can be used by all plugins/modules... + +### echo function + +The `echo()` function displays a message on the in-game screen. + +#### Example + + /js echo( self, 'Hello World') + +For programmers familiar with Javascript web programming, an `alert` +function is also provided. `alert` works exactly the same as `echo` +e.g. `alert( self, 'Hello World')`. + +### require() function + +ScriptCraft's `require()` function is used to load modules. The `require()` function takes a module name as a parameter and will try to load the named module. + +#### Parameters + + * modulename - The name of the module to be loaded. Can be one of the following... + + - A relative file path (with or without `.js` suffix) + - An absolute file path (with or without `.js` suffix) + - A relative directory path (uses node.js rules for directories) + - An absolute directory path (uses node.js rules for directories) + - A name of the form `'events'` - in which case the `lib` directory and `modules` directories are searched for the module. + +#### Return + +require() will return the loaded module's exports. + +### scload() function + +#### No longer recommended for use by Plugin/Module developers (deprecated) + +scload() should only be used to load .json data. + +#### Parameters + + * filename - The name of the file to load. + * warnOnFileNotFound (optional - default: false) - warn if the file was not found. + +#### Returns + +scload() will return the result of the last statement evaluated in the file. + +#### Example + + scload("myFile.js"); // loads a javascript file and evaluates it. + + var myData = scload("myData.json"); // loads a javascript file and evaluates it - eval'd contents are returned. + +##### myData.json contents... + + { players: { + walterh: { + h: ["jsp home {1}"], + sunny:["time set 0", + "weather clear"] + } + } + } + +### scsave() function + +The scsave() function saves an in-memory javascript object to a +specified file. Under the hood, scsave() uses JSON (specifically +json2.js) to save the object. There will usually be no need to call +this function directly - If you want to have a javascript object +automatically loaded at startup and saved on shutdown then use the +`persist()` module. The `persist()` module uses scsave and scload +under the hood. Any in-memory object saved using the `scsave()` +function can later be restored using the `scload()` function. + +#### Parameters + + * objectToSave : The object you want to save. + * filename : The name of the file you want to save it to. + +#### Example + +```javascript +var myObject = { name: 'John Doe', + aliases: ['John Ray', 'John Mee'], + date_of_birth: '1982/01/31' }; +scsave(myObject, 'johndoe.json'); +``` + +##### johndoe.json contents... + + { "name": "John Doe", + "aliases": ["John Ray", "John Mee"], + "date_of_birth": "1982/01/31" + }; + +### plugin() function + +#### Update April 2015 +The `plugin()` function is deprecated. Please refer to [Anatomy of a +ScriptCraft Plugin][anatomy] for an up-to-date step-by-step guide to +creating a plugin which uses persistence (loading and saving data). + +#### Deprecated +The `plugin()` function should be used to declare a javascript module +whose state you want to have managed by ScriptCraft - that is - a +Module whose state will be loaded at start up and saved at shut down. +A plugin is just a regular javascript object whose state is managed by +ScriptCraft. The only member of the plugin which whose persistence is +managed by Scriptcraft is `store` - this special member will be +automatically saved at shutdown and loaded at startup by +ScriptCraft. This makes it easier to write plugins which need to +persist data. + +#### Parameters + + * pluginName (String) : The name of the plugin - this becomes a global variable. + * pluginDefinition (Object) : The various functions and members of the plugin object. + * isPersistent (boolean - optional) : Specifies whether or not the + plugin/object state should be loaded and saved by ScriptCraft. + +#### Example + +See chat/color.js for an example of a simple plugin - one which lets +players choose a default chat color. See also [Anatomy of a +ScriptCraft Plugin][anatomy]. + +[anatomy]: ./Anatomy-of-a-Plugin.md + +### command() function + +The `command()` function is used to expose javascript functions for use by non-operators (regular players). Only operators should be allowed use raw javascript using the `/js ` command because it is too powerful for use by regular players and can be easily abused. However, the `/jsp ` command lets you (the operator / server administrator / plugin author) safely expose javascript functions for use by players. + +#### Parameters + + * commandFunction: The named javascript function which will be invoked when the command is invoked by a player. The name of the function will be used as the command name so name this function accordingly. The callback function in turn takes 2 parameters... + + * params : An Array of type String - the list of parameters passed to the command. + * sender : The [CommandSender][bukcs] object that invoked the command (this is usually a Player object but can be a Block ([BlockCommandSender][bukbcs]). + + * options (Array|Function - optional) : An array of command options/parameters which the player can supply (It's useful to supply an array so that Tab-Completion works for the `/jsp ` commands. If a function is supplied instead of an array then the function will be invoked at TAB-completion time and should return an array of strings. + * intercepts (boolean - optional) : Indicates whether this command can intercept Tab-Completion of the `/jsp ` command - advanced usage - see alias/alias.js for example. + +#### Example + + // javascript code + function boo( params, sender) { + echo( sender, params[0] ); + } + command( boo ); + + # in-game execution + /jsp boo Hi! + > Hi! + +To use a callback for options (TAB-Completion) ... + + var utils = require('utils'); + function boo( params, sender ) { + var receiver = server.getPlayer( params[0] ); + if ( receiver ){ + echo( receiver, sender.name + ' says boo!'); + } + } + command( boo, utils.playerNames ); + +See chat/colors.js or alias/alias.js or homes/homes.js for more examples of how to use the `command()` function. + +### setTimeout() function + +This function mimics the setTimeout() function used in browser-based javascript. However, the function will only accept a function reference, not a string of javascript code. Where setTimeout() in the browser returns a numeric value which can be subsequently passed to clearTimeout(), This implementation returns an object which can be subsequently passed to ScriptCraft's own clearTimeout() implementation. + +If Node.js supports setTimeout() then it's probably good for ScriptCraft to support it too. + +#### Example + +```javascript +// +// start a storm in 5 seconds +// +setTimeout( function() { + var world = server.worlds.get(0); + world.setStorm(true); +}, 5000); +``` + +### clearTimeout() function + +A scriptcraft implementation of clearTimeout(). + +### setInterval() function + +This function mimics the setInterval() function used in browser-based javascript. However, the function will only accept a function reference, not a string of javascript code. Where setInterval() in the browser returns a numeric value which can be subsequently passed to clearInterval(), This implementation returns an object which can be subsequently passed to ScriptCraft's own clearInterval() implementation. + +### clearInterval() function + +A scriptcraft implementation of clearInterval(). + +### refresh() function + +The refresh() function can be used to only reload the ScriptCraft plugin (it's like the `reload` command except it only reloads ScriptCraft). The refresh() function will ... + +1. Disable the ScriptCraft plugin. +2. Unload all event listeners associated with the ScriptCraft plugin. +3. Cancel all timed tasks (created by `setInterval` & `setTimeout`) +3. Enable the ScriptCraft plugin. + +... refresh() can be used during development to reload only scriptcraft javascript files. +See [issue #69][issue69] for more information. + +By default, if `self` is defined at runtime, it checks, whether `self` is server operator, otherwise fails with message. This behavivor can be modified using `skipOpCheck` parameter (useful, if you are doing some custom premission checks before calling this function). + +#### Parameters + + * skipOpCheck (boolean - optional) : If true, the function won't check if `self` is server operator. + +[issue69]: https://github.com/walterhiggins/ScriptCraft/issues/69 + +### addUnloadHandler() function + +The addUnloadHandler() function takes a callback function as a parameter. The callback will be called when the ScriptCraft plugin is unloaded (usually as a result of a a `reload` command or server shutdown). + +This function provides a way for ScriptCraft modules to do any required cleanup/housekeeping just prior to the ScriptCraft Plugin unloading. + +### isOp() function + +This function takes a single parameter and returns true if it's an operator or has operator-level privileges. + +***/ + +/* + wph 20130124 - make self, plugin and server public - these are far more useful now that tab-complete works. +*/ +var global = this; +var server; +global.nashorn = typeof Java !== 'undefined'; +/* + private implementation +*/ +var __onDisableImpl; +function __onDisable ( __engine, __plugin ) { + __onDisableImpl( __engine, __plugin); +} +function __onEnable ( __engine, __plugin, __script ) { + function _echo( ) { + var sender, msg; + if (arguments.length == 2){ + sender = arguments[0]; + msg = arguments[1]; + } else { + if ( typeof self == 'undefined' ) { + return; + } + sender = self; + msg = arguments[0]; + } + if (__plugin.canary){ + sender.message( msg ); + } else { + sender.sendMessage( msg ); + } + } // end echo() + function _canonize( file ) { + return '' + file.getCanonicalPath().replaceAll( '\\\\', '/' ); + } + /* + Save a javascript object to a file (saves using JSON notation) + */ + function _save( objToSave, filename ) { + var objectToStr = null, + f, + out; + try { + objectToStr = JSON.stringify( objToSave, null, 2 ); + + } catch( e ) { + console.error( 'ERROR: ' + e.getMessage() + ' while saving ' + filename ); + return; + } + f = (filename instanceof File) ? filename : new File(filename); + out = new PrintWriter(new FileWriter(f)); + out.println( objectToStr ); + out.close(); + } + function _loadJSON( filename ){ + var result = null, + file = filename, + r, + reader, + br, + contents; + + if ( !( filename instanceof File ) ) { + file = new File(filename); + } + var canonizedFilename = _canonize( file ); + + if ( file.exists() ) { + reader = new FileReader( file ); + br = new BufferedReader( reader ); + contents = ''; + try { + while ( (r = br.readLine()) !== null ) { + contents += r + '\n'; + } + result = JSON.parse(contents); + } catch ( e ) { + logError('Error evaluating ' + canonizedFilename + ', ' + e ); + } + finally { + try { + reader.close(); + } catch ( re ) { + // fail silently on reader close error + } + } + } + return result; + } + /* + Load the contents of the file and evaluate as javascript + */ + function _load( filename, warnOnFileNotFound ) + { + var result = null, + file = filename, + r, + parent, + reader, + br, + code, + wrappedCode; + + if ( !( filename instanceof File ) ) { + file = new File(filename); + } + var canonizedFilename = _canonize( file ); + + if ( file.exists() ) { + reader = new FileReader( file ); + br = new BufferedReader( reader ); + code = ''; + try { + while ( (r = br.readLine()) !== null ) { + code += r + '\n'; + } + wrappedCode = '(' + code + ')'; + result = __engine.eval( wrappedCode ); + // issue #103 avoid side-effects of || operator on Mac Rhino + } catch ( e ) { + logError('Error evaluating ' + canonizedFilename + ', ' + e ); + } + finally { + try { + reader.close(); + } catch ( re ) { + // fail silently on reader close error + } + } + } else { + if ( warnOnFileNotFound ) { + logWarn(canonizedFilename + ' not found' ); + } + } + return result; + } // end _load() + + function _isOp( sender ){ + if (__plugin.canary){ + return sender.receiverType.name() == 'SERVER' || Canary.ops().isOpped(sender); + } else { + return sender.op; + } + } + function _refresh( skipOpCheck ) { + if (!skipOpCheck && typeof self !== 'undefined') { + if (!_isOp(self)) + return echo(self, 'Only operators can refresh()'); + } + + if (__plugin.canary){ + var pluginName = __plugin.name; + Canary.manager().disablePlugin( pluginName ); + Canary.manager().enablePlugin( pluginName ); + } else { + __plugin.pluginLoader.disablePlugin( __plugin ); + org.bukkit.event.HandlerList["unregisterAll(org.bukkit.plugin.Plugin)"]( __plugin ); + server.scheduler.cancelTasks( __plugin ); + __plugin.pluginLoader.enablePlugin( __plugin ); + } + } // end _refresh() + function _onDisable( evt ) { + // save config + _save( global.config, new File( jsPluginsRootDir, 'data/global-config.json' ) ); + _runUnloadHandlers(); + } + function _addUnloadHandler( f ) { + unloadHandlers.push( f ); + } + function _runUnloadHandlers() { + for ( var i = 0; i < unloadHandlers.length; i++ ) { + unloadHandlers[i]( ); + } + } + function __onCommand() { + var jsArgs = [], + i = 0, + jsResult, + result, + cmdName, + sender, + args, + cmd, + label, + fnBody; + + if ( __plugin.canary ) { + sender = arguments[0]; + args = arguments[1]; + cmdName = (''+args[0]).toLowerCase().replace(/^\//,''); + for ( i = 1; i < args.length ; i++ ) { + jsArgs.push( '' + args[i] ); + } + } else { + sender = arguments[0]; + cmd = arguments[1]; + label = arguments[2]; + args = arguments[3]; + cmdName = ( '' + cmd.name ).toLowerCase(); + for ( ; i < args.length ; i++ ) { + jsArgs.push( '' + args[i] ); + } + } + result = false; + + if (cmdName == 'js') + { + result = true; + fnBody = jsArgs.join(' '); + global.self = sender; + global.__engine = __engine; + try { + // cannot rely on native eval in jre7 and jre8 + // because ... + // js var hearts + // js hearts + // ... throws an execption ('hearts' is not defined). vars are not sticky in native eval . + // + jsResult = __engine.eval( fnBody ); + + if ( typeof jsResult != 'undefined' ) { + if ( jsResult == null) { + // engine eval will return null even if the result should be undefined + // this can be confusing so I think it's better to omit output for this case + // sender.sendMessage('(null)'); + } else { + try { + if ( isJavaObject(jsResult) || typeof jsResult === 'function') { + echo(sender, jsResult); + } else { + var replacer = function replacer(key, value){ + return this[key] instanceof java.lang.Object ? '' + this[key] : value; + }; + echo(sender, JSON.stringify( jsResult, replacer, 2) ); + } + } catch ( displayError ) { + logError('Error while trying to display result: ' + jsResult + ', Error: '+ displayError) ; + } + } + } + } catch ( e ) { + logError( 'Error while trying to evaluate javascript: ' + fnBody + ', Error: '+ e ); + echo( sender, 'Error while trying to evaluate javascript: ' + fnBody + ', Error: '+ e ); + throw e; + } finally { + /* + wph 20140312 don't delete self on nashorn until https://bugs.openjdk.java.net/browse/JDK-8034055 is fixed + */ + if ( !nashorn ) { + delete global.self; + delete global.__engine; + } + } + } + if ( cmdName == 'jsp' ) { + cmdModule.exec( jsArgs, sender ); + result = true; + } + return result; + } // end __onCommand() function + + var Bukkit = null; + var Canary = null; + var logger = null; + + if (__plugin.canary){ + Canary = Packages.net.canarymod.Canary; + server = Canary.server; + logger = __plugin.logman; + } else { + Bukkit = Packages.org.bukkit.Bukkit; + server = Bukkit.server; + logger = __plugin.logger; + } + function logError(msg){ + __plugin.canary ? logger.error( msg ) : logger.severe( msg ); + } + function logWarn(msg){ + __plugin.canary ? logger.warn( msg ) : logger.warning( msg ); + } + var File = java.io.File, + FileReader = java.io.FileReader, + BufferedReader = java.io.BufferedReader, + PrintWriter = java.io.PrintWriter, + FileWriter = java.io.FileWriter, + // assumes scriptcraft.js is in mcserver/plugins/scriptcraft/lib directory + jsPluginsRootDir = __script.parentFile.parentFile, + jsPluginsRootDirName = _canonize(jsPluginsRootDir), + unloadHandlers = []; + + /* + make sure eval is present: it's present on JRE 6, 7, and 8 on Linux + */ + if ( typeof eval == 'undefined' ) { + global.eval = function( str ) { + return __engine.eval( str ); + }; + } + + /* + now that load is defined, use it to load a global config object + */ + var configFile = new File(jsPluginsRootDir, 'data/'); + configFile.mkdirs(); + configFile = new File(configFile,'global-config.json'); + var config = _load( configFile ); + if ( !config ) { + config = { verbose: false }; + } + global.config = config; + global.__plugin = __plugin; + /* + wph 20131229 Issue #103 JSON is not bundled with javax.scripting / Rhino on Mac. + */ + (function(){ + var jsonFileReader = new FileReader( new File( jsPluginsRootDirName + '/lib/json2.js' ) ); + var jsonLoaded = __engine['eval(java.io.Reader)']( jsonFileReader ); + }()); + + global.addUnloadHandler = _addUnloadHandler; + global.refresh = _refresh; + global.echo = _echo; + global.alert = _echo; + global.scload = _load; + global.scsave = _save; + global.scloadJSON = _loadJSON; + global.isOp = _isOp; + var configRequire = _load( jsPluginsRootDirName + '/lib/require.js', true ); + /* + setup paths to search for modules + */ + var modulePaths = [ jsPluginsRootDirName + '/lib/', + jsPluginsRootDirName + '/modules/' ]; + + if ( config.verbose ) { + logger.info( 'Setting up CommonJS-style module system. Root Directory: ' + jsPluginsRootDirName ); + logger.info( 'Module paths: ' + JSON.stringify(modulePaths) ); + } + var requireHooks = { + loading: function( path ) { + if ( config.verbose ) { + logger.info( 'loading ' + path ); + } + }, + loaded: function( path ) { + if ( config.verbose ) { + logger.info( 'loaded ' + path ); + } + } + }; + global.require = configRequire( + jsPluginsRootDirName, + modulePaths, + requireHooks, + function( code ) { + return __engine.eval( code ); + } + ); + + var testJSPatch = require('js-patch')( global ); + var console = require('console')(logger); + global.console = console; + testJSPatch(console); + + /* + setup persistence + */ + require('persistence')( jsPluginsRootDir, global ); + + var isJavaObject = require('java-utils').isJavaObject; + + var cmdModule = require('command'); + global.command = cmdModule.command; + var plugins = require('plugin'); + global.__onTabComplete = require('tabcomplete'); + global.plugin = plugins.plugin; + + var events = require('events'); + // wph 20131226 - make events global as it is used by many plugins/modules + global.events = events; + + if (__plugin.canary) { + // canary plugin doesn't get to handle its own plugin disable event + } else { + events.pluginDisable(_onDisable); + } + __onDisableImpl = _onDisable; + global.__onCommand = __onCommand; + plugins.autoload( global, new File(jsPluginsRootDir,'plugins') ); + require('legacy-check')(jsPluginsRootDir); +} diff --git a/src/main/js/lib/tabcomplete-jsp.js b/src/main/js/lib/tabcomplete-jsp.js new file mode 100644 index 0000000..1928b39 --- /dev/null +++ b/src/main/js/lib/tabcomplete-jsp.js @@ -0,0 +1,48 @@ +'use strict'; +var _commands = require('command').commands; +/* + Tab completion for the /jsp commmand +*/ +var __onTabCompleteJSP = function( result, cmdArgs ) { + var cmdInput = cmdArgs[0], + opts, + cmd, + len, + i; + cmd = _commands[cmdInput]; + if ( cmd ) { + if (typeof cmd.options === 'function'){ + opts = cmd.options(); + } else { + opts = cmd.options; + } + len = opts.length; + if ( cmdArgs.length > 1 ) { + // partial e.g. /jsp chat_color dar + for ( i = 0; i < len; i++ ) { + if ( opts[i].indexOf( cmdArgs[1] ) == 0 ) { + result.add( opts[i] ); + } + } + } + } else { + if ( cmdArgs.length == 0 ) { + for ( i in _commands ) { + result.add( i ); + } + } else { + // partial e.g. /jsp ho + // should tabcomplete to home + // + for ( i in _commands ) { + if ( i.indexOf( cmdInput ) == 0 ) { + result.add( i ); + } + } + } + } + return result; +}; +module.exports = __onTabCompleteJSP; + + diff --git a/src/main/js/lib/tabcomplete.js b/src/main/js/lib/tabcomplete.js new file mode 100644 index 0000000..fd7a00c --- /dev/null +++ b/src/main/js/lib/tabcomplete.js @@ -0,0 +1,255 @@ +'use strict'; +var tabCompleteJSP = require('tabcomplete-jsp'), + isJavaObject = require('java-utils').isJavaObject; + +/* + Tab Completion of the /js and /jsp commands +*/ +var _javaLangObjectMethods = [ + 'equals' + ,'getClass' + ,'class' + ,'getClass' + ,'hashCode' + ,'notify' + ,'notifyAll' + ,'toString' + ,'wait' + ,'clone' + ,'finalize' +]; + +var _getProperties = function( o ) { + var result = [], + i, + j, + isObjectMethod, + propValue, + typeofProperty; + + if ( isJavaObject( o ) ) { + /* + fix for issue #115 - java objects are not iterable + see: http://mail.openjdk.java.net/pipermail/nashorn-dev/2014-March/002790.html + */ + if ( typeof Object.bindProperties === 'function' ) { + var placeholder = {}; + Object.bindProperties(placeholder, o); + o = placeholder; + } + propertyLoop: + for ( i in o ) { + // + // don't include standard Object methods + // + isObjectMethod = false; + for ( j = 0; j < _javaLangObjectMethods.length; j++ ) { + if ( _javaLangObjectMethods[j] == i ) { + continue propertyLoop; + } + } + typeofProperty = null; + try { + propValue = o[i]; + typeofProperty = typeof propValue; + } catch( e ) { + if ( e.message == 'java.lang.IllegalStateException: Entity not leashed' ) { + // wph 20131020 fail silently for Entity leashing in craftbukkit + } else { + // don't throw an error during tab completion just make a best effort to + // do the job. + } + } + if ( typeofProperty == 'function' ) { + result.push( i+'()' ); + } else { + result.push( i ); + } + } + } else { + if ( o.constructor == Array ) { + return result; + } + for ( i in o ) { + if ( i.match( /^[^_]/ ) ) { + if ( typeof o[i] == 'function'){ + if ( ! (o[i] instanceof java.lang.Object) ) { + try { + if (o[i].constructor){} // throws error for java objects in jre7 + result.push(i + '()'); + } catch (e ){ + result.push(i); + } + + }else { + result.push( i ); + } + } else { + result.push( i ); + } + } + } + } + return result.sort(); +}; + +var onTabCompleteJS = function( ) { + + var _globalSymbols, + lastArg, + propsOfLastArg, + statement, + statementSyms, + lastSymbol, + parts, + name, + symbol, + lastGoodSymbol, + lastArgProp, + i, + objectProps, + candidate, + re, + li, + possibleCompletion, + result, + cmdSender, + pluginCmd, + cmdArgs; + + result = arguments[0]; + cmdSender = arguments[1]; + if (__plugin.bukkit){ + pluginCmd = arguments[2].name; + cmdArgs = arguments[4]; + } + if (__plugin.canary){ + cmdArgs = arguments[2]; + pluginCmd = arguments[3]; + } + cmdArgs = Array.prototype.slice.call( cmdArgs, 0 ); + + if (__plugin.canary){ + // if 1st element is 'js' then splice + // there's probably a better way to do this + if (cmdArgs[0] == 'js'){ + cmdArgs = cmdArgs.slice(1); + } + } + if ( pluginCmd == 'jsp' ) { + return tabCompleteJSP( result, cmdArgs ); + } + + global.self = cmdSender; // bring in self just for autocomplete + + _globalSymbols = _getProperties(global); + + lastArg = cmdArgs.length ? cmdArgs[ cmdArgs.length - 1 ] + '' : null; + propsOfLastArg = []; + statement = cmdArgs.join(' '); + + statement = statement.replace(/^\s+/,'').replace(/\s+$/,''); + + if ( statement.length == 0 ) { + propsOfLastArg = _globalSymbols; + } else { + if (statement.match(/\)$/)){ + return; + } + statementSyms = statement.split(/[^\$a-zA-Z0-9_\.]/); + + lastSymbol = statementSyms[statementSyms.length-1]; + //print('DEBUG: lastSymbol=[' + lastSymbol + ']'); + // + // try to complete the object ala java IDEs. + // + parts = lastSymbol.split(/\./); + name = parts[0]; + + symbol = global[name]; + + //print('DEBUG: name=' + name + ',symbol=' + symbol); + + lastGoodSymbol = symbol; + if ( typeof symbol !== 'undefined' ) { + for ( i = 1; i < parts.length; i++ ) { + name = parts[i]; + if ( !name ) { // fix issue #115 + break; + } + try { + // this causes problems in jre if symbol is an enum and name is partial-match + symbol = symbol[name]; // this causes problem in jre8 if name is '' + } catch (e){ + symbol = null; + break; + } + if ( typeof symbol == 'undefined' ) { + break; + } + // nashorn - object[missingProperty] returns null not undefined + if ( symbol == null ) { + break; + } + lastGoodSymbol = symbol; + } + if ( typeof symbol == 'undefined' || symbol === null) { + // + // look up partial matches against last good symbol + // + objectProps = _getProperties( lastGoodSymbol ); + if ( name == '' ) { + // if the last symbol looks like this.. + // server. + // + //print('debug:case Y1: server.'); + + for ( i = 0; i < objectProps.length; i++ ) { + candidate = lastSymbol + objectProps[i]; + re = new RegExp( lastSymbol + '$', 'g' ); + propsOfLastArg.push( lastArg.replace( re, candidate ) ); + } + + } else { + // it looks like this.. + // server.wo + // + //print('debug:case Y2: server.wo'); + + li = statement.lastIndexOf(name); + for ( i = 0; i < objectProps.length; i++ ) { + if ( objectProps[i].indexOf(name) == 0 ) { + candidate = lastSymbol.substring( 0, lastSymbol.lastIndexOf( name ) ); + candidate = candidate + objectProps[i]; + re = new RegExp( lastSymbol + '$', 'g' ); + propsOfLastArg.push( lastArg.replace( re, candidate ) ); + } + } + } + } else { + //print('debug:case Y3: server'); + objectProps = _getProperties( symbol ); + for ( i = 0; i < objectProps.length; i++ ) { + re = new RegExp( lastSymbol+ '$', 'g' ); + lastArgProp = lastArg.replace( re, lastSymbol + '.' + objectProps[i] ) ; + lastArgProp = lastArgProp.replace(/\.\./g,'.'); + propsOfLastArg.push( lastArgProp ); + } + } + } else { + for ( i = 0; i < _globalSymbols.length; i++ ) { + if ( _globalSymbols[i].indexOf(lastSymbol) == 0 ) { + possibleCompletion = _globalSymbols[i]; + re = new RegExp( lastSymbol+ '$', 'g' ); + propsOfLastArg.push( lastArg.replace( re, possibleCompletion ) ); + } + } + } + } + for ( i = 0; i < propsOfLastArg.length; i++ ) { + result.add( propsOfLastArg[i] ); + } + + delete global.self; // delete self when no longer needed for autocomplete +}; +module.exports = onTabCompleteJS; diff --git a/src/main/js/lib/task-bukkit.js b/src/main/js/lib/task-bukkit.js new file mode 100644 index 0000000..959589d --- /dev/null +++ b/src/main/js/lib/task-bukkit.js @@ -0,0 +1,24 @@ +'use strict'; +/*global __plugin, module, server*/ +function bukkitSetTimeout( callback, delayInMillis ){ + var delay = Math.ceil( delayInMillis / 50 ); + var task = server.scheduler.runTaskLater( __plugin, callback, delay ); + return task; +} +function bukkitClearTimeout( task ) { + task.cancel(); +} +function bukkitSetInterval( callback, intervalInMillis ) { + var delay = Math.ceil( intervalInMillis / 50); + var task = server.scheduler.runTaskTimer( __plugin, callback, delay, delay ); + return task; +} +function bukkitClearInterval( bukkitTask ) { + bukkitTask.cancel(); +} +module.exports = function($){ + $.setTimeout = bukkitSetTimeout; + $.clearTimeout = bukkitClearTimeout; + $.setInterval = bukkitSetInterval; + $.clearInterval = bukkitClearInterval; +}; diff --git a/src/main/js/lib/task-canary.js b/src/main/js/lib/task-canary.js new file mode 100644 index 0000000..cbe2682 --- /dev/null +++ b/src/main/js/lib/task-canary.js @@ -0,0 +1,35 @@ +'use strict'; +/*global Packages, __plugin, module*/ +/* + javascript programmers familiar with setTimeout know that it expects + a delay in milliseconds. However, bukkit's scheduler expects a delay in ticks + (where 1 tick = 1/20th second) + */ +function canarySetTimeout( callback, delayInMillis ){ + var cmTaskManager = Packages.net.canarymod.tasks.ServerTaskManager; + var delay = Math.ceil( delayInMillis / 50 ); + var task = __plugin.createServerTask(callback, delay, false); + cmTaskManager.addTask(task); + return task; +} +function canaryClearTimeout( task ){ + var cmTaskManager = Packages.net.canarymod.tasks.ServerTaskManager; + cmTaskManager.removeTask( task ); +} +function canarySetInterval( callback, intervalInMillis ) { + var cmTaskManager = Packages.net.canarymod.tasks.ServerTaskManager; + var delay = Math.ceil( intervalInMillis / 50 ); + var task = __plugin.createServerTask(callback, delay, true); + cmTaskManager.addTask(task); + return task; +} +function canaryClearInterval( task ){ + var cmTaskManager = Packages.net.canarymod.tasks.ServerTaskManager; + cmTaskManager.removeTask( task ); +} +module.exports = function($){ + $.setTimeout = canarySetTimeout; + $.clearTimeout = canaryClearTimeout; + $.setInterval = canarySetInterval; + $.clearInterval = canaryClearInterval; +}; diff --git a/src/main/js/modules/at.js b/src/main/js/modules/at.js new file mode 100644 index 0000000..03d3384 --- /dev/null +++ b/src/main/js/modules/at.js @@ -0,0 +1,155 @@ +'use strict'; +/*global events, module, require, __plugin, setInterval, clearInterval, setTimeout, addUnloadHandler*/ +var utils = require('utils'); +/************************************************************************ +## The at Module + +The at module provides a single function `at()` which can be used to schedule +repeating (or non-repeating) tasks to be done at a particular time. + +### at() function + +The utils.at() function will perform a given task at a given time in the +(minecraft) day. + +#### Parameters + + * time24hr : The time in 24hr form - e.g. 9:30 in the morning is '09:30' while + 9:30 pm is '21:30', midnight is '00:00' and midday is '12:00' + * callback : A javascript function which will be invoked at the given time. + * worlds : (optional) An array of worlds. Each world has its own clock. If no array of worlds is specified, all the server's worlds are used. + * repeat : (optional) true or false, default is true (repeat the task every day) + +#### Example + +To warn players when night is approaching: + +```javascript +var utils = require('utils'), + at = require('at'); +function warning(){ + utils.players(function( player ) { + echo( player, 'The night is dark and full of terrors!' ); + }); +} +at('19:00', warning); +``` +To run a task only once at the next given time: +```javascript +var utils = require('utils'), + at = require('at'); +function wakeup(){ + utils.players(function( player ) { + echo( player, "Wake Up Folks!" ); + }); +} +at('06:00', wakeup, null, false); +``` + +***/ +var SECOND = 1000; +var POLLING_INTERVAL = 3 * SECOND; // this is probably precise enough + +function at(time24hr, callback, pWorlds, repeat) { + if (arguments.length === 0){ + // TODO: Document this behaviour + console.log(tasksToString()); + return; + } + var timeParts = time24hr.split( ':' ); + var timeMins = (timeParts[0] * 60) + (timeParts[1] * 1); + if (!pWorlds || pWorlds === undefined ) { + pWorlds = utils.worlds(); + } + if (repeat === undefined){ + repeat = true; + } + utils.foreach( pWorlds, function ( world ) { + atAddTask( timeMins, callback, world, repeat); + }); +}; +var atTasks = {}; + +function tasksToString(){ + var result = ''; + for (var world in atTasks){ + result += 'world: ' + world +'\n'; + for (var time in atTasks[world]){ + var scheduledFuncs = atTasks[world][time]; + for (var i = 0;i < scheduledFuncs.length; i++){ + result += ' ' + time + ': ' + scheduledFuncs[i].constructor + '\n'; + } + } + result += '(current world time: ' + utils.time24(world) + ')\n'; + } + return result; +} +/* + constructs a function which will be called every x ticks to + track the schedule for a given world +*/ +function atMonitorFactory(world){ + var worldName = ''+ world.name; + var lastRun = null; + + return function atMonitorForWorld(){ + var timeMins = utils.time24(world); + if (timeMins === lastRun){ + return; + } + if (lastRun === null ){ + lastRun = timeMins - 1; + }else { + lastRun = lastRun % 1440; + } + var worldSchedule = atTasks[worldName]; + if (!worldSchedule){ + return; + } + while ( lastRun > timeMins ? (lastRun <= 1440) : ( lastRun < timeMins ) ){ + + var tasks = worldSchedule[lastRun++]; + if (!tasks){ + continue; + } + utils.foreach(tasks, function(task, i){ + if (!task){ + return; + } + setTimeout(task.callback.bind(null, timeMins, world), 1); + if (!task.repeat){ + tasks[i] = null; + } + }); + } + }; +} +function atAddTask( timeMins, callback, world, repeat){ + var worldName = ''+world.name; + if (!atTasks[worldName]){ + atTasks[worldName] = {}; + } + if (!atTasks[worldName][timeMins]){ + atTasks[worldName][timeMins] = []; + } + atTasks[worldName][timeMins].push({callback: callback, repeat: repeat}); +} +var atMonitors = []; +function onLoadStartMonitor(event){ + var monitor = setInterval( atMonitorFactory(event.world), POLLING_INTERVAL); + atMonitors.push( monitor ); +} +if (__plugin.canary){ + events.loadWorld( onLoadStartMonitor ); +} +if (__plugin.bukkit){ + events.worldLoad( onLoadStartMonitor ); +} + +addUnloadHandler(function(){ + utils.foreach(atMonitors, function(atInterval){ + clearInterval(atInterval); + }); +}); + +module.exports = at; diff --git a/src/main/js/modules/block-colors.js b/src/main/js/modules/block-colors.js new file mode 100644 index 0000000..9c04f65 --- /dev/null +++ b/src/main/js/modules/block-colors.js @@ -0,0 +1,19 @@ +var colors = { + white: 0, + orange: 1, + magenta: 2, + lightblue: 3, + yellow: 4, + lime: 5, + pink: 6, + gray: 7, + lightgray: 8, + cyan: 9, + purple: 10, + blue: 11, + brown: 12, + green: 13, + red: 14, + black: 15 +}; +module.exports = colors; diff --git a/src/main/js/modules/blockhelper.js b/src/main/js/modules/blockhelper.js new file mode 100644 index 0000000..8955f7a --- /dev/null +++ b/src/main/js/modules/blockhelper.js @@ -0,0 +1,165 @@ +'use strict'; +/*global module, exports, require, Packages, __plugin, server*/ +var blocks = require('blocks'), + bountiful = false; + +if (__plugin.canary){ + bountiful = parseFloat(server.canaryModVersion) > 1.7; +} +if (__plugin.bukkit){ + /* + wph 20150103 - metadata still applies for Craftbukkit 1.8 + bountiful = parseFloat(server.bukkitVersion) > 1.7; + */ +} +var lookup = {}; +function initLookup(){ + var Facing = Packages.net.minecraft.util.EnumFacing, + DyeColor = Packages.net.minecraft.item.EnumDyeColor; + + lookup = { + facing: { + 0: Facing.EAST, + 1: Facing.SOUTH, + 2: Facing.WEST, + 3: Facing.NORTH, + 5: Facing.UP, + east: Facing.EAST, + south: Facing.SOUTH, + west: Facing.WEST, + north: Facing.NORTH, + up: Facing.UP, + down: Facing.DOWN + }, + color: { + black: DyeColor.BLACK, + blue: DyeColor.BLUE, + brown: DyeColor.BROWN, + cyan: DyeColor.CYAN, + gray: DyeColor.GRAY, + green: DyeColor.GREEN, + lightblue: DyeColor.LIGHT_BLUE, + lime: DyeColor.LIME, + magenta: DyeColor.MAGENTA, + orange: DyeColor.ORANGE, + pink: DyeColor.PINK, + purple: DyeColor.PURPLE, + red: DyeColor.RED, + silver: DyeColor.SILVER, + white: DyeColor.WHITE, + yellow: DyeColor.YELLOW, + 0: DyeColor.WHITE, + 1: DyeColor.ORANGE, + 2: DyeColor.MAGENTA, + 3: DyeColor.LIGHT_BLUE, + 4: DyeColor.YELLOW, + 5: DyeColor.LIME, + 6: DyeColor.PINK, + 7: DyeColor.GRAY, + 8: DyeColor.SILVER, + 9: DyeColor.CYAN, + 10: DyeColor.PURPLE, + 11: DyeColor.BLUE, + 12: DyeColor.BROWN, + 13: DyeColor.GREEN, + 14: DyeColor.RED, + 15: DyeColor.BLACK + } + }; +} + +function property( block ){ + var result; + result = { + get: function(p){ + var bp = block.getPropertyForName(p); + return block.getValue(bp); + }, + set: function(name,value){ + var bp = block.getPropertyForName(name); + if (bp === null){ + console.warn(block + ' has no property named ' + name); + return result; + } + if (lookup[bp.name]){ + value = lookup[bp.name][value]; + } + block.setPropertyValue(bp, value); + return result; + } + }; + return result; +} +exports.property = property; +/* + blocks which have facing + */ +function applyFacing( block, metadata ){ + function face(direction){ + property(block).set('facing', lookup.facing[direction]); + } + if ( blocks.isStair(block.typeId) ){ + face( ['east','west','south','north'] [metadata] ); + } else { + switch( block.typeId ){ + case blocks.sign: + case blocks.ladder: + // bug: furnace, chest, dispenser don't always use the right metadata + case blocks.furnace: + case blocks.furnace_burning: + case blocks.chest: + case blocks.enderchest: + case blocks.dispenser: + face( [null,null,'north','south','west','east'][metadata] ); + break; + case blocks.torch: + face( ['up'/* default */,'east','west','south','north','up'][metadata] ); + break; + } + } +} +function applyColors( block, metadata ){ + switch( block.typeId){ + case blocks.wool.white: + case 35: + case blocks.stained_clay.white: + case 159: + case blocks.stained_glass.white: + case 95: + case blocks.stained_glass_pane.white: + case 160: + case blocks.carpet.white: + case 171: + property(block).set('color',metadata); + } +} +function applyRotation( block, metadata ){ + switch (block.typeId){ + case blocks.sign_post: + if (metadata !== 0){ + property(block).set('rotation', new Packages.java.lang.Integer(metadata)); + } + } +} +function applyVariant( block, metadata ){ + var cmQuartzProperties = Packages.net.canarymod.api.world.blocks.properties.helpers.QuartzProperties; + switch (block.typeId){ + case blocks.quartz: + cmQuartzProperties.applyVariant(block, cmQuartzProperties.Variant.valueOf(metadata)); + break; + } +} +function applyProperties( block, metadata ){ + if (!bountiful){ + block.data = metadata; + return; + } + if (!lookup.facing){ + initLookup(); + } + applyFacing( block, metadata ); + applyColors( block, metadata ); + applyRotation( block, metadata ); + applyVariant( block, metadata ); +} +exports.applyProperties = applyProperties; diff --git a/src/main/js/modules/blocks.js b/src/main/js/modules/blocks.js new file mode 100644 index 0000000..920c69a --- /dev/null +++ b/src/main/js/modules/blocks.js @@ -0,0 +1,377 @@ +/************************************************************************ +## Blocks Module + +You hate having to lookup [Data Values][dv] when you use ScriptCraft's +Drone() functions. So do I. So I created this blocks object which is +a helper object for use in construction. + +### Examples + + box( blocks.oak ); // creates a single oak wood block + box( blocks.sand, 3, 2, 1 ); // creates a block of sand 3 wide x 2 high x 1 long + box( blocks.wool.green, 2 ); // creates a block of green wool 2 blocks wide + +Color aliased properties that were a direct descendant of the blocks +object are no longer used to avoid confusion with carpet and stained +clay blocks. In addition, there's a convenience array `blocks.rainbow` +which is an array of the 7 colors of the rainbow (or closest +approximations). + +The blocks module is globally exported by the Drone module. + +***/ +var colors = require('./block-colors'); + +var blocks = { + air: 0, + stone: 1, + grass: 2, + dirt: 3, + cobblestone: 4, + oak: 5, spruce: '5:1', birch: '5:2', jungle: '5:3', acacia: '5:4', dark_oak: '5:5', + sapling: { oak: 6, spruce: '6:1', birch: '6:2', jungle: '6:3', acacia: '6:4', dark_oak: '6:5' }, + bedrock: 7, + water: 8, + water_still: 9, + lava: 10, + lava_still: 11, + sand: 12, + gravel: 13, + gold_ore: 14, + iron_ore: 15, + coal_ore: 16, + wood: 17, + leaves: 18, + sponge: 19, + sponge_wet: '19:1', + glass: 20, + lapis_lazuli_ore: 21, + lapis_lazuli_block: 22, + dispenser: 23, + sandstone: 24, + sandstone_chiseled: '24:1', + sandstone_smooth: '24:2', + sandstone_red: 179, + sandstone_red_chiseled: '179:1', + sandstone_red_smooth: '179:2', + note: 25, + bed: 26, + powered_rail: 27, + detector_rail: 28, + sticky_piston: 29, + cobweb: 30, + grass_tall: 31, + dead_bush: 32, + piston: 33, + piston_extn: 34, piston_head: 34, + wool: { white: 35 /* All other colors added below */ }, + piston_extended: 36, + dandelion: 37, flower_yellow: 37, + rose: 38, flower_red: 38, + mushroom_brown: 39, + mushroom_red: 40, + gold: 41, + iron: 42, + //http://minecraft.gamepedia.com/Data_values#Double_Stone_Slabs + double_slab: { + stone: 43, + sandstone: '43:1', + sandstone_red: 181, + sandstone_red_smooth: '181:8', + wooden: '43:2', + wood: { + oak: 125, + spruce: '125:1', + birch: '125:2', + jungle: '125:3', + acacia: '125:4', + dark_oak: '125:5' + }, + cobblestone: '43:3', + brick: '43:4', + stonebrick: '43:5', + netherbrick:'43:6', + quartz: '43:7', + smooth_stone: '43:8', + smooth_sandstone: '43:9', + tile_quartz: '43:15', + purpur: 204, + }, + slab: { + stone: '44:0', + sandstone: '44:1', + wooden: '44:2', + cobblestone: '44:3', + brick: '44:4', + stonebrick: '44:5', + netherbrick:'44:6', + quartz: '44:7', + upper: { + stone: '44:8', + sandstone: '44:9', + sandstone_red: '182:8', + wooden: '44:10', + cobblestone: '44:11', + brick: '44:12', + stonebrick: '44:13', + netherbrick:'44:14', + quartz: '44:15', + oak: '126:8', + spruce: '126:9', + birch: '126:10', + jungle: '126:11', + acacia: '126:12', + dark_oak: '126:13' + }, + snow: 78, + stone: 44, + oak: 126, + spruce: '126:1', + birch: '126:2', + jungle: '126:3', + acacia: '126:4', + dark_oak: '126:5', + sandstone_red: 182, + purpur: 205, + }, + // see brick.red 45 + tnt: 46, + bookshelf: 47, + moss_stone: 48, + obsidian: 49, + torch: 50, + fire: 51, + monster_spawner: 52, + stairs: { + oak: 53, + cobblestone: 67, + brick: 108, + stone: 109, + nether: 114, + sandstone: 128, + spruce: 134, + birch: 135, + jungle: 136, + quartz: 156, + acacia: 163, + dark_oak: 164, + sandstone_red: 180, + purpur: 203, + }, + chest: 54, + redstone_wire: 55, + diamond_ore: 56, + diamond: 57, + crafting_table: 58, + wheat_seeds: 59, + farmland: 60, + furnace: 61, + furnace_burning: 62, + sign_post: 63, + door_wood: 64, + ladder: 65, + rail: 66, + sign: 68, + lever: 69, + pressure_plate_stone: 70, + door_iron: 71, + pressure_plate_wood: 72, + redstone_ore: 73, + redstone_ore_glowing: 74, + torch_redstone: 75, + torch_redstone_active: 76, + stone_button: 77, + ice: 79, + snow: 80, + cactus: 81, + clay: 82, + sugar_cane: 83, + jukebox: 84, + fence: 85, + pumpkin: 86, + netherrack: 87, + soulsand: 88, + glowstone: 89, + netherportal: 90, + jackolantern: 91, + cake: 92, + redstone_repeater: 93, + redstone_repeater_active: 94, + stained_glass: { + white: 95 // all other colors added below + }, + trapdoor: 96, + monster_egg: 97, + brick: { + stone: 98, + mossy: '98:1', + cracked: '98:2', + chiseled: '98:3', + red: 45 + }, + mushroom_brown_huge: 99, + mushroom_red_huge: 100, + iron_bars: 101, + glass_pane: 102, + melon: 103, + pumpkin_stem: 104, + melon_stem: 105, + vines: 106, + fence_gate: 107, + mycelium: 110, + lily_pad: 111, + nether: 112, + nether_fence: 113, + netherwart: 115, + table_enchantment: 116, + brewing_stand: 117, + cauldron: 118, + endportal: 119, + endportal_frame: 120, + endstone: 121, + dragon_egg: 122, + redstone_lamp: 123, + redstone_lamp_active: 124, + cocoa: 127, + emerald_ore: 129, + enderchest: 130, + tripwire_hook: 131, + tripwire: 132, + emerald: 133, + command: 137, + beacon: 138, + cobblestone_wall: 139, + flowerpot: 140, + carrots: 141, + potatoes: 142, + beetroot: 207, + button_wood: 143, + skull: 144, mobhead: 144, + anvil: 145, + chest_trapped: 146, + pressure_plate_weighted_light: 147, + pressure_plate_weighted_heavy: 148, + redstone_comparator: 149, + redstone_comparator_active: 150, + daylight_sensor: 151, + redstone: 152, + quartzore: 153, netherquartzore: 153, + hopper: 154, + quartz: 155, + quartz_chiseled: '155:1', + quartz_pillar_vertical: '155:2', + quartz_pillar_horizontal: '155:3', + quartz_pillar_cap: '155:4', + // see stairs.quartz + rail_activator: 157, + dropper: 158, + stained_clay: { + white: 159 // All other colors added below + }, + stained_glass_pane: { + white: 160 // all other colors added below + }, + slime: 165, + barrier: 166, + trapdoor_iron: 167, + prismarine: 168, + prismarine_brick: '168:1', + prismarine_dark: '168:2', + sealantern: 169, + hay: 170, + carpet: { + white: 171 // All other colors added below + }, + hardened_clay: 172, + coal_block: 173, + packed_ice: 174, + double_plant: 175, + sunflower: 175, + purpur: 201, + purpur_pillar: 202, + flower: { + sunflower: 175, + lilac: '175:1', + tallgrass: '175:2', + fern: '175:3', + rosebush: '175:4', + peony: '175:5', + yellow: 37, + dandelion: 37, + rose: 38, + red: 38, + poppy: 38, + blueorchid: '38:1', + allium: '38:2', + azure_bluet: '38:3', + red_tulip: '38:4', + orange_tulip: '38:5', + white_tulip: '38:6', + pink_tulip: '38:7', + oxeye_daisy: '38:8' + }, + bonemeal: '351:15', + banner: { + standing: 176, + wallmounted: 177 + }, + daylight_sensor_inverted: 178, + gate: { + spruce: 183, + birch: 184, + jungle: 185, + oak: 186, + acacia: 187 + }, + fence: { + spruce: 188, + birch: 189, + jungle: 190, + oak: 191, + acacia: 192 + } +}; + +// Add all available colors to colorized block collections + +var colorized_blocks = [ + 'wool', + 'stained_clay', + 'carpet', + 'stained_glass', + 'stained_glass_pane' +]; + +for (var i = 0, len = colorized_blocks.length; i < len; i++) { + var block = colorized_blocks[i], + data_value = blocks[block].white; + + for (var color in colors) { + blocks[block][color] = data_value + ':' + colors[color]; + } +}; + +/* + rainbow colors - a convenience + Color aliased properties that were a direct descendant of the blocks + object are no longer used to avoid confusion with carpet and stained + clay blocks. +*/ +blocks.rainbow = [ + blocks.stained_glass.red, + blocks.stained_glass.orange, + blocks.stained_glass.yellow, + blocks.stained_glass.lime, + blocks.stained_glass.lightblue, + blocks.stained_glass.blue, + blocks.stained_glass.purple +]; +blocks.isStair = function(id){ + var p; + for (p in this.stairs){ + if (this.stairs[p] == id) + return true; + } + return false; +}; +module.exports = blocks; diff --git a/src/main/js/modules/bukkit/fireworks.js b/src/main/js/modules/bukkit/fireworks.js new file mode 100644 index 0000000..bf58840 --- /dev/null +++ b/src/main/js/modules/bukkit/fireworks.js @@ -0,0 +1,46 @@ +/* + create a firework at the given location +*/ +function bukkitFirework( location ) { + var bkColor = org.bukkit.Color; + var bkFireworkEffect = org.bukkit.FireworkEffect; + var bkEntityType = org.bukkit.entity.EntityType; + + var randInt = function( n ) { + return Math.floor( Math.random() * n ); + }; + var getColor = function( i ) { + var colors = [ + bkColor.AQUA, bkColor.BLACK, bkColor.BLUE, bkColor.FUCHSIA, bkColor.GRAY, + bkColor.GREEN, bkColor.LIME, bkColor.MAROON, bkColor.NAVY, bkColor.OLIVE, + bkColor.ORANGE, bkColor.PURPLE, bkColor.RED, bkColor.SILVER, bkColor.TEAL, + bkColor.WHITE, bkColor.YELLOW]; + return colors[i]; + }; + var fw = location.world.spawnEntity(location, bkEntityType.FIREWORK); + var fwm = fw.getFireworkMeta(); + var fwTypes = [ + bkFireworkEffect.Type.BALL, + bkFireworkEffect.Type.BALL_LARGE, + bkFireworkEffect.Type.BURST, + bkFireworkEffect.Type.CREEPER, + bkFireworkEffect.Type.STAR + ]; + var type = fwTypes[ randInt( 5 ) ]; + + var r1i = randInt( 17 ); + var r2i = randInt( 17 ); + var c1 = getColor( r1i ); + var c2 = getColor( r2i ); + var effectBuilder = bkFireworkEffect.builder() + .flicker( Math.round( Math.random() ) == 0 ) + .withColor( c1 ) + .withFade( c2 ) + .trail( Math.round( Math.random() ) == 0 ); + effectBuilder['with']( type ); + var effect = effectBuilder.build(); + fwm.addEffect( effect ); + fwm.setPower( randInt( 2 ) + 1 ); + fw.setFireworkMeta( fwm ); +} +module.exports = bukkitFirework; diff --git a/src/main/js/modules/bukkit/input.js b/src/main/js/modules/bukkit/input.js new file mode 100644 index 0000000..c0db662 --- /dev/null +++ b/src/main/js/modules/bukkit/input.js @@ -0,0 +1,28 @@ +var bkPrompt = org.bukkit.conversations.Prompt, + bkConversationFactory = org.bukkit.conversations.ConversationFactory; + +function bukkitAsyncInput( sender, promptMesg, callback) { + var repeat = function(){ + bukkitAsyncInput( sender, promptMesg, callback); + }; + var prompt = new bkPrompt( { + getPromptText: function( ctx ) { + return promptMesg; + }, + acceptInput: function( ctx, value ) { + callback.apply( { repeat: repeat, sender: sender, message: promptMesg, value: value }, + [value, sender, repeat]); + return null; + }, + blocksForInput: function( ctx ) { + return true; + } + }); + + new bkConversationFactory( __plugin ) + .withModality( false ) + .withFirstPrompt( prompt ) + .buildConversation( sender ) + .begin( ); +} +module.exports = bukkitAsyncInput; diff --git a/src/main/js/modules/bukkit/inventory.js b/src/main/js/modules/bukkit/inventory.js new file mode 100644 index 0000000..bba4fea --- /dev/null +++ b/src/main/js/modules/bukkit/inventory.js @@ -0,0 +1,18 @@ +function inventory(entity){ + var inv = entity.inventory; + var result = { + add: function(items){ + inv.addItem([items]); + return result; + }, + remove: function(items){ + inv.removeItem([items]); + return result; + }, + contains: function(items){ + return inv['contains(org.bukkit.inventory.ItemStack)'](items); + } + }; + return result; +} +module.exports = inventory; diff --git a/src/main/js/modules/bukkit/items.js b/src/main/js/modules/bukkit/items.js new file mode 100644 index 0000000..7415295 --- /dev/null +++ b/src/main/js/modules/bukkit/items.js @@ -0,0 +1,29 @@ +/*global require, module, Packages */ +var bkItemStack = Packages.org.bukkit.inventory.ItemStack; +var bkMaterial = Packages.org.bukkit.Material; +var items = function( material, amount ) { + material = material.toUpperCase(); + return new bkItemStack(bkMaterial[material],amount); +}; + +var materials = bkMaterial.values(); + +for (var i = 0;i < materials.length; i++ ){ + var name = (''+materials[i].name()).toLowerCase(); + name = name.replace(/(_.)/g,function(a){ return a.replace(/_/,'').toUpperCase(); }); + + items[name] = (function(material){ + return function(amount){ + if (typeof amount == 'undefined'){ + return material; + } + if (typeof amount == 'number'){ + return new bkItemStack(material, amount); + } else { + return amount == material; + } + }; + })(materials[i]); +} + +module.exports = items; diff --git a/src/main/js/modules/bukkit/recipes.js b/src/main/js/modules/bukkit/recipes.js new file mode 100644 index 0000000..315fd0f --- /dev/null +++ b/src/main/js/modules/bukkit/recipes.js @@ -0,0 +1,15 @@ +var items = require('items'); +var bkShapedRecipe = org.bukkit.inventory.ShapedRecipe; + +exports.add = function( recipe ){ + var result = new bkShapedRecipe( recipe.result ); + result.shape(recipe.shape[0], recipe.shape[1], recipe.shape[2]); + for (var i in recipe.ingredients ){ + result.setIngredient( new java.lang.Character(i), recipe.ingredients[i].getData() ); + } + server.addRecipe(result); + return result; +}; +exports.remove = function( recipe ) { + server.removeRecipe(recipe); +}; diff --git a/src/main/js/modules/bukkit/sounds.js b/src/main/js/modules/bukkit/sounds.js new file mode 100644 index 0000000..dbf2875 --- /dev/null +++ b/src/main/js/modules/bukkit/sounds.js @@ -0,0 +1,61 @@ +var bkLocation = Packages.org.bukkit.Location, + i = 0, + foreach = require('utils').foreach, + allSounds = Packages.org.bukkit.Sound.values(), + len = allSounds.length, + sound, + soundName; + +for ( ; i < len; i++ ) { + sound = allSounds[i]; + soundName = '' + sound.name(); + var methodName = soundName.toLowerCase().replace(/_(.)/g,function(a,b){ return b.toUpperCase();}); + exports[methodName] = (function(sound){ + return function() + { + switch (arguments.length) { + case 3: + exports.play(sound, arguments[0], arguments[1], arguments[2]); + break; + case 2: + // TODO: possible combinations: + // location, volume, + // volume pitch + exports.play(sound, arguments[0],arguments[1]); + break; + case 1: + exports.play(sound, arguments[0]); + break; + case 0: + // play the sound at full vol, medium pitch for all players + // + foreach(server.onlinePlayers,function(player){ + exports.play(sound, player, 1, 0); + }); + default: + } + }; + })(sound); +} +exports.play = function(sound, locationOrHasLocation, volume, pitch) { + var location = null; + if (!locationOrHasLocation) + return; + if (locationOrHasLocation instanceof bkLocation){ + location = locationOrHasLocation; + } else { + locationOrHasLocation = locationOrHasLocation.location; + if (locationOrHasLocation && locationOrHasLocation instanceof bkLocation ){ + location = locationOrHasLocation; + } + } + if (!location){ + console.warn('sounds.play() needs a location'); + return; + } + if (typeof volume == 'undefined') + volume = 1; + if (typeof pitch == 'undefined') + pitch = 1; + location.world.playSound(location, sound, volume, pitch); +}; diff --git a/src/main/js/modules/canary/fireworks.js b/src/main/js/modules/canary/fireworks.js new file mode 100644 index 0000000..1335c6d --- /dev/null +++ b/src/main/js/modules/canary/fireworks.js @@ -0,0 +1,32 @@ +'use strict'; +/*global require, Packages, module*/ +var items = require('items'); +var Canary = Packages.net.canarymod.Canary; +var cmFireworkHelper = Packages.net.canarymod.api.inventory.helper.FireworkHelper; +var cmExplosionType = Packages.net.canarymod.api.inventory.helper.FireworkHelper.ExplosionType; +var explosionTypes = ['STAR','BURST','CREEPER','LARGE','SMALL']; +var cmDyeColor = Packages.net.canarymod.api.DyeColor; +var entityFactory = Canary.factory().entityFactory; +var cmEntityType = Packages.net.canarymod.api.entity.EntityType; + +function canaryFirework( location ) { + + var firework = items.fireworkStar(1); + cmFireworkHelper.addStarColors( firework, cmDyeColor.values() ); + cmFireworkHelper.setDoesFlicker( firework, true ); + cmFireworkHelper.setDoesTrail( firework, true ); + + // use a random explosion type + var rnd = Math.floor(Math.random() * explosionTypes.length); + var type = explosionTypes[rnd]; + cmFireworkHelper.setStarExplosionType( firework, cmExplosionType[type]); + var rocket = items.fireworkRocket(1); + cmFireworkHelper.setFlightDuration( rocket, 3); + cmFireworkHelper.attachFireworkStars( rocket, [firework] ); + var rocketEntity = entityFactory.newEntity(cmEntityType.FIREWORKROCKET, location); + rocketEntity.item = rocket; + rocketEntity.spawn(); + +} + +module.exports = canaryFirework; diff --git a/src/main/js/modules/canary/input.js b/src/main/js/modules/canary/input.js new file mode 100644 index 0000000..de2ef63 --- /dev/null +++ b/src/main/js/modules/canary/input.js @@ -0,0 +1,26 @@ + +function canaryAsyncInput( sender, promptMesg, callback) { + sender.message(promptMesg); + function repeat(){ + setTimeout( function(){ + listener.unregister(); // avoid CME + canaryAsyncInput( sender, promptMesg, callback); + },1); + } + var listener = events.chat(function (event) { + if (event.player == sender) { + var receivers = event.getReceiverList(); + if (receivers.size() == 1 && receivers.contains(sender)){ + var value = event.message; + var that = this; + event.setCanceled(); + callback.apply( { repeat: repeat, sender: sender, message: promptMesg, value: value }, + [value, sender, repeat]); + setTimeout(function(){that.unregister();},10); + } + } + },'CRITICAL'); + // unregister after 30 seconds + setTimeout(function(){ listener.unregister(); }, 30000); +} +module.exports = canaryAsyncInput; diff --git a/src/main/js/modules/canary/inventory.js b/src/main/js/modules/canary/inventory.js new file mode 100644 index 0000000..4a0d25c --- /dev/null +++ b/src/main/js/modules/canary/inventory.js @@ -0,0 +1,20 @@ +function inventory(entity){ + var inv = entity.inventory; + var result = { + add: function(items){ + inv['addItem(net.canarymod.api.inventory.Item)'](items); + return result; + }, + remove: function(items){ + inv['decreaseItemStackSize(int, int)'](items.id, items.amount); + return result; + }, + contains: function(items){ + var type = items.type; + var amount = items.amount; + return inv['hasItemStack(ItemType, int )'](type, amount); + } + }; + return result; +} +module.exports = inventory; diff --git a/src/main/js/modules/canary/items.js b/src/main/js/modules/canary/items.js new file mode 100644 index 0000000..f943f1d --- /dev/null +++ b/src/main/js/modules/canary/items.js @@ -0,0 +1,70 @@ +/*global nashorn, require, Packages, module*/ +var ItemType = Packages.net.canarymod.api.inventory.ItemType; +var Canary = Packages.net.canarymod.Canary; +var itemFactory = Canary.factory().itemFactory; + +function items( material, amount ) { + material = material.toUpperCase(); + var result = itemFactory["newItem(net.canarymod.api.inventory.ItemType)"](material); + result.amount = amount; + return result; +} +function getMaterialHandler( material ){ + return function(amount){ + if (typeof amount == 'undefined'){ + return material; + } + if (typeof amount == 'number'){ + var itemStack = itemFactory["newItem(net.canarymod.api.inventory.ItemType)"](material); + itemStack.amount = amount; + return itemStack; + } else { + var result = (amount == material); + if (!result){ + if (amount.getId && amount.getData){ + var m2 = ItemType.fromIdAndData(amount.id, amount.data); + result = (m2 == material); + } + } + return result; + } + }; +} +if (nashorn){ + /* + nashorn + */ + var itemTypeClass = require('nashorn-type')(ItemType); + var materials = itemTypeClass.getDeclaredFields(); + var name; + for (var i = 0;i < materials.length; i++ ){ + + if (materials[i].type != itemTypeClass) { + continue; + } + var materialField = materials[i]; + name = (''+materialField.name); + name = name.replace(/^(.)/,function(a){ + return a.toLowerCase(); + }); + + items[name] = getMaterialHandler(materialField.get(ItemType)); + } +} else { + // non-nashorn + for (var field in ItemType){ + if (ItemType[field] === undefined){ + continue; + } + if (!(ItemType[field] instanceof ItemType)){ + continue; + } + name = (''+field).replace(/^(.)/,function(a){ + return a.toLowerCase(); + }); + items[name] = getMaterialHandler(ItemType[field]); + } +} + + +module.exports = items; diff --git a/src/main/js/modules/canary/recipes.js b/src/main/js/modules/canary/recipes.js new file mode 100644 index 0000000..3729291 --- /dev/null +++ b/src/main/js/modules/canary/recipes.js @@ -0,0 +1,48 @@ +var cm = Packages.net.canarymod; +var cmRecipe = cm.api.inventory.recipes.CraftingRecipe; +var cmRecipeRow = cm.api.inventory.recipes.RecipeRow; + +function addRecipe( recipe ){ + return server.addRecipe( createRecipe( recipe ) ); +} +function createRecipe( recipe ){ + if (!recipe){ + return null; + } + var result, + rows, + i,j, + cells, + rr; + if (recipe.shape){ + rows = []; + for (i = 0; i < recipe.shape.length; i++){ + cells = recipe.shape[i].split(''); + rr = []; + for ( j = 0; j < cells.length ; j++){ + if (cells[j] != ' '){ + rr.push(recipe.ingredients[cells[j]]); + } + } + rows.push( new cmRecipeRow(recipe.shape[i], rr) ); + } + /* + wph 20150607 short-term workaround for nashorn defect + https://bugs.openjdk.java.net/browse/JDK-8072596 + */ + if ( typeof Java !== 'undefined' && typeof Java.type === 'function' ) { + var RecipeRowArrayType = Java.type('net.canarymod.api.inventory.recipes.RecipeRow[]'); + rows = Java.to( rows, RecipeRowArrayType ); + } + result = cmRecipe.createShapedRecipe( recipe.result, rows); + } else { + result = cmRecipe.createShapelessRecipe( recipe.result, recipe.ingredients ); + } + return result; +} +function removeRecipe( recipe ){ + server.removeRecipe( recipe ); +} +exports.create = createRecipe; +exports.add = addRecipe; +exports.remove = removeRecipe; diff --git a/src/main/js/modules/canary/sounds.js b/src/main/js/modules/canary/sounds.js new file mode 100644 index 0000000..1d8bcc4 --- /dev/null +++ b/src/main/js/modules/canary/sounds.js @@ -0,0 +1,65 @@ +var allSounds = Packages.net.canarymod.api.world.effects.SoundEffect.Type.values(), + cmSoundEffect = Packages.net.canarymod.api.world.effects.SoundEffect, + foreach = require('utils').foreach, + i = 0, + len = allSounds.length, + sound, + soundName; + +function playSound(sound, locationOrHasLocation, volume, pitch ) { + var location = null; + if (!locationOrHasLocation) + return; + if (locationOrHasLocation.world){ + location = locationOrHasLocation; + } else { + locationOrHasLocation = locationOrHasLocation.location; + if (locationOrHasLocation && locationOrHasLocation.world ){ + location = locationOrHasLocation; + } + } + if (!location){ + console.warn('sounds.play() needs a location'); + return; + } + if (typeof volume == 'undefined') + volume = 1; + if (typeof pitch == 'undefined') + pitch = 1; + var soundEffect = new cmSoundEffect(sound, location.x, location.y, location.z, volume, pitch); + location.world.playSound(soundEffect); +} + +for ( ; i < len; i++ ) { + sound = allSounds[i]; + soundName = '' + sound.name(); + var methodName = soundName.toLowerCase().replace(/_(.)/g,function(a,b){ return b.toUpperCase();}); + exports[methodName] = (function(sound){ + return function() + { + switch (arguments.length) { + case 3: + playSound(sound, arguments[0], arguments[1], arguments[2]); + break; + case 2: + // TODO: possible combinations: + // location, volume, + // volume pitch + playSound(sound, arguments[0],arguments[1]); + break; + case 1: + playSound(sound, arguments[0]); + break; + case 0: + // play the sound at full vol, medium pitch for all players + // + foreach( server.playerList, function(player) { + playSound(sound, player, 1, 0); + }); + default: + } + }; + })(sound); +} + +exports.play = playSound; diff --git a/src/main/js/modules/classroom/index.js b/src/main/js/modules/classroom/index.js new file mode 100644 index 0000000..31d1729 --- /dev/null +++ b/src/main/js/modules/classroom/index.js @@ -0,0 +1,258 @@ +'use strict'; +/*global require, module, __plugin, __dirname, echo, persist, isOp, events, Packages, command, global */ +var utils = require('utils'), + watcher = require('watcher'), + autoload = require('plugin').autoload, + foreach = utils.foreach, + watchDir = watcher.watchDir, + unwatchDir = watcher.unwatchDir, + playersDir = __dirname + '/../../players/', + serverAddress = utils.serverAddress(); + +/************************************************************************ +## Classroom Plugin + +The `classroom` object contains a couple of utility functions for use +in a classroom setting. The goal of these functions is to make it +easier for tutors to facilitate ScriptCraft for use by students in a +classroom environment. Although granting ScriptCraft access to +students on a shared server is potentially risky (Students can +potentially abuse it), it is slighlty less risky than granting +operator privileges to each student. (Enterprising students will +quickly realise how to grant themselves and others operator privileges +once they have access to ScriptCraft). + +The goal of this module is not so much to enforce restrictions +(security or otherwise) but to make it easier for tutors to setup a +shared server so students can learn Javascript. When scripting is +turned on, every player who joins the server will have a dedicated +directory into which they can save scripts. All scripts in such +directories are automatically watched and loaded into a global +variable named after the player. + +So for example, if player 'walterh' joins the server, a `walterh` +global variable is created. If a file `greet.js` with the following +content is dropped into the `scriptcraft/players/walterh` +directory... + +```javascript +exports.hi = function( player ){ + echo( player, 'Hi ' + player.name); +}; +``` + +... then it can be invoked like this: `/js walterh.hi( self )` . This +lets every player/student create their own functions without having +naming collisions. + +It's strongly recommended that the +`scriptcraft/players/` directory is shared so that +others can connect to it and drop .js files into their student +directories. On Ubuntu, select the folder in Nautilus (the default +file browser) then right-click and choose *Sharing Options*, check the +*Share this folder* checkbox and the *Allow others to create and +delete files* and *Guest access* checkboxes. Click *Create Share* +button to close the sharing options dialog. Students can then access +the shared folder as follows... + + * Windows: Open Explorer, Go to \\{serverAddress}\players\ + * Macintosh: Open Finder, Go to smb://{serverAddress}/players/ + * Linux: Open Nautilus, Go to smb://{serverAddress}/players/ + +... where {serverAddress} is the ip address of the server (this is +displayed to whoever invokes the classroom.allowScripting() function.) + +### jsp classroom command +The `jsp classroom` command makes it easy for tutors to turn on or off +classroom mode. This command can only be used by server operators. To +turn on classroom mode (enable scripting for all players): + + jsp classroom on + +To turn off classroom mode (disable scripting for all players): + + jsp classroom off + +The `jsp classroom` command is provided as an easier way to turn on or +off classroom mode. This should be used in preference to the +classroom.allowScripting() function which is provided only for +programmatically enabling or disabling classroom mode. + +### classroom.allowScripting() function + +Allow or disallow anyone who connects to the server (or is already +connected) to use ScriptCraft. This function is preferable to granting 'ops' privileges +to every student in a Minecraft classroom environment. + +Whenever any file is added/edited or removed from any of the players/ +directories the contents are automatically reloaded. This is to +facilitate quick turnaround time for students getting to grips with +Javascript. + +#### Parameters + + * canScript : true or false + +#### Example + +To allow all players (and any players who connect to the server) to +use the `js` and `jsp` commands... + + /js classroom.allowScripting( true, self ) + +To disallow scripting (and prevent players who join the server from using the commands)... + + /js classroom.allowScripting( false, self ) + +Only ops users can run the classroom.allowScripting() function - this is so that students +don't try to bar themselves and each other from scripting. + +***/ +var store = persist('classroom', { enableScripting: false }), + File = java.io.File; + +function revokeScripting ( player ) { + if (__plugin.bukkit){ + foreach( player.getEffectivePermissions(), function( perm ) { + if ( (''+perm.permission).indexOf( 'scriptcraft.' ) == 0 ) { + if ( perm.attachment ) { + perm.attachment.remove(); + } + } + }); + } + if (__plugin.canary){ + // + var Canary = Packages.net.canarymod.Canary; + Canary.permissionManager().removePlayerPermission('scriptcraft.evaluate',player); + } + var playerName = '' + player.name; + playerName = playerName.replace(/[^a-zA-Z0-9_\-]/g,''); + var playerDir = new File( playersDir + playerName ); + unwatchDir( playerDir ); +} +var autoloadTime = {}; + +var playerEventHandlers = {}; + +function reloadPlayerModules( playerContext, playerDir ){ + /* + wph 20150118 first unregister any event handlers registered by the player + */ + var playerDirPath = ''+ playerDir.getAbsolutePath(); + var eventHandlers = playerEventHandlers[playerDirPath]; + if (eventHandlers){ + for (var i = 0;i < eventHandlers.length; i++){ + eventHandlers[i].unregister(); + } + eventHandlers.length = 0; + } else { + playerEventHandlers[playerDirPath] = []; + eventHandlers = playerEventHandlers[playerDirPath]; + } + /* + override events.on() so that the listener is stored here so it can be unregistered. + */ + var oldOn = events.on; + var newOn = function( eventType, fn, priority){ + var handler = oldOn(eventType, fn, priority); + eventHandlers.push(handler); + }; + events.on = newOn; + autoload( playerContext, playerDir, { cache: false }); + events.on = oldOn; +} +function grantScripting( player ) { + console.log('Enabling scripting for player ' + player.name); + var playerName = '' + player.name; + playerName = playerName.replace(/[^a-zA-Z0-9_\-]/g,''); + + var playerDir = new File( playersDir + playerName ); + if (!playerDir.exists()) { + playerDir.mkdirs(); + var exampleJs = "//Try running this function from Minecraft with: /js $username.hi( self )\n" + + "//Remember to use your real username instead of $username!\n" + + "//So if you had username 'walterh', you would run: /js walterh.hi( self )\n" + + "exports.hi = function( player ){\n" + + "\techo( player, 'Hi ' + player.name);\n" + + "};" + createFile(playerDir, 'greet.js', exampleJs); + } + + if (__plugin.bukkit){ + player.addAttachment( __plugin, 'scriptcraft.*', true ); + } + if (__plugin.canary){ + player.permissionProvider.addPermission('scriptcraft.evaluate',true); + } + var playerContext = {}; + reloadPlayerModules( playerContext, playerDir ); + global[playerName] = playerContext; + watchDir( playerDir, function( changedDir ){ + var currentTime = new java.util.Date().getTime(); + //this check is here because this callback might get called multiple times for the watch interval + //one call for the file change and another for directory change + //(this happens only in Linux because in Windows the folder lastModifiedTime is not changed) + if (currentTime - autoloadTime[playerName]>1000 ) { + reloadPlayerModules(playerContext, playerDir ); + } + autoloadTime[playerName] = currentTime; + }); + + function createFile(fileDir, fileName, fileContent) { + var out = new java.io.PrintWriter(new File(fileDir, fileName)); + out.println(fileContent); + out.close(); + } + +/* + echo( player, 'Create your own minecraft mods by adding javascript (.js) files'); + echo( player, ' Windows: Open Explorer, go to \\\\' + serverAddress + '\\players\\' + player.name); + echo( player, ' Macintosh: Open Finder, Go to smb://' + serverAddress + '/players/' + player.name); + echo( player, ' Linux: Open Nautilus, Go to smb://' + serverAddress + '/players/' + player.name); +*/ + +} + +var _classroom = { + allowScripting: function (/* boolean: true or false */ canScript, sender ) { + sender = utils.player(sender); + if ( !sender ) { + console.log( 'Attempt to set classroom scripting without credentials' ); + console.log( 'classroom.allowScripting(boolean, sender)' ); + return; + } + /* + only operators should be allowed run this function + */ + if ( !isOp(sender) ) { + console.log( 'Attempt to set classroom scripting without credentials: ' + sender.name ); + echo( sender, 'Only operators can use this function'); + return; + } + utils.players(function(player){ + if (!isOp(player)){ + canScript ? grantScripting(player) : revokeScripting(player); + } + }); + store.enableScripting = canScript; + + echo( sender, 'Scripting turned ' + ( canScript ? 'on' : 'off' ) + + ' for all players on server ' + serverAddress); + } +}; + +if (__plugin.canary){ + events.connection( function( event ) { + if ( store.enableScripting ) { + grantScripting(event.player); + } + }, 'CRITICAL'); +} else { + events.playerJoin( function( event ) { + if ( store.enableScripting ) { + grantScripting(event.player); + } + }, 'HIGHEST'); +} +module.exports = _classroom; diff --git a/src/main/js/modules/drone/arc.js b/src/main/js/modules/drone/arc.js new file mode 100644 index 0000000..6658a37 --- /dev/null +++ b/src/main/js/modules/drone/arc.js @@ -0,0 +1,267 @@ +/*global require*/ +'use strict'; +/************************************************************************ +### Drone.arc() method + +The arc() method can be used to create 1 or more 90 degree arcs in the +horizontal or vertical planes. This method is called by cylinder() and +cylinder0() and the sphere() and sphere0() methods. + +#### Parameters + +arc() takes a single parameter - an object with the following named properties... + + * radius - The radius of the arc. + * blockType - The type of block to use - this is the block Id only (no meta). See [Data Values][dv]. + * meta - The metadata value. See [Data Values][dv]. + * orientation (default: 'horizontal' ) - the orientation of the arc - can be 'vertical' or 'horizontal'. + * stack (default: 1 ) - the height or length of the arc (depending on the orientation - if orientation is horizontal then this parameter refers to the height, if vertical then it refers to the length ). + * strokeWidth (default: 1 ) - the width of the stroke (how many blocks) - if drawing nested arcs it's usually a good idea to set strokeWidth to at least 2 so that there are no gaps between each arc. The arc method uses a [bresenham algorithm][bres] to plot points along the circumference. + * fill - If true (or present) then the arc will be filled in. + * quadrants (default: `{topleft:true,topright:true,bottomleft:true,bottomright:true}` - An object with 4 properties indicating which of the 4 quadrants of a circle to draw. If the quadrants property is absent then all 4 quadrants are drawn. + +#### Examples + +To draw a 1/4 circle (top right quadrant only) with a radius of 10 and +stroke width of 2 blocks ... + + arc({blockType: blocks.iron, + meta: 0, + radius: 10, + strokeWidth: 2, + quadrants: { topright: true }, + orientation: 'vertical', + stack: 1, + fill: false + } ); + +![arc example 1](img/arcex1.png) + +[bres]: http://en.wikipedia.org/wiki/Midpoint_circle_algorithm +[dv]: http://www.minecraftwiki.net/wiki/Data_values + +***/ +/* + do the bresenham thing + */ +function bresenham( x0,y0,radius, setPixel, quadrants ) { + // + // credit: Following code is copied almost verbatim from + // http://en.wikipedia.org/wiki/Midpoint_circle_algorithm + // Bresenham's circle algorithm + // + var f = 1 - radius; + var ddF_x = 1; + var ddF_y = -2 * radius; + var x = 0; + var y = radius; + var defaultQuadrants = {topleft: true, topright: true, bottomleft: true, bottomright: true}; + quadrants = quadrants?quadrants:defaultQuadrants; + /* + II | I + ------------ + III | IV + */ + if ( quadrants.topleft || quadrants.topright ) + setPixel(x0, y0 + radius ); // quadrant I/II topmost + if ( quadrants.bottomleft || quadrants.bottomright ) + setPixel(x0, y0 - radius ); // quadrant III/IV bottommost + if ( quadrants.topright || quadrants.bottomright ) + setPixel(x0 + radius, y0 ); // quadrant I/IV rightmost + if ( quadrants.topleft || quadrants.bottomleft ) + setPixel(x0 - radius, y0 ); // quadrant II/III leftmost + + while ( x < y ) { + if(f >= 0 ) { + y--; + ddF_y += 2; + f += ddF_y; + } + x++; + ddF_x += 2; + f += ddF_x; + if ( quadrants.topright ) { + setPixel(x0 + x, y0 + y ); // quadrant I + setPixel(x0 + y, y0 + x ); // quadrant I + } + if ( quadrants.topleft ) { + setPixel(x0 - x, y0 + y ); // quadrant II + setPixel(x0 - y, y0 + x ); // quadrant II + } + if ( quadrants.bottomleft ) { + setPixel(x0 - x, y0 - y ); // quadrant III + setPixel(x0 - y, y0 - x ); // quadrant III + } + if ( quadrants.bottomright ) { + setPixel(x0 + x, y0 - y ); // quadrant IV + setPixel(x0 + y, y0 - x ); // quadrant IV + } + } +}; + +function getStrokeDir( x,y ) { + var absY = Math.abs(y ); + var absX = Math.abs(x ); + var strokeDir = 0; + if ( y > 0 && absY >= absX ) + strokeDir = 0 ; //down + else if ( y < 0 && absY >= absX ) + strokeDir = 1 ; // up + else if ( x > 0 && absX >= absY ) + strokeDir = 2 ; // left + else if ( x < 0 && absX >= absY ) + strokeDir = 3 ; // right + return strokeDir; +}; + +/* + The daddy of all arc-related API calls - + if you're drawing anything that bends it ends up here. + */ +function arcImpl( params ) { + var drone = params.drone; + var orientation = params.orientation?params.orientation:'horizontal'; + var quadrants = params.quadrants?params.quadrants:{ + topright:1, + topleft:2, + bottomleft:3, + bottomright:4 + }; + var stack = params.stack?params.stack:1; + var radius = params.radius; + var strokeWidth = params.strokeWidth?params.strokeWidth:1; + drone.chkpt('arc2' ); + var x0, y0, gotoxy,setPixel; + + if ( orientation == 'horizontal' ) { + gotoxy = function( x,y ) { return drone.right(x ).fwd(y );}; + drone.right(radius ).fwd(radius ).chkpt('center' ); + switch ( drone.dir ) { + case 0: // east + case 2: // west + x0 = drone.z; + y0 = drone.x; + break; + case 1: // south + case 3: // north + x0 = drone.x; + y0 = drone.z; + } + setPixel = function( x, y ) { + x = ( x-x0 ); + y = ( y-y0 ); + if ( params.fill ) { + // wph 20130114 more efficient esp. for large cylinders/spheres + if ( y < 0 ) { + drone + .fwd( y ).right( x ) + .cuboidX( params.blockType, params.meta, 1, stack, Math.abs( y * 2 ) + 1 ) + .back( y ).left( x ); + } + }else{ + if ( strokeWidth == 1 ) { + gotoxy(x,y ) + .cuboidX( params.blockType, params.meta, + 1, // width + stack, // height + strokeWidth // depth + ) + .move('center' ); + } else { + var strokeDir = getStrokeDir( x, y ); + var width = 1, depth = 1; + switch ( strokeDir ) { + case 0: // down + y = y-( strokeWidth - 1 ); + depth = strokeWidth; + break; + case 1: // up + depth = strokeWidth; + break; + case 2: // left + width = strokeWidth; + x = x-(strokeWidth-1 ); + break; + case 3: // right + width = strokeWidth; + break; + } + gotoxy( x, y ) + .cuboidX( params.blockType, params.meta, width, stack, depth ) + .move( 'center' ); + + } + } + }; + }else{ + // vertical + gotoxy = function( x,y ) { return drone.right(x ).up(y );}; + drone.right(radius ).up(radius ).chkpt('center' ); + switch ( drone.dir ) { + case 0: // east + case 2: // west + x0 = drone.z; + y0 = drone.y; + break; + case 1: // south + case 3: // north + x0 = drone.x; + y0 = drone.y; + } + setPixel = function( x, y ) { + x = ( x - x0 ); + y = ( y - y0 ); + if ( params.fill ) { + // wph 20130114 more efficient esp. for large cylinders/spheres + if ( y < 0 ) { + drone + .up( y ).right( x ) + .cuboidX( params.blockType, params.meta, 1, Math.abs( y * 2 ) + 1, stack ) + .down( y ).left( x ); + } + }else{ + if ( strokeWidth == 1 ) { + gotoxy( x, y ) + .cuboidX( params.blockType, params.meta, strokeWidth, 1, stack ) + .move( 'center' ); + }else{ + var strokeDir = getStrokeDir( x,y ); + var width = 1, height = 1; + switch ( strokeDir ) { + case 0: // down + y = y - ( strokeWidth - 1 ); + height = strokeWidth; + break; + case 1: // up + height = strokeWidth; + break; + case 2: // left + width = strokeWidth; + x = x - ( strokeWidth - 1 ); + break; + case 3: // right + width = strokeWidth; + break; + } + gotoxy(x,y ) + .cuboidX(params.blockType, params.meta, width, height, stack ) + .move('center' ); + + } + } + }; + } + /* + setPixel assumes a 2D plane - need to put a block along appropriate plane + */ + bresenham(x0,y0,radius,setPixel,quadrants ); + + params.drone.move('arc2' ); +}; + +module.exports = function(Drone){ + Drone.extend(function arc( params ) { + params.drone = this; + arcImpl( params ); + }); +}; diff --git a/src/main/js/modules/drone/bed.js b/src/main/js/modules/drone/bed.js new file mode 100644 index 0000000..98442c6 --- /dev/null +++ b/src/main/js/modules/drone/bed.js @@ -0,0 +1,55 @@ +'use strict'; +/*global require, Packages, __plugin, module*/ +var blocks = require('blocks'); +/************************************************************************ +### Drone.bed() method + +Creates a bed. The foot of the bed will be at the drone's location and +the head of the bed will extend away from the drone. + +#### Example +To create a bed at the in-game prompt, look at a block then type: + +```javascript +/js bed() +``` + +Like most Drone methods, this returns the drone so it can be chained like so: + +```javascript +this + .fwd(3) + .bed() + .back(3) +``` +***/ +var bedDirections = { + 0:3, // east + 1:0, // south + 2:1, // west + 3:2 // north +}; +module.exports = function(Drone){ + + Drone.extend( function bed(){ + this.then(function(){ + var foot = this.setBlock(blocks.bed, bedDirections[this.dir], 0,0,0, false); + var head = this.setBlock(blocks.bed, bedDirections[this.dir] + 8, 0,0,1, false); + if (Drone.bountiful){ + var prop = require('blockhelper').property; + var BedHalf = Packages.net.canarymod.api.world.blocks.properties.helpers.BedProperties.Half; + prop(foot) + .set('facing',this.dir) + .set('part', BedHalf.FOOT); + prop(head) + .set('facing',this.dir) + .set('part', BedHalf.HEAD); + } + if (__plugin.canary){ + foot.update(); + head.update(); + } + }); + }); +}; + diff --git a/src/main/js/modules/drone/blocktype.js b/src/main/js/modules/drone/blocktype.js new file mode 100644 index 0000000..0d9160e --- /dev/null +++ b/src/main/js/modules/drone/blocktype.js @@ -0,0 +1,412 @@ +var blocks = require('blocks'); + +/************************************************************************ +### Drone.blocktype() method + +Creates the text out of blocks. Useful for large-scale in-game signs. + +#### Parameters + + * message - The message to create - (use `\n` for newlines) + * foregroundBlock (default: black wool) - The block to use for the foreground + * backgroundBlock (default: none) - The block to use for the background + +#### Example + +To create a 2-line high message using glowstone... + + blocktype('Hello\nWorld', blocks.glowstone); + +![blocktype example][imgbt1] + +[imgbt1]: img/blocktype1.png + +***/ + +var bitmaps = { + raw: { + '0':' ### '+ + ' # # '+ + ' # # '+ + ' # # '+ + ' ### ', + + '1':' # '+ + ' ## '+ + ' # '+ + ' # '+ + ' ### ', + + '2':' ### '+ + ' # '+ + ' ### '+ + ' # '+ + ' ### ', + + '3':' ### '+ + ' # '+ + ' ## '+ + ' # '+ + ' ### ', + + '4':' # '+ + ' ## '+ + ' # # '+ + ' ### '+ + ' # ', + + '5':' ### '+ + ' # '+ + ' ### '+ + ' # '+ + ' ### ', + + '6':' ### '+ + ' # '+ + ' ### '+ + ' # # '+ + ' ### ', + + '7':' ### '+ + ' # '+ + ' # '+ + ' # '+ + ' # ', + + '8':' ### '+ + ' # # '+ + ' ### '+ + ' # # '+ + ' ### ', + + '9':' ### '+ + ' # # '+ + ' ### '+ + ' # '+ + ' ### ', + + 'a':' ### '+ + ' # # '+ + ' ### '+ + ' # # '+ + ' # # ', + + 'b':' ## '+ + ' # # '+ + ' ## '+ + ' # # '+ + ' ## ', + + 'c':' ## '+ + ' # '+ + ' # '+ + ' # '+ + ' ## ', + + 'd':' ## '+ + ' # # '+ + ' # # '+ + ' # # '+ + ' ## ', + + 'e':' ### '+ + ' # '+ + ' ## '+ + ' # '+ + ' ### ', + + 'f':' ### '+ + ' # '+ + ' ## '+ + ' # '+ + ' # ', + + 'g':' ### '+ + ' # '+ + ' # '+ + ' # # '+ + ' ### ', + + 'h':' # # '+ + ' # # '+ + ' ### '+ + ' # # '+ + ' # # ', + + 'i':' ### '+ + ' # '+ + ' # '+ + ' # '+ + ' ### ', + + 'j':' ### '+ + ' # '+ + ' # '+ + ' # '+ + ' # ', + + 'k':' # '+ + ' # # '+ + ' ## '+ + ' # # '+ + ' # # ', + + 'l':' # '+ + ' # '+ + ' # '+ + ' # '+ + ' ### ', + + 'm':' # # '+ + ' ### '+ + ' # # '+ + ' # # '+ + ' # # ', + + 'n':' ## '+ + ' # # '+ + ' # # '+ + ' # # '+ + ' # # ', + + 'o':' # '+ + ' # # '+ + ' # # '+ + ' # # '+ + ' # ', + + 'p':' ### '+ + ' # # '+ + ' ### '+ + ' # '+ + ' # ', + + 'q':' ### '+ + ' # # '+ + ' # # '+ + ' ### '+ + ' # ', + + 'r':' ## '+ + ' # # '+ + ' ## '+ + ' # # '+ + ' # # ', + + 's':' ## '+ + ' # '+ + ' ### '+ + ' # '+ + ' ## ', + + 't':' ### '+ + ' # '+ + ' # '+ + ' # '+ + ' # ', + + 'u':' # # '+ + ' # # '+ + ' # # '+ + ' # # '+ + ' ### ', + + 'v':' # # '+ + ' # # '+ + ' # # '+ + ' # # '+ + ' # ', + + 'w':' # # '+ + ' # # '+ + ' # # '+ + ' ### '+ + ' # # ', + + 'x':' # # '+ + ' # # '+ + ' # '+ + ' # # '+ + ' # # ', + + 'y':' # # '+ + ' # # '+ + ' # # '+ + ' # '+ + ' # ', + + 'z':' ### '+ + ' # '+ + ' # '+ + ' # '+ + ' ### ', + + '!':' # '+ + ' # '+ + ' # '+ + ' '+ + ' # ', + + ':':' '+ + ' # '+ + ' '+ + ' # '+ + ' ', + + ';':' '+ + ' # '+ + ' '+ + ' # '+ + ' # ', + + ',':' '+ + ' '+ + ' '+ + ' # '+ + ' # ', + + '/':' # '+ + ' # '+ + ' # '+ + ' # '+ + ' # ', + + '+':' '+ + ' # '+ + ' ### '+ + ' # '+ + ' ', + + '-':' '+ + ' '+ + ' ### '+ + ' '+ + ' ', + + '.':' '+ + ' '+ + ' '+ + ' '+ + ' # ', + + "'":' # '+ + ' # '+ + ' '+ + ' '+ + ' ', + + '(': ' # '+ + ' # '+ + ' # '+ + ' # '+ + ' # ', + + ')': ' # '+ + ' # '+ + ' # '+ + ' # '+ + ' # ', + + ' ':' '+ + ' '+ + ' '+ + ' '+ + ' ' + }, + computed: {} +}; +/* + wph 20130121 compute the width, and x,y coords of pixels ahead of time +*/ +var c, + bits, + width, + bmInfo, + j; +for ( c in bitmaps.raw ) { + bits = bitmaps.raw[c]; + width = bits.length/5; + bmInfo = { width: width, pixels:[] }; + bitmaps.computed[c] = bmInfo; + for ( j = 0; j < bits.length; j++ ) { + if ( bits.charAt(j) != ' ' ) { + bmInfo.pixels.push( [ + j % width, + Math.ceil( j / width ) + ] ); + } + } +} +function blocktype( message, fg, bg, immediate ) { + + var bmfg, + bmbg, + lines, + lineCount, + h, + line, + i, + x, + y, + ch, + bits, + charWidth, + j; + + this.chkpt('blocktext'); + + if ( typeof fg == 'undefined' ) { + fg = blocks.wool.black; + } + + bmfg = this.getBlockIdAndMeta( fg ); + bmbg = null; + if ( typeof bg != 'undefined' ) { + bmbg = this.getBlockIdAndMeta( bg ); + } + lines = (''+ message).split( '\n' ); + lineCount = lines.length; + + for ( h = 0; h < lineCount; h++) { + + line = lines[h]; + line = line.toLowerCase().replace( /[^0-9a-z \.\-\+\/\;\'\:\!]/g, '' ); + this.up( 7 * ( lineCount - ( h + 1 ) ) ); + + for ( i =0; i < line.length; i++) { + + ch = line.charAt( i ); + bits = bitmaps.computed[ ch ]; + + if ( typeof bits == 'undefined' ) { + bits = bitmaps.computed[' ']; + } + charWidth = bits.width; + + if ( typeof bg != 'undefined' ) { + this.cuboidX( bmbg[0], bmbg[1], charWidth, 7, 1 , immediate); + } + + for ( j = 0; j < bits.pixels.length; j++ ) { + + this.chkpt( 'btbl' ); + x = bits.pixels[ j ][ 0 ]; + y = bits.pixels[ j ][ 1] ; + this.up( 6 - y ).right( x ).cuboidX( bmfg[ 0 ], bmfg[ 1 ], 1, 1, 1, immediate); + this.move( 'btbl' ); + + } + this.right( charWidth - 1 ); + + } + this.move( 'blocktext' ); + } + + return this.move( 'blocktext' ); +} +module.exports = function(Drone){ + Drone.extend(blocktype); +}; + + + diff --git a/src/main/js/modules/drone/copypaste.js b/src/main/js/modules/drone/copypaste.js new file mode 100644 index 0000000..d6dbb64 --- /dev/null +++ b/src/main/js/modules/drone/copypaste.js @@ -0,0 +1,159 @@ +'use strict'; +/*global require, module*/ + +/************************************************************************ +### Copy & Paste using Drone + +A drone can be used to copy and paste areas of the game world. + +#### Deprecated +As of January 10 2015 the copy-paste functions in Drone are no longer +supported. Copy/Paste is: + +1. Difficult to do correctly in a way which works for both Minecraft 1.7 and 1.8 + due to how blocks changed in 1.8 +2. Not aligned with the purpose of ScriptCraft's Drone module which is to provide + a simple set of functions for scripting and in-game building. + +### Drone.copy() method + +Copies an area so it can be pasted elsewhere. The name can be used for +pasting the copied area elsewhere... + +#### Parameters + + * name - the name to be given to the copied area (used by `paste`) + * width - the width of the area to copy + * height - the height of the area to copy + * length - the length of the area (extending away from the drone) to copy + +#### Example + + drone.copy('somethingCool',10,5,10 ).right(12 ).paste('somethingCool' ); + +### Drone.paste() method + +Pastes a copied area to the current location. + +#### Example + +To copy a 10x5x10 area (using the drone's coordinates as the starting +point) into memory. the copied area can be referenced using the name +'somethingCool'. The drone moves 12 blocks right then pastes the copy. + + drone.copy('somethingCool',10,5,10 ) + .right(12 ) + .paste('somethingCool' ); + +***/ +var clipBoard = {}; + +function paste( name, immediate ){ + console.warn('Drone copy/paste is no longer in active development'); + var Drone = this.constructor; + var ccContent = clipBoard[name]; + if (ccContent == undefined){ + console.warn('Nothing called ' + name + ' in clipboard!'); + return; + } + var srcBlocks = ccContent.blocks; + var srcDir = ccContent.dir; // direction player was facing when copied. + var dirOffset = (4 + (this.dir - srcDir ) ) %4; + + this.traverseWidth(srcBlocks.length,function( ww ) { + var h = srcBlocks[ww].length; + this.traverseHeight(h,function( hh ) { + var d = srcBlocks[ww][hh].length; + this.traverseDepth(d,function( dd ) { + var b = srcBlocks[ww][hh][dd], + cb = b.type, + md = b.data, + newDir, + dir, + a, + c, + len; + // + // need to adjust blocks which face a direction + // + switch ( cb ) { + // + // doors + // + case 64: // wood + case 71: // iron + // top half of door doesn't need to change + if ( md < 8 ) { + md = (md + dirOffset ) % 4; + } + break; + // + // stairs + // + case 53: // oak + case 67: // cobblestone + case 108: // red brick + case 109: // stone brick + case 114: // nether brick + case 128: // sandstone + case 134: // spruce + case 135: // birch + case 136: // junglewood + dir = md & 0x3; + a = Drone.PLAYER_STAIRS_FACING; + len = a.length; + for ( c = 0; c < len; c++ ) { + if ( a[c] == dir ) { + break; + } + } + c = (c + dirOffset ) %4; + newDir = a[c]; + md = (md >>2<<2 ) + newDir; + break; + // + // signs , ladders etc + // + case 23: // dispenser + case 54: // chest + case 61: // furnace + case 62: // burning furnace + case 65: // ladder + case 68: // wall sign + a = Drone.PLAYER_SIGN_FACING; + len = a.length; + for ( c=0; c < len; c++ ) { + if ( a[c] == md ) { + break; + } + } + c = (c + dirOffset ) %4; + newDir = a[c]; + md = newDir; + break; + } + this.setBlock(cb,md); + } ); + } ); + } ); +} +function copy( name, w, h, d ) { + console.warn('Drone copy/paste is no longer in active development'); + var ccContent = []; + this.traverseWidth(w,function( ww ) { + ccContent.push([] ); + this.traverseHeight(h,function( hh ) { + ccContent[ww].push([] ); + this.traverseDepth(d,function( dd ) { + var b = this.getBlock(); + ccContent[ww][hh][dd] = {type:b.getTypeId(), data:b.data}; + } ); + } ); + } ); + clipBoard[name] = {dir: this.dir, blocks: ccContent}; +} + +module.exports = function(Drone){ + Drone.extend( copy ); + Drone.extend( paste ); +}; diff --git a/src/main/js/modules/drone/cylinders.js b/src/main/js/modules/drone/cylinders.js new file mode 100644 index 0000000..cd5094e --- /dev/null +++ b/src/main/js/modules/drone/cylinders.js @@ -0,0 +1,76 @@ +'use strict'; + +/************************************************************************** +### Drone.cylinder() method + +A convenience method for building cylinders. Building begins radius blocks to the right and forward. + +#### Parameters + + * block - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` + * radius + * height + +#### Example + +To create a cylinder of Iron 7 blocks in radius and 1 block high... + + cylinder(blocks.iron, 7 , 1); + +![cylinder example](img/cylinderex1.png) + +### Drone.cylinder0() method + +A version of cylinder that hollows out the middle. + +#### Example + +To create a hollow cylinder of Iron 7 blocks in radius and 1 block high... + + cylinder0(blocks.iron, 7, 1); + +![cylinder0 example](img/cylinder0ex1.png) + +***/ + +function cylinder0( block,radius,height,exactParams ) { + var arcParams = { + radius: radius, + fill: false, + orientation: 'horizontal', + stack: height + }; + + if ( exactParams ) { + for ( var p in exactParams ) { + arcParams[p] = exactParams[p]; + } + }else{ + var md = this.getBlockIdAndMeta(block ); + arcParams.blockType = md[0]; + arcParams.meta = md[1]; + } + return this.arc(arcParams ); +}; +function cylinder( block,radius,height,exactParams ) { + var arcParams = { + radius: radius, + fill: true, + orientation: 'horizontal', + stack: height + }; + + if ( exactParams ) { + arcParams.blockType = exactParams.blockType; + arcParams.meta = exactParams.meta; + }else{ + var md = this.getBlockIdAndMeta(block ); + arcParams.blockType = md[0]; + arcParams.meta = md[1]; + } + return this.arc(arcParams ); +}; +module.exports = function(Drone){ + Drone.extend(cylinder0 ); + Drone.extend(cylinder ); +}; diff --git a/src/main/js/modules/drone/doors.js b/src/main/js/modules/drone/doors.js new file mode 100644 index 0000000..54f5229 --- /dev/null +++ b/src/main/js/modules/drone/doors.js @@ -0,0 +1,101 @@ +/*global module*/ +'use strict'; +/************************************************************************* +### Drone.door() method + +create a door - if a parameter is supplied an Iron door is created otherwise a wooden door is created. + +#### Parameters + + * doorType (optional - default wood) - If a parameter is provided then the door is Iron. + +#### Example + +To create a wooden door at the crosshairs/drone's location... + + var drone = new Drone(self); + drone.door(); + +To create an iron door... + + drone.door( blocks.door_iron ); + +![iron door](img/doorex1.png) + +### Drone.door_iron() method + +create an Iron door. + +### Drone.door2() method + +Create double doors (left and right side) + +#### Parameters + + * doorType (optional - default wood) - If a parameter is provided then the door is Iron. + +#### Example + +To create double-doors at the cross-hairs/drone's location... + + drone.door2(); + +![double doors](img/door2ex1.png) + +### Drone.door2_iron() method + +Create double iron doors + +***/ + +var blocks = require('blocks'); +/*global require, Packages, __plugin*/ +function door( doorMaterial, hinge) { + if ( typeof doorMaterial == 'undefined' ) { + doorMaterial = blocks.door_wood; // wood + } + if (typeof hinge == 'undefined') { + hinge = 'left'; + } + var Drone = this.constructor; + this.then(function(){ + var lower = this.setBlock(doorMaterial, this.dir, 0, 0, 0, false); + var upper = this.setBlock(doorMaterial, hinge=='left' ? 8 : 9, 0,1,0, false); + if (Drone.bountiful){ + var DoorHalf = Packages.net.minecraft.block.BlockDoor.EnumDoorHalf, + HingePosition = Packages.net.minecraft.block.BlockDoor.EnumHingePosition, + prop = require('blockhelper').property; + prop(lower) + .set('facing', this.dir) + .set('half', DoorHalf.LOWER ); + prop(upper) + .set('hinge', hinge == 'left' ? HingePosition.LEFT: HingePosition.RIGHT) + .set('half', DoorHalf.UPPER); + } + if (__plugin.canary){ + lower.update(); + upper.update(); + } + }); +} +module.exports = function(Drone){ + Drone.extend( door ); + + Drone.extend( function door_iron( ) { + this.door(blocks.door_iron); + } ); + + Drone.extend( function door2( doorMaterial ) { + if ( typeof doorMaterial == 'undefined' ) { + doorMaterial = blocks.door_wood; + } + this + .door( doorMaterial, 'left') + .right() + .door( doorMaterial, 'right') + .left(); + } ); + Drone.extend( function door2_iron( ) { + this.door2( blocks.door_iron ); + } ); +}; diff --git a/src/main/js/modules/drone/firework.js b/src/main/js/modules/drone/firework.js new file mode 100644 index 0000000..764750a --- /dev/null +++ b/src/main/js/modules/drone/firework.js @@ -0,0 +1,21 @@ +'use strict'; +/*global module, require*/ +var fireworks = require('fireworks'); +/************************************************************************* +### Drone.firework() method + +Launches a firework at the drone's location. + +#### Example + +To launch a firework: + + var drone = new Drone(self); + drone.firework(); + +***/ +module.exports = function(Drone){ + Drone.extend( function firework( ) { + fireworks.firework( this.getLocation() ); + }); +}; diff --git a/src/main/js/modules/drone/garden.js b/src/main/js/modules/drone/garden.js new file mode 100644 index 0000000..9baf227 --- /dev/null +++ b/src/main/js/modules/drone/garden.js @@ -0,0 +1,49 @@ +/*global module, require*/ +'use strict'; +/************************************************************************ +### Drone.garden() method + +places random flowers and long grass (similar to the effect of placing bonemeal on grass) + +#### Parameters + + * width - the width of the garden + * length - how far from the drone the garden extends + +#### Example + +To create a garden 10 blocks wide by 5 blocks long... + + garden(10,5); + +![garden example](img/gardenex1.png) + +***/ +var blocks = require('blocks'); + +function garden( width, depth ) { + if ( typeof width == 'undefined' ) { + width = 10; + } + if ( typeof depth == 'undefined' ) { + depth = width; + } + // make sure grass is present first + this + .box( blocks.grass, width, 1, depth ) + .up(); + + // make flowers more common than long grass + var dist = { }; + dist[blocks.rose] = 3; + dist[blocks.dandelion] = 3; + dist[blocks.grass_tall] = 2; + dist[blocks.air] = 1; + + this + .rand( dist, width, 1, depth, false /* don't overwrite */ ) + .down(); +} +module.exports = function(Drone){ + Drone.extend(garden); +}; diff --git a/src/main/js/modules/drone/index.js b/src/main/js/modules/drone/index.js new file mode 100644 index 0000000..b4d405c --- /dev/null +++ b/src/main/js/modules/drone/index.js @@ -0,0 +1,922 @@ +'use strict'; +/*global __plugin, require, org, setTimeout, addUnloadHandler, global, Packages, server, module*/ +var utils = require('utils'), + blocks = require('blocks'), + THOUSAND = 1000, + MILLION = THOUSAND * THOUSAND; + + +/********************************************************************* +## Drone Plugin + +The Drone is a convenience class for building. + +It uses a fluent interface which means all of the Drone's methods return `this` and can be chained together like so... + + var theDrone = new Drone(self); + theDrone.up().left().box(blocks.oak).down().fwd(3).cylinder0(blocks.lava,8); + +### Constructing a Drone Object + +Drones can be created in any of the following ways... + + 1. Calling any one of the methods listed below will return a Drone object. For example... + + var d = box( blocks.oak ) + + ... creates a 1x1x1 wooden block at the cross-hairs or player's location and returns a Drone object. This might look odd (if you're familiar with Java's Object-dot-method syntax) but all of the Drone class's methods are also global functions that return new Drone objects. This is short-hand for creating drones and is useful for playing around with Drones at the in-game command prompt. It's shorter than typing ... + + var d = new Drone(self).box( blocks.oak ) + + ... All of the Drone's methods return `this` so you can chain operations together like this... + + var d = box( blocks.oak ) + .up() + .box( blocks.oak ,3,1,3) + .down() + .fwd(2) + .box( blocks.oak ) + .turn() + .fwd(2) + .box( blocks.oak ) + .turn() + .fwd(2) + .box( blocks.oak ); + + 2. Using the following form... + + d = new Drone(self) + + ...will create a new Drone taking the current player as the parameter. If the player's cross-hairs are pointing at a block at the time then, that block's location becomes the drone's starting point. If the cross-hairs are _not_ pointing at a block, then the drone's starting location will be 2 blocks directly in front of the player. TIP: Building always happens right and front of the drone's position... + + Plan View: + + ^ + | + | + D----> + + For convenience you can use a _corner stone_ to begin building. The corner stone should be located just above ground level. If the cross-hair is point at or into ground level when you create a new Drone() with either a player or location given as a parameter, then building begins at the location the player was looking at or at the location. You can get around this by pointing at a 'corner stone' just above ground level or alternatively use the following statement... + + d = new Drone(self).up(); + + ... which will move the drone up one block as soon as it's created. + + ![corner stone](img/cornerstone1.png) + + 3. Or by using the following form... + + d = new Drone(x,y,z,direction,world); + + This will create a new Drone at the location you specified using x, y, z In minecraft, the X axis runs west to east and the Z axis runs north to south. The direction parameter says what direction you want the drone to face: 0 = east, 1 = south, 2 = west, 3 = north. If the direction parameter is omitted, the player's direction is used instead. Both the `direction` and `world` parameters are optional. + + 4. Create a new Drone based on a Location object... + + d = new Drone(location); + + This is useful when you want to create a drone at a given `org.bukkit.Location` . The `Location` class is used throughout the bukkit API. For example, if you want to create a drone when a block is broken at the block's location you would do so like this... + + events.blockBreak( function( event ) { + var location = event.block.location; + var drone = new Drone(location); + // do more stuff with the drone here... + }); + +#### Parameters + + * Player : If a player reference is given as the sole parameter then the block the player was looking at will be used as the starting point for the drone. If the player was not looking at a block then the player's location will be used as the starting point. If a `Player` object is provided as a paramter then it should be the only parameter. + * location : *NB* If a `Location` object is provided as a parameter, then it should be the only parameter. + * x : The x coordinate of the Drone (x,y,z,direction and world are not needed if either a player or location parameter is provided) + * y : The y coordinate of the Drone + * z : The z coordinate of the Drone + * direction : The direction in which the Drone is facing. Possible values are 0 (east), 1 (south), 2 (west) or 3 (north) + * world : The world in which the drone is created. + +### Drone.box() method + +the box() method is a convenience method for building things. (For the more performance-oriented method - see cuboid) + +#### parameters + + * b - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` + * w (optional - default 1) - the width of the structure + * h (optional - default 1) - the height of the structure + * d (optional - default 1) - the depth of the structure - NB this is not how deep underground the structure lies - this is how far away (depth of field) from the drone the structure will extend. + +#### Example + +To create a black structure 4 blocks wide, 9 blocks tall and 1 block long... + + box(blocks.wool.black, 4, 9, 1); + +... or the following code does the same but creates a variable that can be used for further methods... + + var drone = new Drone(self); + drone.box(blocks.wool.black, 4, 9, 1); + +![box example 1](img/boxex1.png) + +### Drone.box0() method + +Another convenience method - this one creates 4 walls with no floor or ceiling. + +#### Parameters + + * block - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` + * width (optional - default 1) - the width of the structure + * height (optional - default 1) - the height of the structure + * length (optional - default 1) - the length of the structure - how far + away (depth of field) from the drone the structure will extend. + +#### Example + +To create a stone building with the insided hollowed out 7 wide by 3 tall by 6 long... + + box0( blocks.stone, 7, 3, 6); + +![example box0](img/box0ex1.png) + +### Drone.boxa() method + +Construct a cuboid using an array of blocks. As the drone moves first along the width axis, then the height (y axis) then the length, each block is picked from the array and placed. + +#### Parameters + + * blocks - An array of blocks - each block in the array will be placed in turn. + * width + * height + * length + +#### Example + +Construct a rainbow-colored road 100 blocks long... + + var rainbowColors = [blocks.wool.red, blocks.wool.orange, blocks.wool.yellow, blocks.wool.lime, + blocks.wool.lightblue, blocks.wool.blue, blocks.wool.purple]; + + boxa(rainbowColors,7,1,30); + +![boxa example](img/boxaex1.png) + +### Chaining + +All of the Drone methods return a Drone object, which means methods can be 'chained' together so instead of writing this... + + drone = new Drone( self ); + drone.fwd( 3 ); + drone.left( 2 ); + drone.box( blocks.grass ); // create a grass block + drone.up(); + drone.box( blocks.grass ); // create another grass block + drone.down(); + +...you could simply write ... + + var drone = new Drone(self).fwd(3).left(2).box(blocks.grass).up().box(blocks.grass).down(); + +... since each Drone method is also a global function that constructs a drone if none is supplied, you can shorten even further to just... + + fwd(3).left(2).box(blocks.grass).up().box(blocks.grass).down() + +The Drone object uses a [Fluent Interface][fl] to make ScriptCraft scripts more concise and easier to write and read. Minecraft's in-game command prompt is limited to about 80 characters so chaining drone commands together means more can be done before hitting the command prompt limit. For complex building you should save your commands in a new script file and load it using /js load() + +[fl]: http://en.wikipedia.org/wiki/Fluent_interface + +### Drone Properties + + * x - The Drone's position along the west-east axis (x increases as you move east) + * y - The Drone's position along the vertical axis (y increses as you move up) + * z - The Drone's position along the north-south axis (z increases as you move south) + * dir - The Drone's direction 0 is east, 1 is south , 2 is west and 3 is north. + +### Extending Drone + +The Drone object can be easily extended - new buidling recipes/blueprints can be added and can become part of a Drone's chain using the *static* method `Drone.extend`. + +### Drone.extend() static method + +Use this method to add new methods (which also become chainable global functions) to the Drone object. + +#### Parameters + + * name - The name of the new method e.g. 'pyramid'. + * function - The method body. + +Alternatively if you provide just a function as a parameter, then the function name will be used as the new method name. For example the following two approaches are both valid. + +#### Example 1 Using name and function as parameters + + // submitted by [edonaldson][edonaldson] + var Drone = require('drone'); + Drone.extend('pyramid', function( block, height ) { + this.chkpt('pyramid'); + for ( var i = height; i > 0; i -= 2) { + this.box(block, i, 1, i).up().right().fwd(); + } + return this.move('pyramid'); + }); + +#### Example 2 Using just a named function as a parameter + + var Drone = require('drone'); + function pyramid( block, height ) { + this.chkpt('pyramid'); + for ( var i = height; i > 0; i -= 2) { + this.box(block, i, 1, i).up().right().fwd(); + } + return this.move('pyramid'); + } + Drone.extend( pyramid ); + +Once the method is defined (it can be defined in a new pyramid.js file) it can be used like so... + + var d = new Drone(self); + d.pyramid(blocks.brick.stone, 12); + +... or simply ... + + pyramid(blocks.brick.stone, 12); + +[edonaldson]: https://github.com/edonaldson + +### Drone Constants + +#### Drone.PLAYER_STAIRS_FACING + +An array which can be used when constructing stairs facing in the Drone's direction... + + var d = new Drone(self); + d.box(blocks.stairs.oak + ':' + Drone.PLAYER_STAIRS_FACING[d.dir]); + +... will construct a single oak stair block facing the drone. + +#### Drone.PLAYER_SIGN_FACING + +An array which can be used when placing signs so they face in a given direction. This is used internally by the Drone.sign() method. It should also be used for placing any of the following blocks... + + * chest + * ladder + * furnace + * dispenser + +By default, chests, dispensers, signs, ladders and furnaces are placed facing towards the drone so to place a chest facing the Drone just use: + + drone.box( blocks.chest ); + +To place a chest facing _away_ from the Drone: + + drone.box( blocks.chest + ':' + Drone.PLAYER_SIGN_FACING[(drone.dir + 2) % 4]); + +#### Drone.PLAYER_TORCH_FACING + +Used when placing torches. By default torches will be placed facing up. If you want to place a torch so that it faces towards the drone: + + drone.box( blocks.torch + ':' + Drone.PLAYER_TORCH_FACING[drone.dir]); + +If you want to place a torch so it faces _away_ from the drone: + + drone.box( blocks.torch + ':' + Drone.PLAYER_TORCH_FACING[(drone.dir + 2) % 4]); + +#### Drone.MAX_SIDE + +Specifies the maximum length (in any dimension) when calling the Drone.cuboidX (box) method. +The default value is 1,000 blocks. + +If you see an error message in the console `Build too big!` It's because the width, height or length paramete was greater than the Drone.MAX_SIDE value. + +#### Drone.MAX_VOLUME + +Specifies the maximum value for any call to Drone.cuboidX (box) method. +The default value is 1,000,000 (1 million) blocks. + +If the volume (width X height X length) of any single call to the Drone.cuboidX() method exceeds this value, you will see an error message in the console `Build too big!` . + +The values of both the `Drone.MAX_SiDE` and `Drone.MAX_VOLUME` variables _can_ be overridden but it's not recommended. + +***/ + +// +// Implementation +// ============== +// +// There is no need to read any further unless you want to understand how the Drone object works. +// +function getDirFromRotation( location ) { + // 0 = east, 1 = south, 2 = west, 3 = north + // 46 to 135 = west + // 136 to 225 = north + // 226 to 315 = east + // 316 to 45 = south + var r; + if (__plugin.canary ) { + r = location.rotation; + } + if (__plugin.bukkit) { + r = location.yaw; + } + + // west = -270 + // north = -180 + // east = -90 + // south = 0 + + r = (r + 360 ) % 360; // east could be 270 or -90 + + if ( r > 45 && r <= 135 ) + return 2; // west + if ( r > 135 && r <= 225 ) + return 3; // north + if ( r > 225 && r <= 315 ) + return 0; // east + return 1; // south +} +/* + low-level function to place a block in the world - all drone methods which + place blocks ultimately invoke this function. +*/ +function putBlock( x, y, z, blockId, metadata, world, update ) { + if ( typeof metadata == 'undefined' ) { + metadata = 0; + } + var block = world.getBlockAt( x, y, z ); + + if (__plugin.canary) { + var BlockType = Packages.net.canarymod.api.world.blocks.BlockType; + block.type = BlockType.fromId(blockId); + var applyProperties = require('blockhelper').applyProperties; + applyProperties(block, metadata); + if (typeof update === 'undefined'){ + update = true; + } + if (update){ + block.update(); + } + } + if (__plugin.bukkit) { + block.setTypeIdAndData( blockId, metadata, false ); + block.data = metadata; + } + return block; +} +/* + Drone constructs a new Drone object +*/ +function Drone( x, y, z, dir, world ) { + this.record = false; + var usePlayerCoords = false; + var player = (typeof self !== 'undefined' ? self : null); + var playerPos; + if ( x.location && x.name) { + player = x; + } + playerPos = x.location; + + var that = this; + var populateFromLocation = function( loc ) { + that.x = loc.x; + that.y = loc.y; + that.z = loc.z; + that.dir = getDirFromRotation(loc); + that.world = loc.world; + }; + var mp = utils.getMousePos( player ); + if ( typeof x == 'undefined' || x.location ) { + if ( mp ) { + populateFromLocation( mp ); + if ( playerPos ) { + this.dir = getDirFromRotation(playerPos); + } + } else { + // base it on the player's current location + usePlayerCoords = true; + // + // it's possible that drone.js could be loaded by a non-playing op + // (from the server console) + // + if ( !playerPos ) { + return null; + } + populateFromLocation( playerPos ); + } + } else { + if ( arguments[0].x && arguments[0].y && arguments[0].z ) { + populateFromLocation( arguments[ 0 ] ); + } else { + this.x = x; + this.y = y; + this.z = z; + if ( typeof dir == 'undefined' ) { + this.dir = getDirFromRotation( playerPos); + } else { + this.dir = dir%4; + } + if ( typeof world == 'undefined' ) { + this.world = playerPos.world; + } else { + this.world = world; + } + } + } + + if ( usePlayerCoords ) { + this.fwd( 3 ); + } + this.chkpt( 'start' ); + this.record = true; + this.history = []; + this.player = player; + return this; +} + +Drone.getDirFromRotation = getDirFromRotation; + +Drone.opsPerSec = 10; +var theQueue = []; +function processQueue(){ + var process, + i = 0, + queues = getAllQueues(); + + for ( ; i < queues.length; i++ ) { + process = queues[i].shift(); + if (process){ + try { + process(); + } catch( e ) { + console.log('Drone build error: ' + e + ' while processing ' + process); + } + } + } + setTimeout( processQueue, 1000 / Drone.opsPerSec ); +}; +setTimeout( processQueue, 1000 / Drone.opsPerSec ); + +addUnloadHandler( function() { + var pendingBuildOps = 0; + var allQueues = getAllQueues(); + for (var i = 0; i < allQueues.length; i++){ + pendingBuildOps += allQueues[i].length; + } + if (pendingBuildOps > 0){ + console.warn('There were ' + pendingBuildOps + ' pending build operations which were cancelled'); + } +}); +// +// add custom methods to the Drone object using this function +// +Drone.extend = function( name, func ) { + if (arguments.length == 1){ + func = name; + if ( !func.name ){ + throw 'A Drone extension function must have a name!'; + } + name = func.name; + } + Drone.prototype[ '_' + name ] = func; + Drone.prototype[ name ] = function( ) { + if ( this.record ) { + this.history.push( [ name, arguments ] ); + } + var oldVal = this.record; + this.record = false; + this[ '_' + name ].apply( this, arguments ); + this.record = oldVal; + return this; + }; + + global[name] = function( ) { + var result = new Drone( self ); + result[name].apply( result, arguments ); + return result; + }; +}; + +/************************************************************************** +### Drone.times() Method + +The `times()` method makes building multiple copies of buildings +easy. It's possible to create rows or grids of buildings without +resorting to `for` or `while` loops. + +#### Parameters + + * numTimes : The number of times you want to repeat the preceding statements. + +#### Limitation + +For now, don't use `times()` inside a Drone method implementation – only use it at the in-game prompt as a short-hand workaround for loops. + +#### Example + +Say you want to do the same thing over and over. You have a couple of options: + + * You can use a `for` loop … + + d = new Drone(); for ( var i = 0; i < 4; i++ ) { d.cottage().right(8); } + +While this will fit on the in-game prompt, it's awkward. You need to +declare a new Drone object first, then write a `for` loop to create the +4 cottages. It's also error prone – even the `for` loop is too much +syntax for what should really be simple. + + * You can use a `while` loop … + + d = new Drone(); var i=4; while (i--) { d.cottage().right(8); } + +… which is slightly shorter but still too much syntax. Each of the +above statements is fine for creating a 1-dimensional array of +structures. But what if you want to create a 2-dimensional or +3-dimensional array of structures? Enter the `times()` method. + +The `times()` method lets you repeat commands in a chain any number of +times. So to create 4 cottages in a row you would use the following +statement: + + cottage().right(8).times(4); + +… which will build a cottage, then move right 8 blocks, then do it +again 4 times over so that at the end you will have 4 cottages in a +row. What's more, the `times()` method can be called more than once in +a chain. So if you wanted to create a *grid* of 20 houses ( 4 x 5 ), +you would do so using the following statement: + + cottage().right(8).times(4).fwd(8).left(32).times(5); + +… breaking it down … + + 1. The first 3 calls in the chain ( `cottage()`, `right(8)`, `times(4)` ) build a single row of 4 cottages. + + 2. The last 3 calls in the chain ( `fwd(8)`, `left(32)`, `times(5)` ) move the drone forward 8 then left 32 blocks (4 x 8) to return to the original X coordinate, then everything in the chain is repeated again 5 times so that in the end, we have a grid of 20 cottages, 4 x 5. Normally this would require a nested loop but the `times()` method does away with the need for loops when repeating builds. + +Another example: This statement creates a row of trees 2 by 3: + + oak().right(10).times(2).left(20).fwd(10).times(3) + +… You can see the results below. + +![times example 1](img/times-trees.png) + +***/ +Drone.prototype.times = function( numTimes, commands ) { + if ( typeof commands == 'undefined' ) { + commands = this.history.concat(); + } + + this.history = [ [ 'times', [ numTimes + 1, commands ] ] ]; + var oldVal = this.record; + this.record = false; + for ( var j = 1; j < numTimes; j++ ) { + for ( var i = 0; i < commands.length; i++) { + var command = commands[i]; + var methodName = command[0]; + var args = command[1]; + this[ methodName ].apply( this, args ); + } + } + this.record = oldVal; + return this; +}; + + +Drone.prototype.getBlock = function(){ + return this.world.getBlockAt(this.x,this.y,this.z); +}; +Drone.prototype.setBlock = function(blockType, data, ow, oh, od, update){ + if (typeof ow == 'undefined') + ow = 0; + if (typeof oh == 'undefined') + oh = 0; + if (typeof od == 'undefined') + od = 0; + this + .right(ow) + .up(oh) + .fwd(od); + var result = putBlock(this.x, this.y, this.z, blockType, data, this.world, update); + this + .left(ow) + .down(oh) + .back(od); + return result; +}; +Drone.prototype.traverseWidth = function(width, callback){ + _traverse[this.dir].width(this, width, callback); +}; +Drone.prototype.traverseHeight = function(height, callback){ + traverseHeight(this, height, callback); +}; +Drone.prototype.traverseDepth = function(depth, callback){ + _traverse[this.dir].depth(this, depth, callback); +}; +// +// building +// + +var playerQueues = {}; +/* + if the drone has an associated player, then use that player's queue otherwise + use the global queue. +*/ +function getQueue( drone ){ + if ( drone.player ) { + var playerName = ''+drone.player.name; + var result = playerQueues[playerName]; + if (result === undefined){ + playerQueues[playerName] = []; + return playerQueues[playerName]; + } + return result; + } else { + return theQueue; + } +} +function getAllQueues() { + var result = [ theQueue ]; + for (var pq in playerQueues) { + result.push(playerQueues[pq]) ; + } + return result; +} +Drone.prototype.cuboida = function(/* Array */ blocks, w, h, d, overwrite) { + if ( typeof overwrite == 'undefined' ) { + overwrite = true; + } + if ( typeof h == 'undefined' ) { + h = 1; + } + if ( typeof d == 'undefined' ) { + d = 1; + } + if ( typeof w == 'undefined' ) { + w = 1; + } + // + // wph 20140823 make a copy because don't want to modify array in background + // + var blocksForBuild = blocks.slice(); + var len = blocksForBuild.length, + i = 0; + for ( ; i < len; i++ ) { + blocksForBuild[i] = this.getBlockIdAndMeta( blocksForBuild[ i ] ); + } + this.then(function(){ + var bi = 0; + traverseDHW( this, d,h,w, function traverseWidthCallback( ) { + var properBlock = blocksForBuild[ bi % len ]; + this.setBlock(properBlock[0], properBlock[1]); + bi++; + }); + }); + return this; +}; +Drone.MAX_VOLUME = 1 * MILLION; +Drone.MAX_SIDE = 1 * THOUSAND; + +function isTooBig(w, h, d ) { + return ( w * h * d ) >= Drone.MAX_VOLUME || + ( w >= Drone.MAX_SIDE ) || + ( h >= Drone.MAX_SIDE ) || + ( d >= Drone.MAX_SIDE ); +}; +/* + faster cuboid because blockid, meta and world must be provided + use this method when you need to repeatedly place blocks + */ +Drone.prototype.cuboidX = function( blockType, meta, w, h, d, immediate ) { + + if ( typeof h == 'undefined' ) { + h = 1; + } + if ( typeof d == 'undefined' ) { + d = 1; + } + if ( typeof w == 'undefined' ) { + w = 1; + } + if ( isTooBig( w, h, d ) ) { + this.sign([ + 'Build too Big!', + 'width:' + w, + 'height:' + h, + 'depth:' + d + ], 68); + console.warn('Build too big! ' + w + ' X ' + h + ' X ' + d); + return this; + } + if ( !immediate ) { + this.then(function(){ + traverseDHW( this, d,h,w, function( ) { + this.setBlock( blockType, meta ); + }); + }); + } else { + traverseDHW( this, d,h,w, function( ) { + this.setBlock( blockType, meta ); + }); + } + return this; + +}; +/* + deferred execution of a drone method +*/ +var thenID = 0; +Drone.prototype.then = function( next ){ + var chkptThen = '_now' + (thenID++); + this.chkpt(chkptThen); + var thisNext = next.bind(this); + function wrapperFn(){ + var chkNow = '_now' + (thenID++); + this.chkpt(chkNow); + this.move(chkptThen); + thisNext(); + this.move(chkNow); + } + getQueue(this).push( wrapperFn.bind(this) ); + return this; +}; +Drone.prototype.cuboid = function( block, w, h, d, immediate ) { + var bm = this.getBlockIdAndMeta( block ); + return this.cuboidX( bm[0], bm[1], w, h, d, immediate); +}; + +Drone.prototype.cuboid0 = function( block, w, h, d, immediate ) { + var start = 'cuboid0' + w + h + d + immediate; + this + .chkpt( start ) + .cuboid( block, w, h, 1, immediate ) // Front wall + .cuboid( block, 1, h, d, immediate ) // Left wall + .right( w - 1 ) + .cuboid( block, 1, h, d, immediate ) // Right wall + .left( w - 1 ) + .fwd( d - 1 ) + .cuboid( block, w, h, 1, immediate ) // Back wall + .move( start ); +}; + + + +// player dirs: 0 = east, 1 = south, 2 = west, 3 = north +// block dirs: 0 = east, 1 = west, 2 = south , 3 = north +// sign dirs: 5 = east, 3 = south, 4 = west, 2 = north +Drone.PLAYER_STAIRS_FACING = [ 0, 2, 1, 3 ]; + +// for blocks 68 (wall signs) 65 (ladders) 61,62 (furnaces) 23 (dispenser) and 54 (chest) +Drone.PLAYER_SIGN_FACING = [ 4, 2, 5, 3 ]; +Drone.PLAYER_TORCH_FACING = [ 2, 4, 1, 3 ]; + +Drone.extend('box', Drone.prototype.cuboid ); +Drone.extend('box0',Drone.prototype.cuboid0 ); +Drone.extend('boxa',Drone.prototype.cuboida ); +// +// show the Drone's position and direction +// +Drone.prototype.toString = function( ) { + var dirs = ['east','south','west','north']; + return 'x: ' + this.x + ' y: '+this.y + ' z: ' + this.z + ' dir: ' + this.dir + ' '+dirs[this.dir]; +}; +Drone.prototype.debug = function( ) { + console.log(this.toString( ) ); + return this; +}; + +function getBlockIdAndMeta( b ) { + var defaultMeta = 0, + i = 0, + bs, + md, + sp; + if (typeof b === 'number' || /^[0-9]+$/.test(b)) { + // wph 20130414 - use sensible defaults for certain blocks e.g. stairs + // should face the drone. + if ( blocks.isStair(b) ) { + defaultMeta = Drone.PLAYER_STAIRS_FACING[ this.dir % 4 ]; + } else { + switch (b) { + case blocks.sign: + case blocks.ladder: + // bug: furnace, chest, dispenser don't always use the right metadata + case blocks.furnace: + case blocks.furnace_burning: + case blocks.chest: + case blocks.enderchest: + case blocks.dispenser: + defaultMeta = Drone.PLAYER_SIGN_FACING[ this.dir % 4 ]; + break; + case blocks.sign_post: + defaultMeta = ( 12 + ( ( this.dir + 2 ) * 4 ) ) % 16; + break; + } + } + return [ b, defaultMeta ]; + } + if ( typeof b === 'string' ) { + bs = b; + sp = bs.indexOf(':' ); + if ( sp == -1 ) { + b = parseInt( bs ); + return [ b, defaultMeta ]; + } + b = parseInt(bs.substring(0,sp ) ); + md = parseInt(bs.substring(sp+1,bs.length ) ); + return [b,md]; + } + if (b.id){ + // wph 20141230 we are dealing with an object + var blockInfo = b; + var metadata = {}; + for (i in b){ + if (i !== 'id') + metadata[i] = b[i]; + } + return [b.id, metadata]; + } +} +var _traverse = [{},{},{},{}]; +// east +function walkWidthEast( drone, n,callback ) { + var s = drone.z, e = s + n; + for ( ; drone.z < e; drone.z++ ) { + callback.call(drone ,drone.z-s ); + } + drone.z = s; +} +function walkDepthEast( drone,n,callback ) { + var s = drone.x, e = s+n; + for ( ;drone.x < e;drone.x++ ) { + callback.call(drone, drone.x-s ); + } + drone.x = s; +} +function walkWidthSouth( drone,n,callback ) { + var s = drone.x, e = s-n; + for ( ;drone.x > e;drone.x-- ) { + callback.call(drone, s-drone.x ); + } + drone.x = s; +} +function walkWidthWest( drone,n,callback ) { + var s = drone.z, e = s-n; + for ( ;drone.z > e;drone.z-- ) { + callback.call(drone, s-drone.z ); + } + drone.z = s; +} +_traverse[0].width = walkWidthEast; +_traverse[0].depth = walkDepthEast; +// south +_traverse[1].width = walkWidthSouth; +_traverse[1].depth = walkWidthEast; +// west +_traverse[2].width = walkWidthWest; +_traverse[2].depth = walkWidthSouth; +// north +_traverse[3].width = walkDepthEast; +_traverse[3].depth = walkWidthWest; +function traverseHeight( drone,n,callback ) { + var s = drone.y, e = s + n; + for ( ; drone.y < e; drone.y++ ) { + callback.call(drone, drone.y-s ); + } + drone.y = s; +}; +function traverseDHW( drone, d,h,w, callback ){ + _traverse[drone.dir].depth( drone, d, function traverseDepthCallback( ) { + traverseHeight( this, h, function traverseHeightCallback( ) { + _traverse[this.dir].width( this, w, callback); + }); + }); +} + +// +// wph 20130130 - make this a method - extensions can use it. +// +Drone.prototype.getBlockIdAndMeta = getBlockIdAndMeta; +Drone.prototype._getBlockIdAndMeta = function(b){ + console.warn('_getBlockIdAndMeta is deprecated. Use .getBlockIdAndMeta() instead'); + return this.getBlockIdAndMeta(b); +}; +Drone.bountiful = __plugin.canary ? parseFloat(server.canaryModVersion) > 1.7 : false; + +var droneCoreExts = [ + './arc', + './bed', + './blocktype', + './copypaste', + './cylinders', + './doors', + './firework', + './garden', + './ladder', + './movement', + './prism', + './rand', + './sign', + './sphere', + './stairs', + './trees' +]; +utils.foreach(droneCoreExts, function(path){ + require(path)(Drone); +}); +module.exports = Drone; diff --git a/src/main/js/modules/drone/ladder.js b/src/main/js/modules/drone/ladder.js new file mode 100644 index 0000000..312648a --- /dev/null +++ b/src/main/js/modules/drone/ladder.js @@ -0,0 +1,46 @@ +'use strict'; +/*global require, module*/ +/************************************************************************ +### Drone.ladder() method + +Creates a ladder extending skyward. + +#### Parameters + + * height (optional - default 1) + +#### Example + +To create a ladder extending 10 blocks high: + + var drone = new Drone(self); + drone.ladder(10) + +At the in-game prompt, look at a block and then type: + + /js ladder(10) + +A ladder 10 blocks high will be created at the point you were looking at. + +#### Since +##### 3.0.3 +***/ +var blocks = require('blocks'); + +function ladder( height ){ + this.then(function ladderLater(){ + var block = this.getBlock(); + if (block.typeId == blocks.air || block.typeId == blocks.ladder){ + this.box(blocks.ladder, 1, height, 1, true); + } else { + this + .back() + .box(blocks.ladder, 1, height, 1, true) + .fwd(); + } + }); +} + +module.exports = function(Drone){ + Drone.extend( ladder ); +}; diff --git a/src/main/js/modules/drone/movement.js b/src/main/js/modules/drone/movement.js new file mode 100644 index 0000000..2d63d4a --- /dev/null +++ b/src/main/js/modules/drone/movement.js @@ -0,0 +1,189 @@ +'use strict'; +/*global require,__plugin, module, Packages, org*/ + +/************************************************************************ +### Drone Movement + +Drones can move freely in minecraft's 3-D world. You control the +Drone's movement using any of the following methods.. + + * up() + * down() + * left() + * right() + * fwd() + * back() + * turn() + +... Each of these methods takes a single optional parameter +`numBlocks` - the number of blocks to move in the given direction. If +no parameter is given, the default is 1. + +To change direction use the `turn()` method which also takes a single +optional parameter (numTurns) - the number of 90 degree turns to +make. Turns are always clock-wise. If the drone is facing north, then +drone.turn() will make the turn face east. If the drone is facing east +then drone.turn(2) will make the drone turn twice so that it is facing +west. + +### Drone Positional Info + + * getLocation() - Returns a native Java Location object for the drone + +### Drone Markers + +Markers are useful when your Drone has to do a lot of work. You can +set a check-point and return to the check-point using the move() +method. If your drone is about to undertake a lot of work - +e.g. building a road, skyscraper or forest you should set a +check-point before doing so if you want your drone to return to its +current location. + +A 'start' checkpoint is automatically created when the Drone is first created. + +Markers are created and returned to using the followng two methods... + + * chkpt - Saves the drone's current location so it can be returned to later. + * move - moves the drone to a saved location. Alternatively you can provide a Java Location object or x,y,z and direction parameters. + +#### Parameters + + * name - the name of the checkpoint to save or return to. + +#### Example + + drone.chkpt('town-square'); + // + // the drone can now go off on a long excursion + // + for ( i = 0; i< 100; i++) { + drone.fwd(12).box(6); + } + // + // return to the point before the excursion + // + drone.move('town-square'); + +***/ +var _movements = [{},{},{},{}]; +// east +_movements[0].right = function( drone,n ) { drone.z +=n; return drone;}; +_movements[0].left = function( drone,n ) { drone.z -=n; return drone;}; +_movements[0].fwd = function( drone,n ) { drone.x +=n; return drone;}; +_movements[0].back = function( drone,n ) { drone.x -= n; return drone;}; +// south +_movements[1].right = _movements[0].back; +_movements[1].left = _movements[0].fwd; +_movements[1].fwd = _movements[0].right; +_movements[1].back = _movements[0].left; +// west +_movements[2].right = _movements[0].left; +_movements[2].left = _movements[0].right; +_movements[2].fwd = _movements[0].back; +_movements[2].back = _movements[0].fwd; +// north +_movements[3].right = _movements[0].fwd; +_movements[3].left = _movements[0].back; +_movements[3].fwd = _movements[0].left; +_movements[3].back = _movements[0].right; + +function turn( n ) { + if ( typeof n == 'undefined' ) { + n = 1; + } + this.dir += n; + this.dir %=4; +} +function chkpt( name ) { + this._checkpoints[ name ] = { x:this.x, y:this.y, z:this.z, dir:this.dir }; +} +function move( ) { + var Drone = this.constructor; + if ( arguments[0].x && arguments[0].y && arguments[0].z) { + this.x = arguments[0].x; + this.y = arguments[0].y; + this.z = arguments[0].z; + this.dir = Drone.getDirFromRotation(arguments[0] ); + this.world = arguments[0].world; + } else if ( typeof arguments[0] === 'string' ) { + var coords = this._checkpoints[arguments[0]]; + if ( coords ) { + this.x = coords.x; + this.y = coords.y; + this.z = coords.z; + this.dir = coords.dir%4; + } + } else { + // expect x,y,z,dir + switch( arguments.length ) { + case 4: + this.dir = arguments[3]; + case 3: + this.z = arguments[2]; + case 2: + this.y = arguments[1]; + case 1: + this.x = arguments[0]; + } + } +} +function right( n ) { + if ( typeof n == 'undefined' ) { + n = 1; + } + _movements[ this.dir ].right( this, n ); +} +function left( n ) { + if ( typeof n == 'undefined') { + n = 1; + } + _movements[ this.dir ].left( this, n ); +} +function fwd( n ) { + if ( typeof n == 'undefined' ) { + n = 1; + } + _movements[ this.dir ].fwd( this, n ); +} +function back( n ) { + if ( typeof n == 'undefined' ) { + n = 1; + } + _movements[ this.dir ].back( this, n ); +} +function up( n ) { + if ( typeof n == 'undefined' ) { + n = 1; + } + this.y+= n; +} +function down( n ) { + if ( typeof n == 'undefined' ) { + n = 1; + } + this.y-= n; +} +function getLocation( ) { + if (__plugin.canary) { + var cmLocation = Packages.net.canarymod.api.world.position.Location; + return new cmLocation( this.world, this.x, this.y, this.z, 0, 0); + } + if (__plugin.bukkit) { + var bkLocation = org.bukkit.Location; + return new bkLocation( this.world, this.x, this.y, this.z ); + } +} +module.exports = function(Drone){ + Drone.prototype._checkpoints = {}; + Drone.prototype.getLocation = getLocation; + Drone.extend( chkpt ); + Drone.extend( move ); + Drone.extend( turn ); + Drone.extend( right ); + Drone.extend( left ); + Drone.extend( fwd ); + Drone.extend( back ); + Drone.extend( up ); + Drone.extend( down ); +}; + diff --git a/src/main/js/modules/drone/prism.js b/src/main/js/modules/drone/prism.js new file mode 100644 index 0000000..1096514 --- /dev/null +++ b/src/main/js/modules/drone/prism.js @@ -0,0 +1,98 @@ +'use strict'; +/*global require, module*/ +/************************************************************************ +### Drone.prism() method + +Creates a prism. This is useful for roofs on houses. + +#### Parameters + + * block - the block id - e.g. 6 for an oak sapling or '6:2' for a birch sapling. + Alternatively you can use any one of the `blocks` values e.g. `blocks.sapling.birch` + * width - the width of the prism + * length - the length of the prism (will be 2 time its height) + +#### Example + + prism(blocks.oak,3,12); + +![prism example](img/prismex1.png) + +### Drone.prism0() method + +A variation on `prism` which hollows out the inside of the prism. It +uses the same parameters as `prism`. + +***/ +var STAIRBLOCKS = { + 53: '5:0' // oak wood + ,67: 4 // cobblestone + ,108: 45 // brick + ,109: 98 // stone brick + ,114: 112 // nether brick + ,128: 24 // sandstone + ,134: '5:1' // spruce wood + ,135: '5:2' // birch wood + ,136: '5:3' // jungle wood + ,156: 155 // quartz +}; +// +// prism private implementation +// +function prism( block, w, d ) { + var stairEquiv = STAIRBLOCKS[block]; + if ( stairEquiv ) { + this + .fwd() + .prism( stairEquiv,w,d-2 ) + .back() + .stairs(block, w, d / 2) + .fwd(d - 1) + .right(w - 1) + .turn(2) + .stairs(block, w, d / 2) + .turn(2) + .left(w - 1) + .back(d - 1); + }else{ + var c = 0; + var d2 = d; + while ( d2 >= 1 ) { + this.cuboid(block,w,1,d2 ); + d2 -= 2; + this.fwd( ).up( ); + c++; + } + this.down(c ).back(c ); + } + return this; +}; +// +// prism0 private implementation +// +function prism0( block,w,d ) { + this + .stairs(block,w,d/2) + .fwd(d-1) + .right(w-1) + .turn(2) + .stairs(block,w,d/2) + .turn(2) + .left(w-1) + .back(d-1); + + var se = STAIRBLOCKS[block]; + if (se) { + this + .fwd() + .prism(se,1,d-2) + .right(w-1) + .prism(se,1,d-2) + .left(w-1) + .back(); + } +} +module.exports = function(Drone){ + Drone.extend(prism0); + Drone.extend(prism); +}; diff --git a/src/main/js/modules/drone/rand.js b/src/main/js/modules/drone/rand.js new file mode 100644 index 0000000..8a7ec1a --- /dev/null +++ b/src/main/js/modules/drone/rand.js @@ -0,0 +1,67 @@ +'use strict'; +/*global require, module*/ +/************************************************************************ +### Drone.rand() method + +rand takes either an array (if each blockid has the same chance of occurring) or an object where each property is a blockid and the value is it's weight (an integer) + +#### Example + +place random blocks stone, mossy stone and cracked stone (each block has the same chance of being picked) + + rand( [blocks.brick.stone, blocks.brick.mossy, blocks.brick.cracked ],w,d,h) + +to place random blocks stone has a 50% chance of being picked, + + var distribution = {}; + distribution[ blocks.brick.stone ] = 5; + distribution[ blocks.brick.mossy ] = 3; + distribution[ blocks.brick.cracked ] = 2; + + rand( distribution, width, height, depth) + +regular stone has a 50% chance, mossy stone has a 30% chance and cracked stone has just a 20% chance of being picked. + +***/ +// +// standard fisher-yates shuffle algorithm +// +function fisherYates( myArray ) { + var i = myArray.length; + if ( i == 0 ) return false; + while ( --i ) { + var j = Math.floor( Math.random( ) * ( i + 1 ) ); + var tempi = myArray[i]; + var tempj = myArray[j]; + myArray[i] = tempj; + myArray[j] = tempi; + } +} +function _rand( blockDistribution ) { + if ( !(blockDistribution.constructor == Array ) ) { + var a = []; + for ( var p in blockDistribution ) { + var n = blockDistribution[p]; + for ( var i = 0;i < n;i++ ) { + a.push(p ); + } + } + blockDistribution = a; + } + while ( blockDistribution.length < 1000 ) { + // make array bigger so that it's more random + blockDistribution = blockDistribution.concat(blockDistribution ); + } + fisherYates(blockDistribution ); + return blockDistribution; +} +function rand( dist, width, height, depth, overwrite ) { + if ( typeof overwrite == 'undefined' ) { + overwrite = true; + } + var randomized = _rand( dist ); + this.boxa( randomized, width, height, depth, overwrite); +} +module.exports = function(Drone){ + Drone.extend( rand ); +}; diff --git a/src/main/js/modules/drone/sign.js b/src/main/js/modules/drone/sign.js new file mode 100644 index 0000000..8d84a7f --- /dev/null +++ b/src/main/js/modules/drone/sign.js @@ -0,0 +1,144 @@ +'use strict'; +/*global require, echo,__plugin, module*/ +var blocks = require('blocks'); +/************************************************************************ +### Drone.wallsign() method + +Creates a wall sign (A sign attached to a wall) + +#### Parameters + + * message - can be a string or an array of strings + +#### Example + + drone.wallsign(['Welcome','to','Scriptopia']); + +![wall sign](img/signex2.png) + +### Drone.signpost() method + +Creates a free-standing signpost + +#### Parameters + + * message - can be a string or an array of strings + +#### Example + + drone.signpost(['Hello','World']); + +![ground sign](img/signex1.png) + +### Drone.sign() method + +Deprecated: Use signpost() or wallsign() methods instead. + +Signs must use block 63 (stand-alone signs) or 68 (signs on walls) + +#### Parameters + + * message - can be a string or an array of strings. + * block - can be 63 or 68 + +#### Example + +To create a free-standing sign... + + drone.sign(["Hello","World"], blocks.sign_post); + +![ground sign](img/signex1.png) + +... to create a wall mounted sign... + + drone.sign(["Welcome","to","Scriptopia"], blocks.sign ); + +![wall sign](img/signex2.png) + +***/ +function putSign( drone, texts, blockId, meta ) { + var i, + len = texts.length, + block, + state, + getState, + isSign, + setLine; + + if ( blockId != blocks.sign_post && blockId != blocks.sign ) { + throw new Error( 'Invalid Parameter: blockId must be blocks.sign_post or blocks.sign' ); + } + block = drone.setBlock( blockId, meta); + if (__plugin.canary){ + isSign = function(block){ + var sign = block.getTileEntity(); + return sign.setTextOnLine; + }; + setLine = function( block, i) { + var sign = block.getTileEntity(); + sign.setTextOnLine( texts[i], i ); + sign.update(); + }; + } + if (__plugin.bukkit){ + isSign = function(block){ return block.state && block.state.setLine; }; + setLine = function( block, i) { + var sign = block.state; + sign.setLine( i, texts[i] ); + sign.update(true); + }; + } + if ( isSign(block) ) { + if (len > 4){ + len = 4; + } + for ( i = 0; i < len; i++ ) { + setLine(block, i, texts[ i ] ); + } + } +}; +function signpost( message ){ + this.then(function(){ + this.sign(message, blocks.sign_post); + }); +} +function wallsign( message ){ + /* + must allow for /js wallsign() while looking at a wall block + */ + this.then(function(){ + var block = this.getBlock(); + if (block.typeId == blocks.air || block.typeId == blocks.sign){ + this.sign(message, blocks.sign); + } else { + this + .back() + .sign(message, blocks.sign) + .fwd(); + } +}); + +} +function sign( message, block ) { + if ( message.constructor != Array ) { + message = [message]; + } + var bm = this.getBlockIdAndMeta( block ); + block = bm[0]; + var meta = bm[1]; + if ( block !== blocks.sign_post && block !== blocks.sign ) { + var usage = 'Usage: sign("message", blocks.sign_post) or sign("message", blocks.sign)'; + if ( this.player ) { + echo( this.player, usage); + } + console.error(usage); + return; + } + putSign( this, message, block, meta); + +} +module.exports = function(Drone){ + Drone.extend(sign); + Drone.extend(signpost); + Drone.extend(wallsign); +}; diff --git a/src/main/js/modules/drone/sphere.js b/src/main/js/modules/drone/sphere.js new file mode 100644 index 0000000..c805654 --- /dev/null +++ b/src/main/js/modules/drone/sphere.js @@ -0,0 +1,396 @@ +'use strict'; +/*global module*/ +/************************************************************************ +### Drone.sphere() method + +Creates a sphere. + +#### Parameters + + * block - The block the sphere will be made of. + * radius - The radius of the sphere. + +#### Example + +To create a sphere of Iron with a radius of 10 blocks... + + sphere( blocks.iron, 10); + +![sphere example](img/sphereex1.png) + +Spheres are time-consuming to make. You *can* make large spheres (250 radius) but expect the +server to be very busy for a couple of minutes while doing so. + +***/ +function sphere( block, radius ) { + var lastRadius = radius, + slices = [ [ radius , 0 ] ], + diameter = radius * 2, + bm = this.getBlockIdAndMeta( block ), + r2 = radius * radius, + i = 0, + newRadius, + yOffset, + sr, + sh, + v, + h; + + if ( radius > 127 ) { + throw new Error('Sphere radius must be less than 128 blocks'); + } + for ( i = 0; i <= radius; i++ ) { + newRadius = Math.round( Math.sqrt( r2 - i * i ) ); + if ( newRadius == lastRadius ) { + slices[ slices.length - 1 ][ 1 ]++; + } else { + slices.push( [ newRadius , 1 ] ); + } + lastRadius = newRadius; + } + this.chkpt( 'sphere' ); + // + // mid section + // + this.up( radius - slices[0][1] ) + .cylinder( block, radius, ( slices[0][1]*2 ) - 1, { blockType: bm[0], meta: bm[1] } ) + .down( radius - slices[0][1] ); + + yOffset = -1; + for ( i = 1; i < slices.length; i++ ) { + yOffset += slices[i-1][1]; + sr = slices[i][0]; + sh = slices[i][1]; + v = radius + yOffset; + h = radius - sr; + // northern hemisphere + this.up( v ) + .fwd( h ) + .right( h ) + .cylinder( block, sr, sh, { blockType: bm[0], meta: bm[1] } ) + .left( h ) + .back( h ) + .down( v ); + + // southern hemisphere + v = radius - ( yOffset + sh + 1 ); + this.up( v ) + .fwd( h ) + .right( h ) + .cylinder( block, sr, sh, { blockType: bm[0], meta: bm[1]} ) + .left( h ) + .back( h ) + .down( v ); + } + return this.move( 'sphere' ); +} +/************************************************************************ +### Drone.sphere0() method + +Creates an empty sphere. + +#### Parameters + + * block - The block the sphere will be made of. + * radius - The radius of the sphere. + +#### Example + +To create a sphere of Iron with a radius of 10 blocks... + + sphere0( blocks.iron, 10); + +Spheres are time-consuming to make. You *can* make large spheres (250 radius) but expect the +server to be very busy for a couple of minutes while doing so. + +***/ +function sphere0(block,radius) +{ + var lastRadius = radius, + slices = [ [ radius, 0 ] ], + diameter = radius * 2, + bm = this.getBlockIdAndMeta( block ), + r2 = radius*radius, + i, + newRadius, + sr, + sh, + v, + h, + len, + yOffset; + + if ( radius > 127 ) { + throw new Error('Sphere radius must be less than 128 blocks'); + } + + for ( i = 0; i <= radius; i++ ) { + newRadius = Math.round( Math.sqrt( r2 - i * i ) ); + if ( newRadius == lastRadius ) { + slices[ slices.length - 1 ][ 1 ]++; + } else { + slices.push( [ newRadius, 1 ] ); + } + lastRadius = newRadius; + } + this.chkpt( 'sphere0' ); + // + // mid section + // + this.up( radius - slices[0][1] ) + .arc({ blockType: bm[0], + meta: bm[1], + radius: radius, + strokeWidth: 2, + stack: (slices[0][1]*2)-1, + fill: false + }) + .down( radius - slices[0][1] ); + + yOffset = -1; + len = slices.length; + for ( i = 1; i < len; i++ ) { + yOffset += slices[i-1][1]; + sr = slices[i][0]; + sh = slices[i][1]; + v = radius + yOffset; + h = radius-sr; + // northern hemisphere + // .cylinder(block,sr,sh,{blockType: bm[0],meta: bm[1]}) + this.up( v ).fwd( h ).right( h ) + .arc({ + blockType: bm[0], + meta: bm[1], + radius: sr, + stack: sh, + fill: false, + strokeWidth: i < len - 1 ? 1 + ( sr - slices[ i + 1 ][ 0 ] ) : 1 + }) + .left( h ).back( h ).down( v ); + + // southern hemisphere + v = radius - ( yOffset + sh + 1 ); + this.up( v ).fwd( h ).right( h ) + .arc({ + blockType: bm[0], + meta: bm[1], + radius: sr, + stack: sh, + fill: false, + strokeWidth: i < len - 1 ? 1 + ( sr - slices[ i + 1 ][ 0 ] ) : 1 + }) + .left( h ).back( h ). down( v ); + } + this.move( 'sphere0' ); + + return this; + +} +/************************************************************************ +### Drone.hemisphere() method + +Creates a hemisphere. Hemispheres can be either north or south. + +#### Parameters + + * block - the block the hemisphere will be made of. + * radius - the radius of the hemisphere + * northSouth - whether the hemisphere is 'north' or 'south' + +#### Example + +To create a wood 'north' hemisphere with a radius of 7 blocks... + + hemisphere(blocks.oak, 7, 'north'); + +![hemisphere example](img/hemisphereex1.png) + +***/ +function hemisphere( block, radius, northSouth ) { + var lastRadius = radius, + slices = [ [ radius, 0 ] ], + diameter = radius * 2, + bm = this.getBlockIdAndMeta(block), + r2 = radius * radius, + i = 0, + newRadius; + + if ( radius > 255 ) { + throw new Error('Hemisphere radius must be less than 256 blocks'); + } + + for ( i = 0; i <= radius; i++ ) { + newRadius = Math.round( Math.sqrt( r2 - i * i ) ); + if ( newRadius == lastRadius ) { + slices[ slices.length - 1 ][ 1 ]++; + } else { + slices.push( [ newRadius, 1 ] ); + } + lastRadius = newRadius; + } + this.chkpt( 'hsphere' ); + // + // mid section + // + if ( northSouth == 'north' ) { + this.cylinder( block, radius, slices[0][1], { blockType: bm[0], meta: bm[1] } ); + } else { + this.up( radius - slices[0][1] ) + .cylinder( block, radius, slices[0][1], { blockType: bm[0], meta: bm[1] } ) + .down( radius - slices[0][1] ); + } + + var yOffset = -1; + for ( i = 1; i < slices.length; i++ ) { + yOffset += slices[i-1][1]; + var sr = slices[i][0]; + var sh = slices[i][1]; + var v = yOffset, h = radius-sr; + if ( northSouth == 'north' ) { + // northern hemisphere + this.up( v ).fwd( h ).right( h ) + .cylinder( block, sr, sh, { blockType: bm[0], meta: bm[1] } ) + .left( h ).back( h ).down( v ); + } else { + // southern hemisphere + v = radius - ( yOffset + sh + 1 ); + this.up( v ).fwd( h ).right( h ) + .cylinder( block, sr, sh, { blockType: bm[0], meta: bm[1] } ) + .left( h ).back( h ).down( v ); + } + } + return this.move( 'hsphere' ); +} +/************************************************************************ +### Drone.hemisphere0() method + +Creates a hollow hemisphere. Hemispheres can be either north or south. + +#### Parameters + + * block - the block the hemisphere will be made of. + * radius - the radius of the hemisphere + * northSouth - whether the hemisphere is 'north' or 'south' + +#### Example + +To create a glass 'north' hemisphere with a radius of 20 blocks... + + hemisphere0(blocks.glass, 20, 'north'); + +![hemisphere example](img/hemisphereex2.png) + +***/ +function hemisphere0( block, radius, northSouth ) { + + if ( radius > 255 ) { + throw new Error('Hemisphere radius must be less than 256 blocks'); + } + + var lastRadius = radius, + slices = [ [ radius, 0 ] ], + diameter = radius * 2, + bm = this.getBlockIdAndMeta(block), + r2 = radius * radius, + i = 0, + len, + newRadius; + + if ( radius > 255 ) { + throw new Error('Hemisphere radius must be less than 256 blocks'); + } + + + for ( i = 0; i <= radius; i++ ) { + newRadius = Math.round( Math.sqrt( r2 - i * i ) ); + if ( newRadius == lastRadius ) { + slices[ slices.length - 1 ][ 1 ]++; + } else { + slices.push( [ newRadius, 1 ] ); + } + lastRadius = newRadius; + } + this.chkpt( 'hsphere0' ); + // + // mid section + // + if ( northSouth == 'north' ) { + + this.arc({ + blockType: bm[0], + meta: bm[1], + radius: radius, + strokeWidth: 1, + stack: slices[0][1], + fill: false + }); + } else { + this.up( radius - slices[0][1] ); + + this.arc({ + blockType: bm[0], + meta: bm[1], + radius: radius, + strokeWidth: 1, + stack: slices[0][1], + fill: false + }); + + this.down( radius - slices[0][1] ); + } + + var yOffset = -1; + len = slices.length; + for ( i = 1; i < slices.length; i++ ) { + yOffset += slices[i-1][1]; + var sr = slices[i][0]; + var sh = slices[i][1]; + var v = yOffset, h = radius-sr; + if ( northSouth == 'north' ) { + // northern hemisphere + this.up( v ) + .fwd( h ) + .right( h ); + + this.arc( { + blockType: bm[0], + meta: bm[1], + radius: sr, + stack: sh, + fill: false, + strokeWidth: i < len - 1 ? 1 + ( sr - slices[ i + 1 ][ 0 ] ) : 1 + } ); + + this.left( h ) + .back( h ) + .down( v ); + } else { + // southern hemisphere + v = radius - ( yOffset + sh + 1 ); + this.up( v ) + .fwd( h ) + .right( h ); + + this.arc({ + blockType: bm[0], + meta: bm[1], + radius: sr, + stack: sh, + fill: false, + strokeWidth: i < len - 1 ? 1 + ( sr - slices[ i + 1 ][ 0 ] ) : 1 + }); + + this.left( h ) + .back( h ) + .down( v ); + } + } + return this.move( 'hsphere0' ); + +} +module.exports = function(Drone){ + Drone.extend( sphere ); + Drone.extend( sphere0 ); + Drone.extend( hemisphere ); + Drone.extend( hemisphere0 ); +}; diff --git a/src/main/js/modules/drone/stairs.js b/src/main/js/modules/drone/stairs.js new file mode 100644 index 0000000..519ee1e --- /dev/null +++ b/src/main/js/modules/drone/stairs.js @@ -0,0 +1,61 @@ +'use strict'; +/*global module*/ +/************************************************************************** +### Drone.stairs() function + +The stairs() function will build a flight of stairs + +#### Parameters + + * blockType - should be one of the following: + + * blocks.stairs.oak + * blocks.stairs.cobblestone + * blocks.stairs.brick + * blocks.stairs.stone + * blocks.stairs.nether + * blocks.stairs.sandstone + * blocks.stairs.spruce + * blocks.stairs.birch + * blocks.stairs.jungle + * blocks.stairs.quartz + + * width - The width of the staircase - default is 1 + * height - The height of the staircase - default is 1 + +#### Example + +To build an oak staircase 3 blocks wide and 5 blocks tall: + + /js stairs(blocks.stairs.oak, 3, 5) + +Staircases do not have any blocks beneath them. + +***/ +var blocks = require('blocks'); +/*global require*/ +function stairs(blockType, width, height){ + if (typeof width === 'undefined') + width = 1; + if (typeof height === 'undefined') + height = 1; + if (typeof blockType === 'undefined'){ + blockType = blocks.stairs.oak; + } + var bm = this.getBlockIdAndMeta(blockType); + this.then(function(){ + this.chkpt('_stairs'); + while (height > 0) { + this.traverseWidth(width, function(){ + this.setBlock(bm[0], bm[1]); + }); + + this.fwd().up(); + height -= 1; + } + this.move('_stairs'); + }); +} +module.exports = function(Drone){ + Drone.extend(stairs); +}; diff --git a/src/main/javascript/drone/test.js b/src/main/js/modules/drone/test.js similarity index 60% rename from src/main/javascript/drone/test.js rename to src/main/js/modules/drone/test.js index 1f30dd9..5b870b8 100644 --- a/src/main/javascript/drone/test.js +++ b/src/main/js/modules/drone/test.js @@ -1,4 +1,6 @@ -Drone.prototype.testHorizontalStrokeWidth = function(){ +'use strict'; +/*global module*/ +function testHorizontalStrokeWidth(){ this.arc({ blockType: 42, meta: 0, @@ -7,9 +9,8 @@ Drone.prototype.testHorizontalStrokeWidth = function(){ strokeWidth: 3, quadrants: {topright:true,topleft:true,bottomleft:true,bottomright:true} }); -}; - -Drone.prototype.testVerticalStrokeWidth = function(){ +} +function testVerticalStrokeWidth(){ this.arc({ blockType: 42, meta: 0, @@ -18,4 +19,8 @@ Drone.prototype.testVerticalStrokeWidth = function(){ strokeWidth: 3, quadrants: {topright:true,topleft:true,bottomleft:true,bottomright:true} }); +} +module.exports = function(Drone){ + Drone.prototype.testHorizontalStrokeWidth = testHorizontalStrokeWidth; + Drone.prototype.testVerticalStrokeWidth = testVerticalStrokeWidth; }; diff --git a/src/main/js/modules/drone/trees.js b/src/main/js/modules/drone/trees.js new file mode 100644 index 0000000..797c418 --- /dev/null +++ b/src/main/js/modules/drone/trees.js @@ -0,0 +1,99 @@ +'use strict'; +/*global require, __plugin, Packages, org, echo, module */ +var blocks = require('blocks'); +/************************************************************************ +### Drone Trees methods + + * oak() + * spruce() + * birch() + * jungle() + +#### Example + +To create 4 trees in a row, point the cross-hairs at the ground then type `/js ` and ... + + up( ).oak( ).right(8 ).spruce( ).right(8 ).birch( ).right(8 ).jungle( ); + +Trees won't always generate unless the conditions are right. You +should use the tree methods when the drone is directly above the +ground. Trees will usually grow if the drone's current location is +occupied by Air and is directly above an area of grass (That is why +the `up()` method is called first). + +![tree example](img/treeex1.png) + +None of the tree methods require parameters. Tree methods will only be +successful if the tree is placed on grass in a setting where trees can +grow. + +***/ +function bukkitTreeFactory( k, v ) { + return function( ) { + var block = this.getBlock(); + if ( block.typeId == blocks.grass ) { + this.up( ); + } + var treeLoc = this.getLocation(); + var successful = treeLoc.world.generateTree(treeLoc,v ); + if ( block.typeId == blocks.grass ) { + this.down( ); + } + }; +} +function canaryTreeFactory( k, v ){ + return function(){ + var block = this.getBlock(); + if ( block.typeId == blocks.grass ) { + this.up( ); + } + var treeLoc = this.getLocation(); + if (!treeLoc.world.generateTree){ + var msg = k + '() is not supported in this version'; + if (this.player){ + echo(this.player, msg); + } + console.log(msg); + return; + } + var cmTreeType = Packages.net.canarymod.api.world.TreeType; + var trees = { + oak: cmTreeType.BIGOAK, + birch: cmTreeType.BIRCH, + jungle: cmTreeType.JUNGLE, + spruce: cmTreeType.SPRUCE + }; + + var successful = treeLoc.world.generateTree(treeLoc, trees[k] ); + if ( block.typeId == blocks.grass ) { + this.down( ); + } + }; +} +module.exports = function (Drone){ + var trees = { + oak: null, + birch: null, + jungle: null, + spruce: null + }; + var p; + if (__plugin.canary){ + for (p in trees ) { + Drone.extend(p, canaryTreeFactory ( p, trees[p] ) ); + } + } + if (__plugin.bukkit){ + var bkTreeType = org.bukkit.TreeType; + trees = { + oak: bkTreeType.BIG_TREE , + birch: bkTreeType.BIRCH , + jungle: bkTreeType.JUNGLE, + spruce: bkTreeType.REDWOOD + }; + for (p in trees ) { + Drone.extend(p, bukkitTreeFactory ( p, trees[p] ) ); + } + } +}; + diff --git a/src/main/js/modules/entities.js b/src/main/js/modules/entities.js new file mode 100644 index 0000000..0b74a64 --- /dev/null +++ b/src/main/js/modules/entities.js @@ -0,0 +1,38 @@ +'use strict'; +/*global __plugin, org, Packages, module, exports*/ +var entities = {}, + entitytypes, + t, i, name; +if (__plugin.bukkit) { + entitytypes = org.bukkit.entity.EntityType.values(); +} +if (__plugin.canary) { + entitytypes = Packages.net.canarymod.api.entity.EntityType.values(); +} +function getEntityHandler( entityType ) { + return function( entity ){ + if (arguments.length == 0){ + return entityType; + } + if (arguments.length == 1){ + if (entity){ + if (__plugin.bukkit){ + return entity.type == entityType; + } + if (__plugin.canary){ + return entity.entityType == entityType; + } + } + } + return null; + }; +} +for (t in entitytypes) { + if (entitytypes[t] && entitytypes[t].ordinal) { + name = ('' + entitytypes[t].name()).replace(/^(.*)/,function(a){ + return a.toLowerCase(); + }); + entities[name] = getEntityHandler(entitytypes[t]); + } +} +module.exports = entities; diff --git a/src/main/js/modules/fireworks.js b/src/main/js/modules/fireworks.js new file mode 100644 index 0000000..b8495b1 --- /dev/null +++ b/src/main/js/modules/fireworks.js @@ -0,0 +1,42 @@ +/************************************************************************ +## Fireworks Module + +The fireworks module makes it easy to create fireworks using +ScriptCraft. The module has a single function `firework` which takes +a `org.bukkit.Location` as its 1 and only parameter. + +### Examples + +The module also extends the `Drone` object adding a `firework` method +so that fireworks can be created as a part of a Drone chain. For +Example.... + + /js firework() + +... creates a single firework, while .... + + /js firework().fwd(3).times(5) + +... creates 5 fireworks in a row. Fireworks have also been added as a +possible option for the `arrow` module. To have a firework launch +where an arrow strikes... + + /js arrows.firework() + +To call the fireworks.firework() function directly, you must provide a +location. For example... + + /js var fireworks = require('fireworks'); + /js fireworks.firework( self.location ); + +![firework example](img/firework.png) + +***/ + +if ( __plugin.canary ) { + exports.firework = require('./canary/fireworks'); +} else { + exports.firework = require('./bukkit/fireworks'); +} + + diff --git a/src/main/js/modules/http/package.json b/src/main/js/modules/http/package.json new file mode 100644 index 0000000..a143df2 --- /dev/null +++ b/src/main/js/modules/http/package.json @@ -0,0 +1,4 @@ +{ + "name": "http", + "main": "./request.js" +} diff --git a/src/main/js/modules/http/request.js b/src/main/js/modules/http/request.js new file mode 100644 index 0000000..adbe9cb --- /dev/null +++ b/src/main/js/modules/http/request.js @@ -0,0 +1,137 @@ +/************************************************************************* +## Http Module + +For handling http requests. Not to be confused with the more robust +and functional 'http' module bundled with Node.js. + +### http.request() function + +The http.request() function will fetch a web address asynchronously (on a +separate thread)and pass the URL's response to a callback function +which will be executed synchronously (on the main thread). In this +way, http.request() can be used to fetch web content without blocking the +main thread of execution. + +#### Parameters + + * request: The request details either a plain URL e.g. "http://scriptcraft.js/sample.json" or an object with the following properties... + + - url: The URL of the request. + - method: Should be one of the standard HTTP methods, GET, POST, PUT, DELETE (defaults to GET). + - params: A Javascript object with name-value pairs. This is for supplying parameters to the server. + + * callback: The function to be called when the Web request has completed. This function takes the following parameters... + - responseCode: The numeric response code from the server. If the server did not respond with 200 OK then the response parameter will be undefined. + - response: A string (if the response is of type text) or object containing the HTTP response body. + +#### Example + +The following example illustrates how to use http.request to make a request to a JSON web service and evaluate its response... + +```javascript +var jsResponse; +var http = require('http'); +http.request('http://scriptcraftjs.org/sample.json',function(responseCode, responseBody){ + jsResponse = JSON.parse( responseBody ); +}); +``` +The following example illustrates a more complex use-case POSTing parameters to a CGI process on a server... + +```javascript +var http = require('http'); +http.request( { + url: 'http://pixenate.com/pixenate/pxn8.pl', + method: 'POST', + params: {script: '[]'} + }, + function( responseCode, responseBody ) { + var jsObj = JSON.parse( responseBody ); + }); +``` + +***/ + +/*global exports, encodeURI, server, __plugin, setTimeout*/ +function paramsToString( params ) { + var result = '', + paramNames = [], + i; + for ( i in params ) { + paramNames.push( i ); + } + for ( i = 0; i < paramNames.length; i++ ) { + result += paramNames[i] + '=' + encodeURI( params[ paramNames[i] ] ); + if ( i < paramNames.length-1 ) + result += '&'; + } + return result; +} +function invokeNow( fn ){ + if (__plugin.bukkit){ + server.scheduler.runTask( __plugin, fn); + return; + } + if (__plugin.canary){ + fn(); + return; + } +} +function invokeLater( fn ){ + if (__plugin.bukkit){ + server.scheduler.runTaskAsynchronously( __plugin, fn); + return; + } + if (__plugin.canary){ + setTimeout(fn,20); + return; + } +} +exports.request = function( request, callback ) { + invokeLater( function() { + var url, paramsAsString, conn, requestMethod; + if (typeof request === 'string'){ + url = request; + requestMethod = 'GET'; + }else{ + url = request.url; + paramsAsString = paramsToString( request.params ); + if ( request.method ) { + requestMethod = request.method; + } else { + requestMethod = 'GET'; + } + if ( requestMethod == 'GET' && request.params ) { + // append each parameter to the URL + url = request.url + '?' + paramsAsString; + } + } + conn = new java.net.URL( url ).openConnection(); + conn.requestMethod = requestMethod; + conn.doOutput = true; + conn.instanceFollowRedirects = false; + + if ( conn.requestMethod == 'POST' ) { + conn.doInput = true; + // put each parameter in the outputstream + conn.setRequestProperty('Content-Type', 'application/x-www-form-urlencoded'); + conn.setRequestProperty('charset', 'utf-8'); + conn.setRequestProperty('Content-Length', '' + paramsAsString.length); + conn.useCaches =false ; + var wr = new java.io.DataOutputStream(conn.getOutputStream ()); + wr.writeBytes(paramsAsString); + wr.flush(); + wr.close(); + } + var rc = conn.responseCode; + var response; + var stream; + if ( rc == 200 ) { + stream = conn.getInputStream(); + response = new java.util.Scanner( stream ).useDelimiter("\\A").next(); + } + invokeNow( function( ) { + callback( rc, response ); + }); + }); + +}; diff --git a/src/main/js/modules/input.js b/src/main/js/modules/input.js new file mode 100644 index 0000000..20d7581 --- /dev/null +++ b/src/main/js/modules/input.js @@ -0,0 +1,63 @@ +/************************************************************************* +## Asynchronous Input Module + +The `input` module provides a simple way to prompt players for input at the +in-game prompt. In Javascript browser environments the `prompt()` function provides +a way to block execution and ask the user for input. Execution is blocked until the user +provides input using the modal dialog and clicks OK. Unfortunately Minecraft provides no +equivalent modal dialog which can be used to gather player text input. The only way to gather text +input from the player in Minecraft is to do so asynchronously. That is - a prompt message can be +sent to the player but the player is not obliged to provide input immediately, nor does the program +execution block until the player does so. + +So ScriptCraft has no `prompt()` implementation because `prompt()` is a synchronous function and +Minecraft's API provides no equivalent functions or classes which can be used to implement this synchronously. +The Minecraft API does however have a 'Conversation' API which allows for prompting of the player and asynchronously gathering text input from the player. + +This new `input()` function is best illustrated by example. The following code is for a number-guessing game: + +```javascript +var input = require('input'); +exports.numberguess = function(player){ + var randomNumber = Math.ceil(Math.random() * 10); + input( player, 'Think of a number between 1 and 10 (q to quit)', function( guess, guesser, repeat ) { + if ( guess == 'q'){ + return; + } + if ( +guess !== randomNumber ) { + if (+guess < randomNumber ) { + echo( guesser, 'Too low - guess again'); + } + if (+guess > randomNumber ) { + echo( guesser, 'Too high - guess again'); + } + repeat(); + } else { + echo( guesser, 'You guessed correctly'); + } + }); +}; +``` + +The `input()` function takes 3 parameters, the player, a prompt message and a callback which will be invoked when the player has entered some text at the in-game command prompt. +The callback is bound to an object which has the following properties: + + * sender : The player who input the text + * value : The value of the text which has been input. + * message: The message prompt. + * repeat: A function which when invoked will repeat the original prompt. (this is for flow control) + +The callback function as well as being bound to an object with the above properties (so you can use this.value inside your callback to get the value which has just been input), can also take the following parameters (in exact order): + + * value + * sender + * repeat + +The `value` parameter will be the same as `this.value`, the `repeat` parameter will be the same as `this.repeat` and so on. + +***/ +if (__plugin.canary) { + module.exports = require('./canary/input'); +} else { + module.exports = require('./bukkit/input'); +} diff --git a/src/main/js/modules/inventory/index.js b/src/main/js/modules/inventory/index.js new file mode 100644 index 0000000..72583dc --- /dev/null +++ b/src/main/js/modules/inventory/index.js @@ -0,0 +1,43 @@ +/************************************************************************ +## Inventory Module +This module provides functions to add items to, remove items from and check the +contents of a player or NPC's inventory. + +### Usage +The inventory module is best used in conjunction with the items module. See below for examples of usage. + +```javascript +var inventory = require('inventory'); +var items = require('items'); +var utils = require('utils'); + +// gives every player a cookie and a baked potatoe +utils.players(function(player){ + inventory(player) + .add( items.cookie(1) ) + .add( items.bakedPotato(1) ) +}); + +// give a player 6 cookies then take away 4 of them + +inventory(player) + .add( items.cookie(6) ) + .remove ( items.cookie(4) ) + +// check if a player has any cookies + +var hasCookies = inventory(player).contains( items.cookie(1) ); + +``` +The inventory module exposes a single function which when passed a player or NPC will return an object with 3 methods: + +* add : Adds items to the inventory (Expects parameters of type `net.canarymod.api.inventory.Item` - I strongly recommend using the `items` module for constructing items) +* remove : removes items from the inventory (Expects parameters of type `net.canarymod.api.inventory.Item` - I strongly recommend using the `items` module for constructing items) +* contains : checks to see if there is the specified type and amount of item in the inventory (Expects parameters of type `net.canarymod.api.inventory.Item` - I strongly recommend using the `items` module for constructing items) + +***/ +if ( __plugin.canary ) { + module.exports = require('../canary/inventory'); +} else { + module.exports = require('../bukkit/inventory'); +} diff --git a/src/main/js/modules/items.js b/src/main/js/modules/items.js new file mode 100644 index 0000000..c5ae4cc --- /dev/null +++ b/src/main/js/modules/items.js @@ -0,0 +1,5 @@ +if (__plugin.canary) { + module.exports = require('./canary/items'); +} else { + module.exports = require('./bukkit/items'); +} diff --git a/src/main/js/modules/lightning.js b/src/main/js/modules/lightning.js new file mode 100644 index 0000000..b1e8a41 --- /dev/null +++ b/src/main/js/modules/lightning.js @@ -0,0 +1,30 @@ +'use strict'; +/************************************************************************ +## Lightning module + +Causes a bolt of lightning to strike. + +### Usage +```javascript +// strike lightning wherever a player's arrow lands +var lightning = require('lightning'); +events.projectileHit( function( event ){ + if ( entities.arrow( event.projectile ) // it's an arrow + && entities.player( event.projectile.owner ) // it was shot by a player + ) { + lightning( event.projectile ); // strike lightning at the arrow location + } +}); +``` + +***/ +module.exports = function lightning( something ) { + if (__plugin.canary && something.location){ + return something.location.world.makeLightningBolt(something.location); + } + if (__plugin.bukkit && something.location){ + return something.location.world.strikeLightning(something.location); + } + console.log('Need an object with a location property for lightning strike'); + return null; +}; diff --git a/src/main/js/modules/minigames/scoreboard.js b/src/main/js/modules/minigames/scoreboard.js new file mode 100644 index 0000000..6fc34f4 --- /dev/null +++ b/src/main/js/modules/minigames/scoreboard.js @@ -0,0 +1,70 @@ +'use strict'; +/*global Packages, require, server, exports*/ + +/* + wph 20150103 - temporarily commenting out - textcolors was removed. + var textcolors = require('textcolors'); +*/ +var sb; +if (__plugin.canary){ + var Canary = Packages.net.canarymod.Canary; + sb = Canary.scoreboards().getScoreboard(); +} else { + console.warn('Scoreboard not yet supported in CraftBukkit'); + return; +} +function execCommand( command ){ + server.executeVanillaCommand(server, command); +} +function getTeamByName( teamName ){ + var allTeams = sb.getTeams().toArray(); + for (var i = 0;i < allTeams.length; i++){ + if (allTeams[i].displayName == teamName){ + return allTeams[i]; + } + } + return null; +} +function createScoreboard( objectiveName, displayName ){ + execCommand('scoreboard objectives add ' + objectiveName + ' dummy ' + displayName); + execCommand('scoreboard objectives setdisplay sidebar ' + objectiveName); +} +function addTeamToScoreboard( teamName, color){ + execCommand('scoreboard teams add ' + teamName); + var team = getTeamByName( teamName ); + /* + wph 20150103 - temporarily commenting out - textcolors was removed. + team.prefix = textcolors.colorize(color, ''); + */ + //execCommand('scoreboard teams option ' + teamName + ' color ' + color); +} +function removeScoreboard( name ){ + //execCommand('scoreboard objectives remove ' + name ); + sb['removeScoreObjective(String)'](name); +} +function addPlayerToTeam( objectiveName, teamName, playerName ){ + execCommand('scoreboard teams join ' + teamName + ' ' + playerName); + execCommand('scoreboard players set ' + playerName + ' ' + objectiveName + ' -1'); + updatePlayerScore( objectiveName, playerName, 0); +} + +function updatePlayerScore( objectiveName, playerName, score ){ + /* + wph 20150801 - this fails with CanaryMod 1.8.2 so use command instead - messy for ops but non-ops won't see messages + + var sc = sb['getScore(String, ScoreObjective)']( playerName, sb.getScoreObjective( objectiveName) ); + sc.score = score; + */ + execCommand('scoreboard players set ' + playerName + ' ' + objectiveName + ' ' + score); +} + +function removeTeamFromScoreboard( teamName ){ + execCommand('scoreboard teams remove ' + teamName); + //sb['removeTeam(String)'](teamName); +} +exports.create = createScoreboard; +exports.addTeam = addTeamToScoreboard; +exports.removeTeam = removeTeamFromScoreboard; +exports.addPlayerToTeam = addPlayerToTeam; +exports.updateScore = updatePlayerScore; +exports.remove = removeScoreboard; diff --git a/src/main/js/modules/recipes.js b/src/main/js/modules/recipes.js new file mode 100644 index 0000000..ccefbb4 --- /dev/null +++ b/src/main/js/modules/recipes.js @@ -0,0 +1,34 @@ +/************************************************************************* +## The recipes module + +The Recipes module provides convenience functions for adding and removing recipes +from the game. + +### Example +To add an EnderBow to the game (assumes there's an enchanted Item variable called enderBow)... + + var recipes = require('recipes'); + var items = require('items'); + ... + var enderBowRecipe = recipes.create( { + result: enderBow, + ingredients: { + E: items.enderPearl(1), + S: items.stick(1), + W: items.string(1) + }, + shape: [ 'ESW', + 'SEW', + 'ESW' ] + } ); + // add to server + var addedRecipe = server.addRecipe( enderBowRecipe ); + // to remove... + server.removeRemove( addedRecipe ); + +***/ +if (__plugin.canary) { + module.exports = require('./canary/recipes'); +} else { + module.exports = require('./bukkit/recipes'); +} diff --git a/src/main/js/modules/sc-mqtt.js b/src/main/js/modules/sc-mqtt.js new file mode 100644 index 0000000..da1e135 --- /dev/null +++ b/src/main/js/modules/sc-mqtt.js @@ -0,0 +1,158 @@ +'use strict'; +/************************************************************************* +## sc-mqtt module + +This module provides a simple way to communicate with devices (such as Arduino) +using the popular lightweight [MQTT protocol][mqtt]. + +### Usage + +This module can only be used if the separate `sc-mqtt.jar` file is +present in the CraftBukkit classpath. To use this module, you should +... + + 1. Download sc-mqtt.jar from + 2. Save the file to the same directory where craftbukkit.jar resides. + 3. Create a new batch file (windows-only) called + craftbukkit-sc-mqtt.bat and edit it to include the following + command... + + ```sh + java -classpath sc-mqtt.jar;craftbukkit.jar org.bukkit.craftbukkit.Main + ``` + + If you're using Mac OS, create a new craftbukkit-sc-mqtt.command + file and edit it (using TextWrangler or another text editor) ... + + ```sh + java -classpath sc-mqtt.jar:craftbukkit.jar org.bukkit.craftbukkit.Main + ``` + + 4. Execute the craftbukkit-sc-mqtt batch file / command file to start + Craftbukkit. You can now begin using this module to send and receive + messages to/from a Net-enabled Arduino or any other device which uses + the [MQTT protocol][mqtt] + + ```javascript + var mqtt = require('sc-mqtt'); + // create a new client + var client = mqtt.client( 'tcp://localhost:1883', 'uniqueClientId' ); + // connect to the broker + client.connect( { keepAliveInterval: 15 } ); + // publish a message to the broker + client.publish( 'minecraft', 'loaded' ); + // subscribe to messages on 'arduino' topic + client.subscribe( 'arduino' ); + // do something when an incoming message arrives... + client.onMessageArrived( function( topic, message ) { + console.log( 'Message arrived: topic=' + topic + ', message=' + message ); + }); + + ``` + +The `sc-mqtt` module provides a very simple minimal wrapper around the +[Eclipse Paho MQTT Version 3 Client][pahodocs] java-based MQTT +library. + +[pahodocs]: http://pic.dhe.ibm.com/infocenter/wmqv7/v7r5/index.jsp?topic=/com.ibm.mq.javadoc.doc/WMQMQxrClasses/org/eclipse/paho/client/mqttv3/package-summary.html +[mqtt]: http://mqtt.org/ + +***/ +var MISSING_MQTT = '\nMissing class org.walterhiggins.scriptcraft.ScriptCraftMqttCallback.\n' + + 'Make sure sc-mqtt.jar is in the classpath.\n' + + 'See http://github.com/walterhiggins/scriptcraft-extras-mqtt for details.\n'; + +function Client( brokerUrl, clientId ) { + + var Callback = org.walterhiggins.scriptcraft.ScriptCraftMqttCallback; + var MqttClient = org.eclipse.paho.client.mqttv3.MqttClient; + + var callback = new Callback( + function( err ) { + console.log( 'connectionLost: ' + err ); + }, + function( topic, message ) { + console.log( 'messageArrived ' + topic + '> ' + message ); + }, + function( token ) { + console.log( 'deliveryComplete:' + token ); + } + ); + + if ( !brokerUrl ) { + brokerUrl = 'tcp://localhost:1883'; + } + if ( !clientId ) { + clientId = 'scriptcraft' + new Date().getTime(); + } + var client = new MqttClient( brokerUrl, clientId, null ); + client.setCallback( callback ); + return { + connect: function( options ) { + if ( typeof options === 'undefined' ) { + client.connect(); + }else{ + client.connect(options); + } + return client; + }, + + disconnect: function( quiesceTimeout ) { + if ( typeof quiesceTimeout == 'undefined' ) { + client.disconnect(); + } else { + client.disconnect( quiesceTimeout ); + } + return client; + }, + + publish: function( topic, message, qos, retained ) { + if ( typeof message == 'string' ) { + message = new java.lang.String( message ).bytes; + } + if (typeof qos == 'undefined'){ + qos = 1; + } + if (typeof retained == 'undefined'){ + retained = false; + } + client.publish( topic, message,qos, retained ); + return client; + }, + + subscribe: function( topic ) { + client.subscribe( topic ); + return client; + }, + + unsubscribe: function( topic ) { + client.unsubscribe( topic ); + return client; + }, + + onMessageArrived: function( fn ) { + callback.setMesgArrived( fn ); + return client; + }, + + onDeliveryComplete: function( fn ) { + callback.setDeliveryComplete( fn ); + return client; + }, + + onConnectionLost: function( fn ) { + callback.setConnLost( fn ); + return client; + } + }; +} +/* + Return a new MQTT Client +*/ +exports.client = function( brokerUrl, clientId, options ) { + if ( typeof org.walterhiggins.scriptcraft.ScriptCraftMqttCallback != 'function' ) { + throw MISSING_MQTT; + } + return new Client( brokerUrl, clientId, options ); +}; + diff --git a/src/main/js/modules/signs/menu.js b/src/main/js/modules/signs/menu.js new file mode 100644 index 0000000..02a43c5 --- /dev/null +++ b/src/main/js/modules/signs/menu.js @@ -0,0 +1,215 @@ +'use strict'; +/*global events, require, org, module, persist, __plugin*/ +var utils = require('utils'), + stringExt = require('utils/string-exts'), + store = persist('signs',{}); + +/* + Define the signs module - signs are persistent + (that is - a menu sign will still be a menu after the + server has shut down and started up) plugins now have persistent state - Yay! +*/ +var signs = { }; +var hasSign = null; +module.exports = function(hs){ + hasSign = hs; + return signs; +}; + +var setLine = null; +if (__plugin.canary){ + setLine = function(sign, i, text){ + sign.setTextOnLine( text, i); + }; +} +if (__plugin.bukkit){ + setLine = function(sign, i, text){ + sign.setLine( i, text); + }; +} + +/* + redraw a menu sign +*/ +var _redrawMenuSign = function( p_sign, p_selectedIndex, p_displayOptions ) { + var optLen = p_displayOptions.length, + i, + text; + // the offset is where the menu window begins + var offset = Math.max( 0, Math.min( optLen-3, Math.floor( p_selectedIndex/3 ) * 3) ); + for ( i = 0;i < 3; i++ ) { + text = ""; + if ( offset+i < optLen ) { + text = p_displayOptions[offset+i]; + } + if ( offset+i == p_selectedIndex ) { + text = ('' + text).replace(/^ /,">"); + } + setLine(p_sign, i+1, text); + } + if (__plugin.canary){ + p_sign.update(); + } + if (__plugin.bukkit){ + p_sign.update( true ); + } +}; + +var _updaters = {}; +/* + construct an interactive menu to be subsequently attached to + one or more Signs. +*/ +signs.menu = signMenu; + +function signMenu( label, options, callback, selectedIndex ) { + + if ( typeof selectedIndex == "undefined" ) { + selectedIndex = 0; + } + // + // variables common to all instances of this menu can go here + // + var labelPadding = "---------------"; + var optionPadding = " "; + var i; + var paddedLabel = ( labelPadding + label + labelPadding) + .substr( ( ( label.length+30 ) / 2 ) - 7, 15 ); + var optLen = options.length; + var displayOptions = []; + for ( i = 0; i < options.length; i++ ) { + displayOptions[i] = (" " + options[i] + optionPadding).substring(0,15); + } + /* + this function is returned by signs.menu and when it is invoked it will + attach menu behaviour to an existing sign in the world. + signs.menu is for use by Plugin Authors. + The function returned by signs.menu is for use by admins/ops. + */ + var convertToMenuSign = function(/* Sign */ sign, save) { + if (typeof save == "undefined") { + save = true; + } + // + // per-sign variables go here + // + var cSelectedIndex = selectedIndex; + setLine(sign, 0, paddedLabel.bold()); + var _updateSign = function( p_player, p_sign ) { + cSelectedIndex = ( cSelectedIndex + 1 ) % optLen; + _redrawMenuSign( p_sign, cSelectedIndex, displayOptions ); + var signSelectionEvent = { + player: p_player, + sign: p_sign, + text: options[ cSelectedIndex ], + number: cSelectedIndex + }; + callback( signSelectionEvent ); + }; + + /* + get a unique ID for this particular sign instance + */ + var signLoc = sign.block.location; + var menuSignSaveData = utils.locationToJSON( signLoc ); + var menuSignUID = JSON.stringify( menuSignSaveData ); + /* + keep a reference to the update function for use by the event handler + */ + _updaters[ menuSignUID ] = _updateSign; + + // initialize the sign + _redrawMenuSign( sign, cSelectedIndex, displayOptions ); + + /* + whenever a sign is placed somewhere in the world + (which is what this function does) + save its location for loading and initialization + when the server starts up again. + */ + if ( save ) { + if ( typeof store.menus == "undefined") { + store.menus = {}; + } + var signLocations = store.menus[label]; + if ( typeof signLocations == "undefined" ) { + signLocations = store.menus[label] = []; + } + signLocations.push( menuSignSaveData ); + } + return sign; + }; // end of convertToMenuSign function + + /* + a new sign definition - need to store (in-memory only) + its behaviour and bring back to life other signs of the + same type in the world. Look for other static signs in the + world with this same label and make dynamic again. + */ + + if ( store.menus && store.menus[label] ) { + var signsOfSameLabel = store.menus[ label ]; + var defragged = []; + var len = signsOfSameLabel.length; + for ( i = 0; i < len; i++ ) { + var loc = utils.locationFromJSON(signsOfSameLabel[i]); + var block = utils.blockAt(loc); + var sign = hasSign(block); + if ( sign) { + convertToMenuSign( sign, false ); + defragged.push( loc ); + } + } + /* + remove data for signs which no longer exist. + */ + if ( defragged.length != len ) { + store.menus[label] = defragged; + } + } + return convertToMenuSign; +}; + +/* +if (__plugin.canary){ + console.warn('signs/menu is not yet supported in CanaryMod'); + return; +} +*/ +if (__plugin.canary){ + events.blockRightClick( function( event ){ + var sign = hasSign(event.blockClicked); + if (! sign){ + // it's not a sign + return; + } + var evtLocStr = utils.locationToString(event.blockClicked.location); + var signUpdater = _updaters[evtLocStr]; + if ( signUpdater ) { + signUpdater( event.player, sign); + } + + }); +} +if (__plugin.bukkit){ + // + // update it every time player interacts with it. + // + events.playerInteract( function( event ) { + /* + look up our list of menu signs. If there's a matching location and there's + a sign, then update it. + */ + var sign = hasSign(event.clickedBlock); + if ( ! sign ) { + return; + } + var evtLocStr = utils.locationToString(event.clickedBlock.location); + var signUpdater = _updaters[evtLocStr]; + if ( signUpdater ) { + signUpdater( event.player, sign ); + } + }); +} + + diff --git a/src/main/js/modules/signs/package.json b/src/main/js/modules/signs/package.json new file mode 100644 index 0000000..520d7aa --- /dev/null +++ b/src/main/js/modules/signs/package.json @@ -0,0 +1,4 @@ +{ + name: 'signs', + main: './signs.js' +} diff --git a/src/main/js/modules/signs/signs.js b/src/main/js/modules/signs/signs.js new file mode 100644 index 0000000..5d6d2ab --- /dev/null +++ b/src/main/js/modules/signs/signs.js @@ -0,0 +1,126 @@ +'use strict'; +/*global __plugin, require, module, exports*/ +/************************************************************************ +## Signs Module + +The Signs Module can be used by plugin authors to create interactive +signs - that is - signs which display a list of choices which can be +changed by interacting (right-clicking) with the sign. + +### signs.menu() function + +This function is used to construct a new interactive menu on top of an +existing sign in the game world. + +#### Parameters + + * Label : A string which will be displayed in the topmost line of the + sign. This label is not interactive. + * options : An array of strings which can be selected on the sign by + right-clicking/interacting. + * callback : A function which will be called whenever a player + interacts (changes selection) on a sign. This callback in turn + takes as its parameter, an object with the following properties... + + * player : The player who interacted with the sign. + * sign : The [org.bukkit.block.Sign][buksign] which the player interacted with. + * text : The text for the currently selected option on the sign. + * number : The index of the currently selected option on the sign. + + * selectedIndex : optional: A number (starting at 0) indicating which + of the options should be selected by default. 0 is the default. + +#### Returns +This function does not itself do much. It does however return a +function which when invoked with a given +[org.bukkit.block.Sign][buksign] object, will convert that sign into +an interactive sign. + +#### Example: Create a sign which changes the time of day. + +##### plugins/signs/time-of-day.js + +```javascript +var utils = require('utils'), + signs = require('signs'); + +var onTimeChoice = function(event){ + var selectedIndex = event.number; + // convert to Minecraft time 0 = Dawn, 6000 = midday, 12000 = dusk, 18000 = midnight + var time = selectedIndex * 6000; + event.player.location.world.setTime(time); +}; + +// signs.menu returns a function which can be called for one or more signs in the game. +var convertToTimeMenu = signs.menu('Time of Day', + ['Dawn', 'Midday', 'Dusk', 'Midnight'], + onTimeChoice); + +exports.time_sign = function( player ){ + var sign = signs.getTargetedBy(player); + if ( !sign ) { + throw new Error('You must look at a sign'); + } + convertToTimeMenu(sign); +}; +``` + +To use the above function at the in-game prompt, look at an existing +sign and type... + + /js time_sign(self); + +... and the sign you're looking at will become an interactive sign +which changes the time each time you interact (right-click) with it. + +### signs.getTargetedBy() function + +This function takes a [org.bukkit.entity.LivingEntity][bukle] as a +parameter and returns a [org.bukkit.block.Sign][buksign] object which +the entity has targeted. It is a utility function for use by plugin authors. + +#### Example + +```javascript +var signs = require('signs'), + utils = require('utils'); +var player = utils.player('tom1234'); +var sign = signs.getTargetedBy( player ); +if ( !sign ) { + echo( player, 'Not looking at a sign'); +} +``` + +[buksign]: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/block/Sign.html +[bukle]: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/LivingEntity.html + +***/ +function hasSign( block ){ + if (__plugin.canary){ + if (block && block.tileEntity && block.tileEntity.setTextOnLine){ + return block.tileEntity; + } + } + if (__plugin.bukkit){ + if (block && block.state && block.state.setLine){ + return block.state; + } + } + return false; +} +var utils = require('utils'); +var menu = require('./menu')(hasSign); +// include all menu exports +for ( var i in menu ) { + exports[i] = menu[i]; +} + +function getTargetedBy( livingEntity ) { + var location = utils.getMousePos( livingEntity ); + if ( !location ) { + return null; + } + return hasSign(utils.blockAt(location)); +} +exports.getTargetedBy = getTargetedBy; +exports.hasSign = hasSign; diff --git a/src/main/js/modules/slash.js b/src/main/js/modules/slash.js new file mode 100644 index 0000000..92e66a7 --- /dev/null +++ b/src/main/js/modules/slash.js @@ -0,0 +1,65 @@ +'use strict'; +/*global module, require, server, __plugin*/ +var _ = require('underscore'); +/************************************************************************ +## The slash Module + +This module provides a single function which makes it easy to execute +minecraft commands via javascript. + +### The slash() function + +This function makes it easy to execute one or more minecraft commands. + +#### Parameters + + * commands : A String or Array of strings - each string is a command to be executed. + * sender: (optional) The player on whose behalf the commands should be executed. If not specified the commands will be executed as the server console user. + +#### Examples + +Invoke the `/defaultgamemode creative` command (as server). + +```javascript +var slash = require('slash'); +slash('defaultgamemode creative'); +``` + +Set the time of day to Midday and toggle downfall (as player 'JohnDoe'): + +```javascript +var slash = require('slash'), + utils = require('utils'); +var johnDoe = utils.player('John_Doe'); + +slash([ + 'time set 6000', + 'toggledownfall' +], johnDoe); +``` + +***/ +function slash( commands, sender ){ + if (_.isArray(commands)){ + _.each(commands, function(command){ + slash(command, sender); + }); + return; + } + if (__plugin.canary){ + if (sender === server){ + server.consoleCommand( commands ); + } else { + server.consoleCommand( commands, sender ); + } + } + if (__plugin.bukkit){ + if (!sender){ + // if sender is not specified assume server console + server.dispatchCommand(server.consoleSender, commands); + } else { + server.dispatchCommand(sender, commands); + } + } +} +module.exports = slash; diff --git a/src/main/js/modules/sounds.js b/src/main/js/modules/sounds.js new file mode 100644 index 0000000..4065cc3 --- /dev/null +++ b/src/main/js/modules/sounds.js @@ -0,0 +1,32 @@ +/************************************************************************* +## Sounds Module + +This module is a simple wrapper around the Bukkit Sound class and provides +a simpler way to play sounds. All of the org.bukkit.Sound Enum values are attached. + +### Usage (Bukkit) : + + var sounds = require('sounds'); + sounds.play( bukkit.sound.VILLAGER_NO , self, 1, 0); // plays VILLAGER_NO sound at full volume and medium pitch + sounds.play( bukkit.sound.VILLAGER_NO , self ); // same as previous statement + +The play() function takes either a Location object or any object which has a location. +The volume parameter is in the range 0 to 1 and the pitch parameter is in the range 0 to 4. + +In addition, a play function is provided for each possible sound using the following rules: + +1. The sound is converted from ALL_CAPS_UNDERSCORE to camelCase so for example there is a sounds.villagerNo() function which will play the VILLAGER_NO sound. +2. Each such function can take 3 parameters: location (which can be either an actual Location object or an object which has a location), volume and pitch +3. Or... each such function can be called without parameters meaning the sound will be played for all online players to hear. + + sounds.villagerNo(self, 1, 0); // plays VILLAGER_NO sound at full volume and medium pitch at invoker's location + + sounds.villagerNo(); // plays VILLAGER_NO sound for all players online. + +These methods are provided for convenience to help beginners explore sounds using TAB completion. +***/ +if (__plugin.canary) { + module.exports = require('./canary/sounds'); +} else { + module.exports = require('./bukkit/sounds'); +} diff --git a/src/main/js/modules/spawn.js b/src/main/js/modules/spawn.js new file mode 100644 index 0000000..471d9d5 --- /dev/null +++ b/src/main/js/modules/spawn.js @@ -0,0 +1,46 @@ +/*global require, module, __plugin, Packages*/ +'use strict'; +var entities = require('entities'); +/************************************************************************ +## Spawn Module + +Provides a single function to 'spawn' an entity at a given location. + +### Parameters + + * entityType - The type of entity to spawn. This can be a string (see entities module for reference) or a framework-specific object type (see https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/EntityType.html). A list of [all possible entities][ents] functions (equivalent to the EntityType enum). + + * location - where the entity should be spawned. + +[ents]: #entities-module + +### Example + +Using the entities module as a helper, spawn a new polar bear at the world's default spawn location: + +```javascript +var entities = require('entities'), + spawn = require('spawn'); +... +var spawnLocation = world.spawnLocation; +spawn(entities.polar_bear(), spawnLocation); +``` + +This module is in turn used by the Drone's `spawn()` method and the `jsp spawn` command. +***/ +module.exports = function(entityType, location){ + var entityTypeFn; + if (typeof entityType === 'string'){ + entityTypeFn = entities[entityType.toLowerCase()]; + entityType = entityTypeFn(); + } + var world = location.world; + if (__plugin.bukkit){ + world.spawnEntity( location, entityType); + } + if (__plugin.canary){ + var Canary = Packages.net.canarymod.Canary, + entityInstance = Canary.factory().entityFactory.newEntity(entityType, location); + entityInstance.spawn(); + } +}; diff --git a/src/main/js/modules/teleport.js b/src/main/js/modules/teleport.js new file mode 100644 index 0000000..f98524d --- /dev/null +++ b/src/main/js/modules/teleport.js @@ -0,0 +1,59 @@ +'use strict'; +/*global __plugin, org, module, require*/ +var utils = require('utils'); +/************************************************************************ +## Teleport Module + +This module provides a function to teleport entities (Players or NPCs). + +### Parameters + + * entity - The player or NPC to be teleported. If of type String, then a player with that name will be teleported. + * destination - The location to which they should be teleported. If not of type Location but is a Player, Block or any + object which has a `location` property then that works too. If of type String, then it's assumed that the destination is the player with that name. + +### Example + +The following code will teleport each player back to their spawn position. + +```javascript +var teleport = require('teleport'), + utils = require('utils'), + players = utils.players(), + i = 0; +for ( ; i < players.length; i++ ) { + teleport( players[i], players[i].spawnPosition ); +} +``` + +The following code will teleport 'tom' to 'jane's location. + +```javascript +var teleport = require('teleport'); +teleport('tom' , 'jane'); +``` +***/ +function teleport( entity, destination){ + if (typeof entity === 'String' || entity instanceof java.lang.String){ + entity = utils.player(entity); + } + if (typeof destination === 'String' || destination instanceof java.lang.String){ + var player = utils.player(destination); + if (player){ + destination = player.location; + } + } else { + if (destination.location){ + destination = destination.location; + } + } + if (__plugin.bukkit){ + var bkTeleportCause = org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; + entity.teleport( destination, bkTeleportCause.PLUGIN); + } + if (__plugin.canary){ + var cmTeleportCause = Packages.net.canarymod.hook.player.TeleportHook.TeleportCause; + entity.teleportTo(destination, cmTeleportCause.PLUGIN); + } +} +module.exports = teleport; diff --git a/src/main/js/modules/utils/package.json b/src/main/js/modules/utils/package.json new file mode 100644 index 0000000..d80809d --- /dev/null +++ b/src/main/js/modules/utils/package.json @@ -0,0 +1,4 @@ +{ + "name": 'utils', + "main": './utils.js' +} diff --git a/src/main/js/modules/utils/string-exts.js b/src/main/js/modules/utils/string-exts.js new file mode 100644 index 0000000..14b767d --- /dev/null +++ b/src/main/js/modules/utils/string-exts.js @@ -0,0 +1,79 @@ +'use strict'; +/************************************************************************ +String class extensions +----------------------- +The following chat-formatting methods are added to the javascript String class.. + + * aqua() + * black() + * blue() + * bold() + * brightgreen() + * darkaqua() + * darkblue() + * darkgray() + * darkgreen() + * purple() + * darkpurple() + * darkred() + * gold() + * gray() + * green() + * italic() + * lightpurple() + * indigo() + * green() + * red() + * pink() + * yellow() + * white() + * strike() + * random() + * magic() + * underline() + * reset() + +Example +------- + + /js var boldGoldText = "Hello World".bold().gold(); + /js echo(self, boldGoldText ); + +

Hello World

+ +***/ +var COLOR_CHAR = '\u00a7'; +var formattingCodes = { + aqua: 'b', + black: '0', + blue: '9', + bold: 'l', + brightgreen: 'a', + darkaqua: '3', + darkblue: '1', + darkgray: '8', + darkgreen: '2', + purple: 'd', + darkpurple: '5', + darkred: '4', + gold: '6', + gray: '7', + green: 'a', + italic: 'o', + lightpurple: 'd', + indigo: '9', + red: 'c', + pink: 'd', + yellow: 'e', + white: 'f', + strike: 'm', + random: 'k', + magic: 'k', + underline: 'n', + reset: 'r' +}; +for ( var method in formattingCodes ) { + String.prototype[method] = function( c ) { + return function(){ return c + this; }; + }( COLOR_CHAR + formattingCodes[method] ); +} diff --git a/src/main/js/modules/utils/utils.js b/src/main/js/modules/utils/utils.js new file mode 100644 index 0000000..11dd211 --- /dev/null +++ b/src/main/js/modules/utils/utils.js @@ -0,0 +1,743 @@ +/*global require, __plugin, org, exports, server, setTimeout, Packages, setInterval, addUnloadHandler, clearInterval, events*/ +'use strict'; +var File = java.io.File; + +if (__plugin.bukkit){ + var bkBukkit = org.bukkit.Bukkit, + bkLocation = org.bukkit.Location, + bkBlockCommandSender = org.bukkit.command.BlockCommandSender; +} +if (__plugin.canary){ + var Canary = Packages.net.canarymod.Canary; +} +/************************************************************************ +## Utilities Module + +The `utils` module is a storehouse for various useful utility +functions which can be used by plugin and module authors. It contains +miscellaneous utility functions and classes to help with programming. + +### utils.player() function + +The utils.player() function will return a [Player][cmpl] object +with the given name. This function takes a single parameter +`playerName` which can be either a String or a [Player][cmpl] object - +if it's a Player object, then the same object is returned. If it's a +String, then it tries to find the player with that name. + +#### Parameters + + * playerName : A String or Player object. If no parameter is provided + then player() will try to return the `self` variable . It is + strongly recommended to provide a parameter. + +#### Example + +```javascript +var utils = require('utils'); +var name = 'walterh'; +var player = utils.player(name); +if ( player ) { + echo(player, 'Got ' + name); +} else { + console.log('No player named ' + name); +} +``` + +[bkpl]: http://jd.bukkit.org/dev/apidocs/org/bukkit/entity/Player.html +[cmpl]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/entity/living/humanoid/Player.html +[cmloc]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/world/position/Location.html +[bkloc]: http://jd.bukkit.org/dev/apidocs/org/bukkit/Location.html + +***/ +function _player( playerName ) { + if ( typeof playerName == 'undefined' ) { + if ( typeof self == 'undefined' ) { + return null; + } else { + return self; + } + } else { + if ( typeof playerName == 'string' ) + if (__plugin.canary) { + return Canary.server.getPlayer( playerName ); + } else { + return bkBukkit.getPlayer( playerName ); + } + else + return playerName; // assumes it's a player object + } +}; +/************************************************************************* +### utils.world( worldName ) function + +Returns a World object matching the given name + +***/ +function _world( worldName ){ + if (__plugin.canary){ + if (worldName instanceof Packages.net.canarymod.api.world.World){ + return worldName; + } + var worldMgr = Canary.server.worldManager; + try { + if (worldName === undefined){ + var worldNames = worldMgr.getLoadedWorldsNames(); + worldName = worldNames[0]; + } + return worldMgr.getWorld( worldName, true ); + } catch (error) { + console.error( 'utils.world() failed to load ' + worldName + ',Error:' + error ); + } + } + if (__plugin.bukkit){ + if (worldName instanceof org.bukkit.World){ + return worldName; + } + if (worldName === undefined){ + return bkBukkit.getWorlds().get(0); + } + return bkBukkit.getWorld( worldName ); + } + return null; +} +exports.world = _world; + +/************************************************************************* +### utils.blockAt( Location ) function + +Returns the Block at the given location. + +***/ +function _blockAt( location ){ + if (__plugin.canary){ + return location.world.getBlockAt(location); + } + if (__plugin.bukkit){ + return location.block; + } + return null; +} +exports.blockAt = _blockAt; +/************************************************************************* +### utils.locationToJSON() function + +utils.locationToJSON() returns a [Location][cmloc] object in JSON form... + + { world: 'world5', + x: 56.9324, + y: 103.9954, + z: 43.1323, + yaw: 0.0, + pitch: 0.0 + } + +This can be useful if you write a plugin that needs to store location data since bukkit's Location object is a Java object which cannot be serialized to JSON by default. + +#### Parameters + + * location: An object of type [Location][cmloc] + +#### Returns + +A JSON object in the above form. + +***/ +function _locationToJSON( location ) { + var yaw = __plugin.bukkit ? location.yaw : (__plugin.canary ? location.rotation : 0); + return { + world: ''+location.world.name, + x: location.x, + y: location.y, + z: location.z, + yaw: yaw, + pitch: location.pitch + }; +}; +/************************************************************************* +### utils.locationToString() function + +The utils.locationToString() function returns a +[Location][cmloc] object in string form... + + '{"world":"world5",x:56.9324,y:103.9954,z:43.1323,yaw:0.0,pitch:0.0}' + +... which can be useful if you write a plugin which uses Locations as +keys in a lookup table. + +#### Example + +```javascript +var utils = require('utils'); +... +var key = utils.locationToString(player.location); +lookupTable[key] = player.name; +``` + +***/ +exports.locationToString = function locationToString( location ) { + return JSON.stringify( _locationToJSON( location ) ); +}; +exports.locationToJSON = _locationToJSON; + +/************************************************************************* +### utils.locationFromJSON() function + +This function reconstructs an [Location][cmloc] object from +a JSON representation. This is the counterpart to the +`locationToJSON()` function. It takes a JSON object of the form +returned by locationToJSON() and reconstructs and returns a bukkit +Location object. + +***/ +exports.locationFromJSON = function locationFromJSON( json ) { + var world; + if ( json.constructor == Array ) { + // for support of legacy format + world = _world( json[0] ); + return new bkLocation( world, json[1], json[2] , json[3] ); + } else { + if (__plugin.canary){ + world = _world( json.world ); + var cmLocation = Packages.net.canarymod.api.world.position.Location; + return new cmLocation(world, json.x, json.y, json.z, json.pitch?json.pitch:0, json.yaw?json.yaw:0); + } else { + world = _world( json.world ); + return new bkLocation( world, json.x, json.y , json.z, json.yaw?json.yaw:0, json.pitch?json.pitch:0 ); + } + } +}; + +exports.player = _player; + +exports.getPlayerObject = function getPlayerObject( player ) { + console.warn( 'utils.getPlayerObject() is deprecated. Use utils.player() instead.' ); + return _player(player); +}; +/************************************************************************* +### utils.getPlayerPos() function + +This function returns the player's [Location][cmloc] (x, y, z, pitch +and yaw) for a named player. If the "player" is in fact a +[BlockCommand][bkbcs] then the attached Block's location is returned. + +#### Parameters + + * player : A [org.bukkit.command.CommandSender][bkbcs] (Player or BlockCommandSender) or player name (String). + +#### Returns + +A [Location][cmloc] object. + +[bkbcs]: http://jd.bukkit.org/dev/apidocs/org/bukkit/command/BlockCommandSender.html +[bksndr]: http://jd.bukkit.org/dev/apidocs/index.html?org/bukkit/command/CommandSender.html +***/ +function getPlayerPos( player ){ + player = _player( player ); + if ( player ) { + if (__plugin.bukkit){ + if ( player instanceof bkBlockCommandSender ) + return player.block.location; + else + return player.location; + } + if (__plugin.canary){ + if ( player instanceof Packages.net.canarymod.api.world.blocks.CommandBlock) + return player.block.location; + else + return player.location; + } + } + return null; +} +exports.getPlayerPos = getPlayerPos; +/************************************************************************ +### utils.getMousePos() function + +This function returns a [Location][cmloc] object (the +x,y,z) of the current block being targeted by the named player. This +is the location of the block the player is looking at (targeting). + +#### Parameters + + * player : The player whose targeted location you wish to get. + +#### Example + +The following code will strike lightning at the location the player is looking at... + +```javascript +var utils = require('utils'); +var playerName = 'walterh'; +var targetPos = utils.getMousePos(playerName); +if (targetPos){ + if (__plugin.canary){ + targetPos.world.makeLightningBolt(targetPos); + } + if (__plugin.bukkit){ + targetPos.world.strikeLightning(targetPos); + } +} +``` + +***/ +exports.getMousePos = function getMousePos( player ) { + + player = _player(player); + if ( !player ) { + return null; + } + var targetedBlock ; + if ( __plugin.canary ) { + var cmLineTracer = Packages.net.canarymod.LineTracer; + var lineTracer = new cmLineTracer(player); + targetedBlock = lineTracer.getTargetBlock(); + if (targetedBlock == null){ + return null; + } + } else { + // player might be CONSOLE or a CommandBlock + if ( !player.getTargetBlock ) { + return null; + } + try { + targetedBlock = player.getTargetBlock( null, 5 ); + }catch (e){ + // spigot 1.8.7 adds new overload which causes problems with JDK 7 + targetedBlock = player['getTargetBlock(java.util.Set,int)'](null, 5 ); + } + if ( targetedBlock == null || targetedBlock.isEmpty() ) { + return null; + } + } + return targetedBlock.location; +}; +/************************************************************************ +### utils.foreach() function + +The utils.foreach() function is a utility function for iterating over +an array of objects (or a java.util.Collection of objects) and +processing each object in turn. Where utils.foreach() differs from +other similar functions found in javascript libraries, is that +utils.foreach can process the array immediately or can process it +*nicely* by processing one item at a time then delaying processing of +the next item for a given number of server ticks (there are 20 ticks +per second on the minecraft main thread). This method relies on +Bukkit's [org.bukkit.scheduler][sched] package for scheduling +processing of arrays. + +[sched]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/package-summary.html + +#### Parameters + + * array : The array to be processed - It can be a javascript array, a java array or java.util.Collection + * callback : The function to be called to process each item in the + array. The callback function should have the following signature + `callback(item, index, object, array)`. That is the callback will + be called with the following parameters.... + + - item : The item in the array + - index : The index at which the item can be found in the array. + - object : Additional (optional) information passed into the foreach method. + - array : The entire array. + + * context (optional) : An object which may be used by the callback. + * delayInMilliseconds (optional, numeric) : If a delay is specified then the processing will be scheduled so that + each item will be processed in turn with a delay between the completion of each + item and the start of the next. This is recommended for any CPU-intensive process. + * onDone (optional, function) : A function to be executed when all processing + is complete. This parameter is only used when the processing is delayed. (It's optional even if a + delay parameter is supplied). + +If called with a delay parameter then foreach() will return +immediately after processing just the first item in the array (all +subsequent items are processed later). If your code relies on the +completion of the array processing, then provide an `onDone` parameter +and put the code there. + +#### Example + +The following example illustrates how to use foreach for immediate processing of an array... + +```javascript +var utils = require('utils'); +var players = utils.players(); +utils.foreach (players, function( player ) { + echo( player , 'Hi ' + player); +}); +``` + +... The `utils.foreach()` function can work with Arrays or any +Java-style collection. This is important because many objects in the +CanaryMod and Bukkit APIs use Java-style collections. +***/ +function _foreach( array, callback, context, delay, onCompletion ) { + if ( array instanceof java.util.Collection ) { + array = array.toArray(); + } + var i = 0; + var len = array.length; + function next() { + callback(array[i], i, context, array); + i++; + } + function hasNext() { + return i < len; + } + if ( delay ) { + _nicely( next, hasNext, onCompletion, delay ); + } else { + for ( ;i < len; i++ ) { + callback( array[i], i, context, array ); + } + } +}; +exports.foreach = _foreach; +/************************************************************************ +### utils.nicely() function + +The utils.nicely() function is for performing background processing. utils.nicely() lets you +process with a specified delay between the completion of each `next()` +function and the start of the next `next()` function. +`utils.nicely()` is a recursive function - that is - it calls itself +(schedules itself actually) repeatedly until `hasNext` returns false. + +#### Parameters + + * next : A function which will be called if processing is to be done. + * hasNext : A function which is called to determine if the `next` + callback should be invoked. This should return a boolean value - + true if the `next` function should be called (processing is not + complete), false otherwise. + * onDone : A function which is to be called when all processing is complete (hasNext returned false). + * delayInMilliseconds : The delay between each call. + +#### Example + +See the source code to utils.foreach for an example of how utils.nicely is used. + +***/ +function _nicely( next, hasNext, onDone, delay ) { + if ( hasNext() ){ + next(); + setTimeout( function() { + _nicely( next, hasNext, onDone, delay ); + }, delay ); + }else{ + if ( onDone ) { + onDone(); + } + } +}; +exports.nicely = _nicely; + +function _at( time24hr, callback, pWorlds, repeat ) { + console.warn("utils.at() is deprecated, use require('at') instead"); + var at = require('at'); + return at( time24hr, callback, pWorlds, repeat); +} +exports.at = _at; +/************************************************************************* +### utils.time( world ) function + +Returns the timeofday (in minecraft ticks) for the given world. This function is necessary because +canarymod and bukkit differ in how the timeofday is calculated. + +See http://minecraft.gamepedia.com/Day-night_cycle#Conversions + +***/ +function getTime(world){ + world = _world(world); + + if (__plugin.bukkit){ + return world.time; + } + if (__plugin.canary){ + // there's a bug in canary where if you call world.setTime() the world.totalTime + // becomes huge. + if (world.totalTime < world.rawTime){ + return world.totalTime; + } else { + return ((world.totalTime % world.rawTime) + world.relativeTime) % 24000; + } + } + return 0; +} +exports.time = getTime; + +/************************************************************************* +### utils.time24( world ) function + +Returns the timeofday for the given world using 24 hour notation. (number of minutes) + +See http://minecraft.gamepedia.com/Day-night_cycle#Conversions + +#### Parameters + + * world : the name of the world or world object for which you want to get time + +***/ +function getTime24( world ){ + world = _world(world); // accept world name or object or undeifned + var mcTime = getTime(world); + var mins = Math.floor( ( (mcTime + 6000) % 24000) / 16.6667 ); + return mins; +} +exports.time24 = getTime24; + +/************************************************************************ +### utils.find() function + +The utils.find() function will return a list of all files starting at +a given directory and recursiving trawling all sub-directories. + +#### Parameters + + * dir : The starting path. Must be a string. + * filter : (optional) A [FilenameFilter][fnfltr] object to return only files matching a given pattern. + +[fnfltr]: http://docs.oracle.com/javase/6/docs/api/java/io/FilenameFilter.html + +#### Example + +```javascript +var utils = require('utils'); +var jsFiles = utils.find('./', function(dir,name){ + return name.match(/\.js$/); +}); +``` +***/ +exports.find = function( path, filter){ + console.warn("utils.find() is deprecated, use require('find') instead"); + return require('find')(path, filter); +}; +/************************************************************************ +### utils.serverAddress() function + +The utils.serverAddress() function returns the IP(v4) address of the server. + +```javascript +var utils = require('utils'); +var serverAddress = utils.serverAddress(); +console.log(serverAddress); +``` +***/ +exports.serverAddress = function serverAddress() { + var interfaces = java.net.NetworkInterface.getNetworkInterfaces(); + var current, + addresses, + current_addr; + while ( interfaces.hasMoreElements() ) { + current = interfaces.nextElement(); + if ( ! current.isUp() || current.isLoopback() || current.isVirtual() ) { + continue; + } + addresses = current.getInetAddresses(); + while (addresses.hasMoreElements()) { + current_addr = addresses.nextElement(); + if ( current_addr.isLoopbackAddress() ) + continue; + if ( current_addr instanceof java.net.Inet4Address) + return current_addr.getHostAddress(); + } + } + return null; +}; +/************************************************************************** +### utils.array() function + +Converts Java collection objects to type Javascript array so they can avail of +all of Javascript's Array goodness. + +#### Example + + var utils = require('utils'); + var worlds = utils.array(server.worldManager.getAllWorlds()); + +***/ +function toArray( ){ + var result = [], + javaArray = null, + i = 0; + if (arguments[0] instanceof java.util.Collection){ + // it's a java collection + javaArray = arguments[0].toArray(); + for ( ;i < javaArray.length; i++) { + result.push(javaArray[i]); + } + } else if (arguments[0].constructor === Array){ + // it's a javascript array + return arguments[0]; + } else if (arguments[0].length) { + // it's a java array + javaArray = arguments[0]; + for ( ;i < javaArray.length; i++) { + result.push(javaArray[i]); + } + } + return result; +} +exports.array = toArray; + +function worlds(){ + if (__plugin.canary){ + return toArray(server.worldManager.allWorlds); + } + if (__plugin.bukkit){ + return toArray(server.worlds); + } +} +exports.worlds = worlds; + +/************************************************************************* +### utils.players() function + +This function returns a javascript array of all online players on the +server. You can optionally provide a function which will be invoked +with each player as a parameter. For example, to give each player the +ability to shoot arrows which launch fireworks: + +```javascript +require('utils').players( arrows.firework ) +``` + +Any players with a bow will be able to launch fireworks by shooting. + +### utils.playerNames() function + +This function returns a javascript array of player names (as javascript strings) + +***/ +function getPlayersBukkit(){ + var result = []; + var players = server.getOnlinePlayers(); + for (var i = 0; i < players.size(); i++){ + result.push(players.get(i)); + } + return result; +} +function getPlayersCanary(){ + var result = []; + var players = server.playerList; + for (var i = 0; i < players.size(); i++){ + result.push(players.get(i)); + } + return result; +} +var getPlayers = null; +if (__plugin.canary) { + getPlayers = getPlayersCanary; +} else { + getPlayers = getPlayersBukkit; +} + +function getStatBukkit(){ + if (arguments.length == 1){ + var stat = arguments[1]; + return org.bukkit.Statistic[stat.toUpperCase()]; + } else { + var player = arguments[0]; + var stat = arguments[1]; + return player.getStatistic(org.bukkit.Statistic[stat.toUpperCase()]); + } + +} +function getStatCanary(){ + var cmStatistics = Packages.net.canarymod.api.statistics.Statistics; + if (arguments.length == 1){ + var stat = arguments[0]; + return cmStatistics[stat.toUpperCase()].instance; + } else { + var player = arguments[0]; + var stat = arguments[1]; + return player.getStat(cmStatistics[stat.toUpperCase()].instance); + } +} +if (__plugin.canary){ + var cmStatistics = Packages.net.canarymod.api.statistics.Statistics; + var values = cmStatistics.values(); + for (var i = 0;i < values.length; i++){ + var value = values[i]; + try { + var stat = value.instance; + getStatCanary[value.name()] = stat; + }catch (e){ + // as of 20141018 some calls to getInstance() will generate an NPE + // see https://github.com/CanaryModTeam/CanaryMod/issues/84 + } + } +} + +function getPlayerNames(){ + return getPlayers().map(function(p){ return p.name; }); +} +exports.players = function players(fn){ + var result = getPlayers(); + if (fn){ + result.forEach(fn); + } + return result; +}; +exports.playerNames = getPlayerNames; + +/************************************************************************* +### utils.stat() function + +This function returns a numeric value for a given player statistic. + +#### Parameters + + * Player - The player object (optional - if only the statistic name parameter is provided then the statistic object is returned) + * Statistic - A string whose value should be one of the following (CanaryMod) + * ANIMALSBRED + * BOATONECM + * CLIMBONECM + * CROUCHONECM + * DAMAGEDEALT + * DAMAGETAKEN + * DEATHS + * DRIVEONECM + * DROP + * FALLONECM + * FISHCAUGHT + * FLYONECM + * HORSEONECM + * JUMP + * JUNKFISHED + * LEAVEGAME + * MINECARTONECM + * MOBKILLS + * PIGONECM + * PLAYERKILLS + * PLAYONEMINUTE + * SPRINTONECM + * SWIMONECM + * TALKEDTOVILLAGER + * TIMESINCEDEATH + * TRADEDWITHVILLAGER + * TREASUREFISHED + * WALKONECM + +See [CanaryMod's Statistic][cmstat] class for an up-to-date list of possible stat values + +[cmstat]: https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/statistics/Statistics.html + +#### Example 1 Getting stats for a player + + var utils = require('utils'); + var jumpCount = utils.stat( player, 'jump'); + +#### Example 2 Getting the JUMP statistic object (which can be used elsewhere) + + var utils = require('utils'); + var JUMPSTAT = utils.stat('jump'); + var jumpCount = player.getStat( JUMPSTAT ); // canary-specific code + +This function also contains values for each possible stat so you can get at stats like this... + + var utils = require('utils'); + var JUMPSTAT = utils.stat.JUMP; // Accessing the value + var jumpCount = player.getStat ( JUMPSTAT ); // canary-specific code +***/ +exports.stat = __plugin.canary ? getStatCanary: getStatBukkit; + diff --git a/src/main/js/modules/watcher.js b/src/main/js/modules/watcher.js new file mode 100644 index 0000000..7970d6c --- /dev/null +++ b/src/main/js/modules/watcher.js @@ -0,0 +1,189 @@ +'use strict'; +/*global setTimeout, exports, require*/ +var File = java.io.File; +/************************************************************************ +## The watcher Module + +This module exposes functions for watching for changes to files or directories. + +### watcher.watchFile() function + +Watches for changes to the given file or directory and calls the function provided +when the file changes. + +#### Parameters + + * File - the file to watch (can be a file or directory) + * Callback - The callback to invoke when the file has changed. The callback takes the + changed file as a parameter. + +#### Example + +```javascript +var watcher = require('watcher'); +watcher.watchFile( 'test.txt', function( file ) { + console.log( file + ' has changed'); +}); +``` +***/ +var filesWatched = {}; +var dirsWatched = {}; + +exports.watchFile = function( file, callback ) { + if ( typeof file == 'string' ) { + file = new File(file); + } + filesWatched[file.canonicalPath] = { + callback: callback, + lastModified: file.lastModified() + }; +}; + +/************************************************************************ +### watcher.watchDir() function + +Watches for changes to the given directory and calls the function provided +when the directory changes. It works by calling watchFile/watchDir for each +file/subdirectory. + +#### Parameters + + * Dir - the file to watch (can be a file or directory) + * Callback - The callback to invoke when the directory has changed. + The callback takes the changed file as a parameter. + For each change inside the directory the callback will also + be called. + +#### Example + +```javascript +var watcher = require('watcher'); +watcher.watchDir( 'players/_ial', function( dir ) { + console.log( dir + ' has changed'); +}); +``` +***/ + +exports.watchDir = function( dir, callback ) { + if ( typeof dir == 'string' ) { + dir = new File(dir); + } + dirsWatched[dir.canonicalPath] = { + callback: callback, + lastModified: dir.lastModified() + }; + + var files = dir.listFiles(),file; + if ( !files ) { + return; + } + for ( var i = 0; i < files.length; i++ ) { + file = files[i]; + if (file.isDirectory( )) { + exports.watchDir(file,callback); + }else{ + exports.watchFile(file,callback); + } + } +}; +/************************************************************************ +### watcher.unwatchFile() function + +Removes a file from the watch list. + +#### Example +```javascript +var watcher = require('watcher'); +watcher.unwatchFile('test.txt'); +``` + +***/ +exports.unwatchFile = function( file, callback ) { + if ( typeof file == 'string' ) { + file = new File(file); + } + delete filesWatched[file.canonicalPath]; +}; + +/************************************************************************ +### watcher.unwatchDir() function + +Removes a directory from the watch list and all files inside the directory +are also "unwatched" + +#### Example +```javascript +var watcher = require('watcher'); +watcher.unwatchDir ('players/_ial'); +``` +Would cause also +```javascript +watcher.unwatchFile (file); +``` +for each file inside directory (and unwatchDir for each directory inside it) + +***/ +exports.unwatchDir = function( dir, callback ) { + if ( typeof dir == 'string' ) { + dir = new File(dir); + } + delete dirsWatched[dir.canonicalPath]; + + var files = dir.listFiles(),file; + if ( !files ) { + return; + } + for ( var i = 0; i < files.length; i++ ) { + file = files[i]; + if (file.isDirectory( )) { + exports.unwatchDir(file,callback); + }else{ + exports.unwatchFile(file,callback); + } + } +}; + +function fileWatcher(calledCallbacks) { + for (var file in filesWatched) { + var fileObject = new File(file); + var lm = fileObject.lastModified(); + if ( lm != filesWatched[file].lastModified ) { + filesWatched[file].lastModified = lm; + filesWatched[file].callback(fileObject); + if (!fileObject.exists()) { + exports.unwatchFile(file,filesWatched[file].callback); + } + } + } +}; + + +//monitors directories for time change +//when a change is detected watchFiles are invoked for each of the files in directory +//and callback is called +function dirWatcher(calledCallbacks) { + for (var dir in dirsWatched) { + var dirObject = new File(dir); + var lm = dirObject.lastModified(); + var dw = dirsWatched[dir]; + if ( lm != dirsWatched[dir].lastModified ) { + dirsWatched[dir].lastModified = lm; + dirsWatched[dir].callback(dirObject); + + exports.unwatchDir(dir, dw.callback); + //causes all files to be rewatched + if (dirObject.exists()) { + exports.watchDir(dir, dw.callback); + } + } + } +}; + +//guarantees that a callback is only called once for each change +function monitorDirAndFiles() { + fileWatcher (); + dirWatcher (); + setTimeout( monitorDirAndFiles, 3000 ); +}; + +setTimeout( monitorDirAndFiles, 3000 ); diff --git a/src/main/js/plugins/alias/alias.js b/src/main/js/plugins/alias/alias.js new file mode 100644 index 0000000..9b0998c --- /dev/null +++ b/src/main/js/plugins/alias/alias.js @@ -0,0 +1,253 @@ +'use strict'; +/************************************************************************* +## alias Plugin + +The alias module lets players and server admins create their own +per-player or global custom in-game command aliases. + +### Examples + +To set a command alias which is only visible to the current player +(per-player alias)... + + /jsp alias set cw = time set {1} ; weather {2} + +... Creates a new custom command only usable by the player who set +it called `cw` (short for set Clock and Weather) which when invoked... + + /cw 4000 sun + +... will perform the following commands... + + /time set 4000 + /weather sun + +Aliases can use paramters as above. On the right hand side of the `=`, the +`{1}` refers to the first parameter provided with the `cw` alias, `{2}` +refers to the second parameter and so on. So `cw 4000 sun` is converted to +`time set 4000` and `weather sun`. + +To set a global command alias usable by all (only operators can create +such an alias)... + + /jsp alias global stormy = time 18000; weather storm + +To remove an alias ... + + /jsp alias remove cw + +... removes the 'cw' alias from the appropriate alias map. + +To get a list of aliases currently defined... + + /jsp alias list + +To get help on the `jsp alias` command: + + /jsp alias help + +Aliases can be used at the in-game prompt by players or in the server +console. Aliases will not be able to avail of command autocompletion +(pressing the TAB key will have no effect). + +***/ + +var _usage = '\ +/jsp alias set {alias} = {comand-1} ;{command-2}\n \ +/jsp alias global {alias} = {command-1} ; {command-2}\n \ +/jsp alias list\n \ +/jsp alias remove {alias}\n \ +Create a new alias : \n \ +/jsp alias set cw = time set {1} ; weather {2}\n \ +Execute the alias : \n \ +/cw 4000 sun \n \ +...is the same as \'/time set 4000\' and \'/weather sun\''; +/* + persist aliases +*/ +var _store = { + players: {}, + global: {} +}; +/* + turns 'cw = time set {1} ; weather {2}' into {cmd: 'cw', aliases: ['time set {1}', 'weather {2}']} + _processParams is a private function which takes an array of parameters + used for the 'set' and 'global' options. +*/ +var _processParams = function( params ) { + var paramStr = params.join(' '), + eqPos = paramStr.indexOf('='), + aliasCmd = paramStr.substring( 0, eqPos).trim(), + aliasValue = paramStr.substring( eqPos + 1 ).trim(); + return { + cmd: aliasCmd, + aliases: aliasValue.split( /\s*;\s*/ ) + }; +}; + +var _set = function( params, player ) { + var playerAliases = _store.players[player.name]; + if (!playerAliases ) { + playerAliases = {}; + } + var o = _processParams( params ); + playerAliases[o.cmd] = o.aliases; + _store.players[player.name] = playerAliases; + echo( player, 'Alias ' + o.cmd + ' created.' ); +}; + +var _remove = function( params, player ) { + if ( _store.players[player.name] && _store.players[player.name][ params[0] ] ) { + delete _store.players[player.name][params[0]]; + echo( player, 'Alias ' + params[0] + ' removed.' ); + } + else{ + echo( player, 'Alias ' + params[0] + ' does not exist.' ); + } + if ( isOp(player) ) { + if ( _store.global[params[0]] ) { + delete _store.global[params[0]]; + } + } +}; + +var _global = function( params, player ) { + if ( !isOp(player) ) { + echo( player, 'Only operators can set global aliases. ' + + 'You need to be an operator to perform this command.' ); + return; + } + var o = _processParams( params ); + _store.global[o.cmd] = o.aliases; + echo( player, 'Global alias ' + o.cmd + ' created.' ); +}; + +var _list = function( params, player ) { + var alias = 0; + try { + if ( _store.players[player.name] ) { + echo( player, 'Your aliases:'); + for ( alias in _store.players[player.name] ) { + echo( player, alias + ' = ' + + JSON.stringify( _store.players[player.name][alias] ) ); + } + } else { + echo( player, 'You have no player-specific aliases.' ); + } + echo( player, 'Global aliases:' ); + for ( alias in _store.global ) { + echo( player, alias + ' = ' + JSON.stringify( _store.global[alias] ) ); + } + } catch( e ) { + console.error( 'Error in list function: ' + e.message ); + throw e; + } +}; +var _help = function( params, player ) { + echo( player, 'Usage:\n' + _usage ); +}; + +var alias = plugin( 'alias', { + store: _store, + set: _set, + global: _global, + remove: _remove, + list: _list, + help: _help +}, true ); + +var aliasCmd = command( 'alias', function( params, invoker ) { + var operation = params[0], + fn; + if ( !operation ) { + echo( invoker, 'Usage:\n' + _usage ); + return; + } + /* + wph 20140122 this is kind of dumb but Nashorn has some serious problems + accessing object properties by array index notation + in JRE8 alias[operation] returns null - definitely a bug in Nashorn. + */ + for ( var key in alias ) { + if ( key == operation ) { + fn = alias[key]; + fn( params.slice(1), invoker ); + return; + } + } + echo( invoker, 'Usage:\n' + _usage ); +}); + +var _intercept = function( msg, invoker, exec ) { + if ( msg.trim().length == 0 ) + return false; + var msgParts = msg.split(' '), + command = msg.match( /^\/*([^\s]+)/ )[1], + template = [], + isAlias = false, + cmds = [], + commandObj, + filledinCommand, + i; + + if ( _store.global[command] ) { + template = _store.global[command]; + isAlias = true; + } + /* + allows player-specific aliases to override global aliases + */ + if ( _store.players[invoker] && _store.players[invoker][command] ) { + template = _store.players[invoker][command]; + isAlias = true; + } + for ( i = 0; i < template.length; i++) { + filledinCommand = template[i].replace( /{([0-9]+)}/g, function( match, index ) { + index = parseInt( index, 10 ); + if ( msgParts[index] ) { + return msgParts[index]; + } else { + return match; + } + }); + cmds.push( filledinCommand ); + } + + for (i = 0; i< cmds.length; i++ ) { + exec( cmds[i] ); + } + return isAlias; + +}; +/* + Intercept all command processing and replace with aliased commands if the + command about to be issued matches an alias. +*/ +if (__plugin.canary){ + console.warn('alias plugin is not yet supported in CanaryMod'); + return; +} +events.playerCommandPreprocess( function( evt ) { + var invoker = evt.player; + var exec = function( cmd ) { + invoker.performCommand(cmd); + }; + var isAlias = _intercept( (''+evt.message).trim(), ''+invoker.name, exec); + if ( isAlias ) { + evt.cancelled = true; + } +}); +/* define a 'void' command because ServerCommandEvent can't be canceled */ +command('void',function( ) { +} ); + +events.serverCommand( function( evt ) { + var invoker = evt.sender; + var exec = function( cmd ) { + invoker.server.dispatchCommand( invoker, cmd); + }; + var isAlias = _intercept( (''+evt.command).trim(), ''+ invoker.name, exec ); + if ( isAlias ) { + evt.command = 'jsp void'; + } +}); diff --git a/src/main/js/plugins/arrows.js b/src/main/js/plugins/arrows.js new file mode 100644 index 0000000..212b153 --- /dev/null +++ b/src/main/js/plugins/arrows.js @@ -0,0 +1,171 @@ +'use strict'; +/*global require, __plugin, exports, events, setTimeout */ +/************************************************************************* +## Arrows Plugin + +The arrows mod adds fancy arrows to the game. Arrows which ... + + * Launch fireworks. + * Explode on impact. + * Force Lightning to strike where they land. + * Teleport the player to the landing spot. + * Spawn Trees at the landing spot. + +### Usage: + + * `/js arrows.firework(self)` - A firework launches where the the arrow lands. + * `/js arrows.lightning(self)` - lightning strikes where the arrow lands. + * `/js arrows.teleport(self)` - makes player teleport to where arrow has landed. + * `/js arrows.flourish(self)` - makes a tree grow where the arrow lands. + * `/js arrows.explosive(self)` - makes arrows explode. + * `/js arrows.normal(self)` sets arrow type to normal. + * `/js arrows.sign(self)` turns a targeted sign into an Arrows menu + +All of the above functions can take an optional player object or name +as a parameter. For example: `/js arrows.explosive('player23')` makes +player23's arrows explosive. + +***/ +var Drone = require('drone'), + teleport = require('teleport'), + signs = require('signs'), + fireworks = require('fireworks'), + utils = require('utils'), + bkArrow = org.bukkit.entity.Arrow, + bkPlayer = org.bukkit.entity.Player, + EXPLOSIVE_YIELD = 2.5, + store = persist('arrows',{ players: { } }), + arrows = {}, + i, + type, + _types = [ 'Normal', 'Explosive', 'Teleport', 'Flourish', 'Lightning', 'Firework' ]; + +exports.arrows = arrows; + + +for ( i = 0; i < _types.length; i++ ) { + type = _types[i].toLowerCase(); + // iife (immediately-invoked function expression) + arrows[ type ] = ( function( n ) { + return function( player ) { + player = utils.player( player ); + if ( player ) { + store.players[ player.name ] = n; + } else { + console.warn('arrows.' + n + ' No player ' + player); + } + }; + } )( i ); +} + +/* + called when the player chooses an arrow option from a menu sign + */ +var _onMenuChoice = function( event ) { + store.players[ event.player.name ] = event.number; +}; + +var convertToArrowSign = signs.menu( 'Arrow', _types, _onMenuChoice ); + +/* + turn a sign into a menu of arrow choices + */ +arrows.sign = function( cmdSender ) { + var sign = signs.getTargetedBy( cmdSender ); + if ( !sign ) { + throw new Error( 'You must first look at a sign!' ); + } + return convertToArrowSign( sign, true ); +}; + +/* + event handler called when a projectile hits something + */ +function onBukkitArrowHit( event ) { + var projectile = event.entity, + world = projectile.world, + shooter = projectile.shooter, + fireworkCount = 5, + arrowType; + + function launch(){ + fireworks.firework( projectile.location ); + if ( --fireworkCount ) { + setTimeout( launch, 2000 ); + } + } + if (projectile instanceof bkArrow + && shooter instanceof bkPlayer) { + + arrowType = store.players[ shooter.name ]; + + switch ( arrowType ) { + case 1: + projectile.remove(); + world.createExplosion( projectile.location, EXPLOSIVE_YIELD ); + break; + case 2: + projectile.remove(); + teleport(shooter, projectile.location); + break; + case 3: + projectile.remove(); + new Drone(projectile.location).oak(); + break; + case 4: + projectile.remove(); + world.strikeLightning( projectile.location ); + break; + case 5: + projectile.remove(); + launch(); + break; + } + } +} + +function onCanaryArrowHit( event ) { + var projectile = event.projectile, + world = projectile.world, + shooter = projectile.owner, + fireworkCount = 5, + arrowType, + cmArrow = Packages.net.canarymod.api.entity.Arrow, + cmPlayer = Packages.net.canarymod.api.entity.living.humanoid.Player, + loc = projectile.location, + launch = function( ) { + fireworks.firework( loc); + if ( --fireworkCount ) { + setTimeout( launch, 2000 ); + } + }; + + if (projectile instanceof cmArrow && shooter instanceof cmPlayer) { + + arrowType = store.players[ shooter.name ]; + + switch ( arrowType ) { + case 1: + projectile.destroy(); + world.makeExplosion( shooter, loc, EXPLOSIVE_YIELD, true ); + break; + case 2: + projectile.destroy(); + teleport(shooter, loc); + break; + case 3: + projectile.destroy(); + new Drone( loc ).oak(); + break; + case 4: + projectile.destroy(); + world.makeLightningBolt( loc ); + break; + case 5: + projectile.destroy(); + launch(); + break; + } + } +} +events.projectileHit( __plugin.bukkit ? onBukkitArrowHit : onCanaryArrowHit); diff --git a/src/main/js/plugins/at.js b/src/main/js/plugins/at.js new file mode 100644 index 0000000..f194221 --- /dev/null +++ b/src/main/js/plugins/at.js @@ -0,0 +1,4 @@ +// ensure that the at world-load event handlers are +// registered early in server startup +var at = require('at'); +// nothing more needed. diff --git a/src/main/js/plugins/classroom.js b/src/main/js/plugins/classroom.js new file mode 100644 index 0000000..47f7d45 --- /dev/null +++ b/src/main/js/plugins/classroom.js @@ -0,0 +1,11 @@ +/*global require, exports, command*/ +var cr = require('classroom'); + +command(function classroom( params, sender){ + if (params[0] == 'on'){ + cr.allowScripting(true, sender); + }else { + cr.allowScripting(false, sender); + } +},['on','off']); +exports.classroom = cr; diff --git a/src/main/js/plugins/commando/commando-test.js b/src/main/js/plugins/commando/commando-test.js new file mode 100644 index 0000000..cd9b987 --- /dev/null +++ b/src/main/js/plugins/commando/commando-test.js @@ -0,0 +1,26 @@ +/* + A test of the commando plugin. + Adds a new `/js-time` command with 4 possible options: Dawn, Midday, Dusk, Midnight +*/ +if (__plugin.canary){ + console.warn('commando-test not yet supported in CanaryMod'); + return; +} +var commando = require('./commando').commando, + times = ['Dawn','Midday','Dusk','Midnight']; + +commando( 'js-time' , function( params, sender ) { + var time = ''+params[0].toLowerCase(), + i = 0; + if ( sender.location ) { + for ( ; i < 4; i++ ) { + if ( times[i].toLowerCase() == time ) { + sender.location.world.setTime( i * 6000 ); + break; + } + } + } else { + echo( sender, 'This command only works in-world'); + } + +},times); diff --git a/src/main/js/plugins/commando/commando.js b/src/main/js/plugins/commando/commando.js new file mode 100644 index 0000000..331c99c --- /dev/null +++ b/src/main/js/plugins/commando/commando.js @@ -0,0 +1,122 @@ +/************************************************************************* +## Commando Plugin + +### Description + +commando is a plugin which can be used to add completely new commands +to Minecraft. Normally ScriptCraft only allows for provision of new +commands as extensions to the jsp command. For example, to create a +new simple command for use by all players... + + /js command('hi', function(args,player){ echo( player, 'Hi ' + player.name); }); + +... then players can use this command by typing... + + /jsp hi + +... A couple of ScriptCraft users have asked for the ability to take +this a step further and allow the global command namespace to be +populated so that when a developer creates a new command using the +'command' function, then the command is added to the global command +namespace so that players can use it simply like this... + + /hi + +... There are good reasons why ScriptCraft's core `command()` function +does not do this. Polluting the global namespace with commands would +make ScriptCraft a bad citizen in that Plugins should be able to work +together in the same server and - as much as possible - not step on +each others' toes. The CraftBukkit team have very good reasons for +forcing Plugins to declare their commands in the plugin.yml +configuration file. It makes approving plugins easier and ensures that +craftbukkit plugins behave well together. While it is possible to +override other plugins' commands, the CraftBukkit team do not +recommend this. However, as ScriptCraft users have suggested, it +should be at the discretion of server administrators as to when +overriding or adding new commands to the global namespace is good. + +So this is where `commando()` comes in. It uses the exact same +signature as the core `command()` function but will also make the +command accessible without the `jsp` prefix so instead of having to +type `/jsp hi` for the above command example, players simply type +`/hi` . This functionality is provided as a plugin rather than as part +of the ScriptCraft core. + +### Example hi-command.js + + var commando = require('../commando'); + commando('hi', function(args,player){ + echo( player, 'Hi ' + player.name); + }); + +...Displays a greeting to any player who issues the `/hi` command. + +### Example - timeofday-command.js + + var times = {Dawn: 0, Midday: 6000, Dusk: 12000, Midnight:18000}; + commando('timeofday', function(params,player){ + player.location.world.setTime(times[params[0]]); + }, + ['Dawn','Midday','Dusk','Midnight']); + +... changes the time of day using a new `/timeofday` command (options are Dawn, Midday, Dusk, Midnight) + +### Caveats + +Since commands registered using commando are really just appendages to +the `/jsp` command and are not actually registered globally (it just +looks like that to the player), you won't be able to avail of tab +completion for the command itself or its parameters (unless you go the +traditional route of adding the `jsp` prefix). This plugin uses the +[PlayerCommandPreprocessEvent][pcppevt] which allows plugins to +intercepts all commands and inject their own commands instead. If +anyone reading this knows of a better way to programmatically add new +global commands for a plugin, please let me know. + +[pcppevt]: http://jd.bukkit.org/dev/apidocs/org/bukkit/event/player/PlayerCommandPreprocessEvent.html + +***/ +if (__plugin.canary){ + console.warn('commando plugin is not yet supported in CanaryMod'); + return; +} +var commands = {}; + +exports.commando = function( name, func, options, intercepts ) { + var result = command( name, func, options, intercepts ); + commands[name] = result; + return result; +}; + +events.playerCommandPreprocess( function( evt ) { + var msg = '' + evt.message; + var parts = msg.match( /^\/([^\s]+)/ ); + if ( !parts ) { + return; + } + if ( parts.length < 2 ) { + return; + } + var command = parts[1]; + if ( commands[command] ) { + evt.message = '/jsp ' + msg.replace( /^\//, '' ); + } +} ); +events.serverCommand( function( evt ) { + var msg = '' + evt.command; + var parts = msg.match( /^\/*([^\s]+)/ ); + if ( !parts ) { + return; + } + if ( parts.length < 2 ) { + return; + } + var command = parts[1]; + if ( commands[ command ] ) { + var newCmd = 'jsp ' + msg.replace( /^\//, '' ); + if ( config.verbose ) { + console.log( 'Redirecting to : %s', newCmd ); + } + evt.command = newCmd; + } +}); diff --git a/src/main/js/plugins/drone/contrib/castle.js b/src/main/js/plugins/drone/contrib/castle.js new file mode 100644 index 0000000..3bb3f18 --- /dev/null +++ b/src/main/js/plugins/drone/contrib/castle.js @@ -0,0 +1,80 @@ +'use strict'; +/*global require */ +var Drone = require('drone'), + blocks = require('blocks'); +/************************************************************************ +### Drone.castle() method + +Creates a Castle. A castle is just a big wide fort with 4 taller forts at each corner. +See also Drone.fort() method. + +#### Parameters + + * side - How many blocks wide and long the castle will be (default: 24. Must be greater than 19) + * height - How tall the castle will be (default: 10. Must be geater than 7) + +#### Example + +At the in-game prompt you can create a castle by looking at a block and typing: + +```javascript +/js castle() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the castle() method. + +```javascript +var d = new Drone(player); +d.castle(); +``` +![castle example](img/castleex1.png) + +***/ +function castle( side, height ) { + // + // use sensible default parameter values + // if no parameters are supplied + // + if ( typeof side == 'undefined' ) + side = 24; + if ( typeof height == 'undefined' ) + height = 10; + if ( height < 8 || side < 20 ) + throw new java.lang.RuntimeException('Castles must be at least 20 wide X 8 tall'); + // + // how big the towers at each corner will be... + // + var towerSide = 10; + var towerHeight = height+4; + + // + // the main castle building will be front and right of the first tower + // + this + .chkpt('castle') + .fwd( towerSide / 2 ) + .right( towerSide / 2 ) + .fort( side, height ) + .move( 'castle' ); + // + // now place 4 towers at each corner (each tower is another fort) + // + for ( var corner = 0; corner < 4; corner++ ) { + // construct a 'tower' fort + this + .fort( towerSide, towerHeight ) + .chkpt('tower-' + corner) + .fwd( towerSide - 1 ) + .right( towerSide - 3 ) + .up( towerHeight - 5 ) // create 2 doorways from main castle rampart into each tower + .box( blocks.air, 1, 2, 1 ) + .back( 2 ) + .right( 2 ) + .box( blocks.air, 1, 2, 1 ) + .move( 'tower-' + corner) + .fwd( side + towerSide - 1) // move forward the length of the castle then turn right + .turn(); + } + this.move('castle'); +} +Drone.extend(castle); diff --git a/src/main/js/plugins/drone/contrib/chessboard.js b/src/main/js/plugins/drone/contrib/chessboard.js new file mode 100644 index 0000000..9088ef0 --- /dev/null +++ b/src/main/js/plugins/drone/contrib/chessboard.js @@ -0,0 +1,60 @@ +'use strict'; +/*global require */ +var Drone = require('drone'), + blocks = require('blocks'); +/************************************************************************ +### Drone.chessboard() method + +Creates a tile pattern of given block types and size + +#### Parameters + + * whiteBlock - (optional: default blocks.wool.white) + * blackBlock - (optional: default blocks.wool.black) + * width - width of the chessboard + * length - length of the chessboard + +#### Example + +At the in-game prompt you can create a chessboard by looking at a block and typing: + +```javascript +/js chessboard() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the chessboard() method. + +```javascript +var d = new Drone(player); +d.chessboard(); +``` +![chessboard example](img/chessboardex1.png) + +***/ +Drone.extend('chessboard', function( whiteBlock, blackBlock, width, depth ) { + var i, + j, + block; + + + if ( typeof whiteBlock == 'undefined' ) { + whiteBlock = blocks.wool.white; + } + if ( typeof blackBlock == 'undefined' ) { + blackBlock = blocks.wool.black; + } + if ( typeof width == 'undefined' ) { + width = 8; + } + if ( typeof depth == 'undefined' ) { + depth = width; + } + var squares = [ blackBlock, whiteBlock ]; + + this.chkpt('chessboard-start'); + for ( i = 0; i < depth; i++ ) { + this.boxa( squares, width, 1, 1).fwd(); + squares = squares.reverse(); + } + this.move('chessboard-start'); +}); diff --git a/src/main/js/plugins/drone/contrib/cottage.js b/src/main/js/plugins/drone/contrib/cottage.js new file mode 100644 index 0000000..b3a6405 --- /dev/null +++ b/src/main/js/plugins/drone/contrib/cottage.js @@ -0,0 +1,160 @@ +'use strict'; +/*global require */ +var Drone = require('drone'), + blocks = require('blocks'); +/************************************************************************ +### Drone.cottage() method + +Creates a simple but cosy dwelling. + +#### Example + +At the in-game prompt you can create a cottage by looking at a block and typing: + +```javascript +/js cottage() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the cottage() method. + +```javascript +var d = new Drone(player); +d.cottage(); +``` +![cottage example](img/cottageex1.png) + +***/ +function cottage( ) { + this + .chkpt('cottage') + .down() + .box(blocks.birch, 7, 1, 6) // birch wood floor + .up() + .box(blocks.air, 7, 5, 6) // clear area first + .box0( blocks.moss_stone, 7, 2, 6) // 4 walls + .right(3) + .door() // door front and center + .up(1) + .left(2) + .box( blocks.glass_pane ) // windows to left and right + .right(4) + .box( blocks.glass_pane ) + .left(5) + .up() + .prism0( blocks.stairs.oak, 7, 6) // add a roof + .down() + .right(4) + .back() + .wallsign(['Home','Sweet','Home']) + .fwd() + .move('cottage') + .right(3) + .fwd(4) + .up() + .hangtorch() // place a torch on wall + .move('cottage') + .right() + .fwd(3) + .bed() // place a bed against left wall + .fwd() + .right(4) + .box(blocks.furnace) // place a furnace against right wall + .move('cottage') + ; +} +/************************************************************************ +### Drone.cottage_road() method + +Creates a tree-lined avenue with cottages on both sides. + +#### Parameters + + * numberOfCottages: The number of cottages to build in total (optional: default 6) + +#### Example + +At the in-game prompt you can create a cottage road by looking at a block and typing: + +```javascript +/js cottage_road() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the cottage_road() method. + +```javascript +var d = new Drone(player); +d.cottage_road(); +``` +![cottage_road example](img/cottageroadex1.png) + +***/ + +// +// a more complex script that builds an tree-lined avenue with +// cottages on both sides. +// +function cottage_road( numberCottages ) { + if (typeof numberCottages == 'undefined'){ + numberCottages = 6; + } + var i=0, distanceBetweenTrees = 11; + // + // step 1 build the road. + // + var cottagesPerSide = Math.floor(numberCottages/2); + this + // make sure the drone's state is saved. + .chkpt('cottage_road') + // build the road + .box( blocks.double_slab.stone, 3, 1, cottagesPerSide * ( distanceBetweenTrees + 1 ) ) + .up() + // now centered in middle of road + .right() + // will be returning to this position later + .chkpt('cottage_road_cr'); + + // + // step 2 line the road with trees + // + for ( ; i < cottagesPerSide+1;i++ ) { + this + .left(5) + .oak() + .right(10) + .oak() + .left(5) // return to middle of road + .fwd( distanceBetweenTrees + 1 ); // move forward. + } + this + .move('cottage_road_cr') + .back(6); // move back 1/2 the distance between trees + + // this function builds a path leading to a cottage. + function pathAndCottage( drone ) { + drone + .down() + .box(blocks.double_slab.stone, 1, 1, 5) + .fwd(5) + .left(3) + .up() + .cottage(); + return drone; + }; + // + // step 3 build cottages on each side + // + for ( i = 0; i < cottagesPerSide; i++ ) { + this + .fwd( distanceBetweenTrees + 1 ) + .chkpt('r'+i); + // build cottage on left + pathAndCottage( this.turn(3) ).move( 'r' + i ); + // build cottage on right + pathAndCottage( this.turn() ).move( 'r' + i ); + } + // return drone to where it was at start of function + this.move('cottage_road'); +} +Drone.extend(cottage_road); +Drone.extend(cottage); + diff --git a/src/main/js/plugins/drone/contrib/dancefloor.js b/src/main/js/plugins/drone/contrib/dancefloor.js new file mode 100644 index 0000000..10091d8 --- /dev/null +++ b/src/main/js/plugins/drone/contrib/dancefloor.js @@ -0,0 +1,74 @@ +'use strict'; +/*global require, clearInterval, setInterval*/ +var Drone = require('drone'), + blocks = require('blocks'), + SECOND = 1000; +/************************************************************************ +### Drone.dancefloor() method +Create an animated dance floor of colored tiles some of which emit light. +The tiles change color every second creating a strobe-lit dance-floor effect. +See it in action here [http://www.youtube.com/watch?v=UEooBt6NTFo][ytdance] + +#### Parameters + + * width - how wide the dancefloor should be (optional: default 5) + * length - how long the dancefloor should be (optional: default 5) + * duration - the time duration for which the lights should change (optional: default 30 seconds) + +#### Example + +At the in-game prompt you can create a dancefloor by looking at a block and typing: + +```javascript +/js dancefloor() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the dancefloor() method. + +```javascript +var d = new Drone(player); +d.dancefloor(); +``` + +[ytdance]: http://www.youtube.com/watch?v=UEooBt6NTFo +![dancefloor example](img/dancefloorex1.png) +***/ + + +// +function dancefloor(width, length, duration) +{ + if (typeof width == 'undefined') + width = 5; + if (typeof length == 'undefined') + length = width; + if (typeof duration === 'undefined'){ + duration = 30; + } + // + // create a separate Drone object to lay down disco tiles + // + var disco = new Drone(this.x, this.y, this.z, this.dir, this.world); + // + // under-floor lighting + // + disco + .down() + .box( blocks.glowstone, width, 1, length) + .up(); + + // + // strobe gets called in a java thread - disco only lasts 30 seconds. + // + var task = null; + var strobe = function() { + disco.rand(blocks.rainbow, width, 1, length); + duration--; + if ( duration == 0 ){ + // turn off the lights + clearInterval(task); + } + }; + task = setInterval( strobe, 1 * SECOND); +} +Drone.extend( dancefloor ); diff --git a/src/main/js/plugins/drone/contrib/fort.js b/src/main/js/plugins/drone/contrib/fort.js new file mode 100644 index 0000000..0e41346 --- /dev/null +++ b/src/main/js/plugins/drone/contrib/fort.js @@ -0,0 +1,126 @@ +'use strict'; +/*global require */ +var Drone = require('drone'), + blocks = require('blocks'); +/************************************************************************ +### Drone.fort() method + +Constructs a medieval fort. + +#### Parameters + + * side - How many blocks whide and long the fort will be (default: 18 . Must be greater than 9) + * height - How tall the fort will be (default: 6 . Must be greater than 3) + +#### Example + +At the in-game prompt you can create a fort by looking at a block and typing: + +```javascript +/js fort() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the fort() method. + +```javascript +var d = new Drone(player); +d.fort(); +``` +![fort example](img/fortex1.png) + +***/ +function fort( side, height ) { + var turret, + i, + torch; + + if ( typeof side == 'undefined' ) { + side = 18; + } + if ( typeof height == 'undefined' ) { + height = 6; + } + // make sure side is even + if ( side % 2 ) { + side++; + } + var battlementWidth = 3; + if ( side <= 12 ) { + battlementWidth = 2; + } + if ( height < 4 || side < 10 ) { + throw new java.lang.RuntimeException('Forts must be at least 10 wide X 4 tall'); + } + // + // build walls. + // + this + .chkpt('fort') + .down() + .chessboard( blocks.wool.black, blocks.wool.white, side) + .up() + .box0( blocks.brick.stone, side, height - 1, side) + .up(height-1); + // + // build battlements + // + for ( i = 0; i <= 3; i++ ) { + + turret = [ + blocks.stairs.stone , + blocks.stairs.stone + ':'+ Drone.PLAYER_STAIRS_FACING[ (this.dir + 2) % 4 ] + ]; + this + .box( blocks.brick.stone ) // solid brick corners + .up() + .box(blocks.torch) + .down() // light a torch on each corner + .fwd() + .boxa( turret, 1, 1, side-2) + .fwd( side-2 ) + .turn(); + } + // + // build battlement's floor + // + this + .move('fort') + .up(height-2) + .fwd() + .right(); + + for ( i = 0; i < battlementWidth; i++ ) { + var bside = side - ( 2 + (i * 2) ); + this + .box0( blocks.slab.oak, bside, 1, bside) + .fwd() + .right(); + } + // + // add door + // + torch = blocks.torch + ':' + Drone.PLAYER_TORCH_FACING[this.dir]; + this + .move('fort') + .right( ( side / 2 ) - 1 ) + .door2() // double doors + .back() + .left() + .up() + .box( torch ) // left torch + .right( 3 ) + .box( torch ); // right torch + // + // add ladder up to battlements + // + this + .move('fort') + .right( ( side / 2 ) - 3 ) + .fwd() // move inside fort + .turn( 2 ) + .box( blocks.air, 1, height - 1, 1) + .ladder( height - 1 ) + .move( 'fort' ); +} +Drone.extend(fort); + diff --git a/src/main/js/plugins/drone/contrib/hangtorch.js b/src/main/js/plugins/drone/contrib/hangtorch.js new file mode 100644 index 0000000..9aacc66 --- /dev/null +++ b/src/main/js/plugins/drone/contrib/hangtorch.js @@ -0,0 +1,69 @@ +'use strict'; +/*global require, __plugin, org*/ +var Drone = require('drone'), + blocks = require('blocks'); +/************************************************************************ +### Drone.hangtorch() method + +Adds a hanging torch to a wall. This method will try to hang a torch +against a wall. It will traverse backwards until it finds a block +adjacent to air and hang the torch. If it can't find a block next to +air it will log a message in the server. + +#### Example + +At the in-game prompt you can create a hanging torch by looking at a +block and typing: + +```javascript +/js hangtorch() +``` + +Alternatively you can create a new Drone object from a Player or +Location object and call the hangtorch() method. + +```javascript +var d = new Drone(player); +d.hangtorch(); +``` + +***/ +function canHang( block ) { + + if (__plugin.bukkit){ + var bkMaterial = org.bukkit.Material; + if ( block.type.equals(bkMaterial.AIR) || + block.type.equals(bkMaterial.VINE) ) { + return true; + } + } + if (__plugin.canary){ + if (block.typeId == blocks.air || + block.typeId == blocks.vines ) { + return true; + } + } + return false; +} +function hangtorch() { + var torch = blocks.torch + ':' + Drone.PLAYER_TORCH_FACING[this.dir]; + var moves = 0; + var block = this.getBlock(); + + while ( !canHang(block) ){ + + moves++; + this.back(); + if (moves == 10){ + this + .fwd(moves); + console.log('nowhere to hang torch'); + return; + } + block = this.getBlock(); + } + this + .box(torch) + .fwd(moves); +} +Drone.extend(hangtorch); diff --git a/src/main/js/plugins/drone/contrib/lcd-clock.js b/src/main/js/plugins/drone/contrib/lcd-clock.js new file mode 100644 index 0000000..300b844 --- /dev/null +++ b/src/main/js/plugins/drone/contrib/lcd-clock.js @@ -0,0 +1,105 @@ +'use strict'; +/*global require, setInterval, clearInterval, __plugin, exports*/ +/************************************************************************* +### Drone.lcdclock() method. + +Constructs a large LCD Clock. The clock will display the current time of day. +The clock can be stopped by calling the stopLCD() method of the Drone which created the clock. + +#### Parameters + + * foregroundBlock (Optional - default is blocks.glowstone) + * backgroundBlock (Optional - default is blocks.wool.black) + * borderBlock (Optional - a border around the LCD display - default none) + +#### Example + +At the in-game prompt you can create a LCD clock by looking at a block and typing: + +```javascript +/js var clock = lcdclock() +/js clock.stopLCD() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the lcdclock() method. + +```javascript +var d = new Drone(player); +d.lcdclock(); +d.stopLCD(); +``` +![lcdclock example](img/lcdclockex1.png) +***/ +var blocks = require('blocks'), + utils = require('utils'), + Drone = require('drone'); + +function lcdclock(fgColor, bgColor, border){ + var drone = this; + var lastSecs = [0,0,0,0], + world = drone.world, + intervalId = -1; + + function update(secs) { + var digits = [0,0,0,0], + s = secs % 60, + m = (secs - s) / 60; + digits[3] = s%10; + digits[2] = (s-digits[3])/10; + digits[1] = m%10; + digits[0] = (m-digits[1])/10; + // + // updating all 4 digits each time is expensive + // only update digits which have changed (in most cases - just 1) + // + if (digits[3] != lastSecs[3]){ + drone + .right(14) + .blocktype(''+digits[3],fgColor,bgColor, true) + .left(14); + } + if (digits[2] != lastSecs[2]){ + drone + .right(10) + .blocktype(''+digits[2],fgColor,bgColor, true) + .left(10); + } + if (digits[1] != lastSecs[1]){ + drone + .right(4) + .blocktype(''+digits[1], fgColor, bgColor, true) + .left(4); + } + if (digits[0] != lastSecs[0]){ + drone + .blocktype(''+digits[0], fgColor, bgColor, true); + } + lastSecs[0] = digits[0]; + lastSecs[1] = digits[1]; + lastSecs[2] = digits[2]; + lastSecs[3] = digits[3]; + + } + if ( typeof bgColor == 'undefined' ) { + bgColor = blocks.wool.black; + } + if ( typeof fgColor == 'undefined' ) { + fgColor = blocks.glowstone ; + } + if ( border ) { + drone.box(border,21,9,1); + drone.up().right(); + } + drone.blocktype('00:00', fgColor, bgColor, true); + + function tick() { + var timeOfDayInMins = utils.time24(world); + update( timeOfDayInMins ); + } + intervalId = setInterval(tick, 800); + this.stopLCD = function(){ + clearInterval(intervalId); + }; +} + +Drone.extend(lcdclock); diff --git a/src/main/javascript/drone/contrib/logo.js b/src/main/js/plugins/drone/contrib/logo.js similarity index 68% rename from src/main/javascript/drone/contrib/logo.js rename to src/main/js/plugins/drone/contrib/logo.js index 880a00e..c633472 100644 --- a/src/main/javascript/drone/contrib/logo.js +++ b/src/main/js/plugins/drone/contrib/logo.js @@ -1,67 +1,90 @@ -// -// Constructs the JS logo -// https://raw.github.com/voodootikigod/logo.js/master/js.png -// -// fg -// the material that the letters JS will be made of -// bg -// the material that the square will be made of -// -Drone.extend('logojs', function(fg, bg) { +'use strict'; +/*global require*/ +var Drone = require('drone'), + blocks = require('blocks'); +/************************************************************************ +### Drone.logojs() method + +Constructs a large Javascript Logo (black JS on Yellow background) +See: https://raw.github.com/voodootikigod/logo.js/master/js.png + +#### Parameters + + * foregroundBlock (Optional - default is blocks.wool.gray) + * backgroundBlock (Optional - default is blocks.gold) + +***/ +function logojs(fg, bg) { // foreground defaults to gray wool if (typeof fg == "undefined") - fg = '35:7'; + fg = blocks.wool.gray; // background defaults to gold blocks if (typeof bg == "undefined") - bg = 41; + bg = blocks.gold; // Draw the sqaure - this.chkpt('logojs-start') - .up() - .box(bg, 100, 100, 1); + this + .chkpt('logojs-start') + .up() + .box(bg, 100, 100, 1); // Draw the J, starting with the hook - this.right(30).up(13) - .box(fg) - .right().down() - .box(fg, 1, 3, 1) - .right().down() - .box(fg, 1, 5, 1) - .right().down() - .box(fg, 1, 7, 1) - .right() - .box(fg, 1, 8, 1) - .right().down() - .box(fg, 1, 10, 1) - .right() - .box(fg, 1, 9, 1) - .right() - .box(fg, 1, 8, 1) - .right().down() - .box(fg, 2, 8, 1) - .right(2) - .box(fg, 4, 7, 1) - .right(4) - .box(fg, 1, 8, 1) - .right() - .box(fg, 1, 9, 1) - .right().up() - .box(fg, 3, 10, 1) - .right(3).up() - .box(fg, 2, 9, 1) - .right(2).up() - .box(fg, 2, 8, 1) - .right(2).up() - .box(fg, 1, 7, 1) - .right().up() - .box(fg, 1, 6, 1) - .right().up() - .box(fg, 1, 5, 1) - .right().up(2) - .box(fg, 1, 3, 1) - .left(9).up(3) - .box(fg, 10, 31, 1) + this + .right(30) + .up(13) + .box(fg) + .right() + .down() + .box(fg, 1, 3, 1) + .right() + .down() + .box(fg, 1, 5, 1) + .right() + .down() + .box(fg, 1, 7, 1) + .right() + .box(fg, 1, 8, 1) + .right() + .down() + .box(fg, 1, 10, 1) + .right() + .box(fg, 1, 9, 1) + .right() + .box(fg, 1, 8, 1) + .right() + .down() + .box(fg, 2, 8, 1) + .right(2) + .box(fg, 4, 7, 1) + .right(4) + .box(fg, 1, 8, 1) + .right() + .box(fg, 1, 9, 1) + .right() + .up() + .box(fg, 3, 10, 1) + .right(3) + .up() + .box(fg, 2, 9, 1) + .right(2) + .up() + .box(fg, 2, 8, 1) + .right(2) + .up() + .box(fg, 1, 7, 1) + .right() + .up() + .box(fg, 1, 6, 1) + .right() + .up() + .box(fg, 1, 5, 1) + .right() + .up(2) + .box(fg, 1, 3, 1) + .left(9) + .up(3) + .box(fg, 10, 31, 1) // Draw the S // It's drawn in three strokes from bottom to top. Look for when @@ -179,7 +202,7 @@ Drone.extend('logojs', function(fg, bg) { this.move('logojs-start'); return this; -}); +} // // Makes a cube of JS logos! // This is a wrapper for logojs() so look at its docs @@ -187,7 +210,7 @@ Drone.extend('logojs', function(fg, bg) { // Until the drone can rotate on its Z axis we can't // use logojs() to create top/bottom sides of cube. // -Drone.extend('logojscube', function(fg, bg) { +function logojscube(fg, bg) { this.chkpt('jscube-start') .logojs(fg, bg); @@ -214,4 +237,6 @@ Drone.extend('logojscube', function(fg, bg) { .logojs(fg, bg); return this; -}); +} +Drone.extend( logojs ); +Drone.extend( logojscube ); diff --git a/src/main/js/plugins/drone/contrib/mazegen.js b/src/main/js/plugins/drone/contrib/mazegen.js new file mode 100644 index 0000000..1ff4a1f --- /dev/null +++ b/src/main/js/plugins/drone/contrib/mazegen.js @@ -0,0 +1,146 @@ +'use strict'; +/*global require*/ +/************************************************************************ +### Drone.maze() method + +Maze generation based on http://rosettacode.org/wiki/Maze_generation#JavaScript + +#### Parameters + + * width (optional - default 10) + * length (optional - default 10) + +#### Example + +At the in-game prompt you can create a maze by looking at a block and typing: + +```javascript +/js maze() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the maze() method. + +```javascript +var d = new Drone(player); +d.maze(); +``` +![maze example](img/mazeex1.png) + +***/ +var Drone = require('drone'), + blocks = require('blocks'); + +// User-facing code starts here +// Example: Try /js maze(5,7) +Drone.extend( function maze( width, length ) { + if (typeof width === 'undefined'){ + width = 10; + } + if (typeof length === 'undefined'){ + length = 10; + } + var m = maze_make(width, length); + if (m.x > 0 && m.y > 0) { + maze_draw(maze_display(m), this); + } +} ); +// +// Implementation +// +function maze_make(x,y) { + var n=x*y-1; + if (n<0) { + console.log ("illegal maze dimensions"); + return ({x: 0, y: 0}); + } + var horiz=[]; + var j; + for ( j = 0; j0 && j0 && (j != here[0]+1 || k != here[1]+1)); + } + while (00 && m.verti[j/2-1][Math.floor(k/4)]) + line[k]= ' '; + else + line[k]= '-'; + else + for (var k=0; k0 && m.horiz[(j-1)/2][k/4-1]) + line[k]= ' '; + else + line[k]= '|'; + else + line[k]= ' '; + if (0 == j) line[1]= line[2]= line[3]= ' '; + if (m.x*2-1 == j) line[4*m.y]= ' '; + text.push(line.join('')+' \r\n'); // TWEAKED: space added to get an even number of columns + } + return text.join(''); +} + +// ScriptCraft stuff starts here +// Helper function to parse the ASCII art into Drone actions +// You might also consider creating a new maze_display but for now this will do the work +function maze_draw(maze_string, d) { + // d is the Drone to use + d.chkpt('maze-start'); + for (var j = 0; j < maze_string.length; j += 2) { + switch(maze_string.substr(j, 2)) { + case ' ': + d.box(0).fwd(); // Make sure to empty this position + break; + case '\r\n': + d.move('maze-start'); + d.right().chkpt('maze-start'); + break; + default: // '+ ', '+-', '--', '| ' + if (j == 0) { + d.box(blocks.glowstone,1,2,1); // highlight the maze entry and exit + } else if (j == maze_string.length - 4) { + d.box(blocks.glass,1,2,1); + } else { + d.box(blocks.oak,1,2,1); + } + d.fwd(); + } + } +} diff --git a/src/main/js/plugins/drone/contrib/rainbow.js b/src/main/js/plugins/drone/contrib/rainbow.js new file mode 100644 index 0000000..25a6445 --- /dev/null +++ b/src/main/js/plugins/drone/contrib/rainbow.js @@ -0,0 +1,59 @@ +'use strict'; +/*global require*/ +var Drone = require('drone'), + blocks = require('blocks'); + +/************************************************************************ +### Drone.rainbow() method + +Creates a Rainbow. + +#### Parameters + + * radius (optional - default:18) - The radius of the rainbow + +#### Example + +At the in-game prompt you can create a rainbow by looking at a block and typing: +```javascript +/js rainbow() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the rainbow() method. + +```javascript +var d = new Drone(player); +d.rainbow(30); +``` + +![rainbow example](img/rainbowex1.png) + +***/ +function rainbow( radius ) { + var i, + colors, + bm; + + if ( typeof radius == "undefined" ) { + radius = 18; + } + + this.chkpt('rainbow'); + this.down(radius); + // copy blocks.rainbow and add air at end (to compensate for strokewidth) + colors = blocks.rainbow.slice(0); + colors.push(blocks.air); + for ( i = 0; i < colors.length; i++ ) { + bm = this.getBlockIdAndMeta( colors[i] ); + this.arc({ + blockType: bm[0], + meta: bm[1], + radius: radius-i, + strokeWidth: 2, + quadrants: {topright: true, + topleft: true}, + orientation: 'vertical'}).right().up(); + } + return this.move('rainbow'); +} +Drone.extend(rainbow); diff --git a/src/main/javascript/drone/contrib/redstonewire.js b/src/main/js/plugins/drone/contrib/redstonewire.js similarity index 96% rename from src/main/javascript/drone/contrib/redstonewire.js rename to src/main/js/plugins/drone/contrib/redstonewire.js index fc36992..fa74a62 100644 --- a/src/main/javascript/drone/contrib/redstonewire.js +++ b/src/main/js/plugins/drone/contrib/redstonewire.js @@ -1,3 +1,8 @@ +'use strict'; +/*global require*/ +var Drone = require('drone'), + blocks = require('blocks'); + // // usage: // [1] to place a new block with redstone wire on it (block on bottom, redstone on top) diff --git a/src/main/js/plugins/drone/contrib/spawn.js b/src/main/js/plugins/drone/contrib/spawn.js new file mode 100644 index 0000000..24f86c0 --- /dev/null +++ b/src/main/js/plugins/drone/contrib/spawn.js @@ -0,0 +1,8 @@ +'use strict'; +var spawnFn = require('spawn'), + Drone = require('drone') + ; +function spawn(entityType){ + spawnFn(entityType, this.getBlock().location); +} +Drone.extend(spawn); diff --git a/src/main/js/plugins/drone/contrib/spiral_stairs.js b/src/main/js/plugins/drone/contrib/spiral_stairs.js new file mode 100644 index 0000000..4f9b356 --- /dev/null +++ b/src/main/js/plugins/drone/contrib/spiral_stairs.js @@ -0,0 +1,52 @@ +'use strict'; +/*global require*/ +var Drone = require('drone'), + blocks = require('blocks'); + +/************************************************************************ +### Drone.spiral_stairs() method + +Constructs a spiral staircase with slabs at each corner. + +#### Parameters + + * stairBlock - The block to use for stairs, should be one of the following... + - 'oak' + - 'spruce' + - 'birch' + - 'jungle' + - 'cobblestone' + - 'brick' + - 'stone' + - 'nether' + - 'sandstone' + - 'quartz' + * flights - The number of flights of stairs to build. + +![Spiral Staircase](img/spiralstair1.png) + +#### Example + +To construct a spiral staircase 5 floors high made of oak... + + spiral_stairs('oak', 5); + +***/ +function spiral_stairs(stairBlock, flights){ + this.chkpt('spiral_stairs'); + + for (var i = 0; i < flights; i++){ + this + .box(blocks.stairs[stairBlock] ) + .up() + .fwd() + .box(blocks.stairs[stairBlock] ) + .up() + .fwd() + .box(blocks.slab[stairBlock]) + .turn() + .fwd(); + } + this.move('spiral_stairs'); +} +Drone.extend(spiral_stairs); diff --git a/src/main/js/plugins/drone/contrib/temple.js b/src/main/js/plugins/drone/contrib/temple.js new file mode 100644 index 0000000..d841b0f --- /dev/null +++ b/src/main/js/plugins/drone/contrib/temple.js @@ -0,0 +1,55 @@ +'use strict'; +/*global require*/ +var Drone = require('drone'), + blocks = require('blocks'); +/************************************************************************ +### Drone.temple() method + +Constructs a mayan temple. + +#### Parameters + + * side - How many blocks wide and long the temple will be (default: 20) + +#### Example + +At the in-game prompt you can create a temple by looking at a block and typing: + +```javascript +/js temple() +``` + +Alternatively you can create a new Drone object from a Player or Location object and call the temple() method. + +```javascript +var d = new Drone(player); +d.temple(); +``` +![temple example](img/templeex1.png) + +***/ +function temple( side ) { + if ( !side ) { + side = 20; + } + this.chkpt('temple'); + + while ( side > 4 ) { + var middle = Math.round( (side-2) / 2 ); + this + .chkpt('temple-corner') + .box( blocks.brick.mossy, side, 1, side ) + .right( middle ) + .box( blocks.stairs.stone ) + .right() + .box( blocks.stairs.stone ) + .move('temple-corner') + .up() + .fwd() + .right(); + side = side - 2; + } + + this.move('temple'); +} +Drone.extend( temple ); diff --git a/src/main/js/plugins/drone/drone.js b/src/main/js/plugins/drone/drone.js new file mode 100644 index 0000000..12c032d --- /dev/null +++ b/src/main/js/plugins/drone/drone.js @@ -0,0 +1,4 @@ +/*global require, exports*/ +var blocks = require('blocks'); +exports.Drone = require('drone'); +exports.blocks = blocks; diff --git a/src/main/js/plugins/entities.js b/src/main/js/plugins/entities.js new file mode 100644 index 0000000..48446df --- /dev/null +++ b/src/main/js/plugins/entities.js @@ -0,0 +1,8 @@ +'use strict'; +/*global require, exports*/ +/* + make entities a global variable for use at in-game prompt + Tab completion is a useful way to discover what entity types are available. +*/ +var entities = require('entities'); +exports.entities = entities; diff --git a/src/main/js/plugins/examples/example-1-hello-module.js b/src/main/js/plugins/examples/example-1-hello-module.js new file mode 100644 index 0000000..3306704 --- /dev/null +++ b/src/main/js/plugins/examples/example-1-hello-module.js @@ -0,0 +1,30 @@ +/************************************************************************* +## Example Plugin #1 - A simple extension to Minecraft. + +A simple minecraft plugin. The most basic module. + +### Usage: + +At the in-game prompt type ... + + /js hello(self) + +... and a message `Hello {player-name}` will appear (where + {player-name} is replaced by your own name). + +This example demonstrates the basics of adding new functionality which +is only usable by server operators or users with the +scriptcraft.evaluate permission. By default, only ops are granted this +permission. + +The `hello` function below is only usable by players with the scriptcraft.evaluate +permission since it relies on the `/js` command to execute. + + exports.hello = function(player){ + echo( player, 'Hello ' + player.name); + }; + +***/ +exports.hello = function( player ) { + echo( player, 'Hello ' + player.name ); +}; diff --git a/src/main/js/plugins/examples/example-2-hello-command.js b/src/main/js/plugins/examples/example-2-hello-command.js new file mode 100644 index 0000000..1ef6b20 --- /dev/null +++ b/src/main/js/plugins/examples/example-2-hello-command.js @@ -0,0 +1,32 @@ +/************************************************************************* +## Example Plugin #2 - Making extensions available for all players. + +A simple minecraft plugin. Commands for other players. + +### Usage: + +At the in-game prompt type ... + + /jsp hello + +... and a message `Hello {player-name}` will appear (where {player-name} is +replaced by your own name). + +This example demonstrates the basics of adding new functionality +which is usable all players or those with the scriptcraft.proxy +permission. By default, all players are granted this permission. + +This differs from example 1 in that a new 'jsp ' command extension +is defined. Since all players can use the `jsp` command, all players +can use the new extension. Unlike the previous example, the `jsp hello` +command does not evaluate javascript code so this command is much more secure. + + command('hello', function (parameters, player) { + echo( player, 'Hello ' + player.name); + }); + +***/ + +command( 'hello', function( parameters, player ) { + echo( player, 'Hello ' + player.name ); +}); diff --git a/src/main/js/plugins/examples/example-3-hello-ops-only.js b/src/main/js/plugins/examples/example-3-hello-ops-only.js new file mode 100644 index 0000000..5c7893a --- /dev/null +++ b/src/main/js/plugins/examples/example-3-hello-ops-only.js @@ -0,0 +1,42 @@ +/************************************************************************* +## Example Plugin #3 - Limiting use of commands to operators only. + +A simple minecraft plugin. Commands for operators only. + +### Usage: + +At the in-game prompt type ... + + /jsp op-hello + +... and a message `Hello {player-name}` will appear (where {player-name} is +replaced by your own name). + +This example demonstrates the basics of adding new functionality +which is usable all players or those with the scriptcraft.proxy +permission. By default, all players are granted this permission. In +this command though, the function checks to see if the player is an +operator and if they aren't will return immediately. + +This differs from example 2 in that the function will only print a +message for operators. + + command('op-hello', function (parameters, player) { + if ( !isOp(player) ){ + echo( player, 'Only operators can do this.'); + return; + } + echo( player, 'Hello ' + player.name); + }); +***/ + +command( 'op-hello', function( parameters, player ) { + /* + this is how you limit based on player privileges + */ + if ( !isOp(player) ) { + echo( player, 'Only operators can do this.' ); + return; + } + echo( player, 'Hello ' + player.name ); +}); diff --git a/src/main/js/plugins/examples/example-4-hello-parameters.js b/src/main/js/plugins/examples/example-4-hello-parameters.js new file mode 100644 index 0000000..ba01adc --- /dev/null +++ b/src/main/js/plugins/examples/example-4-hello-parameters.js @@ -0,0 +1,40 @@ +/************************************************************************* +## Example Plugin #4 - Using parameters in commands. + +A simple minecraft plugin. Handling parameters. + +### Usage: + +At the in-game prompt type ... + + /jsp hello-params Hi + /jsp hello-params Saludos + /jsp hello-params Greetings + +... and a message `Hi {player-name}` or `Saludos {player-name}` etc +will appear (where {player-name} is replaced by your own name). + +This example demonstrates adding and using parameters in commands. + +This differs from example 3 in that the greeting can be changed from +a fixed 'Hello ' to anything you like by passing a parameter. + + command( 'hello-params', function ( parameters, player ) { + var salutation = parameters[0] ; + echo( player, salutation + ' ' + player.name ); + }); + +***/ + +command('hello-params', function( parameters, player ) { + /* + parameters is an array (or list) of strings. parameters[0] + refers to the first element in the list. Arrays in Javascript + are 0-based. That is, the 1st element is parameters[0], the 2nd + element is parameters[1], the 3rd element is parameters[2] and + so on. In this example, parameters[1] refers to the first word + which appears after `jsp hello-params `. + */ + var salutation = parameters[0] ; + echo( player, salutation + ' ' + player.name ); +}); diff --git a/src/main/js/plugins/examples/example-5-hello-using-module.js b/src/main/js/plugins/examples/example-5-hello-using-module.js new file mode 100644 index 0000000..aba9b62 --- /dev/null +++ b/src/main/js/plugins/examples/example-5-hello-using-module.js @@ -0,0 +1,41 @@ +/************************************************************************* +## Example Plugin #5 - Re-use - Using your own and others modules. + +A simple minecraft plugin. Using Modules. + +### Usage: + +At the in-game prompt type ... + + /jsp hello-module + +... and a message `Hello {player-name}` will appear (where {player-name} is +replaced by your own name). + +This example demonstrates the use of modules. In +example-1-hello-module.js we created a new javascript module. In +this example, we use that module... + + * We load the module using the `require()` function. Because this + module and the module we require are n the same directory, we + specify `'./example-1-hello-module'` as the path (when loading a + module from the same directory, `./` at the start of the path + indicates that the file should be searched for in the same + directory. + + * We assign the loaded module to a variable (`greetings`) and then + use the module's `hello` method to display the message. + +Source Code... + + var greetings = require('./example-1-hello-module'); + command( 'hello-module', function( parameters, player ) { + greetings.hello( player ); + }); + +***/ +var greetings = require('./example-1-hello-module'); + +command( 'hello-module', function( parameters, player ) { + greetings.hello( player ); +}); diff --git a/src/main/js/plugins/examples/example-6-hello-player.js b/src/main/js/plugins/examples/example-6-hello-player.js new file mode 100644 index 0000000..fd92854 --- /dev/null +++ b/src/main/js/plugins/examples/example-6-hello-player.js @@ -0,0 +1,58 @@ +/************************************************************************* +## Example Plugin #6 - Re-use - Using 'utils' to get Player objects. + +A simple minecraft plugin. Finding players by name. + +### Usage: + +At the in-game prompt type ... + + /jsp hello-byname {player-name} + +... substituting {player-name} with the name of a player currently +online and a message `Hello ...` will be sent to the named +player. + +This example builds on example-5 and also introduces a new concept - +use of shared modules. That is : modules which are not specific to +any one plugin or set of plugins but which can be used by all +plugins. Shared modules should be placed in the +`scriptcraft/modules` directory. + + * The utils module is used. Because the 'utils' module is + located in the modules folder we don't need to specify an exact + path, just 'utils' will do. + + * The `utils.player()` function is used to obtain a player object + matching the player name. Once a player object is obtained, a + message is sent to that player. + +Source Code ... + + var utils = require('utils'); + var greetings = require('./example-1-hello-module'); + + command( 'hello-byname', function( parameters, sender ) { + var playerName = parameters[0]; + var recipient = utils.player( playerName ); + if ( recipient ) { + greetings.hello( recipient ); + } else { + echo( sender, 'Player ' + playerName + ' not found.' ); + } + }); + +***/ + +var utils = require('utils'); +var greetings = require('./example-1-hello-module'); + +command( 'hello-byname', function( parameters, sender ) { + var playerName = parameters[0]; + var recipient = utils.player( playerName ); + if ( recipient ) { + greetings.hello( recipient ); + } else { + echo( sender, 'Player ' + playerName + ' not found.' ); + } +}); diff --git a/src/main/js/plugins/examples/example-7-hello-events.js b/src/main/js/plugins/examples/example-7-hello-events.js new file mode 100644 index 0000000..72ba171 --- /dev/null +++ b/src/main/js/plugins/examples/example-7-hello-events.js @@ -0,0 +1,73 @@ +'use strict'; +/*global events, echo, isOp, __plugin*/ +/************************************************************************* +## Example Plugin #7 - Listening for events, Greet players when they join the game. + +A simple event-driven minecraft plugin. How to handle Events. + +This example demonstrates event-driven programming. The code below +will display the version of ScriptCraft every time an operator joins +the game. This module is notable from previous modules for the +following reasons... + + 1. It does not export any functions or variables. That's fine. Not + all modules need export stuff. Code in this module will be + executed when the module is first loaded. Because it is in the + `/scriptcraft/plugins` directory, it will be loaded automatically + when the server starts up. + + 2. It uses ScriptCraft's `events` module to add a new *Event + Handler*. An *Event Handler* is a function that gets + called whenever a particular *event* happens in the game. The + function defined below will only be executed whenever a player + joins the game. This style of program is sometimes refered to as + *Event-Driven Programming*. + +Adding new *Event Handlers* in ScriptCraft is relatively easy. Use one +of the `events` module's functions to add a new event handler. The +events module has many functions - one for each type of event. Each +function takes a single parameter: + + * The event handling function (also sometimes refered to as a + 'callback'). In ScriptCraft, this function takes a single + parameter, an event object. All of the information about the event + is in the event object. + +In the example below, if a player joins the server and is an operator, +then the ScriptCraft plugin information will be displayed to that +player. + +```javascript +function onJoin( event ){ + if ( isOp(event.player) ) { + echo( event.player, 'Welcome to ' + __plugin ); + } +} +events.connection( onJoin ); +``` +First the onJoin() function is defined, this is our event handler - +the function we wish to be called every time some new player joins the +game. Then we hook up - or register - that function using the +events.connection() function. The events.connection function is the +function responsible for adding new *connection* event handlers - that +is - functions which should be invoked when there's a new *connection* +event in the game. A new *connection* event is fired whenever a player +joins the game. There are many other types of events you can handle in +Minecraft. You can see [a full list of events here][cmEvtList]. + +[cmEvtList]: #events-helper-module-canary-version +***/ + +// wph 20140927 - event handler registration differs depending on framework. +function onJoin( event ) { + if ( isOp(event.player) ) { + echo( event.player, 'Welcome to ' + __plugin ); + } +} +if (__plugin.canary){ + // canarymod + events.connection( onJoin ); +} else { + // bukkit + events.playerJoin( onJoin ); +} diff --git a/src/main/js/plugins/homes/homes.js b/src/main/js/plugins/homes/homes.js new file mode 100644 index 0000000..f6af3ec --- /dev/null +++ b/src/main/js/plugins/homes/homes.js @@ -0,0 +1,404 @@ +/************************************************************************* +## homes Plugin + +The homes plugin lets players set a location as home and return to the +location, invite other players to their home and also visit other +player's homes. + +This module is a good example of how to create a javascript-based +minecraft mod which provides... + + * A programmatic interface (API) and + * A command extension which uses that API to provide new functionality for players. + +The module uses the `plugin()` function to specify an object and +methods, and the `command()` function to expose functionality to +players through a new `jsp home` command. This module also +demonstrates how to enable autocompletion for custom commands (to see +this in action, at the in-game prompt or server console prompt type +`jsp home ` then press the TAB key - you should see a list of further +possible options). + +The `jsp home` command has the following options... + +### Basic options + + * `/jsp home set` Will set your current location as your + 'home' location to which you can return at any time using the ... + + * `/jsp home` ..command will return you to your home, if you have set one. + + * `/jsp home {player}` Will take you to the home of {player} (where + {player} is the name of the player whose home you wish to visit. + + * `/jsp home delete` Deletes your home location from the location + database. This does not actually remove the home from the world or + change the world in any way. This command is completely + non-destructive and cannot be used for griefing. No blocks will be + destroyed by this command. + +### Social options +The following options allow players to open their homes to all or some +players, invite players to their home and see a list of homes they can +visit. + + * `/jsp home list` Lists home which you can visit. + * `/jsp home ilist` Lists players who can visit your home. + * `/jsp home invite {player}` Invites the named player to your home. + * `/jsp home uninvite {player}` Uninvites (revokes invitation) the named player to your home. + * `/jsp home public` Opens your home to all players (all players can visit your home). + * `/jsp home private` Makes your home private (no longer visitable by all). + +### Administration options +The following administration options can only be used by server operators... + + * `/jsp home listall` List all of the homes + * `/jsp home clear {player}` Removes the player's home + location. Again, this command does not destroy any structures in + the world, it simply removes the location from the database. No + blocks are destroyed by this command. + +***/ +var utils = require('utils'), + bkTeleportCause = org.bukkit.event.player.PlayerTeleportEvent.TeleportCause, + bkBukkit = org.bukkit.Bukkit, + _store = { + houses: { }, + openHouses: { }, + invites: { } + }; +/* +*/ +var homes = plugin( 'homes', { + + help: function( ) { + return [ + /* basic functions */ + '/jsp home : Return to your own home', + '/jsp home {player} : Go to player home', + '/jsp home set : Set your current location as home', + '/jsp home delete : Delete your home location', + + /* social */ + '/jsp home list : List homes you can visit', + '/jsp home ilist : List players who can visit your home', + '/jsp home invite {player} : Invite {player} to your home', + '/jsp home uninvite {player} : Uninvite {player} to your home', + '/jsp home public : Open your home to all players', + '/jsp home private : Make your home private', + + /* administration */ + '/jsp home listall : Show all houses (ops only)', + '/jsp home clear {player} : Clears player home location (ops only)' + ]; + }, + /* ======================================================================== + basic functions + ======================================================================== */ + + go: function( guest, host ) { + var loc, + homeLoc; + if ( typeof host == 'undefined' ) { + host = guest; + } + guest = utils.player( guest ); + host = utils.player( host ); + loc = _store.houses[ host.name ]; + if ( !loc ) { + echo( guest, host.name + ' has no home' ); + return; + } + if ( !this._canVisit( guest, host ) ) { + echo( guest, 'You can not visit ' + host.name + "'s home yet" ); + return; + } + homeLoc = utils.locationFromJSON( loc ); + guest.teleport(homeLoc, bkTeleportCause.PLUGIN); + }, + /* + determine whether a guest is allow visit a host's home + */ + _canVisit: function( guest, host ) { + var invitations, + i; + if ( guest == host ) { + return true; + } + if ( _store.openHouses[ host.name ] ) { + return true; + } + invitations = _store.invites[ host.name ]; + if ( invitations ) { + for ( i = 0; i < invitations.length; i++ ) { + if ( invitations[i] == guest.name ) { + return true; + } + } + } + return false; + }, + + set: function( player ) { + player = utils.player( player ); + var loc = player.location; + _store.houses[player.name] = utils.locationToJSON( loc ); + }, + + remove: function( player ) { + player = utils.player( player ); + delete _store.houses[ player.name ]; + }, + /* ======================================================================== + social functions + ======================================================================== */ + + /* + list homes which the player can visit + */ + list: function( player ) { + var result = [], + ohp, + host, + guests, + i; + for ( ohp in _store.openHouses ) { + result.push(ohp); + } + player = utils.player(player); + for ( host in _store.invites ) { + guests = _store.invites[host]; + for ( i = 0; i < guests.length; i++ ) { + if ( guests[i] == player.name ) { + result.push(host); + } + } + } + return result; + }, + /* + list who can visit the player home + */ + ilist: function( player ) { + var result = [], + onlinePlayers, + i; + player = utils.player( player ); + // if home is public - all players + if ( _store.openHouses[player.name] ) { + onlinePlayers = bkBukkit.getOnlinePlayers(); + for ( i = 0; i < onlinePlayers.length; i++ ) { + if ( onlinePlayers[i].name != player.name) { + result.push( onlinePlayers[i].name ); + } + } + } else { + if ( _store.invites[player.name] ) { + result = _store.invites[ player.name ]; + } else { + result = []; + } + } + return result; + }, + /* + Invite a player to the home + */ + invite: function( host, guest ) { + host = utils.player( host ); + guest = utils.player( guest ); + var invitations = []; + if ( _store.invites[host.name] ) { + invitations = _store.invites[host.name]; + } + invitations.push( guest.name ); + _store.invites[host.name] = invitations; + echo( guest, host.name + ' has invited you to their home.' ); + echo( guest, 'type "/jsp home ' + host.name + '" to accept' ); + }, + /* + Uninvite someone to the home + */ + uninvite: function( host, guest ) { + var invitations, + revisedInvites, + i; + host = utils.player( host ); + guest = utils.player( guest ); + invitations = _store.invites[ host.name ]; + if ( !invitations ) { + return; + } + revisedInvites = []; + for ( i = 0; i < invitations.length; i++ ) { + if ( invitations[i] != guest.name ) { + revisedInvites.push( invitations[i] ); + } + } + _store.invites[host.name] = revisedInvites; + }, + /* + make the player house public + */ + open: function( player, optionalMsg ) { + player = utils.player( player ); + _store.openHouses[ player.name ] = true; + if ( typeof optionalMsg != 'undefined' ) { + __plugin.server.broadcastMessage( optionalMsg ); + } + }, + + /* + make the player house private + */ + close: function( player ) { + player = utils.player( player ); + delete _store.openHouses[ player.name ]; + }, + /* ======================================================================== + admin functions + ======================================================================== */ + listall: function( ) { + var result = []; + for ( var home in _store.houses ) { + result.push(home); + } + return result; + }, + + clear: function( player ) { + player = utils.player( player ); + delete _store.houses[ player.name ]; + delete _store.openHouses[ player.name ]; + }, + store: _store +}, true ); + +exports.homes = homes; + +/* + define a set of command options that can be used by players + */ +var options = { + + 'set': function( params, sender ) { + homes.set( sender ); + }, + + 'delete': function( params, sender ) { + homes.remove( sender ); + }, + + 'help': function( params, sender ) { + echo( sender, homes.help() ); + }, + + 'list': function( params, sender ) { + var visitable = homes.list(); + if ( visitable.length == 0 ) { + echo( sender, 'There are no homes to visit' ); + return; + } else { + echo( sender, [ + 'You can visit any of these ' + visitable.length + ' homes' + ,visitable.join(', ') + ]); + } + }, + + 'ilist': function( params, sender ) { + var potentialVisitors = homes.ilist(); + if ( potentialVisitors.length == 0 ) { + echo( sender, 'No one can visit your home'); + } else { + echo( sender, [ + 'These ' + potentialVisitors.length + 'players can visit your home', + potentialVisitors.join(', ')]); + } + }, + + 'invite': function( params, sender ) { + if ( params.length == 1 ) { + echo( sender, 'You must provide a player name' ); + return; + } + var playerName = params[1]; + var guest = utils.player( playerName ); + if ( !guest ) { + echo( sender, playerName + ' is not here' ); + } else { + homes.invite( sender, guest ); + } + }, + + 'uninvite': function( params, sender ) { + if ( params.length == 1 ) { + echo( sender, 'You must provide a player name' ); + return; + } + var playerName = params[1]; + var guest = utils.player( playerName ); + if ( !guest ) { + echo( sender, playerName + ' is not here' ); + } else { + homes.uninvite( sender, guest ); + } + }, + + 'public': function( params, sender ) { + homes.open( sender, params.slice( 1 ).join(' ') ); + echo( sender, 'Your home is open to the public' ); + }, + + 'private': function( params, sender ) { + homes.close( sender ); + echo( sender, 'Your home is closed to the public' ); + }, + + 'listall': function( params, sender ) { + if ( !sender.isOp() ) { + echo( sender, 'Only operators can do this' ); + } else { + echo( sender, homes.listall().join(', ') ); + } + }, + + 'clear': function( params, sender ) { + if ( !sender.isOp() ) { + echo( sender, 'Only operators can do this' ); + } else { + homes.clear( params[1], sender ); + } + } +}; + +var optionList = []; +for ( var o in options ) { + optionList.push(o); +} + +/* + Expose a set of commands that players can use at the in-game command prompt + */ +command( 'home', function ( params , sender) { + var option, + host; + if ( params.length == 0 ) { + homes.go( sender, sender ); + return; + } + option = options[ params[0] ]; + if ( option ) { + option( params, sender ); + } else { + host = utils.player( params[0] ); + if ( !host ) { + echo( sender, params[0] + ' is not here' ); + } else { + homes.go( sender, host ); + } + } +}, optionList ); + + + diff --git a/src/main/js/plugins/minigames/NumberGuess.js b/src/main/js/plugins/minigames/NumberGuess.js new file mode 100644 index 0000000..56770b0 --- /dev/null +++ b/src/main/js/plugins/minigames/NumberGuess.js @@ -0,0 +1,88 @@ +/************************************************************************* +## NumberGuess mini-game: + +### Description +This is a very simple number guessing game. Minecraft will ask you to +guess a number between 1 and 10 and you will tell you if you're too +hight or too low when you guess wrong. The purpose of this mini-game +code is to demonstrate use of Bukkit's Conversation API. + +### Example + + /js Game_NumberGuess.start(self) + +Once the game begins, guess a number by typing the `/` character +followed by a number between 1 and 10. + +***/ +var bkPrompt = org.bukkit.conversations.Prompt, + bkConversationFactory = org.bukkit.conversations.ConversationFactory, + bkConversationPrefix = org.bukkit.conversations.ConversationPrefix, + bkBukkit = org.bukkit.Bukkit; + +var sb = function( cmd ) { + bkBukkit.dispatchCommand( server.consoleSender, 'scoreboard ' + cmd ) ; +}; + +exports.Game_NumberGuess = { + start: function( sender ) { + + var guesses = 0; + + sb( 'objectives add NumberGuess dummy Guesses' ); + sb( 'players set ' + sender.name + ' NumberGuess ' + guesses ); + sb( 'objectives setdisplay sidebar NumberGuess' ); + + var number = Math.ceil( Math.random() * 10 ); + + var prompt = new bkPrompt( ) { + + getPromptText: function( ctx ) { + var hint = ''; + var h = ctx.getSessionData( 'hint' ); + if ( h ) { + hint = h; + } + return hint + 'Think of a number between 1 and 10'; + }, + + acceptInput: function( ctx, s ) { + s = s.replace( /^[^0-9]+/, '' ); // strip leading non-numeric characters (e.g. '/' ) + s = parseInt( s ); + if ( s == number ) { + setTimeout(function( ) { + ctx.forWhom.sendRawMessage( 'You guessed Correct!' ); + sb( 'objectives remove NumberGuess' ); + }, 100 ); + return null; + } else { + if ( s < number ) { + ctx.setSessionData( 'hint', 'too low\n' ); + } + if ( s > number ) { + ctx.setSessionData( 'hint', 'too high\n' ); + } + guesses++; + sb( 'players set ' + sender.name + ' NumberGuess ' + guesses ); + + return prompt; + } + }, + + blocksForInput: function( ctx ) { + return true; + } + }; + var convPrefix = new bkConversationPrefix( ) { + getPrefix: function( ctx ) { + return '[1-10] '; + } + }; + new bkConversationFactory( __plugin ) + .withModality( true ) + .withFirstPrompt( prompt ) + .withPrefix( convPrefix ) + .buildConversation( sender ) + .begin( ); + } +}; diff --git a/src/main/js/plugins/minigames/cow-clicker.js b/src/main/js/plugins/minigames/cow-clicker.js new file mode 100644 index 0000000..9b6836e --- /dev/null +++ b/src/main/js/plugins/minigames/cow-clicker.js @@ -0,0 +1,204 @@ +/************************************************************************* +## Cow Clicker Mini-Game + +### How to Play + +At the in-game prompt type `jsp cowclicker` to start or stop +playing. Right-Click on Cows to score points. No points for killing +cows (hint: use the same keyboard keys you'd use for opening doors). + +Every time you click a cow your score increases by 1 point. Your score +is displayed in a side-bar along the right edge of of the screen. + +![cow clicker](img/cowclicker.png) + +### Rules + + * You can join and leave the Cow Clicker game at any time by typing + `/jsp cowclicker` at the in-game prompt. + + * Once you leave the game, your score is reset to zero. + + * When you disconnect from the server, your score will be reset to zero. + +### Gameplay Mechanics + +This is meant as a trivially simple use of the [Bukkit Scoreboard +API][bukscore]. There are many things you'll want to consider when constructing +your own mini-game... + + * Is the game itself a long-lived game - that is - should players and + scores be persisted (stored) between server restarts? + + * What should happen when a player quits the server - should this also be + understood as quitting the mini-game? + + * What should happen when a player who was previously playing the + mini-game, joins the server - should they automatically resume the + mini-game? + +[bukscore]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scoreboard/package-summary.html + +***/ + +if (__plugin.canary || __plugin.bukkit){ + console.warn('cow-clicker minigame is not yet supported in CanaryMod and Craftbukkit'); + return; +} +var store = {}, + bkBukkit = org.bukkit.Bukkit, + bkCow = org.bukkit.entity.Cow, + bkOfflinePlayer = org.bukkit.OfflinePlayer, + scoreboardConfig = { + cowclicker: { + SIDEBAR: 'Cows Clicked' + } + }; +var scoreboard = require('minigames/scoreboard')(scoreboardConfig); + +var _onPlayerInteract = function( event ) { + var player = event.player, + clickedEntity = event.rightClicked, + loc = clickedEntity.location; + + if ( !store[ player.name ] ) { + return; + } + + var sound = function( snd, vol, pitch ) { + loc.world.playSound( loc, snd, vol, pitch ); + }; + + if ( clickedEntity instanceof bkCow) { + store[ player.name ].score++; + scoreboard.update( 'cowclicker', player, store[ player.name ].score ); + + bkBukkit.dispatchCommand( player, 'me clicked a cow!' ); + sound( bukkit.sound.CLICK, 1, 1 ); + setTimeout( function( ) { + sound( bukkit.sound.COW_HURT, 10, 0.85 ) ; + }, 200 ); + } +}; +var _onPlayerQuit = function( event ) { + _removePlayer( event.player ); +}; +var _onPlayerJoin = function( event ) { + var gamePlayer = store[event.player.name]; + if ( gamePlayer ) { + _addPlayer( event.player, gamePlayer.score ); + } +}; + +var _startGame = function( ) { + var p, + player; + if ( config.verbose ) { + console.log('Staring game: Cow Clicker'); + } + + events.playerQuit( _onPlayerQuit ); + events.playerJoin( _onPlayerJoin ); + events.playerInteractEntity( _onPlayerInteract ); + + scoreboard.start(); + + store = persist( 'cowclicker', store ); + for ( p in store ) { + player = server.getPlayer( p ); + if ( player ) { + /* + only add online players + */ + var score = store[p].score; + _addPlayer( player, score ); + } + } +}; + +var _addPlayer = function( player, score ) { + if ( config.verbose ) { + console.log( 'Adding player %s to Cow Clicker game', player ); + } + if ( typeof score == 'undefined' ) { + score = 0; + } + store[ player.name ] = { score: score }; + scoreboard.update( 'cowclicker', player, store[ player.name ].score); + + echo( player, 'Go forth and click some cows!' ); +}; + +var _removePlayer = function( player, notify ) { + + if ( player instanceof bkOfflinePlayer && player.player ) { + player = player.player; + } + + if ( !store[player.name] ) { + return; + } + if ( config.verbose ) { + console.log( 'Removing player %s from Cow Clicker', player ); + } + + var playerScore = store[ player.name ].score; + + scoreboard.restore( player ); + + delete store[ player.name ]; + if ( notify && player ) { + echo( player, 'You clicked ' + playerScore + ' cows! ' + + 'You must be tired after all that clicking.' ); + } +}; + +var _removeAllPlayers = function( notify ) { + if ( typeof notify == 'undefined' ) { + notify = false; + } + for ( var p in store ) { + var player = server.getOfflinePlayer( p ); + if ( player ) { + _removePlayer( player, notify ); + } + delete store[p]; + } +}; + +var _stopGame = function( removePlayers ) { + if ( typeof removePlayers == 'undefined' ) { + removePlayers = true; + } + if ( config.verbose ) { + console.log( 'Stopping game: Cow Clicker' ); + } + scoreboard.stop(); + if ( !removePlayers ) { + return; + } + _removeAllPlayers( false ); + persist( 'cowclicker', store.pers, 'w' ); + +}; +/* + start the game automatically when this module is loaded. + */ +_startGame(); +/* + players can join and leave the game by typing `jsp cowclicker` + */ +command( 'cowclicker', function( params, sender ) { + if ( !store[sender.name] ) { + _addPlayer( sender ); + } else { + _removePlayer( sender ); + } +}); +/* + stop the game when ScriptCraft is unloaded. + */ +addUnloadHandler( function( ) { + _stopGame( false ); +} ); + diff --git a/src/main/js/plugins/signs/examples.js b/src/main/js/plugins/signs/examples.js new file mode 100644 index 0000000..4bb39ba --- /dev/null +++ b/src/main/js/plugins/signs/examples.js @@ -0,0 +1,51 @@ +var signs = require('signs'); +// +// Usage: +// +// In game, create a sign , target it and type ... +// +// /js signs.menu_food(); +// +// ... or ... +// +// /js signs.menu_time() +// + +var onDinnerChoice = function(event){ + echo( event.player, 'You chose ' + event.text); +}; +var convertToDinnerMenu = signs.menu('Dinner', + ['Lamb','Pork','Chicken','Duck','Beef'], + onDinnerChoice); + +var onTimeChoice = function(event){ + event.player.location.world.setTime( event.number * 6000 ); +}; +var convertToTimeMenu = signs.menu('Time', ['Dawn','Midday','Dusk','Midnight'], onTimeChoice); + +exports.signs = { + menu_food: function(cmdSender){ + var sign = signs.getTargetedBy(cmdSender); + if (!sign){ + throw new Error('You must look at an existing sign'); + } + convertToDinnerMenu(sign); + }, + // + // This is an example sign that displays a menu of times of day + // interacting with the sign will change the time of day accordingly. + // + // In game, create a sign , target it and type ... + // + // /js var signExamples = require('./signs/examples'); + // /js signExamples.timeOfDay() + // + menu_time: function(cmdSender){ + var sign = signs.getTargetedBy(cmdSender); + if (!sign){ + throw new Error('You must look at an existing sign'); + } + convertToTimeMenu(sign); + } +}; + diff --git a/src/main/js/plugins/spawn.js b/src/main/js/plugins/spawn.js new file mode 100644 index 0000000..2ff425a --- /dev/null +++ b/src/main/js/plugins/spawn.js @@ -0,0 +1,42 @@ +'use strict'; +/*global Packages, __plugin, command, echo, isOp, org */ +/*jslint nomen: true, indent: 2 */ +/************************************************************************* +## Spawn Plugin + +Allows in-game operators to easily spawn creatures at current location. + +### Usage + + /jsp spawn cow + /jsp spawn sheep + /jsp spawn wolf + +This command supports TAB completion so to see a list of possible +entitities, type `/jsp spawn ' at the in-game command prompt, then +press TAB. Visit + (Bukkit/SpigotMC) +or (CanaryMod) + +for a list of possible entities (creatures) which can be spawned. + +***/ +var entities = require('entities'), + spawn = require('spawn'); +var entityNames = []; +for (var name in entities){ + entityNames.push(name); +} +command('spawn', function (parameters, sender) { + if (!isOp(sender)) { + echo(sender, 'Only operators can perform this command'); + return; + } + var location = sender.location; + if (!location) { + echo(sender, 'You have no location. This command only works in-game.'); + return; + } + var name = ('' + parameters[0]).toUpperCase(); + spawn( name, sender.location); +}, entityNames); diff --git a/src/main/js/readme.md b/src/main/js/readme.md new file mode 100644 index 0000000..ba82601 --- /dev/null +++ b/src/main/js/readme.md @@ -0,0 +1,13 @@ +# scriptcraft root directory + +This directory contains the following subdirectories... + + * lib - contains core scriptcraft modules and code. + * modules - contains modules for use by others + * plugins - contains plugins (modules which are automatically loaded and globally-namespaced at startup) + +If you are a minecraft modder who wants to develop simple mods then the `plugins` location is where you should probably place your .js files. + +If you are a minecraft modder who wants to develop more complex mods or provide an API for other modders, then modules intended for use by plugins (your own or others) should probably be placed in the `modules` directory. + +The `lib` directory is reserved for use by ScriptCraft. If a module is considered essential for all, or adds significantly useful new functionality to ScriptCraft then it should be placed in the `lib` directory. diff --git a/src/main/resources/Canary.inf b/src/main/resources/Canary.inf new file mode 100644 index 0000000..47f24d4 --- /dev/null +++ b/src/main/resources/Canary.inf @@ -0,0 +1,5 @@ +main-class = org.scriptcraftjs.canarymod.ScriptCraftPlugin +isLibrary = false +name = ScriptCraft +author = Walter Higgins +version = [[version]] \ No newline at end of file diff --git a/src/main/resources/boot.js b/src/main/resources/boot.js new file mode 100644 index 0000000..d0f4d54 --- /dev/null +++ b/src/main/resources/boot.js @@ -0,0 +1,98 @@ +/* + This file is the first and only file executed directly from the Java Plugin. + */ +var __scboot = null; +(function(){ + var File = java.io.File, + FileReader = java.io.FileReader, + FileOutputStream = java.io.FileOutputStream, + ZipInputStream = java.util.zip.ZipInputStream, + //jsPlugins = new File('plugins/scriptcraft'), + jsPlugins = new File('scriptcraft'), + initScript = 'lib/scriptcraft.js'; + + var unzip = function(zis, logger) { + var entry, + reason = null, + unzipFile = false, + zTime = 0, + fTime = 0, + fout = null, + c, + newFile; + + while ( ( entry = zis.nextEntry ) != null ) { + + newFile = new File(jsPlugins, entry.name); + if (entry.isDirectory()){ + newFile.mkdirs(); + zis.closeEntry(); + continue; + } + reason = null; + zTime = entry.time; + unzipFile = false; + if (!newFile.exists()) { + reason = 'NE'; + unzipFile = true; + }else{ + fTime = newFile.lastModified(); + if (zTime > fTime) { + reason = ((zTime - fTime) / 3600000) + "h"; + unzipFile = true; + } + } + if (unzipFile) { + logger.info('Unzipping ' + newFile.canonicalPath + ' (' + reason + ')' ); + fout = new FileOutputStream(newFile); + for (c = zis.read(); c != -1; c = zis.read()) { + fout.write(c); + } + fout.close(); + } + zis.closeEntry(); + } + zis.close(); + }; + /* + Called from Java plugin + */ + __scboot = function ( plugin, engine, classLoader ) + { + var logger = plugin.canary ? plugin.logman : plugin.logger, + initScriptFile = new File(jsPlugins,initScript), + zips = ['lib','plugins','modules'], + i = 0, + zis, + len = zips.length; + + if (!jsPlugins.exists()){ + logger.info('Directory ' + jsPlugins.canonicalPath + ' does not exist.'); + logger.info('Initializing ' + jsPlugins.canonicalPath + ' directory with contents from plugin archive.'); + jsPlugins.mkdirs(); + } + + for (i = 0; i < len;i++){ + if ( plugin.canary ) { + zis = new ZipInputStream(classLoader.getResourceAsStream(zips[i] + '.zip')); + unzip( zis, logger ); + } else { + if ( plugin.config.getBoolean('extract-js.' + zips[i]) ) { + zis = new ZipInputStream(plugin.getResource(zips[i] + '.zip')); + unzip( zis, logger ); + } + } + } + if (plugin.bukkit) { + plugin.saveDefaultConfig(); + } + try { + engine.eval(new FileReader(initScriptFile)); + __onEnable(engine, plugin, initScriptFile); + }catch ( e ){ + var msg = 'Error evaluating ' + initScriptFile + ': ' + e; + plugin.canary ? logger.error(msg) : logger.severe(msg); + throw e; + } + }; +})(); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..96d7c0f --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,4 @@ +extract-js: + plugins: true + modules: true + lib: true diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 56f245e..a61cf09 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ -name: ScriptCraftPlugin -main: net.walterhiggins.scriptcraft.ScriptCraftPlugin +name: scriptcraft +main: org.scriptcraftjs.bukkit.ScriptCraftPlugin version: [[version]] commands: js: @@ -12,11 +12,6 @@ commands: usage: / command-name command-parameters permission: scriptcraft.proxy permission-message: You don't have permission. - coffee: - description: Evaluate coffeescript. - usage: / Coffeescript code - permission: scriptcraft.evaluate - permission-message: You don't have permission. permissions: scriptcraft.*: