This repository has been archived on 2021-07-14. You can view files and clone it, but cannot push or open issues or pull requests.
scriptcraft/src/main/javascript/plugins/homes/homes.js

340 lines
12 KiB
JavaScript
Raw Normal View History

/*************************************************************************
## 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-12-24 01:18:43 +01:00
var homes = plugin("homes", {
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",
/* 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's home location (ops only)"
];
},
/* ========================================================================
basic functions
======================================================================== */
go: function(guest, host){
if (typeof host == "undefined")
host = guest;
guest = utils.player(guest);
host = utils.player(host);
2013-12-24 01:18:43 +01:00
var loc = _store.houses[host.name];
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;
var homeLoc = utils.locationFromJSON(loc);
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])
return true;
2013-12-24 01:18:43 +01:00
var invitations = _store.invites[host.name];
if (invitations)
for (var 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);
2013-12-24 01:18:43 +01:00
delete _store.houses[player.name];
},
/* ========================================================================
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)
result.push(ohp);
player = utils.player(player);
2013-12-24 01:18:43 +01:00
for (var host in _store.invites){
var guests = _store.invites[host];
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){
player = utils.player(player);
var result = [];
// if home is public - all players
2013-12-24 01:18:43 +01:00
if (_store.openHouses[player.name]){
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 = [];
}
return result;
},
/*
Invite a player to the home
*/
invite: function(host, guest){
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];
invitations.push(guest.name);
2013-12-24 01:18:43 +01:00
_store.invites[host.name] = invitations;
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){
host = utils.player(host);
guest = utils.player(guest);
2013-12-24 01:18:43 +01:00
var invitations = _store.invites[host.name];
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;
},
/*
make the player's house public
*/
open: function(player, optionalMsg){
player = utils.player(player);
2013-12-24 01:18:43 +01:00
_store.openHouses[player.name] = true;
if (typeof optionalMsg != "undefined")
__plugin.server.broadcastMessage(optionalMsg);
},
/*
make the player's house private
*/
close: function(player){
player = utils.player(player);
2013-12-24 01:18:43 +01:00
delete _store.openHouses[player.name];
},
/* ========================================================================
admin functions
======================================================================== */
listall: function(){
var result = [];
2013-12-24 01:18:43 +01:00
for (var home in _store.houses)
result.push(home);
return result;
},
clear: function(player){
player = utils.player(player);
2013-12-24 01:18:43 +01:00
delete _store.houses[player.name];
delete _store.openHouses[player.name];
},
store: _store
}, 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-12-24 01:18:43 +01:00
var options = {
'set': function(params, sender){ homes.set(sender); },
'delete': function(params, sender ){ homes.remove(sender);},
'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){
sender.sendMessage("There are no homes to visit");
2013-12-24 01:18:43 +01:00
return;
}else{
sender.sendMessage([
2013-12-24 01:18:43 +01:00
"You can visit any of these " + visitable.length + " homes"
,visitable.join(", ")
]);
}
2013-12-24 01:18:43 +01:00
},
'ilist': function(params, sender){
2013-12-24 01:18:43 +01:00
var potentialVisitors = homes.ilist();
if (potentialVisitors.length == 0)
sender.sendMessage("No one can visit your home");
2013-12-24 01:18:43 +01:00
else
sender.sendMessage([
2013-12-24 01:18:43 +01:00
"These " + potentialVisitors.length + "players can visit your home",
potentialVisitors.join(", ")]);
},
'invite': function(params,sender){
2013-12-24 01:18:43 +01:00
if (params.length == 1){
sender.sendMessage("You must provide a player's name");
return;
}
2013-12-24 01:18:43 +01:00
var playerName = params[1];
var guest = utils.player(playerName);
2013-12-24 01:18:43 +01:00
if (!guest)
sender.sendMessage(playerName + " is not here");
2013-12-24 01:18:43 +01:00
else
homes.invite(sender,guest);
2013-12-24 01:18:43 +01:00
},
'uninvite': function(params,sender){
2013-12-24 01:18:43 +01:00
if (params.length == 1){
sender.sendMessage("You must provide a player's name");
2013-12-24 01:18:43 +01:00
return;
}
2013-12-24 01:18:43 +01:00
var playerName = params[1];
var guest = utils.player(playerName);
2013-12-24 01:18:43 +01:00
if (!guest)
sender.sendMessage(playerName + " is not here");
2013-12-24 01:18:43 +01:00
else
homes.uninvite(sender,guest);
2013-12-24 01:18:43 +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
},
'private': function(params, sender){
homes.close(sender);
sender.sendMessage("Your home is closed to the public");
2013-12-24 01:18:43 +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
sender.sendMessage(homes.listall().join(", "));
2013-12-24 01:18:43 +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
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
*/
command("home", function ( params , sender) {
2013-12-24 01:18:43 +01:00
if (params.length == 0){
homes.go(sender,sender);
2013-12-24 01:18:43 +01:00
return;
}
var option = options[params[0]];
if (option)
option(params,sender);
2013-12-24 01:18:43 +01:00
else{
var host = utils.player(params[0]);
2013-12-24 01:18:43 +01:00
if (!host)
sender.sendMessage(params[0] + " is not here");
2013-12-24 01:18:43 +01:00
else
homes.go(sender,host);
2013-12-24 01:18:43 +01:00
}
},optionList);
2013-12-08 13:16:41 +01:00