Skip to content

Commit 1f7b43f

Browse files
author
SX
committed
Include chatcommands, world to separate file, update LICENSE
1 parent 54fe3d5 commit 1f7b43f

File tree

5 files changed

+219
-34
lines changed

5 files changed

+219
-34
lines changed

LICENSE

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
MIT License
1+
--
2+
-- MIT License for Mineunit main framework code.
3+
-- Applies to everything that is not covered with additional copyright notes.
4+
--
5+
-- Notes:
6+
-- MIT license might not apply to code in common, default or game subdirectories.
7+
-- See notes below MIT license, notes in source code and copyright notes for Minetest.
8+
--
29

3-
Copyright (c) 2020 Minetest mods
10+
Copyright (c) 2020 SX <50966843+S-S-X@users.noreply.github.com>
411

512
Permission is hereby granted, free of charge, to any person obtaining a copy
613
of this software and associated documentation files (the "Software"), to deal
@@ -19,3 +26,28 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1926
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2027
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2128
SOFTWARE.
29+
30+
--
31+
-- Minetest LGPLv2.1 license contents
32+
--
33+
34+
Minetest
35+
Copyright (C) 2010-2018 celeron55, Perttu Ahola <celeron55@gmail.com>
36+
37+
This program is free software; you can redistribute it and/or modify
38+
it under the terms of the GNU Lesser General Public License as published by
39+
the Free Software Foundation; either version 2.1 of the License, or
40+
(at your option) any later version.
41+
42+
This program is distributed in the hope that it will be useful,
43+
but WITHOUT ANY WARRANTY; without even the implied warranty of
44+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45+
GNU Lesser General Public License for more details.
46+
47+
You should have received a copy of the GNU Lesser General Public License along
48+
with this program; if not, write to the Free Software Foundation, Inc.,
49+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
50+
51+
--
52+
-- Additional licenses apply, see source files for more information.
53+
--

common/chatcommands.lua

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
-- Minetest: builtin/common/chatcommands.lua
2+
3+
core.registered_chatcommands = {}
4+
5+
function core.register_chatcommand(cmd, def)
6+
def = def or {}
7+
def.params = def.params or ""
8+
def.description = def.description or ""
9+
def.privs = def.privs or {}
10+
def.mod_origin = core.get_current_modname() or "??"
11+
core.registered_chatcommands[cmd] = def
12+
end
13+
14+
function core.unregister_chatcommand(name)
15+
if core.registered_chatcommands[name] then
16+
core.registered_chatcommands[name] = nil
17+
else
18+
core.log("warning", "Not unregistering chatcommand " ..name..
19+
" because it doesn't exist.")
20+
end
21+
end
22+
23+
function core.override_chatcommand(name, redefinition)
24+
local chatcommand = core.registered_chatcommands[name]
25+
assert(chatcommand, "Attempt to override non-existent chatcommand "..name)
26+
for k, v in pairs(redefinition) do
27+
rawset(chatcommand, k, v)
28+
end
29+
core.registered_chatcommands[name] = chatcommand
30+
end
31+
32+
local cmd_marker = "/"
33+
34+
local function gettext(...)
35+
return ...
36+
end
37+
38+
local function gettext_replace(text, replace)
39+
return text:gsub("$1", replace)
40+
end
41+
42+
43+
if INIT == "client" then
44+
cmd_marker = "."
45+
gettext = core.gettext
46+
gettext_replace = fgettext_ne
47+
end
48+
49+
local function do_help_cmd(name, param)
50+
local function format_help_line(cmd, def)
51+
local msg = core.colorize("#00ffff", cmd_marker .. cmd)
52+
if def.params and def.params ~= "" then
53+
msg = msg .. " " .. def.params
54+
end
55+
if def.description and def.description ~= "" then
56+
msg = msg .. ": " .. def.description
57+
end
58+
return msg
59+
end
60+
if param == "" then
61+
local cmds = {}
62+
for cmd, def in pairs(core.registered_chatcommands) do
63+
if INIT == "client" or core.check_player_privs(name, def.privs) then
64+
cmds[#cmds + 1] = cmd
65+
end
66+
end
67+
table.sort(cmds)
68+
return true, gettext("Available commands: ") .. table.concat(cmds, " ") .. "\n"
69+
.. gettext_replace("Use '$1help <cmd>' to get more information,"
70+
.. " or '$1help all' to list everything.", cmd_marker)
71+
elseif param == "all" then
72+
local cmds = {}
73+
for cmd, def in pairs(core.registered_chatcommands) do
74+
if INIT == "client" or core.check_player_privs(name, def.privs) then
75+
cmds[#cmds + 1] = format_help_line(cmd, def)
76+
end
77+
end
78+
table.sort(cmds)
79+
return true, gettext("Available commands:").."\n"..table.concat(cmds, "\n")
80+
elseif INIT == "game" and param == "privs" then
81+
local privs = {}
82+
for priv, def in pairs(core.registered_privileges) do
83+
privs[#privs + 1] = priv .. ": " .. def.description
84+
end
85+
table.sort(privs)
86+
return true, "Available privileges:\n"..table.concat(privs, "\n")
87+
else
88+
local cmd = param
89+
local def = core.registered_chatcommands[cmd]
90+
if not def then
91+
return false, gettext("Command not available: ")..cmd
92+
else
93+
return true, format_help_line(cmd, def)
94+
end
95+
end
96+
end
97+
98+
if INIT == "client" then
99+
core.register_chatcommand("help", {
100+
params = gettext("[all | <cmd>]"),
101+
description = gettext("Get help for commands"),
102+
func = function(param)
103+
return do_help_cmd(nil, param)
104+
end,
105+
})
106+
else
107+
core.register_chatcommand("help", {
108+
params = "[all | privs | <cmd>]",
109+
description = "Get help for commands or list privileges",
110+
func = do_help_cmd,
111+
})
112+
end

core.lua

+1-31
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,7 @@ local noop_object = {
55
__index = function(...) return function(...)end end,
66
}
77

8-
_G.world = { nodes = {} }
9-
local world = _G.world
10-
_G.world.set_node = function(pos, node)
11-
local hash = minetest.hash_node_position(pos)
12-
world.nodes[hash] = node
13-
local nodedef = minetest.registered_nodes[node.name]
14-
-- Execute on_construct callbacks
15-
if nodedef and type(nodedef.on_construct) == "function" then
16-
nodedef.on_construct(pos)
17-
end
18-
end
19-
_G.world.clear = function() _G.world.nodes = {} end
20-
_G.world.layout = function(layout, offset)
21-
_G.world.clear()
22-
_G.world.add_layout(layout, offset)
23-
end
24-
_G.world.add_layout = function(layout, offset)
25-
for _, node in ipairs(layout) do
26-
local pos = {
27-
x = node[1].x,
28-
y = node[1].y,
29-
z = node[1].z,
30-
}
31-
if offset then
32-
pos.x = pos.x + offset.x
33-
pos.y = pos.y + offset.y
34-
pos.z = pos.z + offset.z
35-
end
36-
_G.world.set_node(pos, {name=node[2], param2=0})
37-
end
38-
end
8+
_G.world = mineunit("world")
399

4010
_G.core.get_worldpath = function(...) return _G.mineunit:get_worldpath(...) end
4111
_G.core.get_modpath = function(...) return _G.mineunit:get_modpath(...) end

init.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ mineunit.__index = mineunit
4242
local _mineunits = {}
4343
setmetatable(mineunit, {
4444
__call = function(self, name)
45+
local res
4546
if not _mineunits[name] then
4647
mineunit:debug("Loading mineunit module", name)
47-
require("mineunit." .. name:gsub("/", "."))
48+
res = require("mineunit." .. name:gsub("/", "."))
4849
end
4950
_mineunits[name] = true
51+
return res
5052
end,
5153
})
5254

world.lua

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
local world = {
2+
nodes = {}
3+
}
4+
5+
-- Helper to execute callbacks
6+
local function call(fn, ...)
7+
if type(fn) == "function" then
8+
return fn(...)
9+
end
10+
end
11+
12+
-- Static pointed_thing
13+
local function get_pointed_thing(pos, pointed_thing_type)
14+
return {
15+
type = pointed_thing_type or "node",
16+
above = {x=pos.x,y=pos.y+1,z=pos.z}, -- Pointing from above to downwards,
17+
under = {x=pos.x,y=pos.y,z=pos.z}, -- crosshair at protected node surface
18+
}
19+
end
20+
21+
-- set_node sets world node without callbacks
22+
function world.set_node(pos, node)
23+
local hash = minetest.hash_node_position(pos)
24+
world.nodes[hash] = node
25+
local nodedef = minetest.registered_nodes[node.name]
26+
if nodedef then
27+
call(nodedef.on_construct, pos)
28+
end
29+
end
30+
31+
-- Called after constructing node when node was placed using
32+
-- minetest.item_place_node / minetest.place_node.
33+
-- If return true no item is taken from itemstack.
34+
function world.place_node(pos, node, placer, itemstack, pointed_thing)
35+
world.set_node(pos, node)
36+
local nodedef = minetest.registered_nodes[node.name]
37+
if nodedef then
38+
itemstack = itemstack or ItemStack(node.name .. " 1")
39+
pointed_thing = pointed_thing or get_pointed_thing(pos)
40+
call(nodedef.after_place_node, pos, placer, itemstack, pointed_thing)
41+
end
42+
end
43+
44+
function world.clear()
45+
world.nodes = {}
46+
end
47+
48+
function world.layout(layout, offset)
49+
world.clear()
50+
world.add_layout(layout, offset)
51+
end
52+
53+
function world.add_layout(layout, offset)
54+
for _, node in ipairs(layout) do
55+
local pos = {
56+
x = node[1].x,
57+
y = node[1].y,
58+
z = node[1].z,
59+
}
60+
if offset then
61+
pos.x = pos.x + offset.x
62+
pos.y = pos.y + offset.y
63+
pos.z = pos.z + offset.z
64+
end
65+
_G.world.set_node(pos, {name=node[2], param2=0})
66+
end
67+
end
68+
69+
return world

0 commit comments

Comments
 (0)