2013-12-28 23:49:13 +01:00
|
|
|
/*************************************************************************
|
|
|
|
## homes Module
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
***/
|
2013-12-24 01:18:43 +01:00
|
|
|
var utils = require('utils');
|
|
|
|
var _store = {
|
|
|
|
houses: {},
|
|
|
|
openHouses: {},
|
|
|
|
invites: {}
|
|
|
|
};
|
2013-01-19 18:01:59 +01:00
|
|
|
/*
|
|
|
|
*/
|
2013-12-24 01:18:43 +01:00
|
|
|
var homes = plugin("homes", {
|
2013-01-20 22:02:56 +01:00
|
|
|
help: function(){
|
|
|
|
return [
|
|
|
|
/* basic functions */
|
|
|
|
"/jsp home : Return to your own home",
|
|
|
|
"/jsp home <player> : Go to player's home",
|
|
|
|
"/jsp home set : Set your current location as home",
|
|
|
|
"/jsp home delete : Delete your home location",
|
2013-01-19 18:01:59 +01:00
|
|
|
|
2013-01-20 22:02:56 +01:00
|
|
|
/* 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",
|
2013-01-19 18:01:59 +01:00
|
|
|
|
2013-01-20 22:02:56 +01:00
|
|
|
/* administration */
|
|
|
|
"/jsp home listall : Show all houses (ops only)",
|
|
|
|
"/jsp home clear <player> : Clears player's home location (ops only)"
|
|
|
|
];
|
|
|
|
},
|
|
|
|
/* ========================================================================
|
|
|
|
basic functions
|
|
|
|
======================================================================== */
|
2013-01-19 18:01:59 +01:00
|
|
|
|
2013-01-20 22:02:56 +01:00
|
|
|
go: function(guest, host){
|
|
|
|
if (typeof host == "undefined")
|
|
|
|
host = guest;
|
2013-12-31 19:21:40 +01:00
|
|
|
guest = utils.player(guest);
|
|
|
|
host = utils.player(host);
|
2013-12-24 01:18:43 +01:00
|
|
|
var loc = _store.houses[host.name];
|
2013-01-20 22:02:56 +01:00
|
|
|
if (!loc){
|
|
|
|
guest.sendMessage(host.name + " has no home");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this._canVisit(guest,host)){
|
|
|
|
guest.sendMessage("You can't visit " + host.name + "'s home yet");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var teleportCause = org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
|
2013-12-31 19:21:40 +01:00
|
|
|
var homeLoc = utils.locationFromJSON(loc);
|
2013-01-20 22:02:56 +01:00
|
|
|
guest.teleport(homeLoc, teleportCause.PLUGIN);
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
determine whether a guest is allow visit a host's home
|
|
|
|
*/
|
|
|
|
_canVisit: function(guest, host){
|
|
|
|
if (guest == host)
|
|
|
|
return true;
|
2013-12-24 01:18:43 +01:00
|
|
|
if (_store.openHouses[host.name])
|
2013-01-20 22:02:56 +01:00
|
|
|
return true;
|
2013-12-24 01:18:43 +01:00
|
|
|
var invitations = _store.invites[host.name];
|
2013-01-20 22:02:56 +01:00
|
|
|
if (invitations)
|
|
|
|
for (var i = 0;i < invitations.length;i++)
|
|
|
|
if (invitations[i] == guest.name)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
set: function(player){
|
2013-12-31 19:21:40 +01:00
|
|
|
player = utils.player(player);
|
2013-01-20 22:02:56 +01:00
|
|
|
var loc = player.location;
|
2013-12-31 19:21:40 +01:00
|
|
|
_store.houses[player.name] = utils.locationToJSON(loc);
|
2013-01-20 22:02:56 +01:00
|
|
|
},
|
|
|
|
remove: function(player){
|
2013-12-31 19:21:40 +01:00
|
|
|
player = utils.player(player);
|
2013-12-24 01:18:43 +01:00
|
|
|
delete _store.houses[player.name];
|
2013-01-20 22:02:56 +01:00
|
|
|
},
|
|
|
|
/* ========================================================================
|
|
|
|
social functions
|
|
|
|
======================================================================== */
|
|
|
|
|
|
|
|
/*
|
|
|
|
list homes which the player can visit
|
|
|
|
*/
|
|
|
|
list: function(player){
|
|
|
|
var result = [];
|
2013-12-24 01:18:43 +01:00
|
|
|
for (var ohp in _store.openHouses)
|
2013-01-20 22:02:56 +01:00
|
|
|
result.push(ohp);
|
2013-12-31 19:21:40 +01:00
|
|
|
player = utils.player(player);
|
2013-12-24 01:18:43 +01:00
|
|
|
for (var host in _store.invites){
|
|
|
|
var guests = _store.invites[host];
|
2013-01-20 22:02:56 +01:00
|
|
|
for (var i = 0;i < guests.length; i++)
|
|
|
|
if (guests[i] == player.name)
|
|
|
|
result.push(host);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
list who can visit the player's home
|
|
|
|
*/
|
|
|
|
ilist: function(player){
|
2013-12-31 19:21:40 +01:00
|
|
|
player = utils.player(player);
|
2013-01-20 22:02:56 +01:00
|
|
|
var result = [];
|
|
|
|
// if home is public - all players
|
2013-12-24 01:18:43 +01:00
|
|
|
if (_store.openHouses[player.name]){
|
2013-01-20 22:02:56 +01:00
|
|
|
var online = org.bukkit.Bukkit.getOnlinePlayers();
|
|
|
|
for (var i = 0;i < online.length; i++)
|
|
|
|
if (online[i].name != player.name)
|
|
|
|
result.push(online[i].name);
|
|
|
|
}else{
|
2013-12-24 01:18:43 +01:00
|
|
|
if (_store.invites[player.name])
|
|
|
|
result = _store.invites[player.name];
|
2013-12-08 13:16:41 +01:00
|
|
|
else
|
|
|
|
result = [];
|
2013-01-20 22:02:56 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
Invite a player to the home
|
|
|
|
*/
|
|
|
|
invite: function(host, guest){
|
2013-12-31 19:21:40 +01:00
|
|
|
host = utils.player(host);
|
|
|
|
guest = utils.player(guest);
|
2013-12-08 13:16:41 +01:00
|
|
|
var invitations = [];
|
2013-12-24 01:18:43 +01:00
|
|
|
if (_store.invites[host.name])
|
|
|
|
invitations = _store.invites[host.name];
|
2013-01-20 22:02:56 +01:00
|
|
|
invitations.push(guest.name);
|
2013-12-24 01:18:43 +01:00
|
|
|
_store.invites[host.name] = invitations;
|
2013-01-20 22:02:56 +01:00
|
|
|
guest.sendMessage(host.name + " has invited you to their home.");
|
|
|
|
guest.sendMessage("type '/jsp home " + host.name + "' to accept");
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
Uninvite someone to the home
|
|
|
|
*/
|
|
|
|
uninvite: function(host, guest){
|
2013-12-31 19:21:40 +01:00
|
|
|
host = utils.player(host);
|
|
|
|
guest = utils.player(guest);
|
2013-12-24 01:18:43 +01:00
|
|
|
var invitations = _store.invites[host.name];
|
2013-01-20 22:02:56 +01:00
|
|
|
if (!invitations)
|
|
|
|
return;
|
|
|
|
var revisedInvites = [];
|
|
|
|
for (var i =0;i < invitations.length; i++)
|
|
|
|
if (invitations[i] != guest.name)
|
|
|
|
revisedInvites.push(invitations[i]);
|
2013-12-24 01:18:43 +01:00
|
|
|
_store.invites[host.name] = revisedInvites;
|
2013-01-20 22:02:56 +01:00
|
|
|
},
|
|
|
|
/*
|
|
|
|
make the player's house public
|
|
|
|
*/
|
|
|
|
open: function(player, optionalMsg){
|
2013-12-31 19:21:40 +01:00
|
|
|
player = utils.player(player);
|
2013-12-24 01:18:43 +01:00
|
|
|
_store.openHouses[player.name] = true;
|
2013-01-20 22:02:56 +01:00
|
|
|
if (typeof optionalMsg != "undefined")
|
|
|
|
__plugin.server.broadcastMessage(optionalMsg);
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
make the player's house private
|
|
|
|
*/
|
|
|
|
close: function(player){
|
2013-12-31 19:21:40 +01:00
|
|
|
player = utils.player(player);
|
2013-12-24 01:18:43 +01:00
|
|
|
delete _store.openHouses[player.name];
|
2013-01-20 22:02:56 +01:00
|
|
|
},
|
|
|
|
/* ========================================================================
|
|
|
|
admin functions
|
|
|
|
======================================================================== */
|
|
|
|
listall: function(){
|
|
|
|
var result = [];
|
2013-12-24 01:18:43 +01:00
|
|
|
for (var home in _store.houses)
|
2013-01-20 22:02:56 +01:00
|
|
|
result.push(home);
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
clear: function(player){
|
2013-12-31 19:21:40 +01:00
|
|
|
player = utils.player(player);
|
2013-12-24 01:18:43 +01:00
|
|
|
delete _store.houses[player.name];
|
|
|
|
delete _store.openHouses[player.name];
|
|
|
|
},
|
|
|
|
store: _store
|
2013-01-19 01:45:33 +01:00
|
|
|
}, true);
|
2013-12-24 01:18:43 +01:00
|
|
|
|
|
|
|
exports.homes = homes;
|
|
|
|
|
|
|
|
/*
|
|
|
|
define a set of command options that can be used by players
|
2013-01-19 01:45:33 +01:00
|
|
|
*/
|
2013-12-24 01:18:43 +01:00
|
|
|
var options = {
|
2013-12-31 19:21:40 +01:00
|
|
|
'set': function(params, sender){ homes.set(sender); },
|
|
|
|
'delete': function(params, sender ){ homes.remove(sender);},
|
2013-12-30 22:33:12 +01:00
|
|
|
'help': function(params, sender){ sender.sendMessage(homes.help());},
|
|
|
|
'list': function(params, sender){
|
2013-12-24 01:18:43 +01:00
|
|
|
var visitable = homes.list();
|
|
|
|
if (visitable.length == 0){
|
2013-12-30 22:33:12 +01:00
|
|
|
sender.sendMessage("There are no homes to visit");
|
2013-12-24 01:18:43 +01:00
|
|
|
return;
|
|
|
|
}else{
|
2013-12-30 22:33:12 +01:00
|
|
|
sender.sendMessage([
|
2013-12-24 01:18:43 +01:00
|
|
|
"You can visit any of these " + visitable.length + " homes"
|
|
|
|
,visitable.join(", ")
|
|
|
|
]);
|
2013-01-20 22:02:56 +01:00
|
|
|
}
|
2013-12-24 01:18:43 +01:00
|
|
|
},
|
2013-12-30 22:33:12 +01:00
|
|
|
'ilist': function(params, sender){
|
2013-12-24 01:18:43 +01:00
|
|
|
var potentialVisitors = homes.ilist();
|
|
|
|
if (potentialVisitors.length == 0)
|
2013-12-30 22:33:12 +01:00
|
|
|
sender.sendMessage("No one can visit your home");
|
2013-12-24 01:18:43 +01:00
|
|
|
else
|
2013-12-30 22:33:12 +01:00
|
|
|
sender.sendMessage([
|
2013-12-24 01:18:43 +01:00
|
|
|
"These " + potentialVisitors.length + "players can visit your home",
|
|
|
|
potentialVisitors.join(", ")]);
|
|
|
|
},
|
2013-12-30 22:33:12 +01:00
|
|
|
'invite': function(params,sender){
|
2013-12-24 01:18:43 +01:00
|
|
|
if (params.length == 1){
|
2013-12-30 22:33:12 +01:00
|
|
|
sender.sendMessage("You must provide a player's name");
|
2013-01-20 22:02:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-12-24 01:18:43 +01:00
|
|
|
var playerName = params[1];
|
2013-12-31 19:21:40 +01:00
|
|
|
var guest = utils.player(playerName);
|
2013-12-24 01:18:43 +01:00
|
|
|
if (!guest)
|
2013-12-30 22:33:12 +01:00
|
|
|
sender.sendMessage(playerName + " is not here");
|
2013-12-24 01:18:43 +01:00
|
|
|
else
|
2013-12-30 22:33:12 +01:00
|
|
|
homes.invite(sender,guest);
|
2013-12-24 01:18:43 +01:00
|
|
|
},
|
2013-12-30 22:33:12 +01:00
|
|
|
'uninvite': function(params,sender){
|
2013-12-24 01:18:43 +01:00
|
|
|
if (params.length == 1){
|
2013-12-30 22:33:12 +01:00
|
|
|
sender.sendMessage("You must provide a player's name");
|
2013-12-24 01:18:43 +01:00
|
|
|
return;
|
2013-01-20 22:02:56 +01:00
|
|
|
}
|
2013-12-24 01:18:43 +01:00
|
|
|
var playerName = params[1];
|
2013-12-31 19:21:40 +01:00
|
|
|
var guest = utils.player(playerName);
|
2013-12-24 01:18:43 +01:00
|
|
|
if (!guest)
|
2013-12-30 22:33:12 +01:00
|
|
|
sender.sendMessage(playerName + " is not here");
|
2013-12-24 01:18:43 +01:00
|
|
|
else
|
2013-12-30 22:33:12 +01:00
|
|
|
homes.uninvite(sender,guest);
|
2013-12-24 01:18:43 +01:00
|
|
|
},
|
2013-12-30 22:33:12 +01:00
|
|
|
'public': function(params,sender){
|
|
|
|
homes.open(sender,params.slice(1).join(' '));
|
|
|
|
sender.sendMessage("Your home is open to the public");
|
2013-12-24 01:18:43 +01:00
|
|
|
},
|
2013-12-30 22:33:12 +01:00
|
|
|
'private': function(params, sender){
|
|
|
|
homes.close(sender);
|
|
|
|
sender.sendMessage("Your home is closed to the public");
|
2013-12-24 01:18:43 +01:00
|
|
|
},
|
2013-12-30 22:33:12 +01:00
|
|
|
'listall': function(params, sender){
|
|
|
|
if (!sender.isOp())
|
|
|
|
sender.sendMessage("Only operators can do this");
|
2013-12-24 01:18:43 +01:00
|
|
|
else
|
2013-12-30 22:33:12 +01:00
|
|
|
sender.sendMessage(homes.listall().join(", "));
|
2013-12-24 01:18:43 +01:00
|
|
|
},
|
2013-12-30 22:33:12 +01:00
|
|
|
'clear': function(params,sender){
|
|
|
|
if (!sender.isOp())
|
|
|
|
sender.sendMessage("Only operators can do this");
|
2013-12-24 01:18:43 +01:00
|
|
|
else
|
2013-12-30 22:33:12 +01:00
|
|
|
homes.clear(params[1], sender);
|
2013-12-24 01:18:43 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
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
|
|
|
|
*/
|
2013-12-30 22:33:12 +01:00
|
|
|
command("home", function ( params , sender) {
|
2013-12-24 01:18:43 +01:00
|
|
|
if (params.length == 0){
|
2013-12-30 22:33:12 +01:00
|
|
|
homes.go(sender,sender);
|
2013-12-24 01:18:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var option = options[params[0]];
|
|
|
|
if (option)
|
2013-12-30 22:33:12 +01:00
|
|
|
option(params,sender);
|
2013-12-24 01:18:43 +01:00
|
|
|
else{
|
2013-12-31 19:21:40 +01:00
|
|
|
var host = utils.player(params[0]);
|
2013-12-24 01:18:43 +01:00
|
|
|
if (!host)
|
2013-12-30 22:33:12 +01:00
|
|
|
sender.sendMessage(params[0] + " is not here");
|
2013-12-24 01:18:43 +01:00
|
|
|
else
|
2013-12-30 22:33:12 +01:00
|
|
|
homes.go(sender,host);
|
2013-12-24 01:18:43 +01:00
|
|
|
}
|
|
|
|
},optionList);
|
|
|
|
|
2013-01-19 18:01:59 +01:00
|
|
|
|
2013-12-08 13:16:41 +01:00
|
|
|
|