2014-02-04 21:53:59 +01:00
|
|
|
/*************************************************************************
|
|
|
|
## chat Plugin
|
|
|
|
|
|
|
|
This plugin lets players choose a text color to use when chatting. Players can list colors by typing...
|
|
|
|
|
|
|
|
/jsp list_colors
|
|
|
|
|
|
|
|
... and can set the color to use when chatting by typing...
|
|
|
|
|
|
|
|
/jsp chat_color {color}
|
|
|
|
|
|
|
|
... where {color} is one of the following colors...
|
|
|
|
|
|
|
|
* black
|
|
|
|
* blue
|
|
|
|
* darkgreen
|
|
|
|
* darkaqua
|
|
|
|
* darkred
|
|
|
|
* purple
|
|
|
|
* gold
|
|
|
|
* gray
|
|
|
|
* darkgray
|
|
|
|
* indigo
|
|
|
|
* brightgreen
|
|
|
|
* aqua
|
|
|
|
* red
|
|
|
|
* pink
|
|
|
|
* yellow
|
|
|
|
* white
|
|
|
|
|
|
|
|
This plugin's source code is useful to study because it is short and demonstrates use of the `plugin()`, and `command()` functions, persistence and event handling.
|
|
|
|
|
|
|
|
***/
|
2014-01-29 20:49:15 +01:00
|
|
|
var _store = { players: { } },
|
|
|
|
colorCodes = {},
|
|
|
|
i,
|
|
|
|
colors = [
|
|
|
|
'black',
|
|
|
|
'blue',
|
|
|
|
'darkgreen',
|
|
|
|
'darkaqua',
|
|
|
|
'darkred',
|
|
|
|
'purple',
|
|
|
|
'gold',
|
|
|
|
'gray',
|
|
|
|
'darkgray',
|
|
|
|
'indigo',
|
|
|
|
'brightgreen',
|
|
|
|
'aqua',
|
|
|
|
'red',
|
|
|
|
'pink',
|
|
|
|
'yellow',
|
|
|
|
'white'
|
|
|
|
],
|
|
|
|
foreach = require('utils').foreach;
|
|
|
|
|
2013-01-23 22:24:32 +01:00
|
|
|
/*
|
|
|
|
declare a new javascript plugin for changing chat text color
|
|
|
|
*/
|
2014-01-29 20:49:15 +01:00
|
|
|
exports.chat = plugin( 'chat', {
|
|
|
|
/*
|
|
|
|
set the color of text for a given player
|
|
|
|
*/
|
|
|
|
setColor: function( player, color ) {
|
|
|
|
_store.players[ player.name ] = color;
|
|
|
|
},
|
2013-12-24 01:18:43 +01:00
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
store: _store
|
2013-12-24 01:18:43 +01:00
|
|
|
|
2013-01-23 22:24:32 +01:00
|
|
|
},true);
|
|
|
|
|
2014-01-29 20:49:15 +01:00
|
|
|
foreach( colors, function ( color, i ) {
|
|
|
|
colorCodes[color] = i.toString( 16 );
|
|
|
|
} );
|
|
|
|
|
2014-04-26 21:20:10 +02:00
|
|
|
events.asyncPlayerChat( function( event ) {
|
2014-04-07 21:43:23 +02:00
|
|
|
var player = event.player;
|
2014-01-29 20:49:15 +01:00
|
|
|
var playerChatColor = _store.players[ player.name ];
|
|
|
|
if ( playerChatColor ) {
|
2014-04-07 21:43:23 +02:00
|
|
|
event.message = '§' + colorCodes[ playerChatColor ] + event.message;
|
2014-01-29 20:49:15 +01:00
|
|
|
}
|
2013-01-23 22:24:32 +01:00
|
|
|
});
|
2014-01-29 20:49:15 +01:00
|
|
|
|
|
|
|
var listColors = function( params, sender ) {
|
|
|
|
var colorNamesInColor = [];
|
|
|
|
foreach (colors, function( color ) {
|
|
|
|
colorNamesInColor.push( '§' + colorCodes[color] + color );
|
|
|
|
} );
|
|
|
|
sender.sendMessage( 'valid chat colors are ' + colorNamesInColor.join( ', ') );
|
2013-12-24 01:18:43 +01:00
|
|
|
};
|
2014-01-29 20:49:15 +01:00
|
|
|
|
|
|
|
command( 'list_colors', listColors );
|
|
|
|
command( 'chat_color', function( params, sender ) {
|
|
|
|
var color = params[0];
|
|
|
|
if ( colorCodes[color] ) {
|
|
|
|
chat.setColor( sender, color );
|
|
|
|
} else {
|
|
|
|
sender.sendMessage( color + ' is not a valid color' );
|
|
|
|
listColors();
|
|
|
|
}
|
|
|
|
}, colors );
|
2013-12-24 01:18:43 +01:00
|
|
|
|