diff --git a/.classpath b/.classpath index aa55a3852..d8600e3ff 100644 --- a/.classpath +++ b/.classpath @@ -2,10 +2,36 @@ - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 000000000..8a54b61f2 --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +src/main/js/lib/json2.js \ No newline at end of file diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 000000000..646fc21b0 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,48 @@ +module.exports = { + "env": { + "browser": true, + "commonjs": true, + "nashorn": true + }, + "globals": { + "__plugin__": true, + "server": true, + "events": true, + "addUnloadHandler": true, + "__dirname": true, + "persist": true, + "isOp": true, + "echo": true, + "scload": true, + "nashorn": true, + "plugin": true, // not the same as __plugin__!!! + "command": true, + "config": true, + "window": false, + "document": false + }, + "extends": "eslint:recommended", + "rules": { + "indent": [ + "error", + 2 + ], + "no-fallthrough": [ + 0 + ], + "no-useless-escape": [ + 0 + ], + "quotes": [ + "error", + "single" + ], + "no-console": [ + 0 + ], + "semi": [ + "error", + "always" + ] + } +}; diff --git a/.gitignore b/.gitignore index b6ba71782..0ee4e41aa 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,12 @@ target *.#* *.idea .DS_Store +.vscode +.project +.settings +.classpath build.local.properties +build.local.properties.* /src/main/javascript/lib/.#tabcomplete.js /src/main/javascript/plugins/.#example-1.js /nbproject/private/private.xml @@ -24,7 +29,8 @@ build.local.properties /src/docs/build/classes/.netbeans_automatic_build /src/docs/build/classes/.netbeans_update_resources build +target nbproject /manifest.mf /platform.properties -CopyLibs \ No newline at end of file +CopyLibs diff --git a/.project b/.project index 27091781d..1cf747656 100644 --- a/.project +++ b/.project @@ -10,8 +10,25 @@ + + org.eclipse.m2e.core.maven2Builder + + + + org.eclipse.m2e.core.maven2Nature org.eclipse.jdt.core.javanature + + + 1622464902837 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/.travis.yml b/.travis.yml index b2168ad38..a6a271e16 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: java +dist: xenial jdk: - - oraclejdk8 - - oraclejdk7 - - openjdk7 + - oraclejdk9 + diff --git a/README.md b/README.md index 809d43fd9..a7df67a6d 100644 --- a/README.md +++ b/README.md @@ -1,243 +1,145 @@ # ScriptCraft - Modding Minecraft with Javascript -[![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) +[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/walterhiggins/ScriptCraft?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) -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. +ScriptCraft is a Minecraft Server plugin that lets operators, administrators, and plug-in authors customize the game using JavaScript. It provides a modern, high-performance scripting environment built directly into the server. -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. +Originally created to make it easier for younger programmers to create their own Minecraft Mods, ScriptCraft has evolved into a robust platform capable of supporting complex, modern JavaScript applications. - * 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. +Anything you can do using the Spigot APIs in Java, you can do using ScriptCraft in JavaScript. -This is a simple mod in a file called greet.js in the scriptcraft/plugins directory: +# Modern ScriptCraft Features -```javascript -function greet( player ) { - echo( player, 'Hello ' + player.name ); -} -exports.greet = greet; -``` - -At the in-game prompt, type: - -```javascript -/js greet(self) -``` +The latest version of ScriptCraft brings significant modernization to the platform: -Anything you can do using the Spigot or CanaryMod APIs in Java, -you can do using ScriptCraft in JavaScript. +* **Java 17 & GraalVM 23+**: High-performance execution with native support for the latest ECMAScript features (ES2023+). +* **Native Module Support**: Use standard `require()` (CommonJS) and `import/export` (ES Modules) natively via GraalVM. No custom wrappers are needed. +* **Global `fetch` API**: Make asynchronous HTTP requests easily using the standard `fetch()` syntax (backed by Java 11's `HttpClient`). +* **Modern I/O**: Access standard globals like `console` (with object inspection), `process`, `setTimeout`, and a complete `fs` module (including `fs.promises` for `async/await` file operations). +* **Modernized Events**: High-performance event multiplexing, `once()` support, and browser-standard `addEventListener()` aliases. +* **`EventEmitter` Support**: Native-feeling `EventEmitter` class for custom inter-plugin communication. +* **Strict JSON-Based Plugins**: A structured, modular plugin architecture using `plugin.json` manifests. +* **Native Bukkit Commands**: Commands declared in `plugin.json` are automatically registered as native, top-level Minecraft commands (e.g., `/mycmd`) with full support for native Tab Completion. +* **TypeScript & Autocompletion**: Full type definitions are included in `scriptcraft/typings/`. A reflection-based Type Generator (`src/tools/js/run-type-gen.js`) is provided to automatically generate types directly from the Bukkit server jar for perfect IntelliSense. -# Description +# JSON-Based Plugin Architecture (Strict Mode) -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: +ScriptCraft **strictly enforces** a structured, modular plugin architecture. Standalone `.js` files dropped into the `plugins/` folder are not supported. -* [SpigotMC][spigot] (Recommended) -* [GlowStone][gs] -* [CanaryMod][cm] +Every plugin must be housed in its own folder and contain a `plugin.json` file for metadata and configuration. -[spigot]: http://www.spigotmc.org/ -[gs]: http://www.glowstone.net/ -[cm]: http://canarymod.net/ +### Example: A Modern Plugin -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. +**`scriptcraft/plugins/my-plugin/plugin.json`** +```json +{ + "name": "super-tools", + "version": "1.0.0", + "main": "index.js", + "config": { + "welcomeMessage": "Welcome to the modernized server!" + }, + "commands": { + "spawn": { + "aliases": ["hub", "home"], + "description": "Teleports you to the main hub", + "tabComplete": ["world", "nether", "end"] + } + } +} +``` -[drone]: https://github.com/walterhiggins/ScriptCraft/tree/master/src/main/javascript/drone/drone.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/ +**`scriptcraft/plugins/my-plugin/index.js`** +```javascript +// registerPlugin is globally available +registerPlugin({ + // Lifecycle hooks + onEnable() { + console.info('Super Tools enabled! Config message:', this.config.welcomeMessage); + }, + + // Command handling + commands: { + // The aliases and tab completion defined in plugin.json are automatically registered natively! + spawn(args, player) { + echo(player, "Teleporting..."); + // ... teleport logic here ... + }, + + // You can also provide dynamic tab completion directly in the code + heal: { + callback(args, player) { + player.setHealth(20); + echo(player, "Healed!"); + }, + tabComplete(args, player) { + // Return dynamic suggestions (e.g., list of online players) + return server.getOnlinePlayers().map(p => p.name); + } + } + }, + + // Event handling + events: { + playerJoin(event) { + echo(event.player, this.config.welcomeMessage); + } + } +}); +``` # Prerequisites -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. - -# 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. +ScriptCraft is a Minecraft Server Mod which works with Minecraft for Personal computers (Windows, Mac, and Linux). -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. +You **must** have **Java version 17 or later** installed. Check your version by typing `java -version` at a command prompt. -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 server and have entered the game, look at a -ground-level block and type: - - /js up().box( blocks.wool.black, 4, 9, 1 ) +# Installation -… 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. +ScriptCraft works with the **SpigotMC** server software. -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. +1. Download and build the latest Spigot server (1.20+) using [BuildTools](https://www.spigotmc.org/wiki/buildtools/). +2. Run your server once to generate the necessary folders, accept the EULA in `eula.txt`, and start it again. +3. Download the `scriptcraft.jar` plugin and place it in your server's `plugins/` directory. +4. Restart the server. ScriptCraft will automatically generate a `scriptcraft/` directory in your server root containing the standard library, modules, and an `example-plugin`. -## Your first mod - Howling blocks -Listed below is a simple mod that will make blocks 'Howl' when they're broken. +# Getting Started & Development -``` 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 ); -``` +Once installed, navigate to the `scriptcraft/plugins/` directory. +* To create a new mod, create a new folder and add a `plugin.json` and `index.js` file. +* Use `console.log()` to debug; it outputs directly to the server console with color support. +* If you use VS Code or another modern editor, open the `scriptcraft/` folder directly. The included `typings/` folder will automatically provide full type checking and autocompletion for the Bukkit API, `fs`, `fetch`, and ScriptCraft globals. -If you're using CanaryMod instead of SpigotMC you can [download the equivalent code](https://gist.github.com/walterhiggins/69cddd15160d803fb096). +### Generating Updated Types +If you upgrade your Spigot server version, you can regenerate the TypeScript definitions to get the latest methods and classes: +From within the game or server console, use the legacy evaluate command to run the generator: +`/js require('../../tools/js/run-type-gen.js')` -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. +# Built-in APIs -To get started writing your own mod, take a look at some of the -[examples][examples]. +### `fetch` +Make async requests easily: +```javascript +const response = await fetch('https://api.example.com/data'); +const data = await response.json(); +console.log(data); +``` -[homes]: src/main/js/plugins/homes/homes.js -[examples]: src/main/js/plugins/examples/ +### `fs.promises` +Read and write files asynchronously without blocking the server: +```javascript +const content = await fs.promises.readFile('my-file.txt', 'UTF-8'); +await fs.promises.writeFile('out.txt', 'Hello World'); +``` # 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, 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 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]. - -# 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: +Because the SpigotMC API is open, all of the SpigotMC API is accessible via javascript. There are a couple of useful Java objects exposed globally: - * [A wolf-bot][wb] - * [L-Systems (Large-scale fractal structures in Minecraft)][ls] + * `__plugin__` – the ScriptCraft Java Plugin instance. + * `server` – The top-level org.bukkit.Server object. + * `self` – The player or console operator who invoked the current command. # Docker @@ -245,22 +147,4 @@ 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 +You can find all files used to build this container in the github project: [docker-scriptcraft](https://github.com/tclavier/docker-scriptcraft) diff --git a/build.properties b/build.properties index f6a95ea6f..fe7343aef 100644 --- a/build.properties +++ b/build.properties @@ -1 +1 @@ -scriptcraft-version=3.2.1 +scriptcraft-version=4.0.0-alpha-1 diff --git a/build.xml b/build.xml index 48d0e1b5c..3af5f5f41 100644 --- a/build.xml +++ b/build.xml @@ -11,26 +11,38 @@ - - + + + + - + + + + + - + + + - - - + + @@ -49,18 +61,15 @@ - - - - + - @@ -70,8 +79,10 @@ - - + + @@ -84,57 +95,50 @@ - - + - - + + - - + + + - - - - - - - - - - - - - - - - -
<!-- +
<!-- IMPORTANT NOTE FOR CONTRIBUTORS ------------------------------- Contributors: This file is generated from comments in javascript source files src/main/js/* @@ -151,20 +155,22 @@ Walter Higgins
- + - + - - @@ -173,7 +179,7 @@ Walter Higgins -
<!-- +
<!-- IMPORTANT NOTE FOR CONTRIBUTORS ------------------------------- Contributors: This file is generated from source file src/docs/templates/ypgpm.md @@ -183,80 +189,21 @@ Walter Higgins
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - [[version]] - - - [[version]] - - - - - - - + - + - + - + + + diff --git a/docs/API-Reference.md b/docs/API-Reference.md index fbfd45d54..f87cc4218 100644 --- a/docs/API-Reference.md +++ b/docs/API-Reference.md @@ -1,4 +1,4 @@ - - - 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 (SpigotMC version)](#items-module-spigotmc-version) + * [Usage](#usage) + * [Sounds module (SpigotMC version)](#sounds-module-spigotmc-version) + * [Usage](#usage-1) + * [Entities module](#entities-module) + * [Usage](#usage-2) -## Items module (CanaryMod version) +## Items module (SpigotMC 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 +See https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.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 + items.book(); // returns org.bukkit.Material.BOOK + items.book(2); // returns a new org.bukkit.inventory.ItemStack object of 2 books + items.book( itemType ); // compares itemType parameter to org.bukkit.Material.BOOK or an Item of type book The following functions are provided: + * acaciaBoat() + * acaciaButton() * acaciaDoor() * acaciaFence() * acaciaFenceGate() * acaciaLeaves() * acaciaLog() + * acaciaPlanks() + * acaciaPressurePlate() * acaciaSapling() + * acaciaSign() + * acaciaSlab() * acaciaStairs() + * acaciaTrapdoor() + * acaciaWallSign() * acaciaWood() - * acaciaWoodSlab() * activatorRail() + * air() * allium() + * ancientDebris() * andesite() + * andesiteSlab() + * andesiteStairs() + * andesiteWall() * anvil() * apple() * armorStand() * arrow() + * attachedMelonStem() + * attachedPumpkinStem() * azureBluet() * bakedPotato() - * banner() + * bamboo() + * bambooSapling() + * barrel() + * barrier() + * basalt() + * batSpawnEgg() * beacon() - * bed() * bedrock() + * beeNest() + * beeSpawnEgg() + * beef() + * beehive() + * beetroot() + * beetrootSeeds() + * beetrootSoup() + * beetroots() + * bell() + * birchBoat() + * birchButton() * birchDoor() * birchFence() * birchFenceGate() * birchLeaves() * birchLog() + * birchPlanks() + * birchPressurePlate() * birchSapling() + * birchSign() + * birchSlab() + * birchStairs() + * birchTrapdoor() + * birchWallSign() * birchWood() - * birchWoodSlab() - * birchWoodStairs() + * blackBanner() + * blackBed() * blackCarpet() - * blackGlass() - * blackGlassPane() - * blackStainedClay() + * blackConcrete() + * blackConcretePowder() + * blackDye() + * blackGlazedTerracotta() + * blackShulkerBox() + * blackStainedGlass() + * blackStainedGlassPane() + * blackTerracotta() + * blackWallBanner() + * blackWool() + * blackstone() + * blackstoneSlab() + * blackstoneStairs() + * blackstoneWall() + * blastFurnace() * blazePowder() * blazeRod() - * blocksRecord() + * blazeSpawnEgg() + * blueBanner() + * blueBed() * blueCarpet() - * blueGlass() - * blueGlassPane() + * blueConcrete() + * blueConcretePowder() + * blueDye() + * blueGlazedTerracotta() + * blueIce() * blueOrchid() - * blueStainedClay() - * boat() + * blueShulkerBox() + * blueStainedGlass() + * blueStainedGlassPane() + * blueTerracotta() + * blueWallBanner() + * blueWool() * bone() - * bonemeal() + * boneBlock() + * boneMeal() * book() - * bookAndQuill() * bookshelf() - * bottleOEnchanting() * bow() * bowl() + * brainCoral() + * brainCoralBlock() + * brainCoralFan() + * brainCoralWallFan() * bread() * brewingStand() - * brickBlock() + * brick() * brickSlab() * brickStairs() + * brickWall() + * bricks() + * brownBanner() + * brownBed() * brownCarpet() - * brownGlass() - * brownGlassPane() + * brownConcrete() + * brownConcretePowder() + * brownDye() + * brownGlazedTerracotta() * brownMushroom() - * brownStainedClay() + * brownMushroomBlock() + * brownShulkerBox() + * brownStainedGlass() + * brownStainedGlassPane() + * brownTerracotta() + * brownWallBanner() + * brownWool() + * bubbleColumn() + * bubbleCoral() + * bubbleCoralBlock() + * bubbleCoralFan() + * bubbleCoralWallFan() * bucket() - * burningFurnace() * cactus() - * cactusGreen() * cake() + * campfire() * carrot() * carrotOnAStick() * carrots() + * cartographyTable() + * carvedPumpkin() + * catSpawnEgg() * cauldron() + * caveAir() + * caveSpiderSpawnEgg() + * chain() + * chainCommandBlock() * chainmailBoots() * chainmailChestplate() * chainmailHelmet() * chainmailLeggings() * charcoal() * chest() - * chirpRecord() + * chestMinecart() + * chicken() + * chickenSpawnEgg() + * chippedAnvil() + * chiseledNetherBricks() + * chiseledPolishedBlackstone() + * chiseledQuartzBlock() + * chiseledRedSandstone() + * chiseledSandstone() + * chiseledStoneBricks() + * chorusFlower() + * chorusFruit() + * chorusPlant() * clay() * clayBall() - * clayBrick() - * clownFish() + * clock() * coal() * coalBlock() * coalOre() * coarseDirt() - * cobble() - * cobbleSilverFishBlock() - * cobbleSlab() - * cobbleStairs() + * cobblestone() + * cobblestoneSlab() + * cobblestoneStairs() * cobblestoneWall() + * cobweb() + * cocoa() * cocoaBeans() - * cocoaPlant() + * cod() + * codBucket() + * codSpawnEgg() * commandBlock() + * commandBlockMinecart() + * comparator() * compass() + * composter() + * conduit() + * cookedBeef() * cookedChicken() - * cookedClownFish() - * cookedFish() + * cookedCod() * cookedMutton() - * cookedPufferFish() + * cookedPorkchop() * cookedRabbit() * cookedSalmon() * cookie() - * crackedSilverFishBlock() - * crackedStoneBrick() + * cornflower() + * cowSpawnEgg() + * crackedNetherBricks() + * crackedPolishedBlackstoneBricks() + * crackedStoneBricks() + * craftingTable() + * creeperBannerPattern() * creeperHead() + * creeperSpawnEgg() + * creeperWallHead() + * crimsonButton() + * crimsonDoor() + * crimsonFence() + * crimsonFenceGate() + * crimsonFungus() + * crimsonHyphae() + * crimsonNylium() + * crimsonPlanks() + * crimsonPressurePlate() + * crimsonRoots() + * crimsonSign() + * crimsonSlab() + * crimsonStairs() + * crimsonStem() + * crimsonTrapdoor() + * crimsonWallSign() + * crossbow() + * cryingObsidian() + * cutRedSandstone() + * cutRedSandstoneSlab() + * cutSandstone() + * cutSandstoneSlab() + * cyanBanner() + * cyanBed() * cyanCarpet() + * cyanConcrete() + * cyanConcretePowder() * cyanDye() - * cyanGlass() - * cyanGlassPane() - * cyanStainedClay() - * dandelionYellow() + * cyanGlazedTerracotta() + * cyanShulkerBox() + * cyanStainedGlass() + * cyanStainedGlassPane() + * cyanTerracotta() + * cyanWallBanner() + * cyanWool() + * damagedAnvil() + * dandelion() + * darkOakBoat() + * darkOakButton() * darkOakDoor() * darkOakFence() * darkOakFenceGate() * darkOakLeaves() * darkOakLog() + * darkOakPlanks() + * darkOakPressurePlate() * darkOakSapling() + * darkOakSign() + * darkOakSlab() * darkOakStairs() + * darkOakTrapdoor() + * darkOakWallSign() * darkOakWood() - * darkOakWoodSlab() - * daylightSensor() + * darkPrismarine() + * darkPrismarineSlab() + * darkPrismarineStairs() + * daylightDetector() + * deadBrainCoral() + * deadBrainCoralBlock() + * deadBrainCoralFan() + * deadBrainCoralWallFan() + * deadBubbleCoral() + * deadBubbleCoralBlock() + * deadBubbleCoralFan() + * deadBubbleCoralWallFan() * deadBush() + * deadFireCoral() + * deadFireCoralBlock() + * deadFireCoralFan() + * deadFireCoralWallFan() + * deadHornCoral() + * deadHornCoralBlock() + * deadHornCoralFan() + * deadHornCoralWallFan() + * deadTubeCoral() + * deadTubeCoralBlock() + * deadTubeCoralFan() + * deadTubeCoralWallFan() + * debugStick() * detectorRail() * diamond() * diamondAxe() @@ -6948,107 +337,160 @@ The following functions are provided: * diamondLeggings() * diamondOre() * diamondPickaxe() - * diamondSpade() + * diamondShovel() * diamondSword() * diorite() + * dioriteSlab() + * dioriteStairs() + * dioriteWall() * dirt() * dispenser() - * doubleAcaciaWoodSlab() - * doubleBirchWoodSlab() - * doubleBrickBlockSlab() - * doubleCobbleSlab() - * doubleDarkOakWoodSlab() - * doubleGrass() - * doubleJungleWoodSlab() - * doubleNetherBrickSlab() - * doubleOakWoodSlab() - * doubleOrnateStoneSlab() - * doubleQuartzSlab() - * doubleRedSandstoneSlab() - * doubleSandStoneTrimSlab() - * doubleSandstoneSlab() - * doubleSpruceWoodSlab() - * doubleStoneBricksSlab() - * doubleStoneSlab() - * doubleWoodSlab() + * dolphinSpawnEgg() + * donkeySpawnEgg() + * dragonBreath() + * dragonEgg() + * dragonHead() + * dragonWallHead() + * driedKelp() + * driedKelpBlock() * dropper() + * drownedSpawnEgg() * egg() - * elevenRecord() + * elderGuardianSpawnEgg() + * elytra() * emerald() * emeraldBlock() * emeraldOre() - * emptyMap() * enchantedBook() - * enchantmentTable() + * enchantedGoldenApple() + * enchantingTable() + * endCrystal() + * endGateway() * endPortal() * endPortalFrame() + * endRod() * endStone() + * endStoneBrickSlab() + * endStoneBrickStairs() + * endStoneBrickWall() + * endStoneBricks() * enderChest() - * enderDragonEgg() + * enderEye() * enderPearl() - * eyeofEnder() - * farRecord() + * endermanSpawnEgg() + * endermiteSpawnEgg() + * evokerSpawnEgg() + * experienceBottle() + * farmland() * feather() - * fence() - * fenceGate() * fermentedSpiderEye() - * fireBlock() + * fern() + * filledMap() + * fire() * fireCharge() + * fireCoral() + * fireCoralBlock() + * fireCoralFan() + * fireCoralWallFan() * fireworkRocket() * fireworkStar() * fishingRod() + * fletchingTable() * flint() * flintAndSteel() + * flowerBannerPattern() * flowerPot() + * foxSpawnEgg() + * frostedIce() * furnace() + * furnaceMinecart() + * ghastSpawnEgg() * ghastTear() + * gildedBlackstone() * glass() * glassBottle() * glassPane() - * glisteringMelon() - * glowStone() + * glisteringMelonSlice() + * globeBannerPattern() + * glowstone() * glowstoneDust() - * goldAxe() * goldBlock() - * goldBoots() - * goldChestplate() - * goldHelmet() - * goldHoe() - * goldHorseArmor() * goldIngot() - * goldLeggings() * goldNugget() * goldOre() - * goldPickaxe() - * goldRecord() - * goldSpade() - * goldSword() * goldenApple() + * goldenAxe() + * goldenBoots() * goldenCarrot() + * goldenChestplate() + * goldenHelmet() + * goldenHoe() + * goldenHorseArmor() + * goldenLeggings() + * goldenPickaxe() + * goldenShovel() + * goldenSword() * granite() + * graniteSlab() + * graniteStairs() + * graniteWall() * grass() + * grassBlock() + * grassPath() * gravel() + * grayBanner() + * grayBed() * grayCarpet() + * grayConcrete() + * grayConcretePowder() * grayDye() - * grayGlass() - * grayGlassPane() - * grayStainedClay() + * grayGlazedTerracotta() + * grayShulkerBox() + * grayStainedGlass() + * grayStainedGlassPane() + * grayTerracotta() + * grayWallBanner() + * grayWool() + * greenBanner() + * greenBed() * greenCarpet() - * greenGlass() - * greenGlassPane() - * greenRecord() - * greenStainedClay() - * grilledPork() + * greenConcrete() + * greenConcretePowder() + * greenDye() + * greenGlazedTerracotta() + * greenShulkerBox() + * greenStainedGlass() + * greenStainedGlassPane() + * greenTerracotta() + * greenWallBanner() + * greenWool() + * grindstone() + * guardianSpawnEgg() * gunpowder() - * hardenedClay() - * hayBale() + * hayBlock() + * heartOfTheSea() * heavyWeightedPressurePlate() + * hoglinSpawnEgg() + * honeyBlock() + * honeyBottle() + * honeycomb() + * honeycombBlock() * hopper() - * hugeBrownMushroom() - * hugeRedMushroom() - * humanHead() + * hopperMinecart() + * hornCoral() + * hornCoralBlock() + * hornCoralFan() + * hornCoralWallFan() + * horseSpawnEgg() + * huskSpawnEgg() * ice() - * inkSack() + * infestedChiseledStoneBricks() + * infestedCobblestone() + * infestedCrackedStoneBricks() + * infestedMossyStoneBricks() + * infestedStone() + * infestedStoneBricks() + * inkSac() * ironAxe() * ironBars() * ironBlock() @@ -7060,291 +502,2103 @@ The following functions are provided: * ironHorseArmor() * ironIngot() * ironLeggings() + * ironNugget() * ironOre() * ironPickaxe() - * ironSpade() + * ironShovel() * ironSword() + * ironTrapdoor() * itemFrame() * jackOLantern() + * jigsaw() * jukebox() + * jungleBoat() + * jungleButton() * jungleDoor() * jungleFence() * jungleFenceGate() * jungleLeaves() * jungleLog() + * junglePlanks() + * junglePressurePlate() * jungleSapling() + * jungleSign() + * jungleSlab() + * jungleStairs() + * jungleTrapdoor() + * jungleWallSign() * jungleWood() - * jungleWoodSlab() - * jungleWoodStairs() + * kelp() + * kelpPlant() + * knowledgeBook() * ladder() + * lantern() * lapisBlock() * lapisLazuli() - * lapislazuliOre() + * lapisOre() * largeFern() * lava() * lavaBucket() - * lavaFlowing() * lead() * leather() * leatherBoots() * leatherChestplate() * leatherHelmet() + * leatherHorseArmor() * leatherLeggings() + * lectern() + * legacyAcaciaDoor() + * legacyAcaciaDoorItem() + * legacyAcaciaFence() + * legacyAcaciaFenceGate() + * legacyAcaciaStairs() + * legacyActivatorRail() + * legacyAir() + * legacyAnvil() + * legacyApple() + * legacyArmorStand() + * legacyArrow() + * legacyBakedPotato() + * legacyBanner() + * legacyBarrier() + * legacyBeacon() + * legacyBed() + * legacyBedBlock() + * legacyBedrock() + * legacyBeetroot() + * legacyBeetrootBlock() + * legacyBeetrootSeeds() + * legacyBeetrootSoup() + * legacyBirchDoor() + * legacyBirchDoorItem() + * legacyBirchFence() + * legacyBirchFenceGate() + * legacyBirchWoodStairs() + * legacyBlackGlazedTerracotta() + * legacyBlackShulkerBox() + * legacyBlazePowder() + * legacyBlazeRod() + * legacyBlueGlazedTerracotta() + * legacyBlueShulkerBox() + * legacyBoat() + * legacyBoatAcacia() + * legacyBoatBirch() + * legacyBoatDarkOak() + * legacyBoatJungle() + * legacyBoatSpruce() + * legacyBone() + * legacyBoneBlock() + * legacyBook() + * legacyBookAndQuill() + * legacyBookshelf() + * legacyBow() + * legacyBowl() + * legacyBread() + * legacyBrewingStand() + * legacyBrewingStandItem() + * legacyBrick() + * legacyBrickStairs() + * legacyBrownGlazedTerracotta() + * legacyBrownMushroom() + * legacyBrownShulkerBox() + * legacyBucket() + * legacyBurningFurnace() + * legacyCactus() + * legacyCake() + * legacyCakeBlock() + * legacyCarpet() + * legacyCarrot() + * legacyCarrotItem() + * legacyCarrotStick() + * legacyCauldron() + * legacyCauldronItem() + * legacyChainmailBoots() + * legacyChainmailChestplate() + * legacyChainmailHelmet() + * legacyChainmailLeggings() + * legacyChest() + * legacyChorusFlower() + * legacyChorusFruit() + * legacyChorusFruitPopped() + * legacyChorusPlant() + * legacyClay() + * legacyClayBall() + * legacyClayBrick() + * legacyCoal() + * legacyCoalBlock() + * legacyCoalOre() + * legacyCobbleWall() + * legacyCobblestone() + * legacyCobblestoneStairs() + * legacyCocoa() + * legacyCommand() + * legacyCommandChain() + * legacyCommandMinecart() + * legacyCommandRepeating() + * legacyCompass() + * legacyConcrete() + * legacyConcretePowder() + * legacyCookedBeef() + * legacyCookedChicken() + * legacyCookedFish() + * legacyCookedMutton() + * legacyCookedRabbit() + * legacyCookie() + * legacyCrops() + * legacyCyanGlazedTerracotta() + * legacyCyanShulkerBox() + * legacyDarkOakDoor() + * legacyDarkOakDoorItem() + * legacyDarkOakFence() + * legacyDarkOakFenceGate() + * legacyDarkOakStairs() + * legacyDaylightDetector() + * legacyDaylightDetectorInverted() + * legacyDeadBush() + * legacyDetectorRail() + * legacyDiamond() + * legacyDiamondAxe() + * legacyDiamondBarding() + * legacyDiamondBlock() + * legacyDiamondBoots() + * legacyDiamondChestplate() + * legacyDiamondHelmet() + * legacyDiamondHoe() + * legacyDiamondLeggings() + * legacyDiamondOre() + * legacyDiamondPickaxe() + * legacyDiamondSpade() + * legacyDiamondSword() + * legacyDiode() + * legacyDiodeBlockOff() + * legacyDiodeBlockOn() + * legacyDirt() + * legacyDispenser() + * legacyDoublePlant() + * legacyDoubleStep() + * legacyDoubleStoneSlab2() + * legacyDragonEgg() + * legacyDragonsBreath() + * legacyDropper() + * legacyEgg() + * legacyElytra() + * legacyEmerald() + * legacyEmeraldBlock() + * legacyEmeraldOre() + * legacyEmptyMap() + * legacyEnchantedBook() + * legacyEnchantmentTable() + * legacyEndBricks() + * legacyEndCrystal() + * legacyEndGateway() + * legacyEndRod() + * legacyEnderChest() + * legacyEnderPearl() + * legacyEnderPortal() + * legacyEnderPortalFrame() + * legacyEnderStone() + * legacyExpBottle() + * legacyExplosiveMinecart() + * legacyEyeOfEnder() + * legacyFeather() + * legacyFence() + * legacyFenceGate() + * legacyFermentedSpiderEye() + * legacyFire() + * legacyFireball() + * legacyFirework() + * legacyFireworkCharge() + * legacyFishingRod() + * legacyFlint() + * legacyFlintAndSteel() + * legacyFlowerPot() + * legacyFlowerPotItem() + * legacyFrostedIce() + * legacyFurnace() + * legacyGhastTear() + * legacyGlass() + * legacyGlassBottle() + * legacyGlowingRedstoneOre() + * legacyGlowstone() + * legacyGlowstoneDust() + * legacyGoldAxe() + * legacyGoldBarding() + * legacyGoldBlock() + * legacyGoldBoots() + * legacyGoldChestplate() + * legacyGoldHelmet() + * legacyGoldHoe() + * legacyGoldIngot() + * legacyGoldLeggings() + * legacyGoldNugget() + * legacyGoldOre() + * legacyGoldPickaxe() + * legacyGoldPlate() + * legacyGoldRecord() + * legacyGoldSpade() + * legacyGoldSword() + * legacyGoldenApple() + * legacyGoldenCarrot() + * legacyGrass() + * legacyGrassPath() + * legacyGravel() + * legacyGrayGlazedTerracotta() + * legacyGrayShulkerBox() + * legacyGreenGlazedTerracotta() + * legacyGreenRecord() + * legacyGreenShulkerBox() + * legacyGrilledPork() + * legacyHardClay() + * legacyHayBlock() + * legacyHopper() + * legacyHopperMinecart() + * legacyHugeMushroom1() + * legacyHugeMushroom2() + * legacyIce() + * legacyInkSack() + * legacyIronAxe() + * legacyIronBarding() + * legacyIronBlock() + * legacyIronBoots() + * legacyIronChestplate() + * legacyIronDoor() + * legacyIronDoorBlock() + * legacyIronFence() + * legacyIronHelmet() + * legacyIronHoe() + * legacyIronIngot() + * legacyIronLeggings() + * legacyIronNugget() + * legacyIronOre() + * legacyIronPickaxe() + * legacyIronPlate() + * legacyIronSpade() + * legacyIronSword() + * legacyIronTrapdoor() + * legacyItemFrame() + * legacyJackOLantern() + * legacyJukebox() + * legacyJungleDoor() + * legacyJungleDoorItem() + * legacyJungleFence() + * legacyJungleFenceGate() + * legacyJungleWoodStairs() + * legacyKnowledgeBook() + * legacyLadder() + * legacyLapisBlock() + * legacyLapisOre() + * legacyLava() + * legacyLavaBucket() + * legacyLeash() + * legacyLeather() + * legacyLeatherBoots() + * legacyLeatherChestplate() + * legacyLeatherHelmet() + * legacyLeatherLeggings() + * legacyLeaves() + * legacyLeaves2() + * legacyLever() + * legacyLightBlueGlazedTerracotta() + * legacyLightBlueShulkerBox() + * legacyLimeGlazedTerracotta() + * legacyLimeShulkerBox() + * legacyLingeringPotion() + * legacyLog() + * legacyLog2() + * legacyLongGrass() + * legacyMagentaGlazedTerracotta() + * legacyMagentaShulkerBox() + * legacyMagma() + * legacyMagmaCream() + * legacyMap() + * legacyMelon() + * legacyMelonBlock() + * legacyMelonSeeds() + * legacyMelonStem() + * legacyMilkBucket() + * legacyMinecart() + * legacyMobSpawner() + * legacyMonsterEgg() + * legacyMonsterEggs() + * legacyMossyCobblestone() + * legacyMushroomSoup() + * legacyMutton() + * legacyMycel() + * legacyNameTag() + * legacyNetherBrick() + * legacyNetherBrickItem() + * legacyNetherBrickStairs() + * legacyNetherFence() + * legacyNetherStalk() + * legacyNetherStar() + * legacyNetherWartBlock() + * legacyNetherWarts() + * legacyNetherrack() + * legacyNoteBlock() + * legacyObserver() + * legacyObsidian() + * legacyOrangeGlazedTerracotta() + * legacyOrangeShulkerBox() + * legacyPackedIce() + * legacyPainting() + * legacyPaper() + * legacyPinkGlazedTerracotta() + * legacyPinkShulkerBox() + * legacyPistonBase() + * legacyPistonExtension() + * legacyPistonMovingPiece() + * legacyPistonStickyBase() + * legacyPoisonousPotato() + * legacyPork() + * legacyPortal() + * legacyPotato() + * legacyPotatoItem() + * legacyPotion() + * legacyPoweredMinecart() + * legacyPoweredRail() + * legacyPrismarine() + * legacyPrismarineCrystals() + * legacyPrismarineShard() + * legacyPumpkin() + * legacyPumpkinPie() + * legacyPumpkinSeeds() + * legacyPumpkinStem() + * legacyPurpleGlazedTerracotta() + * legacyPurpleShulkerBox() + * legacyPurpurBlock() + * legacyPurpurDoubleSlab() + * legacyPurpurPillar() + * legacyPurpurSlab() + * legacyPurpurStairs() + * legacyQuartz() + * legacyQuartzBlock() + * legacyQuartzOre() + * legacyQuartzStairs() + * legacyRabbit() + * legacyRabbitFoot() + * legacyRabbitHide() + * legacyRabbitStew() + * legacyRails() + * legacyRawBeef() + * legacyRawChicken() + * legacyRawFish() + * legacyRecord10() + * legacyRecord11() + * legacyRecord12() + * legacyRecord3() + * legacyRecord4() + * legacyRecord5() + * legacyRecord6() + * legacyRecord7() + * legacyRecord8() + * legacyRecord9() + * legacyRedGlazedTerracotta() + * legacyRedMushroom() + * legacyRedNetherBrick() + * legacyRedRose() + * legacyRedSandstone() + * legacyRedSandstoneStairs() + * legacyRedShulkerBox() + * legacyRedstone() + * legacyRedstoneBlock() + * legacyRedstoneComparator() + * legacyRedstoneComparatorOff() + * legacyRedstoneComparatorOn() + * legacyRedstoneLampOff() + * legacyRedstoneLampOn() + * legacyRedstoneOre() + * legacyRedstoneTorchOff() + * legacyRedstoneTorchOn() + * legacyRedstoneWire() + * legacyRottenFlesh() + * legacySaddle() + * legacySand() + * legacySandstone() + * legacySandstoneStairs() + * legacySapling() + * legacySeaLantern() + * legacySeeds() + * legacyShears() + * legacyShield() + * legacyShulkerShell() + * legacySign() + * legacySignPost() + * legacySilverGlazedTerracotta() + * legacySilverShulkerBox() + * legacySkull() + * legacySkullItem() + * legacySlimeBall() + * legacySlimeBlock() + * legacySmoothBrick() + * legacySmoothStairs() + * legacySnow() + * legacySnowBall() + * legacySnowBlock() + * legacySoil() + * legacySoulSand() + * legacySpeckledMelon() + * legacySpectralArrow() + * legacySpiderEye() + * legacySplashPotion() + * legacySponge() + * legacySpruceDoor() + * legacySpruceDoorItem() + * legacySpruceFence() + * legacySpruceFenceGate() + * legacySpruceWoodStairs() + * legacyStainedClay() + * legacyStainedGlass() + * legacyStainedGlassPane() + * legacyStandingBanner() + * legacyStationaryLava() + * legacyStationaryWater() + * legacyStep() + * legacyStick() + * legacyStone() + * legacyStoneAxe() + * legacyStoneButton() + * legacyStoneHoe() + * legacyStonePickaxe() + * legacyStonePlate() + * legacyStoneSlab2() + * legacyStoneSpade() + * legacyStoneSword() + * legacyStorageMinecart() + * legacyString() + * legacyStructureBlock() + * legacyStructureVoid() + * legacySugar() + * legacySugarCane() + * legacySugarCaneBlock() + * legacySulphur() + * legacyThinGlass() + * legacyTippedArrow() + * legacyTnt() + * legacyTorch() + * legacyTotem() + * legacyTrapDoor() + * legacyTrappedChest() + * legacyTripwire() + * legacyTripwireHook() + * legacyVine() + * legacyWallBanner() + * legacyWallSign() + * legacyWatch() + * legacyWater() + * legacyWaterBucket() + * legacyWaterLily() + * legacyWeb() + * legacyWheat() + * legacyWhiteGlazedTerracotta() + * legacyWhiteShulkerBox() + * legacyWood() + * legacyWoodAxe() + * legacyWoodButton() + * legacyWoodDoor() + * legacyWoodDoubleStep() + * legacyWoodHoe() + * legacyWoodPickaxe() + * legacyWoodPlate() + * legacyWoodSpade() + * legacyWoodStairs() + * legacyWoodStep() + * legacyWoodSword() + * legacyWoodenDoor() + * legacyWool() + * legacyWorkbench() + * legacyWrittenBook() + * legacyYellowFlower() + * legacyYellowGlazedTerracotta() + * legacyYellowShulkerBox() * lever() + * lightBlueBanner() + * lightBlueBed() * lightBlueCarpet() + * lightBlueConcrete() + * lightBlueConcretePowder() * lightBlueDye() - * lightBlueGlass() - * lightBlueGlassPane() - * lightBlueStainedClay() + * lightBlueGlazedTerracotta() + * lightBlueShulkerBox() + * lightBlueStainedGlass() + * lightBlueStainedGlassPane() + * lightBlueTerracotta() + * lightBlueWallBanner() + * lightBlueWool() + * lightGrayBanner() + * lightGrayBed() * lightGrayCarpet() + * lightGrayConcrete() + * lightGrayConcretePowder() * lightGrayDye() - * lightGrayGlass() - * lightGrayGlassPane() - * lightGrayStainedClay() + * lightGrayGlazedTerracotta() + * lightGrayShulkerBox() + * lightGrayStainedGlass() + * lightGrayStainedGlassPane() + * lightGrayTerracotta() + * lightGrayWallBanner() + * lightGrayWool() * lightWeightedPressurePlate() * lilac() - * lilypad() + * lilyOfTheValley() + * lilyPad() + * limeBanner() + * limeBed() * limeCarpet() + * limeConcrete() + * limeConcretePowder() * limeDye() - * limeGlass() - * limeGlassPane() - * limeStainedClay() + * limeGlazedTerracotta() + * limeShulkerBox() + * limeStainedGlass() + * limeStainedGlassPane() + * limeTerracotta() + * limeWallBanner() + * limeWool() + * lingeringPotion() + * llamaSpawnEgg() + * lodestone() + * loom() + * magentaBanner() + * magentaBed() * magentaCarpet() + * magentaConcrete() + * magentaConcretePowder() * magentaDye() - * magentaGlass() - * magentaGlassPane() - * magentaStainedClay() + * magentaGlazedTerracotta() + * magentaShulkerBox() + * magentaStainedGlass() + * magentaStainedGlassPane() + * magentaTerracotta() + * magentaWallBanner() + * magentaWool() + * magmaBlock() * magmaCream() - * mallRecord() + * magmaCubeSpawnEgg() * map() - * mellohiRecord() * melon() * melonSeeds() * melonSlice() + * melonStem() * milkBucket() * minecart() - * minecartCommandBlock() - * minecartHopper() - * minecartTNT() - * mobSpawner() - * mossyBrickSilverFishBlock() - * mossyCobble() - * mossyCobbleWall() - * mossyStoneBrick() - * mushroomSoup() + * mojangBannerPattern() + * mooshroomSpawnEgg() + * mossyCobblestone() + * mossyCobblestoneSlab() + * mossyCobblestoneStairs() + * mossyCobblestoneWall() + * mossyStoneBrickSlab() + * mossyStoneBrickStairs() + * mossyStoneBrickWall() + * mossyStoneBricks() + * movingPiston() + * muleSpawnEgg() + * mushroomStem() + * mushroomStew() + * musicDisc11() + * musicDisc13() + * musicDiscBlocks() + * musicDiscCat() + * musicDiscChirp() + * musicDiscFar() + * musicDiscMall() + * musicDiscMellohi() + * musicDiscPigstep() + * musicDiscStal() + * musicDiscStrad() + * musicDiscWait() + * musicDiscWard() + * mutton() * mycelium() * nameTag() + * nautilusShell() * netherBrick() * netherBrickFence() + * netherBrickSlab() * netherBrickStairs() + * netherBrickWall() * netherBricks() - * netherBricksSlab() - * netherQuartz() + * netherGoldOre() + * netherPortal() * netherQuartzOre() + * netherSprouts() * netherStar() * netherWart() + * netherWartBlock() + * netheriteAxe() + * netheriteBlock() + * netheriteBoots() + * netheriteChestplate() + * netheriteHelmet() + * netheriteHoe() + * netheriteIngot() + * netheriteLeggings() + * netheritePickaxe() + * netheriteScrap() + * netheriteShovel() + * netheriteSword() * netherrack() * noteBlock() + * oakBoat() + * oakButton() + * oakDoor() + * oakFence() + * oakFenceGate() * oakLeaves() * oakLog() + * oakPlanks() + * oakPressurePlate() * oakSapling() + * oakSign() + * oakSlab() + * oakStairs() + * oakTrapdoor() + * oakWallSign() * oakWood() - * oakWoodSlab() + * observer() * obsidian() + * ocelotSpawnEgg() + * orangeBanner() + * orangeBed() * orangeCarpet() + * orangeConcrete() + * orangeConcretePowder() * orangeDye() - * orangeGlass() - * orangeGlassPane() - * orangeStainedClay() + * orangeGlazedTerracotta() + * orangeShulkerBox() + * orangeStainedGlass() + * orangeStainedGlassPane() + * orangeTerracotta() * orangeTulip() - * ornateQuartzBlock() - * ornateSilverFishBlock() - * ornateStoneBrick() - * ornateStoneSlab() + * orangeWallBanner() + * orangeWool() * oxeyeDaisy() * packedIce() * painting() + * pandaSpawnEgg() * paper() + * parrotSpawnEgg() * peony() - * pineLeaves() - * pineLog() - * pineWoodStairs() + * petrifiedOakSlab() + * phantomMembrane() + * phantomSpawnEgg() + * pigSpawnEgg() + * piglinBannerPattern() + * piglinBruteSpawnEgg() + * piglinSpawnEgg() + * pillagerSpawnEgg() + * pinkBanner() + * pinkBed() * pinkCarpet() + * pinkConcrete() + * pinkConcretePowder() * pinkDye() - * pinkGlass() - * pinkGlassPane() - * pinkStainedClay() + * pinkGlazedTerracotta() + * pinkShulkerBox() + * pinkStainedGlass() + * pinkStainedGlassPane() + * pinkTerracotta() * pinkTulip() + * pinkWallBanner() + * pinkWool() * piston() + * pistonHead() + * playerHead() + * playerWallHead() * podzol() * poisonousPotato() + * polarBearSpawnEgg() * polishedAndesite() + * polishedAndesiteSlab() + * polishedAndesiteStairs() + * polishedBasalt() + * polishedBlackstone() + * polishedBlackstoneBrickSlab() + * polishedBlackstoneBrickStairs() + * polishedBlackstoneBrickWall() + * polishedBlackstoneBricks() + * polishedBlackstoneButton() + * polishedBlackstonePressurePlate() + * polishedBlackstoneSlab() + * polishedBlackstoneStairs() + * polishedBlackstoneWall() * polishedDiorite() + * polishedDioriteSlab() + * polishedDioriteStairs() * polishedGranite() + * polishedGraniteSlab() + * polishedGraniteStairs() + * poppedChorusFruit() * poppy() - * pork() - * portal() + * porkchop() * potato() * potatoes() * potion() - * poweredMinecart() + * pottedAcaciaSapling() + * pottedAllium() + * pottedAzureBluet() + * pottedBamboo() + * pottedBirchSapling() + * pottedBlueOrchid() + * pottedBrownMushroom() + * pottedCactus() + * pottedCornflower() + * pottedCrimsonFungus() + * pottedCrimsonRoots() + * pottedDandelion() + * pottedDarkOakSapling() + * pottedDeadBush() + * pottedFern() + * pottedJungleSapling() + * pottedLilyOfTheValley() + * pottedOakSapling() + * pottedOrangeTulip() + * pottedOxeyeDaisy() + * pottedPinkTulip() + * pottedPoppy() + * pottedRedMushroom() + * pottedRedTulip() + * pottedSpruceSapling() + * pottedWarpedFungus() + * pottedWarpedRoots() + * pottedWhiteTulip() + * pottedWitherRose() * poweredRail() + * prismarine() + * prismarineBrickSlab() + * prismarineBrickStairs() + * prismarineBricks() * prismarineCrystals() * prismarineShard() - * pufferFish() + * prismarineSlab() + * prismarineStairs() + * prismarineWall() + * pufferfish() + * pufferfishBucket() + * pufferfishSpawnEgg() * pumpkin() * pumpkinPie() * pumpkinSeeds() + * pumpkinStem() + * purpleBanner() + * purpleBed() * purpleCarpet() + * purpleConcrete() + * purpleConcretePowder() * purpleDye() - * purpleGlass() - * purpleGlassPane() - * purpleStainedClay() + * purpleGlazedTerracotta() + * purpleShulkerBox() + * purpleStainedGlass() + * purpleStainedGlassPane() + * purpleTerracotta() + * purpleWallBanner() + * purpleWool() + * purpurBlock() + * purpurPillar() + * purpurSlab() + * purpurStairs() + * quartz() * quartzBlock() - * quartzPillarCap() - * quartzPillarHorizontal() - * quartzPillarVertical() + * quartzBricks() + * quartzPillar() * quartzSlab() * quartzStairs() + * rabbit() * rabbitFoot() * rabbitHide() + * rabbitSpawnEgg() * rabbitStew() * rail() - * rawBeef() - * rawChicken() - * rawFish() - * rawMutton() - * rawRabbit() - * rawSalmon() + * ravagerSpawnEgg() + * redBanner() + * redBed() * redCarpet() - * redGlass() - * redGlassPane() + * redConcrete() + * redConcretePowder() + * redDye() + * redGlazedTerracotta() * redMushroom() + * redMushroomBlock() + * redNetherBrickSlab() + * redNetherBrickStairs() + * redNetherBrickWall() + * redNetherBricks() + * redSand() * redSandstone() - * redSandstoneBlank() - * redSandstoneOrnate() * redSandstoneSlab() * redSandstoneStairs() - * redStainedClay() - * redStone() + * redSandstoneWall() + * redShulkerBox() + * redStainedGlass() + * redStainedGlassPane() + * redTerracotta() * redTulip() + * redWallBanner() + * redWool() + * redstone() * redstoneBlock() - * redstoneComparator() - * redstoneLampOff() + * redstoneLamp() * redstoneOre() - * redstoneRepeater() - * redstoneTorchOn() - * reed() + * redstoneTorch() + * redstoneWallTorch() + * redstoneWire() + * repeater() + * repeatingCommandBlock() + * respawnAnchor() * roseBush() - * roseRed() * rottenFlesh() * saddle() + * salmon() + * salmonBucket() + * salmonSpawnEgg() * sand() - * sandStoneTrimSlab() * sandstone() - * sandstoneBlank() - * sandstoneOrnate() * sandstoneSlab() * sandstoneStairs() - * seeds() + * sandstoneWall() + * scaffolding() + * scute() + * seaLantern() + * seaPickle() + * seagrass() * shears() - * shrub() - * sign() - * skeletonHead() + * sheepSpawnEgg() + * shield() + * shroomlight() + * shulkerBox() + * shulkerShell() + * shulkerSpawnEgg() + * silverfishSpawnEgg() + * skeletonHorseSpawnEgg() + * skeletonSkull() + * skeletonSpawnEgg() + * skeletonWallSkull() + * skullBannerPattern() * slimeBall() + * slimeBlock() + * slimeSpawnEgg() + * smithingTable() + * smoker() + * smoothQuartz() + * smoothQuartzSlab() + * smoothQuartzStairs() + * smoothRedSandstone() + * smoothRedSandstoneSlab() + * smoothRedSandstoneStairs() + * smoothSandstone() + * smoothSandstoneSlab() + * smoothSandstoneStairs() + * smoothStone() + * smoothStoneSlab() * snow() - * snowBall() * snowBlock() - * soil() + * snowball() + * soulCampfire() + * soulFire() + * soulLantern() * soulSand() - * spawnEgg() + * soulSoil() + * soulTorch() + * soulWallTorch() + * spawner() + * spectralArrow() * spiderEye() - * spiderWeb() + * spiderSpawnEgg() + * splashPotion() * sponge() + * spruceBoat() + * spruceButton() * spruceDoor() * spruceFence() * spruceFenceGate() + * spruceLeaves() + * spruceLog() + * sprucePlanks() + * sprucePressurePlate() * spruceSapling() + * spruceSign() + * spruceSlab() + * spruceStairs() + * spruceTrapdoor() + * spruceWallSign() * spruceWood() - * spruceWoodSlab() - * stalRecord() - * steak() + * squidSpawnEgg() * stick() * stickyPiston() * stone() * stoneAxe() - * stoneBrick() - * stoneBrickSilverFishBlock() + * stoneBrickSlab() * stoneBrickStairs() - * stoneBricksSlab() + * stoneBrickWall() + * stoneBricks() * stoneButton() * stoneHoe() * stonePickaxe() - * stonePlate() - * stoneSilverFishBlock() + * stonePressurePlate() + * stoneShovel() * stoneSlab() - * stoneSpade() + * stoneStairs() * stoneSword() - * storageMinecart() - * stradRecord() + * stonecutter() + * straySpawnEgg() + * striderSpawnEgg() * string() + * strippedAcaciaLog() + * strippedAcaciaWood() + * strippedBirchLog() + * strippedBirchWood() + * strippedCrimsonHyphae() + * strippedCrimsonStem() + * strippedDarkOakLog() + * strippedDarkOakWood() + * strippedJungleLog() + * strippedJungleWood() + * strippedOakLog() + * strippedOakWood() + * strippedSpruceLog() + * strippedSpruceWood() + * strippedWarpedHyphae() + * strippedWarpedStem() + * structureBlock() + * structureVoid() * sugar() + * sugarCane() * sunflower() - * tallFern() + * suspiciousStew() + * sweetBerries() + * sweetBerryBush() * tallGrass() + * tallSeagrass() + * target() + * terracotta() + * tippedArrow() * tnt() + * tntMinecart() * torch() - * trapdoor() + * totemOfUndying() + * traderLlamaSpawnEgg() * trappedChest() + * trident() + * tripwire() * tripwireHook() - * vines() - * waitRecord() - * wardRecord() - * watch() + * tropicalFish() + * tropicalFishBucket() + * tropicalFishSpawnEgg() + * tubeCoral() + * tubeCoralBlock() + * tubeCoralFan() + * tubeCoralWallFan() + * turtleEgg() + * turtleHelmet() + * turtleSpawnEgg() + * twistingVines() + * twistingVinesPlant() + * vexSpawnEgg() + * villagerSpawnEgg() + * vindicatorSpawnEgg() + * vine() + * voidAir() + * wallTorch() + * wanderingTraderSpawnEgg() + * warpedButton() + * warpedDoor() + * warpedFence() + * warpedFenceGate() + * warpedFungus() + * warpedFungusOnAStick() + * warpedHyphae() + * warpedNylium() + * warpedPlanks() + * warpedPressurePlate() + * warpedRoots() + * warpedSign() + * warpedSlab() + * warpedStairs() + * warpedStem() + * warpedTrapdoor() + * warpedWallSign() + * warpedWartBlock() * water() * waterBucket() - * waterFlowing() + * weepingVines() + * weepingVinesPlant() + * wetSponge() * wheat() + * wheatSeeds() + * whiteBanner() + * whiteBed() * whiteCarpet() - * whiteGlass() - * whiteGlassPane() - * whiteStainedClay() + * whiteConcrete() + * whiteConcretePowder() + * whiteDye() + * whiteGlazedTerracotta() + * whiteShulkerBox() + * whiteStainedGlass() + * whiteStainedGlassPane() + * whiteTerracotta() * 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() + * whiteWallBanner() + * whiteWool() + * witchSpawnEgg() + * witherRose() + * witherSkeletonSkull() + * witherSkeletonSpawnEgg() + * witherSkeletonWallSkull() + * wolfSpawnEgg() + * woodenAxe() + * woodenHoe() + * woodenPickaxe() + * woodenShovel() + * woodenSword() + * writableBook() * writtenBook() + * yellowBanner() + * yellowBed() * yellowCarpet() - * yellowFlower() - * yellowGlass() - * yellowGlassPane() - * yellowStainedClay() + * yellowConcrete() + * yellowConcretePowder() + * yellowDye() + * yellowGlazedTerracotta() + * yellowShulkerBox() + * yellowStainedGlass() + * yellowStainedGlassPane() + * yellowTerracotta() + * yellowWallBanner() + * yellowWool() + * zoglinSpawnEgg() * zombieHead() + * zombieHorseSpawnEgg() + * zombieSpawnEgg() + * zombieVillagerSpawnEgg() + * zombieWallHead() + * zombifiedPiglinSpawnEgg() + + +## Sounds module (SpigotMC version) + +This module provides a simple way to play sounds. + +### Usage + + var sounds = require("sounds"); + // plays ENTITY_WOLF_HOWL sound at full volume and medium pitch + sounds.play( org.bukkit.Sound.ENTITY_WOLF_HOWL, self, 1, 0); + // same as previous statement + sounds.play( org.bukkit.Sound.ENTITY_WOLF_HOWL , self ); + +The play() function takes as parameters: + + * A Sound value (see https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Sound.html for a list of possible values) + * A Location orbject or any object which has a location + * The Volume parameter is in the range 0 to 1 (default: 1) + * The Pitch parameter is in the range 0 to 4 (default: 0) + +In addition, the Sounds module provides a suite of helper functions - one for each possible sound. + + var sounds = require("sounds"); + // same as previous examples + sounds.entityWolfHowl( self ); + +Each of the following functions takes as parameters: + + * A Location orbject or any object which has a location + * The Volume parameter is in the range 0 to 1 (default: 1) + * The Pitch parameter is in the range 0 to 4 (default: 0) + +The following functions are provided for convenience and to help beginners explore sounds using TAB completion: + + * ambientBasaltDeltasAdditions() + * ambientBasaltDeltasLoop() + * ambientBasaltDeltasMood() + * ambientCave() + * ambientCrimsonForestAdditions() + * ambientCrimsonForestLoop() + * ambientCrimsonForestMood() + * ambientNetherWastesAdditions() + * ambientNetherWastesLoop() + * ambientNetherWastesMood() + * ambientSoulSandValleyAdditions() + * ambientSoulSandValleyLoop() + * ambientSoulSandValleyMood() + * ambientUnderwaterEnter() + * ambientUnderwaterExit() + * ambientUnderwaterLoop() + * ambientUnderwaterLoopAdditions() + * ambientUnderwaterLoopAdditionsRare() + * ambientUnderwaterLoopAdditionsUltraRare() + * ambientWarpedForestAdditions() + * ambientWarpedForestLoop() + * ambientWarpedForestMood() + * blockAncientDebrisBreak() + * blockAncientDebrisFall() + * blockAncientDebrisHit() + * blockAncientDebrisPlace() + * blockAncientDebrisStep() + * blockAnvilBreak() + * blockAnvilDestroy() + * blockAnvilFall() + * blockAnvilHit() + * blockAnvilLand() + * blockAnvilPlace() + * blockAnvilStep() + * blockAnvilUse() + * blockBambooBreak() + * blockBambooFall() + * blockBambooHit() + * blockBambooPlace() + * blockBambooSaplingBreak() + * blockBambooSaplingHit() + * blockBambooSaplingPlace() + * blockBambooStep() + * blockBarrelClose() + * blockBarrelOpen() + * blockBasaltBreak() + * blockBasaltFall() + * blockBasaltHit() + * blockBasaltPlace() + * blockBasaltStep() + * blockBeaconActivate() + * blockBeaconAmbient() + * blockBeaconDeactivate() + * blockBeaconPowerSelect() + * blockBeehiveDrip() + * blockBeehiveEnter() + * blockBeehiveExit() + * blockBeehiveShear() + * blockBeehiveWork() + * blockBellResonate() + * blockBellUse() + * blockBlastfurnaceFireCrackle() + * blockBoneBlockBreak() + * blockBoneBlockFall() + * blockBoneBlockHit() + * blockBoneBlockPlace() + * blockBoneBlockStep() + * blockBrewingStandBrew() + * blockBubbleColumnBubblePop() + * blockBubbleColumnUpwardsAmbient() + * blockBubbleColumnUpwardsInside() + * blockBubbleColumnWhirlpoolAmbient() + * blockBubbleColumnWhirlpoolInside() + * blockCampfireCrackle() + * blockChainBreak() + * blockChainFall() + * blockChainHit() + * blockChainPlace() + * blockChainStep() + * blockChestClose() + * blockChestLocked() + * blockChestOpen() + * blockChorusFlowerDeath() + * blockChorusFlowerGrow() + * blockComparatorClick() + * blockComposterEmpty() + * blockComposterFill() + * blockComposterFillSuccess() + * blockComposterReady() + * blockConduitActivate() + * blockConduitAmbient() + * blockConduitAmbientShort() + * blockConduitAttackTarget() + * blockConduitDeactivate() + * blockCoralBlockBreak() + * blockCoralBlockFall() + * blockCoralBlockHit() + * blockCoralBlockPlace() + * blockCoralBlockStep() + * blockCropBreak() + * blockDispenserDispense() + * blockDispenserFail() + * blockDispenserLaunch() + * blockEnchantmentTableUse() + * blockEndGatewaySpawn() + * blockEndPortalFrameFill() + * blockEndPortalSpawn() + * blockEnderChestClose() + * blockEnderChestOpen() + * blockFenceGateClose() + * blockFenceGateOpen() + * blockFireAmbient() + * blockFireExtinguish() + * blockFungusBreak() + * blockFungusFall() + * blockFungusHit() + * blockFungusPlace() + * blockFungusStep() + * blockFurnaceFireCrackle() + * blockGildedBlackstoneBreak() + * blockGildedBlackstoneFall() + * blockGildedBlackstoneHit() + * blockGildedBlackstonePlace() + * blockGildedBlackstoneStep() + * blockGlassBreak() + * blockGlassFall() + * blockGlassHit() + * blockGlassPlace() + * blockGlassStep() + * blockGrassBreak() + * blockGrassFall() + * blockGrassHit() + * blockGrassPlace() + * blockGrassStep() + * blockGravelBreak() + * blockGravelFall() + * blockGravelHit() + * blockGravelPlace() + * blockGravelStep() + * blockGrindstoneUse() + * blockHoneyBlockBreak() + * blockHoneyBlockFall() + * blockHoneyBlockHit() + * blockHoneyBlockPlace() + * blockHoneyBlockSlide() + * blockHoneyBlockStep() + * blockIronDoorClose() + * blockIronDoorOpen() + * blockIronTrapdoorClose() + * blockIronTrapdoorOpen() + * blockLadderBreak() + * blockLadderFall() + * blockLadderHit() + * blockLadderPlace() + * blockLadderStep() + * blockLanternBreak() + * blockLanternFall() + * blockLanternHit() + * blockLanternPlace() + * blockLanternStep() + * blockLavaAmbient() + * blockLavaExtinguish() + * blockLavaPop() + * blockLeverClick() + * blockLilyPadPlace() + * blockLodestoneBreak() + * blockLodestoneFall() + * blockLodestoneHit() + * blockLodestonePlace() + * blockLodestoneStep() + * blockMetalBreak() + * blockMetalFall() + * blockMetalHit() + * blockMetalPlace() + * blockMetalPressurePlateClickOff() + * blockMetalPressurePlateClickOn() + * blockMetalStep() + * blockNetherBricksBreak() + * blockNetherBricksFall() + * blockNetherBricksHit() + * blockNetherBricksPlace() + * blockNetherBricksStep() + * blockNetherGoldOreBreak() + * blockNetherGoldOreFall() + * blockNetherGoldOreHit() + * blockNetherGoldOrePlace() + * blockNetherGoldOreStep() + * blockNetherOreBreak() + * blockNetherOreFall() + * blockNetherOreHit() + * blockNetherOrePlace() + * blockNetherOreStep() + * blockNetherSproutsBreak() + * blockNetherSproutsFall() + * blockNetherSproutsHit() + * blockNetherSproutsPlace() + * blockNetherSproutsStep() + * blockNetherWartBreak() + * blockNetheriteBlockBreak() + * blockNetheriteBlockFall() + * blockNetheriteBlockHit() + * blockNetheriteBlockPlace() + * blockNetheriteBlockStep() + * blockNetherrackBreak() + * blockNetherrackFall() + * blockNetherrackHit() + * blockNetherrackPlace() + * blockNetherrackStep() + * blockNoteBlockBanjo() + * blockNoteBlockBasedrum() + * blockNoteBlockBass() + * blockNoteBlockBell() + * blockNoteBlockBit() + * blockNoteBlockChime() + * blockNoteBlockCowBell() + * blockNoteBlockDidgeridoo() + * blockNoteBlockFlute() + * blockNoteBlockGuitar() + * blockNoteBlockHarp() + * blockNoteBlockHat() + * blockNoteBlockIronXylophone() + * blockNoteBlockPling() + * blockNoteBlockSnare() + * blockNoteBlockXylophone() + * blockNyliumBreak() + * blockNyliumFall() + * blockNyliumHit() + * blockNyliumPlace() + * blockNyliumStep() + * blockPistonContract() + * blockPistonExtend() + * blockPortalAmbient() + * blockPortalTravel() + * blockPortalTrigger() + * blockPumpkinCarve() + * blockRedstoneTorchBurnout() + * blockRespawnAnchorAmbient() + * blockRespawnAnchorCharge() + * blockRespawnAnchorDeplete() + * blockRespawnAnchorSetSpawn() + * blockRootsBreak() + * blockRootsFall() + * blockRootsHit() + * blockRootsPlace() + * blockRootsStep() + * blockSandBreak() + * blockSandFall() + * blockSandHit() + * blockSandPlace() + * blockSandStep() + * blockScaffoldingBreak() + * blockScaffoldingFall() + * blockScaffoldingHit() + * blockScaffoldingPlace() + * blockScaffoldingStep() + * blockShroomlightBreak() + * blockShroomlightFall() + * blockShroomlightHit() + * blockShroomlightPlace() + * blockShroomlightStep() + * blockShulkerBoxClose() + * blockShulkerBoxOpen() + * blockSlimeBlockBreak() + * blockSlimeBlockFall() + * blockSlimeBlockHit() + * blockSlimeBlockPlace() + * blockSlimeBlockStep() + * blockSmithingTableUse() + * blockSmokerSmoke() + * blockSnowBreak() + * blockSnowFall() + * blockSnowHit() + * blockSnowPlace() + * blockSnowStep() + * blockSoulSandBreak() + * blockSoulSandFall() + * blockSoulSandHit() + * blockSoulSandPlace() + * blockSoulSandStep() + * blockSoulSoilBreak() + * blockSoulSoilFall() + * blockSoulSoilHit() + * blockSoulSoilPlace() + * blockSoulSoilStep() + * blockStemBreak() + * blockStemFall() + * blockStemHit() + * blockStemPlace() + * blockStemStep() + * blockStoneBreak() + * blockStoneButtonClickOff() + * blockStoneButtonClickOn() + * blockStoneFall() + * blockStoneHit() + * blockStonePlace() + * blockStonePressurePlateClickOff() + * blockStonePressurePlateClickOn() + * blockStoneStep() + * blockSweetBerryBushBreak() + * blockSweetBerryBushPlace() + * blockTripwireAttach() + * blockTripwireClickOff() + * blockTripwireClickOn() + * blockTripwireDetach() + * blockVineStep() + * blockWartBlockBreak() + * blockWartBlockFall() + * blockWartBlockHit() + * blockWartBlockPlace() + * blockWartBlockStep() + * blockWaterAmbient() + * blockWeepingVinesBreak() + * blockWeepingVinesFall() + * blockWeepingVinesHit() + * blockWeepingVinesPlace() + * blockWeepingVinesStep() + * blockWetGrassBreak() + * blockWetGrassFall() + * blockWetGrassHit() + * blockWetGrassPlace() + * blockWetGrassStep() + * blockWoodBreak() + * blockWoodFall() + * blockWoodHit() + * blockWoodPlace() + * blockWoodStep() + * blockWoodenButtonClickOff() + * blockWoodenButtonClickOn() + * blockWoodenDoorClose() + * blockWoodenDoorOpen() + * blockWoodenPressurePlateClickOff() + * blockWoodenPressurePlateClickOn() + * blockWoodenTrapdoorClose() + * blockWoodenTrapdoorOpen() + * blockWoolBreak() + * blockWoolFall() + * blockWoolHit() + * blockWoolPlace() + * blockWoolStep() + * enchantThornsHit() + * entityArmorStandBreak() + * entityArmorStandFall() + * entityArmorStandHit() + * entityArmorStandPlace() + * entityArrowHit() + * entityArrowHitPlayer() + * entityArrowShoot() + * entityBatAmbient() + * entityBatDeath() + * entityBatHurt() + * entityBatLoop() + * entityBatTakeoff() + * entityBeeDeath() + * entityBeeHurt() + * entityBeeLoop() + * entityBeeLoopAggressive() + * entityBeePollinate() + * entityBeeSting() + * entityBlazeAmbient() + * entityBlazeBurn() + * entityBlazeDeath() + * entityBlazeHurt() + * entityBlazeShoot() + * entityBoatPaddleLand() + * entityBoatPaddleWater() + * entityCatAmbient() + * entityCatBegForFood() + * entityCatDeath() + * entityCatEat() + * entityCatHiss() + * entityCatHurt() + * entityCatPurr() + * entityCatPurreow() + * entityCatStrayAmbient() + * entityChickenAmbient() + * entityChickenDeath() + * entityChickenEgg() + * entityChickenHurt() + * entityChickenStep() + * entityCodAmbient() + * entityCodDeath() + * entityCodFlop() + * entityCodHurt() + * entityCowAmbient() + * entityCowDeath() + * entityCowHurt() + * entityCowMilk() + * entityCowStep() + * entityCreeperDeath() + * entityCreeperHurt() + * entityCreeperPrimed() + * entityDolphinAmbient() + * entityDolphinAmbientWater() + * entityDolphinAttack() + * entityDolphinDeath() + * entityDolphinEat() + * entityDolphinHurt() + * entityDolphinJump() + * entityDolphinPlay() + * entityDolphinSplash() + * entityDolphinSwim() + * entityDonkeyAmbient() + * entityDonkeyAngry() + * entityDonkeyChest() + * entityDonkeyDeath() + * entityDonkeyEat() + * entityDonkeyHurt() + * entityDragonFireballExplode() + * entityDrownedAmbient() + * entityDrownedAmbientWater() + * entityDrownedDeath() + * entityDrownedDeathWater() + * entityDrownedHurt() + * entityDrownedHurtWater() + * entityDrownedShoot() + * entityDrownedStep() + * entityDrownedSwim() + * entityEggThrow() + * entityElderGuardianAmbient() + * entityElderGuardianAmbientLand() + * entityElderGuardianCurse() + * entityElderGuardianDeath() + * entityElderGuardianDeathLand() + * entityElderGuardianFlop() + * entityElderGuardianHurt() + * entityElderGuardianHurtLand() + * entityEnderDragonAmbient() + * entityEnderDragonDeath() + * entityEnderDragonFlap() + * entityEnderDragonGrowl() + * entityEnderDragonHurt() + * entityEnderDragonShoot() + * entityEnderEyeDeath() + * entityEnderEyeLaunch() + * entityEnderPearlThrow() + * entityEndermanAmbient() + * entityEndermanDeath() + * entityEndermanHurt() + * entityEndermanScream() + * entityEndermanStare() + * entityEndermanTeleport() + * entityEndermiteAmbient() + * entityEndermiteDeath() + * entityEndermiteHurt() + * entityEndermiteStep() + * entityEvokerAmbient() + * entityEvokerCastSpell() + * entityEvokerCelebrate() + * entityEvokerDeath() + * entityEvokerFangsAttack() + * entityEvokerHurt() + * entityEvokerPrepareAttack() + * entityEvokerPrepareSummon() + * entityEvokerPrepareWololo() + * entityExperienceBottleThrow() + * entityExperienceOrbPickup() + * entityFireworkRocketBlast() + * entityFireworkRocketBlastFar() + * entityFireworkRocketLargeBlast() + * entityFireworkRocketLargeBlastFar() + * entityFireworkRocketLaunch() + * entityFireworkRocketShoot() + * entityFireworkRocketTwinkle() + * entityFireworkRocketTwinkleFar() + * entityFishSwim() + * entityFishingBobberRetrieve() + * entityFishingBobberSplash() + * entityFishingBobberThrow() + * entityFoxAggro() + * entityFoxAmbient() + * entityFoxBite() + * entityFoxDeath() + * entityFoxEat() + * entityFoxHurt() + * entityFoxScreech() + * entityFoxSleep() + * entityFoxSniff() + * entityFoxSpit() + * entityFoxTeleport() + * entityGenericBigFall() + * entityGenericBurn() + * entityGenericDeath() + * entityGenericDrink() + * entityGenericEat() + * entityGenericExplode() + * entityGenericExtinguishFire() + * entityGenericHurt() + * entityGenericSmallFall() + * entityGenericSplash() + * entityGenericSwim() + * entityGhastAmbient() + * entityGhastDeath() + * entityGhastHurt() + * entityGhastScream() + * entityGhastShoot() + * entityGhastWarn() + * entityGuardianAmbient() + * entityGuardianAmbientLand() + * entityGuardianAttack() + * entityGuardianDeath() + * entityGuardianDeathLand() + * entityGuardianFlop() + * entityGuardianHurt() + * entityGuardianHurtLand() + * entityHoglinAmbient() + * entityHoglinAngry() + * entityHoglinAttack() + * entityHoglinConvertedToZombified() + * entityHoglinDeath() + * entityHoglinHurt() + * entityHoglinRetreat() + * entityHoglinStep() + * entityHorseAmbient() + * entityHorseAngry() + * entityHorseArmor() + * entityHorseBreathe() + * entityHorseDeath() + * entityHorseEat() + * entityHorseGallop() + * entityHorseHurt() + * entityHorseJump() + * entityHorseLand() + * entityHorseSaddle() + * entityHorseStep() + * entityHorseStepWood() + * entityHostileBigFall() + * entityHostileDeath() + * entityHostileHurt() + * entityHostileSmallFall() + * entityHostileSplash() + * entityHostileSwim() + * entityHuskAmbient() + * entityHuskConvertedToZombie() + * entityHuskDeath() + * entityHuskHurt() + * entityHuskStep() + * entityIllusionerAmbient() + * entityIllusionerCastSpell() + * entityIllusionerDeath() + * entityIllusionerHurt() + * entityIllusionerMirrorMove() + * entityIllusionerPrepareBlindness() + * entityIllusionerPrepareMirror() + * entityIronGolemAttack() + * entityIronGolemDamage() + * entityIronGolemDeath() + * entityIronGolemHurt() + * entityIronGolemRepair() + * entityIronGolemStep() + * entityItemBreak() + * entityItemFrameAddItem() + * entityItemFrameBreak() + * entityItemFramePlace() + * entityItemFrameRemoveItem() + * entityItemFrameRotateItem() + * entityItemPickup() + * entityLeashKnotBreak() + * entityLeashKnotPlace() + * entityLightningBoltImpact() + * entityLightningBoltThunder() + * entityLingeringPotionThrow() + * entityLlamaAmbient() + * entityLlamaAngry() + * entityLlamaChest() + * entityLlamaDeath() + * entityLlamaEat() + * entityLlamaHurt() + * entityLlamaSpit() + * entityLlamaStep() + * entityLlamaSwag() + * entityMagmaCubeDeath() + * entityMagmaCubeDeathSmall() + * entityMagmaCubeHurt() + * entityMagmaCubeHurtSmall() + * entityMagmaCubeJump() + * entityMagmaCubeSquish() + * entityMagmaCubeSquishSmall() + * entityMinecartInside() + * entityMinecartRiding() + * entityMooshroomConvert() + * entityMooshroomEat() + * entityMooshroomMilk() + * entityMooshroomShear() + * entityMooshroomSuspiciousMilk() + * entityMuleAmbient() + * entityMuleAngry() + * entityMuleChest() + * entityMuleDeath() + * entityMuleEat() + * entityMuleHurt() + * entityOcelotAmbient() + * entityOcelotDeath() + * entityOcelotHurt() + * entityPaintingBreak() + * entityPaintingPlace() + * entityPandaAggressiveAmbient() + * entityPandaAmbient() + * entityPandaBite() + * entityPandaCantBreed() + * entityPandaDeath() + * entityPandaEat() + * entityPandaHurt() + * entityPandaPreSneeze() + * entityPandaSneeze() + * entityPandaStep() + * entityPandaWorriedAmbient() + * entityParrotAmbient() + * entityParrotDeath() + * entityParrotEat() + * entityParrotFly() + * entityParrotHurt() + * entityParrotImitateBlaze() + * entityParrotImitateCreeper() + * entityParrotImitateDrowned() + * entityParrotImitateElderGuardian() + * entityParrotImitateEnderDragon() + * entityParrotImitateEndermite() + * entityParrotImitateEvoker() + * entityParrotImitateGhast() + * entityParrotImitateGuardian() + * entityParrotImitateHoglin() + * entityParrotImitateHusk() + * entityParrotImitateIllusioner() + * entityParrotImitateMagmaCube() + * entityParrotImitatePhantom() + * entityParrotImitatePiglin() + * entityParrotImitatePiglinBrute() + * entityParrotImitatePillager() + * entityParrotImitateRavager() + * entityParrotImitateShulker() + * entityParrotImitateSilverfish() + * entityParrotImitateSkeleton() + * entityParrotImitateSlime() + * entityParrotImitateSpider() + * entityParrotImitateStray() + * entityParrotImitateVex() + * entityParrotImitateVindicator() + * entityParrotImitateWitch() + * entityParrotImitateWither() + * entityParrotImitateWitherSkeleton() + * entityParrotImitateZoglin() + * entityParrotImitateZombie() + * entityParrotImitateZombieVillager() + * entityParrotStep() + * entityPhantomAmbient() + * entityPhantomBite() + * entityPhantomDeath() + * entityPhantomFlap() + * entityPhantomHurt() + * entityPhantomSwoop() + * entityPigAmbient() + * entityPigDeath() + * entityPigHurt() + * entityPigSaddle() + * entityPigStep() + * entityPiglinAdmiringItem() + * entityPiglinAmbient() + * entityPiglinAngry() + * entityPiglinBruteAmbient() + * entityPiglinBruteAngry() + * entityPiglinBruteConvertedToZombified() + * entityPiglinBruteDeath() + * entityPiglinBruteHurt() + * entityPiglinBruteStep() + * entityPiglinCelebrate() + * entityPiglinConvertedToZombified() + * entityPiglinDeath() + * entityPiglinHurt() + * entityPiglinJealous() + * entityPiglinRetreat() + * entityPiglinStep() + * entityPillagerAmbient() + * entityPillagerCelebrate() + * entityPillagerDeath() + * entityPillagerHurt() + * entityPlayerAttackCrit() + * entityPlayerAttackKnockback() + * entityPlayerAttackNodamage() + * entityPlayerAttackStrong() + * entityPlayerAttackSweep() + * entityPlayerAttackWeak() + * entityPlayerBigFall() + * entityPlayerBreath() + * entityPlayerBurp() + * entityPlayerDeath() + * entityPlayerHurt() + * entityPlayerHurtDrown() + * entityPlayerHurtOnFire() + * entityPlayerHurtSweetBerryBush() + * entityPlayerLevelup() + * entityPlayerSmallFall() + * entityPlayerSplash() + * entityPlayerSplashHighSpeed() + * entityPlayerSwim() + * entityPolarBearAmbient() + * entityPolarBearAmbientBaby() + * entityPolarBearDeath() + * entityPolarBearHurt() + * entityPolarBearStep() + * entityPolarBearWarning() + * entityPufferFishAmbient() + * entityPufferFishBlowOut() + * entityPufferFishBlowUp() + * entityPufferFishDeath() + * entityPufferFishFlop() + * entityPufferFishHurt() + * entityPufferFishSting() + * entityRabbitAmbient() + * entityRabbitAttack() + * entityRabbitDeath() + * entityRabbitHurt() + * entityRabbitJump() + * entityRavagerAmbient() + * entityRavagerAttack() + * entityRavagerCelebrate() + * entityRavagerDeath() + * entityRavagerHurt() + * entityRavagerRoar() + * entityRavagerStep() + * entityRavagerStunned() + * entitySalmonAmbient() + * entitySalmonDeath() + * entitySalmonFlop() + * entitySalmonHurt() + * entitySheepAmbient() + * entitySheepDeath() + * entitySheepHurt() + * entitySheepShear() + * entitySheepStep() + * entityShulkerAmbient() + * entityShulkerBulletHit() + * entityShulkerBulletHurt() + * entityShulkerClose() + * entityShulkerDeath() + * entityShulkerHurt() + * entityShulkerHurtClosed() + * entityShulkerOpen() + * entityShulkerShoot() + * entityShulkerTeleport() + * entitySilverfishAmbient() + * entitySilverfishDeath() + * entitySilverfishHurt() + * entitySilverfishStep() + * entitySkeletonAmbient() + * entitySkeletonDeath() + * entitySkeletonHorseAmbient() + * entitySkeletonHorseAmbientWater() + * entitySkeletonHorseDeath() + * entitySkeletonHorseGallopWater() + * entitySkeletonHorseHurt() + * entitySkeletonHorseJumpWater() + * entitySkeletonHorseStepWater() + * entitySkeletonHorseSwim() + * entitySkeletonHurt() + * entitySkeletonShoot() + * entitySkeletonStep() + * entitySlimeAttack() + * entitySlimeDeath() + * entitySlimeDeathSmall() + * entitySlimeHurt() + * entitySlimeHurtSmall() + * entitySlimeJump() + * entitySlimeJumpSmall() + * entitySlimeSquish() + * entitySlimeSquishSmall() + * entitySnowGolemAmbient() + * entitySnowGolemDeath() + * entitySnowGolemHurt() + * entitySnowGolemShear() + * entitySnowGolemShoot() + * entitySnowballThrow() + * entitySpiderAmbient() + * entitySpiderDeath() + * entitySpiderHurt() + * entitySpiderStep() + * entitySplashPotionBreak() + * entitySplashPotionThrow() + * entitySquidAmbient() + * entitySquidDeath() + * entitySquidHurt() + * entitySquidSquirt() + * entityStrayAmbient() + * entityStrayDeath() + * entityStrayHurt() + * entityStrayStep() + * entityStriderAmbient() + * entityStriderDeath() + * entityStriderEat() + * entityStriderHappy() + * entityStriderHurt() + * entityStriderRetreat() + * entityStriderSaddle() + * entityStriderStep() + * entityStriderStepLava() + * entityTntPrimed() + * entityTropicalFishAmbient() + * entityTropicalFishDeath() + * entityTropicalFishFlop() + * entityTropicalFishHurt() + * entityTurtleAmbientLand() + * entityTurtleDeath() + * entityTurtleDeathBaby() + * entityTurtleEggBreak() + * entityTurtleEggCrack() + * entityTurtleEggHatch() + * entityTurtleHurt() + * entityTurtleHurtBaby() + * entityTurtleLayEgg() + * entityTurtleShamble() + * entityTurtleShambleBaby() + * entityTurtleSwim() + * entityVexAmbient() + * entityVexCharge() + * entityVexDeath() + * entityVexHurt() + * entityVillagerAmbient() + * entityVillagerCelebrate() + * entityVillagerDeath() + * entityVillagerHurt() + * entityVillagerNo() + * entityVillagerTrade() + * entityVillagerWorkArmorer() + * entityVillagerWorkButcher() + * entityVillagerWorkCartographer() + * entityVillagerWorkCleric() + * entityVillagerWorkFarmer() + * entityVillagerWorkFisherman() + * entityVillagerWorkFletcher() + * entityVillagerWorkLeatherworker() + * entityVillagerWorkLibrarian() + * entityVillagerWorkMason() + * entityVillagerWorkShepherd() + * entityVillagerWorkToolsmith() + * entityVillagerWorkWeaponsmith() + * entityVillagerYes() + * entityVindicatorAmbient() + * entityVindicatorCelebrate() + * entityVindicatorDeath() + * entityVindicatorHurt() + * entityWanderingTraderAmbient() + * entityWanderingTraderDeath() + * entityWanderingTraderDisappeared() + * entityWanderingTraderDrinkMilk() + * entityWanderingTraderDrinkPotion() + * entityWanderingTraderHurt() + * entityWanderingTraderNo() + * entityWanderingTraderReappeared() + * entityWanderingTraderTrade() + * entityWanderingTraderYes() + * entityWitchAmbient() + * entityWitchCelebrate() + * entityWitchDeath() + * entityWitchDrink() + * entityWitchHurt() + * entityWitchThrow() + * entityWitherAmbient() + * entityWitherBreakBlock() + * entityWitherDeath() + * entityWitherHurt() + * entityWitherShoot() + * entityWitherSkeletonAmbient() + * entityWitherSkeletonDeath() + * entityWitherSkeletonHurt() + * entityWitherSkeletonStep() + * entityWitherSpawn() + * entityWolfAmbient() + * entityWolfDeath() + * entityWolfGrowl() + * entityWolfHowl() + * entityWolfHurt() + * entityWolfPant() + * entityWolfShake() + * entityWolfStep() + * entityWolfWhine() + * entityZoglinAmbient() + * entityZoglinAngry() + * entityZoglinAttack() + * entityZoglinDeath() + * entityZoglinHurt() + * entityZoglinStep() + * entityZombieAmbient() + * entityZombieAttackIronDoor() + * entityZombieAttackWoodenDoor() + * entityZombieBreakWoodenDoor() + * entityZombieConvertedToDrowned() + * entityZombieDeath() + * entityZombieDestroyEgg() + * entityZombieHorseAmbient() + * entityZombieHorseDeath() + * entityZombieHorseHurt() + * entityZombieHurt() + * entityZombieInfect() + * entityZombieStep() + * entityZombieVillagerAmbient() + * entityZombieVillagerConverted() + * entityZombieVillagerCure() + * entityZombieVillagerDeath() + * entityZombieVillagerHurt() + * entityZombieVillagerStep() + * entityZombifiedPiglinAmbient() + * entityZombifiedPiglinAngry() + * entityZombifiedPiglinDeath() + * entityZombifiedPiglinHurt() + * eventRaidHorn() + * itemArmorEquipChain() + * itemArmorEquipDiamond() + * itemArmorEquipElytra() + * itemArmorEquipGeneric() + * itemArmorEquipGold() + * itemArmorEquipIron() + * itemArmorEquipLeather() + * itemArmorEquipNetherite() + * itemArmorEquipTurtle() + * itemAxeStrip() + * itemBookPageTurn() + * itemBookPut() + * itemBottleEmpty() + * itemBottleFill() + * itemBottleFillDragonbreath() + * itemBucketEmpty() + * itemBucketEmptyFish() + * itemBucketEmptyLava() + * itemBucketFill() + * itemBucketFillFish() + * itemBucketFillLava() + * itemChorusFruitTeleport() + * itemCropPlant() + * itemCrossbowHit() + * itemCrossbowLoadingEnd() + * itemCrossbowLoadingMiddle() + * itemCrossbowLoadingStart() + * itemCrossbowQuickCharge1() + * itemCrossbowQuickCharge2() + * itemCrossbowQuickCharge3() + * itemCrossbowShoot() + * itemElytraFlying() + * itemFirechargeUse() + * itemFlintandsteelUse() + * itemHoeTill() + * itemHoneyBottleDrink() + * itemLodestoneCompassLock() + * itemNetherWartPlant() + * itemShieldBlock() + * itemShieldBreak() + * itemShovelFlatten() + * itemSweetBerriesPickFromBush() + * itemTotemUse() + * itemTridentHit() + * itemTridentHitGround() + * itemTridentReturn() + * itemTridentRiptide1() + * itemTridentRiptide2() + * itemTridentRiptide3() + * itemTridentThrow() + * itemTridentThunder() + * musicCreative() + * musicCredits() + * musicDisc11() + * musicDisc13() + * musicDiscBlocks() + * musicDiscCat() + * musicDiscChirp() + * musicDiscFar() + * musicDiscMall() + * musicDiscMellohi() + * musicDiscPigstep() + * musicDiscStal() + * musicDiscStrad() + * musicDiscWait() + * musicDiscWard() + * musicDragon() + * musicEnd() + * musicGame() + * musicMenu() + * musicNetherBasaltDeltas() + * musicNetherCrimsonForest() + * musicNetherNetherWastes() + * musicNetherSoulSandValley() + * musicNetherWarpedForest() + * musicUnderWater() + * particleSoulEscape() + * uiButtonClick() + * uiCartographyTableTakeResult() + * uiLoomSelectPattern() + * uiLoomTakeResult() + * uiStonecutterSelectRecipe() + * uiStonecutterTakeResult() + * uiToastChallengeComplete() + * uiToastIn() + * uiToastOut() + * weatherRain() + * weatherRainAbove() ## Entities module @@ -7370,16 +2624,20 @@ The following functions are provided: * armor_stand() * arrow() * bat() + * bee() * blaze() * boat() + * cat() * cave_spider() * chicken() - * complex_part() + * cod() * cow() * creeper() + * dolphin() * donkey() * dragon_fireball() * dropped_item() + * drowned() * egg() * elder_guardian() * ender_crystal() @@ -7395,16 +2653,18 @@ The following functions are provided: * fireball() * firework() * fishing_hook() + * fox() * ghast() * giant() * guardian() + * hoglin() * horse() * husk() + * illusioner() * iron_golem() * item_frame() * leash_hitch() * lightning() - * lingering_potion() * llama() * llama_spit() * magma_cube() @@ -7419,12 +2679,20 @@ The following functions are provided: * mushroom_cow() * ocelot() * painting() + * panda() + * parrot() + * phantom() * pig() - * pig_zombie() + * piglin() + * piglin_brute() + * pillager() * player() * polar_bear() * primed_tnt() + * pufferfish() * rabbit() + * ravager() + * salmon() * sheep() * shulker() * shulker_bullet() @@ -7440,19 +2708,25 @@ The following functions are provided: * splash_potion() * squid() * stray() + * strider() * thrown_exp_bottle() - * tipped_arrow() + * trader_llama() + * trident() + * tropical_fish() + * turtle() * unknown() * vex() * villager() * vindicator() - * weather() + * wandering_trader() * witch() * wither() * wither_skeleton() * wither_skull() * wolf() + * zoglin() * zombie() * zombie_horse() * zombie_villager() + * zombified_piglin() diff --git a/docs/Anatomy-of-a-Plugin.md b/docs/Anatomy-of-a-Plugin.md index 125b8e84b..573b88834 100644 --- a/docs/Anatomy-of-a-Plugin.md +++ b/docs/Anatomy-of-a-Plugin.md @@ -1,11 +1,11 @@ # 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 +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 @@ -36,18 +36,18 @@ color of their messages in the in-game chat window … ```javascript var store = persist('chat-colors', {players: {}}); -exports.chat = { - setColor: function(player,chatColor) { +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 +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 @@ -57,7 +57,7 @@ var colors = ['black', 'blue', 'darkgreen', 'darkaqua', 'darkred', 'yellow', 'white']; var colorCodes = {}; var COLOR_CHAR = '\u00a7'; -for (var i =0;i < colors.length;i++) +for (var i =0;i < colors.length;i++) colorCodes[colors[i]] = i.toString(16); var addColor = function( evt ) { @@ -68,29 +68,25 @@ var addColor = function( evt ) { } }; -if (__plugin.bukkit) { - events.asyncPlayerChat(addColor); -} else if (__plugin.canary) { - events.chat(addColor); -}; +events.asyncPlayerChat(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. + +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 +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 @@ -106,23 +102,23 @@ function chat_color( params, sender ){ 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 +… 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 +… 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) { +exports.chat = { + setColor: function(player,chatColor) { store.players[player.name] = chatColor; } } @@ -132,7 +128,7 @@ var colors = ['black', 'blue', 'darkgreen', 'darkaqua', 'darkred', 'yellow', 'white']; var colorCodes = {}; var COLOR_CHAR = '\u00a7'; -for (var i =0;i < colors.length;i++) +for (var i =0;i < colors.length;i++) colorCodes[colors[i]] = i.toString(16); var addColor = function( evt ) { @@ -143,11 +139,7 @@ var addColor = function( evt ) { } }; -if (__plugin.bukkit) { - events.asyncPlayerChat(addColor); -} else if (__plugin.canary) { - events.chat(addColor); -}; +events.asyncPlayerChat(addColor); function chat_color( params, sender ){ var color = params[0]; @@ -161,15 +153,14 @@ function chat_color( params, sender ){ 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. +… 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 index 792143a14..ff6230c51 100644 --- a/docs/Frequently-Asked-Questions.md +++ b/docs/Frequently-Asked-Questions.md @@ -14,18 +14,18 @@ The above question refers to using ScriptCraft for CraftBukkit so I'll answer th You can get the permissionsEx (or any other Bukkit plugin) like this... ```javascript -var pex = server.pluginManager.getPlugin('PermissionsEx'); +var pex = server.getPluginManager().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. +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.getPluginManager().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 pluginMgr = Canary.getPluginManager(); var dConomy = pluginMgr.getPlugin('dConomy'); var dConomyServer = dConomy.modServer; // from here on in you can access all of the dConomyServer object's calls @@ -35,12 +35,12 @@ var dConomyServer = dConomy.modServer; 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'); +var otherPlugin = server.getPluginManager().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'); +var otherPlugin = Canary.getPluginManager().getPlugin('PLUGIN_NAME_GOES_HERE'); ``` diff --git a/docs/YoungPersonsGuideToProgrammingMinecraft.md b/docs/YoungPersonsGuideToProgrammingMinecraft.md index a41d9738e..3d49ebe42 100644 --- a/docs/YoungPersonsGuideToProgrammingMinecraft.md +++ b/docs/YoungPersonsGuideToProgrammingMinecraft.md @@ -1,4 +1,4 @@ - + + org.graalvm.truffle + truffle-api + 23.0.0 + + + org.spigotmc + spigot-api + 1.20.1-R0.1-SNAPSHOT + provided + + + + + + src/main/java + + + maven-assembly-plugin + ... + + maven-compiler-plugin + 3.11.0 + + 17 + 17 + + + + + jar-with-dependencies + + false + ${project.artifactId}-${project.version} + + + + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + + + + + src/main/resources + ./ + + + + diff --git a/release-notes.md b/release-notes.md index 5d19238ed..00062517d 100644 --- a/release-notes.md +++ b/release-notes.md @@ -1,5 +1,19 @@ RELEASE NOTES + +============= +4.0.0-alpha-1 Release (2021 06 08) +-------------------------- +Removed A Large Amount of CanaryMod Implementations +Updated to Spigot 1.16.5 +Removed Drone lb + ============= +3.3.0 Release (2017 10 15) +-------------------------- + +Bug Fixes. +Updated from Spigot 1.11.2 to 1.12.2. +Expanded documentation (sounds). 3.2.1 Release (2016 12 23) -------------------------- @@ -269,7 +283,7 @@ Various bug fixes, enhanced classroom module and improved error logging on start 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 + which must be included in the CraftBukkit classpath. If using MQTT in ScriptCraft, then Craftbukkit should be launched like this... diff --git a/src/.prettierrc b/src/.prettierrc new file mode 100644 index 000000000..4480a3f65 --- /dev/null +++ b/src/.prettierrc @@ -0,0 +1,7 @@ +# .prettierrc +printWidth: 80 +singleQuote: true +tabWidth: 2 +useTabs: false +semi: true + diff --git a/src/docs/java/jscript.java b/src/docs/java/jscript.java index 53334a134..7ea08060e 100644 --- a/src/docs/java/jscript.java +++ b/src/docs/java/jscript.java @@ -1,17 +1,19 @@ import javax.script.*; import java.io.FileReader; -import net.canarymod.api.inventory.ItemType; +// import net.canarymod.api.inventory.ItemType; public class jscript { public static void main(String[] args) throws Exception { ScriptEngineManager factory = new ScriptEngineManager(); - ScriptEngine engine = factory.getEngineByName("JavaScript"); + ScriptEngine engine = factory.getEngineByName("js"); 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/js/generateApiDocs.js b/src/docs/js/generateApiDocs.js index 467dbf712..991cb709e 100644 --- a/src/docs/js/generateApiDocs.js +++ b/src/docs/js/generateApiDocs.js @@ -2,26 +2,24 @@ /* 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); +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) { +function find(dir, store, re) { var files = dir.listFiles(); - foreach (files, function(filename){ - filename = "" + filename; + foreach(files, function(filename) { + filename = '' + filename; var file = new File(filename); if (file.isDirectory()) { - find(file,store,re); + find(file, store, re); } else { - if (typeof re == "undefined") - store.push(filename); - else if (filename.match(re)) - store.push(filename); + if (typeof re == 'undefined') store.push(filename); + else if (filename.match(re)) store.push(filename); } }); } @@ -30,56 +28,50 @@ function find(dir,store,re) { (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) - { +function sorter(precedence) { + return function(a, b) { // convert from Java string to JS string - a = '' + a; + 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]; + var aparts = a.split(new RegExp('\/')); + var bparts = b.split(new RegExp('\/')); + 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++){ + 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) && 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 (a.match(re)) return -1; + if (b.match(re)) return 1; } - if(adirbdir) return 1; - afile = afile.replace(/\.js$/,''); - if (afile == adir){ + if (adir < bdir) return -1; + if (adir > bdir) return 1; + afile = afile.replace(new RegExp('\.js$'), ''); + if (afile == adir) { return -1; - } - else { + } else { var result = 0; - if (afile < bfile){ - result = -1; + if (afile < bfile) { + result = -1; } - if (afile > bfile){ + 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); +args = Array.prototype.slice.call(args, 1); -if (typeof importPackage == 'undefined'){ +if (typeof importPackage == 'undefined') { // load compatibility script load('nashorn:mozilla_compat.js'); } @@ -88,28 +80,36 @@ 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$/); +find(new io.File(dir), store, /\/[a-zA-Z0-9_\-]+\.js$/); -store.sort(sorter([ - /lib\/scriptcraft\.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/, + /lib\//, + /modules\/drone\/index\.js/, /modules\/drone\//, /plugins\/drone\//, /modules\//, /examples\// -])); + ]) +); var contents = []; -foreach(store, function(filename){ +foreach(store, function(filename) { + if (filename.match(new RegExp('babel\.js'))) { + return; + } + if (filename.match(new RegExp('underscore\.js'))) { + return; + } var br = new io.BufferedReader(new io.FileReader(filename)); - var line ; - while ( (line = br.readLine()) != null){ + var line; + while ((line = br.readLine()) != null) { contents.push(line); } br.close(); @@ -120,17 +120,16 @@ var writeComment = false; var startComment = /^\/\*{10}/; var endComment = /^\*{3}\//; -for (var i = 0;i < contents.length; i++){ +for (var i = 0; i < contents.length; i++) { var line = contents[i]; - if (line.match(startComment)){ + if (line.match(startComment)) { writeComment = true; i++; } - if (line.match(endComment)){ + if (line.match(endComment)) { writeComment = false; } - if (writeComment){ + if (writeComment) { java.lang.System.out.println(contents[i]); } } - diff --git a/src/docs/js/generateEntitiesDoc.js b/src/docs/js/generateEntitiesDocBukkit.js similarity index 73% rename from src/docs/js/generateEntitiesDoc.js rename to src/docs/js/generateEntitiesDocBukkit.js index f24e368c2..a61b00210 100644 --- a/src/docs/js/generateEntitiesDoc.js +++ b/src/docs/js/generateEntitiesDocBukkit.js @@ -1,4 +1,4 @@ -args = Array.prototype.slice.call(args,1); +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, @@ -13,12 +13,12 @@ var content = [ '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. ', + "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.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)', '', @@ -26,21 +26,23 @@ var content = [ '' ]; -var enumVals = [], t, i, name; +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(); }); + name = ('' + name).replace(new RegExp('^(.*)'), function(a) { + return a.toLowerCase(); + }); enumVals.push(' * ' + name + '()'); } } enumVals.sort(); content = content.concat(enumVals); content.push(''); -for (i = 0; i< content.length; i++){ +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 index d32fcb864..b7b1122b1 100644 --- a/src/docs/js/generateEventsHelper.js +++ b/src/docs/js/generateEventsHelper.js @@ -1,103 +1,120 @@ -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], + Bukkit = org.bukkit.event, + FileUtils = org.apache.commons.io.FileUtils, + FRAMEWORK = "Spigot", out = java.lang.System.out, err = java.lang.System.err, + System = java.lang.System, 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; + + +var jars = System.getProperty("java.class.path").split(";"); + + +var bukkit = "./spigot.jar"; +for(var j in jars){ + var m = jars[j].replace(/.+(\\|\/)([^\/\\]+)/, "$2") + var shrtname = m.replace(/.*(spigot).*\.jar\S*/i, "$1") + ""; + if (shrtname.toLowerCase() === "spigot"){ + bukkit = jars[j]; + } } +print("# " + bukkit); + +var zis = new ZipInputStream(new FileInputStream(bukkit)); + +let final = ` +/********************* + +## Events Helper Module (${FRAMEWORK} 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. + +***/ +`; + -for (var i = 0; i< content.length; i++){ - out.println(content[i]); +var names = []; +while ((entry = zis.getNextEntry()) != null) { + names.push(String(entry.getName())); } -while ( ( entry = zis.nextEntry) != null) { - var name = new String( entry.name ); + +names.sort(); +names.forEach(function(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 { + if (re1.test(name)) { + name = name.replace(new RegExp('\/', 'g'), '.').replace('.class', ''); + try { clz = java.lang.Class.forName(name); - }catch ( e) { + } catch (e) { err.println('Warning: could not Class.forName("' + name + '")'); clz = engine.eval(name); } var isAbstract = Modifier.isAbstract(clz.getModifiers()); - if ( isAbstract ) { - continue; + if (isAbstract) { + return; } 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/'; + shortName = name.replace('org.bukkit.event.', ''); + var fname = parts + .reverse() + .shift() + .replace(new RegExp('^(.)'), function(a) { + return a.toLowerCase(); + }); + fname = fname.replace(new RegExp('Hook$'), ''); + var javaDoc = 'https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/'; + var comment = [ '/*********************', '### events.' + fname + '()', '', - '#### Parameters ', + '#### Parameters', '', - ' * callback - A function which is called whenever the ['+ shortName + ' event](' + javaDoc + shortName.replace('.','/') + '.html) is fired', + ' * 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 + //http://jd.bukkit.org/rb/apidocs/org/bukkit/event/player/PlayerJoinEvent.html ]; - for (var i = 0; i < comment.length; i++){ - out.println(comment[i]); + for (var i = 0; i < comment.length; i++) { + final += comment[i] + "\n" } - 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('};'); - } -} + var fungen = 'exports.' + fname + ' = function(callback,priority){ \n' + + ' return events.on(' + name + ',callback,priority);\n' + + '};\n'; - + final += fungen; + } +}); + +FileUtils.writeStringToFile(new File("scriptcraft/lib/events-helper-bukkit.js"), final); diff --git a/src/docs/js/generateItemsDoc.js b/src/docs/js/generateItemsDoc.js deleted file mode 100644 index fe5715fe6..000000000 --- a/src/docs/js/generateItemsDoc.js +++ /dev/null @@ -1,77 +0,0 @@ -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/generateItemsDocBukkit.js b/src/docs/js/generateItemsDocBukkit.js new file mode 100644 index 000000000..f042da31e --- /dev/null +++ b/src/docs/js/generateItemsDocBukkit.js @@ -0,0 +1,40 @@ +'use strict'; +var out = java.lang.System.out; +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.inventory.ItemStack object of 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(new RegExp('(_.)', 'g'), function(a) { + return a.replace(new RegExp('_'), '').toUpperCase(); + }); + 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/generateItemsDocCanary.js b/src/docs/js/generateItemsDocCanary.js new file mode 100644 index 000000000..d5f18047c --- /dev/null +++ b/src/docs/js/generateItemsDocCanary.js @@ -0,0 +1,39 @@ +'use strict'; +/*global cmItemTypeClass*/ +var out = java.lang.System.out; +var 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(new RegExp('^(.)'), 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/generateSoundsDocBukkit.js b/src/docs/js/generateSoundsDocBukkit.js new file mode 100644 index 000000000..cee563308 --- /dev/null +++ b/src/docs/js/generateSoundsDocBukkit.js @@ -0,0 +1,59 @@ +var out = java.lang.System.out; +var content = [ + '', + '## Sounds module (SpigotMC version)', + '', + 'This module provides a simple way to play sounds.', + '', + '### Usage', + '', + ' var sounds = require("sounds");', + ' // plays ENTITY_WOLF_HOWL sound at full volume and medium pitch', + ' sounds.play( org.bukkit.Sound.ENTITY_WOLF_HOWL, self, 1, 0); ', + ' // same as previous statement', + ' sounds.play( org.bukkit.Sound.ENTITY_WOLF_HOWL , self );', + '', + 'The play() function takes as parameters:', + '', + ' * A Sound value (see https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Sound.html for a list of possible values)', + ' * A Location orbject or any object which has a location', + ' * The Volume parameter is in the range 0 to 1 (default: 1)', + ' * The Pitch parameter is in the range 0 to 4 (default: 0)', + '', + 'In addition, the Sounds module provides a suite of helper functions - one for each possible sound. ', + '', + ' var sounds = require("sounds");', + ' // same as previous examples', + ' sounds.entityWolfHowl( self );', + '', + 'Each of the following functions takes as parameters:', + '', + ' * A Location orbject or any object which has a location', + ' * The Volume parameter is in the range 0 to 1 (default: 1)', + ' * The Pitch parameter is in the range 0 to 4 (default: 0)', + '', + 'The following functions are provided for convenience and to help beginners explore sounds using TAB completion:', + '' +]; + +var enumVals = [], + i; +var sound, + soundName, + sounds = org.bukkit.Sound.values(); +for (i = 0; i < sounds.length; i++) { + sound = sounds[i]; + soundName = '' + sound.name(); + var methodName = ('' + soundName) + .toLowerCase() + .replace(new RegExp('_(.)', 'g'), function(a, b) { + return b.toUpperCase(); + }); + enumVals.push(' * ' + methodName + '()'); +} +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/generateSoundsDocCanary.js b/src/docs/js/generateSoundsDocCanary.js new file mode 100644 index 000000000..a4bd2cd7a --- /dev/null +++ b/src/docs/js/generateSoundsDocCanary.js @@ -0,0 +1,58 @@ +var out = java.lang.System.out; +var content = [ + '', + '## Sounds module (CanaryMod version)', + '', + 'This module provides a simple way to play sounds.', + '', + '### Usage', + '', + ' var sounds = require("sounds");', + ' // plays WOLF_HOWL sound at full volume and medium pitch', + ' sounds.play( Packages.net.canarymod.api.world.effects.SoundEffect.Type.WOLF_HOWL, self, 1, 0); ', + ' // same as previous statement', + ' sounds.play( Packages.net.canarymod.api.world.effects.SoundEffect.Type.WOLF_HOWL, self );', + '', + 'The play() function takes as parameters:', + '', + ' * A Sound value (see https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/world/effects/SoundEffect.Type.html for a list of possible values)', + ' * A Location orbject or any object which has a location', + ' * The Volume parameter is in the range 0 to 1 (default: 1)', + ' * The Pitch parameter is in the range 0 to 4 (default: 0)', + '', + 'In addition, the Sounds module provides a suite of helper functions - one for each possible sound. ', + '', + ' var sounds = require("sounds");', + ' // same as previous examples', + ' sounds.wolfHowl( self );', + '', + 'Each of the following functions takes as parameters:', + '', + ' * A Location orbject or any object which has a location', + ' * The Volume parameter is in the range 0 to 1 (default: 1)', + ' * The Pitch parameter is in the range 0 to 4 (default: 0)', + '', + 'The following functions are provided for convenience and to help beginners explore sounds using TAB completion:', + '' +]; + +var enumVals = [], + i, + sound, + soundName, + sounds; +sounds = Packages.net.canarymod.api.world.effects.SoundEffect.Type.values(); +for (i = 0; i < sounds.length; i++) { + sound = sounds[i]; + soundName = '' + sound.name(); + methodName = ('' + soundName).toLowerCase().replace(new RegExp('_(.)', 'g'), function(a, b) { + return b.toUpperCase(); + }); + enumVals.push(' * ' + methodName + '()'); +} +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/generateTOC.js b/src/docs/js/generateTOC.js index 22e35df68..4447684bf 100644 --- a/src/docs/js/generateTOC.js +++ b/src/docs/js/generateTOC.js @@ -1,10 +1,10 @@ -args = Array.prototype.slice.call(args,1); +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,''); - }; +if (typeof String.prototype.trim == 'undefined') { + String.prototype.trim = function() { + return this.replace(new RegExp('^\s+|\s+$', 'g'), ''); + }; } var template = args[0]; @@ -12,39 +12,40 @@ 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) ); +var contents = [], + line = undefined; +var br = new BufferedReader(new FileReader(template)); -while ( (line = br.readLine()) != null){ - contents.push(line); +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; +var createLink = function(text) { + var result = text.replace(new RegExp('_', 'g'), '_'); + result = result.replace(new RegExp('[^a-zA-Z0-9 _\-]', 'g'), ''); + result = result.replace(new RegExp(' ', '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 + ')'); - } +for (var i = 0; i < contents.length; i++) { + line = contents[i]; + if (line.match(new RegExp('^##\s+'))) { + var h2 = line.match(new RegExp('^##\s+(.*)'))[1].trim(); + var link = createLink(h2); + java.lang.System.out.println(' * [' + h2 + '](#' + link + ')'); + } + if (line.match(new RegExp('^###\s+'))) { + var h3 = line.match(new RegExp('^###\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 index 445166c48..86bc043ca 100644 --- a/src/docs/templates/ypgpm.md +++ b/src/docs/templates/ypgpm.md @@ -34,6 +34,7 @@ Follow these steps to download and install SpigotMC. 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. @@ -1243,11 +1244,13 @@ Reference][spigotapi] provides lots of valuable information about the different objects and methods available for use by ScriptCraft. +[spigotdl]: https://hub.spigotmc.org/jenkins/job/BuildTools/ +[dl]: https://github.com/walterhiggins/ScriptCraft/releases/latest [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/ +[sc-plugin]: https://scriptcraftjs.org/download/ [ce]: http://www.codecademy.com/ [mcdv]: http://www.minecraftwiki.net/wiki/Data_values [np]: http://notepad-plus-plus.org/ diff --git a/src/main/java/bukkit/org/scriptcraftjs/bukkit/ScriptCraftPlugin.java b/src/main/java/bukkit/org/scriptcraftjs/bukkit/ScriptCraftPlugin.java index 9713500dc..da43a5fe8 100644 --- a/src/main/java/bukkit/org/scriptcraftjs/bukkit/ScriptCraftPlugin.java +++ b/src/main/java/bukkit/org/scriptcraftjs/bukkit/ScriptCraftPlugin.java @@ -2,17 +2,29 @@ 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 org.apache.commons.io.FileUtils; + import java.util.ArrayList; import java.util.List; +import java.util.Enumeration; +import java.util.jar.JarFile; +import java.util.jar.JarEntry; + +import java.net.URL; +import java.net.JarURLConnection; + +import java.io.*; + +import javax.script.Invocable; + +import org.graalvm.polyglot.*; +import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine; + + -public class ScriptCraftPlugin extends JavaPlugin implements Listener +public class ScriptCraftPlugin extends JavaPlugin { public boolean canary = false; public boolean bukkit = true; @@ -20,53 +32,111 @@ public class ScriptCraftPlugin extends JavaPlugin implements Listener // 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; + protected GraalJSScriptEngine engine = null; + + + + + ScriptCraftPlugin self = this; + @Override public void onEnable() { Thread currentThread = Thread.currentThread(); - ClassLoader previousClassLoader = currentThread.getContextClassLoader(); - currentThread.setContextClassLoader(getClassLoader()); + currentThread.setContextClassLoader(getClass().getClassLoader()); + ClassLoader classLoader = currentThread.getContextClassLoader(); 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); - } + // The Node Environment controls the environment for many scripts + + // Check For Engines + // List engines = (new ScriptEngineManager()).getEngineFactories(); + // for (ScriptEngineFactory f: engines) { + // System.out.println(f.getLanguageName()+" -> "+f.getEngineName()+" ->"+f.getNames().toString()); + // } + + File scdir = new File("scriptcraft"); + if(scdir.exists() == false ){ + FileUtils.forceMkdir(scdir); + } + + // Loop through all resources + Enumeration res = classLoader.getResources("scriptcraft"); + while (res.hasMoreElements()) { + URL url = res.nextElement(); + JarURLConnection urlcon = (JarURLConnection) (url.openConnection()); + JarFile jar = urlcon.getJarFile(); + Enumeration entries = jar.entries(); + // Loop Through All Jar Entries + while (entries.hasMoreElements()) { + JarEntry element = entries.nextElement(); + String name = element.getName(); + // If this is a scriptcraft resource we will export it out of the jar + if (name.startsWith("scriptcraft")){ + // if it already exists continue + if(new File(name).exists()) continue; + System.out.println("Generating ScriptCraft File " + name); + // otherwise make new + if (element.isDirectory()) FileUtils.forceMkdir(new File(name)); + else FileUtils.copyInputStreamToFile(classLoader.getResourceAsStream(name), new File(name)); + } + } + } + + + System.out.println(scdir.getAbsolutePath()); + + Context.Builder builder = Context + .newBuilder("js") + .allowPolyglotAccess(PolyglotAccess.ALL) + .allowAllAccess(true) + .allowHostAccess(HostAccess.ALL) + .allowHostClassLookup(s -> true) + .allowHostClassLoading(true) + .allowCreateThread(true) + .allowIO(true) + .allowExperimentalOptions(true) + .option("js.nashorn-compat", "true") + .option("js.ecmascript-version", "latest") + .option("js.v8-compat", "true") + .option("js.commonjs-require", "true") + .option("js.commonjs-require-cwd", scdir.getAbsolutePath()) + .option("js.commonjs-require-path", "lib" + File.pathSeparator + "modules") + .option("js.esm-eval-returns-exports", "true") + .option("js.foreign-object-prototype", "true"); + + this.engine = GraalJSScriptEngine.create(null, builder); + this.engine.put("__plugin__", this); + + + //Now start up scriptcraft + File bootstrap = new File("scriptcraft/bootstrap.js"); + InputStreamReader jscript = new InputStreamReader( new FileInputStream(bootstrap)); + this.engine.eval(jscript); + + Invocable inv = (Invocable) this.engine; + inv.invokeFunction("__onEnable"); + + } catch (Exception e) { + e.printStackTrace(); + this.getLogger().severe(e.getMessage()); + } finally { + currentThread.setContextClassLoader(classLoader); + } } + 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; + // Tab completion is now handled directly by the dynamically registered BukkitCommands + // and the native-command.js utility, so we no longer need to proxy it through the + // legacy __onTabComplete global JS function for the base /jsp command. + return new ArrayList(); } - + 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); @@ -82,6 +152,6 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String if (jsResult != null){ return ((Boolean)jsResult).booleanValue(); } - return result; + return false; } } diff --git a/src/main/java/canary/org/scriptcraftjs/canarymod/ScriptCraftPlugin.java b/src/main/java/canary/org/scriptcraftjs/canarymod/ScriptCraftPlugin.java deleted file mode 100644 index 6c16cc171..000000000 --- a/src/main/java/canary/org/scriptcraftjs/canarymod/ScriptCraftPlugin.java +++ /dev/null @@ -1,164 +0,0 @@ -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/js/lib/command.js b/src/main/js/lib/command.js deleted file mode 100644 index 2ba6a4d29..000000000 --- a/src/main/js/lib/command.js +++ /dev/null @@ -1,64 +0,0 @@ -'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 deleted file mode 100644 index 854aa3402..000000000 --- a/src/main/js/lib/console.js +++ /dev/null @@ -1,79 +0,0 @@ -'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 deleted file mode 100644 index 875d91c34..000000000 --- a/src/main/js/lib/events-bukkit.js +++ /dev/null @@ -1,78 +0,0 @@ -/*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 deleted file mode 100644 index 59e983b72..000000000 --- a/src/main/js/lib/events-canary.js +++ /dev/null @@ -1,68 +0,0 @@ -/*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/find.js b/src/main/js/lib/find.js deleted file mode 100644 index 2f686fff5..000000000 --- a/src/main/js/lib/find.js +++ /dev/null @@ -1,29 +0,0 @@ -'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 deleted file mode 100644 index aaa065ecf..000000000 --- a/src/main/js/lib/java-utils.js +++ /dev/null @@ -1,29 +0,0 @@ -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 deleted file mode 100644 index 20cd7fa1c..000000000 --- a/src/main/js/lib/js-patch.js +++ /dev/null @@ -1,77 +0,0 @@ -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/js/lib/json2.js b/src/main/js/lib/json2.js deleted file mode 100644 index d89ecc7a2..000000000 --- a/src/main/js/lib/json2.js +++ /dev/null @@ -1,486 +0,0 @@ -/* - json2.js - 2013-05-26 - - Public Domain. - - NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. - - See http://www.JSON.org/js.html - - - This code should be minified before deployment. - See http://javascript.crockford.com/jsmin.html - - USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO - NOT CONTROL. - - - This file creates a global JSON object containing two methods: stringify - and parse. - - JSON.stringify(value, replacer, space) - value any JavaScript value, usually an object or array. - - replacer an optional parameter that determines how object - values are stringified for objects. It can be a - function or an array of strings. - - space an optional parameter that specifies the indentation - of nested structures. If it is omitted, the text will - be packed without extra whitespace. If it is a number, - it will specify the number of spaces to indent at each - level. If it is a string (such as '\t' or ' '), - it contains the characters used to indent at each level. - - This method produces a JSON text from a JavaScript value. - - When an object value is found, if the object contains a toJSON - method, its toJSON method will be called and the result will be - stringified. A toJSON method does not serialize: it returns the - value represented by the name/value pair that should be serialized, - or undefined if nothing should be serialized. The toJSON method - will be passed the key associated with the value, and this will be - bound to the value - - For example, this would serialize Dates as ISO strings. - - Date.prototype.toJSON = function (key) { - function f(n) { - // Format integers to have at least two digits. - return n < 10 ? '0' + n : n; - } - - return this.getUTCFullYear() + '-' + - f(this.getUTCMonth() + 1) + '-' + - f(this.getUTCDate()) + 'T' + - f(this.getUTCHours()) + ':' + - f(this.getUTCMinutes()) + ':' + - f(this.getUTCSeconds()) + 'Z'; - }; - - You can provide an optional replacer method. It will be passed the - key and value of each member, with this bound to the containing - object. The value that is returned from your method will be - serialized. If your method returns undefined, then the member will - be excluded from the serialization. - - If the replacer parameter is an array of strings, then it will be - used to select the members to be serialized. It filters the results - such that only members with keys listed in the replacer array are - stringified. - - Values that do not have JSON representations, such as undefined or - functions, will not be serialized. Such values in objects will be - dropped; in arrays they will be replaced with null. You can use - a replacer function to replace those with JSON values. - JSON.stringify(undefined) returns undefined. - - The optional space parameter produces a stringification of the - value that is filled with line breaks and indentation to make it - easier to read. - - If the space parameter is a non-empty string, then that string will - be used for indentation. If the space parameter is a number, then - the indentation will be that many spaces. - - Example: - - text = JSON.stringify(['e', {pluribus: 'unum'}]); - // text is '["e",{"pluribus":"unum"}]' - - - text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t'); - // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]' - - text = JSON.stringify([new Date()], function (key, value) { - return this[key] instanceof Date ? - 'Date(' + this[key] + ')' : value; - }); - // text is '["Date(---current time---)"]' - - - JSON.parse(text, reviver) - This method parses a JSON text to produce an object or array. - It can throw a SyntaxError exception. - - The optional reviver parameter is a function that can filter and - transform the results. It receives each of the keys and values, - and its return value is used instead of the original value. - If it returns what it received, then the structure is not modified. - If it returns undefined then the member is deleted. - - Example: - - // Parse the text. Values that look like ISO date strings will - // be converted to Date objects. - - myData = JSON.parse(text, function (key, value) { - var a; - if (typeof value === 'string') { - a = -/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value); - if (a) { - return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], - +a[5], +a[6])); - } - } - return value; - }); - - myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) { - var d; - if (typeof value === 'string' && - value.slice(0, 5) === 'Date(' && - value.slice(-1) === ')') { - d = new Date(value.slice(5, -1)); - if (d) { - return d; - } - } - return value; - }); - - - This is a reference implementation. You are free to copy, modify, or - redistribute. -*/ - -/*jslint evil: true, regexp: true */ - -/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply, - call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours, - getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join, - lastIndex, length, parse, prototype, push, replace, slice, stringify, - test, toJSON, toString, valueOf -*/ - - -// Create a JSON object only if one does not already exist. We create the -// methods in a closure to avoid creating global variables. - -if (typeof JSON !== 'object') { - JSON = {}; -} - -(function () { - 'use strict'; - - function f(n) { - // Format integers to have at least two digits. - return n < 10 ? '0' + n : n; - } - - if (typeof Date.prototype.toJSON !== 'function') { - - Date.prototype.toJSON = function () { - - return isFinite(this.valueOf()) - ? this.getUTCFullYear() + '-' + - f(this.getUTCMonth() + 1) + '-' + - f(this.getUTCDate()) + 'T' + - f(this.getUTCHours()) + ':' + - f(this.getUTCMinutes()) + ':' + - f(this.getUTCSeconds()) + 'Z' - : null; - }; - - String.prototype.toJSON = - Number.prototype.toJSON = - Boolean.prototype.toJSON = function () { - return this.valueOf(); - }; - } - - var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, - escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, - gap, - indent, - meta = { // table of character substitutions - '\b': '\\b', - '\t': '\\t', - '\n': '\\n', - '\f': '\\f', - '\r': '\\r', - '"' : '\\"', - '\\': '\\\\' - }, - rep; - - - function quote(string) { - -// If the string contains no control characters, no quote characters, and no -// backslash characters, then we can safely slap some quotes around it. -// Otherwise we must also replace the offending characters with safe escape -// sequences. - - escapable.lastIndex = 0; - return escapable.test(string) ? '"' + string.replace(escapable, function (a) { - var c = meta[a]; - return typeof c === 'string' - ? c - : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); - }) + '"' : '"' + string + '"'; - } - - - function str(key, holder) { - -// Produce a string from holder[key]. - - var i, // The loop counter. - k, // The member key. - v, // The member value. - length, - mind = gap, - partial, - value = holder[key]; - -// If the value has a toJSON method, call it to obtain a replacement value. - - if (value && typeof value === 'object' && - typeof value.toJSON === 'function') { - value = value.toJSON(key); - } - -// If we were called with a replacer function, then call the replacer to -// obtain a replacement value. - - if (typeof rep === 'function') { - value = rep.call(holder, key, value); - } - -// What happens next depends on the value's type. - - switch (typeof value) { - case 'string': - return quote(value); - - case 'number': - -// JSON numbers must be finite. Encode non-finite numbers as null. - - return isFinite(value) ? String(value) : 'null'; - - case 'boolean': - case 'null': - -// If the value is a boolean or null, convert it to a string. Note: -// typeof null does not produce 'null'. The case is included here in -// the remote chance that this gets fixed someday. - - return String(value); - -// If the type is 'object', we might be dealing with an object or an array or -// null. - - case 'object': - -// Due to a specification blunder in ECMAScript, typeof null is 'object', -// so watch out for that case. - - if (!value) { - return 'null'; - } - -// Make an array to hold the partial results of stringifying this object value. - - gap += indent; - partial = []; - -// Is the value an array? - - if (Object.prototype.toString.apply(value) === '[object Array]') { - -// The value is an array. Stringify every element. Use null as a placeholder -// for non-JSON values. - - length = value.length; - for (i = 0; i < length; i += 1) { - partial[i] = str(i, value) || 'null'; - } - -// Join all of the elements together, separated with commas, and wrap them in -// brackets. - - v = partial.length === 0 - ? '[]' - : gap - ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' - : '[' + partial.join(',') + ']'; - gap = mind; - return v; - } - -// If the replacer is an array, use it to select the members to be stringified. - - if (rep && typeof rep === 'object') { - length = rep.length; - for (i = 0; i < length; i += 1) { - if (typeof rep[i] === 'string') { - k = rep[i]; - v = str(k, value); - if (v) { - partial.push(quote(k) + (gap ? ': ' : ':') + v); - } - } - } - } else { - -// Otherwise, iterate through all of the keys in the object. - - for (k in value) { - if (Object.prototype.hasOwnProperty.call(value, k)) { - v = str(k, value); - if (v) { - partial.push(quote(k) + (gap ? ': ' : ':') + v); - } - } - } - } - -// Join all of the member texts together, separated with commas, -// and wrap them in braces. - - v = partial.length === 0 - ? '{}' - : gap - ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' - : '{' + partial.join(',') + '}'; - gap = mind; - return v; - } - } - -// If the JSON object does not yet have a stringify method, give it one. - - if (typeof JSON.stringify !== 'function') { - JSON.stringify = function (value, replacer, space) { - -// The stringify method takes a value and an optional replacer, and an optional -// space parameter, and returns a JSON text. The replacer can be a function -// that can replace values, or an array of strings that will select the keys. -// A default replacer method can be provided. Use of the space parameter can -// produce text that is more easily readable. - - var i; - gap = ''; - indent = ''; - -// If the space parameter is a number, make an indent string containing that -// many spaces. - - if (typeof space === 'number') { - for (i = 0; i < space; i += 1) { - indent += ' '; - } - -// If the space parameter is a string, it will be used as the indent string. - - } else if (typeof space === 'string') { - indent = space; - } - -// If there is a replacer, it must be a function or an array. -// Otherwise, throw an error. - - rep = replacer; - if (replacer && typeof replacer !== 'function' && - (typeof replacer !== 'object' || - typeof replacer.length !== 'number')) { - throw new Error('JSON.stringify'); - } - -// Make a fake root object containing our value under the key of ''. -// Return the result of stringifying the value. - - return str('', {'': value}); - }; - } - - -// If the JSON object does not yet have a parse method, give it one. - - if (typeof JSON.parse !== 'function') { - JSON.parse = function (text, reviver) { - -// The parse method takes a text and an optional reviver function, and returns -// a JavaScript value if the text is a valid JSON text. - - var j; - - function walk(holder, key) { - -// The walk method is used to recursively walk the resulting structure so -// that modifications can be made. - - var k, v, value = holder[key]; - if (value && typeof value === 'object') { - for (k in value) { - if (Object.prototype.hasOwnProperty.call(value, k)) { - v = walk(value, k); - if (v !== undefined) { - value[k] = v; - } else { - delete value[k]; - } - } - } - } - return reviver.call(holder, key, value); - } - - -// Parsing happens in four stages. In the first stage, we replace certain -// Unicode characters with escape sequences. JavaScript handles many characters -// incorrectly, either silently deleting them, or treating them as line endings. - - text = String(text); - cx.lastIndex = 0; - if (cx.test(text)) { - text = text.replace(cx, function (a) { - return '\\u' + - ('0000' + a.charCodeAt(0).toString(16)).slice(-4); - }); - } - -// In the second stage, we run the text against regular expressions that look -// for non-JSON patterns. We are especially concerned with '()' and 'new' -// because they can cause invocation, and '=' because it can cause mutation. -// But just to be safe, we want to reject all unexpected forms. - -// We split the second stage into 4 regexp operations in order to work around -// crippling inefficiencies in IE's and Safari's regexp engines. First we -// replace the JSON backslash pairs with '@' (a non-JSON character). Second, we -// replace all simple value tokens with ']' characters. Third, we delete all -// open brackets that follow a colon or comma or that begin the text. Finally, -// we look to see that the remaining characters are only whitespace or ']' or -// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval. - - if (/^[\],:{}\s]*$/ - .test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') - .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') - .replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { - -// In the third stage we use the eval function to compile the text into a -// JavaScript structure. The '{' operator is subject to a syntactic ambiguity -// in JavaScript: it can begin a block or an object literal. We wrap the text -// in parens to eliminate the ambiguity. - - j = eval('(' + text + ')'); - -// In the optional fourth stage, we recursively walk the new structure, passing -// each name/value pair to a reviver function for possible transformation. - - return typeof reviver === 'function' - ? walk({'': j}, '') - : j; - } - -// If the text is not JSON parseable, then a SyntaxError is thrown. - - throw new SyntaxError('JSON.parse'); - }; - } -}()); diff --git a/src/main/js/lib/legacy-check.js b/src/main/js/lib/legacy-check.js deleted file mode 100644 index 21bf89c45..000000000 --- a/src/main/js/lib/legacy-check.js +++ /dev/null @@ -1,32 +0,0 @@ -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 deleted file mode 100644 index ad3f73174..000000000 --- a/src/main/js/lib/nashorn-type.js +++ /dev/null @@ -1,8 +0,0 @@ -/* - 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 deleted file mode 100644 index 36eabb406..000000000 --- a/src/main/js/lib/persistence.js +++ /dev/null @@ -1,52 +0,0 @@ -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 deleted file mode 100644 index ac64be9a2..000000000 --- a/src/main/js/lib/plugin.js +++ /dev/null @@ -1,72 +0,0 @@ -'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 deleted file mode 100644 index 972a96600..000000000 --- a/src/main/js/lib/readme.md +++ /dev/null @@ -1,12 +0,0 @@ -# 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 deleted file mode 100644 index c75fba2ae..000000000 --- a/src/main/js/lib/require.js +++ /dev/null @@ -1,321 +0,0 @@ -/************************************************************************* -## 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 deleted file mode 100644 index 369f7c09c..000000000 --- a/src/main/js/lib/scriptcraft.js +++ /dev/null @@ -1,785 +0,0 @@ -'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 deleted file mode 100644 index 1928b3927..000000000 --- a/src/main/js/lib/tabcomplete-jsp.js +++ /dev/null @@ -1,48 +0,0 @@ -'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 deleted file mode 100644 index fd7a00c02..000000000 --- a/src/main/js/lib/tabcomplete.js +++ /dev/null @@ -1,255 +0,0 @@ -'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 deleted file mode 100644 index 959589df9..000000000 --- a/src/main/js/lib/task-bukkit.js +++ /dev/null @@ -1,24 +0,0 @@ -'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 deleted file mode 100644 index cbe268250..000000000 --- a/src/main/js/lib/task-canary.js +++ /dev/null @@ -1,35 +0,0 @@ -'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 deleted file mode 100644 index 03d33845d..000000000 --- a/src/main/js/modules/at.js +++ /dev/null @@ -1,155 +0,0 @@ -'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/blockhelper.js b/src/main/js/modules/blockhelper.js deleted file mode 100644 index 8955f7add..000000000 --- a/src/main/js/modules/blockhelper.js +++ /dev/null @@ -1,165 +0,0 @@ -'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/bukkit/fireworks.js b/src/main/js/modules/bukkit/fireworks.js deleted file mode 100644 index bf58840c0..000000000 --- a/src/main/js/modules/bukkit/fireworks.js +++ /dev/null @@ -1,46 +0,0 @@ -/* - 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 deleted file mode 100644 index c0db66290..000000000 --- a/src/main/js/modules/bukkit/input.js +++ /dev/null @@ -1,28 +0,0 @@ -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/items.js b/src/main/js/modules/bukkit/items.js deleted file mode 100644 index 74152953c..000000000 --- a/src/main/js/modules/bukkit/items.js +++ /dev/null @@ -1,29 +0,0 @@ -/*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 deleted file mode 100644 index 315fd0f3b..000000000 --- a/src/main/js/modules/bukkit/recipes.js +++ /dev/null @@ -1,15 +0,0 @@ -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 deleted file mode 100644 index dbf2875e2..000000000 --- a/src/main/js/modules/bukkit/sounds.js +++ /dev/null @@ -1,61 +0,0 @@ -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 deleted file mode 100644 index 1335c6d44..000000000 --- a/src/main/js/modules/canary/fireworks.js +++ /dev/null @@ -1,32 +0,0 @@ -'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 deleted file mode 100644 index de2ef63e2..000000000 --- a/src/main/js/modules/canary/input.js +++ /dev/null @@ -1,26 +0,0 @@ - -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 deleted file mode 100644 index 4a0d25c0d..000000000 --- a/src/main/js/modules/canary/inventory.js +++ /dev/null @@ -1,20 +0,0 @@ -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 deleted file mode 100644 index f943f1dae..000000000 --- a/src/main/js/modules/canary/items.js +++ /dev/null @@ -1,70 +0,0 @@ -/*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 deleted file mode 100644 index 3729291e2..000000000 --- a/src/main/js/modules/canary/recipes.js +++ /dev/null @@ -1,48 +0,0 @@ -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 deleted file mode 100644 index 1d8bcc4ba..000000000 --- a/src/main/js/modules/canary/sounds.js +++ /dev/null @@ -1,65 +0,0 @@ -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 deleted file mode 100644 index 31d17296c..000000000 --- a/src/main/js/modules/classroom/index.js +++ /dev/null @@ -1,258 +0,0 @@ -'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 deleted file mode 100644 index 6658a372a..000000000 --- a/src/main/js/modules/drone/arc.js +++ /dev/null @@ -1,267 +0,0 @@ -/*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 deleted file mode 100644 index 98442c601..000000000 --- a/src/main/js/modules/drone/bed.js +++ /dev/null @@ -1,55 +0,0 @@ -'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 deleted file mode 100644 index 0d9160e38..000000000 --- a/src/main/js/modules/drone/blocktype.js +++ /dev/null @@ -1,412 +0,0 @@ -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 deleted file mode 100644 index d6dbb645b..000000000 --- a/src/main/js/modules/drone/copypaste.js +++ /dev/null @@ -1,159 +0,0 @@ -'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 deleted file mode 100644 index cd5094ef2..000000000 --- a/src/main/js/modules/drone/cylinders.js +++ /dev/null @@ -1,76 +0,0 @@ -'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 deleted file mode 100644 index 54f522916..000000000 --- a/src/main/js/modules/drone/doors.js +++ /dev/null @@ -1,101 +0,0 @@ -/*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 deleted file mode 100644 index 764750a29..000000000 --- a/src/main/js/modules/drone/firework.js +++ /dev/null @@ -1,21 +0,0 @@ -'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 deleted file mode 100644 index 9baf2275f..000000000 --- a/src/main/js/modules/drone/garden.js +++ /dev/null @@ -1,49 +0,0 @@ -/*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 deleted file mode 100644 index b4d405c1a..000000000 --- a/src/main/js/modules/drone/index.js +++ /dev/null @@ -1,922 +0,0 @@ -'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 deleted file mode 100644 index 312648a57..000000000 --- a/src/main/js/modules/drone/ladder.js +++ /dev/null @@ -1,46 +0,0 @@ -'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 deleted file mode 100644 index 2d63d4a27..000000000 --- a/src/main/js/modules/drone/movement.js +++ /dev/null @@ -1,189 +0,0 @@ -'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 deleted file mode 100644 index 109651402..000000000 --- a/src/main/js/modules/drone/prism.js +++ /dev/null @@ -1,98 +0,0 @@ -'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 deleted file mode 100644 index 8a7ec1ab2..000000000 --- a/src/main/js/modules/drone/rand.js +++ /dev/null @@ -1,67 +0,0 @@ -'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 deleted file mode 100644 index 8d84a7f56..000000000 --- a/src/main/js/modules/drone/sign.js +++ /dev/null @@ -1,144 +0,0 @@ -'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 deleted file mode 100644 index c8056548b..000000000 --- a/src/main/js/modules/drone/sphere.js +++ /dev/null @@ -1,396 +0,0 @@ -'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 deleted file mode 100644 index 519ee1ee2..000000000 --- a/src/main/js/modules/drone/stairs.js +++ /dev/null @@ -1,61 +0,0 @@ -'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/js/modules/drone/test.js b/src/main/js/modules/drone/test.js deleted file mode 100644 index 5b870b854..000000000 --- a/src/main/js/modules/drone/test.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict'; -/*global module*/ -function testHorizontalStrokeWidth(){ - this.arc({ - blockType: 42, - meta: 0, - radius: 8, - orientation: 'horizontal', - strokeWidth: 3, - quadrants: {topright:true,topleft:true,bottomleft:true,bottomright:true} - }); -} -function testVerticalStrokeWidth(){ - this.arc({ - blockType: 42, - meta: 0, - radius: 8, - orientation: 'vertical', - 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 deleted file mode 100644 index 797c418e9..000000000 --- a/src/main/js/modules/drone/trees.js +++ /dev/null @@ -1,99 +0,0 @@ -'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 deleted file mode 100644 index 0b74a642b..000000000 --- a/src/main/js/modules/entities.js +++ /dev/null @@ -1,38 +0,0 @@ -'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/http/package.json b/src/main/js/modules/http/package.json deleted file mode 100644 index a143df2ae..000000000 --- a/src/main/js/modules/http/package.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "http", - "main": "./request.js" -} diff --git a/src/main/js/modules/http/request.js b/src/main/js/modules/http/request.js deleted file mode 100644 index adbe9cb6f..000000000 --- a/src/main/js/modules/http/request.js +++ /dev/null @@ -1,137 +0,0 @@ -/************************************************************************* -## 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/items.js b/src/main/js/modules/items.js deleted file mode 100644 index c5ae4cc0f..000000000 --- a/src/main/js/modules/items.js +++ /dev/null @@ -1,5 +0,0 @@ -if (__plugin.canary) { - module.exports = require('./canary/items'); -} else { - module.exports = require('./bukkit/items'); -} diff --git a/src/main/js/modules/minigames/scoreboard.js b/src/main/js/modules/minigames/scoreboard.js deleted file mode 100644 index 6fc34f436..000000000 --- a/src/main/js/modules/minigames/scoreboard.js +++ /dev/null @@ -1,70 +0,0 @@ -'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/signs/menu.js b/src/main/js/modules/signs/menu.js deleted file mode 100644 index 02a43c5ee..000000000 --- a/src/main/js/modules/signs/menu.js +++ /dev/null @@ -1,215 +0,0 @@ -'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 deleted file mode 100644 index 520d7aa8d..000000000 --- a/src/main/js/modules/signs/package.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - name: 'signs', - main: './signs.js' -} diff --git a/src/main/js/modules/sounds.js b/src/main/js/modules/sounds.js deleted file mode 100644 index 4065cc39e..000000000 --- a/src/main/js/modules/sounds.js +++ /dev/null @@ -1,32 +0,0 @@ -/************************************************************************* -## 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/utils/package.json b/src/main/js/modules/utils/package.json deleted file mode 100644 index d80809da0..000000000 --- a/src/main/js/modules/utils/package.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": 'utils', - "main": './utils.js' -} diff --git a/src/main/js/plugins/alias/alias.js b/src/main/js/plugins/alias/alias.js deleted file mode 100644 index 9b0998cf5..000000000 --- a/src/main/js/plugins/alias/alias.js +++ /dev/null @@ -1,253 +0,0 @@ -'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 deleted file mode 100644 index 212b1538a..000000000 --- a/src/main/js/plugins/arrows.js +++ /dev/null @@ -1,171 +0,0 @@ -'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 deleted file mode 100644 index f19422112..000000000 --- a/src/main/js/plugins/at.js +++ /dev/null @@ -1,4 +0,0 @@ -// 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 deleted file mode 100644 index 47f7d45ac..000000000 --- a/src/main/js/plugins/classroom.js +++ /dev/null @@ -1,11 +0,0 @@ -/*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 deleted file mode 100644 index cd9b987e9..000000000 --- a/src/main/js/plugins/commando/commando-test.js +++ /dev/null @@ -1,26 +0,0 @@ -/* - 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 deleted file mode 100644 index 331c99cdd..000000000 --- a/src/main/js/plugins/commando/commando.js +++ /dev/null @@ -1,122 +0,0 @@ -/************************************************************************* -## 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 deleted file mode 100644 index 3bb3f186e..000000000 --- a/src/main/js/plugins/drone/contrib/castle.js +++ /dev/null @@ -1,80 +0,0 @@ -'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 deleted file mode 100644 index 9088ef01c..000000000 --- a/src/main/js/plugins/drone/contrib/chessboard.js +++ /dev/null @@ -1,60 +0,0 @@ -'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 deleted file mode 100644 index b3a6405dc..000000000 --- a/src/main/js/plugins/drone/contrib/cottage.js +++ /dev/null @@ -1,160 +0,0 @@ -'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 deleted file mode 100644 index 10091d8c5..000000000 --- a/src/main/js/plugins/drone/contrib/dancefloor.js +++ /dev/null @@ -1,74 +0,0 @@ -'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 deleted file mode 100644 index 0e4134661..000000000 --- a/src/main/js/plugins/drone/contrib/fort.js +++ /dev/null @@ -1,126 +0,0 @@ -'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 deleted file mode 100644 index 9aacc665a..000000000 --- a/src/main/js/plugins/drone/contrib/hangtorch.js +++ /dev/null @@ -1,69 +0,0 @@ -'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 deleted file mode 100644 index 300b844e5..000000000 --- a/src/main/js/plugins/drone/contrib/lcd-clock.js +++ /dev/null @@ -1,105 +0,0 @@ -'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/js/plugins/drone/contrib/logo.js b/src/main/js/plugins/drone/contrib/logo.js deleted file mode 100644 index c633472fa..000000000 --- a/src/main/js/plugins/drone/contrib/logo.js +++ /dev/null @@ -1,242 +0,0 @@ -'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 = blocks.wool.gray; - // background defaults to gold blocks - if (typeof bg == "undefined") - bg = blocks.gold; - - // Draw the sqaure - 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) - - // Draw the S - // It's drawn in three strokes from bottom to top. Look for when - // it switches from .right() to .left() then back again - - // move to starting point for S - .right(22).down(6) - // stroke 1 - .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, 2, 8, 1) - .right(2) - .box(fg, 1, 9, 1) - .right().up() - .box(fg, 1, 9, 1) - .right() - .box(fg, 1, 10, 1) - .right() - .box(fg, 1, 22, 1) - .right().up() - .box(fg, 2, 20, 1) - .right().up() - .box(fg, 1, 18, 1) - .right().up() - .box(fg, 1, 17, 1) - .right().up() - .box(fg, 1, 15, 1) - .right().up() - .box(fg, 1, 13, 1) - .right().up(2) - .box(fg, 1, 9, 1) - .right().up(2) - .box(fg, 1, 5, 1) - // stroke 2 - .left(8).up(4) - .box(fg, 1, 9, 1) - .left().up() - .box(fg, 1, 9, 1) - .left().up() - .box(fg, 1, 8, 1) - .left(2).up() - .box(fg, 2, 8, 1) - .left(2).up() - .box(fg, 2, 7, 1) - .left(2).up() - .box(fg, 2, 7, 1) - .left() - .box(fg, 1, 8, 1) - .left().up() - .box(fg, 1, 8, 1) - .left() - .box(fg, 1, 9, 1) - .left(2).up() - .box(fg, 2, 19, 1) - .left().up() - .box(fg, 1, 17, 1) - .left() - .box(fg, 1, 16, 1) - .left().up() - .box(fg, 1, 14, 1) - .left().up(2) - .box(fg, 1, 10, 1) - .left().up(2) - .box(fg, 1, 6, 1) - // stroke 3 - .right(7).up(6) - .box(fg, 1, 8, 1) - .right().up() - .box(fg, 1, 7, 1) - .right().up() - .box(fg, 2, 7, 1) - .right(2).up() - .box(fg, 4, 6, 1) - .right(4).down() - .box(fg, 2, 7, 1) - .right().down() - .box(fg, 1, 8, 1) - .right() - .box(fg, 1, 7, 1) - .right().down() - .box(fg, 1, 8, 1) - .right().down() - .box(fg, 1, 9, 1) - .right().down() - .box(fg, 1, 9, 1) - .right().up() - .box(fg, 1, 8, 1) - .right().up() - .box(fg, 1, 6, 1) - .right().up() - .box(fg, 1, 5, 1) - .right().up() - .box(fg, 1, 3, 1) - .right().up() - .box(fg); - - this.move('logojs-start'); - - return this; -} -// -// Makes a cube of JS logos! -// This is a wrapper for logojs() so look at its docs -// -// Until the drone can rotate on its Z axis we can't -// use logojs() to create top/bottom sides of cube. -// -function logojscube(fg, bg) { - - this.chkpt('jscube-start') - .logojs(fg, bg); - - this.move('jscube-start') - .right(100) - .turn(3) - .logojs(fg, bg); - - this.move('jscube-start') - .right(100) - .turn(3) - .right(100) - .turn(3) - .logojs(fg, bg); - - this.move('jscube-start') - .right(100) - .turn(3) - .right(100) - .turn(3) - .right(100) - .turn(3) - .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 deleted file mode 100644 index 1ff4a1fbb..000000000 --- a/src/main/js/plugins/drone/contrib/mazegen.js +++ /dev/null @@ -1,146 +0,0 @@ -'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 deleted file mode 100644 index 25a64451e..000000000 --- a/src/main/js/plugins/drone/contrib/rainbow.js +++ /dev/null @@ -1,59 +0,0 @@ -'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/js/plugins/drone/contrib/redstonewire.js b/src/main/js/plugins/drone/contrib/redstonewire.js deleted file mode 100644 index fa74a622a..000000000 --- a/src/main/js/plugins/drone/contrib/redstonewire.js +++ /dev/null @@ -1,110 +0,0 @@ -'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) -// /js wireblock(blocks.sandstone); -// -// [2] to drop wire on to an existing block -// /js wire() -// -// [3] to place a (redstone) torch on a new block -// /js torchblock(blocks.sandstone) -// -// [4] to place a repeater on a new block -// /js repeaterblock(blocks.sandstone) -// -// [5] To create a long redstone wire (with necessary repeaters, powererd by a single torch) -// /js wirestraight(blocks.sandstone, distance) -// -// [6] To create a 'road' with redstone torches and wire lining each side -// /js redstoneroad(blocks.stone, blocks.sandstone, 25) - -Drone.extend('wireblock',function(blockType) -{ - this.chkpt('wireblock') - .box(blockType,1,2,1) // 2 blocks tall, top block will be wire dropped on lower - .up(); - - this.world.getBlockAt(this.x,this.y,this.z).setTypeId(55); //apply wire - - return this.move('wireblock'); -}); - -Drone.extend('wire',function () -{ - this.chkpt('wire') - .up(); - - this.world.getBlockAt(this.x,this.y,this.z).setTypeId(55); // apply wire - - return this.move('wire'); -}); - -Drone.extend('torchblock', function(blockType) -{ - this.box(blockType,1,2,1) // 2 blocks tall - .up(); - - this.world.getBlockAt(this.x,this.y,this.z).setTypeId(76); // apply torch - - return this.down(); -}); - -Drone.extend('repeaterblock',function(blockType) -{ - this.chkpt('repeaterblock') - .box(blockType,1,2,1) - .up(); - - var block = this.world.getBlockAt(this.x,this.y,this.z); - block.setTypeId(94); // apply repeater - - // redstone repeater dirs: north=0,east=1,south=2,west=3 - var direction = [1,2,3,0][this.dir]; // convert drone dir to repeater dir. - block.setData(direction); - - return this.move('repeaterblock'); -}); - - -Drone.extend('wirestraight',function (blockType,distance) -{ - this.chkpt('wirestraight'); - - this.torchblock(blockType); - this.fwd(); - - for (var i = 1; i < distance; i++) { - if(i % 14 == 0) - { - this.repeaterblock(blockType); - } - else - { - this.wireblock(blockType); - } - - this.fwd(); - }; - - return this.move('wirestraight'); -}); - - -Drone.extend('redstoneroad', function (roadBlockType, redstoneunderBlockType, distance) -{ - return this.down() - .wirestraight(redstoneunderBlockType, distance) - .right() - .box(roadBlockType, 4,1,distance) - .right(4) - .wirestraight(redstoneunderBlockType, distance) - .up(); -}); - - - diff --git a/src/main/js/plugins/drone/contrib/spawn.js b/src/main/js/plugins/drone/contrib/spawn.js deleted file mode 100644 index 24f86c071..000000000 --- a/src/main/js/plugins/drone/contrib/spawn.js +++ /dev/null @@ -1,8 +0,0 @@ -'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 deleted file mode 100644 index 4f9b35674..000000000 --- a/src/main/js/plugins/drone/contrib/spiral_stairs.js +++ /dev/null @@ -1,52 +0,0 @@ -'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 deleted file mode 100644 index d841b0fe9..000000000 --- a/src/main/js/plugins/drone/contrib/temple.js +++ /dev/null @@ -1,55 +0,0 @@ -'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 deleted file mode 100644 index 12c032dba..000000000 --- a/src/main/js/plugins/drone/drone.js +++ /dev/null @@ -1,4 +0,0 @@ -/*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 deleted file mode 100644 index 48446df32..000000000 --- a/src/main/js/plugins/entities.js +++ /dev/null @@ -1,8 +0,0 @@ -'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 deleted file mode 100644 index 330670414..000000000 --- a/src/main/js/plugins/examples/example-1-hello-module.js +++ /dev/null @@ -1,30 +0,0 @@ -/************************************************************************* -## 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 deleted file mode 100644 index 1ef6b2065..000000000 --- a/src/main/js/plugins/examples/example-2-hello-command.js +++ /dev/null @@ -1,32 +0,0 @@ -/************************************************************************* -## 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 deleted file mode 100644 index 5c7893a6d..000000000 --- a/src/main/js/plugins/examples/example-3-hello-ops-only.js +++ /dev/null @@ -1,42 +0,0 @@ -/************************************************************************* -## 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 deleted file mode 100644 index ba01adca0..000000000 --- a/src/main/js/plugins/examples/example-4-hello-parameters.js +++ /dev/null @@ -1,40 +0,0 @@ -/************************************************************************* -## 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 deleted file mode 100644 index aba9b6270..000000000 --- a/src/main/js/plugins/examples/example-5-hello-using-module.js +++ /dev/null @@ -1,41 +0,0 @@ -/************************************************************************* -## 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 deleted file mode 100644 index fd928549c..000000000 --- a/src/main/js/plugins/examples/example-6-hello-player.js +++ /dev/null @@ -1,58 +0,0 @@ -/************************************************************************* -## 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 deleted file mode 100644 index 72ba171af..000000000 --- a/src/main/js/plugins/examples/example-7-hello-events.js +++ /dev/null @@ -1,73 +0,0 @@ -'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 deleted file mode 100644 index f6af3eca8..000000000 --- a/src/main/js/plugins/homes/homes.js +++ /dev/null @@ -1,404 +0,0 @@ -/************************************************************************* -## 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 deleted file mode 100644 index 56770b0bd..000000000 --- a/src/main/js/plugins/minigames/NumberGuess.js +++ /dev/null @@ -1,88 +0,0 @@ -/************************************************************************* -## 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 deleted file mode 100644 index 9b6836ed1..000000000 --- a/src/main/js/plugins/minigames/cow-clicker.js +++ /dev/null @@ -1,204 +0,0 @@ -/************************************************************************* -## 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 deleted file mode 100644 index 4bb39badf..000000000 --- a/src/main/js/plugins/signs/examples.js +++ /dev/null @@ -1,51 +0,0 @@ -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 deleted file mode 100644 index 2ff425ae7..000000000 --- a/src/main/js/plugins/spawn.js +++ /dev/null @@ -1,42 +0,0 @@ -'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/resources/META-INF/truffle/language b/src/main/resources/META-INF/truffle/language new file mode 100644 index 000000000..11debdcb8 --- /dev/null +++ b/src/main/resources/META-INF/truffle/language @@ -0,0 +1,22 @@ +#https://github.com/graalvm/graaljs/issues/125 + +language1.characterMimeType.0=application/tregex +language1.className=com.oracle.truffle.regex.RegexLanguage +language1.id=regex +language1.implementationName= +language1.interactive=false +language1.internal=true +language1.name=REGEX + +language2.characterMimeType.0=application/javascript +language2.characterMimeType.1=application/javascript+module +language2.characterMimeType.2=text/javascript +language2.className=com.oracle.truffle.js.lang.JavaScriptLanguage +language2.defaultMimeType=application/javascript +language2.dependentLanguage.0=regex +language2.fileTypeDetector0=com.oracle.truffle.js.lang.JSFileTypeDetector +language2.id=javascript +language2.implementationName=GraalVM JavaScript +language2.interactive=true +language2.internal=false +language2.name=JavaScript \ No newline at end of file diff --git a/src/main/resources/boot.js b/src/main/resources/boot.js deleted file mode 100644 index d0f4d543b..000000000 --- a/src/main/resources/boot.js +++ /dev/null @@ -1,98 +0,0 @@ -/* - 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/plugin.yml b/src/main/resources/plugin.yml index a61cf09ec..4188b75a2 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,7 @@ name: scriptcraft main: org.scriptcraftjs.bukkit.ScriptCraftPlugin -version: [[version]] +version: 4.0.0.1 +description: ScriptCraft version 4.0.0-alpha-1 - Javascript In Minecraft!. commands: js: description: Evaluate javascript. diff --git a/src/main/resources/scriptcraft/bootstrap.js b/src/main/resources/scriptcraft/bootstrap.js new file mode 100644 index 000000000..ece3b1b7a --- /dev/null +++ b/src/main/resources/scriptcraft/bootstrap.js @@ -0,0 +1,125 @@ +/** + * ScriptCraft Modern Bootstrap + * Central entry point for all initialization logic. + */ + +(function(global) { + 'use strict'; + + // 1. Initialize core constants and Java bridges + const File = java.io.File; + const SCRoot = new File("./scriptcraft").getCanonicalPath(); + global.SCRoot = SCRoot; + + global.Bukkit = Packages.org.bukkit; + global.Server = global.Bukkit.Bukkit.getServer(); + global.CurrentPlugin = __plugin__; + + global.isJavaObject = function(o) { + if (o === global) return false; + if (o !== undefined && o !== null) { + try { + if (typeof o.constructor === 'function') return false; + } catch (e) { return true; } + try { + var result = o.getClass ? true : false; + if (result == true) return result; + } catch (e2) { } + if (o instanceof Packages.java.lang.Object) return true; + } + return o instanceof Packages.java.lang.Object; + }; + + // 2. Setup the Native Module System + // We strictly rely on GraalVM's native CommonJS and ESM support configured in Java. + + // 3. Initialize Modern Polyfills and Globals + global.echo = function(...args) { + var _self = false; + try { + _self = args[0].getClass().getSimpleName() === "CraftPlayer" + || args[0].getClass().getSimpleName() === "ColouredConsoleSender"; + } catch(e){ + _self = false; + } + var sender = _self ? args[0] : global.self; + var msg = _self ? args[1] : args[0]; + if (sender) { + sender.sendMessage(msg); + } else { + console.log(msg); // Fallback if no sender context + } + }; + global.alert = global.echo; + + global.console = require('console'); + global.process = require('process'); + global.fs = require('fs'); + global.fetch = require('fetch'); + global.hooks = require('hooks'); + global.EventEmitter = require('event-emitter'); + require('timers'); // Timers initialize themselves globally + + // 4. Initialize Core Systems + const cmdModule = require('command'); + global.command = cmdModule.command; + + // Event Multiplexer Setup + // Dynamically generate events-helper-bukkit.js based on the current server jar + var eventsHelper = new File(SCRoot, "lib/events-helper-bukkit.js"); + if (!eventsHelper.exists()) { + console.info('Generating dynamic events helper...'); + load("./scriptcraft/lib/generate-events-helper.js"); + } + + global.events = require('events-helper-bukkit'); + const evnts = require('events-bukkit'); + for (var func in evnts) { + global.events[func] = evnts[func]; + } + + // Modern Plugin System + global.registerPlugin = require('modern-plugin'); + + // 6. Handle Plugin Lifecycle + global.__onEnable = function() { + console.info('ScriptCraft Modern Core initialized.'); + + // Autoload legacy plugins + const pluginsDir = new File(SCRoot, 'plugins'); + if (pluginsDir.exists()) { + const pluginLoader = require('plugin'); + pluginLoader.autoload(global, pluginsDir); + } + + // Trigger the enable hook for modern plugins + global.hooks.trigger('enable'); + }; + + global.__onCommand = function(sender, cmd, label, args) { + const cmdName = ('' + cmd.getName()).toLowerCase(); + global.self = sender; + + try { + if (cmdName === 'js') { + const code = args.join(' '); + const result = eval(code); + if (result !== undefined && result !== null) { + global.echo(sender, result); + } + return true; + } + if (cmdName === 'jsp') { + cmdModule.exec(args, sender); + return true; + } + } catch (e) { + console.error('Command Error:', e); + global.echo(sender, 'Error: ' + e.message); + } finally { + delete global.self; + } + return false; + }; + +})(this); diff --git a/src/main/js/lib/.gitignore b/src/main/resources/scriptcraft/lib/.gitignore similarity index 50% rename from src/main/js/lib/.gitignore rename to src/main/resources/scriptcraft/lib/.gitignore index 455bdfa69..a3bb3b21d 100644 --- a/src/main/js/lib/.gitignore +++ b/src/main/resources/scriptcraft/lib/.gitignore @@ -1,2 +1 @@ /events-helper-bukkit.js -/events-helper-canary.js diff --git a/src/main/resources/scriptcraft/lib/command.js b/src/main/resources/scriptcraft/lib/command.js new file mode 100644 index 000000000..935b3f72d --- /dev/null +++ b/src/main/resources/scriptcraft/lib/command.js @@ -0,0 +1,73 @@ +'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. +*/ +/** + * Defines a new JavaScript plugin command (JSP). + * + * @param {string|function} name The name of the command, or the callback function if it has a name. + * @param {function} [func] The callback function to execute when the command is called. + * @param {string[]} [options] An array of options for the command (e.g., tab-completion candidates). + * @param {boolean} [intercepts] If true, this command will intercept all JSP calls. + * @returns {function} The callback function. + */ +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/resources/scriptcraft/lib/console.js b/src/main/resources/scriptcraft/lib/console.js new file mode 100644 index 000000000..c13c7d215 --- /dev/null +++ b/src/main/resources/scriptcraft/lib/console.js @@ -0,0 +1,47 @@ +'use strict'; + +/** + * A modern console implementation for ScriptCraft that integrates with + * Bukkit's logger and provides better object inspection. + */ + +var logger = __plugin__.getLogger(); + +function format(args) { + return Array.prototype.slice.call(args).map(function(arg) { + if (typeof arg === 'object' && arg !== null) { + try { + // Handle Java objects + if (arg instanceof java.lang.Object) { + return arg.toString(); + } + return JSON.stringify(arg, null, 2); + } catch (e) { + return '[Complex Object]'; + } + } + return String(arg); + }).join(' '); +} + +var console = { + log: function() { + logger.info(format(arguments)); + }, + info: function() { + logger.info(format(arguments)); + }, + warn: function() { + logger.warning(format(arguments)); + }, + error: function() { + logger.severe(format(arguments)); + }, + debug: function() { + if (global.config && global.config.verbose) { + logger.info('[DEBUG] ' + format(arguments)); + } + } +}; + +module.exports = console; diff --git a/src/main/resources/scriptcraft/lib/event-emitter.js b/src/main/resources/scriptcraft/lib/event-emitter.js new file mode 100644 index 000000000..4a4feef67 --- /dev/null +++ b/src/main/resources/scriptcraft/lib/event-emitter.js @@ -0,0 +1,50 @@ +'use strict'; + +/** + * A simple Node.js-style EventEmitter implementation. + */ + +function EventEmitter() { + this._events = {}; +} + +EventEmitter.prototype.on = function(type, listener) { + this._events[type] = this._events[type] || []; + this._events[type].push(listener); + return this; +}; + +EventEmitter.prototype.once = function(type, listener) { + var self = this; + function g() { + self.removeListener(type, g); + listener.apply(this, arguments); + } + g.listener = listener; + this.on(type, g); + return this; +}; + +EventEmitter.prototype.removeListener = function(type, listener) { + if (!this._events[type]) return this; + var list = this._events[type]; + for (var i = 0; i < list.length; i++) { + if (list[i] === listener || list[i].listener === listener) { + list.splice(i, 1); + break; + } + } + return this; +}; + +EventEmitter.prototype.emit = function(type) { + if (!this._events[type]) return false; + var handler = this._events[type]; + var args = Array.prototype.slice.call(arguments, 1); + for (var i = 0; i < handler.length; i++) { + handler[i].apply(this, args); + } + return true; +}; + +module.exports = EventEmitter; diff --git a/src/main/resources/scriptcraft/lib/events-bukkit.js b/src/main/resources/scriptcraft/lib/events-bukkit.js new file mode 100644 index 000000000..eac6b2011 --- /dev/null +++ b/src/main/resources/scriptcraft/lib/events-bukkit.js @@ -0,0 +1,125 @@ +/*global Java, exports, org, __plugin__ */ + +var bkEventPriority = org.bukkit.event.EventPriority; +var bkHandlerList = org.bukkit.event.HandlerList; +var bkPluginManager = org.bukkit.Bukkit.getPluginManager(); +var ScriptCraftListener = Java.extend(org.bukkit.event.Listener, {}); + +// Central registry for multiplexing event listeners +// Key: EventClassName + Priority +var _dispatchers = {}; + +/** + * Registers an event listener with the Bukkit plugin manager. + * + * @param {any} eventType The Java class of the event to listen for. + * @param {function} handler The function to call when the event fires. + * @param {string} [priority] The priority of the event. Defaults to HIGHEST. + * @returns {{ unregister: function(): void }} + */ +exports.on = function(eventType, handler, priority) { + if (typeof priority == 'undefined') { + priority = 'HIGHEST'; + } else { + priority = priority.toUpperCase().trim(); + } + + var eventClassName = eventType.class.name; + var dispatcherKey = eventClassName + ':' + priority; + + if (!_dispatchers[dispatcherKey]) { + var callbacks = []; + var listener = new ScriptCraftListener(); + + var eventExecutor = function(l, evt) { + var callbacksToRun = callbacks.slice(); // Copy to avoid issues if unregistered during loop + for (var i = 0; i < callbacksToRun.length; i++) { + var cbObj = callbacksToRun[i]; + var cancel = function() { + if (evt instanceof org.bukkit.event.Cancellable) { + evt.setCancelled(true); + } + }; + + var bound = { + unregister: cbObj.unregister, + cancel: cancel + }; + + try { + cbObj.handler.call(bound, evt, cancel); + } catch (e) { + console.error('Error in event ' + eventClassName + ': ' + e); + } + } + }; + + bkPluginManager.registerEvent( + eventType.class, + listener, + bkEventPriority[priority], + eventExecutor, + __plugin__ + ); + + _dispatchers[dispatcherKey] = { + listener: listener, + callbacks: callbacks + }; + } + + var dispatcher = _dispatchers[dispatcherKey]; + var cbRecord = { handler: handler }; + + cbRecord.unregister = function() { + var idx = dispatcher.callbacks.indexOf(cbRecord); + if (idx > -1) { + dispatcher.callbacks.splice(idx, 1); + } + if (dispatcher.callbacks.length === 0) { + bkHandlerList.unregisterAll(dispatcher.listener); + delete _dispatchers[dispatcherKey]; + } + }; + + dispatcher.callbacks.push(cbRecord); + + return { unregister: cbRecord.unregister }; +}; + +/** + * Alias for on() to match browser standards. + */ +exports.addEventListener = exports.on; + +/** + * Alias for on() with automatic unregistration after first fire. + */ +exports.once = function(eventType, handler, priority) { + var reg = exports.on(eventType, function(evt, cancel) { + this.unregister(); + handler.call(this, evt, cancel); + }, priority); + return reg; +}; + +/** + * Alias for removeEventListener/off - note that this requires the exact same function reference. + */ +exports.off = function(eventType, handler, priority) { + // This is tricky with the multiplexer since we store cbRecords. + // We'd need to search for the handler. + priority = (priority || 'HIGHEST').toUpperCase().trim(); + var dispatcherKey = eventType.class.name + ':' + priority; + var dispatcher = _dispatchers[dispatcherKey]; + if (dispatcher) { + for (var i = 0; i < dispatcher.callbacks.length; i++) { + if (dispatcher.callbacks[i].handler === handler) { + dispatcher.callbacks[i].unregister(); + break; + } + } + } +}; + +exports.removeEventListener = exports.off; diff --git a/src/main/js/lib/events.js b/src/main/resources/scriptcraft/lib/events.js similarity index 79% rename from src/main/js/lib/events.js rename to src/main/resources/scriptcraft/lib/events.js index f66fd1150..884df7966 100644 --- a/src/main/js/lib/events.js +++ b/src/main/resources/scriptcraft/lib/events.js @@ -10,11 +10,11 @@ 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. +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 ) { +events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt, cancel ) { echo(evt.player, evt.player.name + ' broke a block!'); } ); ``` @@ -22,7 +22,7 @@ events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt, c or you can (and probably should) use the more succinct: ```javascript -events.blockDestroy( function( evt, cancel ) { +events.blockDestroy( function( evt, cancel ) { echo(evt.player, evt.player.name + ' broke a block!'); } ); ``` @@ -33,41 +33,41 @@ events and can also be used to register non-standard events - that is #### Parameters - * eventType - A Java class. See the [CanaryMod Hook API][cmEvtApi] or [Bukkit Event API][buk] for details of the many event types. + * 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: - + 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. + * 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]. + 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. +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 ) { +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 ) { +```javascript +events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt, cancel ) { echo( evt.player, evt.player.name + ' broke a block!'); this.unregister(); } ); @@ -82,7 +82,7 @@ method is only available from within the event handling function. To unregister a listener *outside* of the listener function... -```javascript +```javascript var myBlockBreakListener = events.on( Packages.net.canarymod.hook.player.BlockDestroyHook, function( evt ) { ... } ); ... myBlockBreakListener.unregister(); @@ -95,18 +95,9 @@ myBlockBreakListener.unregister(); ***/ 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 ) { +/*global __plugin__, module, require*/ +module.exports = require('./lib/events-bukkit.js'); +helper = require('./lib/events-helper-bukkit.js'); +for (var func in helper) { module.exports[func] = helper[func]; -}; +} diff --git a/src/main/resources/scriptcraft/lib/fetch.js b/src/main/resources/scriptcraft/lib/fetch.js new file mode 100644 index 000000000..0d06bc5d7 --- /dev/null +++ b/src/main/resources/scriptcraft/lib/fetch.js @@ -0,0 +1,87 @@ +'use strict'; + +/** + * A minimal, native-feeling fetch implementation for ScriptCraft + * using Java 11's HttpClient. + */ + +var HttpClient = java.net.http.HttpClient; +var HttpRequest = java.net.http.HttpRequest; +var HttpResponse = java.net.http.HttpResponse; +var URI = java.net.URI; +var Duration = java.time.Duration; + +var client = HttpClient.newBuilder() + .connectTimeout(Duration.ofSeconds(10)) + .build(); + +/** + * Implementation of the global fetch() function. + * + * @param {string} url The URL to fetch. + * @param {object} [options] Fetch options (method, headers, body). + * @returns {Promise} A promise that resolves to a Response object. + */ +function fetch(url, options) { + options = options || {}; + var method = options.method || 'GET'; + var headers = options.headers || {}; + var body = options.body; + + var requestBuilder = HttpRequest.newBuilder() + .uri(URI.create(url)) + .timeout(Duration.ofSeconds(30)); + + // Set Method and Body + if (method === 'GET') { + requestBuilder.GET(); + } else if (method === 'POST') { + requestBuilder.POST(HttpRequest.BodyPublishers.ofString(body || '')); + } else if (method === 'PUT') { + requestBuilder.PUT(HttpRequest.BodyPublishers.ofString(body || '')); + } else if (method === 'DELETE') { + requestBuilder.DELETE(); + } else { + requestBuilder.method(method, HttpRequest.BodyPublishers.ofString(body || '')); + } + + // Set Headers + for (var header in headers) { + requestBuilder.header(header, headers[header]); + } + + var request = requestBuilder.build(); + + // In ScriptCraft/GraalVM, we can use CompletableFuture and wrap it in a JS Promise + return new Promise(function(resolve, reject) { + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) + .thenAccept(function(response) { + resolve({ + ok: response.statusCode() >= 200 && response.statusCode() < 300, + status: response.statusCode(), + statusText: response.statusCode().toString(), + headers: { + get: function(name) { + return response.headers().firstValue(name).orElse(null); + } + }, + text: function() { + return Promise.resolve(response.body()); + }, + json: function() { + try { + return Promise.resolve(JSON.parse(response.body())); + } catch (e) { + return Promise.reject(e); + } + } + }); + }) + .exceptionally(function(ex) { + reject(ex); + return null; + }); + }); +} + +module.exports = fetch; diff --git a/src/main/resources/scriptcraft/lib/fs.js b/src/main/resources/scriptcraft/lib/fs.js new file mode 100644 index 000000000..9cf396130 --- /dev/null +++ b/src/main/resources/scriptcraft/lib/fs.js @@ -0,0 +1,138 @@ +'use strict'; + +/** + * A Node.js-compatible 'fs' module for ScriptCraft using java.nio.file. + */ + +var Files = java.nio.file.Files; +var Paths = java.nio.file.Paths; +var StandardOpenOption = java.nio.file.StandardOpenOption; +var Charset = java.nio.charset.Charset; + +var UTF8 = Charset.forName('UTF-8'); + +function toPath(p) { + return Paths.get(p); +} + +var fs = { + readFileSync: function(path, options) { + var bytes = Files.readAllBytes(toPath(path)); + var encoding = (typeof options === 'string' ? options : (options && options.encoding)); + if (encoding) { + return new java.lang.String(bytes, Charset.forName(encoding)).toString(); + } + return bytes; // Returns a byte array if no encoding specified + }, + + writeFileSync: function(path, data, options) { + var bytes = (typeof data === 'string') ? new java.lang.String(data).getBytes(UTF8) : data; + Files.write(toPath(path), bytes, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); + }, + + appendFileSync: function(path, data) { + var bytes = (typeof data === 'string') ? new java.lang.String(data).getBytes(UTF8) : data; + Files.write(toPath(path), bytes, StandardOpenOption.CREATE, StandardOpenOption.APPEND); + }, + + existsSync: function(path) { + return Files.exists(toPath(path)); + }, + + mkdirSync: function(path, options) { + var recursive = (options && options.recursive); + if (recursive) { + Files.createDirectories(toPath(path)); + } else { + Files.createDirectory(toPath(path)); + } + }, + + readdirSync: function(path) { + var stream = Files.newDirectoryStream(toPath(path)); + var files = []; + var iterator = stream.iterator(); + while (iterator.hasNext()) { + files.push(iterator.next().getFileName().toString()); + } + stream.close(); + return files; + }, + + unlinkSync: function(path) { + Files.delete(toPath(path)); + }, + + statSync: function(path) { + var p = toPath(path); + var attrs = Files.readAttributes(p, "basic:*"); + return { + isFile: function() { return !Files.isDirectory(p); }, + isDirectory: function() { return Files.isDirectory(p); }, + size: attrs.get("size"), + mtime: new Date(attrs.get("lastModifiedTime").toMillis()) + }; + } +}; + +fs.promises = { + readFile: function(path, options) { + return new Promise(function(resolve, reject) { + try { + resolve(fs.readFileSync(path, options)); + } catch (e) { + reject(e); + } + }); + }, + writeFile: function(path, data, options) { + return new Promise(function(resolve, reject) { + try { + fs.writeFileSync(path, data, options); + resolve(); + } catch (e) { + reject(e); + } + }); + }, + readdir: function(path) { + return new Promise(function(resolve, reject) { + try { + resolve(fs.readdirSync(path)); + } catch (e) { + reject(e); + } + }); + }, + mkdir: function(path, options) { + return new Promise(function(resolve, reject) { + try { + fs.mkdirSync(path, options); + resolve(); + } catch (e) { + reject(e); + } + }); + }, + unlink: function(path) { + return new Promise(function(resolve, reject) { + try { + fs.unlinkSync(path); + resolve(); + } catch (e) { + reject(e); + } + }); + }, + stat: function(path) { + return new Promise(function(resolve, reject) { + try { + resolve(fs.statSync(path)); + } catch (e) { + reject(e); + } + }); + } +}; + +module.exports = fs; diff --git a/src/main/resources/scriptcraft/lib/generate-events-helper.js b/src/main/resources/scriptcraft/lib/generate-events-helper.js new file mode 100644 index 000000000..e8707639b --- /dev/null +++ b/src/main/resources/scriptcraft/lib/generate-events-helper.js @@ -0,0 +1,122 @@ + + +// [0] = type, [1] = lib.jar [2] = blockX, [3] = classX +var File = java.io.File, + FileReader = java.io.FileReader, + FileInputStream = java.io.FileInputStream, + Bukkit = org.bukkit.event, + FileUtils = org.apache.commons.io.FileUtils, + FRAMEWORK = "Spigot", + out = java.lang.System.out, + err = java.lang.System.err, + System = java.lang.System, + Modifier = java.lang.reflect.Modifier, + clz, + ZipInputStream = java.util.zip.ZipInputStream, + entry = null; + + +var jars = System.getProperty("java.class.path").split(";"); + + +var bukkit = "./spigot.jar"; +for(var j in jars){ + var m = jars[j].replace(/.+(\\|\/)([^\/\\]+)/, "$2") + var shrtname = m.replace(/.*(spigot).*\.jar\S*/i, "$1") + ""; + if (shrtname.toLowerCase() === "spigot"){ + bukkit = jars[j]; + } +} +print("# " + bukkit); + +var zis = new ZipInputStream(new FileInputStream(bukkit)); + +let final = ` +/********************* + +## Events Helper Module (${FRAMEWORK} 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. + +***/ +`; + + +var names = []; +while ((entry = zis.getNextEntry()) != null) { + names.push(String(entry.getName())); +} + +names.sort(); +names.forEach(function(name) { + var re1 = /org\/bukkit\/event\/.+Event\.class$/; + if (re1.test(name)) { + name = name.replace(new RegExp('\/', '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) { + return; + } + var parts = name.split('.'); + var shortName = null; + shortName = name.replace('org.bukkit.event.', ''); + var fname = parts + .reverse() + .shift() + .replace(new RegExp('^(.)'), function(a) { + return a.toLowerCase(); + }); + fname = fname.replace(new RegExp('Hook$'), ''); + + var javaDoc = '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++) { + final += comment[i] + "\n" + } + + var fungen = 'function ' + fname + '(callback,priority){ \n' + + ' return events.on(' + name + ',callback,priority);\n' + + '};\n' + + 'module.exports.' + fname + ' = ' + fname + ';\n' + + 'module.exports.' + fname.replace(/Event\s*$/, "") + ' = ' + fname + ';\n'; + final += fungen; + } +}); + +FileUtils.writeStringToFile(new File("./lib/events-helper-bukkit.js"), final); diff --git a/src/main/resources/scriptcraft/lib/hooks.js b/src/main/resources/scriptcraft/lib/hooks.js new file mode 100644 index 000000000..d48d0ed29 --- /dev/null +++ b/src/main/resources/scriptcraft/lib/hooks.js @@ -0,0 +1,44 @@ +'use strict'; + +/** + * Hook system for ScriptCraft. + * Allows plugins to register and trigger custom hooks. + */ + +var _hooks = {}; + +/** + * Registers a callback for a specific hook. + * + * @param {string} hookName The name of the hook. + * @param {function} callback The function to call when the hook is triggered. + */ +function addHook(hookName, callback) { + if (!_hooks[hookName]) { + _hooks[hookName] = []; + } + _hooks[hookName].push(callback); +} + +/** + * Triggers a hook, calling all registered callbacks. + * + * @param {string} hookName The name of the hook to trigger. + * @param {...any} args Arguments to pass to the callbacks. + */ +function triggerHook(hookName) { + var args = Array.prototype.slice.call(arguments, 1); + var callbacks = _hooks[hookName]; + if (callbacks) { + for (var i = 0; i < callbacks.length; i++) { + try { + callbacks[i].apply(null, args); + } catch (e) { + console.error('Error in hook ' + hookName + ': ' + e); + } + } + } +} + +exports.add = addHook; +exports.trigger = triggerHook; diff --git a/src/main/resources/scriptcraft/lib/modern-plugin.js b/src/main/resources/scriptcraft/lib/modern-plugin.js new file mode 100644 index 000000000..a36af909c --- /dev/null +++ b/src/main/resources/scriptcraft/lib/modern-plugin.js @@ -0,0 +1,111 @@ +'use strict'; + +/** + * Modern Plugin API for ScriptCraft with JSON support. + */ + +var hooks = require('./hooks.js'); +var command = require('./command.js').command; +var events = require('./events.js'); +var nativeCmd = require('./native-command.js'); +var persist = require('./persistence.js'); + +function createPlugin(name, definition) { + // If name is an object, it might be the definition with name in metadata + if (typeof name === 'object') { + definition = name; + name = definition.name || (global.__plugin_metadata && global.__plugin_metadata.name); + } + + var metadata = global.__plugin_metadata || definition.metadata || {}; + + var pluginObj = { + name: name, + metadata: metadata, + definition: definition, + store: {}, + config: metadata.config || definition.config || {}, + storage: { + get: function(key) { return pluginObj.store[key]; }, + set: function(key, val) { pluginObj.store[key] = val; }, + all: function() { return pluginObj.store; } + } + }; + + // Load persistent store + // Use the persistence module if available + if (global.persist) { + pluginObj.store = global.persist(name, pluginObj.store); + } + + // Handle Enable + if (definition.onEnable) { + hooks.add('enable', function() { + definition.onEnable.call(pluginObj); + }); + } + + // Handle Commands from Definition + if (definition.commands) { + var metaCommands = metadata.commands || {}; + + for (var cmdName in definition.commands) { + var cmdDef = definition.commands[cmdName]; + var metaDef = metaCommands[cmdName]; + var callback = (typeof cmdDef === 'function') ? cmdDef : cmdDef.callback; + + if (callback) { + var boundCallback = callback.bind(pluginObj); + + // Always register under /jsp as a safe fallback + command(cmdName, boundCallback); + + // If it's declared in plugin.json, treat it as a manifest for a top-level native command + if (metaDef !== undefined) { + var aliases = cmdDef.aliases || metaDef.aliases || []; + var description = cmdDef.description || metaDef.description || ''; + var tabComplete = cmdDef.tabComplete || metaDef.tabComplete; + + nativeCmd.register(cmdName, aliases, description, function(sender, label, args) { + boundCallback(args, sender); + return true; + }, function(sender, alias, args) { + if (typeof tabComplete === 'function') { + return tabComplete.call(pluginObj, args, sender); + } else if (Array.isArray(tabComplete)) { + var lastArg = args.length > 0 ? args[args.length - 1].toLowerCase() : ''; + return tabComplete.filter(function(cand) { + return String(cand).toLowerCase().indexOf(lastArg) === 0; + }); + } + return []; + }); + } + } + } + } + + // Handle Events + if (definition.events) { + for (var eventName in definition.events) { + if (events[eventName]) { + events[eventName](definition.events[eventName].bind(pluginObj)); + } else { + console.warn('Unknown event: ' + eventName + ' in plugin ' + name); + } + } + } + + // Handle Disable + if (definition.onDisable) { + if (global.addUnloadHandler) { + global.addUnloadHandler(function() { + definition.onDisable.call(pluginObj); + }); + } + } + + return pluginObj; +} + +module.exports = createPlugin; diff --git a/src/main/resources/scriptcraft/lib/native-command.js b/src/main/resources/scriptcraft/lib/native-command.js new file mode 100644 index 000000000..78a1c1f8e --- /dev/null +++ b/src/main/resources/scriptcraft/lib/native-command.js @@ -0,0 +1,74 @@ +'use strict'; + +/** + * Utility for registering top-level Bukkit commands at runtime. + * Uses reflection to access Bukkit's internal CommandMap. + */ + +var Bukkit = org.bukkit.Bukkit; +var CommandMap = null; + +try { + var server = Bukkit.getServer(); + var getCommandMap = server.getClass().getDeclaredMethod('getCommandMap'); + getCommandMap.setAccessible(true); + CommandMap = getCommandMap.invoke(server); +} catch (e) { + console.warn('Failed to access Bukkit CommandMap. Top-level aliases will not be available: ' + e); +} + +/** + * Registers a new top-level command. + * + * @param {string} name The primary command name. + * @param {string[]} aliases Array of alternative names. + * @param {string} description Command description. + * @param {function} callback The function to execute. + * @param {function} [tabCompleteCallback] The function to handle tab completion. + */ +function register(name, aliases, description, callback, tabCompleteCallback) { + if (!CommandMap) return; + + var ScriptCraftCommand = Java.extend(org.bukkit.command.defaults.BukkitCommand, { + execute: function(sender, label, args) { + try { + return callback(sender, label, Array.prototype.slice.call(args)); + } catch (e) { + console.error('Error executing native command ' + name + ': ' + e); + return false; + } + }, + tabComplete: function(sender, alias, args) { + var javaList = new java.util.ArrayList(); + try { + if (typeof tabCompleteCallback === 'function') { + var result = tabCompleteCallback(sender, alias, Array.prototype.slice.call(args)); + if (result && result.length) { + for (var i = 0; i < result.length; i++) { + javaList.add(String(result[i])); + } + } + } + } catch (e) { + console.error('Error in tabComplete for native command ' + name + ': ' + e); + } + return javaList; + } + }); + + var cmd = new ScriptCraftCommand(name); + cmd.setDescription(description || ''); + if (aliases && aliases.length > 0) { + var aliasList = new java.util.ArrayList(); + for (var i = 0; i < aliases.length; i++) { + aliasList.add(aliases[i]); + } + cmd.setAliases(aliasList); + } + + CommandMap.register('scriptcraft', cmd); +} + +module.exports = { + register: register +}; diff --git a/src/main/resources/scriptcraft/lib/persistence.js b/src/main/resources/scriptcraft/lib/persistence.js new file mode 100644 index 000000000..91c36a3e6 --- /dev/null +++ b/src/main/resources/scriptcraft/lib/persistence.js @@ -0,0 +1,50 @@ +var _dataDir = null, + _persistentData = {}; + +module.exports = function(rootDir, $) { + var _load = function(name) { + var result = $.scloadJSON( + _dataDir.getCanonicalPath() + '/' + name + '-store.json' + ); + return result; + }; + + var _save = function(name, objToSave) { + $.scsave(objToSave, _dataDir.getCanonicalPath() + '/' + 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/resources/scriptcraft/lib/plugin.js b/src/main/resources/scriptcraft/lib/plugin.js new file mode 100644 index 000000000..639ec268a --- /dev/null +++ b/src/main/resources/scriptcraft/lib/plugin.js @@ -0,0 +1,84 @@ +'use strict'; + +/** + * Enhanced Plugin Loader for ScriptCraft. + * Supports legacy .js files and modern JSON-configured plugins. + */ + +var _plugins = {}; +var File = java.io.File; + +function _plugin(moduleName, moduleObject, isPersistent) { + 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 && global.persist) { + moduleObject.store = global.persist(moduleName, moduleObject.store); + } + return moduleObject; +} + +function _autoload(context, pluginDir) { + if (!pluginDir.exists()) return; + + var items = pluginDir.listFiles(); + for (var i = 0; i < items.length; i++) { + var item = items[i]; + + if (item.isDirectory()) { + // Check for modern plugin structure (plugin.json) + var jsonFile = new File(item, 'plugin.json'); + if (jsonFile.exists()) { + _loadModernPlugin(item, jsonFile); + } else { + // If it's a directory but no plugin.json, we don't load it as a plugin directly, + // but we could recursively search if desired. For strictness, we'll only load + // top-level plugin directories that contain plugin.json. + if (global.config && global.config.verbose) { + console.warn('Ignoring directory without plugin.json: ' + item.name); + } + } + } else if (item.name.match(/\.js$/)) { + if (global.config && global.config.verbose) { + console.warn('Ignoring legacy standalone script (use plugin.json instead): ' + item.name); + } + } + } +} + +function _loadModernPlugin(dir, jsonFile) { + try { + var jsonStr = require('fs').readFileSync(jsonFile.getAbsolutePath(), 'UTF-8'); + var metadata = JSON.parse(jsonStr); + var mainFile = new File(dir, metadata.main || 'index.js'); + + if (!mainFile.exists()) { + console.warn('Main file ' + mainFile.name + ' not found for plugin ' + metadata.name); + return; + } + + // Require the main file - it should call registerPlugin() + // We pass the metadata to the context so registerPlugin can access it + global.__plugin_metadata = metadata; + require(mainFile.getAbsolutePath()); + delete global.__plugin_metadata; + + if (global.config && global.config.verbose) { + console.info('Loaded plugin: ' + (metadata.name || dir.name)); + } + } catch (e) { + console.error('Error loading modern plugin from ' + dir.name + ': ' + e); + } +} + +module.exports = { + plugin: _plugin, + autoload: _autoload +}; diff --git a/src/main/resources/scriptcraft/lib/process.js b/src/main/resources/scriptcraft/lib/process.js new file mode 100644 index 000000000..374320591 --- /dev/null +++ b/src/main/resources/scriptcraft/lib/process.js @@ -0,0 +1,30 @@ +'use strict'; + +/** + * A minimal 'process' global for ScriptCraft. + */ + +var System = java.lang.System; + +var process = { + env: {}, + platform: System.getProperty('os.name').toLowerCase().indexOf('win') >= 0 ? 'win32' : 'linux', + version: 'v20.0.0', // Faked version for compatibility + arch: System.getProperty('os.arch'), + cwd: function() { + return new java.io.File('.').getCanonicalPath(); + }, + nextTick: function(callback) { + server.getScheduler().runTask(__plugin__, callback); + } +}; + +// Populate env +var env = System.getenv(); +var keys = env.keySet().iterator(); +while (keys.hasNext()) { + var key = keys.next(); + process.env[key] = env.get(key); +} + +module.exports = process; diff --git a/src/main/resources/scriptcraft/lib/readme.md b/src/main/resources/scriptcraft/lib/readme.md new file mode 100644 index 000000000..45b55f72a --- /dev/null +++ b/src/main/resources/scriptcraft/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) + * events.js - Event handling module for use by plugin/module developers. + * hooks.js - Custom hook system for plugin lifecycle and inter-plugin communication. + * modern-plugin.js - Modern API for registering plugins (`registerPlugin`). + +When `require('modulename')` is called, GraalVM handles module resolution natively. + +[njsmod]: http://nodejs.org/api/modules.html diff --git a/src/main/resources/scriptcraft/lib/timers.js b/src/main/resources/scriptcraft/lib/timers.js new file mode 100644 index 000000000..54920c314 --- /dev/null +++ b/src/main/resources/scriptcraft/lib/timers.js @@ -0,0 +1,50 @@ +'use strict'; + +/** + * Modernized Timers for ScriptCraft using Bukkit's Scheduler. + * Converts millisecond delays to Minecraft ticks (approx. 50ms per tick). + */ + +(function() { + const Bukkit = org.bukkit.Bukkit; + const Server = Bukkit.getServer(); + const Runnable = Java.type('java.lang.Runnable'); + + function toTicks(ms) { + return Math.max(1, Math.ceil(ms / 50)); + } + + global.setTimeout = function(callback, delay, ...args) { + const task = Server.getScheduler().runTaskLater(__plugin__, new Runnable({ + run: function() { + callback(...args); + } + }), toTicks(delay || 0)); + return task; + }; + + global.clearTimeout = function(task) { + if (task && task.cancel) { + task.cancel(); + } + }; + + global.setInterval = function(callback, interval, ...args) { + const ticks = toTicks(interval || 0); + const task = Server.getScheduler().runTaskTimer(__plugin__, new Runnable({ + run: function() { + callback(...args); + } + }), ticks, ticks); + return task; + }; + + global.clearInterval = global.clearTimeout; + + // nextTick is already in process.js, but let's ensure consistency + if (!global.setImmediate) { + global.setImmediate = function(callback, ...args) { + return global.setTimeout(callback, 0, ...args); + }; + } +})(); diff --git a/src/main/resources/scriptcraft/modules/at.js b/src/main/resources/scriptcraft/modules/at.js new file mode 100644 index 000000000..3f2ea3e7b --- /dev/null +++ b/src/main/resources/scriptcraft/modules/at.js @@ -0,0 +1,148 @@ +'use strict'; +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); +} + +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/resources/scriptcraft/modules/block-colors.js similarity index 100% rename from src/main/js/modules/block-colors.js rename to src/main/resources/scriptcraft/modules/block-colors.js diff --git a/src/main/resources/scriptcraft/modules/blockhelper.js b/src/main/resources/scriptcraft/modules/blockhelper.js new file mode 100644 index 000000000..b33d72ac7 --- /dev/null +++ b/src/main/resources/scriptcraft/modules/blockhelper.js @@ -0,0 +1,165 @@ +'use strict'; +/*global module, exports, require, Packages, __plugin__, server*/ +var blocks = require('blocks'), + bountiful = false; + +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: + case blocks.furnace: + case blocks.furnace_burning: + case blocks.chest: + case blocks.enderchest: + case blocks.dispenser: + // bug: furnace, chest, dispenser don't always use the right metadata + 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/resources/scriptcraft/modules/blocks.js similarity index 85% rename from src/main/js/modules/blocks.js rename to src/main/resources/scriptcraft/modules/blocks.js index 920c69a7c..8b2f13bdc 100644 --- a/src/main/js/modules/blocks.js +++ b/src/main/resources/scriptcraft/modules/blocks.js @@ -1,35 +1,49 @@ /************************************************************************ ## 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, + granite: '1:1', + polished_granite: '1:2', + diorite: '1:3', + polished_diorite: '1:4', + andesite: '1:5', + polished_andesite: '1:6', grass: 2, dirt: 3, + coarse_dirt: '3:1', + podzol: '3:2', 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' }, + 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, @@ -41,7 +55,13 @@ var blocks = { iron_ore: 15, coal_ore: 16, wood: 17, + spruce_wood: '17:1', + birch_wood: '17:2', + jungle_wood: '17:3', leaves: 18, + spruce_leaves: '18:1', + birch_leaves: '18:2', + jungle_leaves: '18:3', sponge: 19, sponge_wet: '19:1', glass: 20, @@ -63,11 +83,14 @@ var blocks = { grass_tall: 31, dead_bush: 32, piston: 33, - piston_extn: 34, piston_head: 34, - wool: { white: 35 /* All other colors added below */ }, + 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, + dandelion: 37, + flower_yellow: 37, + rose: 38, + flower_red: 38, mushroom_brown: 39, mushroom_red: 40, gold: 41, @@ -90,12 +113,12 @@ var blocks = { cobblestone: '43:3', brick: '43:4', stonebrick: '43:5', - netherbrick:'43:6', + netherbrick: '43:6', quartz: '43:7', smooth_stone: '43:8', smooth_sandstone: '43:9', tile_quartz: '43:15', - purpur: 204, + purpur: 204 }, slab: { stone: '44:0', @@ -104,7 +127,7 @@ var blocks = { cobblestone: '44:3', brick: '44:4', stonebrick: '44:5', - netherbrick:'44:6', + netherbrick: '44:6', quartz: '44:7', upper: { stone: '44:8', @@ -114,7 +137,7 @@ var blocks = { cobblestone: '44:11', brick: '44:12', stonebrick: '44:13', - netherbrick:'44:14', + netherbrick: '44:14', quartz: '44:15', oak: '126:8', spruce: '126:9', @@ -124,7 +147,6 @@ var blocks = { dark_oak: '126:13' }, snow: 78, - stone: 44, oak: 126, spruce: '126:1', birch: '126:2', @@ -132,7 +154,7 @@ var blocks = { acacia: '126:4', dark_oak: '126:5', sandstone_red: 182, - purpur: 205, + purpur: 205 }, // see brick.red 45 tnt: 46, @@ -156,7 +178,7 @@ var blocks = { acacia: 163, dark_oak: 164, sandstone_red: 180, - purpur: 203, + purpur: 203 }, chest: 54, redstone_wire: 55, @@ -187,7 +209,6 @@ var blocks = { clay: 82, sugar_cane: 83, jukebox: 84, - fence: 85, pumpkin: 86, netherrack: 87, soulsand: 88, @@ -197,7 +218,7 @@ var blocks = { cake: 92, redstone_repeater: 93, redstone_repeater_active: 94, - stained_glass: { + stained_glass: { white: 95 // all other colors added below }, trapdoor: 96, @@ -221,7 +242,6 @@ var blocks = { mycelium: 110, lily_pad: 111, nether: 112, - nether_fence: 113, netherwart: 115, table_enchantment: 116, brewing_stand: 117, @@ -246,7 +266,8 @@ var blocks = { potatoes: 142, beetroot: 207, button_wood: 143, - skull: 144, mobhead: 144, + skull: 144, + mobhead: 144, anvil: 145, chest_trapped: 146, pressure_plate_weighted_light: 147, @@ -255,10 +276,11 @@ var blocks = { redstone_comparator_active: 150, daylight_sensor: 151, redstone: 152, - quartzore: 153, netherquartzore: 153, + quartzore: 153, + netherquartzore: 153, hopper: 154, quartz: 155, - quartz_chiseled: '155:1', + quartz_chiseled: '155:1', quartz_pillar_vertical: '155:2', quartz_pillar_horizontal: '155:3', quartz_pillar_cap: '155:4', @@ -285,7 +307,7 @@ var blocks = { hardened_clay: 172, coal_block: 173, packed_ice: 174, - double_plant: 175, + double_plant: 175, sunflower: 175, purpur: 201, purpur_pillar: 202, @@ -324,10 +346,12 @@ var blocks = { acacia: 187 }, fence: { + oak: 85, + nether: 113, spruce: 188, birch: 189, jungle: 190, - oak: 191, + darkoak: 191, acacia: 192 } }; @@ -335,21 +359,21 @@ var blocks = { // Add all available colors to colorized block collections var colorized_blocks = [ - 'wool', - 'stained_clay', - 'carpet', - 'stained_glass', + '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; - + data_value = blocks[block].white; + for (var color in colors) { blocks[block][color] = data_value + ':' + colors[color]; } -}; +} /* rainbow colors - a convenience @@ -366,11 +390,10 @@ blocks.rainbow = [ blocks.stained_glass.blue, blocks.stained_glass.purple ]; -blocks.isStair = function(id){ +blocks.isStair = function(id) { var p; - for (p in this.stairs){ - if (this.stairs[p] == id) - return true; + for (p in this.stairs) { + if (this.stairs[p] == id) return true; } return false; }; diff --git a/src/main/resources/scriptcraft/modules/bukkit/fireworks.js b/src/main/resources/scriptcraft/modules/bukkit/fireworks.js new file mode 100644 index 000000000..3c7669869 --- /dev/null +++ b/src/main/resources/scriptcraft/modules/bukkit/fireworks.js @@ -0,0 +1,61 @@ +/* + 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.getWorld().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/resources/scriptcraft/modules/bukkit/input.js b/src/main/resources/scriptcraft/modules/bukkit/input.js new file mode 100644 index 000000000..e8b1f3d62 --- /dev/null +++ b/src/main/resources/scriptcraft/modules/bukkit/input.js @@ -0,0 +1,30 @@ +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/resources/scriptcraft/modules/bukkit/inventory.js similarity index 71% rename from src/main/js/modules/bukkit/inventory.js rename to src/main/resources/scriptcraft/modules/bukkit/inventory.js index bba4feaae..9f1fc2550 100644 --- a/src/main/js/modules/bukkit/inventory.js +++ b/src/main/resources/scriptcraft/modules/bukkit/inventory.js @@ -1,15 +1,15 @@ -function inventory(entity){ +function inventory(entity) { var inv = entity.inventory; var result = { - add: function(items){ + add: function(items) { inv.addItem([items]); return result; }, - remove: function(items){ + remove: function(items) { inv.removeItem([items]); return result; }, - contains: function(items){ + contains: function(items) { return inv['contains(org.bukkit.inventory.ItemStack)'](items); } }; diff --git a/src/main/resources/scriptcraft/modules/bukkit/items.js b/src/main/resources/scriptcraft/modules/bukkit/items.js new file mode 100644 index 000000000..cb5f98f47 --- /dev/null +++ b/src/main/resources/scriptcraft/modules/bukkit/items.js @@ -0,0 +1,31 @@ +/*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/resources/scriptcraft/modules/bukkit/recipes.js b/src/main/resources/scriptcraft/modules/bukkit/recipes.js new file mode 100644 index 000000000..daf539eb6 --- /dev/null +++ b/src/main/resources/scriptcraft/modules/bukkit/recipes.js @@ -0,0 +1,17 @@ +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/resources/scriptcraft/modules/bukkit/sounds.js b/src/main/resources/scriptcraft/modules/bukkit/sounds.js new file mode 100644 index 000000000..4dcd5f0ef --- /dev/null +++ b/src/main/resources/scriptcraft/modules/bukkit/sounds.js @@ -0,0 +1,63 @@ +var bkLocation = Packages.org.bukkit.Location, + i = 0, + foreach = require('utils').foreach, + allSounds = Packages.org.bukkit.Sound.values(), + len = allSounds.length, + sound, + soundName; + +function play(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); +} + +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); + }); + break; + default: + } + }; + })(sound); +} +exports.play = play; diff --git a/src/main/resources/scriptcraft/modules/entities.js b/src/main/resources/scriptcraft/modules/entities.js new file mode 100644 index 000000000..748e9e9bd --- /dev/null +++ b/src/main/resources/scriptcraft/modules/entities.js @@ -0,0 +1,30 @@ +'use strict'; +var entities = {}, + entitytypes, + t, + name; + +entitytypes = Packages.org.bukkit.entity.EntityType.values(); + +function getEntityHandler(entityType) { + return function(entity) { + if (arguments.length == 0) { + return entityType; + } + if (arguments.length == 1) { + if (entity) { + return entity.type == 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/resources/scriptcraft/modules/fireworks.js similarity index 83% rename from src/main/js/modules/fireworks.js rename to src/main/resources/scriptcraft/modules/fireworks.js index b8495b1b3..13917ac0a 100644 --- a/src/main/js/modules/fireworks.js +++ b/src/main/resources/scriptcraft/modules/fireworks.js @@ -15,7 +15,7 @@ Example.... ... creates a single firework, while .... - /js firework().fwd(3).times(5) + /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 @@ -32,11 +32,4 @@ location. For example... ![firework example](img/firework.png) ***/ - -if ( __plugin.canary ) { - exports.firework = require('./canary/fireworks'); -} else { - exports.firework = require('./bukkit/fireworks'); -} - - +exports.firework = require('./bukkit/fireworks'); diff --git a/src/main/js/modules/input.js b/src/main/resources/scriptcraft/modules/input.js similarity index 84% rename from src/main/js/modules/input.js rename to src/main/resources/scriptcraft/modules/input.js index 20d75814f..1dd4b8450 100644 --- a/src/main/js/modules/input.js +++ b/src/main/resources/scriptcraft/modules/input.js @@ -1,18 +1,18 @@ /************************************************************************* ## Asynchronous Input Module -The `input` module provides a simple way to prompt players for input at the +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 +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. +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: @@ -24,7 +24,7 @@ exports.numberguess = function(player){ if ( guess == 'q'){ return; } - if ( +guess !== randomNumber ) { + if ( +guess !== randomNumber ) { if (+guess < randomNumber ) { echo( guesser, 'Too low - guess again'); } @@ -39,7 +39,7 @@ exports.numberguess = function(player){ }; ``` -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 `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 @@ -56,8 +56,4 @@ The callback function as well as being bound to an object with the above propert 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'); -} +module.exports = require('./bukkit/input'); diff --git a/src/main/js/modules/inventory/index.js b/src/main/resources/scriptcraft/modules/inventory/index.js similarity index 68% rename from src/main/js/modules/inventory/index.js rename to src/main/resources/scriptcraft/modules/inventory/index.js index 72583dc1e..750a35e9e 100644 --- a/src/main/js/modules/inventory/index.js +++ b/src/main/resources/scriptcraft/modules/inventory/index.js @@ -1,7 +1,7 @@ /************************************************************************ ## Inventory Module -This module provides functions to add items to, remove items from and check the -contents of a player or NPC's inventory. +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. @@ -11,16 +11,19 @@ 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) ) +// gives every player 2 cookies and a baked potatoe +var bakedPotato = items.bakedPotato(1); +var cookies2 = items.cookie(2); + +utils.players(function( player ){ + inventory( player ) + .add( cookies2 ) + .add( bakedPotato ) }); // give a player 6 cookies then take away 4 of them -inventory(player) +inventory( player ) .add( items.cookie(6) ) .remove ( items.cookie(4) ) @@ -28,6 +31,13 @@ inventory(player) var hasCookies = inventory(player).contains( items.cookie(1) ); +// Enchant an item and give it to the player (CraftBukkit/Spigot only) + +var luck = org.bukkit.enchantments.Enchantment.getByName("LUCK"); +var luckyRod = items.fishingRod( 1 ); +luckyRod.addEnchantment( luck, 3); +inventory( player ).add( luckyRod ); + ``` The inventory module exposes a single function which when passed a player or NPC will return an object with 3 methods: @@ -36,8 +46,4 @@ The inventory module exposes a single function which when passed a player or NPC * 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'); -} +module.exports = require('../bukkit/inventory'); diff --git a/src/main/resources/scriptcraft/modules/items.js b/src/main/resources/scriptcraft/modules/items.js new file mode 100644 index 000000000..096b18937 --- /dev/null +++ b/src/main/resources/scriptcraft/modules/items.js @@ -0,0 +1 @@ +module.exports = require('./bukkit/items'); diff --git a/src/main/js/modules/lightning.js b/src/main/resources/scriptcraft/modules/lightning.js similarity index 58% rename from src/main/js/modules/lightning.js rename to src/main/resources/scriptcraft/modules/lightning.js index b1e8a4166..7c39b3eed 100644 --- a/src/main/js/modules/lightning.js +++ b/src/main/resources/scriptcraft/modules/lightning.js @@ -18,13 +18,6 @@ events.projectileHit( function( event ){ ``` ***/ -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; +module.exports = function lightning(something) { + return something.location.world.strikeLightning(something.location); }; diff --git a/src/main/js/modules/recipes.js b/src/main/resources/scriptcraft/modules/recipes.js similarity index 85% rename from src/main/js/modules/recipes.js rename to src/main/resources/scriptcraft/modules/recipes.js index ccefbb48e..c0e6c2e1c 100644 --- a/src/main/js/modules/recipes.js +++ b/src/main/resources/scriptcraft/modules/recipes.js @@ -27,8 +27,4 @@ To add an EnderBow to the game (assumes there's an enchanted Item variable calle server.removeRemove( addedRecipe ); ***/ -if (__plugin.canary) { - module.exports = require('./canary/recipes'); -} else { - module.exports = require('./bukkit/recipes'); -} +module.exports = require('./bukkit/recipes'); diff --git a/src/main/js/modules/sc-mqtt.js b/src/main/resources/scriptcraft/modules/sc-mqtt.js similarity index 62% rename from src/main/js/modules/sc-mqtt.js rename to src/main/resources/scriptcraft/modules/sc-mqtt.js index da1e13576..f7fca842c 100644 --- a/src/main/js/modules/sc-mqtt.js +++ b/src/main/resources/scriptcraft/modules/sc-mqtt.js @@ -11,7 +11,7 @@ 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 + 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 @@ -58,90 +58,90 @@ library. [mqtt]: http://mqtt.org/ ***/ -var MISSING_MQTT = '\nMissing class org.walterhiggins.scriptcraft.ScriptCraftMqttCallback.\n' + - 'Make sure sc-mqtt.jar is in the classpath.\n' + +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 ) { - +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(err) { + console.log('connectionLost: ' + err); }, - function( topic, message ) { - console.log( 'messageArrived ' + topic + '> ' + message ); + function(topic, message) { + console.log('messageArrived ' + topic + '> ' + message); }, - function( token ) { - console.log( 'deliveryComplete:' + token ); + function(token) { + console.log('deliveryComplete:' + token); } ); - - if ( !brokerUrl ) { + + if (!brokerUrl) { brokerUrl = 'tcp://localhost:1883'; } - if ( !clientId ) { + if (!clientId) { clientId = 'scriptcraft' + new Date().getTime(); } - var client = new MqttClient( brokerUrl, clientId, null ); - client.setCallback( callback ); + var client = new MqttClient(brokerUrl, clientId, null); + client.setCallback(callback); return { - connect: function( options ) { - if ( typeof options === 'undefined' ) { + connect: function(options) { + if (typeof options === 'undefined') { client.connect(); - }else{ + } else { client.connect(options); } return client; }, - disconnect: function( quiesceTimeout ) { - if ( typeof quiesceTimeout == 'undefined' ) { - client.disconnect(); + disconnect: function(quiesceTimeout) { + if (typeof quiesceTimeout == 'undefined') { + client.disconnect(); } else { - client.disconnect( quiesceTimeout ); + client.disconnect(quiesceTimeout); } return client; }, - publish: function( topic, message, qos, retained ) { - if ( typeof message == 'string' ) { - message = new java.lang.String( message ).bytes; + publish: function(topic, message, qos, retained) { + if (typeof message == 'string') { + message = new java.lang.String(message).bytes; } - if (typeof qos == 'undefined'){ + if (typeof qos == 'undefined') { qos = 1; } - if (typeof retained == 'undefined'){ + if (typeof retained == 'undefined') { retained = false; } - client.publish( topic, message,qos, retained ); + client.publish(topic, message, qos, retained); return client; }, - subscribe: function( topic ) { - client.subscribe( topic ); + subscribe: function(topic) { + client.subscribe(topic); return client; }, - unsubscribe: function( topic ) { - client.unsubscribe( topic ); + unsubscribe: function(topic) { + client.unsubscribe(topic); return client; }, - onMessageArrived: function( fn ) { - callback.setMesgArrived( fn ); + onMessageArrived: function(fn) { + callback.setMesgArrived(fn); return client; }, - onDeliveryComplete: function( fn ) { - callback.setDeliveryComplete( fn ); + onDeliveryComplete: function(fn) { + callback.setDeliveryComplete(fn); return client; }, - onConnectionLost: function( fn ) { - callback.setConnLost( fn ); + onConnectionLost: function(fn) { + callback.setConnLost(fn); return client; } }; @@ -149,10 +149,11 @@ function Client( brokerUrl, clientId ) { /* Return a new MQTT Client */ -exports.client = function( brokerUrl, clientId, options ) { - if ( typeof org.walterhiggins.scriptcraft.ScriptCraftMqttCallback != 'function' ) { +exports.client = function(brokerUrl, clientId, options) { + if ( + typeof org.walterhiggins.scriptcraft.ScriptCraftMqttCallback != 'function' + ) { throw MISSING_MQTT; } - return new Client( brokerUrl, clientId, options ); + return new Client(brokerUrl, clientId, options); }; - diff --git a/src/main/resources/scriptcraft/modules/signs/menu.js b/src/main/resources/scriptcraft/modules/signs/menu.js new file mode 100644 index 000000000..e08b78a3f --- /dev/null +++ b/src/main/resources/scriptcraft/modules/signs/menu.js @@ -0,0 +1,182 @@ +'use strict'; +var utils = require('utils'), + store = persist('signs', {}); + +require('utils/string-exts'); +/* + 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; +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); + } + 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; +} + + +// +// 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/resources/scriptcraft/modules/signs/package.json b/src/main/resources/scriptcraft/modules/signs/package.json new file mode 100644 index 000000000..0cc8cf123 --- /dev/null +++ b/src/main/resources/scriptcraft/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/resources/scriptcraft/modules/signs/signs.js similarity index 84% rename from src/main/js/modules/signs/signs.js rename to src/main/resources/scriptcraft/modules/signs/signs.js index 5d6d2ab0c..5f7604534 100644 --- a/src/main/js/modules/signs/signs.js +++ b/src/main/resources/scriptcraft/modules/signs/signs.js @@ -1,5 +1,5 @@ 'use strict'; -/*global __plugin, require, module, exports*/ +/*global __plugin__, require, module, exports*/ /************************************************************************ ## Signs Module @@ -15,8 +15,8 @@ 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 + 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 @@ -26,7 +26,7 @@ existing sign in the game world. * 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. @@ -39,15 +39,15 @@ an interactive sign. #### Example: Create a sign which changes the time of day. ##### plugins/signs/time-of-day.js - -```javascript + +```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; + var time = selectedIndex * 6000; event.player.location.world.setTime(time); }; @@ -55,12 +55,12 @@ var onTimeChoice = function(event){ 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); }; ``` @@ -81,12 +81,12 @@ the entity has targeted. It is a utility function for use by plugin authors. #### Example -```javascript +```javascript var signs = require('signs'), utils = require('utils'); var player = utils.player('tom1234'); var sign = signs.getTargetedBy( player ); -if ( !sign ) { +if ( !sign ) { echo( player, 'Not looking at a sign'); } ``` @@ -95,29 +95,21 @@ if ( !sign ) { [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; - } +function hasSign(block) { + 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 ) { +for (var i in menu) { exports[i] = menu[i]; } -function getTargetedBy( livingEntity ) { - var location = utils.getMousePos( livingEntity ); - if ( !location ) { +function getTargetedBy(livingEntity) { + var location = utils.getMousePos(livingEntity); + if (!location) { return null; } return hasSign(utils.blockAt(location)); diff --git a/src/main/js/modules/slash.js b/src/main/resources/scriptcraft/modules/slash.js similarity index 67% rename from src/main/js/modules/slash.js rename to src/main/resources/scriptcraft/modules/slash.js index 92e66a7c1..71af7ecc3 100644 --- a/src/main/js/modules/slash.js +++ b/src/main/resources/scriptcraft/modules/slash.js @@ -1,5 +1,5 @@ 'use strict'; -/*global module, require, server, __plugin*/ +/*global module, require, server, __plugin__*/ var _ = require('underscore'); /************************************************************************ ## The slash Module @@ -39,27 +39,18 @@ slash([ ``` ***/ -function slash( commands, sender ){ - if (_.isArray(commands)){ - _.each(commands, function(command){ +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); - } + 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/resources/scriptcraft/modules/sounds.js b/src/main/resources/scriptcraft/modules/sounds.js new file mode 100644 index 000000000..237e009e5 --- /dev/null +++ b/src/main/resources/scriptcraft/modules/sounds.js @@ -0,0 +1 @@ +module.exports = require('./bukkit/sounds'); diff --git a/src/main/js/modules/spawn.js b/src/main/resources/scriptcraft/modules/spawn.js similarity index 67% rename from src/main/js/modules/spawn.js rename to src/main/resources/scriptcraft/modules/spawn.js index 471d9d565..c82f39851 100644 --- a/src/main/js/modules/spawn.js +++ b/src/main/resources/scriptcraft/modules/spawn.js @@ -1,4 +1,4 @@ -/*global require, module, __plugin, Packages*/ +/*global require, module, __plugin__, Packages*/ 'use strict'; var entities = require('entities'); /************************************************************************ @@ -24,23 +24,19 @@ var entities = require('entities'), ... 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(); - } +module.exports = function(entityType, location) { 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(); + if (typeof entityType === 'string') { + var entityTypeFn = entities[entityType.toLowerCase()]; + try { + entityType = entityTypeFn(); + world.spawnEntity(location, entityType); + } catch(e) { + echo(global.self, `Error Spawning ${entityType}`) + } } }; diff --git a/src/main/js/modules/teleport.js b/src/main/resources/scriptcraft/modules/teleport.js similarity index 62% rename from src/main/js/modules/teleport.js rename to src/main/resources/scriptcraft/modules/teleport.js index f98524dc2..a233b7b58 100644 --- a/src/main/js/modules/teleport.js +++ b/src/main/resources/scriptcraft/modules/teleport.js @@ -1,10 +1,10 @@ 'use strict'; -/*global __plugin, org, module, require*/ +/*global __plugin__, org, module, require*/ var utils = require('utils'); /************************************************************************ ## Teleport Module -This module provides a function to teleport entities (Players or NPCs). +This module provides a function to teleport entities (Players or NPCs). ### Parameters @@ -12,14 +12,14 @@ This module provides a function to teleport entities (Players or NPCs). * 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 +### Example The following code will teleport each player back to their spawn position. ```javascript var teleport = require('teleport'), utils = require('utils'), - players = utils.players(), + players = utils.players(), i = 0; for ( ; i < players.length; i++ ) { teleport( players[i], players[i].spawnPosition ); @@ -30,30 +30,28 @@ The following code will teleport 'tom' to 'jane's location. ```javascript var teleport = require('teleport'); -teleport('tom' , 'jane'); +teleport('tom' , 'jane'); ``` ***/ -function teleport( entity, destination){ - if (typeof entity === 'String' || entity instanceof java.lang.String){ +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){ + if ( + typeof destination === 'string' || + destination instanceof java.lang.String + ) { var player = utils.player(destination); - if (player){ + if (player) { destination = player.location; } } else { - if (destination.location){ + 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); - } + var bkTeleportCause = + org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; + entity.teleport(destination, bkTeleportCause.PLUGIN); } module.exports = teleport; diff --git a/src/main/resources/scriptcraft/modules/utils/package.json b/src/main/resources/scriptcraft/modules/utils/package.json new file mode 100644 index 000000000..4349dba58 --- /dev/null +++ b/src/main/resources/scriptcraft/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/resources/scriptcraft/modules/utils/string-exts.js similarity index 87% rename from src/main/js/modules/utils/string-exts.js rename to src/main/resources/scriptcraft/modules/utils/string-exts.js index 14b767d9d..18ccaecca 100644 --- a/src/main/js/modules/utils/string-exts.js +++ b/src/main/resources/scriptcraft/modules/utils/string-exts.js @@ -72,8 +72,10 @@ var formattingCodes = { underline: 'n', reset: 'r' }; -for ( var method in formattingCodes ) { - String.prototype[method] = function( c ) { - return function(){ return c + this; }; - }( COLOR_CHAR + formattingCodes[method] ); +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/resources/scriptcraft/modules/utils/utils.js similarity index 64% rename from src/main/js/modules/utils/utils.js rename to src/main/resources/scriptcraft/modules/utils/utils.js index 11dd21113..2d62f5ae9 100644 --- a/src/main/js/modules/utils/utils.js +++ b/src/main/resources/scriptcraft/modules/utils/utils.js @@ -1,15 +1,7 @@ -/*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; -} +var bkBukkit = org.bukkit.Bukkit, + bkLocation = org.bukkit.Location, + bkBlockCommandSender = org.bukkit.command.BlockCommandSender; /************************************************************************ ## Utilities Module @@ -28,7 +20,7 @@ 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 + then player() will try to return the `self` variable . It is strongly recommended to provide a parameter. #### Example @@ -46,60 +38,36 @@ if ( player ) { [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 +[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' ) { +function _player(playerName) { + if (typeof playerName == 'undefined') { + if (typeof self == 'undefined') { return null; - } else { + } 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 + if (typeof playerName == 'string') 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 ); - } +function _world(worldName) { + if (worldName instanceof org.bukkit.World) { + return worldName; } - if (__plugin.bukkit){ - if (worldName instanceof org.bukkit.World){ - return worldName; - } - if (worldName === undefined){ - return bkBukkit.getWorlds().get(0); - } - return bkBukkit.getWorld( worldName ); + if (worldName === undefined) { + return bkBukkit.getWorlds().get(0); } - return null; + return bkBukkit.getWorld(worldName); } exports.world = _world; @@ -109,14 +77,8 @@ exports.world = _world; 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; +function _blockAt(location) { + return location.block; } exports.blockAt = _blockAt; /************************************************************************* @@ -135,25 +97,25 @@ utils.locationToJSON() returns a [Location][cmloc] object in JSON form... 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, +function _locationToJSON(location) { + var yaw = location.yaw; + return { + world: '' + location.world.name, + x: location.x, + y: location.y, + z: location.z, yaw: yaw, pitch: location.pitch }; -}; +} /************************************************************************* ### utils.locationToString() function @@ -167,7 +129,7 @@ keys in a lookup table. #### Example -```javascript +```javascript var utils = require('utils'); ... var key = utils.locationToString(player.location); @@ -175,8 +137,8 @@ lookupTable[key] = player.name; ``` ***/ -exports.locationToString = function locationToString( location ) { - return JSON.stringify( _locationToJSON( location ) ); +exports.locationToString = function locationToString(location) { + return JSON.stringify(_locationToJSON(location)); }; exports.locationToJSON = _locationToJSON; @@ -190,28 +152,31 @@ returned by locationToJSON() and reconstructs and returns a bukkit Location object. ***/ -exports.locationFromJSON = function locationFromJSON( json ) { +exports.locationFromJSON = function locationFromJSON(json) { var world; - if ( json.constructor == Array ) { + if (json.constructor == Array) { // for support of legacy format - world = _world( json[0] ); - return new bkLocation( world, json[1], json[2] , json[3] ); + 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 ); - } + 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.' ); +exports.getPlayerObject = function getPlayerObject(player) { + console.warn( + 'utils.getPlayerObject() is deprecated. Use utils.player() instead.' + ); return _player(player); }; /************************************************************************* @@ -232,22 +197,12 @@ 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; - } - } +function getPlayerPos(player) { + player = _player(player); + if (player) { + if (player instanceof bkBlockCommandSender) return player.block.location; + else return player.location; + } return null; } exports.getPlayerPos = getPlayerPos; @@ -271,44 +226,29 @@ 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); - } + targetPos.world.strikeLightning(targetPos); } ``` ***/ -exports.getMousePos = function getMousePos( player ) { - +exports.getMousePos = function getMousePos(player) { player = _player(player); - if ( !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; - } + var targetedBlock; + // 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; }; @@ -345,8 +285,8 @@ processing of arrays. * 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 + * 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 @@ -362,7 +302,7 @@ The following example illustrates how to use foreach for immediate processing of ```javascript var utils = require('utils'); var players = utils.players(); -utils.foreach (players, function( player ) { +utils.foreach (players, function( player ) { echo( player , 'Hi ' + player); }); ``` @@ -371,27 +311,27 @@ utils.foreach (players, function( player ) { 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 ) { +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); + function next() { + callback(array[i], i, context, array); i++; } function hasNext() { return i < len; } - if ( delay ) { - _nicely( next, hasNext, onCompletion, delay ); + if (delay) { + _nicely(next, hasNext, onCompletion, delay); } else { - for ( ;i < len; i++ ) { - callback( array[i], i, context, array ); + for (; i < len; i++) { + callback(array[i], i, context, array); } } -}; +} exports.foreach = _foreach; /************************************************************************ ### utils.nicely() function @@ -404,7 +344,7 @@ function and the start of the next `next()` function. #### Parameters - * next : A function which will be called if processing is to be done. + * 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 @@ -417,51 +357,38 @@ function and the start of the next `next()` function. See the source code to utils.foreach for an example of how utils.nicely is used. ***/ -function _nicely( next, hasNext, onDone, delay ) { - if ( hasNext() ){ +function _nicely(next, hasNext, onDone, delay) { + if (hasNext()) { next(); - setTimeout( function() { - _nicely( next, hasNext, onDone, delay ); - }, delay ); - }else{ - if ( onDone ) { + setTimeout(function() { + _nicely(next, hasNext, onDone, delay); + }, delay); + } else { + if (onDone) { onDone(); } } -}; +} exports.nicely = _nicely; -function _at( time24hr, callback, pWorlds, repeat ) { +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); + 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. +canarymod and bukkit differ in how the timeofday is calculated. See http://minecraft.gamepedia.com/Day-night_cycle#Conversions ***/ -function getTime(world){ +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; + return world.time; } exports.time = getTime; @@ -477,10 +404,10 @@ See http://minecraft.gamepedia.com/Day-night_cycle#Conversions * world : the name of the world or world object for which you want to get time ***/ -function getTime24( world ){ +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 ); + var mins = Math.floor(((mcTime + 6000) % 24000) / 16.6667); return mins; } exports.time24 = getTime24; @@ -504,10 +431,10 @@ a given directory and recursiving trawling all sub-directories. var utils = require('utils'); var jsFiles = utils.find('./', function(dir,name){ return name.match(/\.js$/); -}); +}); ``` ***/ -exports.find = function( path, filter){ +exports.find = function(path, filter) { console.warn("utils.find() is deprecated, use require('find') instead"); return require('find')(path, filter); }; @@ -524,23 +451,20 @@ console.log(serverAddress); ***/ exports.serverAddress = function serverAddress() { var interfaces = java.net.NetworkInterface.getNetworkInterfaces(); - var current, - addresses, - current_addr; - while ( interfaces.hasMoreElements() ) { + var current, addresses, current_addr; + while (interfaces.hasMoreElements()) { current = interfaces.nextElement(); - if ( ! current.isUp() || current.isLoopback() || current.isVirtual() ) { + 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(); + if (current_addr.isLoopbackAddress()) continue; + if (current_addr instanceof java.net.Inet4Address) + return current_addr.getHostAddress(); } - } + } return null; }; /************************************************************************** @@ -548,30 +472,30 @@ exports.serverAddress = function serverAddress() { 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( ){ +function toArray() { var result = [], javaArray = null, i = 0; - if (arguments[0] instanceof java.util.Collection){ + if (arguments[0] instanceof java.util.Collection) { // it's a java collection javaArray = arguments[0].toArray(); - for ( ;i < javaArray.length; i++) { + for (; i < javaArray.length; i++) { result.push(javaArray[i]); } - } else if (arguments[0].constructor === Array){ + } 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++) { + for (; i < javaArray.length; i++) { result.push(javaArray[i]); } } @@ -579,13 +503,8 @@ function toArray( ){ } exports.array = toArray; -function worlds(){ - if (__plugin.canary){ - return toArray(server.worldManager.allWorlds); - } - if (__plugin.bukkit){ - return toArray(server.worlds); - } +function worlds() { + return toArray(server.worlds); } exports.worlds = worlds; @@ -608,72 +527,58 @@ Any players with a bow will be able to launch fireworks by shooting. This function returns a javascript array of player names (as javascript strings) ***/ -function getPlayersBukkit(){ +function getPlayersBukkit() { var result = []; var players = server.getOnlinePlayers(); - for (var i = 0; i < players.size(); i++){ + for (var i = 0; i < players.size(); i++) { result.push(players.get(i)); } return result; } -function getPlayersCanary(){ +function getPlayersCanary() { var result = []; var players = server.playerList; - for (var i = 0; i < players.size(); i++){ + 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; -} +getPlayers = getPlayersBukkit; -function getStatBukkit(){ - if (arguments.length == 1){ - var stat = arguments[1]; +function getStatBukkit() { + var stat, player; + if (arguments.length == 1) { + stat = arguments[1]; return org.bukkit.Statistic[stat.toUpperCase()]; } else { - var player = arguments[0]; - var stat = arguments[1]; + player = arguments[0]; + 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]; +function getStatCanary() { + var stat, + player, + cmStatistics = Packages.net.canarymod.api.statistics.Statistics; + if (arguments.length == 1) { + stat = arguments[0]; return cmStatistics[stat.toUpperCase()].instance; } else { - var player = arguments[0]; - var stat = arguments[1]; + player = arguments[0]; + 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; }); +function getPlayerNames() { + return getPlayers().map(function(p) { + return p.name; + }); } -exports.players = function players(fn){ +exports.players = function players(fn) { var result = getPlayers(); - if (fn){ + if (fn) { result.forEach(fn); } return result; @@ -688,35 +593,35 @@ 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 + * 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 @@ -739,5 +644,4 @@ This function also contains values for each possible stat so you can get at stat var JUMPSTAT = utils.stat.JUMP; // Accessing the value var jumpCount = player.getStat ( JUMPSTAT ); // canary-specific code ***/ -exports.stat = __plugin.canary ? getStatCanary: getStatBukkit; - +exports.stat = getStatBukkit; diff --git a/src/main/js/modules/watcher.js b/src/main/resources/scriptcraft/modules/watcher.js similarity index 72% rename from src/main/js/modules/watcher.js rename to src/main/resources/scriptcraft/modules/watcher.js index 7970d6c83..9daefddbd 100644 --- a/src/main/js/modules/watcher.js +++ b/src/main/resources/scriptcraft/modules/watcher.js @@ -29,11 +29,11 @@ watcher.watchFile( 'test.txt', function( file ) { var filesWatched = {}; var dirsWatched = {}; -exports.watchFile = function( file, callback ) { - if ( typeof file == 'string' ) { +exports.watchFile = function(file, callback) { + if (typeof file == 'string') { file = new File(file); } - filesWatched[file.canonicalPath] = { + filesWatched[file.getCanonicalPath()] = { callback: callback, lastModified: file.lastModified() }; @@ -64,25 +64,26 @@ watcher.watchDir( 'players/_ial', function( dir ) { ``` ***/ -exports.watchDir = function( dir, callback ) { - if ( typeof dir == 'string' ) { +exports.watchDir = function(dir, callback) { + if (typeof dir == 'string') { dir = new File(dir); } - dirsWatched[dir.canonicalPath] = { + dirsWatched[dir.getCanonicalPath()] = { callback: callback, lastModified: dir.lastModified() }; - - var files = dir.listFiles(),file; - if ( !files ) { + + var files = dir.listFiles(), + file; + if (!files) { return; } - for ( var i = 0; i < files.length; i++ ) { + for (var i = 0; i < files.length; i++) { file = files[i]; - if (file.isDirectory( )) { - exports.watchDir(file,callback); - }else{ - exports.watchFile(file,callback); + if (file.isDirectory()) { + exports.watchDir(file, callback); + } else { + exports.watchFile(file, callback); } } }; @@ -98,11 +99,11 @@ watcher.unwatchFile('test.txt'); ``` ***/ -exports.unwatchFile = function( file, callback ) { - if ( typeof file == 'string' ) { +exports.unwatchFile = function(file) { + if (typeof file == 'string') { file = new File(file); } - delete filesWatched[file.canonicalPath]; + delete filesWatched[file.getCanonicalPath()]; }; /************************************************************************ @@ -123,67 +124,67 @@ watcher.unwatchFile (file); for each file inside directory (and unwatchDir for each directory inside it) ***/ -exports.unwatchDir = function( dir, callback ) { - if ( typeof dir == 'string' ) { +exports.unwatchDir = function(dir) { + if (typeof dir == 'string') { dir = new File(dir); } - delete dirsWatched[dir.canonicalPath]; - - var files = dir.listFiles(),file; - if ( !files ) { + delete dirsWatched[dir.getCanonicalPath()]; + + var files = dir.listFiles(), + file; + if (!files) { return; } - for ( var i = 0; i < files.length; i++ ) { + for (var i = 0; i < files.length; i++) { file = files[i]; - if (file.isDirectory( )) { - exports.unwatchDir(file,callback); - }else{ - exports.unwatchFile(file,callback); + if (file.isDirectory()) { + exports.unwatchDir(file); + } else { + exports.unwatchFile(file); } } }; -function fileWatcher(calledCallbacks) { +function fileWatcher() { for (var file in filesWatched) { var fileObject = new File(file); var lm = fileObject.lastModified(); - if ( lm != filesWatched[file].lastModified ) { + if (String(lm) != String(filesWatched[file].lastModified)) { filesWatched[file].lastModified = lm; filesWatched[file].callback(fileObject); if (!fileObject.exists()) { - exports.unwatchFile(file,filesWatched[file].callback); + 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) { +function dirWatcher() { for (var dir in dirsWatched) { var dirObject = new File(dir); var lm = dirObject.lastModified(); var dw = dirsWatched[dir]; - if ( lm != dirsWatched[dir].lastModified ) { + if (String(lm) != String(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 +//guarantees that a callback is only called once for each change function monitorDirAndFiles() { - fileWatcher (); - dirWatcher (); - setTimeout( monitorDirAndFiles, 3000 ); -}; + fileWatcher(); + dirWatcher(); + setTimeout(monitorDirAndFiles, 3000); +} -setTimeout( monitorDirAndFiles, 3000 ); +setTimeout(monitorDirAndFiles, 3000); diff --git a/src/main/resources/scriptcraft/plugins/example-plugin/index.js b/src/main/resources/scriptcraft/plugins/example-plugin/index.js new file mode 100644 index 000000000..3ba6ecff6 --- /dev/null +++ b/src/main/resources/scriptcraft/plugins/example-plugin/index.js @@ -0,0 +1,21 @@ +/** + * Example Modern Plugin + */ + +registerPlugin({ + onEnable() { + console.info('Example plugin enabled! Config message: ' + this.config.welcomeMessage); + }, + + commands: { + greet(args, player) { + echo(player, this.config.welcomeMessage); + } + }, + + events: { + playerJoin(event) { + echo(event.player, this.config.welcomeMessage); + } + } +}); diff --git a/src/main/resources/scriptcraft/plugins/example-plugin/plugin.json b/src/main/resources/scriptcraft/plugins/example-plugin/plugin.json new file mode 100644 index 000000000..21db34480 --- /dev/null +++ b/src/main/resources/scriptcraft/plugins/example-plugin/plugin.json @@ -0,0 +1,15 @@ +{ + "name": "example-plugin", + "version": "1.0.0", + "main": "index.js", + "description": "A modern ScriptCraft plugin example.", + "config": { + "welcomeMessage": "Welcome to the modernized ScriptCraft!" + }, + "commands": { + "greet": { + "aliases": ["hello", "hi"], + "description": "Greets the player." + } + } +} diff --git a/src/main/js/readme.md b/src/main/resources/scriptcraft/readme.md similarity index 100% rename from src/main/js/readme.md rename to src/main/resources/scriptcraft/readme.md diff --git a/src/main/resources/scriptcraft/typings/bukkit.d.ts b/src/main/resources/scriptcraft/typings/bukkit.d.ts new file mode 100644 index 000000000..4d52d7524 Binary files /dev/null and b/src/main/resources/scriptcraft/typings/bukkit.d.ts differ diff --git a/src/main/resources/scriptcraft/typings/scriptcraft.d.ts b/src/main/resources/scriptcraft/typings/scriptcraft.d.ts new file mode 100644 index 000000000..dc58424d9 --- /dev/null +++ b/src/main/resources/scriptcraft/typings/scriptcraft.d.ts @@ -0,0 +1,163 @@ +/** + * ScriptCraft Type Definitions + */ + +/// +/// + +declare interface ScriptCraftEvents { + /** + * Registers an event listener. + */ + on(eventType: any, handler: (event: any, cancel: () => void) => void, priority?: string): { unregister: () => void }; + + /** + * Alias for on() to match browser standards. + */ + addEventListener(eventType: any, handler: (event: any, cancel: () => void) => void, priority?: string): { unregister: () => void }; + + /** + * Registers a one-time event listener. + */ + once(eventType: any, handler: (event: any, cancel: () => void) => void, priority?: string): { unregister: () => void }; + + /** + * Removes an event listener. + */ + off(eventType: any, handler: Function, priority?: string): void; + + /** + * Alias for off() to match browser standards. + */ + removeEventListener(eventType: any, handler: Function, priority?: string): void; + + [eventName: string]: (handler: (event: any, cancel: () => void) => void, priority?: string) => { unregister: () => void }; +} + +declare class EventEmitter { + on(type: string, listener: Function): this; + once(type: string, listener: Function): this; + off(type: string, listener: Function): this; + removeListener(type: string, listener: Function): this; + emit(type: string, ...args: any[]): boolean; +} + +declare interface ScriptCraftCommands { + /** + * Defines a new command. + */ + (name: string | Function, func?: Function, options?: string[], intercepts?: boolean): Function; +} + +declare function echo(player: any, message: string): void; + +declare interface FetchResponse { + ok: boolean; + status: number; + statusText: string; + headers: { get(name: string): string | null }; + text(): Promise; + json(): Promise; +} + +declare function fetch(url: string, options?: { + method?: string, + headers?: { [key: string]: string }, + body?: string +}): Promise; + +declare interface ScriptCraftPlugin { + store: any; + storage: { + get(key: string): any; + set(key: string, value: any): void; + all(): any; + }; +} + +declare interface ScriptCraftProcess { + env: { [key: string]: string }; + platform: 'win32' | 'linux'; + version: string; + arch: string; + cwd(): string; + nextTick(callback: Function): void; +} + +declare interface ScriptCraftConsole { + log(...args: any[]): void; + info(...args: any[]): void; + warn(...args: any[]): void; + error(...args: any[]): void; + debug(...args: any[]): void; +} + +declare interface ScriptCraftFS { + readFileSync(path: string, options?: string | { encoding?: string }): string | any; + writeFileSync(path: string, data: string | any, options?: any): void; + appendFileSync(path: string, data: string | any): void; + existsSync(path: string): boolean; + mkdirSync(path: string, options?: { recursive?: boolean }): void; + readdirSync(path: string): string[]; + unlinkSync(path: string): void; + statSync(path: string): { + isFile(): boolean; + isDirectory(): boolean; + size: number; + mtime: Date; + }; + promises: { + readFile(path: string, options?: string | { encoding?: string }): Promise; + writeFile(path: string, data: string | any, options?: any): Promise; + readdir(path: string): Promise; + mkdir(path: string, options?: { recursive?: boolean }): Promise; + unlink(path: string): Promise; + stat(path: string): Promise<{ + isFile(): boolean; + isDirectory(): boolean; + size: number; + mtime: Date; + }>; + }; +} + +declare const process: ScriptCraftProcess; +declare const console: ScriptCraftConsole; +declare const fs: ScriptCraftFS; +declare const events: ScriptCraftEvents; +declare const hooks: ScriptCraftHooks; + +declare const global: any; +declare const self: any; + +declare function setTimeout(callback: Function, delay: number, ...args: any[]): any; +declare function clearTimeout(task: any): void; +declare function setInterval(callback: Function, interval: number, ...args: any[]): any; +declare function clearInterval(task: any): void; +declare function setImmediate(callback: Function, ...args: any[]): any; +declare function alert(message: string): void; + +declare interface PluginDefinition { + onEnable?: (this: ScriptCraftPlugin) => void; + onDisable?: (this: ScriptCraftPlugin) => void; + commands?: { + [name: string]: ((this: ScriptCraftPlugin, args: string[], player: any) => void) | { + callback: (this: ScriptCraftPlugin, args: string[], player: any) => void; + aliases?: string[]; + description?: string; + tabComplete?: string[] | ((this: ScriptCraftPlugin, args: string[], player: any) => string[]); + } + }; + events?: { [name: string]: (this: ScriptCraftPlugin, event: any) => void }; +} + +declare function plugin(name: string, moduleObject: any, isPersistent?: boolean): ScriptCraftPlugin; + +declare function registerPlugin(name: string, definition: PluginDefinition): ScriptCraftPlugin; + +declare interface ScriptCraftHooks { + add(hookName: string, callback: Function): void; + trigger(hookName: string, ...args: any[]): void; +} + +declare const hooks: ScriptCraftHooks; diff --git a/src/tools/js/run-type-gen.js b/src/tools/js/run-type-gen.js new file mode 100644 index 000000000..6cc54368e --- /dev/null +++ b/src/tools/js/run-type-gen.js @@ -0,0 +1,31 @@ +'use strict'; + +/** + * Runner for the Type Generator. + * This can be run via jscript.java or within ScriptCraft. + */ + +var TypeGenerator = require('./type-generator-core.js'); +var File = java.io.File; +var FileUtils = org.apache.commons.io.FileUtils; + +var generator = new TypeGenerator(); + +var coreClasses = [ + 'org.bukkit.Server', + 'org.bukkit.World', + 'org.bukkit.entity.Player', + 'org.bukkit.block.Block', + 'org.bukkit.inventory.ItemStack', + 'org.bukkit.Location', + 'org.bukkit.entity.Entity', + 'org.bukkit.command.CommandSender', + 'org.bukkit.command.Command' +]; + +var types = generator.run(coreClasses); + +var outputFile = new File('src/main/resources/scriptcraft/typings/bukkit-generated.d.ts'); +FileUtils.writeStringToFile(outputFile, types, 'UTF-8'); + +console.log('Types generated to: ' + outputFile.getAbsolutePath()); diff --git a/src/tools/js/type-generator-core.js b/src/tools/js/type-generator-core.js new file mode 100644 index 000000000..c13bf02ae --- /dev/null +++ b/src/tools/js/type-generator-core.js @@ -0,0 +1,107 @@ +'use strict'; + +/** + * TypeScript Type Generator for ScriptCraft. + * Uses Java Reflection to generate .d.ts files from Java classes. + */ + +var Modifier = java.lang.reflect.Modifier; +var File = java.io.File; +var FileUtils = org.apache.commons.io.FileUtils; + +function TypeGenerator() { + this.classes = {}; + this.queue = []; + this.processed = {}; +} + +TypeGenerator.prototype.mapType = function(javaType) { + if (javaType.isPrimitive()) { + var name = javaType.name; + if (name === 'boolean') return 'boolean'; + if (name === 'void') return 'void'; + return 'number'; // int, long, float, etc. + } + + if (javaType.isArray()) { + return this.mapType(javaType.componentType) + '[]'; + } + + var fullName = javaType.name; + if (fullName === 'java.lang.String') return 'string'; + if (fullName === 'java.lang.Object') return 'any'; + if (fullName === 'java.lang.Boolean') return 'boolean'; + if (fullName.startsWith('java.lang.Number') || + fullName === 'java.lang.Integer' || + fullName === 'java.lang.Long' || + fullName === 'java.lang.Double') return 'number'; + + // Extract short name for the interface + var parts = fullName.split('.'); + var shortName = parts[parts.length - 1].replace(/\$/g, '_'); + + // Add to queue if not processed + if (!this.processed[fullName] && fullName.startsWith('org.bukkit')) { + this.queue.push(fullName); + } + + return shortName; +}; + +TypeGenerator.prototype.generateClass = function(className) { + if (this.processed[className]) return; + this.processed[className] = true; + + try { + var clz = java.lang.Class.forName(className); + var parts = className.split('.'); + var shortName = parts[parts.length - 1].replace(/\$/g, '_'); + + var output = 'interface ' + shortName; + + // Handle Inheritance + var superClz = clz.getSuperclass(); + if (superClz && superClz.name !== 'java.lang.Object' && superClz.name.startsWith('org.bukkit')) { + output += ' extends ' + this.mapType(superClz); + } + + output += ' {\n'; + + // Methods + var methods = clz.getDeclaredMethods(); + for (var i = 0; i < methods.length; i++) { + var method = methods[i]; + if (!Modifier.isPublic(method.getModifiers())) continue; + + var methodName = method.getName(); + var params = method.getParameters(); + var paramStrs = []; + for (var j = 0; j < params.length; j++) { + paramStrs.push('arg' + j + ': ' + this.mapType(params[j].getType())); + } + + output += ' ' + methodName + '(' + paramStrs.join(', ') + '): ' + this.mapType(method.getReturnType()) + ';\n'; + } + + output += '}\n\n'; + this.classes[className] = output; + } catch (e) { + // console.error('Failed to process class: ' + className + ' - ' + e); + } +}; + +TypeGenerator.prototype.run = function(startClasses) { + this.queue = startClasses; + while (this.queue.length > 0) { + var next = this.queue.shift(); + this.generateClass(next); + } + + var finalOutput = '/** Autogenerated ScriptCraft Types **/\n\n'; + for (var clz in this.classes) { + finalOutput += this.classes[clz]; + } + return finalOutput; +}; + +module.exports = TypeGenerator;