Add comments and drop items when destroyed

This commit is contained in:
ChrisPHP 2023-08-01 18:17:48 +01:00 committed by the-real-herowl
parent 07d2759ae4
commit 356045b3e3

@ -7,6 +7,7 @@
local S = minetest.get_translator("mcl_stonecutter") local S = minetest.get_translator("mcl_stonecutter")
-- compatible items for the stonecutter
local compaitble_items = { local compaitble_items = {
"mcl_core:cobble", "mcl_core:cobble",
"mcl_core:mossycobble", "mcl_core:mossycobble",
@ -41,11 +42,13 @@ local FMT = {
item_image_button = "item_image_button[%f,%f;%f,%f;%s;%s;%s]", item_image_button = "item_image_button[%f,%f;%f,%f;%s;%s;%s]",
} }
-- formspecs
local function show_stonecutter_formspec(items, input) local function show_stonecutter_formspec(items, input)
local cut_items = {} local cut_items = {}
local x_len = 0 local x_len = 0
local y_len = 0.5 local y_len = 0.5
-- This loops through all the items that can be made and inserted into the formspec
if items ~= nil then if items ~= nil then
for index, value in pairs(items) do for index, value in pairs(items) do
x_len = x_len + 1 x_len = x_len + 1
@ -79,6 +82,7 @@ local function show_stonecutter_formspec(items, input)
return formspec return formspec
end end
-- Strips the start of the item like "mcl_core:" and removes any numbers or whitespaces after it
local function get_item_string_name(input) local function get_item_string_name(input)
local colonIndex = string.find(input, ":") local colonIndex = string.find(input, ":")
if colonIndex then if colonIndex then
@ -94,6 +98,7 @@ local function get_item_string_name(input)
end end
end end
-- Simply checks if the item is compaitble with the stonecutter
local function is_input_in_table(element) local function is_input_in_table(element)
for _, value in ipairs(compaitble_items) do for _, value in ipairs(compaitble_items) do
if value == element then if value == element then
@ -103,20 +108,25 @@ local function is_input_in_table(element)
return false return false
end end
-- Updates the formspec
local function update_stonecutter_slots(meta) local function update_stonecutter_slots(meta)
local inv = meta:get_inventory() local inv = meta:get_inventory()
local input = inv:get_stack("input", 1) local input = inv:get_stack("input", 1)
local name = input:get_name() local name = input:get_name()
local name_stripped = get_item_string_name(input:to_string())
local cuttable_recipes = {}
local new_output = meta:get_string("cut_stone") local new_output = meta:get_string("cut_stone")
-- Checks if input is in the array
if is_input_in_table(name) then if is_input_in_table(name) then
local cuttable_recipes = {}
local name_stripped = get_item_string_name(input:to_string())
if name_stripped ~= "" then if name_stripped ~= "" then
-- Strings for the possible items it can craft into
local stair = "mcl_stairs:stair_"..name_stripped local stair = "mcl_stairs:stair_"..name_stripped
local slab = "mcl_stairs:slab_"..name_stripped local slab = "mcl_stairs:slab_"..name_stripped
local wall = "mcl_walls:"..name_stripped local wall = "mcl_walls:"..name_stripped
local smooth = "mcl_core:"..name_stripped.."_smooth" local smooth = "mcl_core:"..name_stripped.."_smooth"
-- Goes through and checks if the item exists and inserts it into the table
if minetest.registered_items[slab] ~= nil then if minetest.registered_items[slab] ~= nil then
table.insert(cuttable_recipes, slab) table.insert(cuttable_recipes, slab)
end end
@ -143,7 +153,9 @@ local function update_stonecutter_slots(meta)
else else
meta:set_string("formspec", show_stonecutter_formspec(nil)) meta:set_string("formspec", show_stonecutter_formspec(nil))
end end
if new_output = '' then
-- Checks if the chosen item is a slab or not, if it's a slab set the output to be a stack of 2
if new_output ~= '' then
cut_item = ItemStack(new_output) cut_item = ItemStack(new_output)
if string.find(new_output, "mcl_stairs:slab_") then if string.find(new_output, "mcl_stairs:slab_") then
cut_item:set_count(2) cut_item:set_count(2)
@ -154,6 +166,18 @@ local function update_stonecutter_slots(meta)
end end
end end
-- Only drop the items that were in the input slot
local function drop_stonecutter_items(pos, meta)
local inv = meta:get_inventory()
for i=1, inv:get_size("input") do
local stack = inv:get_stack("input", i)
if not stack:is_empty() then
local p = {x=pos.x+math.random(0, 10)/10-0.5, y=pos.y, z=pos.z+math.random(0, 10)/10-0.5}
minetest.add_item(p, stack)
end
end
end
minetest.register_node("mcl_stonecutter:stonecutter", { minetest.register_node("mcl_stonecutter:stonecutter", {
description = S("Stone Cutter"), description = S("Stone Cutter"),
_tt_help = S("Used to cut stone like materials."), _tt_help = S("Used to cut stone like materials."),
@ -194,6 +218,13 @@ minetest.register_node("mcl_stonecutter:stonecutter", {
_mcl_hardness = 3.5, _mcl_hardness = 3.5,
sounds = mcl_sounds.node_sound_stone_defaults(), sounds = mcl_sounds.node_sound_stone_defaults(),
after_dig_node = function(pos, oldnode, oldmetadata, digger)
local meta = minetest.get_meta(pos)
local meta2 = meta:to_table()
meta:from_table(oldmetadata)
drop_stonecutter_items(pos, meta)
meta:from_table(meta2)
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player) allow_metadata_inventory_take = function(pos, listname, index, stack, player)
local name = player:get_player_name() local name = player:get_player_name()
if minetest.is_protected(pos, name) then if minetest.is_protected(pos, name) then
@ -203,6 +234,25 @@ minetest.register_node("mcl_stonecutter:stonecutter", {
return stack:get_count() return stack:get_count()
end end
end, end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local name = player:get_player_name()
if minetest.is_protected(pos, name) then
minetest.record_protection_violation(pos, name)
return 0
elseif to_list == "output" then
return 0
elseif from_list == "output" and to_list == "input" then
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if inv:get_stack(to_list, to_index):is_empty() then
return count
else
return 0
end
else
return count
end
end,
on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
if from_list == "output" and to_list == "input" then if from_list == "output" and to_list == "input" then