From f676ff40a15b63d2d3124730f3b1652eb4ac0cbb Mon Sep 17 00:00:00 2001 From: Zenon Seth Date: Sat, 11 Nov 2023 14:03:54 +0000 Subject: [PATCH] Various fixes and improvements; bring code up to date; use transltor --- depends.txt | 1 - init.lua | 64 +++++++++++++++++++++++++++++++---------------------- license.txt | 38 ++++++++++++++++++++++++++++++- mod.conf | 6 +++++ utils.lua | 51 ++++++------------------------------------ 5 files changed, 88 insertions(+), 72 deletions(-) delete mode 100644 depends.txt create mode 100644 mod.conf diff --git a/depends.txt b/depends.txt deleted file mode 100644 index 331d858..0000000 --- a/depends.txt +++ /dev/null @@ -1 +0,0 @@ -default \ No newline at end of file diff --git a/init.lua b/init.lua index c6b5859..96aa2d5 100644 --- a/init.lua +++ b/init.lua @@ -1,4 +1,9 @@ +local MOD_NAME = minetest.get_current_modname() or "treasure_chest" +local S = function(s) return s end +if minetest.get_translator then S = minetest.get_translator(MOD_NAME) end + +treasure_chest = {} dofile(minetest.get_modpath("treasure_chest") .. "/utils.lua") @@ -24,11 +29,11 @@ local fieldI4P = "i4p"; local fieldI5P = "i5p"; local buttonExit = "exit"; -local strDescription = "A chest that gives semi-randomized rewards per player"; -local strOneTime = "This is a one-time use chest, and you already opened it!"; -local strTooSoon = "To get another reward come back in "; -local strFromRefreshLabel = "Refresh time, in minutes, integer. E.g.: 60 = 1 hour, 1440 = 1 day, 10080 = 1 week"; -local strProbabiltiesLabel = "Item probability of being given, integer, range 0..100: 0 = never, 100 = always"; +local strDescription = S("A chest that gives semi-randomized rewards per player"); +local strOneTime = S("This is a one-time use chest, and you already opened it!"); +local strTooSoon = S("To get another reward come back in "); +local strFromRefreshLabel = S("Refresh time, in minutes, integer. E.g.: 60 = 1 hour, 1440 = 1 day, 10080 = 1 week"); +local strProbabiltiesLabel = S("Item probability of being given, integer, range 0..100: 0 = never, 100 = always"); minetest.register_node("treasure_chest:treasure_chest", { description = strDescription, @@ -109,9 +114,9 @@ minetest.register_node("treasure_chest:treasure_chest", { and k ~= metaInt5p then local tv = tonumber(v) if tv then - diff = gameTime - tv + local diff = gameTime - tv if diff > refresh * 60 then - newMetaTable["fields"] = table.removeKey(newMetaTable["fields"], k) + newMetaTable["fields"] = treasure_chest.removeKey(newMetaTable["fields"], k) end end end @@ -162,13 +167,13 @@ minetest.register_node("treasure_chest:treasure_chest", { diff = math.floor(diff / 60 + 0.5) local time = "" if diff <= 1 then - time = "1 minute" + time = S("1 minute") elseif diff < 60 then - time = diff .. " minutes" + time = diff .. S(" minutes") elseif diff < 1440 then - time = math.floor(diff/60 + 0.5) .. " hours" + time = math.floor(diff/60 + 0.5) .. S(" hours") else - time = math.floor(diff/1440 + 0.5) .. " days" + time = math.floor(diff/1440 + 0.5) .. S(" days") end reason = strTooSoon .. time end @@ -184,7 +189,7 @@ minetest.register_node("treasure_chest:treasure_chest", { local metaAccessString = index.."p"; local probability = meta:get_int(metaAccessString); print("wield list name = "..player:get_wield_list()); - if (randomCheck(probability)) then + if (treasure_chest.randomCheck(probability)) then local itemStackToAdd = nodeInv:get_stack("main", index+1); -- +1 for inventory indexing begins at 1 itemStackToAdd = playerInv:add_item("main", itemStackToAdd); if not itemStackToAdd:is_empty() then @@ -202,31 +207,38 @@ minetest.register_node("treasure_chest:treasure_chest", { minetest.register_on_player_receive_fields(function(player, formname, fields) if formname == "treasure_chest:setup_inventory" then - local playerName = player:get_player_name(); + local playerName = player:get_player_name() if (not fields[fieldRefresh]) then -- User cancelled, quit now - return true; + openedTreasureChestConfigs[playerName] = nil + return true end - local pos = openedTreasureChestConfigs[playerName]; + local pos = openedTreasureChestConfigs[playerName] if pos == nil then - return; + return + end + openedTreasureChestConfigs[playerName] = nil + + local meta = minetest.get_meta(pos) + + local owner = meta:get_string(metaStrOwner) + if not minetest.check_player_privs(player, "server") or owner ~= playerName then + return true end - openedTreasureChestConfigs[playerName] = nil; - local meta = minetest.get_meta(pos); if meta:get_string(metaStrType) ~= metaExpectedType then - return; + return true end - meta:set_int(metaIntRefresh, clamp(toNum(fields[fieldRefresh], meta:get_int(metaIntRefresh)), -1, nil) ); - meta:set_int(metaInt0p, clamp(toNum(fields[fieldI0P], meta:get_int(metaInt0p)), 0, 100)); - meta:set_int(metaInt1p, clamp(toNum(fields[fieldI1P], meta:get_int(metaInt1p)), 0, 100)); - meta:set_int(metaInt2p, clamp(toNum(fields[fieldI2P], meta:get_int(metaInt2p)), 0, 100)); - meta:set_int(metaInt3p, clamp(toNum(fields[fieldI3P], meta:get_int(metaInt3p)), 0, 100)); - meta:set_int(metaInt4p, clamp(toNum(fields[fieldI4P], meta:get_int(metaInt4p)), 0, 100)); - meta:set_int(metaInt5p, clamp(toNum(fields[fieldI5P], meta:get_int(metaInt5p)), 0, 100)); + meta:set_int(metaIntRefresh, treasure_chest.clamp(treasure_chest.toNum(fields[fieldRefresh], meta:get_int(metaIntRefresh)), -1, nil) ) + meta:set_int(metaInt0p, treasure_chest.clamp(treasure_chest.toNum(fields[fieldI0P], meta:get_int(metaInt0p)), 0, 100)) + meta:set_int(metaInt1p, treasure_chest.clamp(treasure_chest.toNum(fields[fieldI1P], meta:get_int(metaInt1p)), 0, 100)) + meta:set_int(metaInt2p, treasure_chest.clamp(treasure_chest.toNum(fields[fieldI2P], meta:get_int(metaInt2p)), 0, 100)) + meta:set_int(metaInt3p, treasure_chest.clamp(treasure_chest.toNum(fields[fieldI3P], meta:get_int(metaInt3p)), 0, 100)) + meta:set_int(metaInt4p, treasure_chest.clamp(treasure_chest.toNum(fields[fieldI4P], meta:get_int(metaInt4p)), 0, 100)) + meta:set_int(metaInt5p, treasure_chest.clamp(treasure_chest.toNum(fields[fieldI5P], meta:get_int(metaInt5p)), 0, 100)) return true end return false diff --git a/license.txt b/license.txt index 486bd90..51d2f90 100644 --- a/license.txt +++ b/license.txt @@ -2,7 +2,7 @@ License of source code ---------------------- The MIT License (MIT) -Copyright (C) 2017-2022 ZenonSeth +Copyright (C) 2017-2023 ZenonSeth Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software @@ -22,3 +22,39 @@ DEALINGS IN THE SOFTWARE. For more details: https://opensource.org/licenses/MIT + + + +Licenses of media (textures) +---------------------------- + +Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) +Copyright (C) 2017-2023 Zenon Seth + +You are free to: +Share — copy and redistribute the material in any medium or format. +Adapt — remix, transform, and build upon the material for any purpose, even commercially. +The licensor cannot revoke these freedoms as long as you follow the license terms. + +Under the following terms: + +Attribution — You must give appropriate credit, provide a link to the license, and +indicate if changes were made. You may do so in any reasonable manner, but not in any way +that suggests the licensor endorses you or your use. + +ShareAlike — If you remix, transform, or build upon the material, you must distribute +your contributions under the same license as the original. + +No additional restrictions — You may not apply legal terms or technological measures that +legally restrict others from doing anything the license permits. + +Notices: + +You do not have to comply with the license for elements of the material in the public +domain or where your use is permitted by an applicable exception or limitation. +No warranties are given. The license may not give you all of the permissions necessary +for your intended use. For example, other rights such as publicity, privacy, or moral +rights may limit how you use the material. + +For more details: +http://creativecommons.org/licenses/by-sa/3.0/ diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..0bd0a0a --- /dev/null +++ b/mod.conf @@ -0,0 +1,6 @@ +name = treasure_chest +min_minetest_version = 0.4.17 +author = ZenonSeth +release = 1.1 +description = Configurable Chest that gives players rewards on a timer +title = Treasure Chest diff --git a/utils.lua b/utils.lua index 52ffda9..9c5e816 100644 --- a/utils.lua +++ b/utils.lua @@ -1,4 +1,4 @@ -function table.removeKey(t, k) +function treasure_chest.removeKey(t, k) local i = 0 local keys, values = {},{} for k,v in pairs(t) do @@ -24,44 +24,7 @@ function table.removeKey(t, k) return a end -function splitStringToTable(inputString, splitter) - local ret = {}; - local tmp; - - if inputString == nil then return nil; end - - if (splitter == nil) then - table.insert(ret, inputString); - return ret; - end - - -- print("inputString: " .. inputString .. ", splitter:" .. splitter); - local found = true; - while found do - local s,e = inputString:find(splitter); - if s == nil then - table.insert(ret, inputString); - found = false; - else - -- print("s/e=" .. s .. "/" .. e); - tmp = inputString:sub(0,s - 1); - table.insert(ret, tmp); - inputString = inputString:sub(e + 1); - end - end - -- for k,v in pairs(ret) do print(k,v) end - return ret; -end - - -function tableLength(table) - if (table == nil) then return 0; end - local count = 0 - for _ in pairs(table) do count = count + 1 end - return count -end - -function clamp(value, min, max) +function treasure_chest.clamp(value, min, max) if value == nil then return nil; end if max == nil and min == nil then return value; end if min == nil then return math.min(value, max); end @@ -69,13 +32,13 @@ function clamp(value, min, max) return math.max(math.min(value, max), min); end -function toNum(number, default) +function treasure_chest.toNum(number, default) default = default or 0; return tonumber(number) or default; end -function randomCheck(normalizedIntProb, minValue, maxValue) - minValue = toNum(minValue, 1); - maxValue = toNum(maxValue, 100); - return math.random(1,100) <= toNum(normalizedIntProb); +function treasure_chest.randomCheck(normalizedIntProb, minValue, maxValue) + minValue = treasure_chest.toNum(minValue, 1); + maxValue = treasure_chest.toNum(maxValue, 100); + return math.random(1,100) <= treasure_chest.toNum(normalizedIntProb); end