2 -- A button that when pressed emits power for 1 second
3 -- and then turns off again
5 local button_get_output_rules
= mesecon
.rules
.wallmounted_get
9 wall_side
= { -8/16, -2/16, -4/16, -6/16, 2/16, 4/16 },
10 wall_bottom
= { -4/16, -8/16, -2/16, 4/16, -6/16, 2/16 },
11 wall_top
= { -4/16, 6/16, -2/16, 4/16, 8/16, 2/16 },
15 wall_side
= { -8/16, -2/16, -4/16, -7/16, 2/16, 4/16 },
16 wall_bottom
= { -4/16, -8/16, -2/16, 4/16, -7/16, 2/16 },
17 wall_top
= { -4/16, 7/16, -2/16, 4/16, 8/16, 2/16 },
21 mesecon
.push_button
= function(pos
, node
)
22 -- No-op if button is already pushed
23 if mesecon
.is_receptor_on(node
) then
26 local def
= minetest
.registered_nodes
[node
.name
]
27 minetest
.set_node(pos
, {name
="mesecons_button:button_"..def
._mcl_button_basename
.."_on", param2
=node
.param2
})
28 mesecon
.receptor_on(pos
, button_get_output_rules(node
))
29 minetest
.sound_play("mesecons_button_push", {pos
=pos
})
30 local timer
= minetest
.get_node_timer(pos
)
31 timer
:start(def
._mcl_button_timer
)
34 local on_button_place
= function(itemstack
, placer
, pointed_thing
)
35 if pointed_thing
.type ~= "node" then
36 -- no interaction possible with entities
40 local under
= pointed_thing
.under
41 local node
= minetest
.get_node(under
)
42 local def
= minetest
.registered_nodes
[node
.name
]
43 if not def
then return end
44 local groups
= def
.groups
46 -- Check special rightclick action of pointed node
47 if def
and def
.on_rightclick
then
48 if not placer
:get_player_control().sneak
then
49 return def
.on_rightclick(under
, node
, placer
, itemstack
,
50 pointed_thing
) or itemstack
, false
54 -- If the pointed node is buildable, let's look at the node *behind* that node
55 if def
.buildable_to
then
56 local dir
= vector
.subtract(pointed_thing
.above
, pointed_thing
.under
)
57 local actual
= vector
.subtract(under
, dir
)
58 local actualnode
= minetest
.get_node(actual
)
59 def
= minetest
.registered_nodes
[actualnode
.name
]
63 -- Only allow placement on full-cube solid opaque nodes
64 if (not groups
) or (not groups
.solid
) or (not groups
.opaque
) or (def
.node_box
and def
.node_box
.type ~= "regular") then
68 local above
= pointed_thing
.above
70 local idef
= itemstack
:get_definition()
71 local itemstack
, success
= minetest
.item_place_node(itemstack
, placer
, pointed_thing
)
74 if idef
.sounds
and idef
.sounds
.place
then
75 minetest
.sound_play(idef
.sounds
.place
, {pos
=above
, gain
=1})
81 local buttonuse
= "Rightclick the button to push it."
83 mesecon
.register_button
= function(basename
, description
, texture
, recipeitem
, sounds
, plusgroups
, button_timer
, push_by_arrow
, longdesc
)
84 local groups_off
= table.copy(plusgroups
)
85 groups_off
.attached_node
=1
86 groups_off
.dig_by_water
=1
87 groups_off
.destroy_by_lava_flow
=1
88 groups_off
.dig_by_piston
=1
89 groups_off
.button
=1 -- button (off)
91 local groups_on
= table.copy(groups_off
)
92 groups_on
.not_in_creative_inventory
=1
93 groups_on
.button
=2 -- button (on)
96 groups_off
.button_push_by_arrow
= 1
97 groups_on
.button_push_by_arrow
= 1
100 minetest
.register_node("mesecons_button:button_"..basename
.."_off", {
101 drawtype
= "nodebox",
103 wield_image
= "mesecons_button_wield_mask.png^"..texture
.."^mesecons_button_wield_mask.png^[makealpha:255,126,126",
104 -- FIXME: Use proper 3D inventory image
105 inventory_image
= "mesecons_button_wield_mask.png^"..texture
.."^mesecons_button_wield_mask.png^[makealpha:255,126,126",
106 wield_scale
= { x
=1, y
=1, z
=1},
108 paramtype2
= "wallmounted",
109 is_ground_content
= false,
111 sunlight_propagates
= true,
112 node_box
= boxes_off
,
114 description
= description
,
115 _doc_items_longdesc
= longdesc
,
116 _doc_items_usagehelp
= buttonuse
,
117 on_place
= on_button_place
,
118 node_placement_prediction
= "",
119 on_rightclick
= function (pos
, node
)
120 mesecon
.push_button(pos
, node
)
123 mesecons
= {receptor
= {
124 state
= mesecon
.state
.off
,
125 rules
= button_get_output_rules
,
127 _mcl_button_basename
= basename
,
128 _mcl_button_timer
= button_timer
,
130 _mcl_blast_resistance
= 2.5,
134 minetest
.register_node("mesecons_button:button_"..basename
.."_on", {
135 drawtype
= "nodebox",
137 wield_image
= "mesecons_button_wield_mask.png^"..texture
.."^mesecons_button_wield_mask.png^[makealpha:255,126,126",
138 inventory_image
= "mesecons_button_wield_mask.png^"..texture
.."^mesecons_button_wield_mask.png^[makealpha:255,126,126",
139 wield_scale
= { x
=1, y
=1, z
=0.5},
141 paramtype2
= "wallmounted",
142 is_ground_content
= false,
144 sunlight_propagates
= true,
147 drop
= 'mesecons_button:button_'..basename
..'_off',
148 _doc_items_create_entry
= false,
149 node_placement_prediction
= "",
151 mesecons
= {receptor
= {
152 state
= mesecon
.state
.on
,
153 rules
= button_get_output_rules
155 _mcl_button_basename
= basename
,
156 _mcl_button_timer
= button_timer
,
157 on_timer
= function(pos
, elapsed
)
158 local node
= minetest
.get_node(pos
)
159 if node
.name
=="mesecons_button:button_"..basename
.."_on" then --has not been dug
160 -- Is button pushable by arrow?
161 if push_by_arrow
then
162 -- If there's an arrow stuck in the button, keep it pressed and check
164 local objs
= minetest
.get_objects_inside_radius(pos
, 1)
166 local entity
= objs
[o
]:get_luaentity()
167 if entity
and entity
.name
== "mcl_bows:arrow_entity" then
168 local timer
= minetest
.get_node_timer(pos
)
169 timer
:start(button_timer
)
175 -- Normal operation: Un-press the button
176 minetest
.set_node(pos
, {name
="mesecons_button:button_"..basename
.."_off",param2
=node
.param2
})
177 minetest
.sound_play("mesecons_button_pop", {pos
=pos
})
178 mesecon
.receptor_off(pos
, button_get_output_rules(node
))
182 _mcl_blast_resistance
= 2.5,
186 minetest
.register_craft({
187 output
= "mesecons_button:button_"..basename
.."_off",
188 recipe
= {{ recipeitem
}},
192 mesecon
.register_button(
197 mcl_sounds
.node_sound_stone_defaults(),
198 {material_stone
=1,handy
=1,pickaxey
=1},
201 "A stone button is a redstone component made out of stone which can be pushed to provide redstone power. When pushed, it powers adjacent redstone components for 1 second. It can only be placed on solid opaque full cubes (like cobblestone).")
204 { "wood", "mcl_core:wood", "default_wood.png", "Oak Button" },
205 { "acaciawood", "mcl_core:acaciawood", "default_acacia_wood.png", "Acacia Button" },
206 { "birchwood", "mcl_core:birchwood", "mcl_core_planks_birch.png", "Birch Button" },
207 { "darkwood", "mcl_core:darkwood", "mcl_core_planks_big_oak.png", "Dark Oak Button" },
208 { "sprucewood", "mcl_core:sprucewood", "mcl_core_planks_spruce.png", "Spruce Button" },
209 { "junglewood", "mcl_core:junglewood", "default_junglewood.png", "Jungle Button" },
213 mesecon
.register_button(
218 mcl_sounds
.node_sound_wood_defaults(),
219 {material_wood
=1,handy
=1,axey
=1},
222 "A wooden button is a redstone component made out of wood which can be pushed to provide redstone power. When pushed, it powers adjacent redstone components for 1.5 seconds. It can only be placed on solid opaque full cubes (like cobblestone). Wooden buttons may also be pushed by arrows.")
224 minetest
.register_craft({
226 recipe
= "mesecons_button:button_"..woods
[w
][1].."_off",
231 -- Add entry aliases for the Help
232 if minetest
.get_modpath("doc") then
233 doc
.add_entry_alias("nodes", "mesecons_button:button_wood_off", "nodes", "mesecons_button:button_wood_on")
234 doc
.add_entry_alias("nodes", "mesecons_button:button_stone_off", "nodes", "mesecons_button:button_stone_on")