Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d33c9ca

Browse files
committed
1 parent 30cc710 commit d33c9ca

File tree

3 files changed

+99
-25
lines changed

3 files changed

+99
-25
lines changed

docs/API-Reference.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ Walter Higgins
401401
* [utils.foreach() function](#utilsforeach-function)
402402
* [utils.nicely() function](#utilsnicely-function)
403403
* [utils.at() function](#utilsat-function)
404+
* [utils.time( world ) function](#utilstime-world--function)
405+
* [utils.time24( world ) function](#utilstime24-world--function)
404406
* [utils.find() function](#utilsfind-function)
405407
* [utils.serverAddress() function](#utilsserveraddress-function)
406408
* [utils.watchFile() function](#utilswatchfile-function)
@@ -4811,6 +4813,19 @@ utils.at( '19:00', function() {
48114813
});
48124814
```
48134815
4816+
### utils.time( world ) function
4817+
4818+
Returns the timeofday (in minecraft ticks) for the given world. This function is necessary because
4819+
canarymod and bukkit differ in how the timeofday is calculated.
4820+
4821+
See http://minecraft.gamepedia.com/Day-night_cycle#Conversions
4822+
4823+
### utils.time24( world ) function
4824+
4825+
Returns the timeofday for the given world using 24 hour notation. (number of minutes)
4826+
4827+
See http://minecraft.gamepedia.com/Day-night_cycle#Conversions
4828+
48144829
### utils.find() function
48154830
48164831
The utils.find() function will return a list of all files starting at
@@ -4920,7 +4935,16 @@ all of Javascript's Array goodness.
49204935
49214936
### utils.players() function
49224937
4923-
This function returns a javascript array of all online players on the server.
4938+
This function returns a javascript array of all online players on the
4939+
server. You can optionally provide a function which will be invoked
4940+
with each player as a parameter. For example, to give each player the
4941+
ability to shoot arrows which launch fireworks:
4942+
4943+
```javascript
4944+
require('utils').players( arrows.firework )
4945+
```
4946+
4947+
Any players with a bow will be able to launch fireworks by shooting.
49244948
49254949
### utils.playerNames() function
49264950

src/main/js/modules/utils/utils.js

+59-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*global __plugin, org, exports*/
1+
/*global __plugin, org, exports, server*/
22
'use strict';
33
var File = java.io.File;
44

@@ -473,14 +473,54 @@ exports.at = function( time24hr, callback, worlds ) {
473473
}
474474
_nicely( function() {
475475
_foreach( worlds, function ( world ) {
476-
var time = world.getTime();
476+
var time = getTime(world);
477477
var diff = timeMc - time;
478478
if ( diff > 0 && diff < 100 ) {
479479
callback();
480480
}
481481
});
482482
}, forever, null, 100 );
483483
};
484+
/*************************************************************************
485+
### utils.time( world ) function
486+
487+
Returns the timeofday (in minecraft ticks) for the given world. This function is necessary because
488+
canarymod and bukkit differ in how the timeofday is calculated.
489+
490+
See http://minecraft.gamepedia.com/Day-night_cycle#Conversions
491+
492+
***/
493+
function getTime(world){
494+
if (__plugin.bukkit){
495+
return world.time;
496+
}
497+
if (__plugin.canary){
498+
// there's a bug in canary where if you call world.setTime() the world.totalTime
499+
// becomes huge.
500+
if (world.totalTime < world.rawTime){
501+
return world.totalTime;
502+
} else {
503+
return ((world.totalTime % world.rawTime) + world.relativeTime) % 24000;
504+
}
505+
}
506+
return 0;
507+
}
508+
exports.time = getTime;
509+
510+
/*************************************************************************
511+
### utils.time24( world ) function
512+
513+
Returns the timeofday for the given world using 24 hour notation. (number of minutes)
514+
515+
See http://minecraft.gamepedia.com/Day-night_cycle#Conversions
516+
517+
***/
518+
function getTime24(world){
519+
var mcTime = getTime(world);
520+
var mins = Math.floor( ( (mcTime + 6000) % 24000) / 16.6667 );
521+
return mins;
522+
}
523+
exports.time24 = getTime24;
484524

485525
/************************************************************************
486526
### utils.find() function
@@ -797,7 +837,16 @@ exports.worlds = __plugin.canary ? canaryWorlds : bukkitWorlds;
797837
/*************************************************************************
798838
### utils.players() function
799839
800-
This function returns a javascript array of all online players on the server.
840+
This function returns a javascript array of all online players on the
841+
server. You can optionally provide a function which will be invoked
842+
with each player as a parameter. For example, to give each player the
843+
ability to shoot arrows which launch fireworks:
844+
845+
```javascript
846+
require('utils').players( arrows.firework )
847+
```
848+
849+
Any players with a bow will be able to launch fireworks by shooting.
801850
802851
### utils.playerNames() function
803852
@@ -866,7 +915,13 @@ if (__plugin.canary){
866915
function getPlayerNames(){
867916
return getPlayers().map(function(p){ return p.name; });
868917
}
869-
exports.players = getPlayers;
918+
exports.players = function(fn){
919+
var result = getPlayers();
920+
if (fn){
921+
result.forEach(fn);
922+
}
923+
return result;
924+
};
870925
exports.playerNames = getPlayerNames;
871926

872927
/*************************************************************************

src/main/js/plugins/drone/contrib/lcd-clock.js

+15-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
/*global require, setInterval, clearInterval, __plugin, exports*/
33
/*
4-
Experimental:
54
Point at a block and issue the following ...
65
/js var d = new Drone();
76
/js var clock = new LCDClock(d);
@@ -10,10 +9,14 @@
109
/js clock.stop24();
1110
... stops the clock...
1211
*/
13-
var blocks = require('blocks');
12+
var blocks = require('blocks'),
13+
utils = require('utils'),
14+
Drone = require('drone');
1415

15-
exports.LCDClock = function(drone, fgColor,bgColor,border) {
16+
Drone.extend(lcdclock);
1617

18+
function lcdclock(fgColor, bgColor, border){
19+
var drone = this;
1720
var lastSecs = [0,0,0,0],
1821
world = drone.world,
1922
intervalId = -1;
@@ -62,27 +65,19 @@ exports.LCDClock = function(drone, fgColor,bgColor,border) {
6265
bgColor = blocks.wool.black;
6366
}
6467
if ( typeof fgColor == 'undefined' ) {
65-
fgColor = blocks.wool.white ; // white wool
68+
fgColor = blocks.glowstone ;
6669
}
6770
if ( border ) {
6871
drone.box(border,21,9,1);
6972
drone.up().right();
7073
}
7174
drone.blocktype('00:00', fgColor, bgColor, true);
72-
return {
73-
start24: function( ) {
74-
function tick() {
75-
var rolloverMins = 24*60,
76-
mcTime = __plugin.canary ? world.totalTime : world.time,
77-
timeOfDayInMins = Math.floor(((mcTime + 6000) % 24000) / 16.6667);
78-
timeOfDayInMins = timeOfDayInMins % rolloverMins;
79-
update( timeOfDayInMins );
80-
};
81-
intervalId = setInterval(tick, 800);
82-
},
83-
stop24: function() {
84-
clearInterval( intervalId );
85-
}
86-
};
87-
};
75+
76+
function tick() {
77+
var timeOfDayInMins = utils.time24(world);
78+
update( timeOfDayInMins );
79+
}
80+
intervalId = setInterval(tick, 800);
81+
console.log('lcdclock started background task:' + intervalId);
82+
}
8883

0 commit comments

Comments
 (0)