Update stdcmd.lua

This commit is contained in:
Mihail 2024-07-31 16:18:29 +04:00 committed by GitHub
parent 4502a1206e
commit e05b9bff5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,13 +1,13 @@
local SEPARATOR = "________________" local SEPARATOR = "________________"
SEPARATOR = SEPARATOR..SEPARATOR..SEPARATOR SEPARATOR = SEPARATOR .. SEPARATOR .. SEPARATOR
function build_scheme(command) function build_scheme(command)
local str = command.name.." " local str = command.name .. " "
for i,arg in ipairs(command.args) do for i,arg in ipairs(command.args) do
if arg.optional then if arg.optional then
str = str.."["..arg.name.."] " str = str .. "[" .. arg.name .. "] "
else else
str = str.."<"..arg.name.."> " str = str .. "<" .. arg.name .. "> "
end end
end end
return str return str
@ -16,7 +16,7 @@ end
console.add_command( console.add_command(
"clear", "clear",
"Clears the console", "Clears the console",
function () function()
local document = Document.new("core:console") local document = Document.new("core:console")
document.log.text = "" document.log.text = ""
end end
@ -25,8 +25,7 @@ console.add_command(
console.add_command( console.add_command(
"help name:str=''", "help name:str=''",
"Show help infomation for the specified command", "Show help infomation for the specified command",
function (args, kwargs) function(args, kwargs)
local name = args[1] local name = args[1]
if #name == 0 then if #name == 0 then
@ -35,10 +34,10 @@ console.add_command(
local str = "Available commands:" local str = "Available commands:"
for i,k in ipairs(commands) do for i,k in ipairs(commands) do
str = str.."\n "..build_scheme(console.get_command_info(k)) str = str .. "\n " .. build_scheme(console.get_command_info(k))
end end
return str.."\nuse 'help <command>'" return str .. "\nuse 'help <command>'"
end end
@ -49,25 +48,24 @@ console.add_command(
end end
local where = ":" local where = ":"
local str = SEPARATOR.."\n"..command.description.."\n"..name.." " local str = SEPARATOR .. "\n" .. command.description .. "\n" .. name .. " "
for _, arg in ipairs(command.args) do for _, arg in ipairs(command.args) do
where = where.."\n "..arg.name.." - "..arg.type where = where .. "\n " .. arg.name .. " - " .. arg.type
if arg.optional then if arg.optional then
str = str.."["..arg.name.."] " str = str .. "[" .. arg.name .. "] "
where = where.." (optional)" where = where .. " (optional)"
else else
str = str.."<"..arg.name.."> " str = str .. "<" .. arg.name .. "> "
end end
end end
if #command.args > 0 then if #command.args > 0 then
str = str.."\nwhere"..where str = str .. "\nwhere" .. where
end end
return str.."\n"..SEPARATOR return str .. "\n" .. SEPARATOR
end end
) )
@ -75,43 +73,24 @@ console.add_command(
"time.uptime", "time.uptime",
"Get time elapsed since the engine started", "Get time elapsed since the engine started",
function() function()
local uptime = time.uptime() local uptime = time.uptime()
local years = math.floor(uptime / 31536000)
local days = math.floor((uptime % 31536000) / 86400) % 365
local hours = math.floor((uptime % 86400) / 3600) % 24
local minutes = math.floor((uptime % 3600) / 60) % 60
local seconds = math.floor(uptime % 60)
local formatted_uptime = "" local formatted_uptime = ""
if years > 0 then local t = string.FormattedTime(uptime)
formatted_uptime = formatted_uptime .. years .. "y "
end
if days > 0 or years > 0 then
formatted_uptime = formatted_uptime .. days .. "d "
end
if hours > 0 or days > 0 or years > 0 then
formatted_uptime = formatted_uptime .. hours .. "h "
end
if minutes > 0 or hours > 0 or days > 0 or years > 0 then
formatted_uptime = formatted_uptime .. minutes .. "m "
end
if seconds > 0 or minutes > 0 or hours > 0 or days > 0 or years > 0 then
formatted_uptime = formatted_uptime .. seconds .. "s"
end
return uptime .. " (" .. formatted_uptime .. ")" formatted_uptime = t.h .. "h " .. t.m .. "m " .. t.s .. "s"
return formatted_uptime .. " (" .. uptime .. "s)"
end end
) )
console.add_command( console.add_command(
"tp entity:sel=$entity.id x:num~pos.x y:num~pos.y z:num~pos.z", "tp entity:sel=$entity.id x:num~pos.x y:num~pos.y z:num~pos.z",
"Teleport entity", "Teleport entity",
function (args, kwargs) function(args, kwargs)
local eid, x, y, z = unpack(args) local eid, x, y, z = unpack(args)
local entity = entities.get(eid) local entity = entities.get(eid)
if entity ~= nil then if entity then
entity.transform:set_pos({x, y, z}) entity.transform:set_pos({x, y, z})
end end
end end
@ -119,53 +98,54 @@ console.add_command(
console.add_command( console.add_command(
"echo value:str", "echo value:str",
"Print value to the console", "Print value to the console",
function (args, kwargs) function(args, kwargs)
return args[1] return args[1]
end end
) )
console.add_command( console.add_command(
"time.set value:num", "time.set value:num",
"Set day time [0..1] where 0 is midnight, 0.5 is noon", "Set day time [0..1] where 0 is midnight, 0.5 is noon",
function (args, kwargs) function(args, kwargs)
return world.set_day_time(args[1]) world.set_day_time(args[1])
return "Time set to " .. args[1]
end end
) )
console.add_command( console.add_command(
"blocks.fill id:str x:num~pos.x y:num~pos.y z:num~pos.z w:int h:int d:int", "blocks.fill id:str x:num~pos.x y:num~pos.y z:num~pos.z w:int h:int d:int",
"Fill specified zone with blocks", "Fill specified zone with blocks",
function (args, kwargs) function(args, kwargs)
local name, x, y, z, w, h, d = unpack(args) local name, x, y, z, w, h, d = unpack(args)
local id = block.index(name) local id = block.index(name)
for ly=0,h-1 do for ly = 0, h - 1 do
for lz=0,d-1 do for lz = 0, d - 1 do
for lx=0,w-1 do for lx = 0, w - 1 do
block.set(x+lx, y+ly, z+lz, id) block.set(x + lx, y + ly, z + lz, id)
end end
end end
end end
return tostring(w*h*d).." blocks set" return tostring(w * h * d) .. " blocks set"
end end
) )
console.add_command( console.add_command(
"player.respawn player:sel=$obj.id", "player.respawn player:sel=$obj.id",
"Respawn player entity", "Respawn player entity",
function (args, kwargs) function(args, kwargs)
local eid = entities.spawn("base:player", {player.get_pos(args[1])}):get_uid() local eid = entities.spawn("base:player", {player.get_pos(args[1])}):get_uid()
player.set_entity(args[1], eid) player.set_entity(args[1], eid)
return "spawned new player entity #"..tostring(eid) return "spawned new player entity #" .. tostring(eid)
end end
) )
console.add_command( console.add_command(
"entity.despawn entity:sel=$entity.selected", "entity.despawn entity:sel=$entity.selected",
"Despawn entity", "Despawn entity",
function (args, kwargs) function(args, kwargs)
local eid = args[1] local eid = args[1]
local entity = entities.get(eid) local entity = entities.get(eid)
if entity ~= nil then if entity ~= nil then
entity:despawn() entity:despawn()
return "despawned entity #"..tostring(eid) return "despawned entity #" .. tostring(eid)
end end
end end
) )