diff --git a/docs/api.md b/docs/api.md index 246857e..bd7383d 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1312,10 +1312,19 @@ Example The following example illustrates how to use foreach for immediate processing of an array... var players = ["moe", "larry", "curly"]; - foreach (players, function(item){ + utils.foreach (players, function(item){ server.getPlayer(item).sendMessage("Hi " + item); }); - + +... The `utils.foreach()` function can work with Arrays or any Java-style collection. This is important +because many objects in the Bukkit API use Java-style collections... + + utils.foreach( server.onlinePlayers, function(player){ + player.chat("Hello!"); + }); + +... the above code sends a "Hello!" to every online player. + The following example is a more complex use case - The need to build an enormous structure without hogging CPU usage... @@ -1335,7 +1344,7 @@ without hogging CPU usage... var onDone = function(){ player.sendMessage("Job Done!"); }; - foreach (a, processItem, null, 10, onDone); + utils.foreach (a, processItem, null, 10, onDone); utils.nicely() function ======================= @@ -1379,9 +1388,9 @@ Example To warn players when night is approaching... - utils.at( "19:00", function(){ + utils.at( "19:00", function() { /* it's 7 in the evening so warn all players that night is coming ! */ - utils.foreach(server.onlinePlayers, function(player){ + utils.foreach( server.onlinePlayers, function(player){ player.chat("The night is dark and full of terrors!"); }); }, self.world); diff --git a/src/main/javascript/utils/utils.js b/src/main/javascript/utils/utils.js index 07328c2..8a73907 100644 --- a/src/main/javascript/utils/utils.js +++ b/src/main/javascript/utils/utils.js @@ -71,10 +71,19 @@ Example The following example illustrates how to use foreach for immediate processing of an array... var players = ["moe", "larry", "curly"]; - foreach (players, function(item){ + utils.foreach (players, function(item){ server.getPlayer(item).sendMessage("Hi " + item); }); - + +... The `utils.foreach()` function can work with Arrays or any Java-style collection. This is important +because many objects in the Bukkit API use Java-style collections... + + utils.foreach( server.onlinePlayers, function(player){ + player.chat("Hello!"); + }); + +... the above code sends a "Hello!" to every online player. + The following example is a more complex use case - The need to build an enormous structure without hogging CPU usage... @@ -94,7 +103,7 @@ without hogging CPU usage... var onDone = function(){ player.sendMessage("Job Done!"); }; - foreach (a, processItem, null, 10, onDone); + utils.foreach (a, processItem, null, 10, onDone); ***/ foreach: function(array, callback, object, delay, onCompletion) {