Auto-teleport if player has only 1 home

If the player only has one home, allow teleporting to that home even if they do not specify it by name.
This commit is contained in:
octacian 2017-05-16 16:04:26 -07:00
parent 65bfd2e3f1
commit a8b2822e65
3 changed files with 42 additions and 2 deletions

6
API.md
View File

@ -20,6 +20,12 @@ The MultiHome mod provides a simplistic API allowing creation, deletion, access,
* `player`: PlayerRef * `player`: PlayerRef
* `name`: Home name * `name`: Home name
`multihome.get_default(player)`
* Returns a "default" home if the player has only one home set
* Allows the player to be automatically teleported without specifying the home name
* `player`: PlayerRef
`multihome.list(player)` `multihome.list(player)`
* List a player's homes in format suitable for chat * List a player's homes in format suitable for chat

View File

@ -15,6 +15,8 @@ MultiHome provides exactly two compatibility modes allowing the mod to work with
The first two compatibility modes use the same chatcommand (`/multihome`) as documented below, however, `deprecate` overrides chatcommands from sethome causing them to display a message stating that they are deprecated and `/multihome` should be used instead. __Note:__ If a compatibility mode is set and the sethome mod is not found, compatibility is automatically set to `none`. The first two compatibility modes use the same chatcommand (`/multihome`) as documented below, however, `deprecate` overrides chatcommands from sethome causing them to display a message stating that they are deprecated and `/multihome` should be used instead. __Note:__ If a compatibility mode is set and the sethome mod is not found, compatibility is automatically set to `none`.
When attempting to vist a home using any compatibility mode, if the player does not specify a home to teleport to and they only have one home, the player will be automatically teleported to that home.
#### Compatibility: None/Deprecate #### Compatibility: None/Deprecate
Both the `none` and `deprecate` compatibility modes use the same chatcommand, however `deprecate` adds deprecation messages as mentioned above. The best way to learn how to use this `/multihome` chatcommand is to use the in-game help, `/help multihome`, however, an overview of the usage can be found below. Both the `none` and `deprecate` compatibility modes use the same chatcommand, however `deprecate` adds deprecation messages as mentioned above. The best way to learn how to use this `/multihome` chatcommand is to use the in-game help, `/help multihome`, however, an overview of the usage can be found below.

View File

@ -98,6 +98,25 @@ function multihome.get(player, name)
return homes[name] return homes[name]
end end
-- [function] Get player's default home
function multihome.get_default(player)
if type(player) == "string" then
player = minetest.get_player_by_name(player)
end
local default
local count = 0
local homes = minetest.deserialize(player:get_attribute("multihome"))
for home, pos in pairs(homes) do
count = count + 1
default = home
end
if count == 1 then
return default
end
end
-- [function] List homes -- [function] List homes
function multihome.list(player) function multihome.list(player)
if type(player) == "string" then if type(player) == "string" then
@ -182,8 +201,16 @@ if compat == "none" or compat == "deprecate" then
return multihome.set(name, params[2]) return multihome.set(name, params[2])
elseif #params == 2 and params[1] == "del" then elseif #params == 2 and params[1] == "del" then
return multihome.remove(name, params[2]) return multihome.remove(name, params[2])
elseif #params == 2 and params[1] == "go" then elseif params[1] == "go" then
return multihome.go(name, params[2]) local home = params[2]
if not home then
home = multihome.get_default(name)
if not home then
return false, "Invalid parameters (see /help multihome)"
end
end
return multihome.go(name, home)
elseif params[1] == "list" then elseif params[1] == "list" then
return multihome.list(name) return multihome.list(name)
else else
@ -221,6 +248,11 @@ if compat == "override" then
if param and param ~= "" then if param and param ~= "" then
return multihome.go(name, param) return multihome.go(name, param)
else else
local home = multihome.get_default(name)
if home then
return multihome.go(name, home)
end
return false, "Invalid parameters (see /help home)" return false, "Invalid parameters (see /help home)"
end end
end, end,