Prevent error on incorrect index selection

This commit is contained in:
SmallJoker 2023-09-03 08:38:03 +02:00
parent 8085ab8988
commit 8b63913bf8

View File

@ -110,7 +110,7 @@ local function swap_entry(player_name, state, direction)
if next_index < 1 or next_index > state.entry_counts[category_index] then if next_index < 1 or next_index > state.entry_counts[category_index] then
return return
end end
local current_topic = modstore:get_string(player_name .. "_category_" .. category_index .. "_entry_" .. entry_index .. "_topic") local current_topic = modstore:get_string(player_name .. "_category_" .. category_index .. "_entry_" .. entry_index .. "_topic")
local current_content = modstore:get_string(player_name .. "_category_" .. category_index .. "_entry_" .. entry_index .. "_content") local current_content = modstore:get_string(player_name .. "_category_" .. category_index .. "_entry_" .. entry_index .. "_content")
local next_topic = modstore:get_string(player_name .. "_category_" .. category_index .. "_entry_" .. next_index .. "_topic") local next_topic = modstore:get_string(player_name .. "_category_" .. category_index .. "_entry_" .. next_index .. "_topic")
@ -189,10 +189,10 @@ local function write_book(player_name)
-- If it's a location or an event, add a little context to the title -- If it's a location or an event, add a little context to the title
topic = topic .. ": " .. first_line(content) topic = topic .. ": " .. first_line(content)
end end
local new_book = ItemStack(book_written) local new_book = ItemStack(book_written)
local meta = new_book:get_meta() local meta = new_book:get_meta()
meta:set_string(author_meta_field, player_name) meta:set_string(author_meta_field, player_name)
meta:set_string("title", topic:sub(1, max_title_size)) meta:set_string("title", topic:sub(1, max_title_size))
meta:set_string("description", S("\"@1\" by @2", truncate_string(topic, short_title_size), player_name)) meta:set_string("description", S("\"@1\" by @2", truncate_string(topic, short_title_size), player_name))
@ -229,7 +229,7 @@ local function write_ccompass(player_name, old_compass)
return return
end end
local entry_selected = state.entry_selected[category] local entry_selected = state.entry_selected[category]
local topic = modstore:get_string(player_name .. "_category_" .. category .. "_entry_" .. entry_selected .. "_topic") local topic = modstore:get_string(player_name .. "_category_" .. category .. "_entry_" .. entry_selected .. "_topic")
local pos = minetest.string_to_pos(topic) local pos = minetest.string_to_pos(topic)
if not pos then if not pos then
@ -271,7 +271,7 @@ local function read_book(itemstack, player_name)
local date_string = topic:match("^%d%d%d%d%-%d%d%-%d%d") local date_string = topic:match("^%d%d%d%d%-%d%d%-%d%d")
local pos_string = topic:match("^%(%-?[0-9]+,%-?[0-9]+,%-?[0-9]+%)") local pos_string = topic:match("^%(%-?[0-9]+,%-?[0-9]+,%-?[0-9]+%)")
local category = GENERAL_CATEGORY local category = GENERAL_CATEGORY
if date_string then if date_string then
topic = date_string topic = date_string
@ -280,7 +280,7 @@ local function read_book(itemstack, player_name)
topic = pos_string topic = pos_string
category = LOCATION_CATEGORY category = LOCATION_CATEGORY
end end
local state = get_state(player_name) local state = get_state(player_name)
local entry_index = state.entry_counts[category] + 1 local entry_index = state.entry_counts[category] + 1
state.entry_counts[category] = entry_index state.entry_counts[category] = entry_index
@ -295,7 +295,7 @@ local function read_ccompass(itemstack, player_name)
local prefix_start, prefix_end = content:find(ccompass_description_prefix) local prefix_start, prefix_end = content:find(ccompass_description_prefix)
if prefix_end then if prefix_end then
content = content:sub(prefix_end+1) content = content:sub(prefix_end+1)
end end
local state = get_state(player_name) local state = get_state(player_name)
local entry_index = state.entry_counts[LOCATION_CATEGORY] + 1 local entry_index = state.entry_counts[LOCATION_CATEGORY] + 1
state.entry_counts[LOCATION_CATEGORY] = entry_index state.entry_counts[LOCATION_CATEGORY] = entry_index
@ -347,7 +347,7 @@ local function ccompass_permitted_target(itemstack)
if has_pos and not ccompass_recalibration_allowed then if has_pos and not ccompass_recalibration_allowed then
return false return false
end end
return true return true
end end
local function ccompass_permitted_source(itemstack) local function ccompass_permitted_source(itemstack)
if itemstack:get_name():sub(1,ccompass_prefix_length) ~= ccompass_prefix then if itemstack:get_name():sub(1,ccompass_prefix_length) ~= ccompass_prefix then
@ -371,7 +371,7 @@ local detached_callbacks = {
local player_name = player:get_player_name() local player_name = player:get_player_name()
local state = get_state(player_name) local state = get_state(player_name)
local category = state.category local category = state.category
if category == LOCATION_CATEGORY and if category == LOCATION_CATEGORY and
(stack_name == "compassgps:cgpsmap" or (stack_name == "compassgps:cgpsmap" or
ccompass_permitted_target(stack)) then ccompass_permitted_target(stack)) then
return 1 return 1
@ -391,7 +391,7 @@ local detached_callbacks = {
local player_name = player:get_player_name() local player_name = player:get_player_name()
if listname == "export_item" then if listname == "export_item" then
local new_item = write_item(player_name, stack) local new_item = write_item(player_name, stack)
inv:remove_item(listname, stack) inv:remove_item(listname, stack)
inv:add_item(listname, new_item) inv:add_item(listname, new_item)
elseif listname == "import_item" then elseif listname == "import_item" then
read_item(stack, player_name) read_item(stack, player_name)
@ -482,12 +482,12 @@ local function item_formspec(player_name, category, listname, topic)
.. "list[current_player;main;0,1.5;8,4;]" .. "list[current_player;main;0,1.5;8,4;]"
.. "listring[]" .. "listring[]"
.. "button[3.5,5.5;1,1;back;"..S("Back").."]" .. "button[3.5,5.5;1,1;back;"..S("Back").."]"
if mcl_formspec_itemslot then if mcl_formspec_itemslot then
formspec = formspec .. mcl_formspec_itemslot(3.5, 0, 1, 1) formspec = formspec .. mcl_formspec_itemslot(3.5, 0, 1, 1)
.. mcl_formspec_itemslot(0,1.5,8,4) .. mcl_formspec_itemslot(0,1.5,8,4)
end end
return formspec return formspec
end end
@ -499,9 +499,9 @@ local function make_personal_log_formspec(player)
local state = get_state(player_name) local state = get_state(player_name)
local category_index = state.category local category_index = state.category
ensure_detached_inventory(player_name) ensure_detached_inventory(player_name)
local formspec = { local formspec = {
"formspec_version[2]" "formspec_version[2]"
.."size[10,10]" .."size[10,10]"
@ -511,26 +511,20 @@ local function make_personal_log_formspec(player)
.. "label[0.5,0.5;"..S("Category:").."]" .. "label[0.5,0.5;"..S("Category:").."]"
.. "label[4.5,0.5;"..S("Personal Log Entries").."]" .. "label[4.5,0.5;"..S("Personal Log Entries").."]"
} }
local entries = {} local entries = {}
for i = 1, state.entry_counts[category_index] do for i = 1, state.entry_counts[category_index] do
table.insert(entries, modstore:get_string(player_name .. "_category_" .. category_index .. "_entry_" .. i .. "_content")) table.insert(entries, modstore:get_string(player_name .. "_category_" .. category_index .. "_entry_" .. i .. "_content"))
end end
local entry = ""
local entry_selected = state.entry_selected[category_index] local entry_selected = state.entry_selected[category_index]
if entry_selected > 0 then local entry = entries[entry_selected] or ""
entry = entries[entry_selected]
end
local topics = {} local topics = {}
for i = 1, state.entry_counts[category_index] do for i = 1, state.entry_counts[category_index] do
table.insert(topics, modstore:get_string(player_name .. "_category_" .. category_index .. "_entry_" .. i .. "_topic")) table.insert(topics, modstore:get_string(player_name .. "_category_" .. category_index .. "_entry_" .. i .. "_topic"))
end end
local topic = "" local topic = topics[entry_selected] or ""
if entry_selected > 0 then
topic = topics[entry_selected]
end
formspec[#formspec+1] = "tablecolumns[text;text]table[0.5,1.0;9,4.75;log_table;" formspec[#formspec+1] = "tablecolumns[text;text]table[0.5,1.0;9,4.75;log_table;"
for i, entry in ipairs(entries) do for i, entry in ipairs(entries) do
formspec[#formspec+1] = minetest.formspec_escape(truncate_string(topics[i], 30)) .. "," formspec[#formspec+1] = minetest.formspec_escape(truncate_string(topics[i], 30)) .. ","
@ -538,7 +532,7 @@ local function make_personal_log_formspec(player)
formspec[#formspec+1] = "," formspec[#formspec+1] = ","
end end
formspec[#formspec] = ";"..entry_selected.."]" -- don't use +1, this overwrites the last "," formspec[#formspec] = ";"..entry_selected.."]" -- don't use +1, this overwrites the last ","
if category_index == GENERAL_CATEGORY then if category_index == GENERAL_CATEGORY then
formspec[#formspec+1] = "textarea[0.5,6.0;9,0.5;topic_data;;" .. minetest.formspec_escape(topic) .. "]" formspec[#formspec+1] = "textarea[0.5,6.0;9,0.5;topic_data;;" .. minetest.formspec_escape(topic) .. "]"
formspec[#formspec+1] = "textarea[0.5,6.5;9,1.75;entry_data;;".. minetest.formspec_escape(entry) .."]" formspec[#formspec+1] = "textarea[0.5,6.5;9,1.75;entry_data;;".. minetest.formspec_escape(entry) .."]"
@ -600,7 +594,7 @@ local function on_player_receive_fields(player, fields, update_callback)
return return
end end
end end
if fields.save then if fields.save then
if category == GENERAL_CATEGORY then if category == GENERAL_CATEGORY then
save_entry(player_name, category, entry_selected, fields.entry_data, fields.topic_data) save_entry(player_name, category, entry_selected, fields.entry_data, fields.topic_data)
@ -610,7 +604,7 @@ local function on_player_receive_fields(player, fields, update_callback)
update_callback(player) update_callback(player)
return return
end end
if fields.create then if fields.create then
local content = "" local content = ""
local general_topic = "" local general_topic = ""
@ -618,7 +612,7 @@ local function on_player_receive_fields(player, fields, update_callback)
content = fields.entry_data content = fields.entry_data
general_topic = fields.topic_data general_topic = fields.topic_data
end end
local entry_index = state.entry_counts[category] + 1 local entry_index = state.entry_counts[category] + 1
state.entry_counts[category] = entry_index state.entry_counts[category] = entry_index
state.entry_selected[category] = entry_index state.entry_selected[category] = entry_index
@ -635,7 +629,7 @@ local function on_player_receive_fields(player, fields, update_callback)
update_callback(player) update_callback(player)
return return
end end
if fields.move_up then if fields.move_up then
swap_entry(player_name, state, -1) swap_entry(player_name, state, -1)
update_callback(player) update_callback(player)
@ -651,7 +645,7 @@ local function on_player_receive_fields(player, fields, update_callback)
update_callback(player) update_callback(player)
return return
end end
if fields.teleport if fields.teleport
and category == LOCATION_CATEGORY and category == LOCATION_CATEGORY
and valid_entry_selected and valid_entry_selected
@ -775,7 +769,7 @@ if craftable_setting or not (unified_inventory_modpath or sfinv_modpath or sfinv
minetest.show_formspec(name,"personal_log:root", make_personal_log_formspec(user)) minetest.show_formspec(name,"personal_log:root", make_personal_log_formspec(user))
end end
attributes.on_place = function(itemstack, user, pointed_thing) attributes.on_place = function(itemstack, user, pointed_thing)
if not user:get_player_control().sneak then if not user:get_player_control().sneak then
local new_stack = mcl_util.call_on_rightclick(itemstack, user, pointed_thing) local new_stack = mcl_util.call_on_rightclick(itemstack, user, pointed_thing)
if new_stack then if new_stack then
return new_stack return new_stack
@ -856,8 +850,8 @@ personal_log.add_event_entry = function(player_name, content, event_date)
event_date = os.date("%Y-%m-%d") event_date = os.date("%Y-%m-%d")
end end
add_entry_for_player(player_name, EVENT_CATEGORY, content, event_date) add_entry_for_player(player_name, EVENT_CATEGORY, content, event_date)
end end
personal_log.add_general_entry = function(player_name, content, general_topic) personal_log.add_general_entry = function(player_name, content, general_topic)
add_entry_for_player(player_name, GENERAL_CATEGORY, content, general_topic) add_entry_for_player(player_name, GENERAL_CATEGORY, content, general_topic)
end end