Improve command system and add command blocks

This commit is contained in:
IamPyu 2024-12-07 20:28:34 -06:00
parent c8a5f27f28
commit f883ccc3f5
12 changed files with 191 additions and 341 deletions

View File

@ -93,7 +93,7 @@ core.register_on_dieplayer(function(player, reason)
end
end
elseif reason.type == "node_damage" then
local desc = minetest.registered_nodes[reason.node].description or "a block"
local desc = core.registered_nodes[reason.node].description or "a block"
message = string.format("%s was killed by %s", playername, desc)
end

View File

@ -1,325 +0,0 @@
# Made in Blockbench 4.11.1
mtllib pyutest-frying-pan.mtl
o cube
v 0.5 -0.4375 0.5
v 0.5 -0.4375 -0.5
v 0.5 -0.5 0.5
v 0.5 -0.5 -0.5
v -0.5 -0.4375 -0.5
v -0.5 -0.4375 0.5
v -0.5 -0.5 -0.5
v -0.5 -0.5 0.5
vt 0 1
vt 1 1
vt 1 0.9375
vt 0 0.9375
vt 0 1
vt 1 1
vt 1 0.9375
vt 0 0.9375
vt 0 1
vt 1 1
vt 1 0.9375
vt 0 0.9375
vt 0 1
vt 1 1
vt 1 0.9375
vt 0 0.9375
vt 0 1
vt 1 1
vt 1 0
vt 0 0
vt 0 1
vt 1 1
vt 1 0
vt 0 0
vn 0 0 -1
vn 1 0 0
vn 0 0 1
vn -1 0 0
vn 0 1 0
vn 0 -1 0
usemtl none
f 4/4/1 7/3/1 5/2/1 2/1/1
f 3/8/2 4/7/2 2/6/2 1/5/2
f 8/12/3 3/11/3 1/10/3 6/9/3
f 7/16/4 8/15/4 6/14/4 5/13/4
f 6/20/5 1/19/5 2/18/5 5/17/5
f 7/24/6 4/23/6 3/22/6 8/21/6
o cube
v 0.5 -0.3125 -0.4375
v 0.5 -0.3125 -0.5
v 0.5 -0.4375 -0.4375
v 0.5 -0.4375 -0.5
v -0.5 -0.3125 -0.5
v -0.5 -0.3125 -0.4375
v -0.5 -0.4375 -0.5
v -0.5 -0.4375 -0.4375
vt 0 1
vt 1 1
vt 1 0.875
vt 0 0.875
vt 0 1
vt 0.0625 1
vt 0.0625 0.875
vt 0 0.875
vt 0 1
vt 1 1
vt 1 0.875
vt 0 0.875
vt 0 1
vt 0.0625 1
vt 0.0625 0.875
vt 0 0.875
vt 0 1
vt 1 1
vt 1 0.9375
vt 0 0.9375
vt 0 1
vt 1 1
vt 1 0.9375
vt 0 0.9375
vn 0 0 -1
vn 1 0 0
vn 0 0 1
vn -1 0 0
vn 0 1 0
vn 0 -1 0
usemtl none
f 12/28/7 15/27/7 13/26/7 10/25/7
f 11/32/8 12/31/8 10/30/8 9/29/8
f 16/36/9 11/35/9 9/34/9 14/33/9
f 15/40/10 16/39/10 14/38/10 13/37/10
f 14/44/11 9/43/11 10/42/11 13/41/11
f 15/48/12 12/47/12 11/46/12 16/45/12
o cube
v 0.5 -0.3125 0.5
v 0.5 -0.3125 0.4375
v 0.5 -0.4375 0.5
v 0.5 -0.4375 0.4375
v -0.5 -0.3125 0.4375
v -0.5 -0.3125 0.5
v -0.5 -0.4375 0.4375
v -0.5 -0.4375 0.5
vt 0 1
vt 1 1
vt 1 0.875
vt 0 0.875
vt 0 1
vt 0.0625 1
vt 0.0625 0.875
vt 0 0.875
vt 0 1
vt 1 1
vt 1 0.875
vt 0 0.875
vt 0 1
vt 0.0625 1
vt 0.0625 0.875
vt 0 0.875
vt 0 1
vt 1 1
vt 1 0.9375
vt 0 0.9375
vt 0 1
vt 1 1
vt 1 0.9375
vt 0 0.9375
vn 0 0 -1
vn 1 0 0
vn 0 0 1
vn -1 0 0
vn 0 1 0
vn 0 -1 0
usemtl none
f 20/52/13 23/51/13 21/50/13 18/49/13
f 19/56/14 20/55/14 18/54/14 17/53/14
f 24/60/15 19/59/15 17/58/15 22/57/15
f 23/64/16 24/63/16 22/62/16 21/61/16
f 22/68/17 17/67/17 18/66/17 21/65/17
f 23/72/18 20/71/18 19/70/18 24/69/18
o cube
v 0.4375000000000001 -0.3125 0.4375
v 0.5 -0.3125 0.4375
v 0.4375000000000001 -0.4375 0.4375
v 0.5 -0.4375 0.4375
v 0.4999999999999999 -0.3125 -0.4375
v 0.4374999999999999 -0.3125 -0.4375
v 0.4999999999999999 -0.4375 -0.4375
v 0.4374999999999999 -0.4375 -0.4375
vt 0 1
vt 0.875 1
vt 0.875 0.875
vt 0 0.875
vt 0 1
vt 0.0625 1
vt 0.0625 0.875
vt 0 0.875
vt 0 1
vt 0.875 1
vt 0.875 0.875
vt 0 0.875
vt 0 1
vt 0.0625 1
vt 0.0625 0.875
vt 0 0.875
vt 0 1
vt 0.875 1
vt 0.875 0.9375
vt 0 0.9375
vt 0 1
vt 0.875 1
vt 0.875 0.9375
vt 0 0.9375
vn 1 0 -2.220446049250313e-16
vn 2.220446049250313e-16 0 1
vn -1 0 2.220446049250313e-16
vn -2.220446049250313e-16 0 -1
vn 0 1 0
vn 0 -1 0
usemtl none
f 28/76/19 31/75/19 29/74/19 26/73/19
f 27/80/20 28/79/20 26/78/20 25/77/20
f 32/84/21 27/83/21 25/82/21 30/81/21
f 31/88/22 32/87/22 30/86/22 29/85/22
f 30/92/23 25/91/23 26/90/23 29/89/23
f 31/96/24 28/95/24 27/94/24 32/93/24
o cube
v -0.4999999999999999 -0.3125 0.4375
v -0.4374999999999999 -0.3125 0.4375
v -0.4999999999999999 -0.4375 0.4375
v -0.4374999999999999 -0.4375 0.4375
v -0.4375000000000001 -0.3125 -0.4375
v -0.5000000000000001 -0.3125 -0.4375
v -0.4375000000000001 -0.4375 -0.4375
v -0.5000000000000001 -0.4375 -0.4375
vt 0 1
vt 0.875 1
vt 0.875 0.875
vt 0 0.875
vt 0 1
vt 0.0625 1
vt 0.0625 0.875
vt 0 0.875
vt 0 1
vt 0.875 1
vt 0.875 0.875
vt 0 0.875
vt 0 1
vt 0.0625 1
vt 0.0625 0.875
vt 0 0.875
vt 0 1
vt 0.875 1
vt 0.875 0.9375
vt 0 0.9375
vt 0 1
vt 0.875 1
vt 0.875 0.9375
vt 0 0.9375
vn 1 0 -2.220446049250313e-16
vn 2.220446049250313e-16 0 1
vn -1 0 2.220446049250313e-16
vn -2.220446049250313e-16 0 -1
vn 0 1 0
vn 0 -1 0
usemtl none
f 36/100/25 39/99/25 37/98/25 34/97/25
f 35/104/26 36/103/26 34/102/26 33/101/26
f 40/108/27 35/107/27 33/106/27 38/105/27
f 39/112/28 40/111/28 38/110/28 37/109/28
f 38/116/29 33/115/29 34/114/29 37/113/29
f 39/120/30 36/119/30 35/118/30 40/117/30
o cube
v 0.0625 -0.3125 1.25
v 0.0625 -0.3125 0.625
v 0.0625 -0.4375 1.25
v 0.0625 -0.4375 0.625
v -0.0625 -0.3125 0.625
v -0.0625 -0.3125 1.25
v -0.0625 -0.4375 0.625
v -0.0625 -0.4375 1.25
vt 0 1
vt 0.125 1
vt 0.125 0.875
vt 0 0.875
vt 0 1
vt 0.625 1
vt 0.625 0.875
vt 0 0.875
vt 0 1
vt 0.125 1
vt 0.125 0.875
vt 0 0.875
vt 0 1
vt 0.625 1
vt 0.625 0.875
vt 0 0.875
vt 0 1
vt 0.125 1
vt 0.125 0.375
vt 0 0.375
vt 0 1
vt 0.125 1
vt 0.125 0.375
vt 0 0.375
vn 0 0 -1
vn 1 0 0
vn 0 0 1
vn -1 0 0
vn 0 1 0
vn 0 -1 0
usemtl none
f 44/124/31 47/123/31 45/122/31 42/121/31
f 43/128/32 44/127/32 42/126/32 41/125/32
f 48/132/33 43/131/33 41/130/33 46/129/33
f 47/136/34 48/135/34 46/134/34 45/133/34
f 46/140/35 41/139/35 42/138/35 45/137/35
f 47/144/36 44/143/36 43/142/36 48/141/36
o cube
v 0.125 -0.3125 0.625
v 0.125 -0.3125 0.5
v 0.125 -0.4375 0.625
v 0.125 -0.4375 0.5
v -0.125 -0.3125 0.5
v -0.125 -0.3125 0.625
v -0.125 -0.4375 0.5
v -0.125 -0.4375 0.625
vt 0 1
vt 0.25 1
vt 0.25 0.875
vt 0 0.875
vt 0 1
vt 0.125 1
vt 0.125 0.875
vt 0 0.875
vt 0 1
vt 0.25 1
vt 0.25 0.875
vt 0 0.875
vt 0 1
vt 0.125 1
vt 0.125 0.875
vt 0 0.875
vt 0 1
vt 0.25 1
vt 0.25 0.875
vt 0 0.875
vt 0 1
vt 0.25 1
vt 0.25 0.875
vt 0 0.875
vn 0 0 -1
vn 1 0 0
vn 0 0 1
vn -1 0 0
vn 0 1 0
vn 0 -1 0
usemtl none
f 52/148/37 55/147/37 53/146/37 50/145/37
f 51/152/38 52/151/38 50/150/38 49/149/38
f 56/156/39 51/155/39 49/154/39 54/153/39
f 55/160/40 56/159/40 54/158/40 53/157/40
f 54/164/41 49/163/41 50/162/41 53/161/41
f 55/168/42 52/167/42 51/166/42 56/165/42

File diff suppressed because one or more lines are too long

View File

@ -317,3 +317,69 @@ PyuTest.drop_item = function(pos, name)
o:add_velocity(vector.new(math.random(-2, 2), 5, math.random(-2, 2)))
end
end
PyuTest.chatcommand_entity_selector = function(caller, param)
local p_caller = core.get_player_by_name(caller)
if core.get_player_by_name(param) ~= nil then
return {param}
end
if param == "@self" or param == "@s" then
return {caller}
elseif param == "@random" or param == "@r" then
local players = core.get_connected_players()
return {players[math.random(#players)]:get_player_name()}
elseif param == "@nearest" or param == "@n" then
local players = core.get_connected_players()
local pos = p_caller:get_pos()
local cd = math.huge
local nearest
for _, v in pairs(players) do
local dist = vector.distance(pos, v:get_pos())
if dist < cd and v ~= p_caller then
cd = dist
nearest = v
end
end
if nearest then
return {nearest:get_player_name()}
end
elseif param == "@all" or param == "@a" then
local list = {}
for _, v in pairs(core.get_connected_players()) do
list[#list+1] = v:get_player_name()
end
return list
end
return {caller}
end
PyuTest.execute_as = function(runner, command)
local pos = command:find(" ")
local cmd, param = command, ""
if pos then
cmd = command:sub(1, pos - 1)
param = command:sub(pos + 1)
end
local cmddef = core.chatcommands[cmd]
if not cmddef then
core.chat_send_player(runner, "The command "..cmd.." does not exist")
return
end
local has_privs, missing_privs = core.check_player_privs(runner, cmddef.privs)
if not has_privs then
core.chat_send_player(runner, "You don't have permission "
.."to run "..cmd
.." (missing privileges: "
..table.concat(missing_privs, ", ")..")")
return
end
cmddef.func(runner, param)
end

View File

@ -79,6 +79,8 @@ PyuTest.make_building_blocks = function(name, desc, tex, colortint, cgroups, ext
local id_door = name .. "_door"
local id_sign = name .. "_sign"
core.register_alias(name, id_block)
table.insert(PyuTest.building_blocks, {
ns = name:split(":")[1],
name = name:split(":")[2],

View File

@ -0,0 +1,29 @@
local function formspec()
return table.concat({
"field[setcmd;Set Command;${command}]"
})
end
PyuTest.make_electricity_device("pyutest_electricity:command_block", "Command Block", {
cracky = PyuTest.BLOCK_FAST,
}, {"pyutest-device.png"}, nil, {
after_place_node = function(pos, placer)
local meta = core.get_meta(pos)
meta:set_string("owner", placer:get_player_name())
meta:set_string("command", "say Hello World!")
meta:set_string("formspec", formspec())
end,
on_receive_fields = function(pos, formname, fields, player)
if fields.setcmd then
local cmd = fields.setcmd
local meta = core.get_meta(pos)
meta:set_string("command", cmd)
end
end
}, function (pos, node)
local meta = core.get_meta(pos)
local owner = meta:get_string("owner")
local command = meta:get_string("command")
PyuTest.execute_as(owner, command)
end)

View File

@ -50,6 +50,10 @@ PyuTest.make_wire = function(id, desc, groups, color, opts, texture, delay)
inventory_image = "pyutest-powder.png",
wield_image = "pyutest-powder.png",
buildable_to = true,
selection_box = {
type = "fixed",
fixed = PyuTest.NODE_BOXES.CARPET.fixed
},
__on_electricity_activated = function(pos, node, sender_pos)
for _, v in pairs(PyuTest.get_full_neighbours(pos)) do
@ -113,5 +117,6 @@ core.register_craft({
})
dofile(modpath .. "/devices.lua")
dofile(modpath .. "/devices/command_block.lua")
dofile(modpath .. "/delayer.lua")
dofile(modpath .. "/components.lua")

View File

@ -1,4 +1,82 @@
local modpath = core.get_modpath("pyutest_cmds")
core.register_chatcommand("say", {
params = "<text>",
description = [[Say <text>
There are some text replacement macros you can use.
Here is a list of them:
- %s: Replaced with your username
]],
func = function(name, param)
local str = param
str = str:gsub("%%s", name)
core.chat_send_all(str)
end
})
core.register_chatcommand("teleport", {
params = "<player> <x> <y> <z>",
description = "Teleport <player> to <x> <y> <z>",
func = function(name, param)
local found, _, player, x, y, z = param:find("^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+([^%s]+)$")
if found == nil then
core.chat_send_player(name, "Invalid usage: " .. param)
return
end
local targets = PyuTest.chatcommand_entity_selector(name, player)
for _, v in pairs(targets) do
if not core.get_player_by_name(v) then
core.chat_send_player(name, "Invalid player: " .. v)
return
end
end
for _, v in pairs(targets) do
local plr = core.get_player_by_name(v)
local pos = plr:get_pos()
local nx = core.parse_relative_number(x, pos.x)
local ny = core.parse_relative_number(y, pos.y)
local nz = core.parse_relative_number(z, pos.z)
plr:set_pos(vector.new(nx, ny, nz))
end
end
})
core.register_chatcommand("execute", {
params = "at|as <player> <command>",
description = "Execute <command> as or at <player>",
func = function (name, param)
local found, _, method, player, command = param:find("^([^%s]+)%s+([^%s]+)%s+(.*)$")
if found == nil then
core.chat_send_player(name, "Invalid usage: " .. param)
return
end
local targets = PyuTest.chatcommand_entity_selector(name, player)
for _, v in pairs(targets) do
if not core.get_player_by_name(v) then
core.chat_send_player(name, "Invalid player: " .. v)
return
end
end
for _, v in pairs(targets) do
if method == "as" then
PyuTest.execute_as(v, command)
elseif method == "at" then
-- TODO
end
end
end
})
dofile(modpath .. "/worldedit.lua")
dofile(modpath .. "/gameplay.lua")
dofile(modpath .. "/fun.lua")

View File

@ -24,10 +24,10 @@ core.register_chatcommand("replacenear", {
local function replace(p)
for _, v in pairs(from) do
local name = core.get_node(p).name
local anyblock_match = (v == "anyblock" or v == "anynode" and name ~= "air" and name ~= "ignore")
local nname = core.get_node(p).name
local anyblock_match = (v == "anyblock" or v == "anynode" and nname ~= "air" and nname ~= "ignore")
if name == v or v == "any" or anyblock_match then
if nname == v or v == "any" or anyblock_match then
core.set_node(p, { name = to })
replaced = replaced + 1
end

View File

@ -10,23 +10,19 @@ PyuTest.Hudbar = {}
local HudbarMeta = { __index = PyuTest.Hudbar, __newindex = PyuTest.Hudbar }
core.register_on_joinplayer(function(player)
local key = player:get_player_name()
PyuTest.HUDBARS.players[key] = PyuTest.HUDBARS.players[key] or {}
for k, v in pairs(PyuTest.HUDBARS.defs) do
local key = player:get_player_name()
if PyuTest.HUDBARS.players[key] == nil then
PyuTest.HUDBARS.players[key] = {}
end
local ref = PyuTest.HUDBARS.players[key]
ref[k] = {idx = player:hud_add(v.cfg), name = k}
end
end)
core.register_globalstep(function(dtime)
for _, player in pairs(core.get_connected_players()) do
for _, v in pairs(PyuTest.HUDBARS.players[player:get_player_name()]) do
PyuTest.HUDBARS.defs[v.name].update(v.idx, player)
end
end
core.register_globalstep(function(dtime)
for _, v in pairs(PyuTest.HUDBARS.players[key]) do
PyuTest.HUDBARS.defs[v.name].update(v.idx, player)
end
end)
end)
function PyuTest.Hudbar:add(name, def, update)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 172 B

After

Width:  |  Height:  |  Size: 153 B