Skip to content

Commit

Permalink
Artisan Benches: Grinder: extract common definition & inventory_callb…
Browse files Browse the repository at this point in the history
…acks. Relates to #1787
  • Loading branch information
alek13 committed Nov 15, 2024
1 parent f2a4e8f commit 600afa3
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 205 deletions.
151 changes: 28 additions & 123 deletions mods/lord/Blocks/grinder/src/grinder/definition/node/active.lua
Original file line number Diff line number Diff line change
@@ -1,130 +1,35 @@
local SL = minetest.get_mod_translator()

local form = require('grinder.definition.node.form')
local common = require('grinder.definition.node.common')
local inventory_callbacks = require('grinder.definition.node.inventory_callbacks')

return {

--- @param width number Width of a frame in pixels.
--- @param height number Height of a frame in pixels.
--- @param length number Full loop length.
local function animation_vf(width, height, length)
return { type = "vertical_frames", aspect_w = width, aspect_h = height, length = length }
end

--- @param image string image file name
--- @param width number Width of a frame in pixels.
--- @param height number Height of a frame in pixels.
--- @param length number Full loop length.
local function animated_tile(image, width, height, length)
return { image = image, backface_culling = false, animation = animation_vf(width, height, length) }
end


return table.merge(common, table.merge(inventory_callbacks, {
description = SL("Grinder"),
tiles = {
{
image = "grinder_top_active.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 1.6
},
}, "grinder_bottom.png",
{
image = "grinder_side_left_active.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 3.2
},
},
{
image = "grinder_side_right_active.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 3.2
},
},
{
image = "grinder_side_active.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 1.0
},
},
{
image = "grinder_front_active.png",
backface_culling = false,
animation = {
type = "vertical_frames",
aspect_w = 32,
aspect_h = 32,
length = 1.0
},
}
animated_tile("grinder_top_active.png", 32, 32, 1.6),
"grinder_bottom.png",
animated_tile("grinder_side_left_active.png", 32, 32, 3.2),
animated_tile("grinder_side_right_active.png", 32, 32, 3.2),
animated_tile("grinder_side_active.png", 32, 32, 1.0),
animated_tile("grinder_front_active.png", 32, 32, 1.0)
},
paramtype2 = "facedir",
light_source = 8,
drop = "grinder:grinder",
groups = {cracky=2, not_in_creative_inventory=1,hot=1},
legacy_facedir_simple = true,
is_ground_content = false,
sounds = default.node_sound_stone_defaults(),
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", form.get('inactive')) -- почему 'inactive' ?
meta:set_string("infotext", "Grinder");
local inv = meta:get_inventory()
inv:set_size("fuel", 1)
inv:set_size("src", 1)
inv:set_size("dst", 4)
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
if not inv:is_empty("fuel") then
return false
elseif not inv:is_empty("dst") then
return false
elseif not inv:is_empty("src") then
return false
end
return true
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if listname == "fuel" then
if minetest.get_craft_result({method="fuel",width=1,items={stack}}).time ~= 0 then
if inv:is_empty("src") then
meta:set_string("infotext",SL("Grinder is empty"))
end
return stack:get_count()
else
return 0
end
elseif listname == "src" then
return stack:get_count()
elseif listname == "dst" then
return 0
end
end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local stack = inv:get_stack(from_list, from_index)
if to_list == "fuel" then
if minetest.get_craft_result({method="fuel",width=1,items={stack}}).time ~= 0 then
if inv:is_empty("src") then
meta:set_string("infotext",SL("Grinder is empty"))
end
return count
else
return 0
end
elseif to_list == "src" then
return count
elseif to_list == "dst" then
return 0
end
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
return stack:get_count()
end,
}
groups = { not_in_creative_inventory = 1, hot = 1 },
}))
33 changes: 33 additions & 0 deletions mods/lord/Blocks/grinder/src/grinder/definition/node/common.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local SL = minetest.get_mod_translator()

local form = require('grinder.definition.node.form')

return {
paramtype2 = "facedir",
drop = "grinder:grinder",
groups = { cracky = 2 },
legacy_facedir_simple = true,
is_ground_content = false,
sounds = default.node_sound_stone_defaults(),
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", form.get('inactive'))
meta:set_string("infotext", SL("Grinder"))
local inv = meta:get_inventory()
inv:set_size("fuel", 1)
inv:set_size("src", 1)
inv:set_size("dst", 4)
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
if not inv:is_empty("fuel") then
return false
elseif not inv:is_empty("dst") then
return false
elseif not inv:is_empty("src") then
return false
end
return true
end,
}
94 changes: 12 additions & 82 deletions mods/lord/Blocks/grinder/src/grinder/definition/node/inactive.lua
Original file line number Diff line number Diff line change
@@ -1,92 +1,22 @@
local SL = minetest.get_mod_translator()

local form = require('grinder.definition.node.form')
local common = require('grinder.definition.node.common')
local inventory_callbacks = require('grinder.definition.node.inventory_callbacks')
local form = require('grinder.definition.node.form')

return {

return table.merge(common, table.merge(inventory_callbacks, {
description = SL("Grinder"),
tiles = {"grinder_top.png", "carts_steam_mechanismn.png",
"grinder_side_left.png", "grinder_side_right.png",
"grinder_side.png", "grinder_front.png"},
paramtype2 = "facedir",
groups = {cracky=2},
legacy_facedir_simple = true,
is_ground_content = false,
sounds = default.node_sound_stone_defaults(),
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", form.get('inactive'))
meta:set_string("infotext", SL("Grinder"))
local inv = meta:get_inventory()
inv:set_size("fuel", 1)
inv:set_size("src", 1)
inv:set_size("dst", 4)
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
if not inv:is_empty("fuel") then
return false
elseif not inv:is_empty("dst") then
return false
elseif not inv:is_empty("src") then
return false
end
return true
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
if listname == "fuel" then
if minetest.get_craft_result({method="fuel",width=1,items={stack}}).time ~= 0 then
if inv:is_empty("src") then
meta:set_string("infotext", SL("Grinder is empty"))
end
return stack:get_count()
else
return 0
end
elseif listname == "src" then
return stack:get_count()
elseif listname == "dst" then
return 0
end
end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local stack = inv:get_stack(from_list, from_index)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
if to_list == "fuel" then
if minetest.get_craft_result({method="fuel",width=1,items={stack}}).time ~= 0 then
if inv:is_empty("src") then
meta:set_string("infotext", SL("Grinder is empty"))
end
return count
else
return 0
end
elseif to_list == "src" then
return count
elseif to_list == "dst" then
return 0
end
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
return stack:get_count()
end,
tiles = {
"grinder_top.png", "carts_steam_mechanismn.png",
"grinder_side_left.png", "grinder_side_right.png",
"grinder_side.png", "grinder_front.png"
},

-- backwards compatibility: punch to set formspec
on_punch = function(pos,player)
on_punch = function(pos, player)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", SL("Grinder"))
meta:set_string("formspec", form.get('inactive'))
end
}
}))
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
local SL = minetest.get_mod_translator()


return {
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
if listname == "fuel" then
if minetest.get_craft_result({method="fuel",width=1,items={stack}}).time ~= 0 then
if inv:is_empty("src") then
meta:set_string("infotext", SL("Grinder is empty"))
end
return stack:get_count()
else
return 0
end
elseif listname == "src" then
return stack:get_count()
elseif listname == "dst" then
return 0
end
end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local stack = inv:get_stack(from_list, from_index)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
if to_list == "fuel" then
if minetest.get_craft_result({method="fuel",width=1,items={stack}}).time ~= 0 then
if inv:is_empty("src") then
meta:set_string("infotext", SL("Grinder is empty"))
end
return count
else
return 0
end
elseif to_list == "src" then
return count
elseif to_list == "dst" then
return 0
end
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
return stack:get_count()
end,
}

0 comments on commit 600afa3

Please sign in to comment.