Updated code based on reviews

This commit is contained in:
Dehydrate6684 2023-10-09 13:55:08 +08:00
parent cc217d0089
commit 0c48a46f7c
Signed by untrusted user: Dehydrate6684
GPG Key ID: 5926A2FA9B7653EF
2 changed files with 83 additions and 87 deletions

View File

@ -17,15 +17,17 @@ end
---@param ladder integer The param2 value of the ladder. ---@param ladder integer The param2 value of the ladder.
---@param trapdoor integer The param2 value of the trapdoor. ---@param trapdoor integer The param2 value of the trapdoor.
---@return boolean If the ladder and trapdoor are in the same direction. ---@return boolean If the ladder and trapdoor are in the same direction.
local function check_direction(ladder, trapdoor) function mcl_core.check_direction(ladder, trapdoor)
local convert_table = {}; local convert_table = {
convert_table[2] = { 1, 23 } nil,
convert_table[3] = { 3, 21 } { 1, 23 },
convert_table[4] = { 0, 20 } { 3, 21 },
convert_table[5] = { 2, 22 } { 0, 20 },
{ 2, 22 },
}
local conversion = convert_table[ladder]; local conversion = convert_table[ladder];
if conversion == nil then if not conversion then
return false return false
elseif conversion[1] == trapdoor or conversion[2] == trapdoor then elseif conversion[1] == trapdoor or conversion[2] == trapdoor then
return true return true
@ -42,7 +44,10 @@ end
local function update_trapdoor(pos, ladder, event) local function update_trapdoor(pos, ladder, event)
local top_pos = vector.add(pos, { x = 0, y = 1, z = 0 }) local top_pos = vector.add(pos, { x = 0, y = 1, z = 0 })
local top_node = minetest.get_node_or_nil(top_pos) local top_node = minetest.get_node_or_nil(top_pos)
if top_node then
if top_node and minetest.get_item_group(top_node.name, "trapdoor") == 2
and mcl_core.check_direction(ladder, top_node.param2) then
local new_name = top_node.name local new_name = top_node.name
if event == "place" then if event == "place" then
new_name = string.gsub(new_name, "open$", "ladder") new_name = string.gsub(new_name, "open$", "ladder")
@ -50,26 +55,23 @@ local function update_trapdoor(pos, ladder, event)
new_name = string.gsub(new_name, "ladder$", "open") new_name = string.gsub(new_name, "ladder$", "open")
end end
local is_trapdoor = minetest.get_item_group(top_node.name, "trapdoor")
-- If node above is an opened trapdoor -- If node above is an opened trapdoor
if is_trapdoor == 2 and check_direction(ladder, top_node.param2) then minetest.swap_node(top_pos, {
minetest.swap_node(top_pos, { name = new_name,
name = new_name, param1 = top_node.param1,
param1 = top_node.param1, param2 = top_node.param2,
param2 = top_node.param2, })
})
end
end end
end end
-- TODO: Move ladders into their own API. -- TODO: Move ladders into their own API.
minetest.register_node("mcl_core:ladder", { minetest.register_node("mcl_core:ladder", {
description = S("Ladder"), description = S("Ladder"),
_doc_items_longdesc = S("A piece of ladder which allows you to climb vertically. Ladders can only be placed on the side of solid blocks and not on glass, leaves, ice, slabs, glowstone, nor sea lanterns."), _doc_items_longdesc = S(
"A piece of ladder which allows you to climb vertically. Ladders can only be placed on the side of solid blocks and not on glass, leaves, ice, slabs, glowstone, nor sea lanterns."),
drawtype = "signlike", drawtype = "signlike",
is_ground_content = false, is_ground_content = false,
tiles = {"default_ladder.png"}, tiles = { "default_ladder.png" },
inventory_image = "default_ladder.png", inventory_image = "default_ladder.png",
wield_image = "default_ladder.png", wield_image = "default_ladder.png",
paramtype = "light", paramtype = "light",
@ -79,11 +81,11 @@ minetest.register_node("mcl_core:ladder", {
climbable = true, climbable = true,
node_box = { node_box = {
type = "wallmounted", type = "wallmounted",
wall_side = { -0.5, -0.5, -0.5, -7/16, 0.5, 0.5 }, wall_side = { -0.5, -0.5, -0.5, -7 / 16, 0.5, 0.5 },
}, },
selection_box = { selection_box = {
type = "wallmounted", type = "wallmounted",
wall_side = { -0.5, -0.5, -0.5, -7/16, 0.5, 0.5 }, wall_side = { -0.5, -0.5, -0.5, -7 / 16, 0.5, 0.5 },
}, },
stack_max = 64, stack_max = 64,
groups = { groups = {
@ -113,7 +115,7 @@ minetest.register_node("mcl_core:ladder", {
-- Don't allow to place the ladder at particular nodes -- Don't allow to place the ladder at particular nodes
if (groups and (groups.glass or groups.leaves or groups.slab)) or if (groups and (groups.glass or groups.leaves or groups.slab)) or
node.name == "mcl_core:ladder" or node.name == "mcl_core:ice" or node.name == "mcl_nether:glowstone" or node.name == "mcl_ocean:sea_lantern" then node.name == "mcl_core:ladder" or node.name == "mcl_core:ice" or node.name == "mcl_nether:glowstone" or node.name == "mcl_ocean:sea_lantern" then
return itemstack return itemstack
end end
@ -135,7 +137,7 @@ minetest.register_node("mcl_core:ladder", {
if success then if success then
if idef.sounds and idef.sounds.place then if idef.sounds and idef.sounds.place then
minetest.sound_play(idef.sounds.place, {pos=above, gain=1}, true) minetest.sound_play(idef.sounds.place, { pos = above, gain = 1 }, true)
end end
end end
return itemstack return itemstack
@ -155,9 +157,10 @@ minetest.register_node("mcl_core:ladder", {
minetest.register_node("mcl_core:vine", { minetest.register_node("mcl_core:vine", {
description = S("Vines"), description = S("Vines"),
_doc_items_longdesc = S("Vines are climbable blocks which can be placed on the sides of solid full-cube blocks. Vines slowly grow and spread."), _doc_items_longdesc = S(
"Vines are climbable blocks which can be placed on the sides of solid full-cube blocks. Vines slowly grow and spread."),
drawtype = "signlike", drawtype = "signlike",
tiles = {"mcl_core_vine.png"}, tiles = { "mcl_core_vine.png" },
color = "#48B518", color = "#48B518",
inventory_image = "mcl_core_vine.png", inventory_image = "mcl_core_vine.png",
wield_image = "mcl_core_vine.png", wield_image = "mcl_core_vine.png",
@ -173,9 +176,18 @@ minetest.register_node("mcl_core:vine", {
}, },
stack_max = 64, stack_max = 64,
groups = { groups = {
handy = 1, axey = 1, shearsy = 1, swordy = 1, deco_block = 1, handy = 1,
dig_by_piston = 1, destroy_by_lava_flow = 1, compostability = 50, axey = 1,
flammable = 2, fire_encouragement = 15, fire_flammability = 100, foliage_palette_wallmounted = 1 shearsy = 1,
swordy = 1,
deco_block = 1,
dig_by_piston = 1,
destroy_by_lava_flow = 1,
compostability = 50,
flammable = 2,
fire_encouragement = 15,
fire_flammability = 100,
foliage_palette_wallmounted = 1
}, },
sounds = mcl_sounds.node_sound_leaves_defaults(), sounds = mcl_sounds.node_sound_leaves_defaults(),
drop = "", drop = "",
@ -217,7 +229,7 @@ minetest.register_node("mcl_core:vine", {
if success then if success then
if idef.sounds and idef.sounds.place then if idef.sounds and idef.sounds.place then
minetest.sound_play(idef.sounds.place, {pos=above, gain=1}, true) minetest.sound_play(idef.sounds.place, { pos = above, gain = 1 }, true)
end end
end end
return itemstack return itemstack
@ -240,7 +252,7 @@ minetest.register_node("mcl_core:vine", {
-- If dug, also dig a “dependant” vine below it. -- If dug, also dig a “dependant” vine below it.
-- A vine is dependant if it hangs from this node and has no supporting block. -- A vine is dependant if it hangs from this node and has no supporting block.
on_dig = function(pos, node, digger) on_dig = function(pos, node, digger)
local below = vector.offset(pos,0,-1,0) local below = vector.offset(pos, 0, -1, 0)
local belownode = minetest.get_node(below) local belownode = minetest.get_node(below)
minetest.node_dig(pos, node, digger) minetest.node_dig(pos, node, digger)
if belownode.name == node.name and (not mcl_core.check_vines_supported(below, belownode)) then if belownode.name == node.name and (not mcl_core.check_vines_supported(below, belownode)) then

View File

@ -48,29 +48,6 @@ if minetest.get_modpath("screwdriver") then
end end
end end
---Checks the direction (param2) of a ladder and a trapdoor and determine if they are
---facing the same direction.
---
---@param ladder integer The param2 value of the ladder.
---@param trapdoor integer The param2 value of the trapdoor.
---@return boolean If the ladder and trapdoor are in the same direction.
function check_direction(ladder, trapdoor)
local convert_table = {};
convert_table[2] = { 1, 23 }
convert_table[3] = { 3, 21 }
convert_table[4] = { 0, 20 }
convert_table[5] = { 2, 22 }
local conversion = convert_table[ladder];
if conversion == nil then
return false
elseif conversion[1] == trapdoor or conversion[2] == trapdoor then
return true
end
return false
end
function mcl_doors:register_trapdoor(name, def) function mcl_doors:register_trapdoor(name, def)
local groups = table.copy(def.groups) local groups = table.copy(def.groups)
if groups == nil then if groups == nil then
@ -102,7 +79,7 @@ function mcl_doors:register_trapdoor(name, def)
-- Checking if there is something underneath the trapdoor -- Checking if there is something underneath the trapdoor
if bottom_node then if bottom_node then
local is_ladder = minetest.get_item_group(bottom_node.name, "ladder") local is_ladder = minetest.get_item_group(bottom_node.name, "ladder")
local same_direction = check_direction(bottom_node.param2, me.param2) local same_direction = mcl_core.check_direction(bottom_node.param2, me.param2)
if is_ladder > 0 and same_direction then if is_ladder > 0 and same_direction then
name_end = "_ladder" name_end = "_ladder"
end end
@ -124,20 +101,22 @@ function mcl_doors:register_trapdoor(name, def)
longdesc = def._doc_items_longdesc longdesc = def._doc_items_longdesc
if not longdesc then if not longdesc then
if def.only_redstone_can_open then if def.only_redstone_can_open then
longdesc = S("Trapdoors are horizontal barriers which can be opened or closed and climbed like a ladder when open. They occupy the upper or lower part of a block, depending on how they have been placed. This trapdoor can only be opened or closed by redstone power.") longdesc = S(
"Trapdoors are horizontal barriers which can be opened or closed and climbed like a ladder when open. They occupy the upper or lower part of a block, depending on how they have been placed. This trapdoor can only be opened or closed by redstone power.")
else else
longdesc = S("Trapdoors are horizontal barriers which can be opened or closed and climbed like a ladder when open. They occupy the upper or lower part of a block, depending on how they have been placed. This trapdoor can be opened or closed by hand or redstone power.") longdesc = S(
"Trapdoors are horizontal barriers which can be opened or closed and climbed like a ladder when open. They occupy the upper or lower part of a block, depending on how they have been placed. This trapdoor can be opened or closed by hand or redstone power.")
end end
end end
usagehelp = def._doc_items_usagehelp usagehelp = def._doc_items_usagehelp
if not usagehelp and not def.only_redstone_can_open then if not usagehelp and not def.only_redstone_can_open then
usagehelp = S("To open or close this trapdoor, rightclick it or send a redstone signal to it.") usagehelp = S("To open or close this trapdoor, rightclick it or send a redstone signal to it.")
end end
if def.only_redstone_can_open then if def.only_redstone_can_open then
tt_help = S("Openable by redstone power") tt_help = S("Openable by redstone power")
else else
tt_help = S("Openable by players and redstone power") tt_help = S("Openable by players and redstone power")
end end
-- Closed trapdoor -- Closed trapdoor
@ -163,7 +142,7 @@ function mcl_doors:register_trapdoor(name, def)
_doc_items_usagehelp = usagehelp, _doc_items_usagehelp = usagehelp,
drawtype = "nodebox", drawtype = "nodebox",
tiles = tiles_closed, tiles = tiles_closed,
use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "clip" or true, use_texture_alpha = "clip",
inventory_image = def.inventory_image, inventory_image = def.inventory_image,
wield_image = def.wield_image, wield_image = def.wield_image,
is_ground_content = false, is_ground_content = false,
@ -178,13 +157,15 @@ function mcl_doors:register_trapdoor(name, def)
node_box = { node_box = {
type = "fixed", type = "fixed",
fixed = { fixed = {
{-8/16, -8/16, -8/16, 8/16, -5/16, 8/16},}, { -8 / 16, -8 / 16, -8 / 16, 8 / 16, -5 / 16, 8 / 16 }, },
},
mesecons = {
effector = {
action_on = (function(pos, node)
punch(pos)
end),
}
}, },
mesecons = {effector = {
action_on = (function(pos, node)
punch(pos)
end),
}},
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
local p0 = pointed_thing.under local p0 = pointed_thing.under
local p1 = pointed_thing.above local p1 = pointed_thing.above
@ -199,7 +180,7 @@ function mcl_doors:register_trapdoor(name, def)
--local origname = itemstack:get_name() --local origname = itemstack:get_name()
if p0.y - 1 == p1.y or (fpos > 0 and fpos < 0.5) if p0.y - 1 == p1.y or (fpos > 0 and fpos < 0.5)
or (fpos < -0.5 and fpos > -0.999999999) then or (fpos < -0.5 and fpos > -0.999999999) then
param2 = param2 + 20 param2 = param2 + 20
if param2 == 21 then if param2 == 21 then
param2 = 23 param2 = 23
@ -229,10 +210,10 @@ function mcl_doors:register_trapdoor(name, def)
groups_open.trapdoor = 2 groups_open.trapdoor = 2
groups_open.not_in_creative_inventory = 1 groups_open.not_in_creative_inventory = 1
-- Non-climbable opened -- Non-climbable opened
minetest.register_node(name.."_open", { minetest.register_node(name .. "_open", {
drawtype = "nodebox", drawtype = "nodebox",
tiles = tiles_open, tiles = tiles_open,
use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "clip" or true, use_texture_alpha = "clip",
is_ground_content = false, is_ground_content = false,
paramtype = "light", paramtype = "light",
paramtype2 = "facedir", paramtype2 = "facedir",
@ -245,22 +226,24 @@ function mcl_doors:register_trapdoor(name, def)
drop = name, drop = name,
node_box = { node_box = {
type = "fixed", type = "fixed",
fixed = {-0.5, -0.5, 5/16, 0.5, 0.5, 0.5} fixed = { -0.5, -0.5, 5 / 16, 0.5, 0.5, 0.5 }
}, },
on_rightclick = on_rightclick, on_rightclick = on_rightclick,
mesecons = {effector = { mesecons = {
action_off = (function(pos, node) effector = {
punch(pos) action_off = (function(pos, node)
end), punch(pos)
}}, end),
}
},
on_rotate = on_rotate, on_rotate = on_rotate,
}) })
-- Climbable opened -- Climbable opened
minetest.register_node(name.."_ladder", { minetest.register_node(name .. "_ladder", {
drawtype = "nodebox", drawtype = "nodebox",
tiles = tiles_open, tiles = tiles_open,
use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "clip" or true, use_texture_alpha = "clip",
is_ground_content = false, is_ground_content = false,
paramtype = "light", paramtype = "light",
paramtype2 = "facedir", paramtype2 = "facedir",
@ -274,19 +257,20 @@ function mcl_doors:register_trapdoor(name, def)
drop = name, drop = name,
node_box = { node_box = {
type = "fixed", type = "fixed",
fixed = {-0.5, -0.5, 5/16, 0.5, 0.5, 0.5} fixed = { -0.5, -0.5, 5 / 16, 0.5, 0.5, 0.5 }
}, },
on_rightclick = on_rightclick, on_rightclick = on_rightclick,
mesecons = {effector = { mesecons = {
action_off = (function(pos, node) effector = {
punch(pos) action_off = (function(pos, node)
end), punch(pos)
}}, end),
}
},
on_rotate = on_rotate, on_rotate = on_rotate,
}) })
if minetest.get_modpath("doc") then if minetest.get_modpath("doc") then
doc.add_entry_alias("nodes", name, "nodes", name.."_open") doc.add_entry_alias("nodes", name, "nodes", name .. "_open")
end end
end end