Skip to content

Commit e1e7858

Browse files
committed
Merge pull request walterhiggins#234 from carlrobert/patch-14
Linked to homes and added chat_color intro
2 parents 52070cf + 4a1b20c commit e1e7858

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

docs/Anatomy-of-a-Plugin.md

+33-29
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
# Anatomy of a ScriptCraft Plugin
22

3-
Anything you can do using a java-based plugin, you can do it
4-
faster and easier in Javascript with the ScriptCraft plugin. To
3+
Anything you can do using a Java-based plugin, you can do it
4+
faster and easier in JavaScript with the ScriptCraft plugin. To
55
demonstrate this, I've recreated a commonly-used mod (homes) in
6-
javascript. The `homes` javascript plugin lets players set their current
6+
javascript. The [homes][homes] JavaScript plugin lets players set their current
77
location as home and return to that location using in-game commands.
88
They can also visit other players' homes. It's a simple plugin that
9-
demonstrates a couple of new features in ScriptCraft ...
9+
demonstrates a couple of new features in ScriptCraft …
1010

1111
* Persistence
1212
* Adding Player (non-operator) commands
1313

14+
[homes]: src/main/js/plugins/homes/homes.js
15+
16+
Here, I walk you through another useful plugin which lets players modify the color of the in-game chat.
17+
1418
## Persistence
15-
... First persistence. Persistence is the ability to retain state
19+
… First persistence. Persistence is the ability to retain state
1620
after the server has shutdown and started up again. You can create a
17-
Javascript object which will be saved at shutdown and reloaded at
21+
JavaScript object which will be saved at shutdown and reloaded at
1822
startup by using the built-in `persist()` function.
1923

2024
```javascript
@@ -28,7 +32,7 @@ In the example above, a new empty object is created and stored in a file called
2832
The data is persisted in JSON form so it's even somewhat
2933
human-readable. Declaring a new plugin is easy. I'm going to create a
3034
new plugin called "chat" that will let players change the default
31-
color of their messages in the in-game chat window...
35+
color of their messages in the in-game chat window …
3236

3337
```javascript
3438
var store = persist('chat-colors', {players: {}});
@@ -43,8 +47,8 @@ player's color choice ( `/js chat.setColor(self, 'green')` ). A little
4347
bit more code has to be added so that the player's text color will
4448
change when chatting with other players, but the above code will ensure
4549
the player's color setting is at least saved. The following code just
46-
ensures that when a player chats , the text will be displayed in their
47-
chosen color...
50+
ensures that when a player chats, the text will be displayed in their
51+
chosen color …
4852

4953
```javascript
5054
var colors = ['black', 'blue', 'darkgreen', 'darkaqua', 'darkred',
@@ -69,18 +73,18 @@ handler which intercepts and inserts color codes into player's text
6973
messages.
7074

7175
## Adding new Player Commands
72-
The other command in ScriptCraft is the `/jsp` command - this lets
76+
The other command in ScriptCraft is the `/jsp` command – this lets
7377
operators expose plugins for use by regular players. To be clear, `/jsp`
74-
does not do any javascript evaluation, it just accepts parameters which
75-
are then passed on to the appropriate javascript plugin. So far in this
76-
example plugin we haven't provided any way for regular players to - you
77-
know - actually set their text color of choice - only operators can do
78-
this for a player using the `js chat.setColor(...)` javascript
79-
expression. Let's be clear - giving your players access to the whole API
80-
via javascript isn't a good idea. So how do you safely let players
81-
choose their text color? If you've written a javascript function and
78+
does not do any JavaScript evaluation, it just accepts parameters which
79+
are then passed on to the appropriate JavaScript plugin. So far in this
80+
example plugin we haven't provided any way for regular players to – you
81+
know – actually set their text color of choice – only operators can do
82+
this for a player using the `js chat.setColor(...)` JavaScript
83+
expression. Let's be clear – giving your players access to the whole API
84+
via JavaScript isn't a good idea. So how do you safely let players
85+
choose their text color? If you've written a JavaScript function and
8286
want players to be able to use that function, you expose it using the
83-
new `command()` function like so...
87+
new `command()` function like so …
8488

8589
```javascript
8690
function chat_color( params, sender ){
@@ -95,18 +99,18 @@ function chat_color( params, sender ){
9599
command(chat_color, colors);
96100
```
97101

98-
... The above code adds a new *subcommand* to the `/jsp` command and
99-
also specifies autocomplete options (the last parameter - `colors`) for
102+
… The above code adds a new *subcommand* to the `/jsp` command and
103+
also specifies autocomplete options (the last parameter – `colors`) for
100104
that command when the player presses the `TAB` key. Now the player
101-
themselves can change their chosen chat color like so...
105+
themselves can change their chosen chat color like so …
102106

103107
/jsp chat_color yellow
104108

105-
... What I've done here is create a new plugin which lets players choose
109+
… What I've done here is create a new plugin which lets players choose
106110
a chat color and saves that preference when the server shuts down and
107-
starts up. I've also added a new `jsp` sub-command - `chat_color` that
111+
starts up. I've also added a new `jsp` sub-command – `chat_color` that
108112
players use to change their chat color setting. The full plugin source
109-
code is just a couple of lines of code but is a fully working plugin...
113+
code is just a couple of lines of code but is a fully working plugin …
110114

111115
```javascript
112116
var store = persist('chat-colors', {players: {}});
@@ -144,10 +148,10 @@ command(chat_color, colors);
144148

145149
![Chat Color plugin][1]
146150

147-
... this is what I would call a minimum viable plugin and it
148-
demonstrates some of the new features of ScriptCraft - persistence
149-
(automatic) , event handling, and exposing new functionality to players
150-
using the `/jsp` command. I hope this will give potential MineCraft
151+
… this is what I would call a minimum viable plugin and it
152+
demonstrates some of the new features of ScriptCraft – persistence
153+
(automatic), event handling, and exposing new functionality to players
154+
using the `/jsp` command. I hope this will give potential Minecraft
151155
modders a feel for just how easy it can be to change the game to suit
152156
their needs.
153157

0 commit comments

Comments
 (0)