1 if minetest.raycast == nil then
3 "worldedit_brush requires at least Minetest 5.0"
7 local BRUSH_MAX_DIST = 150
8 local BRUSH_ALLOWED_COMMANDS = {
9 -- basically everything that only needs pos1
10 -- TODO: determine automatically now that `require_pos` exists
36 local brush_on_use = function(itemstack, placer)
37 local meta = itemstack:get_meta()
38 local name = placer:get_player_name()
40 local cmd = meta:get_string("command")
42 worldedit.player_notify(name,
43 "This brush is not bound, use //brush to bind a command to it.")
47 local cmddef = worldedit.registered_commands[cmd]
48 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(),
58 {x=0, y=placer:get_properties().eye_height, z=0})
59 local rayend = vector.add(raybegin, vector.multiply(placer:get_look_dir(), BRUSH_MAX_DIST))
60 local ray = minetest.raycast(raybegin, rayend, false, true)
61 local pointed_thing = ray:next()
62 if pointed_thing == nil then
63 worldedit.player_notify(name, "Too far away.")
67 assert(pointed_thing.type == "node")
68 worldedit.pos1[name] = pointed_thing.under
69 worldedit.pos2[name] = nil
70 worldedit.mark_region(name)
72 -- this isn't really clean...
73 local player_notify_old = worldedit.player_notify
74 worldedit.player_notify = function(name, msg)
75 if string.match(msg, "^%d") then return end -- discard "1234 nodes added."
76 return player_notify_old(name, msg)
79 assert(cmddef.require_pos < 2)
80 local parsed = {cmddef.parse(meta:get_string("params"))}
81 if not table.remove(parsed, 1) then return false end -- shouldn't happen
83 minetest.log("action", string.format("%s uses WorldEdit brush (//%s) at %s",
84 name, cmd, minetest.pos_to_string(pointed_thing.under)))
85 cmddef.func(name, unpack(parsed))
87 worldedit.player_notify = player_notify_old
91 minetest.register_tool(":worldedit:brush", {
92 description = "WorldEdit Brush",
93 inventory_image = "worldedit_brush.png",
94 stack_max = 1, -- no need to stack these (metadata prevents this anyway)
96 on_use = function(itemstack, placer, pointed_thing)
97 brush_on_use(itemstack, placer)
98 return itemstack -- nothing consumed, nothing changed
102 worldedit.register_command("brush", {
103 privs = {worldedit=true},
104 params = "none/<cmd> [parameters]",
105 description = "Assign command to WorldEdit brush item",
106 parse = function(param)
107 local found, _, cmd, params = param:find("^([^%s]+)%s+(.+)$")
110 found, _, cmd = param:find("^(.+)$")
115 return true, cmd, params
117 func = function(name, cmd, params)
118 local itemstack = minetest.get_player_by_name(name):get_wielded_item()
119 if itemstack == nil or itemstack:get_name() ~= "worldedit:brush" then
120 worldedit.player_notify(name, "Not holding brush item.")
125 local meta = itemstack:get_meta()
126 if cmd == "none" then
128 worldedit.player_notify(name, "Brush assignment cleared.")
131 if table.indexof(BRUSH_ALLOWED_COMMANDS, cmd) ~= -1 then
132 cmddef = worldedit.registered_commands[cmd]
134 if cmddef == nil then
135 worldedit.player_notify(name, "Invalid command for brush use: //" .. cmd)
139 -- Try parsing command params so we can give the user feedback
140 local ok, err = cmddef.parse(params)
142 err = err or "invalid usage"
143 worldedit.player_notify(name, "Brush command: " .. err)
147 meta:set_string("command", cmd)
148 meta:set_string("params", params)
149 local fullcmd = "//" .. cmd .. " " .. params
150 meta:set_string("description",
151 minetest.registered_tools["worldedit:brush"].description .. ": " .. fullcmd)
152 worldedit.player_notify(name, "Brush assigned to command: " .. fullcmd)
154 minetest.get_player_by_name(name):set_wielded_item(itemstack)