Check for privileges + option to not displaying an icon when the privileges condition is not satisfied

master
Zughy 2020-06-21 17:13:28 +02:00
parent 39df515dea
commit 93f4ed474d
13 changed files with 63 additions and 25 deletions

View File

@ -1,6 +1,6 @@
# Magic Compass
Teleport system for Minetest (yes, GUI will be improved)
Teleport system for Minetest (yes, GUI aspect will be improved)
<a href="https://liberapay.com/Zughy/"><img src="https://i.imgur.com/4B2PxjP.png" alt="Support my work"/></a>
@ -12,10 +12,12 @@ Look for the "Magic Compass" item in your inventory, put it in your hand and lef
### Customisation
Every icon in the menu is a location. Locations must be declared in a .txt document inside the `locations` folder like so:
```
Forest -- the name you want to show in the menu when hovering the icon
magiccompass_forest.png -- the associated texture
-3.5, 5.0, -20.5 -- where the player will be teleported
5 -- (optional) cooldown before being able to use it again
Red Forest -- the name you want to show in the menu when hovering the icon
magiccompass_redforest.png -- the associated texture
-3.5, 5.0, -20.5 -- where the player will be teleported
5 -- (optional) cooldown before being able to use it again. Leave empty or put -1 for none
interact, mygdr_lv10 -- (optional) privileges required in order to use it. Use ", " to separate them or it won't work
HIDE -- (optional) whether to hide the icon to players who don't have the required privileges
```
The file name is important too, as it must start with a number followed by an underscore like `5_whatever name.txt`.
The number indicates the position of the associated item in the grid (which scales according to the highest number declared), and empty spaces are generated automatically if the numbers of the items don't represent a full sequence

View File

@ -17,8 +17,21 @@ for _, file_name in pairs(locations_content) do
local i_desc = data[1]
local i_texture = data[2]
local i_pos = data[3]
local i_cooldown = tonumber(data[4])
local i_privileges = data[5]
local i_cooldown
local i_privileges
local i_hide
if data[4] and tonumber(data[4]) ~= -1 then
i_cooldown = tonumber(data[4])
end
if data[5] then
i_privileges = data[5]
end
if data[6] and data[6] == "HIDE" then
i_hide = true
end
-- creo l'oggetto
minetest.register_tool("magic_compass:" .. i_ID, {
@ -29,6 +42,6 @@ for _, file_name in pairs(locations_content) do
})
magic_compass.items[tonumber(i_ID)] = {desc = i_desc, pos = i_pos, cooldown = i_cooldown}
magic_compass.items[tonumber(i_ID)] = {desc = i_desc, pos = i_pos, cooldown = i_cooldown, privs = i_privileges, hide = i_hide}
end

View File

@ -1,4 +1,4 @@
function magic_compass.get_formspec()
function magic_compass.get_formspec(p_name)
local SLOTS_PER_ROW = 5
local rows = math.floor(table.maxn(magic_compass.items) / SLOTS_PER_ROW - 0.1) + 1
@ -17,7 +17,15 @@ function magic_compass.get_formspec()
local item = magic_compass.items[ID]
if item then
table.insert(formspec, i+1, "item_image_button[" .. x .. "," .. y .. ";1,1;magic_compass:" .. ID .. ";" .. ID .. ";]")
if item.privs then
if not item.hide or (item.hide and minetest.check_player_privs(p_name, minetest.string_to_privs(item.privs, ", "))) then
table.insert(formspec, i+1, "item_image_button[" .. x .. "," .. y .. ";1,1;magic_compass:" .. ID .. ";" .. ID .. ";]")
else
table.insert(formspec, i+1, "image_button[" .. x .. "," .. y .. ";1,1;;EMPTY;]")
end
else
table.insert(formspec, i+1, "item_image_button[" .. x .. "," .. y .. ";1,1;magic_compass:" .. ID .. ";" .. ID .. ";]")
end
else
table.insert(formspec, i+1, "image_button[" .. x .. "," .. y .. ";1,1;;EMPTY;]")
end

View File

@ -4,4 +4,3 @@ dofile(minetest.get_modpath("magic_compass") .. "/deserializer.lua")
dofile(minetest.get_modpath("magic_compass") .. "/formspec.lua")
dofile(minetest.get_modpath("magic_compass") .. "/items.lua")
dofile(minetest.get_modpath("magic_compass") .. "/player_manager.lua")
dofile(minetest.get_modpath("magic_compass") .. "/privs.lua")

View File

@ -11,7 +11,7 @@ minetest.register_tool("magic_compass:compass", {
on_drop = function() end,
on_use = function(itemstack, user, pointed_thing)
minetest.show_formspec(user:get_player_name(), "magic_compass:GUI", magic_compass.get_formspec())
minetest.show_formspec(user:get_player_name(), "magic_compass:GUI", magic_compass.get_formspec(user:get_player_name()))
end
})

View File

@ -1,4 +1,4 @@
# version 1.1.0-dev
# version 1.1.0
# author(s): Zughy
# reviewer(s):
# textdomain: magic_compass
@ -7,5 +7,6 @@
Magic Compass=Bussola Magica
# player_manager.lua
You can't use this item yet! (seconds remaining: @1)=Non puoi ancora riutilizzare quest'oggetto! (secondi rimanenti: @1)
This location is not available for you at the moment!=Non ti è consentito l'accesso a questo luogo ora!
You can't reteleport to this location so quickly! (seconds remaining: @1)=Non puoi ritelerasportarti in questo luogo così velocemente! (secondi rimanenti: @1)
Wooosh!=Wooosh!

View File

@ -1,4 +1,4 @@
# version 1.1.0-dev
# version 1.1.0
# author(s):
# reviewer(s):
# textdomain: magic_compass
@ -7,5 +7,6 @@
Magic Compass=
# player_manager.lua
You can't use this item yet! (seconds remaining: @1)=
This location is not available for you at the moment!=
You can't reteleport to this location so quickly! (seconds remaining: @1)=
Wooosh!=

View File

@ -0,0 +1,6 @@
Do you see me? That's because you have the 'fast' privilege. People who don't, they simply can't!
magiccompass_example6.png
90.5, 20.0, -30.5
-1
fast
HIDE

View File

@ -1,3 +1,5 @@
And how about privileges? We have those too. Like this, which doesn't work without the 'fly' privilege (TODO)
And how about privileges? We have those too. Like this, which doesn't work without the 'fly' and 'shout' privilege
magiccompass_example5.png
40.5, 40.0, 1.5
-1
fly, shout

View File

@ -1,8 +1,9 @@
local S = minetest.get_translator("magic_compass")
local function start_cooldown() end
-- on_cooldown non viene creato in automatico al login, bensì al primo utilizzo di un oggetto con cooldown.
-- Questo perché si potrebbero avere zero oggetti con cooldown, creando tabelle inutili
local on_cooldown = {} -- KEY: player name; VALUE: {items, on, cooldown}
local S = minetest.get_translator("magic_compass")
@ -15,9 +16,14 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
local item = magic_compass.items[tonumber(ID)]
local p_name = player:get_player_name()
-- se non ha i permessi, annullo
if item.privs and not minetest.check_player_privs(p_name, minetest.string_to_privs(item.privs, ", ")) then
minetest.chat_send_player(p_name, S("This location is not available for you at the moment!"))
return end
-- se è in cooldown, annullo
if item.cooldown and on_cooldown[p_name] and on_cooldown[p_name][ID] then
minetest.chat_send_player(p_name, S("You can't use this item yet! (seconds remaining: @1)", on_cooldown[p_name][ID]))
minetest.chat_send_player(p_name, S("You can't reteleport to this location so quickly! (seconds remaining: @1)", on_cooldown[p_name][ID]))
return end
-- teletrasporto
@ -31,22 +37,23 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
on_cooldown[p_name] = {}
end
-- lo imposto
on_cooldown[p_name][ID] = item.cooldown
-- avvio cooldown
start_cooldown(p_name, ID)
-- e lo avvio
run_cooldown(p_name, ID)
end
end)
function start_cooldown(p_name, ID)
function run_cooldown(p_name, ID)
on_cooldown[p_name][ID] = on_cooldown[p_name][ID] -1
if on_cooldown[p_name][ID] == 0 then
on_cooldown[p_name][ID] = nil
else
minetest.after(1, function()
start_cooldown(p_name, ID)
run_cooldown(p_name, ID)
end)
end
end

View File

@ -1 +0,0 @@
minetest.register_privilege("magiccompass_admin", {})

Binary file not shown.

After

Width:  |  Height:  |  Size: 169 B