Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to row - up/down/sideways #17

Open
CasimirKaPazi opened this issue Apr 21, 2017 · 1 comment
Open

Add to row - up/down/sideways #17

CasimirKaPazi opened this issue Apr 21, 2017 · 1 comment

Comments

@CasimirKaPazi
Copy link
Owner

A function that would be useful for many mods. Inverse to dig_up, build_row would add nodes of the same kind in a specified direction. Can be used for stacking, chains, etc. Might be easy to abuse on servers (maybe add an optional limit).

Rudimentary code that works as far as tested (up, down):

-- Add to row
function default.place_row(pos, node, user, itemstack, dir)
	if user == nil then return end
	if not dir then dir = {x = 0, y = 1, z = 0} end
	local nn = minetest.get_node(pos)
	if nn.name ~= itemstack:get_name() then return end
	local above = {x = pos.x + dir.x, y = pos.y + dir.y, z = pos.z + dir.z}
	local n_above = minetest.get_node(above)
	n_above_def = minetest.registered_nodes[n_above.name]
	if n_above.name == node.name then
		n_above_def.on_rightclick(above, n_above, user, itemstack)
	elseif n_above_def.buildable_to == true then
		minetest.set_node(above, node, user)
		minetest.sound_play("default_place_node", {pos = above, gain = 0.5})
		if minetest.setting_getbool("creative_mode") then return end
		itemstack:take_item()
		return itemstack
	end
end

In node definition:

on_rightclick = function(pos, node, user, itemstack)
	local dir = {x = 0, y = 1, z = 0}
	default.place_row(pos, node, user, itemstack, dir)
end,
@CasimirKaPazi
Copy link
Owner Author

Using on_place instead.

-- Add to row
function default.place_row(pointed_thing, node, user, itemstack, dir)
	if not user then return end
	if not pointed_thing.under then return end
	local pos = pointed_thing.under
	if not dir then dir = {x = 0, y = 1, z = 0} end

	local previous_node = minetest.get_node(pos)
	if previous_node.name ~= itemstack:get_name() then
		itemstack = minetest.item_place_node(itemstack, user, pointed_thing)
		return itemstack
	end
	local next_pos = {x = pos.x + dir.x, y = pos.y + dir.y, z = pos.z + dir.z}
	pointed_thing.under = next_pos
	local n_next_node = minetest.get_node(next_pos)
	local n_next_node_def = minetest.registered_nodes[n_next_node.name]

	if n_next_node.name == node.name then
		-- There is already a row, so we call the next node in line.
		n_next_node_def.on_place(itemstack, user, pointed_thing)
		return itemstack
	elseif n_next_node_def.buildable_to == true then
		-- Free space. We can add a node.
		itemstack = minetest.item_place_node(itemstack, user, pointed_thing)
		return itemstack
	end
end

Node def:

on_place = function(itemstack, user, pointed_thing)
	local dir = {x = 0, y = 1, z = 0}
	itemstack = default.place_row(pointed_thing, {name="default:papyrus"}, user, itemstack, dir)
	return itemstack
end,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant