diff --git a/computers/commands.lua b/computers/commands.lua new file mode 100644 index 0000000..cab340a --- /dev/null +++ b/computers/commands.lua @@ -0,0 +1,16 @@ +computers.registered_commands = {} + +function computers.register_command(modpath) + local func = dofile(modpath .. "/init.lua") + local f = io.open(modpath .. "/conf.json") + local conf = minetest.parse_json(f:read("*all")) + f:close() + if func and computers.os.version >= conf.engine then computers.registered_commands[conf.name] = func end +end + +--load default commands +local dirs = minetest.get_dir_list(computers.modpath .. "/commands", true) + +for _, dir in pairs(dirs) do + computers.register_command(computers.modpath .. "/commands/" .. dir) +end \ No newline at end of file diff --git a/computers/commands/echo/conf.json b/computers/commands/echo/conf.json new file mode 100644 index 0000000..f7d1230 --- /dev/null +++ b/computers/commands/echo/conf.json @@ -0,0 +1,6 @@ +{ + "name": "echo", + "license": "MIT", + "version": 0.1, + "engine": 0.3 +} \ No newline at end of file diff --git a/computers/commands/echo/init.lua b/computers/commands/echo/init.lua new file mode 100644 index 0000000..bfbc32e --- /dev/null +++ b/computers/commands/echo/init.lua @@ -0,0 +1,5 @@ +function echo(input) + return input +end + +return echo \ No newline at end of file diff --git a/computers/commands/test/conf.json b/computers/commands/test/conf.json new file mode 100644 index 0000000..f7bf272 --- /dev/null +++ b/computers/commands/test/conf.json @@ -0,0 +1,6 @@ +{ + "name": "test", + "license": "MIT", + "version": 0.1, + "engine": 100000000 +} \ No newline at end of file diff --git a/computers/commands/test/init.lua b/computers/commands/test/init.lua new file mode 100644 index 0000000..6ad0416 --- /dev/null +++ b/computers/commands/test/init.lua @@ -0,0 +1,6 @@ +function test() + --minetest.chat_send_all("test") + return 0 +end + +return test \ No newline at end of file diff --git a/computers/gui.lua b/computers/gui.lua index fd82094..4134fc3 100644 --- a/computers/gui.lua +++ b/computers/gui.lua @@ -122,16 +122,32 @@ function computers.load_gui(pos, node, clicker) border = false, }, on_event = function(form, player, element, value) - minetest.chat_send_all("value: " .. value) local cindex = futil.get_index_by_name(form, "terminal_ctn") local eindex = futil.get_index_by_name(form[cindex], "terminal_output") local text = form[cindex][eindex].default - form[cindex][eindex].default = text .. "user:~$ " .. value .. "\n" + --form[cindex][eindex].default = text .. "user:~$ " .. value .. "\n" if value == "clear" then form[cindex][eindex].default = "user:~$\n" end + local cdata = value:split(" ", false, 1) + + if computers.registered_commands[cdata[1]] then + form[cindex][eindex].default = text .. "user:~$ " .. cdata[1] .. "\n" + text = form[cindex][eindex].default + local output = computers.registered_commands[cdata[1]](cdata[2]) + if output and output ~= "" and type(output) == "string" then + form[cindex][eindex].default = text .. output .. "\n" + end + elseif value == "clear" then + form[cindex][eindex].default = "user:~$\n" + elseif value == "" then + form[cindex][eindex].default = text .. "user:~$\n" + else + form[cindex][eindex].default = text .. "user:~$ " .. value .. "\nERROR: command not found\n" + end + return form end, }, diff --git a/computers/init.lua b/computers/init.lua index a8b2e74..29b072f 100644 --- a/computers/init.lua +++ b/computers/init.lua @@ -1,7 +1,16 @@ computers = {} computers.modpath = minetest.get_modpath("computers") +computers.os = { + version = 0.3, + name = "kuto", + authors = {"wsor", "luk3yx"}, + license = "MIT", +} + dofile(computers.modpath .. "/old_stuff/init.lua") + dofile(computers.modpath .. "/formspec.lua") +dofile(computers.modpath .. "/commands.lua") dofile(computers.modpath .. "/gui.lua") dofile(computers.modpath .. "/demo.lua")