Skip to content

Instantly share code, notes, and snippets.

@corarona
Last active January 14, 2024 04:09
Show Gist options
  • Save corarona/9b43a167b213faad1b9b18eef399e64a to your computer and use it in GitHub Desktop.
Save corarona/9b43a167b213faad1b9b18eef399e64a to your computer and use it in GitHub Desktop.
mcla_itemframes POC
mcla_itemframes = {}
local S = minetest.get_translator(minetest.get_current_modname())
local fbox = {type = "fixed", fixed = {-6/16, -1/2, -6/16, 7/16, -7/16, 6/16}}
local function find_entity(pos)
for _,o in pairs(minetest.get_objects_inside_radius(pos, 0.4)) do
local l = o:get_luaentity()
if l and l.name == "mcla_itemframes:item" then
return l
end
end
end
local function remove_entity(pos)
local l = find_entity(pos)
if l then
l.object:remove()
end
end
local function update_entity(pos, itemstring)
local n = minetest.get_node(pos)
local dir = minetest.wallmounted_to_dir(n.param2)
local l = find_entity(pos)
if not itemstring then
local m = minetest.get_meta(pos)
itemstring = ItemStack(m:get_string("item")):get_name()
end
if not l then
local o = minetest.add_entity(pos, "mcla_itemframes:item")
l = o:get_luaentity()
end
if itemstring then
l:set_item(itemstring, dir)
elseif l then
l.object:remove()
end
end
minetest.register_node("mcla_itemframes:frame", {
description = S("Item Frame"),
name = "mcl_itemframes:item_frame",
_tt_help = S("Can hold an item"),
_doc_items_longdesc = S("Item frames are decorative blocks in which items can be placed."),
_doc_items_usagehelp = S("Just place any item on the item frame. Use the item frame again to retrieve the item."),
drawtype = "nodebox",
is_ground_content = false,
--mesh = "mcl_itemframes_itemframe1facedir.obj",
node_box = fbox,
selection_box = fbox,
collision_box = fbox,
--tiles = { "mcl_itemframes_item_frame_back.png", "mcl_itemframes_item_frame_back.png", "mcl_itemframes_item_frame_back.png", "mcl_itemframes_item_frame_back.png", "default_wood.png", "mcl_itemframes_item_frame_back.png" },
tiles = { "mcl_itemframes_item_frame.png" },
inventory_image = "mcl_itemframes_item_frame.png",
wield_image = "mcl_itemframes_item_frame.png",
use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "opaque" or false,
paramtype = "light",
paramtype2 = "wallmounted",
sunlight_propagates = true,
groups = { dig_immediate = 3, deco_block = 1, dig_by_piston = 1, handy = 1, axey = 1, }, -- attached_node_facedir = 1 }, -- allows for more placement options.
sounds = mcl_sounds.node_sound_defaults(),
node_placement_prediction = "",
_mcl_hardness = 0.5,
_mcl_blast_resistance = 0.5,
on_rightclick = function(pos, node, clicker, pstack, pointed_thing)
local itemstack = pstack:take_item()
local m = minetest.get_meta(pos)
m:set_string("item", itemstack:to_string())
update_entity(pos, itemstack:get_name(), minetest.wallmounted_to_dir(node.param2))
if not minetest.is_creative_enabled(clicker:get_player_name()) then
return pstack
end
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
if oldmetadata and oldmetadata.fields and oldmetadata.fields.item then
minetest.add_item(pos, ItemStack(oldmetadata.fields.item))
end
remove_entity(pos)
end
})
minetest.register_entity("mcla_itemframes:item", {
initial_properties = {
physical = false,
pointable = false,
visual = "wielditem",
visual_size = {x=0.3, y=0.3},
collisionbox = {0,0,0,0,0,0},
},
set_item = function(self, itemstring, dir)
self._item = itemstring
self.object:set_properties({ wield_item = itemstring })
if dir then
self.object:set_pos(vector.add(self.object:get_pos(), dir * 0.4))
self.object:set_rotation(vector.dir_to_rotation(dir))
end
end,
get_staticdata = function(self)
return minetest.serialize({ _item = self._item })
end,
on_activate = function(self, staticdata)
if type(staticdata) == "userdata" then return end
local s = minetest.deserialize(staticdata)
if s and s._item then
self:set_item(s._item)
elseif s and not s._item then
self.object:remove()
return
end
self.object:set_armor_groups({ immortal = 1 })
end,
})
minetest.register_lbm({
label = "Respawn item frame item entities",
name = "mcla_itemframes:respawn_entities",
nodenames = { "mcla_itemframes:frame" },
run_at_every_load = true,
action = function(pos,_)
update_entity(pos)
end
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment