Compare commits

..

2 Commits

Author SHA1 Message Date
Henry Behrendt c2dbcedbd3 Merge branch 'master' of https://git.minetest.land/MineClone2/MineClone2 into item_frame 2021-09-20 21:16:49 +02:00
Henry Behrendt afe1206f3d Item Frame: rotate containing item
right-click rotate the item by 45 degrees (90 degrees for maps)
first left-click drops the containing item
second left-click destructs the item frame itself

in creative-mode first left-click destructs the item frame
2021-09-19 16:27:56 +02:00
1 changed files with 60 additions and 27 deletions

View File

@ -89,6 +89,26 @@ local remove_item_entity = function(pos, node)
end
end
local function setRotation(pos, node, meta, rotate)
if node.name == "mcl_itemframes:item_frame" then
for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.5)) do
local entity = obj:get_luaentity()
if entity and (entity.name == "mcl_itemframes:item" or entity.name == "mcl_itemframes:map") then
local rotation = meta:get_int("rotation") or 0
if rotate then
rotation = (rotation +1) % 8
meta:set_int("rotation", rotation)
end
local r = obj:get_rotation()
--* `get_rotation()`: returns the rotation, a vector (radians)
r.z = math.pi / ((entity.name == "mcl_itemframes:item") and 4 or 2) * rotation
obj:set_rotation(r)
break
end
end
end
end
local update_item_entity = function(pos, node, param2)
remove_item_entity(pos, node)
local meta = minetest.get_meta(pos)
@ -122,10 +142,12 @@ local update_item_entity = function(pos, node, param2)
lua:_update_texture()
if node.name == "mcl_itemframes:item_frame" then
e:set_yaw(yaw)
setRotation(pos, node, meta)
end
else
local e = minetest.add_entity(pos, "mcl_itemframes:map", map_id)
e:set_yaw(yaw)
setRotation(pos, node, meta)
end
end
end
@ -135,15 +157,18 @@ local drop_item = function(pos, node, meta, clicker)
if clicker and clicker:is_player() then
cname = clicker:get_player_name()
end
local res = false
if node.name == "mcl_itemframes:item_frame" and not minetest.is_creative_enabled(cname) then
local inv = meta:get_inventory()
local item = inv:get_stack("main", 1)
if not item:is_empty() then
minetest.add_item(pos, item)
res = true
end
end
meta:set_string("infotext", "")
remove_item_entity(pos, node)
return res
end
minetest.register_node("mcl_itemframes:item_frame",{
@ -210,36 +235,33 @@ minetest.register_node("mcl_itemframes:item_frame",{
return
end
local meta = minetest.get_meta(pos)
drop_item(pos, node, meta, clicker)
local inv = meta:get_inventory()
if itemstack:is_empty() then
remove_item_entity(pos, node)
meta:set_string("infotext", "")
inv:set_stack("main", 1, "")
if inv:is_empty("main") then
if itemstack:is_empty() then return itemstack end --nothing to do
local put_itemstack = ItemStack(itemstack)
put_itemstack:set_count(1)
local itemname = put_itemstack:get_name()
if minetest.get_item_group(itemname, "compass") > 0 then
put_itemstack:set_name("mcl_compass:" .. mcl_compass.get_compass_image(pos, minetest.dir_to_yaw(minetest.facedir_to_dir(node.param2))))
end
if minetest.get_item_group(itemname, "clock") > 0 then
minetest.get_node_timer(pos):start(1.0)
end
inv:set_stack("main", 1, put_itemstack)
update_item_entity(pos, node)
-- Add node infotext when item has been named
local imeta = itemstack:get_meta()
local iname = imeta:get_string("name")
if iname then
meta:set_string("infotext", iname)
end
if not minetest.is_creative_enabled(clicker:get_player_name()) then
itemstack:take_item()
end
return itemstack
else
setRotation(pos, node, meta, true)
end
local put_itemstack = ItemStack(itemstack)
put_itemstack:set_count(1)
local itemname = put_itemstack:get_name()
if minetest.get_item_group(itemname, "compass") > 0 then
put_itemstack:set_name("mcl_compass:" .. mcl_compass.get_compass_image(pos, minetest.dir_to_yaw(minetest.facedir_to_dir(node.param2))))
end
if minetest.get_item_group(itemname, "clock") > 0 then
minetest.get_node_timer(pos):start(1.0)
end
inv:set_stack("main", 1, put_itemstack)
update_item_entity(pos, node)
-- Add node infotext when item has been named
local imeta = itemstack:get_meta()
local iname = imeta:get_string("name")
if iname then
meta:set_string("infotext", iname)
end
if not minetest.is_creative_enabled(clicker:get_player_name()) then
itemstack:take_item()
end
return itemstack
end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
local name = player:get_player_name()
@ -268,6 +290,17 @@ minetest.register_node("mcl_itemframes:item_frame",{
return stack:get_count()
end
end,
can_dig = function(pos, clicker)
local meta = minetest.get_meta(pos)
local node = minetest.get_node(pos)
if drop_item(pos, node, meta, clicker) then
local inv = meta:get_inventory()
meta:set_string("infotext", "")
inv:set_stack("main", 1, "")
return false
end
return true
end,
on_destruct = function(pos)
local meta = minetest.get_meta(pos)
local node = minetest.get_node(pos)