Skip to content

Commit f1925ef

Browse files
committed
added syntax-highlighting to code samples
1 parent fe62f61 commit f1925ef

7 files changed

Lines changed: 616 additions & 485 deletions

File tree

docs/API-Reference.md

Lines changed: 144 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,23 @@ loads the module circle.js in the same directory.
156156

157157
The contents of foo.js:
158158

159-
var circle = require('./circle.js');
160-
console.log( 'The area of a circle of radius 4 is '
161-
+ circle.area(4));
159+
```javascript
160+
var circle = require('./circle.js');
161+
console.log( 'The area of a circle of radius 4 is '
162+
+ circle.area(4));
163+
```
162164

163165
The contents of circle.js:
164166

165-
var PI = Math.PI;
166-
167-
exports.area = function (r) {
168-
return PI * r * r;
169-
};
170-
171-
exports.circumference = function (r) {
172-
return 2 * PI * r;
173-
};
167+
```javascript
168+
var PI = Math.PI;
169+
exports.area = function (r) {
170+
return PI * r * r;
171+
};
172+
exports.circumference = function (r) {
173+
return 2 * PI * r;
174+
};
175+
```
174176

175177
The module circle.js has exported the functions area() and
176178
circumference(). To add functions and objects to the root of your
@@ -210,9 +212,11 @@ module in the `plugins` directory exports becomes a global
210212
variable. For example, if you have a module greeting.js in the plugins
211213
directory....
212214

213-
exports.greet = function(player) {
214-
player.sendMessage('Hello ' + player.name);
215-
};
215+
```javascript
216+
exports.greet = function(player) {
217+
player.sendMessage('Hello ' + player.name);
218+
};
219+
```
216220

217221
... then `greet` becomes a global function and can be used at the
218222
in-game (or server) command prompt like so...
@@ -400,10 +404,12 @@ restored using the `scload()` function.
400404

401405
#### Example
402406

403-
var myObject = { name: 'John Doe',
404-
aliases: ['John Ray', 'John Mee'],
405-
date_of_birth: '1982/01/31' };
406-
scsave(myObject, 'johndoe.json');
407+
```javascript
408+
var myObject = { name: 'John Doe',
409+
aliases: ['John Ray', 'John Mee'],
410+
date_of_birth: '1982/01/31' };
411+
scsave(myObject, 'johndoe.json');
412+
```
407413

408414
##### johndoe.json contents...
409415

@@ -490,13 +496,15 @@ If Node.js supports setTimeout() then it's probably good for ScriptCraft to supp
490496

491497
#### Example
492498

493-
//
494-
// start a storm in 5 seconds
495-
//
496-
setTimeout( function() {
497-
var world = server.worlds.get(0);
498-
world.setStorm(true);
499-
}, 5000);
499+
```javascript
500+
//
501+
// start a storm in 5 seconds
502+
//
503+
setTimeout( function() {
504+
var world = server.worlds.get(0);
505+
world.setStorm(true);
506+
}, 5000);
507+
```
500508

501509
### clearTimeout() function
502510

@@ -566,23 +574,29 @@ For example imagine you have 3 files program.js, inc.js and math.js ...
566574

567575
### math.js
568576

569-
exports.add = function(a,b){
570-
return a + b;
571-
}
577+
```javascript
578+
exports.add = function(a,b){
579+
return a + b;
580+
}
581+
```
572582

573583
### inc.js
574584

575-
var math = require('./math');
576-
exports.increment = function(n){
577-
return math.add(n, 1);
578-
}
585+
```javascript
586+
var math = require('./math');
587+
exports.increment = function(n){
588+
return math.add(n, 1);
589+
}
590+
```
579591

580592
### program.js
581593

582-
var inc = require('./inc').increment;
583-
var a = 7;
584-
a = inc(a);
585-
print(a);
594+
```javascript
595+
var inc = require('./inc').increment;
596+
var a = 7;
597+
a = inc(a);
598+
print(a);
599+
```
586600

587601
You can see from the above sample code that programs can use modules
588602
and modules themeselves can use other modules. Modules have full
@@ -857,31 +871,38 @@ present in the CraftBukkit classpath. To use this module, you should
857871
craftbukkit-sc-mqtt.bat and edit it to include the following
858872
command...
859873

860-
java -classpath sc-mqtt.jar;craftbukit.jar org.bukkit.craftbukkit.Main
861-
874+
```sh
875+
java -classpath sc-mqtt.jar;craftbukit.jar org.bukkit.craftbukkit.Main
876+
```
877+
862878
If you're using Mac OS, create a new craftbukkit-sc-mqtt.command
863879
file and edit it (using TextWrangler or another text editor) ...
864880
865-
java -classpath sc-mqtt.jar:craftbukkit.jar org.bukit.craftbukkit.Main
881+
```sh
882+
java -classpath sc-mqtt.jar:craftbukkit.jar org.bukit.craftbukkit.Main
883+
```
866884
867885
4. Execute the craftbukkit-sc-mqtt batch file / command file to start
868886
Craftbukkit. You can now begin using this module to send and receive
869887
messages to/from a Net-enabled Arduino or any other device which uses
870888
the [MQTT protocol][mqtt]
871889
872-
var mqtt = require('sc-mqtt');
873-
// create a new client
874-
var client = mqtt.client( 'tcp://localhost:1883', 'uniqueClientId' );
875-
// connect to the broker
876-
client.connect( { keepAliveInterval: 15 } );
877-
// publish a message to the broker
878-
client.publish( 'minecraft', 'loaded' );
879-
// subscribe to messages on 'arduino' topic
880-
client.subscribe( 'arduino' );
881-
// do something when an incoming message arrives...
882-
client.onMessageArrived( function( topic, message ) {
883-
console.log( 'Message arrived: topic=' + topic + ', message=' + message );
884-
});
890+
```javascript
891+
var mqtt = require('sc-mqtt');
892+
// create a new client
893+
var client = mqtt.client( 'tcp://localhost:1883', 'uniqueClientId' );
894+
// connect to the broker
895+
client.connect( { keepAliveInterval: 15 } );
896+
// publish a message to the broker
897+
client.publish( 'minecraft', 'loaded' );
898+
// subscribe to messages on 'arduino' topic
899+
client.subscribe( 'arduino' );
900+
// do something when an incoming message arrives...
901+
client.onMessageArrived( function( topic, message ) {
902+
console.log( 'Message arrived: topic=' + topic + ', message=' + message );
903+
});
904+
905+
```
885906
886907
The `sc-mqtt` module provides a very simple minimal wrapper around the
887908
[Eclipse Paho MQTT Version 3 Client][pahodocs] java-based MQTT
@@ -1042,14 +1063,16 @@ String, then it tries to find the player with that name.
10421063

10431064
#### Example
10441065

1045-
var utils = require('utils');
1046-
var name = 'walterh';
1047-
var player = utils.player(name);
1048-
if (player) {
1049-
player.sendMessage('Got ' + name);
1050-
}else{
1051-
console.log('No player named ' + name);
1052-
}
1066+
```javascript
1067+
var utils = require('utils');
1068+
var name = 'walterh';
1069+
var player = utils.player(name);
1070+
if ( player ) {
1071+
player.sendMessage('Got ' + name);
1072+
} else {
1073+
console.log('No player named ' + name);
1074+
}
1075+
```
10531076
10541077
[bkpl]: http://jd.bukkit.org/dev/apidocs/org/bukkit/entity/Player.html
10551078
[bkloc]: http://jd.bukkit.org/dev/apidocs/org/bukkit/Location.html
@@ -1087,11 +1110,13 @@ The utils.locationToString() function returns a
10871110
keys in a lookup table.
10881111
10891112
#### Example
1090-
1091-
var utils = require('utils');
1092-
...
1093-
var key = utils.locationToString(player.location);
1094-
lookupTable[key] = player.name;
1113+
1114+
```javascript
1115+
var utils = require('utils');
1116+
...
1117+
var key = utils.locationToString(player.location);
1118+
lookupTable[key] = player.name;
1119+
```
10951120
10961121
### utils.locationFromJSON() function
10971122
@@ -1132,12 +1157,14 @@ is the location of the block the player is looking at (targeting).
11321157
11331158
The following code will strike lightning at the location the player is looking at...
11341159
1135-
var utils = require('utils');
1136-
var playerName = 'walterh';
1137-
var targetPos = utils.getMousePos(playerName);
1138-
if (targetPos){
1139-
targetPos.world.strikeLightning(targetPos);
1140-
}
1160+
```javascript
1161+
var utils = require('utils');
1162+
var playerName = 'walterh';
1163+
var targetPos = utils.getMousePos(playerName);
1164+
if (targetPos){
1165+
targetPos.world.strikeLightning(targetPos);
1166+
}
1167+
```
11411168
11421169
### utils.foreach() function
11431170
@@ -1186,42 +1213,48 @@ and put the code there.
11861213
11871214
The following example illustrates how to use foreach for immediate processing of an array...
11881215
1189-
var utils = require('utils');
1190-
var players = ['moe', 'larry', 'curly'];
1191-
utils.foreach (players, function(item){
1192-
server.getPlayer(item).sendMessage('Hi ' + item);
1193-
});
1216+
```javascript
1217+
var utils = require('utils');
1218+
var players = ['moe', 'larry', 'curly'];
1219+
utils.foreach (players, function(item){
1220+
server.getPlayer(item).sendMessage('Hi ' + item);
1221+
});
1222+
```
11941223
11951224
... The `utils.foreach()` function can work with Arrays or any Java-style collection. This is important
11961225
because many objects in the Bukkit API use Java-style collections...
11971226
1198-
utils.foreach( server.onlinePlayers, function(player){
1199-
player.chat('Hello!');
1200-
});
1227+
```javascript
1228+
utils.foreach( server.onlinePlayers, function(player){
1229+
player.chat('Hello!');
1230+
});
1231+
```
12011232
12021233
... the above code sends a 'Hello!' to every online player.
12031234
12041235
The following example is a more complex use case - The need to build an enormous structure
12051236
without hogging CPU usage...
12061237
1207-
// build a structure 200 wide x 200 tall x 200 long
1208-
// (That's 8 Million Blocks - enough to tax any machine!)
1209-
var utils = require('utils');
1210-
1211-
var a = [];
1212-
a.length = 200;
1213-
var drone = new Drone();
1214-
var processItem = function(item, index, object, array){
1215-
// build a box 200 wide by 200 long then move up
1216-
drone.box(blocks.wood, 200, 1, 200).up();
1217-
};
1218-
// by the time the job's done 'self' might be someone else
1219-
// assume this code is within a function/closure
1220-
var player = self;
1221-
var onDone = function(){
1222-
player.sendMessage('Job Done!');
1223-
};
1224-
utils.foreach (a, processItem, null, 10, onDone);
1238+
```javascript
1239+
// build a structure 200 wide x 200 tall x 200 long
1240+
// (That's 8 Million Blocks - enough to tax any machine!)
1241+
var utils = require('utils');
1242+
1243+
var a = [];
1244+
a.length = 200;
1245+
var drone = new Drone();
1246+
var processItem = function(item, index, object, array){
1247+
// build a box 200 wide by 200 long then move up
1248+
drone.box(blocks.wood, 200, 1, 200).up();
1249+
};
1250+
// by the time the job's done 'self' might be someone else
1251+
// assume this code is within a function/closure
1252+
var player = self;
1253+
var onDone = function(){
1254+
player.sendMessage('Job Done!');
1255+
};
1256+
utils.foreach (a, processItem, null, 10, onDone);
1257+
```
12251258
12261259
### utils.nicely() function
12271260
@@ -1262,15 +1295,17 @@ The utils.at() function will perform a given task at a given time every
12621295
12631296
To warn players when night is approaching...
12641297
1265-
var utils = require('utils');
1298+
```javascript
1299+
var utils = require('utils');
12661300
1267-
utils.at( '19:00', function() {
1301+
utils.at( '19:00', function() {
12681302
1269-
utils.foreach( server.onlinePlayers, function( player ) {
1303+
utils.foreach( server.onlinePlayers, function( player ) {
12701304
player.chat( 'The night is dark and full of terrors!' );
1271-
});
1272-
12731305
});
1306+
1307+
});
1308+
```
12741309
12751310
### utils.find() function
12761311
@@ -1286,10 +1321,12 @@ a given directory and recursiving trawling all sub-directories.
12861321
12871322
#### Example
12881323
1289-
var utils = require('utils');
1290-
var jsFiles = utils.find('./', function(dir,name){
1291-
return name.match(/\.js$/);
1292-
});
1324+
```javascript
1325+
var utils = require('utils');
1326+
var jsFiles = utils.find('./', function(dir,name){
1327+
return name.match(/\.js$/);
1328+
});
1329+
```
12931330
12941331
## Drone Plugin
12951332

0 commit comments

Comments
 (0)