Editor: less checks when performing actions

master
Marco 2020-07-01 14:47:23 +02:00
parent 06b74d0bb0
commit e5d5c721b6
6 changed files with 28 additions and 24 deletions

View File

@ -58,7 +58,7 @@ The function calling the editor is
`arena_lib.enter_editor(sender, mod, arena_name)`
#### 1.2.2 CLI
If you don't want to rely on the hotbar, or you want both the editor and the commands via chat, here's how the commands work.
If you don't want to rely on the hotbar, or you want both the editor and the commands via chat, here's how the commands work. Note that there actually is another parameter at the end of each of these functions named `in_editor` but, since it's solely used by the editor itself in order run less checks, I chose to odmit it (it's in `set_spawner` too).
##### 1.2.2.1 Players management
`arena_lib.change_players_amount(sender, mod, arena_name, min_players, max_players)` changes the amount of players in a specific arena. It also works by specifying only one field (such as `([...] myarena, 3)` or `([...] myarena, nil, 6)`). It returns true if it succeeded.

View File

@ -129,7 +129,7 @@ minetest.register_tool("arena_lib:editor_enable", {
local mod = user:get_meta():get_string("arena_lib_editor.mod")
local arena_name = user:get_meta():get_string("arena_lib_editor.arena")
arena_lib.enable_arena(user:get_player_name(), mod, arena_name)
arena_lib.enable_arena(user:get_player_name(), mod, arena_name, true)
end
})

View File

@ -27,7 +27,7 @@ minetest.register_node("arena_lib:players_min", {
local arena_name = user:get_meta():get_string("arena_lib_editor.arena")
local players_amount = user:get_meta():get_int("arena_lib_editor.players_number")
if not arena_lib.change_players_amount(user:get_player_name(), mod, arena_name, players_amount, nil) then return end
if not arena_lib.change_players_amount(user:get_player_name(), mod, arena_name, players_amount, nil, true) then return end
-- aggiorno lo stack se il cambio è andato a buon fine
minetest.after(0, function()
@ -53,7 +53,7 @@ minetest.register_node("arena_lib:players_max", {
local arena_name = user:get_meta():get_string("arena_lib_editor.arena")
local players_amount = user:get_meta():get_int("arena_lib_editor.players_number")
if not arena_lib.change_players_amount(user:get_player_name(), mod, arena_name, nil, players_amount) then return end
if not arena_lib.change_players_amount(user:get_player_name(), mod, arena_name, nil, players_amount, true) then return end
-- aggiorno lo stack se il cambio è andato a buon fine
minetest.after(0, function()
@ -96,7 +96,7 @@ minetest.register_tool("arena_lib:players_teams_on", {
local mod = user:get_meta():get_string("arena_lib_editor.mod")
local arena_name = user:get_meta():get_string("arena_lib_editor.arena")
arena_lib.toggle_teams_per_arena(user:get_player_name(), mod, arena_name, 0)
arena_lib.toggle_teams_per_arena(user:get_player_name(), mod, arena_name, 0, true)
minetest.after(0, function()
user:get_inventory():set_stack("main", 5, "arena_lib:players_teams_off")

View File

@ -36,7 +36,7 @@ minetest.register_tool("arena_lib:spawner_add", {
local mod = user:get_meta():get_string("arena_lib_editor.mod")
local arena_name = user:get_meta():get_string("arena_lib_editor.arena")
arena_lib.set_spawner(user:get_player_name(), mod, arena_name)
arena_lib.set_spawner(user:get_player_name(), mod, arena_name, nil, nil, nil, true)
end
})
@ -56,7 +56,7 @@ minetest.register_tool("arena_lib:spawner_remove", {
local arena_name = user:get_meta():get_string("arena_lib_editor.arena")
local spawner_ID = user:get_meta():get_int("arena_lib_editor.spawner_ID")
arena_lib.set_spawner(user:get_player_name(), mod, arena_name, nil, "delete", spawner_ID)
arena_lib.set_spawner(user:get_player_name(), mod, arena_name, nil, "delete", spawner_ID, true)
end,
@ -82,7 +82,7 @@ minetest.register_tool("arena_lib:spawner_team_add", {
local arena_name = user:get_meta():get_string("arena_lib_editor.arena")
local team_ID = user:get_meta():get_int("arena_lib_editor.team_ID")
arena_lib.set_spawner(user:get_player_name(), mod, arena_name, team_ID)
arena_lib.set_spawner(user:get_player_name(), mod, arena_name, team_ID, nil, nil, true)
end
})
@ -103,7 +103,7 @@ minetest.register_tool("arena_lib:spawner_team_remove", {
local spawner_ID = user:get_meta():get_int("arena_lib_editor.spawner_ID")
local team_ID = user:get_meta():get_int("arena_lib_editor.team_ID")
arena_lib.set_spawner(user:get_player_name(), mod, arena_name, nil, "delete", spawner_ID)
arena_lib.set_spawner(user:get_player_name(), mod, arena_name, nil, "delete", spawner_ID, true)
end,
@ -162,7 +162,7 @@ minetest.register_tool("arena_lib:spawner_deleteall", {
local arena_name = user:get_meta():get_string("arena_lib_editor.arena")
local p_name = user:get_player_name()
arena_lib.set_spawner(p_name, mod, arena_name, nil, "deleteall")
arena_lib.set_spawner(p_name, mod, arena_name, nil, "deleteall", nil, true)
end
})
@ -184,7 +184,7 @@ minetest.register_tool("arena_lib:spawner_team_deleteall", {
local team_ID = user:get_meta():get_int("arena_lib_editor.team_ID")
local p_name = user:get_player_name()
arena_lib.set_spawner(p_name, mod, arena_name, team_ID, "deleteall")
arena_lib.set_spawner(p_name, mod, arena_name, team_ID, "deleteall", nil, true)
end
})

24
api.lua
View File

@ -344,11 +344,13 @@ end
function arena_lib.change_players_amount(sender, mod, arena_name, min_players, max_players)
function arena_lib.change_players_amount(sender, mod, arena_name, min_players, max_players, in_editor)
local id, arena = arena_lib.get_arena_by_name(mod, arena_name)
if not ARENA_LIB_EDIT_PRECHECKS_PASSED(sender, arena) then return end
if not in_editor then
if not ARENA_LIB_EDIT_PRECHECKS_PASSED(sender, arena) then return end
end
-- salvo i vecchi parametri così da poterne modificare anche solo uno senza if lunghissimi
local old_min_players = arena.min_players
@ -378,11 +380,13 @@ end
function arena_lib.toggle_teams_per_arena(sender, mod, arena_name, enable) -- enable can be 0 or 1
function arena_lib.toggle_teams_per_arena(sender, mod, arena_name, enable, in_editor) -- enable can be 0 or 1
local id, arena = arena_lib.get_arena_by_name(mod, arena_name)
if not ARENA_LIB_EDIT_PRECHECKS_PASSED(sender, arena) then return end
if not in_editor then
if not ARENA_LIB_EDIT_PRECHECKS_PASSED(sender, arena) then return end
end
-- se non ci sono team nella mod, annullo
if #arena_lib.mods[mod].teams == 1 then
@ -433,11 +437,13 @@ end
-- Gli spawn points si impostano prendendo la coordinata del giocatore che lancia il comando.
-- Non ci possono essere più spawn points del numero massimo di giocatori.
-- 'param' può essere: "overwrite", "delete", "deleteall"
function arena_lib.set_spawner(sender, mod, arena_name, teamID_or_name, param, ID)
function arena_lib.set_spawner(sender, mod, arena_name, teamID_or_name, param, ID, in_editor)
local id, arena = arena_lib.get_arena_by_name(mod, arena_name)
if not ARENA_LIB_EDIT_PRECHECKS_PASSED(sender, arena) then return end
if not in_editor then
if not ARENA_LIB_EDIT_PRECHECKS_PASSED(sender, arena) then return end
end
local mod_ref = arena_lib.mods[mod]
local team
@ -683,11 +689,13 @@ end
function arena_lib.enable_arena(sender, mod, arena_name)
function arena_lib.enable_arena(sender, mod, arena_name, in_editor)
local arena_ID, arena = arena_lib.get_arena_by_name(mod, arena_name)
if not ARENA_LIB_EDIT_PRECHECKS_PASSED(sender, arena) then return end
if not in_editor then
if not ARENA_LIB_EDIT_PRECHECKS_PASSED(sender, arena) then return end
end
local arena_max_players = arena.max_players * #arena.teams

View File

@ -16,13 +16,9 @@ function ARENA_LIB_EDIT_PRECHECKS_PASSED(sender, arena, skip_enabled)
-- se è in modalità edit, annullo
if arena_lib.is_arena_in_edit_mode(arena.name) then
local p_name_inside = arena_lib.get_player_in_edit_mode(arena.name)
if sender ~= p_name_inside then
minetest.chat_send_player(sender, minetest.colorize("#e6482e", S("[!] There must be no one inside the editor of the arena to perform this command! (now inside: @1)", p_name_inside)))
minetest.chat_send_player(sender, minetest.colorize("#e6482e", S("[!] There must be no one inside the editor of the arena to perform this command! (now inside: @1)", p_name_inside)))
return end
end
return true
end