Add support for place_failed sound more nodes

This commit is contained in:
Wuzzy 2023-03-11 18:22:38 +01:00
parent 22842aadfe
commit fda4ae4d12
13 changed files with 90 additions and 17 deletions

View File

@ -75,7 +75,7 @@ Pixture was inspired by [Kenney](http://kenney.nl).
### Sounds
* Various authors
* There is a large number of authors
* See the individual README files in the mods for details
* Note: All sounds are compatible with CC BY-SA 4.0

View File

@ -482,6 +482,7 @@ minetest.register_node(
fixed = {-0.5, -0.5, -0.5, 0.5, 2/16, 1.5}
},
node_placement_prediction = "",
on_place = function(itemstack, placer, pointed_thing)
local under = pointed_thing.under
@ -509,6 +510,7 @@ minetest.register_node(
local node_def = minetest.registered_nodes[minetest.get_node(pos).name]
if not node_def or not node_def.buildable_to then
rp_sounds.play_place_failed_sound(placer)
return itemstack
end
@ -525,11 +527,15 @@ minetest.register_node(
local botdef = minetest.registered_nodes[bot.name]
-- Check if the 2nd node for the bed is free or already a bed head.
if not (bot.name == "rp_bed:bed_head" and bot.param2 == dir) and (not botdef or not botdef.buildable_to) then
rp_sounds.play_place_failed_sound(placer)
return itemstack
end
minetest.set_node(pos, {name = "rp_bed:bed_foot", param2 = dir})
minetest.set_node(botpos, {name = "rp_bed:bed_head", param2 = dir})
local footnode = {name = "rp_bed:bed_foot", param2 = dir}
local headnode = {name = "rp_bed:bed_head", param2 = dir}
minetest.set_node(pos, footnode)
minetest.set_node(botpos, headnode)
rp_sounds.play_node_sound(pos, footnode, "place")
if not minetest.is_creative_enabled(placer:get_player_name()) then
itemstack:take_item()

View File

@ -545,6 +545,7 @@ local register_boat = function(name, def)
if def.check_boat_space then
local res = def.check_boat_space(place_pos, on_liquid) -- returns true if enough space, false otherwise
if not res then
rp_sounds.play_place_failed_sound(placer)
return itemstack
end
end

View File

@ -89,6 +89,7 @@ function default.place_sapling(itemstack, placer, pointed_thing)
-- Find position to place sapling at
local place_in, place_floor = util.pointed_thing_to_place_pos(pointed_thing)
if place_in == nil then
rp_sounds.play_place_failed_sound(placer)
return itemstack
end
local floornode = minetest.get_node(place_floor)
@ -102,11 +103,14 @@ function default.place_sapling(itemstack, placer, pointed_thing)
-- Floor must be soil
if minetest.get_item_group(floornode.name, "soil") == 0 then
rp_sounds.play_place_failed_sound(placer)
return itemstack
end
-- Place sapling
minetest.set_node(place_in, {name = itemstack:get_name()})
local newnode = {name = itemstack:get_name()}
minetest.set_node(place_in, newnode)
rp_sounds.play_node_sound(place_in, newnode, "place")
-- Reduce item count
if not minetest.is_creative_enabled(placer:get_player_name()) then

View File

@ -157,6 +157,7 @@ minetest.register_node(
minetest.remove_node(pos)
util.dig_down(pos, oldnode)
end,
node_placement_prediction = "",
on_place = function(itemstack, placer, pointed_thing)
-- Boilerplate to handle pointed node handlers
local handled, handled_itemstack = util.on_place_pointed_node_handler(itemstack, placer, pointed_thing)
@ -167,12 +168,14 @@ minetest.register_node(
-- Find position to place vine at
local place_in, place_floor = util.pointed_thing_to_place_pos(pointed_thing, true)
if place_in == nil then
rp_sounds.play_place_failed_sound(placer)
return itemstack
end
local ceilingnode = minetest.get_node(place_floor)
-- Ceiling must be stone, dirt or another vine
if minetest.get_item_group(ceilingnode.name, "dirt") == 0 and ceilingnode.name ~= "rp_default:stone" and ceilingnode.name ~= "rp_default:vine" then
rp_sounds.play_place_failed_sound(placer)
return itemstack
end
@ -200,7 +203,9 @@ minetest.register_node(
end
-- Place vine
minetest.set_node(place_in, {name = itemstack:get_name(), param2 = age})
local newnode = {name = itemstack:get_name(), param2 = age}
minetest.set_node(place_in, newnode)
rp_sounds.play_node_sound(place_in, newnode, "place")
-- Reduce item count
if not minetest.is_creative_enabled(placer:get_player_name()) then

View File

@ -33,6 +33,7 @@ minetest.register_node(
default.begin_growing_sapling(pos)
end,
node_placement_prediction = "",
on_place = default.place_sapling,
})
@ -64,6 +65,7 @@ minetest.register_node(
default.begin_growing_sapling(pos)
end,
node_placement_prediction = "",
on_place = default.place_sapling,
})
@ -96,6 +98,7 @@ minetest.register_node(
default.begin_growing_sapling(pos)
end,
node_placement_prediction = "",
on_place = default.place_sapling,
})

View File

@ -82,10 +82,7 @@ return function(itemstack, placer, pointed_thing)
local is_fertilized = minetest.get_item_group(undernode.name, "plantable_fertilizer") == 1
meta:set_int("age", get_init_age(underdef._waterplant_max_height, is_fertilized))
end
local snd = underdef.sounds.place
if snd and top then
minetest.sound_play(snd, {pos = top}, true)
end
rp_sounds.play_node_sound(pointed_thing.under, undernode, "place")
if not minetest.is_creative_enabled(player_name) then
itemstack:take_item()
end
@ -96,6 +93,7 @@ return function(itemstack, placer, pointed_thing)
-- Find position to place plant at
local place_in, place_floor = util.pointed_thing_to_place_pos(pointed_thing)
if place_in == nil then
rp_sounds.play_place_failed_sound(placer)
return itemstack
end
local floornode = minetest.get_node(place_floor)
@ -111,7 +109,8 @@ return function(itemstack, placer, pointed_thing)
local def_floor = minetest.registered_nodes[node_floor.name]
if not util.is_water_source_or_waterfall(place_in) then
return itemstack
rp_sounds.play_place_failed_sound(placer)
return itemstack
end
if node_floor.name == "rp_default:dirt" then
@ -135,6 +134,7 @@ return function(itemstack, placer, pointed_thing)
elseif base == "airweed_inert" and node_floor.name == "rp_default:fertilized_dry_dirt" then
node_floor.name = "rp_default:"..base.."_on_fertilized_dry_dirt"
else
rp_sounds.play_place_failed_sound(placer)
return itemstack
end

View File

@ -89,8 +89,9 @@ local function register_torch(subname, description, tt_help, tiles, overlay_tile
itemstack, place_pos = minetest.item_place(fakestack, placer, pointed_thing, wdir)
end
if place_pos then
local sounds = minetest.registered_nodes["rp_default:"..subname].sounds
minetest.sound_play(sounds.place, {pos = place_pos}, true)
rp_sounds.play_node_sound(place_pos, {name="rp_default:"..subname}, "place")
else
rp_sounds.play_place_failed_sound(placer)
end
itemstack:set_name("rp_default:"..subname)

View File

@ -99,6 +99,7 @@ function door.register_door(name, def)
not placer or
not placer:is_player()
then
rp_sounds.play_place_failed_sound(placer)
return itemstack
end

View File

@ -191,6 +191,7 @@ function farming.place_plant(itemstack, placer, pointed_thing)
-- Find placement position
local place_in, place_on = util.pointed_thing_to_place_pos(pointed_thing)
if not place_in then
rp_sounds.play_place_failed_sound(placer)
return itemstack
end
@ -198,6 +199,7 @@ function farming.place_plant(itemstack, placer, pointed_thing)
local place_in_node = minetest.get_node(place_in)
local pidef = minetest.registered_nodes[place_in_node.name]
if pidef and pidef._rp_farming_plant_name == name then
rp_sounds.play_place_failed_sound(placer)
return itemstack
end
@ -213,10 +215,11 @@ function farming.place_plant(itemstack, placer, pointed_thing)
if idef and idef.sounds and idef.sounds.place then
minetest.sound_play(idef.sounds.place, {pos=place_on}, true)
end
break
return itemstack
end
end
rp_sounds.play_place_failed_sound(placer)
return itemstack
end

View File

@ -269,6 +269,7 @@ for c=0,7 do
local place_in, place_floor = util.pointed_thing_to_place_pos(pointed_thing)
if place_in == nil then
rp_sounds.play_place_failed_sound(placer)
return itemstack
end
@ -297,6 +298,7 @@ for c=0,7 do
local node_floor = minetest.get_node(place_floor)
local def_floor = minetest.registered_nodes[node_floor.name]
if (not def_floor) or (not def_floor.walkable) or minetest.get_item_group(node_floor.name, "attached_node") == 1 then
rp_sounds.play_place_failed_sound(placer)
return itemstack
end
-- Place node

View File

@ -3,9 +3,13 @@
This mod gives you the node sounds. You need these helper
functions to give nodes sounds.
### Basic function syntax
### Node sound functions
Every function returns a value that you can use to set the node's
These functions are functions for the node's `sounds` table.
#### Basic syntax
Every node sound function returns a value that you can use to set the node's
`sounds` argument with. You can choose between stone, dirt, sand,
and other sounds. There is an optional argument `table` in which
you can override the default sound table returned by this
@ -29,7 +33,7 @@ Returns a sound definition with dirt sounds, except the footstep
sound was changed to `rp_sounds_footstep_grass`.
### List of functions
#### List of node sound functions
These are the available functions:
@ -53,3 +57,27 @@ These are the available functions:
* `rp_sounds.node_sound_fuzzy_defaults(table)`: Fuzzy, soft surface (like wool, cotton, bedsheet)
* `rp_sounds.node_sound_water_defaults(table)`: Water
* `rp_sounds.node_sound_snow_defaults(table)`: Snow (incomplete, not recommended)
### Helper functions
#### `rp_sounds.play_place_failed_sound(player)`
Play the default `place_failed` sound (when placement of a node/item fails)
for `player`. If `player` is not a player, nothing is played.
This function is useful if you want to handle a node placement manually
and want to fail the placement of a node. The rule of thumb is to play this
when you manually make node placement fail, *except* when it was because
of a protection violation Then no sound should be played.
(We expect protection handing to be done by the protection mod.)
#### `rp_sounds.play_node_sound(pos, node, soundtype)`
Convenience function that plays a node sound of the node `node`
at `pos`, taken from the nodes `sounds` table.
* `pos`: Position to play sound at
* `node`: Node table of the node to take the sounds from
* `soundtype`: The type of sound (name from the node definitions `sounds` table, e.g. `"place"`, `"dig"`, etc.)
If the node or soundtype is unknown, no sound is played.

View File

@ -2,6 +2,8 @@
-- Sounds
--
local PLACE_FAILED_GAIN = 0.25
rp_sounds = {}
function rp_sounds.node_sound_defaults(table)
@ -15,7 +17,7 @@ function rp_sounds.node_sound_defaults(table)
table.place = table.place or
{name="default_place_node_hard", gain=0.8}
table.place_failed = table.place_failed or
{name="rp_sounds_place_failed", gain=0.12}
{name="rp_sounds_place_failed", gain=PLACE_FAILED_GAIN}
return table
end
@ -292,4 +294,21 @@ function rp_sounds.node_sound_snow_defaults(table)
return rp_sounds.node_sound_dirt_defaults(table)
end
function rp_sounds.play_place_failed_sound(player)
if not player or not player:is_player() then
return
end
minetest.sound_play({name="rp_sounds_place_failed", gain=PLACE_FAILED_GAIN}, {to_player=player:get_player_name()}, true)
end
function rp_sounds.play_node_sound(pos, node, soundtype)
local def = minetest.registered_nodes[node.name]
if not def then
return
end
local sounds = def.sounds
if not sounds or not sounds[soundtype] then
return
end
minetest.sound_play(sounds[soundtype], {pos=pos}, true)
end