1
1
# Anatomy of a ScriptCraft Plugin
2
2
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
5
5
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
7
7
location as home and return to that location using in-game commands.
8
8
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 & hellip ;
10
10
11
11
* Persistence
12
12
* Adding Player (non-operator) commands
13
13
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
+
14
18
## Persistence
15
- ... First persistence. Persistence is the ability to retain state
19
+ & hellip ; First persistence. Persistence is the ability to retain state
16
20
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
18
22
startup by using the built-in ` persist() ` function.
19
23
20
24
``` javascript
@@ -28,7 +32,7 @@ In the example above, a new empty object is created and stored in a file called
28
32
The data is persisted in JSON form so it's even somewhat
29
33
human-readable. Declaring a new plugin is easy. I'm going to create a
30
34
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 & hellip ;
32
36
33
37
``` javascript
34
38
var store = persist (' chat-colors' , {players: {}});
@@ -43,8 +47,8 @@ player's color choice ( `/js chat.setColor(self, 'green')` ). A little
43
47
bit more code has to be added so that the player's text color will
44
48
change when chatting with other players, but the above code will ensure
45
49
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 & hellip ;
48
52
49
53
``` javascript
50
54
var colors = [' black' , ' blue' , ' darkgreen' , ' darkaqua' , ' darkred' ,
@@ -69,18 +73,18 @@ handler which intercepts and inserts color codes into player's text
69
73
messages.
70
74
71
75
## 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 & ndash ; this lets
73
77
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 & ndash ; you
81
+ know & ndash ; actually set their text color of choice & ndash ; only operators can do
82
+ this for a player using the ` js chat.setColor(...) ` JavaScript
83
+ expression. Let's be clear & ndash ; 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
82
86
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 & hellip ;
84
88
85
89
``` javascript
86
90
function chat_color ( params , sender ){
@@ -95,18 +99,18 @@ function chat_color( params, sender ){
95
99
command (chat_color, colors);
96
100
```
97
101
98
- ... The above code adds a new * subcommand* to the ` /jsp ` command and
99
- also specifies autocomplete options (the last parameter - ` colors ` ) for
102
+ & hellip ; The above code adds a new * subcommand* to the ` /jsp ` command and
103
+ also specifies autocomplete options (the last parameter & ndash ; ` colors ` ) for
100
104
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 & hellip ;
102
106
103
107
/jsp chat_color yellow
104
108
105
- ... What I've done here is create a new plugin which lets players choose
109
+ & hellip ; What I've done here is create a new plugin which lets players choose
106
110
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 & ndash ; ` chat_color ` that
108
112
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 & hellip ;
110
114
111
115
``` javascript
112
116
var store = persist (' chat-colors' , {players: {}});
@@ -144,10 +148,10 @@ command(chat_color, colors);
144
148
145
149
![ Chat Color plugin] [ 1 ]
146
150
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
+ & hellip ; this is what I would call a minimum viable plugin and it
152
+ demonstrates some of the new features of ScriptCraft & ndash ; persistence
153
+ (automatic), event handling, and exposing new functionality to players
154
+ using the ` /jsp ` command. I hope this will give potential Minecraft
151
155
modders a feel for just how easy it can be to change the game to suit
152
156
their needs.
153
157
0 commit comments