1 if minetest.raycast == nil then
3 "================================\n"..
4 "This mod requires a suitable version of 0.4.16-dev/0.5.0-dev\n"..
5 "that includes support for minetest.raycast() [since 7th July 2017]\n"..
6 "================================\n"
10 local BRUSH_MAX_DIST = 150
11 local BRUSH_ALLOWED_COMMANDS = {
12 -- basically everything that only needs pos1
38 local brush_on_use = function(itemstack, placer)
39 local meta = itemstack:get_meta()
40 local name = placer:get_player_name()
42 local cmd = meta:get_string("command")
44 worldedit.player_notify(name,
45 "This brush is not bound, use //brush to bind a command to it.")
48 local cmddef = minetest.registered_chatcommands["/" .. cmd]
49 if cmddef == nil then return false end -- shouldn't happen as //brush checks this
50 local has_privs, missing_privs = minetest.check_player_privs(name, cmddef.privs)
52 worldedit.player_notify(name,
53 "Missing privileges: " .. table.concat(missing_privs, ", "))
57 local raybegin = vector.add(placer:get_pos(), {x=0, y=2, z=0}) -- player head
58 local rayend = vector.add(raybegin, vector.multiply(placer:get_look_dir(), BRUSH_MAX_DIST))
59 local ray = minetest.raycast(raybegin, rayend, false, true)
60 local pointed_thing = ray:next()
61 if pointed_thing == nil then
62 worldedit.player_notify(name, "Too far away.")
66 assert(pointed_thing.type == "node")
67 worldedit.pos1[name] = pointed_thing.under
68 worldedit.pos2[name] = nil
69 worldedit.mark_region(name)
70 -- is this a horrible hack? oh yes.
71 worldedit._override_safe_regions = true
72 local player_notify_old = worldedit.player_notify
73 worldedit.player_notify = function(name, msg)
74 if string.match(msg, "^%d") then return end -- discard "1234 nodes added."
75 return player_notify_old(name, msg)
78 minetest.log("action", string.format("%s uses WorldEdit brush (//%s) at %s",
79 name, cmd, minetest.pos_to_string(pointed_thing.under)))
80 cmddef.func(name, meta:get_string("params"))
82 worldedit._override_safe_regions = false
83 worldedit.player_notify = player_notify_old
87 minetest.register_tool(":worldedit:brush", {
88 description = "WorldEdit Brush",
89 inventory_image = "worldedit_brush.png",
90 stack_max = 1, -- no need to stack these (metadata prevents this anyway)
92 on_use = function(itemstack, placer, pointed_thing)
93 brush_on_use(itemstack, placer)
94 return itemstack -- nothing consumed, nothing changed
98 minetest.register_chatcommand("/brush", {
99 privs = {worldedit=true},
100 params = "none/<cmd> [parameters]",
101 description = "Assign command to WorldEdit brush item",
102 func = function(name, param)
103 local found, _, cmd, params = param:find("^([^%s]+)%s+(.+)$")
106 found, _, cmd = param:find("^(.+)$")
109 worldedit.player_notify(name, "Invalid usage.")
113 local itemstack = minetest.get_player_by_name(name):get_wielded_item()
114 if itemstack == nil or itemstack:get_name() ~= "worldedit:brush" then
115 worldedit.player_notify(name, "Not holding brush item.")
120 local meta = itemstack:get_meta()
121 if cmd == "none" then
123 worldedit.player_notify(name, "Brush assignment cleared.")
126 if table.indexof(BRUSH_ALLOWED_COMMANDS, cmd) ~= -1 then
127 cmddef = minetest.registered_chatcommands["/" .. cmd]
131 if cmddef == nil then
132 worldedit.player_notify(name, "Invalid command for brush use: //" .. cmd)
135 meta:set_string("command", cmd)
136 meta:set_string("params", params)
137 local fullcmd = "//" .. cmd .. " " .. params
138 meta:set_string("description",
139 minetest.registered_tools["worldedit:brush"].description .. ": " .. fullcmd)
140 worldedit.player_notify(name, "Brush assigned to command: " .. fullcmd)
142 minetest.get_player_by_name(name):set_wielded_item(itemstack)