1
0
Fork 0

Dropper + Dispenser inventory

This commit is contained in:
AFCMS 2022-09-10 11:39:10 +02:00
parent 1065eb4d8c
commit 37176976b6
Signed by untrusted user: AFCMS
GPG Key ID: 8720389A25B652E3
2 changed files with 117 additions and 88 deletions

View File

@ -8,23 +8,36 @@ All node definitions share a lot of code, so this is the reason why there
are so many weird tables below.
]]
local S = minetest.get_translator(minetest.get_current_modname())
local C = minetest.colorize
local F = minetest.formspec_escape
-- For after_place_node
local dispenser_formspec = table.concat({
"formspec_version[4]",
"size[11.75,10.425]",
"label[4.125,0.375;" .. F(C(mcl_formspec.label_color, S("Dispenser"))) .. "]",
mcl_formspec.get_itemslot_bg_v4(4.125, 0.75, 3, 3),
"list[context;main;4.125,0.75;3,3;]",
"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;]",
"listring[context;main]",
"listring[current_player;main]",
})
---For after_place_node
---@param pos Vector
local function setup_dispenser(pos)
-- Set formspec and inventory
local form = "size[9,8.75]"..
"label[0,4.0;"..minetest.formspec_escape(minetest.colorize("#313131", S("Inventory"))).."]"..
"list[current_player;main;0,4.5;9,3;9]"..
mcl_formspec.get_itemslot_bg(0,4.5,9,3)..
"list[current_player;main;0,7.74;9,1;]"..
mcl_formspec.get_itemslot_bg(0,7.74,9,1)..
"label[3,0;"..minetest.formspec_escape(minetest.colorize("#313131", S("Dispenser"))).."]"..
"list[context;main;3,0.5;3,3;]"..
mcl_formspec.get_itemslot_bg(3,0.5,3,3)..
"listring[context;main]"..
"listring[current_player;main]"
local meta = minetest.get_meta(pos)
meta:set_string("formspec", form)
meta:set_string("formspec", dispenser_formspec)
local inv = meta:get_inventory()
inv:set_size("main", 9)
end
@ -38,9 +51,9 @@ local function orientate_dispenser(pos, placer)
local node = minetest.get_node(pos)
if pitch > 55 then
minetest.swap_node(pos, {name="mcl_dispensers:dispenser_up", param2 = node.param2})
minetest.swap_node(pos, { name = "mcl_dispensers:dispenser_up", param2 = node.param2 })
elseif pitch < -55 then
minetest.swap_node(pos, {name="mcl_dispensers:dispenser_down", param2 = node.param2})
minetest.swap_node(pos, { name = "mcl_dispensers:dispenser_down", param2 = node.param2 })
end
end
@ -85,10 +98,10 @@ local dispenserdef = {
local meta2 = meta:to_table()
meta:from_table(oldmetadata)
local inv = meta:get_inventory()
for i=1, inv:get_size("main") do
for i = 1, inv:get_size("main") do
local stack = inv:get_stack("main", 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}
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
@ -107,19 +120,19 @@ local dispenserdef = {
dropdir = vector.multiply(minetest.facedir_to_dir(node.param2), -1)
droppos = vector.add(pos, dropdir)
elseif node.name == "mcl_dispensers:dispenser_up" then
dropdir = {x=0, y=1, z=0}
droppos = {x=pos.x, y=pos.y+1, z=pos.z}
dropdir = { x = 0, y = 1, z = 0 }
droppos = { x = pos.x, y = pos.y + 1, z = pos.z }
elseif node.name == "mcl_dispensers:dispenser_down" then
dropdir = {x=0, y=-1, z=0}
droppos = {x=pos.x, y=pos.y-1, z=pos.z}
dropdir = { x = 0, y = -1, z = 0 }
droppos = { x = pos.x, y = pos.y - 1, z = pos.z }
end
local dropnode = minetest.get_node(droppos)
local dropnodedef = minetest.registered_nodes[dropnode.name]
local stacks = {}
for i=1,inv:get_size("main") do
for i = 1, inv:get_size("main") do
local stack = inv:get_stack("main", i)
if not stack:is_empty() then
table.insert(stacks, {stack = stack, stackpos = i})
table.insert(stacks, { stack = stack, stackpos = i })
end
end
if #stacks >= 1 then
@ -143,9 +156,10 @@ local dispenserdef = {
-- Armor, mob heads and pumpkins
if igroups.armor then
local droppos_below = {x = droppos.x, y = droppos.y - 1, z = droppos.z}
local droppos_below = { x = droppos.x, y = droppos.y - 1, z = droppos.z }
for _, objs in ipairs({minetest.get_objects_inside_radius(droppos, 1), minetest.get_objects_inside_radius(droppos_below, 1)}) do
for _, objs in ipairs({ minetest.get_objects_inside_radius(droppos, 1),
minetest.get_objects_inside_radius(droppos_below, 1) }) do
for _, obj in ipairs(objs) do
stack = mcl_armor.equip(stack, obj)
if stack:is_empty() then
@ -161,7 +175,7 @@ local dispenserdef = {
if not stack:is_empty() then
if igroups.head or iname == "mcl_farming:pumpkin_face" then
if dropnodedef.buildable_to then
minetest.set_node(droppos, {name = iname, param2 = node.param2})
minetest.set_node(droppos, { name = iname, param2 = node.param2 })
stack:take_item()
end
end
@ -263,7 +277,7 @@ local dispenserdef = {
local item_entity = minetest.add_item(droppos, dropitem)
local drop_vel = vector.subtract(droppos, pos)
local speed = 3
item_entity:set_velocity(vector.multiply(drop_vel,speed))
item_entity:set_velocity(vector.multiply(drop_vel, speed))
end
else
stack:take_item()
@ -280,7 +294,7 @@ local dispenserdef = {
local item_entity = minetest.add_item(droppos, dropitem)
local drop_vel = vector.subtract(droppos, pos)
local speed = 3
item_entity:set_velocity(vector.multiply(drop_vel,speed))
item_entity:set_velocity(vector.multiply(drop_vel, speed))
stack:take_item()
inv:set_stack("main", stack_id, stack)
end
@ -299,27 +313,28 @@ local dispenserdef = {
local horizontal_def = table.copy(dispenserdef)
horizontal_def.description = S("Dispenser")
horizontal_def._tt_help = S("9 inventory slots").."\n"..S("Launches item when powered by redstone power")
horizontal_def._tt_help = S("9 inventory slots") .. "\n" .. S("Launches item when powered by redstone power")
horizontal_def._doc_items_longdesc = S("A dispenser is a block which acts as a redstone component which, when powered with redstone power, dispenses an item. It has a container with 9 inventory slots.")
horizontal_def._doc_items_usagehelp = S("Place the dispenser in one of 6 possible directions. The “hole” is where items will fly out of the dispenser. Use the dispenser to access its inventory. Insert the items you wish to dispense. Supply the dispenser with redstone energy once to dispense a random item.").."\n\n"..
horizontal_def._doc_items_usagehelp = S("Place the dispenser in one of 6 possible directions. The “hole” is where items will fly out of the dispenser. Use the dispenser to access its inventory. Insert the items you wish to dispense. Supply the dispenser with redstone energy once to dispense a random item.")
.. "\n\n" ..
S("The dispenser will do different things, depending on the dispensed item:").."\n\n"..
S("The dispenser will do different things, depending on the dispensed item:") .. "\n\n" ..
S("• Arrows: Are launched").."\n"..
S("• Eggs and snowballs: Are thrown").."\n"..
S("• Fire charges: Are fired in a straight line").."\n"..
S("• Armor: Will be equipped to players and armor stands").."\n"..
S("• Boats: Are placed on water or are dropped").."\n"..
S("• Minecart: Are placed on rails or are dropped").."\n"..
S("• Bone meal: Is applied on the block it is facing").."\n"..
S("• Empty buckets: Are used to collect a liquid source").."\n"..
S("• Filled buckets: Are used to place a liquid source").."\n"..
S("• Heads, pumpkins: Equipped to players and armor stands, or placed as a block").."\n"..
S("• Shulker boxes: Are placed as a block").."\n"..
S("• TNT: Is placed and ignited").."\n"..
S("• Flint and steel: Is used to ignite a fire in air and to ignite TNT").."\n"..
S("• Spawn eggs: Will summon the mob they contain").."\n"..
S("• Other items: Are simply dropped")
S("• Arrows: Are launched") .. "\n" ..
S("• Eggs and snowballs: Are thrown") .. "\n" ..
S("• Fire charges: Are fired in a straight line") .. "\n" ..
S("• Armor: Will be equipped to players and armor stands") .. "\n" ..
S("• Boats: Are placed on water or are dropped") .. "\n" ..
S("• Minecart: Are placed on rails or are dropped") .. "\n" ..
S("• Bone meal: Is applied on the block it is facing") .. "\n" ..
S("• Empty buckets: Are used to collect a liquid source") .. "\n" ..
S("• Filled buckets: Are used to place a liquid source") .. "\n" ..
S("• Heads, pumpkins: Equipped to players and armor stands, or placed as a block") .. "\n" ..
S("• Shulker boxes: Are placed as a block") .. "\n" ..
S("• TNT: Is placed and ignited") .. "\n" ..
S("• Flint and steel: Is used to ignite a fire in air and to ignite TNT") .. "\n" ..
S("• Spawn eggs: Will summon the mob they contain") .. "\n" ..
S("• Other items: Are simply dropped")
function horizontal_def.after_place_node(pos, placer, itemstack, pointed_thing)
setup_dispenser(pos)
@ -332,7 +347,7 @@ horizontal_def.tiles = {
"default_furnace_side.png", "mcl_dispensers_dispenser_front_horizontal.png"
}
horizontal_def.paramtype2 = "facedir"
horizontal_def.groups = {pickaxey=1, container=2, material_stone=1}
horizontal_def.groups = { pickaxey = 1, container = 2, material_stone = 1 }
minetest.register_node("mcl_dispensers:dispenser", horizontal_def)
@ -345,7 +360,7 @@ down_def.tiles = {
"default_furnace_side.png", "default_furnace_side.png",
"default_furnace_side.png", "default_furnace_side.png"
}
down_def.groups = {pickaxey=1, container=2,not_in_creative_inventory=1, material_stone=1}
down_def.groups = { pickaxey = 1, container = 2, not_in_creative_inventory = 1, material_stone = 1 }
down_def._doc_items_create_entry = false
down_def.drop = "mcl_dispensers:dispenser"
minetest.register_node("mcl_dispensers:dispenser_down", down_def)
@ -365,9 +380,9 @@ minetest.register_node("mcl_dispensers:dispenser_up", up_def)
minetest.register_craft({
output = "mcl_dispensers:dispenser",
recipe = {
{"mcl_core:cobble", "mcl_core:cobble", "mcl_core:cobble",},
{"mcl_core:cobble", "mcl_bows:bow", "mcl_core:cobble",},
{"mcl_core:cobble", "mesecons:redstone", "mcl_core:cobble",},
{ "mcl_core:cobble", "mcl_core:cobble", "mcl_core:cobble", },
{ "mcl_core:cobble", "mcl_bows:bow", "mcl_core:cobble", },
{ "mcl_core:cobble", "mesecons:redstone", "mcl_core:cobble", },
}
})
@ -384,6 +399,6 @@ minetest.register_lbm({
nodenames = { "mcl_dispensers:dispenser", "mcl_dispensers:dispenser_down", "mcl_dispensers:dispenser_up" },
action = function(pos, node)
setup_dispenser(pos)
minetest.log("action", "[mcl_dispenser] Node formspec updated at "..minetest.pos_to_string(pos))
minetest.log("action", "[mcl_dispenser] Node formspec updated at " .. minetest.pos_to_string(pos))
end,
})

View File

@ -9,23 +9,36 @@ are so many weird tables below.
]]
local S = minetest.get_translator(minetest.get_current_modname())
local C = minetest.colorize
local F = minetest.formspec_escape
-- For after_place_node
local dropper_formspec = table.concat({
"formspec_version[4]",
"size[11.75,10.425]",
"label[4.125,0.375;" .. F(C(mcl_formspec.label_color, S("Dropper"))) .. "]",
mcl_formspec.get_itemslot_bg_v4(4.125, 0.75, 3, 3),
"list[context;main;4.125,0.75;3,3;]",
"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;]",
"listring[context;main]",
"listring[current_player;main]",
})
---For after_place_node
---@param pos Vector
local function setup_dropper(pos)
-- Set formspec and inventory
local form = "size[9,8.75]"..
"label[0,4.0;"..minetest.formspec_escape(minetest.colorize("#313131", S("Inventory"))).."]"..
"list[current_player;main;0,4.5;9,3;9]"..
mcl_formspec.get_itemslot_bg(0,4.5,9,3)..
"list[current_player;main;0,7.74;9,1;]"..
mcl_formspec.get_itemslot_bg(0,7.74,9,1)..
"label[3,0;"..minetest.formspec_escape(minetest.colorize("#313131", S("Dropper"))).."]"..
"list[context;main;3,0.5;3,3;]"..
mcl_formspec.get_itemslot_bg(3,0.5,3,3)..
"listring[context;main]"..
"listring[current_player;main]"
local meta = minetest.get_meta(pos)
meta:set_string("formspec", form)
meta:set_string("formspec", dropper_formspec)
local inv = meta:get_inventory()
inv:set_size("main", 9)
end
@ -38,9 +51,9 @@ local function orientate_dropper(pos, placer)
local pitch = placer:get_look_vertical() * (180 / math.pi)
if pitch > 55 then
minetest.swap_node(pos, {name="mcl_droppers:dropper_up"})
minetest.swap_node(pos, { name = "mcl_droppers:dropper_up" })
elseif pitch < -55 then
minetest.swap_node(pos, {name="mcl_droppers:dropper_down"})
minetest.swap_node(pos, { name = "mcl_droppers:dropper_down" })
end
end
@ -58,10 +71,10 @@ local dropperdef = {
local meta2 = meta:to_table()
meta:from_table(oldmetadata)
local inv = meta:get_inventory()
for i=1, inv:get_size("main") do
for i = 1, inv:get_size("main") do
local stack = inv:get_stack("main", 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}
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
@ -96,7 +109,7 @@ local dropperdef = {
end,
_mcl_blast_resistance = 3.5,
_mcl_hardness = 3.5,
mesecons = {effector = {
mesecons = { effector = {
-- Drop random item when triggered
action_on = function(pos, node)
if not pos then return end
@ -106,9 +119,9 @@ local dropperdef = {
if node.name == "mcl_droppers:dropper" then
droppos = vector.subtract(pos, minetest.facedir_to_dir(node.param2))
elseif node.name == "mcl_droppers:dropper_up" then
droppos = {x=pos.x, y=pos.y+1, z=pos.z}
droppos = { x = pos.x, y = pos.y + 1, z = pos.z }
elseif node.name == "mcl_droppers:dropper_down" then
droppos = {x=pos.x, y=pos.y-1, z=pos.z}
droppos = { x = pos.x, y = pos.y - 1, z = pos.z }
end
local dropnode = minetest.get_node(droppos)
-- Do not drop into solid nodes, unless they are containers
@ -117,10 +130,10 @@ local dropperdef = {
return
end
local stacks = {}
for i=1,inv:get_size("main") do
for i = 1, inv:get_size("main") do
local stack = inv:get_stack("main", i)
if not stack:is_empty() then
table.insert(stacks, {stack = stack, stackpos = i})
table.insert(stacks, { stack = stack, stackpos = i })
end
end
if #stacks >= 1 then
@ -144,14 +157,14 @@ local dropperdef = {
local item_entity = minetest.add_item(droppos, dropitem)
local drop_vel = vector.subtract(droppos, pos)
local speed = 3
item_entity:set_velocity(vector.multiply(drop_vel,speed))
item_entity:set_velocity(vector.multiply(drop_vel, speed))
stack:take_item()
inv:set_stack("main", stack_id, stack)
end
end
end,
rules = mesecon.rules.alldirs,
}},
} },
on_rotate = on_rotate,
}
@ -159,20 +172,21 @@ local dropperdef = {
local horizontal_def = table.copy(dropperdef)
horizontal_def.description = S("Dropper")
horizontal_def._tt_help = S("9 inventory slots").."\n"..S("Drops item when powered by redstone power")
horizontal_def._tt_help = S("9 inventory slots") .. "\n" .. S("Drops item when powered by redstone power")
horizontal_def._doc_items_longdesc = S("A dropper is a redstone component and a container with 9 inventory slots which, when supplied with redstone power, drops an item or puts it into a container in front of it.")
horizontal_def._doc_items_usagehelp = S("Droppers can be placed in 6 possible directions, items will be dropped out of the hole. Use the dropper to access its inventory. Supply it with redstone energy once to make the dropper drop or transfer a random item.")
function horizontal_def.after_place_node(pos, placer, itemstack, pointed_thing)
setup_dropper(pos)
orientate_dropper(pos, placer)
end
horizontal_def.tiles = {
"default_furnace_top.png", "default_furnace_bottom.png",
"default_furnace_side.png", "default_furnace_side.png",
"default_furnace_side.png", "mcl_droppers_dropper_front_horizontal.png"
}
horizontal_def.paramtype2 = "facedir"
horizontal_def.groups = {pickaxey=1, container=2, material_stone=1}
horizontal_def.groups = { pickaxey = 1, container = 2, material_stone = 1 }
minetest.register_node("mcl_droppers:dropper", horizontal_def)
@ -185,7 +199,7 @@ down_def.tiles = {
"default_furnace_side.png", "default_furnace_side.png",
"default_furnace_side.png", "default_furnace_side.png"
}
down_def.groups = {pickaxey=1, container=2,not_in_creative_inventory=1, material_stone=1}
down_def.groups = { pickaxey = 1, container = 2, not_in_creative_inventory = 1, material_stone = 1 }
down_def._doc_items_create_entry = false
down_def.drop = "mcl_droppers:dropper"
minetest.register_node("mcl_droppers:dropper_down", down_def)
@ -207,9 +221,9 @@ minetest.register_node("mcl_droppers:dropper_up", up_def)
minetest.register_craft({
output = "mcl_droppers:dropper",
recipe = {
{"mcl_core:cobble", "mcl_core:cobble", "mcl_core:cobble",},
{"mcl_core:cobble", "", "mcl_core:cobble",},
{"mcl_core:cobble", "mesecons:redstone", "mcl_core:cobble",},
{ "mcl_core:cobble", "mcl_core:cobble", "mcl_core:cobble", },
{ "mcl_core:cobble", "", "mcl_core:cobble", },
{ "mcl_core:cobble", "mesecons:redstone", "mcl_core:cobble", },
}
})
@ -226,6 +240,6 @@ minetest.register_lbm({
nodenames = { "mcl_droppers:dropper", "mcl_droppers:dropper_down", "mcl_droppers:dropper_up" },
action = function(pos, node)
setup_dropper(pos)
minetest.log("action", "[mcl_droppers] Node formspec updated at "..minetest.pos_to_string(pos))
minetest.log("action", "[mcl_droppers] Node formspec updated at " .. minetest.pos_to_string(pos))
end,
})