2013-12-31 10:48:38 +01:00
# Anatomy of a ScriptCraft Plugin
2015-04-16 22:13:44 +02:00
Anything you can do using a Java-based plugin, you can do it
faster and easier in JavaScript with the ScriptCraft plugin. To
2013-12-31 10:48:38 +01:00
demonstrate this, I've recreated a commonly-used mod (homes) in
2015-04-16 22:13:44 +02:00
javascript. The [homes][homes] JavaScript plugin lets players set their current
2013-12-31 10:48:38 +01:00
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
2015-04-16 22:13:44 +02:00
demonstrates a couple of new features in ScriptCraft …
2013-12-31 10:48:38 +01:00
* Persistence
* Adding Player (non-operator) commands
2015-04-16 22:13:44 +02:00
[homes]: src/main/js/plugins/homes/homes.js
Here, I walk you through another useful plugin which lets players modify the color of the in-game chat.
2014-12-29 13:16:45 +01:00
## Persistence
2015-04-16 22:13:44 +02:00
… First persistence. Persistence is the ability to retain state
2014-12-29 13:16:45 +01:00
after the server has shutdown and started up again. You can create a
2015-04-16 22:13:44 +02:00
JavaScript object which will be saved at shutdown and reloaded at
2014-12-29 13:16:45 +01:00
startup by using the built-in `persist()` function.
```javascript
// file: scriptcraft/plugins/my-first-plugin.js
var prefs = persist('myprefs', {});
...
prefs.color = 'black';
```
In the example above, a new empty object is created and stored in a file called `myprefs-store.json` . The empty object is returned (if data is not already present in that file or the file does not exist) and any changes to the object's contents are written to the file when the server is shutdown.
The data is persisted in JSON form so it's even somewhat
human-readable. Declaring a new plugin is easy. I'm going to create a
new plugin called "chat" that will let players change the default
2015-04-16 22:13:44 +02:00
color of their messages in the in-game chat window …
2014-12-29 13:16:45 +01:00
```javascript
var store = persist('chat-colors', {players: {}});
exports.chat = {
setColor: function(player,chatColor) {
store.players[player.name] = chatColor;
}
}
```
2013-12-31 10:48:38 +01:00
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
2015-04-16 22:13:44 +02:00
ensures that when a player chats, the text will be displayed in their
chosen color …
2013-12-31 10:48:38 +01:00
2014-12-29 13:16:45 +01:00
```javascript
var colors = ['black', 'blue', 'darkgreen', 'darkaqua', 'darkred',
'purple', 'gold', 'gray', 'darkgray', 'indigo',
'brightgreen', 'aqua', 'red', 'pink',
'yellow', 'white'];
var colorCodes = {};
2015-04-17 17:17:34 +02:00
var COLOR_CHAR = '\u00a7';
2014-12-29 13:16:45 +01:00
for (var i =0;i < colors.length ; i + + )
colorCodes[colors[i]] = i.toString(16);
2015-04-17 17:17:34 +02:00
var addColor = function( evt ) {
2014-12-29 13:16:45 +01:00
var player = evt.player;
2014-12-29 14:06:48 +01:00
var playerChatColor = store.players[ player.name ];
2014-12-29 13:16:45 +01:00
if ( playerChatColor ) {
2015-04-17 17:17:34 +02:00
evt.message = COLOR_CHAR + colorCodes[ playerChatColor ] + evt.message;
2014-12-29 13:16:45 +01:00
}
2015-04-17 17:17:34 +02:00
};
if (__plugin.bukkit) {
events.asyncPlayerChat(addColor);
} else if (__plugin.canary) {
events.chat(addColor);
};
2014-12-29 13:16:45 +01:00
```
2013-12-31 10:48:38 +01:00
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.
2014-12-29 13:16:45 +01:00
## Adding new Player Commands
2015-04-16 22:13:44 +02:00
The other command in ScriptCraft is the `/jsp` command – this lets
2013-12-31 10:48:38 +01:00
operators expose plugins for use by regular players. To be clear, `/jsp`
2015-04-16 22:13:44 +02:00
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
2013-12-31 10:48:38 +01:00
want players to be able to use that function, you expose it using the
2015-04-16 22:13:44 +02:00
new `command()` function like so …
2013-12-31 10:48:38 +01:00
2014-12-29 13:16:45 +01:00
```javascript
function chat_color( params, sender ){
var color = params[0];
if (colorCodes[color]){
chat.setColor(sender,color);
}else{
echo(sender, color + ' is not a valid color');
echo(sender, 'valid colors: ' + colors.join(', '));
}
}
command(chat_color, colors);
```
2013-12-31 10:48:38 +01:00
2015-04-16 22:13:44 +02:00
… The above code adds a new *subcommand* to the `/jsp` command and
also specifies autocomplete options (the last parameter – `colors` ) for
2013-12-31 10:48:38 +01:00
that command when the player presses the `TAB` key. Now the player
2015-04-16 22:13:44 +02:00
themselves can change their chosen chat color like so …
2013-12-31 10:48:38 +01:00
/jsp chat_color yellow
2015-04-16 22:13:44 +02:00
… What I've done here is create a new plugin which lets players choose
2013-12-31 10:48:38 +01:00
a chat color and saves that preference when the server shuts down and
2015-04-16 22:13:44 +02:00
starts up. I've also added a new `jsp` sub-command – `chat_color` that
2013-12-31 10:48:38 +01:00
players use to change their chat color setting. The full plugin source
2015-04-16 22:13:44 +02:00
code is just a couple of lines of code but is a fully working plugin …
2013-12-31 10:48:38 +01:00
2014-12-29 13:16:45 +01:00
```javascript
var store = persist('chat-colors', {players: {}});
exports.chat = {
setColor: function(player,chatColor) {
store.players[player.name] = chatColor;
}
}
var colors = ['black', 'blue', 'darkgreen', 'darkaqua', 'darkred',
'purple', 'gold', 'gray', 'darkgray', 'indigo',
'brightgreen', 'aqua', 'red', 'pink',
'yellow', 'white'];
var colorCodes = {};
2015-04-17 17:17:34 +02:00
var COLOR_CHAR = '\u00a7';
2014-12-29 13:16:45 +01:00
for (var i =0;i < colors.length ; i + + )
colorCodes[colors[i]] = i.toString(16);
2015-04-17 17:17:34 +02:00
var addColor = function( evt ) {
2014-12-29 13:16:45 +01:00
var player = evt.player;
2014-12-29 14:04:52 +01:00
var playerChatColor = store.players[ player.name ];
2014-12-29 13:16:45 +01:00
if ( playerChatColor ) {
2015-04-17 17:17:34 +02:00
evt.message = COLOR_CHAR + colorCodes[ playerChatColor ] + evt.message;
2014-12-29 13:16:45 +01:00
}
2015-04-17 17:17:34 +02:00
};
if (__plugin.bukkit) {
events.asyncPlayerChat(addColor);
} else if (__plugin.canary) {
events.chat(addColor);
};
2014-12-29 13:16:45 +01:00
function chat_color( params, sender ){
var color = params[0];
if (colorCodes[color]){
chat.setColor(sender,color);
}else{
echo(sender, color + ' is not a valid color');
echo(sender, 'valid colors: ' + colors.join(', '));
}
}
2015-04-17 17:17:34 +02:00
2014-12-29 13:16:45 +01:00
command(chat_color, colors);
```
2013-12-31 10:48:38 +01:00
![Chat Color plugin][1]
2015-04-16 22:13:44 +02:00
… 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
2013-12-31 10:48:38 +01:00
modders a feel for just how easy it can be to change the game to suit
their needs.
[1]: img/scriptcraft-chat-color.png