Several properties fixes

master
Zughy 2020-09-06 00:31:58 +02:00
parent 3dbd64dfb7
commit f82e066991
3 changed files with 31 additions and 19 deletions

View File

@ -243,7 +243,11 @@ in doing so, we can easily access the `kill_leader` field whenever we want from
> Beware: you DO need to initialise your properties (whatever type) or it'll return an error
##### 2.4.1.1 Updating properties for old arenas
##### 2.4.1.1 Updating non temporary properties via code
Let's say you want to change a property from your mod. A naive approach would be doing `yourarena.property = something`. This, though, won't update it in the storage, so when you restart the server it'll still have the old value.
Instead, the right way to permanently update a property for an arena is calling `arena_lib.change_arena_property(<sender>, mod, arena_name, property, new_value)`. If `sender` is nil, the output message will be printed in the log.
##### 2.4.1.2 Updating properties for old arenas
This is done automatically by arena_lib every time you change the properties declaration in `register_minigame`, so don't worry. Just, keep in mind that when a property is removed, it'll be removed from every arena, so if you're not sure about what you're doing, do a backup first.
#### 2.4.2 Players properties

View File

@ -223,7 +223,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
-- se premo per sovrascrivere
elseif fields.property_overwrite or fields.key_enter then
arena_lib.change_arena_properties(p_name, mod, arena_name, sel_property_attr[p_name].name, fields.sel_property_value, true)
arena_lib.change_arena_property(p_name, mod, arena_name, sel_property_attr[p_name].name, fields.sel_property_value, true)
minetest.show_formspec(p_name, "arena_lib:settings_properties", get_properties_formspec(p_name, mod, arena, sel_property_attr[p_name].id))
end

42
api.lua
View File

@ -323,7 +323,7 @@ end
function arena_lib.change_arena_properties(sender, mod, arena_name, property, new_value, in_editor)
function arena_lib.change_arena_property(sender, mod, arena_name, property, new_value, in_editor)
local id, arena = arena_lib.get_arena_by_name(mod, arena_name)
@ -333,34 +333,42 @@ function arena_lib.change_arena_properties(sender, mod, arena_name, property, ne
-- se la proprietà non esiste
if arena[property] == nil then
minetest.chat_send_player(sender, minetest.colorize("#e6482e", S("[!] Parameters don't seem right!")))
if sender then minetest.chat_send_player(sender, minetest.colorize("#e6482e", S("[!] Parameters don't seem right!")))
else minetest.log("warning", "[ARENA_LIB] [!] Properties - Parameters don't seem right!") end
return end
local func, error_msg = loadstring("return (" .. new_value .. ")")
-- se da editor, converto la stringa nel tipo corrispettivo
if in_editor then
local func, error_msg = loadstring("return (" .. new_value .. ")")
-- se non ritorna una sintassi corretta
if not func then
minetest.chat_send_player(sender, minetest.colorize("#e6482e", "[!] " .. error_msg))
return end
-- se non ritorna una sintassi corretta
if not func then
minetest.chat_send_player(sender, minetest.colorize("#e6482e", "[SYNTAX!] " .. error_msg))
return end
setfenv(func, {})
setfenv(func, {})
local good, result = pcall(func)
local good, result = pcall(func)
-- se le operazioni della funzione causano errori
if not good then
minetest.chat_send_player(sender, minetest.colorize("#e6482e", "[!] " .. result))
return end
-- se le operazioni della funzione causano errori
if not good then
minetest.chat_send_player(sender, minetest.colorize("#e6482e", "[RUNTIME!] " .. result))
return end
new_value = result
end
-- se il tipo è diverso dal precedente
if type(arena[property]) ~= type(result) then
minetest.chat_send_player(sender, minetest.colorize("#e6482e", S("[!] You can't change property type!")))
if type(arena[property]) ~= type(new_value) then
if sender then minetest.chat_send_player(sender, minetest.colorize("#e6482e", S("[!] You can't change property type!")))
else minetest.log("warning", "[ARENA_LIB] [!] Properties - You can't change property type!") end
return end
arena[property] = result
arena[property] = new_value
update_storage(false, mod, id, arena)
minetest.chat_send_player(sender, S("Property @1 successfully overwritten", property))
if sender then minetest.chat_send_player(sender, S("Property @1 successfully overwritten", property))
else minetest.log("action", "[ARENA_LIB] Property " .. property .. " successfully overwritten") end
end