Fix torches unplacable on disabled hoppers
[MineClone/MineClone2.git] / mods / ITEMS / mcl_torches / init.lua
blob7a0be478d4c1a8fd3355ca821f5c56dc93289361
2 --
3 -- 3d torch part
4 --
6 mcl_torches = {}
8 mcl_torches.register_torch = function(substring, description, doc_items_longdesc, doc_items_usagehelp, icon, mesh_floor, mesh_wall, tiles, light, groups, sounds, moredef)
9 local itemstring = minetest.get_current_modname()..":"..substring
10 local itemstring_wall = minetest.get_current_modname()..":"..substring.."_wall"
12 if light == nil then light = 14 end
13 if mesh_floor == nil then mesh_floor = "mcl_torches_torch_floor.obj" end
14 if mesh_wall == nil then mesh_wall = "mcl_torches_torch_wall.obj" end
15 if groups == nil then groups = {} end
17 groups.attached_node = 1
18 groups.torch = 1
19 groups.dig_by_water = 1
20 groups.destroy_by_lava_flow = 1
21 groups.dig_by_piston = 1
23 local floordef = {
24 description = description,
25 _doc_items_longdesc = doc_items_longdesc,
26 _doc_items_usagehelp = doc_items_usagehelp,
27 drawtype = "mesh",
28 mesh = mesh_floor,
29 inventory_image = icon,
30 wield_image = icon,
31 tiles = tiles,
32 paramtype = "light",
33 paramtype2 = "wallmounted",
34 sunlight_propagates = true,
35 is_ground_content = false,
36 walkable = false,
37 liquids_pointable = false,
38 light_source = light,
39 groups = groups,
40 drop = itemstring,
41 selection_box = {
42 type = "wallmounted",
43 wall_top = {-1/16, -1/16, -1/16, 1/16, 0.5, 1/16},
44 wall_bottom = {-1/16, -0.5, -1/16, 1/16, 1/16, 1/16},
46 sounds = sounds,
47 node_placement_prediction = "",
48 on_place = function(itemstack, placer, pointed_thing)
49 if pointed_thing.type ~= "node" then
50 -- no interaction possible with entities, for now.
51 return itemstack
52 end
54 local under = pointed_thing.under
55 local node = minetest.get_node(under)
56 local def = minetest.registered_nodes[node.name]
57 if not def then return itemstack end
59 -- Call on_rightclick if the pointed node defines it
60 if placer and not placer:get_player_control().sneak then
61 if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
62 return minetest.registered_nodes[node.name].on_rightclick(under, node, placer, itemstack) or itemstack
63 end
64 end
66 local above = pointed_thing.above
67 local wdir = minetest.dir_to_wallmounted({x = under.x - above.x, y = under.y - above.y, z = under.z - above.z})
69 -- Torch placement rules: Disallow placement on some nodes. General rule: Solid, opaque, full cube collision box nodes are allowed.
70 -- Special allowed nodes:
71 -- * soul sand
72 -- * end portal frame (TODO)
73 -- * mob spawner
74 -- * Fence, wall, glass, hopper: Only on top
75 -- * Slab: Only on top if upside down
76 -- * Stairs: Only on top if upside down
78 -- Special forbidden nodes:
79 -- * Piston
80 -- * Sticky piston
81 if not def.buildable_to then
82 if node.name ~= "mcl_nether:soul_sand" and node.name ~= "mcl_mobspawners:spawner" and
83 ((not def.groups.solid) or (not def.groups.opaque)) then
84 -- Only allow top placement on these nodes
85 if def.groups.glass or node.name == "mcl_hoppers:hopper" or node.name == "mcl_hoppers:hopper_side" or node.name == "mcl_hoppers:hopper_disabled" or node.name == "mcl_hoppers:hopper_side_disabled" or def.groups.fence == 1 or def.groups.wall or def.groups.slab_top == 1 or (def.groups.stair == 1 and minetest.facedir_to_dir(node.param2).y ~= 0) then
86 if wdir ~= 1 then
87 return itemstack
88 end
89 else
90 return itemstack
91 end
92 elseif node.name == "mesecons_pistons:piston_up_normal_off" or node.name == "mesecons_pistons:piston_up_sticky_off" or
93 node.name == "mesecons_pistons:piston_normal_off" or node.name == "mesecons_pistons:piston_sticky_off" or
94 node.name == "mesecons_pistons:piston_down_normal_off" or node.name == "mesecons_pistons:piston_down_sticky_off" then
95 return itemstack
96 end
97 end
99 local itemstring = itemstack:get_name()
100 local fakestack = ItemStack(itemstack)
101 local idef = fakestack:get_definition()
102 local retval
104 if wdir == 0 then
105 -- Prevent placement of ceiling torches
106 return itemstack
107 elseif wdir == 1 then
108 retval = fakestack:set_name(itemstring)
109 else
110 retval = fakestack:set_name(itemstring_wall)
112 if not retval then
113 return itemstack
116 local success
117 itemstack, success = minetest.item_place(fakestack, placer, pointed_thing, wdir)
118 itemstack:set_name(itemstring)
120 if success and idef.sounds and idef.sounds.place then
121 minetest.sound_play(idef.sounds.place, {pos=under, gain=1})
123 return itemstack
126 if moredef ~= nil then
127 for k,v in pairs(moredef) do
128 floordef[k] = v
131 minetest.register_node(itemstring, floordef)
133 local groups_wall = table.copy(groups)
134 groups_wall.torch = 2
136 local walldef = {
137 drawtype = "mesh",
138 mesh = mesh_wall,
139 tiles = tiles,
140 paramtype = "light",
141 paramtype2 = "wallmounted",
142 sunlight_propagates = true,
143 is_ground_content = false,
144 walkable = false,
145 light_source = light,
146 groups = groups_wall,
147 drop = itemstring,
148 selection_box = {
149 type = "wallmounted",
150 wall_top = {-0.1, -0.1, -0.1, 0.1, 0.5, 0.1},
151 wall_bottom = {-0.1, -0.5, -0.1, 0.1, 0.1, 0.1},
152 wall_side = {-0.5, -0.5, -0.1, -0.2, 0.1, 0.1},
154 sounds = sounds,
156 if moredef ~= nil then
157 for k,v in pairs(moredef) do
158 walldef[k] = v
161 minetest.register_node(itemstring_wall, walldef)
164 -- Add entry alias for the Help
165 if minetest.get_modpath("doc") then
166 doc.add_entry_alias("nodes", itemstring, "nodes", itemstring_wall)
171 mcl_torches.register_torch("torch",
172 "Torch",
173 "Torches are light sources which can be placed at the side or on the top of most blocks.",
174 [[Torches can generally be placed on full solid opaque blocks. The following exceptions apply:
175 • Glass, fence, wall, hopper: Can only be placed on top
176 • Upside-down slab/stair: Can only be placed on top
177 • Soul sand, mob spawner: Placement possible
178 • Glowstone and pistons: No placement possible]],
179 "default_torch_on_floor.png",
180 "mcl_torches_torch_floor.obj", "mcl_torches_torch_wall.obj",
182 name = "default_torch_on_floor_animated.png",
183 animation = {type = "vertical_frames", aspect_w = 16, aspect_h = 16, length = 3.3}
186 {dig_immediate=3, torch=1, deco_block=1},
187 mcl_sounds.node_sound_wood_defaults(),
188 {_doc_items_hidden = false})
191 minetest.register_craft({
192 output = "mcl_torches:torch 4",
193 recipe = {
194 { "group:coal" },
195 { "mcl_core:stick" },