Try calling on_place with fill block

This commit is contained in:
Johannes Fritz 2022-04-27 07:40:29 -05:00
parent 324c1d2481
commit 13c7aba071
2 changed files with 146 additions and 136 deletions

View File

@ -10,6 +10,8 @@ This mod is named `edit`, permits to a player, manage regions by the usage
of blocks to mark ones. It provides functionalities of `cut`, `copy`, `paste` and `delete`, of blocks to mark ones. It provides functionalities of `cut`, `copy`, `paste` and `delete`,
but using always special blocks to mark the dessired area. but using always special blocks to mark the dessired area.
This mod was inspired by the Fill Start and Fill End blocks in Manic Digger.
![screenshot](screenshot.png) ![screenshot](screenshot.png)
Technical information Technical information
@ -73,5 +75,5 @@ LICENCE
------- -------
CC0 by MrRar check [LICENSE](LICENSE) file, this mod was started by MrRar, CC0 by MrRar check [LICENSE](LICENSE) file, this mod was started by MrRar,
minetest-mods community later absorved but abandoned. minetest-mods community later absorved.

278
init.lua
View File

@ -1,15 +1,28 @@
--[[ --[[
Edit Mod v0.2 Edit Mod v0.3
]] ]]
--Add priv --Add priv
minetest.register_privilege("edit", { minetest.register_privilege("edit", {
description = "Let you use edit, copy, paste, delete blocks", description = "Lets you use edit, copy, paste, delete blocks",
give_to_singleplayer= true, give_to_singleplayer= true,
give_to_admin = true, give_to_admin = true,
}) })
--end add priv --end add priv
-- add in priv check
local function check_privilege(player)
local name = player:get_player_name()
if minetest.check_player_privs(name, {edit = true}) then
return true
else
minetest.chat_send_player(name, "You can't use an edit block without the edit privilege.")
return false
end
end
--end priv check
local function sign(x) -- different from math.sign never returns 0. local function sign(x) -- different from math.sign never returns 0.
if x > 0 then if x > 0 then
return 1 return 1
@ -20,25 +33,21 @@ local function sign(x) -- different from math.sign never returns 0.
end end
-- Delete Block -- Delete Block
minetest.register_node("edit:delete",{ minetest.register_node("edit:delete", {
description = "Delete", description = "Delete",
inventory_image = "edit_delete.png", inventory_image = "edit_delete.png",
groups = {snappy = 2, oddly_breakable_by_hand = 3}, groups = {snappy = 2, oddly_breakable_by_hand = 3},
tiles = {"edit_delete.png"}, tiles = {"edit_delete.png"},
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
if not placer then return itemstack end if not placer then return itemstack end
-- add in priv check
local name = placer:get_player_name() if not check_privilege(placer) then return itemstack end
if not minetest.check_player_privs(name, {edit = true}) then
minetest.chat_send_player(name, "You can't use an edit block without the edit privilege.") if clipboard[placer:get_player_name()].delete_block1_pos then
return itemstack local p1 = clipboard[placer:get_player_name()].delete_block1_pos
end
--end priv check
if clipboard[placer:get_player_name()].deleteBlock1Pos then
local p1 = clipboard[placer:get_player_name()].deleteBlock1Pos
local p2 = pointed_thing.above local p2 = pointed_thing.above
minetest.remove_node(p1); minetest.remove_node(p1)
p1.x = p1.x + math.sign(p2.x - p1.x) p1.x = p1.x + math.sign(p2.x - p1.x)
p1.y = p1.y + math.sign(p2.y - p1.y) p1.y = p1.y + math.sign(p2.y - p1.y)
@ -50,26 +59,26 @@ minetest.register_node("edit:delete",{
for x = p1.x, p2.x, sign(p2.x - p1.x) do for x = p1.x, p2.x, sign(p2.x - p1.x) do
for y = p1.y, p2.y, sign(p2.y - p1.y) do for y = p1.y, p2.y, sign(p2.y - p1.y) do
for z = p1.z, p2.z, sign(p2.z - p1.z) do for z = p1.z, p2.z, sign(p2.z - p1.z) do
minetest.remove_node({x=x, y=y, z=z}); minetest.remove_node(vector.new(x, y, z));
end end
end end
end end
clipboard[placer:get_player_name()].deleteBlock1Pos = nil clipboard[placer:get_player_name()].delete_block1_pos = nil
else else
minetest.set_node(pointed_thing.above, {name = "edit:delete"}) minetest.set_node(pointed_thing.above, {name = "edit:delete"})
clipboard[placer:get_player_name()].deleteBlock1Pos = pointed_thing.above clipboard[placer:get_player_name()].delete_block1_pos = pointed_thing.above
end end
end, end,
on_dig = function(pos, node, digger) on_dig = function(pos, node, digger)
for name, value in pairs(clipboard) do for name, value in pairs(clipboard) do
minetest.remove_node(pos); minetest.remove_node(pos);
if if
clipboard[name].deleteBlock1Pos clipboard[name].delete_block1_pos
and clipboard[name].deleteBlock1Pos.x == pos.x and clipboard[name].delete_block1_pos.x == pos.x
and clipboard[name].deleteBlock1Pos.y == pos.y and clipboard[name].delete_block1_pos.y == pos.y
and clipboard[name].deleteBlock1Pos.z == pos.z and clipboard[name].delete_block1_pos.z == pos.z
then then
clipboard[name].deleteBlock1Pos = nil clipboard[name].delete_block1_pos = nil
break break
end end
end end
@ -84,17 +93,14 @@ minetest.register_node("edit:copy",{
groups = {snappy = 2, oddly_breakable_by_hand = 3}, groups = {snappy = 2, oddly_breakable_by_hand = 3},
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
if not placer then return itemstack end if not placer then return itemstack end
-- add in priv check
if not check_privilege(placer) then return itemstack end
local name = placer:get_player_name() local name = placer:get_player_name()
if not minetest.check_player_privs(name, {edit = true}) then if clipboard[name].copy_block1_pos then
minetest.chat_send_player(name, "You can't use an edit block without the edit privilege.") clipboard[name].copy_data = {} -- clear out old copy data
return itemstack local copy_data = clipboard[name].copy_data
end local p1 = clipboard[name].copy_block1_pos
--end priv check
if clipboard[placer:get_player_name()].copyBlock1Pos then
clipboard[placer:get_player_name()].copyData = {} -- clear out old copy data
local copyData = clipboard[placer:get_player_name()].copyData
local p1 = clipboard[placer:get_player_name()].copyBlock1Pos
local p2 = pointed_thing.above local p2 = pointed_thing.above
minetest.remove_node(p1); -- remove copy block 1. minetest.remove_node(p1); -- remove copy block 1.
@ -114,34 +120,34 @@ minetest.register_node("edit:copy",{
for x = p1.x, p2.x, sign(p2.x - p1.x) do for x = p1.x, p2.x, sign(p2.x - p1.x) do
local xDif = offsetX + x - p1.x local xDif = offsetX + x - p1.x
copyData[xDif] = {} copy_data[xDif] = {}
for y = p1.y, p2.y, sign(p2.y - p1.y) do for y = p1.y, p2.y, sign(p2.y - p1.y) do
local yDif = offsetY + y - p1.y local yDif = offsetY + y - p1.y
copyData[xDif][yDif] = {} copy_data[xDif][yDif] = {}
for z = p1.z, p2.z, sign(p2.z - p1.z) do for z = p1.z, p2.z, sign(p2.z - p1.z) do
local zDif = offsetZ + z - p1.z local zDif = offsetZ + z - p1.z
copyData[xDif][yDif][zDif] = minetest.get_node( copy_data[xDif][yDif][zDif] = minetest.get_node(
{x = x, y = y, z = z} vector.new(x, y, z)
) )
end end
end end
end end
clipboard[placer:get_player_name()].copyBlock1Pos = nil clipboard[placer:get_player_name()].copy_block1_pos = nil
else else
minetest.set_node(pointed_thing.above, {name = "edit:copy"}) minetest.set_node(pointed_thing.above, {name = "edit:copy"})
clipboard[placer:get_player_name()].copyBlock1Pos = pointed_thing.above clipboard[placer:get_player_name()].copy_block1_pos = pointed_thing.above
end end
end, end,
on_dig = function(pos, node, digger) on_dig = function(pos, node, digger)
for name, value in pairs(clipboard) do for name, value in pairs(clipboard) do
minetest.remove_node(pos); minetest.remove_node(pos);
if if
clipboard[name].copyBlock1Pos clipboard[name].copy_block1_pos
and clipboard[name].copyBlock1Pos.x == pos.x and clipboard[name].copy_block1_pos.x == pos.x
and clipboard[name].copyBlock1Pos.y == pos.y and clipboard[name].copy_block1_pos.y == pos.y
and clipboard[name].copyBlock1Pos.z == pos.z and clipboard[name].copy_block1_pos.z == pos.z
then then
clipboard[name].copyBlock1Pos = nil clipboard[name].copy_block1_pos = nil
break break
end end
end end
@ -156,26 +162,19 @@ minetest.register_node("edit:paste", {
groups = {snappy = 2, oddly_breakable_by_hand = 3}, groups = {snappy = 2, oddly_breakable_by_hand = 3},
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
if not placer then return itemstack end if not placer then return itemstack end
-- add in priv check
local name = placer:get_player_name() if not check_privilege(placer) then return itemstack end
if not minetest.check_player_privs(name, {edit = true}) then
minetest.chat_send_player(name, "You can't use an edit block without the edit privilege.") local copy_data = clipboard[placer:get_player_name()].copy_data
return itemstack
end
--end priv check
local copyData = clipboard[placer:get_player_name()].copyData
local pos = pointed_thing.above local pos = pointed_thing.above
for x, yTable in pairs(copyData) do for x, yTable in pairs(copy_data) do
for y, zTable in pairs(yTable) do for y, zTable in pairs(yTable) do
for z, node in pairs(zTable) do for z, node in pairs(zTable) do
minetest.set_node( minetest.set_node(vector.new(
{ pos.x + x,
x = pos.x + x, pos.y + y,
y = pos.y + y, pos.z + z
z = pos.z + z ), node)
},
node
)
end end
end end
end end
@ -190,114 +189,123 @@ minetest.register_node("edit:fill",{
groups = {snappy = 2, oddly_breakable_by_hand = 3}, groups = {snappy = 2, oddly_breakable_by_hand = 3},
on_place = function(itemstack, placer, pointed_thing) on_place = function(itemstack, placer, pointed_thing)
if not placer then return itemstack end if not placer then return itemstack end
-- add in priv check
local name = placer:get_player_name() if not check_privilege(placer) then return itemstack end
if not minetest.check_player_privs(name, {edit = true}) then
minetest.chat_send_player(name, "You can't use an edit block without the edit privilege.") if clipboard[placer:get_player_name()].fill_block1 then
return itemstack
end
--end priv check
if clipboard[placer:get_player_name()].fillBlock1Pos then
minetest.set_node(pointed_thing.above, {name = "edit:fill"}) minetest.set_node(pointed_thing.above, {name = "edit:fill"})
clipboard[placer:get_player_name()].fillBlock2Pos = pointed_thing.above clipboard[placer:get_player_name()].fill_block2 = pointed_thing
local inv = minetest.get_inventory({type = "player", name = placer:get_player_name()}) local inv = minetest.get_inventory({type = "player", name = placer:get_player_name()})
local formSpec = "size[8,6]label[0.5,0.5;Select the material you would like to use]button_exit[7,0;1,1;quit;X]" local formSpec = "size[8,6]label[0.5,0.5;Select the material you would like to use]button_exit[7,0;1,1;quit;X]"
for y = 1, 4 do for y = 1, 4 do
for x = 1, 8 do for x = 1, 8 do
local name = inv:get_stack("main", ((y - 1) * 8) + x):get_name() local name = inv:get_stack("main", ((y - 1) * 8) + x):get_name()
formSpec formSpec =
= formSpec
formSpec .. "item_image_button["
.. "item_image_button[" .. (x - 1) .. ","
.. (x - 1) .. "," .. (y + 1) .. ";1,1;"
.. (y + 1) .. ";1,1;" .. name .. ";"
.. name .. ";" .. name .. ";]"
.. name .. ";]"
end end
end end
minetest.show_formspec(placer:get_player_name(), "edit:pasteType", formSpec) minetest.show_formspec(placer:get_player_name(), "edit:select_fill_type", formSpec)
else else
minetest.set_node(pointed_thing.above, {name = "edit:fill"}) minetest.set_node(pointed_thing.above, {name = "edit:fill"})
clipboard[placer:get_player_name()].fillBlock1Pos = pointed_thing.above clipboard[placer:get_player_name()].fill_block1 = pointed_thing
end end
end, end,
on_dig = function(pos, node, digger) on_dig = function(pos, node, digger)
minetest.remove_node(pos); minetest.remove_node(pos);
for name, value in pairs(clipboard) do for name, fills in pairs(clipboard) do
if local block1 = fills.fill_block1
clipboard[name].fillBlock1Pos local block2 = fills.fill_block2
and clipboard[name].fillBlock1Pos.x == pos.x if block1 and vector.equals(block1.above, pos) then
and clipboard[name].fillBlock1Pos.y == pos.y fills.fill_block1 = nil
and clipboard[name].fillBlock1Pos.z == pos.z fills.fill_block2 = nil
then minetest.remove_node(block1.above)
clipboard[name].fillBlock1Pos = nil return
break end
elseif if block2 and vector.equals(block2.above, pos) then
clipboard[name].fillBlock2Pos fills.fill_block1 = nil
and clipboard[name].fillBlock2Pos.x == pos.x fills.fill_block2 = nil
and clipboard[name].fillBlock2Pos.y == pos.y minetest.remove_node(block2.above)
and clipboard[name].fillBlock2Pos.z == pos.z return
then
clipboard[digger:get_player_name()].fillBlock2Pos = nil
break
end end
end end
end end
}) })
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
if not player then return false end if not player then return false end
if formname == "edit:pasteType" then if formname ~= "edit:select_fill_type" then return false end
for key, value in pairs(fields) do
if minetest.close_formspec(player:get_player_name(), "edit:select_fill_type")
clipboard[player:get_player_name()].fillBlock1Pos
and clipboard[player:get_player_name()].fillBlock2Pos if
then not clipboard[player:get_player_name()].fill_block1
local p1 = clipboard[player:get_player_name()].fillBlock1Pos or not clipboard[player:get_player_name()].fill_block2
local p2 = clipboard[player:get_player_name()].fillBlock2Pos then return true end
if key == "quit" then
minetest.remove_node(p1) local p1 = clipboard[player:get_player_name()].fill_block1.above
minetest.remove_node(p2) local p2 = clipboard[player:get_player_name()].fill_block2.above
clipboard[player:get_player_name()].fillBlock1Pos = nil local pointed_thing = clipboard[player:get_player_name()].fill_block1
clipboard[player:get_player_name()].fillBlock2Pos = nil clipboard[player:get_player_name()].fill_block1 = nil
clipboard[player:get_player_name()].fill_block2 = nil
minetest.remove_node(p1)
minetest.remove_node(p2)
local item = next(fields)
if item == "quit" then return true end
if item == "" then item = "air" end
local def = minetest.registered_nodes[item]
or minetest.registered_craftitems[item]
or minetest.registered_tools[item]
or minetest.registered_items[item]
if not def then return true end
local is_node = minetest.registered_nodes[item]
local param2
if def.paramtype2 == "facedir" then
param2 = minetest.dir_to_facedir(player:get_look_dir())
elseif def.paramtype2 == "wallmounted" then
param2 = minetest.dir_to_wallmounted(player:get_look_dir(), true)
end
for x = p1.x, p2.x, sign(p2.x - p1.x) do
for y = p1.y, p2.y, sign(p2.y - p1.y) do
for z = p1.z, p2.z, sign(p2.z - p1.z) do
if is_node then
minetest.set_node(vector.new(x, y, z), {name = item, param2 = param2})
else else
if key == "" then key = "air" end minetest.remove_node(vector.new(x, y, z))
local def = minetest.registered_nodes[key] end
if not def then return end if def.on_place then
local param2 local itemstack = ItemStack(item)
if def.paramtype2 == "facedir" then pointed_thing.intersection_point = vector.new(x + 0.5, y, z + 0.5)
param2 = minetest.dir_to_facedir(player:get_look_dir()) pointed_thing.above = vector.new(x, y, z)
elseif def.paramtype2 == "wallmounted" then pointed_thing.below = vector.new(x, y - 1, z)
param2 = minetest.dir_to_wallmounted(player:get_look_dir(), true) def.on_place(itemstack, player, pointed_thing)
end
--minetest.chat_send_all("" .. param2)
for x = p1.x, p2.x, sign(p2.x - p1.x) do
for y = p1.y, p2.y, sign(p2.y - p1.y) do
for z = p1.z, p2.z, sign(p2.z - p1.z) do
minetest.set_node({x = x, y = y, z = z}, {name = key, param2 = param2})
end
end
end
minetest.close_formspec(player:get_player_name(), "edit:pasteType")
clipboard[player:get_player_name()].fillBlock1Pos = nil
clipboard[player:get_player_name()].fillBlock2Pos = nil
end end
end end
end end
return true
end end
return false return true
end) end)
clipboard = {}; clipboard = {};
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
if player then if player then
clipboard[player:get_player_name()] = { clipboard[player:get_player_name()] = {
["fillBlock1Pos"] = nil, fill_block1 = nil,
["fillBlock2Pos"] = nil, fill_block2 = nil,
["copyBlock1Pos"] = nil, copy_block1_pos = nil,
["deleteBlock1Pos"] = nil, delete_block1_pos = nil,
["copyData"] = {}, copy_data = {},
}; };
end end
end); end);