forked from walterhiggins/ScriptCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
homes.js
403 lines (366 loc) · 11.3 KB
/
homes.js
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*************************************************************************
## homes Plugin
The homes plugin lets players set a location as home and return to the
location, invite other players to their home and also visit other
player's homes.
This module is a good example of how to create a javascript-based
minecraft mod which provides...
* A programmatic interface (API) and
* A command extension which uses that API to provide new functionality for players.
The module uses the `plugin()` function to specify an object and
methods, and the `command()` function to expose functionality to
players through a new `jsp home` command. This module also
demonstrates how to enable autocompletion for custom commands (to see
this in action, at the in-game prompt or server console prompt type
`jsp home ` then press the TAB key - you should see a list of further
possible options).
The `jsp home` command has the following options...
### Basic options
* `/jsp home set` Will set your current location as your
'home' location to which you can return at any time using the ...
* `/jsp home` ..command will return you to your home, if you have set one.
* `/jsp home {player}` Will take you to the home of {player} (where
{player} is the name of the player whose home you wish to visit.
* `/jsp home delete` Deletes your home location from the location
database. This does not actually remove the home from the world or
change the world in any way. This command is completely
non-destructive and cannot be used for griefing. No blocks will be
destroyed by this command.
### Social options
The following options allow players to open their homes to all or some
players, invite players to their home and see a list of homes they can
visit.
* `/jsp home list` Lists home which you can visit.
* `/jsp home ilist` Lists players who can visit your home.
* `/jsp home invite {player}` Invites the named player to your home.
* `/jsp home uninvite {player}` Uninvites (revokes invitation) the named player to your home.
* `/jsp home public` Opens your home to all players (all players can visit your home).
* `/jsp home private` Makes your home private (no longer visitable by all).
### Administration options
The following administration options can only be used by server operators...
* `/jsp home listall` List all of the homes
* `/jsp home clear {player}` Removes the player's home
location. Again, this command does not destroy any structures in
the world, it simply removes the location from the database. No
blocks are destroyed by this command.
***/
var utils = require('utils'),
bkTeleportCause = org.bukkit.event.player.PlayerTeleportEvent.TeleportCause,
bkBukkit = org.bukkit.Bukkit,
_store = {
houses: {},
openHouses: {},
invites: {}
};
/*
*/
var homes = plugin(
'homes',
{
help: function() {
return [
/* basic functions */
'/jsp home : Return to your own home',
'/jsp home {player} : Go to player home',
'/jsp home set : Set your current location as home',
'/jsp home delete : Delete your home location',
/* social */
'/jsp home list : List homes you can visit',
'/jsp home ilist : List players who can visit your home',
'/jsp home invite {player} : Invite {player} to your home',
'/jsp home uninvite {player} : Uninvite {player} to your home',
'/jsp home public : Open your home to all players',
'/jsp home private : Make your home private',
/* administration */
'/jsp home listall : Show all houses (ops only)',
'/jsp home clear {player} : Clears player home location (ops only)'
];
},
/* ========================================================================
basic functions
======================================================================== */
go: function(guest, host) {
var loc, homeLoc;
if (typeof host == 'undefined') {
host = guest;
}
guest = utils.player(guest);
host = utils.player(host);
loc = _store.houses[host.name];
if (!loc) {
echo(guest, host.name + ' has no home');
return;
}
if (!this._canVisit(guest, host)) {
echo(guest, 'You can not visit ' + host.name + "'s home yet");
return;
}
homeLoc = utils.locationFromJSON(loc);
guest.teleport(homeLoc, bkTeleportCause.PLUGIN);
},
/*
determine whether a guest is allow visit a host's home
*/
_canVisit: function(guest, host) {
var invitations, i;
if (guest == host) {
return true;
}
if (_store.openHouses[host.name]) {
return true;
}
invitations = _store.invites[host.name];
if (invitations) {
for (i = 0; i < invitations.length; i++) {
if (invitations[i] == guest.name) {
return true;
}
}
}
return false;
},
set: function(player) {
player = utils.player(player);
var loc = player.location;
_store.houses[player.name] = utils.locationToJSON(loc);
},
remove: function(player) {
player = utils.player(player);
delete _store.houses[player.name];
},
/* ========================================================================
social functions
======================================================================== */
/*
list homes which the player can visit
*/
list: function(player) {
var result = [],
ohp,
host,
guests,
i;
for (ohp in _store.openHouses) {
result.push(ohp);
}
player = utils.player(player);
for (host in _store.invites) {
guests = _store.invites[host];
for (i = 0; i < guests.length; i++) {
if (guests[i] == player.name) {
result.push(host);
}
}
}
return result;
},
/*
list who can visit the player home
*/
ilist: function(player) {
var result = [],
onlinePlayers,
i;
player = utils.player(player);
// if home is public - all players
if (_store.openHouses[player.name]) {
onlinePlayers = bkBukkit.getOnlinePlayers();
for (i = 0; i < onlinePlayers.length; i++) {
if (onlinePlayers[i].name != player.name) {
result.push(onlinePlayers[i].name);
}
}
} else {
if (_store.invites[player.name]) {
result = _store.invites[player.name];
} else {
result = [];
}
}
return result;
},
/*
Invite a player to the home
*/
invite: function(host, guest) {
host = utils.player(host);
guest = utils.player(guest);
var invitations = [];
if (_store.invites[host.name]) {
invitations = _store.invites[host.name];
}
invitations.push(guest.name);
_store.invites[host.name] = invitations;
echo(guest, host.name + ' has invited you to their home.');
echo(guest, 'type "/jsp home ' + host.name + '" to accept');
},
/*
Uninvite someone to the home
*/
uninvite: function(host, guest) {
var invitations, revisedInvites, i;
host = utils.player(host);
guest = utils.player(guest);
invitations = _store.invites[host.name];
if (!invitations) {
return;
}
revisedInvites = [];
for (i = 0; i < invitations.length; i++) {
if (invitations[i] != guest.name) {
revisedInvites.push(invitations[i]);
}
}
_store.invites[host.name] = revisedInvites;
},
/*
make the player house public
*/
open: function(player, optionalMsg) {
player = utils.player(player);
_store.openHouses[player.name] = true;
if (typeof optionalMsg != 'undefined') {
__plugin.server.broadcastMessage(optionalMsg);
}
},
/*
make the player house private
*/
close: function(player) {
player = utils.player(player);
delete _store.openHouses[player.name];
},
/* ========================================================================
admin functions
======================================================================== */
listall: function() {
var result = [];
for (var home in _store.houses) {
result.push(home);
}
return result;
},
clear: function(player) {
player = utils.player(player);
delete _store.houses[player.name];
delete _store.openHouses[player.name];
},
store: _store
},
true
);
exports.homes = homes;
/*
define a set of command options that can be used by players
*/
var options = {
set: function(params, sender) {
homes.set(sender);
},
delete: function(params, sender) {
homes.remove(sender);
},
help: function(params, sender) {
echo(sender, homes.help());
},
list: function(params, sender) {
var visitable = homes.list();
if (visitable.length == 0) {
echo(sender, 'There are no homes to visit');
return;
} else {
echo(sender, [
'You can visit any of these ' + visitable.length + ' homes',
visitable.join(', ')
]);
}
},
ilist: function(params, sender) {
var potentialVisitors = homes.ilist();
if (potentialVisitors.length == 0) {
echo(sender, 'No one can visit your home');
} else {
echo(sender, [
'These ' + potentialVisitors.length + 'players can visit your home',
potentialVisitors.join(', ')
]);
}
},
invite: function(params, sender) {
if (params.length == 1) {
echo(sender, 'You must provide a player name');
return;
}
var playerName = params[1];
var guest = utils.player(playerName);
if (!guest) {
echo(sender, playerName + ' is not here');
} else {
homes.invite(sender, guest);
}
},
uninvite: function(params, sender) {
if (params.length == 1) {
echo(sender, 'You must provide a player name');
return;
}
var playerName = params[1];
var guest = utils.player(playerName);
if (!guest) {
echo(sender, playerName + ' is not here');
} else {
homes.uninvite(sender, guest);
}
},
public: function(params, sender) {
homes.open(sender, params.slice(1).join(' '));
echo(sender, 'Your home is open to the public');
},
private: function(params, sender) {
homes.close(sender);
echo(sender, 'Your home is closed to the public');
},
listall: function(params, sender) {
if (!sender.isOp()) {
echo(sender, 'Only operators can do this');
} else {
echo(sender, homes.listall().join(', '));
}
},
clear: function(params, sender) {
if (!sender.isOp()) {
echo(sender, 'Only operators can do this');
} else {
homes.clear(params[1], sender);
}
}
};
var optionList = [];
for (var o in options) {
optionList.push(o);
}
/*
Expose a set of commands that players can use at the in-game command prompt
*/
command(
'home',
function(params, sender) {
var option, host;
if (params.length == 0) {
homes.go(sender, sender);
return;
}
option = options[params[0]];
if (option) {
option(params, sender);
} else {
host = utils.player(params[0]);
if (!host) {
echo(sender, params[0] + ' is not here');
} else {
homes.go(sender, host);
}
}
},
optionList
);