Mod rewrite and new models (#21)

* Aliases moved to utils folder

* Method to generate two different formspec layouts, big and small

* Log Actions moved to separate module

* Added method to generate a chest definition

* Rewritten models

* Add Fridge model

* Add Toolbox models

* "Mod loaded" message; Updated localization template

* Add Italian localization

* Fridge now has both normal and big (2 blocks) models

* Fixed mixed indentation

* Rewritten README; improved IT and FR (thanks to @louisroyer) localizations.
This commit is contained in:
nxet
2020-10-08 11:17:51 +02:00
committed by Louis
parent cac8cddc66
commit 886108a1af
36 changed files with 655 additions and 542 deletions

90
utils/actions.lua Normal file
View File

@ -0,0 +1,90 @@
local function get_inventory_auth_string(player, meta, pos, label)
return player:get_player_name() .. " tried to access a locked " .. label .. " belonging to " .. meta:get_string("owner") .. " at " .. minetest.pos_to_string(pos)
end
function has_locked_chest_privilege(meta, player)
if player:get_player_name() ~= meta:get_string("owner") then
return false
end
return true
end
function get_allow_metadata_inventory_move(t)
setmetatable(t, {__index={check_privs=has_locked_chest_privilege}})
local label, check_privs = t[1], t.check_privs
return function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
if not check_privs(meta, player) then
minetest.log("action", get_inventory_auth_string(player, meta, pos, label))
return 0
end
return count
end
end
function get_allow_metadata_inventory_put(t)
setmetatable(t, {__index={check_privs=has_locked_chest_privilege}})
local label, check_privs = t[1], t.check_privs
return function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
if not check_privs(meta, player) then
minetest.log("action", get_inventory_auth_string(player, meta, pos, label))
return 0
end
return stack:get_count()
end
end
function get_allow_metadata_inventory_take(t)
setmetatable(t, {__index={check_privs=has_locked_chest_privilege}})
local label, check_privs = t[1], t.check_privs
return function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
if not check_privs(meta, player) then
minetest.log("action", get_inventory_auth_string(player, meta, pos, label))
return 0
end
return stack:get_count()
end
end
local function get_inventory_action_string(player, pos, action, label)
return player:get_player_name() .. " moves stuff " .. action .. " locked " .. label .. " at " .. minetest.pos_to_string(pos)
end
function get_on_metadata_inventory_move(label)
return function(pos, from_list, from_index, to_list, to_index, count, player)
minetest.log("action", get_inventory_action_string(player, pos, "in", label)
)
end
end
function get_on_metadata_inventory_put(label)
return function(pos, listname, index, stack, player)
minetest.log("action", get_inventory_action_string(player, pos, "to", label))
end
end
function get_on_metadata_inventory_take(label)
return function(pos, listname, index, stack, player)
minetest.log("action", get_inventory_action_string(player, pos, "from", label))
end
end
actions = {
has_locked_chest_privilege = has_locked_chest_privilege,
get_allow_metadata_inventory_move = get_allow_metadata_inventory_move,
get_allow_metadata_inventory_put = get_allow_metadata_inventory_put,
get_allow_metadata_inventory_take = get_allow_metadata_inventory_take,
get_on_metadata_inventory_move = get_on_metadata_inventory_move,
get_on_metadata_inventory_put = get_on_metadata_inventory_put,
get_on_metadata_inventory_take = get_on_metadata_inventory_take,
}
return actions

6
utils/aliases.lua Normal file
View File

@ -0,0 +1,6 @@
-- Aliases for compatibility with 0gb.us's chests_0gb_us mod.
minetest.register_alias("chests_0gb_us:cobble", "more_chests:cobble")
minetest.register_alias("chests_0gb_us:wifi", "more_chests:wifi")
minetest.register_alias("chests_0gb_us:shared", "more_chests:shared")
minetest.register_alias("chests_0gb_us:secret", "more_chests:secret")
minetest.register_alias("chests_0gb_us:dropbox", "more_chests:dropbox")

97
utils/base.lua Normal file
View File

@ -0,0 +1,97 @@
-- NOTE: `require` is not allowed with mod security on
-- `dofile` with a return on the module is used instead
generate_formspec_string = dofile(minetest.get_modpath("more_chests").."/utils/formspec.lua")
local actions = dofile(minetest.get_modpath("more_chests").."/utils/actions.lua")
local S = minetest.get_translator("more_chests")
local function parse_action(value, default_getter)
if value == false then -- model disabled this attribute
return nil
elseif value == nil then -- model wants the default attribute
return default_getter()
else -- model provided its own attribute
return value
end
end
function generate_chest_def(def)
-- TODO assert def.size in ("big", "small")
local out = {
description = def.description,
tiles = {
def.tiles.top or def.tiles.side,
def.tiles.top or def.tiles.side,
def.tiles.side,
def.tiles.side,
def.tiles.side,
def.tiles.front
},
paramtype2 = "facedir",
legacy_facedir_simple = true,
groups = {
snappy=2,
choppy=2,
oddly_breakable_by_hand=2
},
sounds = def.sounds or default.node_sound_wood_defaults(),
recipe = def.recipe, -- TODO this is buggy, reason why all models except from toolbox have their own extra call to minetest.register_craft - TODO possibily other similar bugs?
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("owner", placer:get_player_name() or "")
meta:set_string("infotext", S("@1 (owned by @2)", def.description, meta:get_string("owner")))
end,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local formspec_str = def.formspec or generate_formspec_string(def.size)
meta:set_string("formspec", formspec_str)
meta:set_string("infotext", def.description)
meta:set_string("owner", "")
local inv = meta:get_inventory()
local chest_size = def.size == "big" and 14*5 or 8*4
inv:set_size("main", chest_size)
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:is_empty("main")
end,
}
-- register log actions, NOTE passing an anonymous function to avoid getting the default if not necessary
out.allow_metadata_inventory_move = parse_action(def.allow_metadata_inventory_move, function() actions.get_allow_metadata_inventory_move{def.type} end)
out.allow_metadata_inventory_put = parse_action(def.allow_metadata_inventory_put, function() actions.get_allow_metadata_inventory_put{def.type} end)
out.allow_metadata_inventory_take = parse_action(def.allow_metadata_inventory_take, function() actions.get_allow_metadata_inventory_take{def.type} end)
out.on_metadata_inventory_move = parse_action(def.on_metadata_inventory_move, function() actions.get_on_metadata_inventory_move(def.type) end)
out.on_metadata_inventory_put = parse_action(def.on_metadata_inventory_put, function() actions.get_on_metadata_inventory_put(def.type) end)
out.on_metadata_inventory_take = parse_action(def.on_metadata_inventory_take, function() actions.get_on_metadata_inventory_take(def.type) end)
-- if model is not a simple block handle node_box attribute
if def.node_box then
out.drawtype = "nodebox"
out.node_box = {
type = "fixed",
fixed = def.node_box,
}
end
-- add pipeworks compatibility, TODO needs proper testing
if def.pipeworks_enabled == true then
out.groups.tubedevice = 1
out.groups.tubedevice_receiver = 1
out.tube = {
insert_object = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:add_item("main", stack)
end,
can_insert = function(pos, node, stack, direction)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return inv:room_for_item("main", stack)
end,
input_inventory = "main",
connect_sides = {left = 1, right = 1, back = 1, front = 1, bottom = 1, top = 1}
}
end
return out
end
return generate_chest_def

38
utils/formspec.lua Normal file
View File

@ -0,0 +1,38 @@
function generate(size)
if size == "small" then
cfg = {
window_width = 8,
window_height = 9,
chest_width = 8,
chest_height = 4,
}
elseif size == "big" then
cfg = {
window_width = 14,
window_height = 10,
chest_width = 14,
chest_height = 5,
}
end
-- calc padding to vertically align center the chest and the player's inventory
local player_inv_y_orig = cfg.chest_height + 0.85
local player_inv_x_orig = (cfg.window_width - 8) / 2 -- 8=player_inv_width
return "size[" ..
cfg.window_width .. "," .. cfg.window_height .. "]" ..
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
"list[current_name;main;0,0.3;" ..
cfg.chest_width .. "," .. cfg.chest_height .. ";]" ..
"list[current_player;main;" ..
player_inv_x_orig .. "," .. player_inv_y_orig ..
";8,1;]" ..
"list[current_player;main;" ..
player_inv_x_orig .. "," .. (player_inv_y_orig + 1.15) ..
";8,3;8]" ..
"listring[current_name;main]" ..
"listring[current_player;main]" ..
default.get_hotbar_bg(player_inv_x_orig, player_inv_y_orig)
end
return generate