2014-01-25 00:38:56 +01:00
|
|
|
'use strict';
|
2013-12-27 23:52:16 +01:00
|
|
|
/*************************************************************************
|
2014-01-04 19:39:49 +01:00
|
|
|
## alias Plugin
|
2013-12-24 01:18:43 +01:00
|
|
|
|
2013-12-27 23:52:16 +01:00
|
|
|
The alias module lets players and server admins create their own
|
|
|
|
per-player or global custom in-game command aliases.
|
|
|
|
|
|
|
|
### Examples
|
|
|
|
|
|
|
|
To set a command alias which is only visible to the current player
|
|
|
|
(per-player alias)...
|
|
|
|
|
|
|
|
/jsp alias set cw = time set {1} ; weather {2}
|
|
|
|
|
|
|
|
... Creates a new custom command only usable by the player who set
|
|
|
|
it called `cw` (short for set Clock and Weather) which when invoked...
|
|
|
|
|
|
|
|
/cw 4000 sun
|
|
|
|
|
|
|
|
... will perform the following commands...
|
|
|
|
|
|
|
|
/time set 4000
|
|
|
|
/weather sun
|
|
|
|
|
|
|
|
Aliases can use paramters as above. On the right hand side of the `=`, the
|
|
|
|
`{1}` refers to the first parameter provided with the `cw` alias, `{2}`
|
|
|
|
refers to the second parameter and so on. So `cw 4000 sun` is converted to
|
|
|
|
`time set 4000` and `weather sun`.
|
|
|
|
|
|
|
|
To set a global command alias usable by all (only operators can create
|
|
|
|
such an alias)...
|
|
|
|
|
|
|
|
/jsp alias global stormy = time 18000; weather storm
|
|
|
|
|
2014-01-23 00:57:27 +01:00
|
|
|
To remove an alias ...
|
2013-12-27 23:52:16 +01:00
|
|
|
|
2014-01-23 00:57:27 +01:00
|
|
|
/jsp alias remove cw
|
2013-12-27 23:52:16 +01:00
|
|
|
|
2014-01-23 00:57:27 +01:00
|
|
|
... removes the 'cw' alias from the appropriate alias map.
|
2013-12-27 23:52:16 +01:00
|
|
|
|
|
|
|
To get a list of aliases currently defined...
|
|
|
|
|
|
|
|
/jsp alias list
|
|
|
|
|
|
|
|
To get help on the `jsp alias` command:
|
|
|
|
|
|
|
|
/jsp alias help
|
|
|
|
|
|
|
|
Aliases can be used at the in-game prompt by players or in the server
|
|
|
|
console. Aliases will not be able to avail of command autocompletion
|
|
|
|
(pressing the TAB key will have no effect).
|
|
|
|
|
|
|
|
***/
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
var _usage = '\
|
2013-12-27 23:52:16 +01:00
|
|
|
/jsp alias set {alias} = {comand-1} ;{command-2}\n \
|
|
|
|
/jsp alias global {alias} = {command-1} ; {command-2}\n \
|
|
|
|
/jsp alias list\n \
|
2014-01-23 00:57:27 +01:00
|
|
|
/jsp alias remove {alias}\n \
|
2013-12-27 23:52:16 +01:00
|
|
|
Create a new alias : \n \
|
|
|
|
/jsp alias set cw = time set {1} ; weather {2}\n \
|
|
|
|
Execute the alias : \n \
|
|
|
|
/cw 4000 sun \n \
|
2014-01-29 20:49:15 +01:00
|
|
|
...is the same as \'/time set 4000\' and \'/weather sun\'';
|
2013-12-27 23:52:16 +01:00
|
|
|
/*
|
|
|
|
persist aliases
|
|
|
|
*/
|
|
|
|
var _store = {
|
2014-01-25 00:38:56 +01:00
|
|
|
players: {},
|
|
|
|
global: {}
|
2013-12-27 23:52:16 +01:00
|
|
|
};
|
|
|
|
/*
|
|
|
|
turns 'cw = time set {1} ; weather {2}' into {cmd: 'cw', aliases: ['time set {1}', 'weather {2}']}
|
|
|
|
_processParams is a private function which takes an array of parameters
|
|
|
|
used for the 'set' and 'global' options.
|
|
|
|
*/
|
2014-01-29 20:49:15 +01:00
|
|
|
var _processParams = function( params ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
var paramStr = params.join(' '),
|
|
|
|
eqPos = paramStr.indexOf('='),
|
2014-01-29 20:49:15 +01:00
|
|
|
aliasCmd = paramStr.substring( 0, eqPos).trim(),
|
|
|
|
aliasValue = paramStr.substring( eqPos + 1 ).trim();
|
2014-01-25 00:38:56 +01:00
|
|
|
return {
|
|
|
|
cmd: aliasCmd,
|
2014-01-29 20:49:15 +01:00
|
|
|
aliases: aliasValue.split( /\s*;\s*/ )
|
2014-01-25 00:38:56 +01:00
|
|
|
};
|
2013-12-27 23:52:16 +01:00
|
|
|
};
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
var _set = function( params, player ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
var playerAliases = _store.players[player.name];
|
2014-01-29 20:49:15 +01:00
|
|
|
if (!playerAliases ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
playerAliases = {};
|
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
var o = _processParams( params );
|
2014-01-25 00:38:56 +01:00
|
|
|
playerAliases[o.cmd] = o.aliases;
|
|
|
|
_store.players[player.name] = playerAliases;
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( player, 'Alias ' + o.cmd + ' created.' );
|
2013-12-27 23:52:16 +01:00
|
|
|
};
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
var _remove = function( params, player ) {
|
|
|
|
if ( _store.players[player.name] && _store.players[player.name][ params[0] ] ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
delete _store.players[player.name][params[0]];
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( player, 'Alias ' + params[0] + ' removed.' );
|
2014-01-25 00:38:56 +01:00
|
|
|
}
|
|
|
|
else{
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( player, 'Alias ' + params[0] + ' does not exist.' );
|
2014-01-25 00:38:56 +01:00
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( player.op ) {
|
|
|
|
if ( _store.global[params[0]] ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
delete _store.global[params[0]];
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
2014-01-25 00:38:56 +01:00
|
|
|
}
|
2013-12-27 23:52:16 +01:00
|
|
|
};
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
var _global = function( params, player ) {
|
|
|
|
if ( !player.op ) {
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( player, 'Only operators can set global aliases. ' +
|
|
|
|
'You need to be an operator to perform this command.' );
|
2014-01-25 00:38:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
var o = _processParams( params );
|
2014-01-25 00:38:56 +01:00
|
|
|
_store.global[o.cmd] = o.aliases;
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( player, 'Global alias ' + o.cmd + ' created.' );
|
2013-12-27 23:52:16 +01:00
|
|
|
};
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
var _list = function( params, player ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
var alias = 0;
|
|
|
|
try {
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( _store.players[player.name] ) {
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( player, 'Your aliases:');
|
2014-01-29 20:49:15 +01:00
|
|
|
for ( alias in _store.players[player.name] ) {
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( player, alias + ' = ' +
|
|
|
|
JSON.stringify( _store.players[player.name][alias] ) );
|
2014-01-25 00:38:56 +01:00
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
} else {
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( player, 'You have no player-specific aliases.' );
|
2014-01-25 00:38:56 +01:00
|
|
|
}
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( player, 'Global aliases:' );
|
2014-01-29 20:49:15 +01:00
|
|
|
for ( alias in _store.global ) {
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( player, alias + ' = ' + JSON.stringify( _store.global[alias] ) );
|
2013-01-26 18:49:11 +01:00
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
} catch( e ) {
|
|
|
|
console.error( 'Error in list function: ' + e.message );
|
2014-01-25 00:38:56 +01:00
|
|
|
throw e;
|
|
|
|
}
|
2013-12-27 23:52:16 +01:00
|
|
|
};
|
2014-01-29 20:49:15 +01:00
|
|
|
var _help = function( params, player ) {
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( player, 'Usage:\n' + _usage );
|
2014-01-23 00:57:27 +01:00
|
|
|
};
|
2014-01-25 00:38:56 +01:00
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
var alias = plugin( 'alias', {
|
2014-01-25 00:38:56 +01:00
|
|
|
store: _store,
|
|
|
|
set: _set,
|
|
|
|
global: _global,
|
|
|
|
remove: _remove,
|
|
|
|
list: _list,
|
|
|
|
help: _help
|
2013-12-27 23:52:16 +01:00
|
|
|
}, true );
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
var aliasCmd = command( 'alias', function( params, invoker ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
var operation = params[0],
|
|
|
|
fn;
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( !operation ) {
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( invoker, 'Usage:\n' + _usage );
|
2014-01-25 00:38:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
wph 20140122 this is kind of dumb but Nashorn has some serious problems
|
|
|
|
accessing object properties by array index notation
|
|
|
|
in JRE8 alias[operation] returns null - definitely a bug in Nashorn.
|
|
|
|
*/
|
2014-01-29 20:49:15 +01:00
|
|
|
for ( var key in alias ) {
|
|
|
|
if ( key == operation ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
fn = alias[key];
|
2014-01-29 20:49:15 +01:00
|
|
|
fn( params.slice(1), invoker );
|
2014-01-25 00:38:56 +01:00
|
|
|
return;
|
2014-01-23 00:57:27 +01:00
|
|
|
}
|
2014-01-25 00:38:56 +01:00
|
|
|
}
|
2014-09-30 00:42:41 +02:00
|
|
|
echo( invoker, 'Usage:\n' + _usage );
|
2013-12-27 23:52:16 +01:00
|
|
|
});
|
|
|
|
|
2014-04-25 21:51:15 +02:00
|
|
|
var _intercept = function( msg, invoker, exec ) {
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( msg.trim().length == 0 )
|
2014-01-25 00:38:56 +01:00
|
|
|
return false;
|
|
|
|
var msgParts = msg.split(' '),
|
2014-01-29 20:49:15 +01:00
|
|
|
command = msg.match( /^\/*([^\s]+)/ )[1],
|
|
|
|
template = [],
|
|
|
|
isAlias = false,
|
|
|
|
cmds = [],
|
2014-01-25 00:38:56 +01:00
|
|
|
commandObj,
|
2014-01-29 20:49:15 +01:00
|
|
|
filledinCommand,
|
|
|
|
i;
|
2014-01-25 00:38:56 +01:00
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( _store.global[command] ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
template = _store.global[command];
|
|
|
|
isAlias = true;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
allows player-specific aliases to override global aliases
|
|
|
|
*/
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( _store.players[invoker] && _store.players[invoker][command] ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
template = _store.players[invoker][command];
|
|
|
|
isAlias = true;
|
|
|
|
}
|
2014-01-29 20:49:15 +01:00
|
|
|
for ( i = 0; i < template.length; i++) {
|
|
|
|
filledinCommand = template[i].replace( /{([0-9]+)}/g, function( match, index ) {
|
|
|
|
index = parseInt( index, 10 );
|
|
|
|
if ( msgParts[index] ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
return msgParts[index];
|
2014-01-29 20:49:15 +01:00
|
|
|
} else {
|
2014-01-25 00:38:56 +01:00
|
|
|
return match;
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
2014-01-25 00:38:56 +01:00
|
|
|
});
|
2014-01-29 20:49:15 +01:00
|
|
|
cmds.push( filledinCommand );
|
2014-01-25 00:38:56 +01:00
|
|
|
}
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
for (i = 0; i< cmds.length; i++ ) {
|
|
|
|
exec( cmds[i] );
|
2014-01-25 00:38:56 +01:00
|
|
|
}
|
|
|
|
return isAlias;
|
2013-12-27 23:52:16 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
Intercept all command processing and replace with aliased commands if the
|
|
|
|
command about to be issued matches an alias.
|
|
|
|
*/
|
2014-09-30 00:42:41 +02:00
|
|
|
if (__plugin.canary){
|
|
|
|
console.warn('alias plugin is not yet supported in CanaryMod');
|
|
|
|
return;
|
|
|
|
}
|
2014-04-26 21:20:10 +02:00
|
|
|
events.playerCommandPreprocess( function( evt ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
var invoker = evt.player;
|
2014-01-29 20:49:15 +01:00
|
|
|
var exec = function( cmd ) {
|
|
|
|
invoker.performCommand(cmd);
|
|
|
|
};
|
2014-04-25 21:51:15 +02:00
|
|
|
var isAlias = _intercept( (''+evt.message).trim(), ''+invoker.name, exec);
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( isAlias ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
evt.cancelled = true;
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
2013-12-27 23:52:16 +01:00
|
|
|
});
|
|
|
|
/* define a 'void' command because ServerCommandEvent can't be canceled */
|
2014-01-29 20:49:15 +01:00
|
|
|
command('void',function( ) {
|
|
|
|
} );
|
2014-01-25 00:38:56 +01:00
|
|
|
|
2014-04-26 21:20:10 +02:00
|
|
|
events.serverCommand( function( evt ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
var invoker = evt.sender;
|
2014-01-29 20:49:15 +01:00
|
|
|
var exec = function( cmd ) {
|
|
|
|
invoker.server.dispatchCommand( invoker, cmd);
|
|
|
|
};
|
2014-04-25 21:51:15 +02:00
|
|
|
var isAlias = _intercept( (''+evt.command).trim(), ''+ invoker.name, exec );
|
2014-01-29 20:49:15 +01:00
|
|
|
if ( isAlias ) {
|
2014-01-25 00:38:56 +01:00
|
|
|
evt.command = 'jsp void';
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
2013-12-27 23:52:16 +01:00
|
|
|
});
|