Add stonecutter fonctionality

This commit is contained in:
mirqf 2023-11-24 11:52:06 +01:00 committed by AFCMS
parent 7cbba73d50
commit 8ef4cddc38
Signed by: AFCMS
GPG Key ID: 8720389A25B652E3
2 changed files with 123 additions and 14 deletions

View File

@ -1,13 +1,70 @@
--|||||||||||||||||||||||
--||||| STONECUTTER |||||
--|||||||||||||||||||||||
-- TO-DO:
-- * Add GUI
local S = minetest.get_translator(minetest.get_current_modname())
local C = minetest.colorize
local F = minetest.formspec_escape
minetest.register_node("mcl_stonecutter:stonecutter", {
-- Some nodes have special recepies, all of them logged here
local special = {
["mcl_core:stone"] = {"mcl_core:stonebrickcarved", "mcl_stairs:slab_stonebrick 2", "mcl_stairs:stair_stonebrick", "mcl_walls:stonebrick", "mcl_core:stonebrick", "mcl_stairs:slab_stone_rough 2", "mcl_stairs:stair_stone_rough"},
}
local function show_stonecutter_formspec(pos, player)
local inv = minetest.get_meta(pos):get_inventory()
local input = inv:get_stack("input", 1):get_name()
local container_content = ""
local otp = {}
if special[input] then
table.insert_all(otp, special[input])
else
if minetest.get_item_group(input, "material_stone") > 0 and minetest.registered_nodes["mcl_stairs:slab_" .. input:split(":")[2]] then
local material = input:split(":")[2]
for i, grindment in ipairs({"mcl_stairs:slab_%s 2", "mcl_stairs:stair_%s", "mcl_walls:%s"}) do
local output = string.format(grindment, material)
if ItemStack(output):is_known() then table.insert(otp, output) end
end
end
end
for i, new in ipairs(otp) do
container_content = container_content .. string.format("item_image_button[%f,%f;1.1,1.1;%s;%s;]", ((i%4>0 and i%4 or 4)-1)*1.1, (math.ceil(i/4)-1)*1.1, new, new)
end
local formspec = table.concat({
"formspec_version[4]",
"size[11.75,10.425]",
"label[0.375,0.375;" .. F(C(mcl_formspec.label_color, S("Stonecutter"))) .. "]",
-- Input slot
mcl_formspec.get_itemslot_bg_v4(1, 2, 1, 1),
"list[nodemeta:" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ";input;1,2;1,1;]",
-- Container background
"image[3.23,0.85;4.4,3.3;mcl_inventory_background9.png;2]",
-- It should be a scroll container, but no default recepies exist to overflow the limit of 12 buttons.
"container[3.23,0.85]",
container_content,
"container_end[]",
-- Scrollbar
-- TODO: style the scrollbar correctly when possible
"scrollbaroptions[min=0;max=1;smallstep=0;largesteps=0]",
"scrollbar[7.6,0.85;0.75,3.3;vertical;scroll;0]",
-- Output slot
mcl_formspec.get_itemslot_bg_v4(9.75, 2, 1, 1, 0.2),
"list[nodemeta:" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ";output;9.75,2;1,1;]",
-- Player inventory
"label[0.375,4.7;" .. F(C(mcl_formspec.label_color, S("Inventory"))) .. "]",
mcl_formspec.get_itemslot_bg_v4(0.375, 5.1, 9, 3),
"list[current_player;main;0.375,5.1;9,3;9]",
mcl_formspec.get_itemslot_bg_v4(0.375, 9.05, 9, 1),
"list[current_player;main;0.375,9.05;9,1;]",
})
minetest.show_formspec(player:get_player_name(), ("mcl_stonecutter:%f_%f_%f"):format(pos.x, pos.y, pos.z), formspec)
end
minetest.register_node("mcl_functional:stonecutter", {
description = S("Stone Cutter"),
_tt_help = S("Used to cut stone like materials."),
_doc_items_longdesc = S("Stonecutters are used to create stairs and slabs from stone like materials. It is also the jobsite for the Stone Mason Villager."),
@ -45,14 +102,66 @@ minetest.register_node("mcl_stonecutter:stonecutter", {
},
_mcl_blast_resistance = 3.5,
_mcl_hardness = 3.5,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
return (minetest.is_protected(pos, player:get_player_name()) or listname == "output") and 0 or stack:get_count()
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
return minetest.is_protected(pos, player:get_player_name()) and 0 or stack:get_count()
end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
return (minetest.is_protected(pos, player:get_player_name()) or to_list == "output") and 0 or count
end,
on_metadata_inventory_put = function(pos, _, _, _, player)
show_stonecutter_formspec(pos, player)
end,
on_metadata_inventory_take = function(pos, listname, index, stack, player)
local meta = minetest.get_meta(pos)
if listname == "input" then
show_stonecutter_formspec(pos, player)
meta:get_inventory():set_stack("output", 1, "")
else
local input = meta:get_inventory():get_stack("input", 1)
input:take_item(stack:get_count())
meta:get_inventory():set_stack("input", 1, input)
if input:is_empty() then show_stonecutter_formspec(pos, player) end
end
end,
sounds = mcl_sounds.node_sound_stone_defaults(),
on_construct = function(pos)
local inv = minetest.get_meta(pos):get_inventory()
inv:set_size("input", 1)
inv:set_size("output", 1)
end,
on_rightclick = function(pos, node, player, itemstack)
if not player:get_player_control().sneak then
show_stonecutter_formspec(pos, player)
end
end,
after_dig_node = function(pos, _, oldmetadata, _)
local meta = minetest.get_meta(pos)
local meta2 = meta:to_table()
meta:from_table(oldmetadata)
local input = meta:get_inventory():get_stack("input", 1)
if not input:is_empty() then minetest.add_item(vector.offset(pos, math.random(0,10)/10-0.5, 0, math.random(0,10)/10-0.5), input) end
meta:from_table(meta2)
end,
})
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname:find("mcl_stonecutter") then
if fields.quit then return end
local p = formname:split(":")[2]:split("_")
local pos = vector.new(tonumber(p[1]), tonumber(p[2]), tonumber(p[3]))
local item; for k, v in pairs(fields) do if k ~= "scroll" then item = k break end end; if not item then return end
minetest.get_meta(pos):get_inventory():set_stack("output", 1, item)
end
end)
minetest.register_craft({
output = "mcl_stonecutter:stonecutter",
output = "mcl_functional:stonecutter",
recipe = {
{ "", "", "" },
{ "", "mcl_core:iron_ingot", "" },
{ "mcl_core:stone", "mcl_core:stone", "mcl_core:stone" },
{"", "", ""},
{"", "mcl_core:iron_ingot", ""},
{"mcl_core:stone", "mcl_core:stone", "mcl_core:stone"},
}
})

View File

@ -1,4 +1,4 @@
name = mcl_stonecutter
author = PrairieWind
author = PrairieWind, mirqf
description = This mod adds a stonecutter, which is used to cut stone like materials, and used as the jobsite for the Stone Mason Villager.
depends = mcl_sounds
depends = mcl_sounds, mcl_formspec