forked from walterhiggins/ScriptCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalias.js
More file actions
236 lines (203 loc) · 7.01 KB
/
Copy pathalias.js
File metadata and controls
236 lines (203 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*************************************************************************
## alias Plugin
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
To delete an alias ...
/jsp alias delete cw
... deletes the 'cw' alias from the appropriate alias map.
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).
***/
var _usage = "\
/jsp alias set {alias} = {comand-1} ;{command-2}\n \
/jsp alias global {alias} = {command-1} ; {command-2}\n \
/jsp alias list\n \
/jsp alias delete {alias}\n \
Create a new alias : \n \
/jsp alias set cw = time set {1} ; weather {2}\n \
Execute the alias : \n \
/cw 4000 sun \n \
...is the same as '/time set 4000' and '/weather sun'";
/*
persist aliases
*/
var _store = {
players: {},
global: {}
};
/*
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.
*/
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 _set = function(player, params){
var playerAliases = _store.players[player.name];
if (!playerAliases){
playerAliases = {};
}
var o = _processParams(params);
playerAliases[o.cmd] = o.aliases;
_store.players[player.name] = playerAliases;
player.sendMessage("Alias '" + o.cmd + "' created.");
};
var _delete = function(player, params){
if (_store.players[player.name] &&
_store.players[player.name][params[0]]){
delete _store.players[player.name][params[0]];
player.sendMessage("Alias '" + params[0] + "' deleted.");
}
else{
player.sendMessage("Alias '" + params[0] + "' does not exist.");
}
if (player.op){
if (_store.global[params[0]])
delete _store.global[params[0]];
}
};
var _global = function(player, params){
if (!player.op){
player.sendMessage("Only operators can set global aliases. " +
"You need to be an operator to perform this command.");
return;
}
var o = _processParams(params);
_store.global[o.cmd] = o.aliases;
player.sendMessage("Global alias '" + o.cmd + "' created.");
};
var _list = function(player){
try {
var alias = 0;
if (_store.players[player.name]){
player.sendMessage("Your aliases:");
for (alias in _store.players[player.name]){
player.sendMessage(alias + " = " +
JSON.stringify(_store.players[player.name][alias]));
}
}else{
player.sendMessage("You have no player-specific aliases.");
}
player.sendMessage("Global aliases:");
for (alias in _store.global){
player.sendMessage(alias + " = " + JSON.stringify(_store.global[alias]) );
}
}catch(e){
console.error("Error in list function: " + e.message);
throw e;
}
};
var alias = plugin('alias', {
"store": _store,
"set": _set,
"global": _global,
"delete": _delete,
"list": _list,
"help": function(player){ player.sendMessage("Usage:\n" + _usage);}
}, true );
var aliasCmd = command('alias', function(params,invoker){
var operation = params[0];
if (!operation){
invoker.sendMessage("Usage:\n" + _usage);
return;
}
if (alias[operation])
alias[operation](invoker, params.slice(1));
else
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 = [];
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
*/
if (_store.players[invoker] &&
_store.players[invoker][command])
{
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){
index = parseInt(index,10);
if (msgParts[index])
return msgParts[index]
else
return match;
});
cmds.push(filledinCommand);
}
for (var i = 0; i< cmds.length; i++){
exec(cmds[i]);
}
return isAlias;
};
/*
Intercept all command processing and replace with aliased commands if the
command about to be issued matches an alias.
*/
events.on('player.PlayerCommandPreprocessEvent', function(listener,evt){
var invoker = evt.player;
var exec = function(cmd){ invoker.performCommand(cmd);};
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); };
var isAlias = _intercept(''+evt.command, ''+ invoker.name, exec);
if (isAlias)
evt.command = "jsp void";
});