Added experimental LCDGameClock
This commit is contained in:
parent
f71d1a4e78
commit
8c690452e7
6 changed files with 390 additions and 309 deletions
|
@ -1,8 +1,8 @@
|
|||
'use strict';
|
||||
var console = require('./console');
|
||||
var File = java.io.File;
|
||||
var FileWriter = java.io.FileWriter;
|
||||
var PrintWriter = java.io.PrintWriter;
|
||||
var console = require('./console'),
|
||||
File = java.io.File,
|
||||
FileWriter = java.io.FileWriter,
|
||||
PrintWriter = java.io.PrintWriter;
|
||||
/*
|
||||
plugin management
|
||||
*/
|
||||
|
|
|
@ -174,12 +174,12 @@ When resolving module names to file paths, ScriptCraft uses the following rules.
|
|||
wph 20131215 Experimental
|
||||
*/
|
||||
var _loadedModules = {};
|
||||
|
||||
var _format = java.lang.String.format;
|
||||
var _require = function(parentFile, path)
|
||||
{
|
||||
var file = resolveModuleToFile(path, parentFile);
|
||||
if (!file){
|
||||
var errMsg = '' + java.lang.String.format("require() failed to find matching file for module '%s' " +
|
||||
var errMsg = '' + _format("require() failed to find matching file for module '%s' " +
|
||||
"in working directory '%s' ", [path, parentFile.canonicalPath]);
|
||||
if (! ( (''+path).match(/^\./) )){
|
||||
errMsg = errMsg + ' and not found in paths ' + JSON.stringify(modulePaths);
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
'use strict';
|
||||
/*************************************************************************
|
||||
## sc-mqtt module
|
||||
|
||||
|
@ -86,11 +87,12 @@ function Client(brokerUrl, clientId){
|
|||
brokerUrl = 'tcp://localhost:1883';
|
||||
}
|
||||
if (!clientId){
|
||||
clientId = 'scriptcraft';
|
||||
clientId = 'scriptcraft' + new Date().getTime();
|
||||
}
|
||||
var client = new MqttClient(brokerUrl, clientId, null);
|
||||
client.setCallback(callback);
|
||||
return {
|
||||
|
||||
connect: function(options){
|
||||
if (typeof options === 'undefined'){
|
||||
client.connect();
|
||||
|
@ -99,6 +101,15 @@ function Client(brokerUrl, clientId){
|
|||
}
|
||||
return client;
|
||||
},
|
||||
|
||||
disconnect: function(quiesceTimeout){
|
||||
if (typeof quiesceTimeout == 'undefined')
|
||||
client.disconnect();
|
||||
else
|
||||
client.disconnect(quiesceTimeout);
|
||||
return client;
|
||||
},
|
||||
|
||||
publish: function(topic, message, qos, retained){
|
||||
if (typeof message == 'string'){
|
||||
message = new java.lang.String(message).bytes;
|
||||
|
@ -112,28 +123,33 @@ function Client(brokerUrl, clientId){
|
|||
client.publish(topic, message,qos, retained);
|
||||
return client;
|
||||
},
|
||||
|
||||
subscribe: function(topic){
|
||||
client.subscribe(topic);
|
||||
return client;
|
||||
},
|
||||
|
||||
unsubscribe: function(topic){
|
||||
client.unsubscribe(topic);
|
||||
return client;
|
||||
},
|
||||
|
||||
onMessageArrived: function(fn){
|
||||
callback.setMesgArrived(fn);
|
||||
return client;
|
||||
},
|
||||
|
||||
onDeliveryComplete: function(fn){
|
||||
callback.setDeliveryComplete(fn);
|
||||
return client;
|
||||
},
|
||||
|
||||
onConnectionLost: function(fn){
|
||||
callback.setConnLost(fn);
|
||||
return client;
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
exports.client = function(brokerUrl, clientId, options){
|
||||
if (typeof org.walterhiggins.scriptcraft.ScriptCraftMqttCallback != 'function'){
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
'use strict';
|
||||
/*************************************************************************
|
||||
## alias Plugin
|
||||
|
||||
|
@ -74,11 +75,14 @@ var _store = {
|
|||
used for the 'set' and 'global' options.
|
||||
*/
|
||||
var _processParams = function(params){
|
||||
var paramStr = params.join(' ');
|
||||
var eqPos = paramStr.indexOf('=');
|
||||
var aliasCmd = paramStr.substring(0,eqPos).trim();
|
||||
var aliasValue = paramStr.substring(eqPos+1).trim();
|
||||
return { cmd: aliasCmd, aliases: aliasValue.split(/\s*;\s*/) };
|
||||
var paramStr = params.join(' '),
|
||||
eqPos = paramStr.indexOf('='),
|
||||
aliasCmd = paramStr.substring(0,eqPos).trim(),
|
||||
aliasValue = paramStr.substring(eqPos+1).trim();
|
||||
return {
|
||||
cmd: aliasCmd,
|
||||
aliases: aliasValue.split(/\s*;\s*/)
|
||||
};
|
||||
};
|
||||
|
||||
var _set = function(params, player){
|
||||
|
@ -119,8 +123,8 @@ var _global = function(params, player){
|
|||
};
|
||||
|
||||
var _list = function(params, player){
|
||||
try {
|
||||
var alias = 0;
|
||||
try {
|
||||
if (_store.players[player.name]){
|
||||
player.sendMessage("Your aliases:");
|
||||
for (alias in _store.players[player.name]){
|
||||
|
@ -142,6 +146,7 @@ var _list = function(params, player){
|
|||
var _help = function(params, player){
|
||||
player.sendMessage('Usage:\n' + _usage);
|
||||
};
|
||||
|
||||
var alias = plugin('alias', {
|
||||
store: _store,
|
||||
set: _set,
|
||||
|
@ -152,7 +157,8 @@ var alias = plugin('alias', {
|
|||
}, true );
|
||||
|
||||
var aliasCmd = command('alias', function( params, invoker ) {
|
||||
var operation = params[0], fn;
|
||||
var operation = params[0],
|
||||
fn;
|
||||
if (!operation){
|
||||
invoker.sendMessage('Usage:\n' + _usage);
|
||||
return;
|
||||
|
@ -162,39 +168,29 @@ var aliasCmd = command('alias', function( params, invoker ) {
|
|||
accessing object properties by array index notation
|
||||
in JRE8 alias[operation] returns null - definitely a bug in Nashorn.
|
||||
*/
|
||||
if (operation == 'set'){
|
||||
alias.set(params.slice(1), invoker);
|
||||
}else if (operation == 'global'){
|
||||
alias.global(params.slice(1), invoker);
|
||||
}else if (operation == 'remove'){
|
||||
alias.remove(params.slice(1), invoker);
|
||||
}else if (operation == 'list'){
|
||||
alias.list(params.slice(1), invoker);
|
||||
}else if (operation == 'help'){
|
||||
alias.help(params.slice(1), invoker);
|
||||
}else {
|
||||
invoker.sendMessage('Usage:\n' + _usage);
|
||||
for (var key in alias){
|
||||
if (key == operation){
|
||||
fn = alias[key];
|
||||
fn(params.slice(1),invoker);
|
||||
return;
|
||||
}
|
||||
}
|
||||
invoker.sendMessage('Usage:\n' + _usage);
|
||||
});
|
||||
|
||||
var _intercept = function( msg, invoker, exec)
|
||||
{
|
||||
if (msg.trim().length == 0)
|
||||
return false;
|
||||
var msgParts = msg.split(' ');
|
||||
var command = msg.match(/^\/*([^\s]+)/)[1];
|
||||
|
||||
var template = [], isAlias = false, cmds = [];
|
||||
var msgParts = msg.split(' '),
|
||||
command = msg.match(/^\/*([^\s]+)/)[1],
|
||||
template = [], isAlias = false, cmds = [],
|
||||
commandObj,
|
||||
filledinCommand;
|
||||
|
||||
if (_store.global[command]){
|
||||
template = _store.global[command];
|
||||
isAlias = true;
|
||||
}else{
|
||||
if (config.verbose){
|
||||
var commandObj = server.commandMap.getCommand(command);
|
||||
if (!commandObj)
|
||||
console.info('No global alias found for command: ' + command);
|
||||
}
|
||||
}
|
||||
/*
|
||||
allows player-specific aliases to override global aliases
|
||||
|
@ -204,19 +200,13 @@ var _intercept = function( msg, invoker, exec)
|
|||
{
|
||||
template = _store.players[invoker][command];
|
||||
isAlias = true;
|
||||
}else{
|
||||
if (config.verbose){
|
||||
var commandObj = server.commandMap.getCommand(command);
|
||||
if (!commandObj)
|
||||
console.info('No player alias found for command: ' + command);
|
||||
}
|
||||
}
|
||||
for (var i = 0;i < template.length; i++)
|
||||
{
|
||||
var filledinCommand = template[i].replace(/{([0-9]+)}/g, function (match,index){
|
||||
filledinCommand = template[i].replace(/{([0-9]+)}/g, function (match,index){
|
||||
index = parseInt(index,10);
|
||||
if (msgParts[index])
|
||||
return msgParts[index]
|
||||
return msgParts[index];
|
||||
else
|
||||
return match;
|
||||
});
|
||||
|
@ -239,10 +229,10 @@ events.on('player.PlayerCommandPreprocessEvent', function(listener,evt){
|
|||
var isAlias = _intercept(''+evt.message, ''+invoker.name, exec);
|
||||
if (isAlias)
|
||||
evt.cancelled = true;
|
||||
|
||||
});
|
||||
/* define a 'void' command because ServerCommandEvent can't be canceled */
|
||||
command('void',function(){});
|
||||
|
||||
events.on('server.ServerCommandEvent', function(listener,evt){
|
||||
var invoker = evt.sender;
|
||||
var exec = function(cmd){ invoker.server.dispatchCommand(invoker, cmd); };
|
||||
|
|
75
src/main/javascript/plugins/drone/contrib/lcd-clock.js
Normal file
75
src/main/javascript/plugins/drone/contrib/lcd-clock.js
Normal file
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
Experimental:
|
||||
Point at a block and issue the following ...
|
||||
/js var d = new Drone();
|
||||
/js var clock = new LCDClock(d);
|
||||
/js clock.start24();
|
||||
... start the clock...
|
||||
/js clock.stop24();
|
||||
... stops the clock...
|
||||
*/
|
||||
var Drone = require('../drone').Drone;
|
||||
var blocktype = require('../blocktype');
|
||||
|
||||
|
||||
exports.LCDClock = function(drone, fgColor,bgColor,border) {
|
||||
var lastSecs = [0,0,0,0],
|
||||
world = drone.world,
|
||||
intervalId = -1;
|
||||
|
||||
if (typeof bgColor == 'undefined')
|
||||
bgColor = '35:15'; // black wool
|
||||
|
||||
if (typeof fgColor == 'undefined')
|
||||
fgColor = 35 ; // white wool
|
||||
|
||||
if (border){
|
||||
drone.box(border,21,9,1);
|
||||
drone.up().right();
|
||||
}
|
||||
drone.blocktype('00:00',fgColor,bgColor);
|
||||
return {
|
||||
start24: function(){
|
||||
var clock = this;
|
||||
function tick(){
|
||||
var rolloverMins = 24*60;
|
||||
var timeOfDayInMins = Math.floor(((world.time + 6000) % 24000) / 16.6667);
|
||||
timeOfDayInMins = timeOfDayInMins % rolloverMins;
|
||||
console.log('Minecraft time: ' + world.time + ' timeOfDayInMins: ' + timeOfDayInMins);
|
||||
clock.update(timeOfDayInMins);
|
||||
};
|
||||
intervalId = setInterval(tick, 800);
|
||||
},
|
||||
stop24: function(){
|
||||
clearInterval(intervalId);
|
||||
},
|
||||
update: function(secs){
|
||||
var digits = [0,0,0,0],
|
||||
s = secs % 60;
|
||||
m = (secs - s) / 60;
|
||||
digits[3] = s%10;
|
||||
digits[2] = (s-digits[3])/10;
|
||||
digits[1] = m%10;
|
||||
digits[0] = (m-digits[1])/10;
|
||||
//
|
||||
// updating all 4 digits each time is expensive
|
||||
// only update digits which have changed (in most cases - just 1)
|
||||
//
|
||||
if (digits[3] != lastSecs[3])
|
||||
drone.right(14).blocktype(''+digits[3],fgColor,bgColor).left(14);
|
||||
if (digits[2] != lastSecs[2])
|
||||
drone.right(10).blocktype(''+digits[2],fgColor,bgColor).left(10);
|
||||
if (digits[1] != lastSecs[1])
|
||||
drone.right(4).blocktype(''+digits[1], fgColor, bgColor).left(4);
|
||||
if (digits[0] != lastSecs[0])
|
||||
drone.blocktype(''+digits[0], fgColor, bgColor);
|
||||
|
||||
lastSecs[0] = digits[0];
|
||||
lastSecs[1] = digits[1];
|
||||
lastSecs[2] = digits[2];
|
||||
lastSecs[3] = digits[3];
|
||||
|
||||
}
|
||||
};
|
||||
};
|
||||
|
Reference in a new issue