HUD supports areas mod

This commit is contained in:
Evert Prants 2019-01-17 00:49:20 +02:00
parent 9ebb7bc788
commit d467d8cd61
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 87 additions and 70 deletions

View File

@ -1,2 +1,3 @@
towny towny
towny_nations? towny_nations?
areas?

View File

@ -4,81 +4,97 @@
local modpath = minetest.get_modpath(minetest.get_current_modname()) local modpath = minetest.get_modpath(minetest.get_current_modname())
-- This code is borrowed from [areas] by ShadowNinja -- This code is borrowed from [areas] by ShadowNinja
-- TODO: Support areas (areas:getExternalHudEntries)
towny.hud = {} local function towns_at_pos(pos, areas)
if not areas then areas = {} end
local t,p,c = towny.regions.get_town_at(pos)
minetest.register_globalstep(function(dtime) if t then
for _, player in pairs(minetest.get_connected_players()) do if towny.nations then
local name = player:get_player_name() local n = towny.nations.get_town_nation(t)
local pos = vector.round(player:getpos()) if n then
local areaStrings = {} table.insert(areas, { name = towny.nations.get_full_name(n) })
local t,p,c = towny.regions.get_town_at(pos)
if t then
if towny.nations then
local n = towny.nations.get_town_nation(t)
if n then
table.insert(areaStrings, towny.nations.get_full_name(n))
end
end
local town = towny.get_full_name(t)
local tdata = towny.towns[t]
local greeting = ""
if tdata.flags.greeting then
greeting = "- "..tdata.flags.greeting
end
table.insert(areaStrings, ("%s %s"):format(town, greeting))
if p then
local plot = tdata.plots[p]
local greeting = ("%s's Plot"):format(plot.owner)
if plot.flags['greeting'] then
greeting = ("%s (%s)"):format(plot.flags['greeting'], plot.owner)
end
if plot.flags['claimable'] then
if plot.flags['cost'] and plot.flags['cost'] > 0 then
-- TODO: economy
end
greeting = greeting .. " (For sale: FREE!)"
end
table.insert(areaStrings, ("%s - %s"):format(town, greeting))
end end
end end
local areaString = "Towns:" local town = towny.get_full_name(t)
if #areaStrings > 0 then local tdata = towny.towns[t]
areaString = areaString.."\n".. local greeting = ""
table.concat(areaStrings, "\n") if tdata.flags.greeting then
greeting = "- "..tdata.flags.greeting
end end
local hud = towny.hud[name] table.insert(areas, { name = ("%s %s"):format(town, greeting) })
if not hud then
hud = {} if p then
towny.hud[name] = hud local plot = tdata.plots[p]
hud.areasId = player:hud_add({ local greeting = ("%s's Plot"):format(plot.owner)
hud_elem_type = "text",
name = "Towns", if plot.flags['greeting'] then
number = 0xFFFFFF, greeting = ("%s (%s)"):format(plot.flags['greeting'], plot.owner)
position = {x=0, y=1}, end
offset = {x=8, y=-8},
text = areaString, -- Override unowned plot greeting
scale = {x=200, y=60}, if not plot.owner then
alignment = {x=1, y=-1}, greeting = "Unowned Plot"
}) end
hud.oldAreas = areaString
return if plot.flags['claimable'] then
elseif hud.oldAreas ~= areaString then if plot.flags['cost'] and plot.flags['cost'] > 0 then
player:hud_change(hud.areasId, "text", areaString) -- TODO: economy
hud.oldAreas = areaString end
greeting = greeting .. " (For sale: FREE!)"
end
table.insert(areas, { name = ("%s - %s"):format(town, greeting) })
end end
end end
end)
return areas
end
minetest.register_on_leaveplayer(function(player) if minetest.get_modpath("areas") ~= nil then
towny.hud[player:get_player_name()] = nil areas:registerHudHandler(towns_at_pos)
end) else
towny.hud = {}
minetest.register_globalstep(function(dtime)
for _, player in pairs(minetest.get_connected_players()) do
local name = player:get_player_name()
local pos = vector.round(player:getpos())
local areaStrings = {}
for i,town in pairs(towns_at_pos(pos)) do
table.insert(areaStrings, town.name)
end
local areaString = "Towns:"
if #areaStrings > 0 then
areaString = areaString.."\n"..
table.concat(areaStrings, "\n")
end
local hud = towny.hud[name]
if not hud then
hud = {}
towny.hud[name] = hud
hud.areasId = player:hud_add({
hud_elem_type = "text",
name = "Towns",
number = 0xFFFFFF,
position = {x=0, y=1},
offset = {x=8, y=-8},
text = areaString,
scale = {x=200, y=60},
alignment = {x=1, y=-1},
})
hud.oldAreas = areaString
return
elseif hud.oldAreas ~= areaString then
player:hud_change(hud.areasId, "text", areaString)
hud.oldAreas = areaString
end
end
end)
minetest.register_on_leaveplayer(function(player)
towny.hud[player:get_player_name()] = nil
end)
end

View File

@ -1,4 +1,4 @@
name = towny_hud name = towny_hud
description = Heads Up Display information for towns description = Heads Up Display information for towns
depends = towny depends = towny
optional_depends = towny_nations optional_depends = towny_nations,areas