From d33c9ca1e4bd0c88c4598d423831dcd0e612f972 Mon Sep 17 00:00:00 2001 From: walterhiggins Date: Sun, 11 Jan 2015 12:27:47 +0000 Subject: [PATCH] Fix issue #193 --- docs/API-Reference.md | 26 +++++++- src/main/js/modules/utils/utils.js | 63 +++++++++++++++++-- .../js/plugins/drone/contrib/lcd-clock.js | 35 +++++------ 3 files changed, 99 insertions(+), 25 deletions(-) diff --git a/docs/API-Reference.md b/docs/API-Reference.md index 9c8af23..c38ec8b 100644 --- a/docs/API-Reference.md +++ b/docs/API-Reference.md @@ -401,6 +401,8 @@ Walter Higgins * [utils.foreach() function](#utilsforeach-function) * [utils.nicely() function](#utilsnicely-function) * [utils.at() function](#utilsat-function) + * [utils.time( world ) function](#utilstime-world--function) + * [utils.time24( world ) function](#utilstime24-world--function) * [utils.find() function](#utilsfind-function) * [utils.serverAddress() function](#utilsserveraddress-function) * [utils.watchFile() function](#utilswatchfile-function) @@ -4811,6 +4813,19 @@ utils.at( '19:00', function() { }); ``` +### utils.time( world ) function + +Returns the timeofday (in minecraft ticks) for the given world. This function is necessary because +canarymod and bukkit differ in how the timeofday is calculated. + +See http://minecraft.gamepedia.com/Day-night_cycle#Conversions + +### utils.time24( world ) function + +Returns the timeofday for the given world using 24 hour notation. (number of minutes) + +See http://minecraft.gamepedia.com/Day-night_cycle#Conversions + ### utils.find() function The utils.find() function will return a list of all files starting at @@ -4920,7 +4935,16 @@ all of Javascript's Array goodness. ### utils.players() function -This function returns a javascript array of all online players on the server. +This function returns a javascript array of all online players on the +server. You can optionally provide a function which will be invoked +with each player as a parameter. For example, to give each player the +ability to shoot arrows which launch fireworks: + +```javascript +require('utils').players( arrows.firework ) +``` + +Any players with a bow will be able to launch fireworks by shooting. ### utils.playerNames() function diff --git a/src/main/js/modules/utils/utils.js b/src/main/js/modules/utils/utils.js index 896b918..caefe2e 100644 --- a/src/main/js/modules/utils/utils.js +++ b/src/main/js/modules/utils/utils.js @@ -1,4 +1,4 @@ -/*global __plugin, org, exports*/ +/*global __plugin, org, exports, server*/ 'use strict'; var File = java.io.File; @@ -473,7 +473,7 @@ exports.at = function( time24hr, callback, worlds ) { } _nicely( function() { _foreach( worlds, function ( world ) { - var time = world.getTime(); + var time = getTime(world); var diff = timeMc - time; if ( diff > 0 && diff < 100 ) { callback(); @@ -481,6 +481,46 @@ exports.at = function( time24hr, callback, worlds ) { }); }, forever, null, 100 ); }; +/************************************************************************* +### utils.time( world ) function + +Returns the timeofday (in minecraft ticks) for the given world. This function is necessary because +canarymod and bukkit differ in how the timeofday is calculated. + +See http://minecraft.gamepedia.com/Day-night_cycle#Conversions + +***/ +function getTime(world){ + if (__plugin.bukkit){ + return world.time; + } + if (__plugin.canary){ + // there's a bug in canary where if you call world.setTime() the world.totalTime + // becomes huge. + if (world.totalTime < world.rawTime){ + return world.totalTime; + } else { + return ((world.totalTime % world.rawTime) + world.relativeTime) % 24000; + } + } + return 0; +} +exports.time = getTime; + +/************************************************************************* +### utils.time24( world ) function + +Returns the timeofday for the given world using 24 hour notation. (number of minutes) + +See http://minecraft.gamepedia.com/Day-night_cycle#Conversions + +***/ +function getTime24(world){ + var mcTime = getTime(world); + var mins = Math.floor( ( (mcTime + 6000) % 24000) / 16.6667 ); + return mins; +} +exports.time24 = getTime24; /************************************************************************ ### utils.find() function @@ -797,7 +837,16 @@ exports.worlds = __plugin.canary ? canaryWorlds : bukkitWorlds; /************************************************************************* ### utils.players() function -This function returns a javascript array of all online players on the server. +This function returns a javascript array of all online players on the +server. You can optionally provide a function which will be invoked +with each player as a parameter. For example, to give each player the +ability to shoot arrows which launch fireworks: + +```javascript +require('utils').players( arrows.firework ) +``` + +Any players with a bow will be able to launch fireworks by shooting. ### utils.playerNames() function @@ -866,7 +915,13 @@ if (__plugin.canary){ function getPlayerNames(){ return getPlayers().map(function(p){ return p.name; }); } -exports.players = getPlayers; +exports.players = function(fn){ + var result = getPlayers(); + if (fn){ + result.forEach(fn); + } + return result; +}; exports.playerNames = getPlayerNames; /************************************************************************* diff --git a/src/main/js/plugins/drone/contrib/lcd-clock.js b/src/main/js/plugins/drone/contrib/lcd-clock.js index f813c69..bc1f123 100644 --- a/src/main/js/plugins/drone/contrib/lcd-clock.js +++ b/src/main/js/plugins/drone/contrib/lcd-clock.js @@ -1,7 +1,6 @@ 'use strict'; /*global require, setInterval, clearInterval, __plugin, exports*/ /* - Experimental: Point at a block and issue the following ... /js var d = new Drone(); /js var clock = new LCDClock(d); @@ -10,10 +9,14 @@ /js clock.stop24(); ... stops the clock... */ -var blocks = require('blocks'); +var blocks = require('blocks'), + utils = require('utils'), + Drone = require('drone'); -exports.LCDClock = function(drone, fgColor,bgColor,border) { +Drone.extend(lcdclock); +function lcdclock(fgColor, bgColor, border){ + var drone = this; var lastSecs = [0,0,0,0], world = drone.world, intervalId = -1; @@ -62,27 +65,19 @@ exports.LCDClock = function(drone, fgColor,bgColor,border) { bgColor = blocks.wool.black; } if ( typeof fgColor == 'undefined' ) { - fgColor = blocks.wool.white ; // white wool + fgColor = blocks.glowstone ; } if ( border ) { drone.box(border,21,9,1); drone.up().right(); } drone.blocktype('00:00', fgColor, bgColor, true); - return { - start24: function( ) { - function tick() { - var rolloverMins = 24*60, - mcTime = __plugin.canary ? world.totalTime : world.time, - timeOfDayInMins = Math.floor(((mcTime + 6000) % 24000) / 16.6667); - timeOfDayInMins = timeOfDayInMins % rolloverMins; - update( timeOfDayInMins ); - }; - intervalId = setInterval(tick, 800); - }, - stop24: function() { - clearInterval( intervalId ); - } - }; -}; + + function tick() { + var timeOfDayInMins = utils.time24(world); + update( timeOfDayInMins ); + } + intervalId = setInterval(tick, 800); + console.log('lcdclock started background task:' + intervalId); +}