This repository was archived by the owner on Jan 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_commands.lua
More file actions
79 lines (66 loc) · 2.82 KB
/
example_commands.lua
File metadata and controls
79 lines (66 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
-- Attempt to get jelly_console
local console = rawget(_G, 'jelly_console')
-- If jelly_console is not installed, create a fake table
-- this table will contain functions that simply do nothing.
-- This way, you can still add functions even if the user
-- hasn't installed jell_console. They won't work, but
-- they won't break anything either.
if not console then
local console_light = {
add_command = function() end,
get_scope = function() return {} end
}
console = setmetatable({}, { __index = console_light })
end
-- Adds a new command which just prints what it receives
console.add_command(
'@example_echo',
function(
name, -- name of the command the user used to call this command
args, -- the arguments as table. If the function was called as @name (which can be figured out by looking at `name`), this is simply whitespace separated. Otherwise, it's grouped as defined by Jelly.Console+
arg_str, -- the complete argument string as passed by the user. Useful when you won't need more than one parameter or require text input
response -- the response object from the console call. This can be used to reject/resolve. Normally, returning a value results in a resolve returning these values, whereas an error results in a reject with the error message
)
-- Print all relevant things
print(name, args, arg_str)
-- Return them too
-- You're free to return whatever you want. What you return here is displayed to the user in the console, however, so you might want to specially format this.
return { status = 'Success', name = name, args = args, arg_str = arg_str }
end,
'Usage: example_echo ...'
)
-- Returns the longest parameter. This function will not accept the simple mode (by prefixing the command with @).
console.add_command('get_longest_argument', function(name, args)
local str, length = nil, 0
for k, v in pairs(args) do
if #v > length then
str, length = v, #v
end
end
return { status = (str and 'Success' or 'Failure'), longest = str, length = length }
end, 'Usage: get_longest_argument ...')
-- Shows how to use SELECTED and USAGE
console.add_command('biggify', function(name, args)
-- Make sure that args[1] is the entity
if not radiant.check.is_entity(args[1]) then
-- If no entity was passed, check if there was a SELECTED one
if not radiant.check.is_entity(SELECTED) then
USAGE('No entity selected')
end
-- the SELECTED entity becomes args[1]
table.insert(args, 1, SELECTED)
end
-- Get the entity and the size
local ent, size = args[1], args[2]
-- Determine size by the argument given
if size == 'titan' then
size = 1
elseif size == 'tiny' then
size = 0.01
else
-- Throw an error, showing the usage of the function
USAGE('size must be either titan or tiny!')
end
ent:add_component('render_info'):set_scale(size)
end, 'Usage: biggify [entity] (titan|tiny)')
return {}