Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions gamemode/command/class.sh.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
Command = Command or Class.New()

function Command:Init()
self.name = ""
self.cmds = {}
self.args = {}
self.flags = {}
self.callback = function() end
end

function Command:RemoveFlags(flags)
if istable(flags) then
for _, flag in ipairs(flags) do
self.flags[flag] = false
end
else
self.flags[flags] = false
end
end

function Command:AddFlags(flags)
if istable(flags) then
for _, flag in ipairs(flags) do
self.flags[flag] = true
end
else
self.flags[flags] = true
end
end

function Command:HasFlag(flag)
return self.flags[flag]
end

function Command:SetCommand(...)
self.cmds = table.concat({...}, " "):lower():Split(" ")
self.name = self.cmds[1]

for _, cmd in ipairs(CommandService.Commands) do
if cmd.name:sub(1, 1) ~= self.name:sub(1, 1) then
continue
end

return
end

table.insert(self.cmds, self.name:sub(1, 1))
end

function Command:SetCallback(args, callback)
self.args = args
self.callback = function(...)
local args = self.args
local command_args = {...}
local sender = table.remove(command_args, 1)
local player_table = false

if #command_args > 0 then
for k, arg in ipairs(args) do
if isnumber(arg) and isnumber(command_args[k - 1]) then
if isnumber(args[k + 1]) then
command_args[k - 1] = math.min(command_args[k - 1], args[k + 1])
table.remove(command_args, k + 1)
end

command_args[k - 1] = math.max(command_args[k - 1], arg)
table.remove(command_args, k)
continue
end

if command_args[k] then
if CommandService.symbols[command_args[k]] then
if table.HasValue(CommandService.symbols[command_args[k]].varargs, arg) then
command_args[k] = CommandService.symbols[command_args[k]].callback(sender)
end
end

command_args[k] = CommandService.object_types[arg](command_args[k])

if arg == "players" and istable(command_args[k]) then
player_table = {key = k, players = command_args[k]}
end
end
end

if player_table then
for k, ply in ipairs(player_table.players) do
local command_args_temp = table.Copy(command_args)

command_args_temp[player_table.key] = ply
callback(sender, unpack(command_args_temp))
end
return
end

callback(sender, unpack(command_args))
else
callback(sender)
end
end
end

function Command:Execute(sender, ...)
local cmd_hk

sender = (sender != NULL and sender or "server")
cmd_hk = hook.Call("OnCommand", GAMEMODE, sender, self, {...})

if cmd_hk then
return
end

self.callback(sender, ...)
end
13 changes: 13 additions & 0 deletions gamemode/command/command.cl.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function CommandService.CreateConCommand(cmd)
concommand.Add("emm_" .. cmd.name, function(sender, _, args)
if CLIENT then
RunConsoleCommand("net_emm_" .. cmd.name, unpack(args))
else
for _, ply in ipairs(player.GetAll()) do
concommand.Run(ply, "net_emm_" .. cmd.name, args)
end
end

cmd:Execute(sender, unpack(args))
end, CommandService.AutoComplete)
end
171 changes: 171 additions & 0 deletions gamemode/command/command.sh.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
CommandService = CommandService or {}
CommandService.Commands = {}


-- # Object Types

CommandService.object_types = {
bool = tobool,
string = tostring,
number = tonumber,
table = function(tbl) return CommandService.ToType("table", "string.Explode(',', '"..tbl.."')") end,
vector = function(vector) return CommandService.ToType("vector", "Vector(unpack(string.Explode(',', '"..vector.."')))") end,
angle = function(ang) return CommandService.ToType("angle", "Angle(unpack(string.Explode(',', '"..ang.."')))") end,
player = function(ply) return CommandService.FindTarget(ply)[1] end,
players = function(tbl)
local targets = CommandService.object_types.table(tbl)

if #targets > 1 then
return CommandService.FindTarget(targets)
else
return CommandService.FindTarget(targets)[1]
end
end,
}


-- # Symbols

CommandService.prefix = "!/"

CommandService.symbols = {
["^"] = {varargs = {"player", "players"}, callback = function(sender) return sender:Nick() end},
["*"] = {varargs = {"players"}, callback = function(sender)
local names = ""

for _, ply in pairs(player.GetAll()) do
names = names..","..ply:Nick()
end

return names:sub(2)
end}
}


-- # Utils

function CommandService.FindTarget(target)
local tbl = {}

if istable(target) then
for _, name in pairs(target) do
if not IsEntity(name) then
for _, ply in pairs(player.GetAll()) do
if string.lower(ply:Nick()):find(name:lower()) and not table.HasValue(tbl, ply) then
table.insert(tbl, ply)
break
end
end
else
table.insert(tbl, target)
end
end
else
if not IsEntity(target) then
for _, ply in pairs(player.GetAll()) do
if string.lower(ply:Nick()):find(target:lower()) then
table.insert(tbl, ply)
end
end
else
table.insert(tbl, target)
end
end

if #tbl > 0 then
return tbl
else
return {nil}
end
end

function CommandService.FindCommand(str)
local tbl = {}

str = str:lower()

for name, cmd in pairs(CommandService.Commands) do
if name:find(str) and table.HasValue(cmd.cmds, str) then
return cmd
end
end

return nil
end

function CommandService.ExecuteCommand(cmd, sender, ...)
cmd = CommandService.FindCommand(cmd)

if cmd then
cmd:Execute(sender, ...)
end
end


function CommandService.AutoComplete(cmd, args)
local auto_complete = {}
local con_cmd = cmd.." "

cmd = CommandService.Commands[cmd:sub(5)]

if istable(cmd) then
args = args:sub(2):Split(" ")

if cmd.args[#args] == "player" then
for _, ply in pairs(player.GetAll()) do
if string.lower(" "..ply:Nick()):find(args[#args]:lower()) then
table.insert(auto_complete, con_cmd..ply:Nick())
end
end

return auto_complete
elseif cmd.args[#args] == "number" then
auto_complete = {con_cmd..args[#args]}

if isnumber(cmd.args[#args + 1]) then
auto_complete = {con_cmd.."[".. cmd.args[#args + 1].."]"}

if isnumber(cmd.args[#args + 2]) then
auto_complete = {con_cmd.."[".. cmd.args[#args + 1].." - "..cmd.args[#args + 2].."]"}
end
end

return auto_complete
end
end
end

function CommandService.ToType(type, arg)
local type_pass, arg = CompileString("return is"..type.."("..arg.."), "..arg, "CommandService.TypeCheck")()

if type_pass then
return arg
else
return nil
end
end

function CommandService.AddCommand(props)
assert(istable(props), "Properties is not a table")
assert(props.name, "Command name not specified")

local cmd = Command.New()

if not props.callback then
props.callback = function() end
end

if not props.varargs then
props.varargs = {}
end

if props.flags then
cmd:AddFlags(props.flags)
end

assert(istable(props.varargs), "Varargs is not a table")
cmd:SetCommand(props.name)
cmd:SetCallback(props.varargs, props.callback)
CommandService.CreateConCommand(cmd)
CommandService.Commands[cmd.name] = cmd
end
5 changes: 5 additions & 0 deletions gamemode/command/command.sv.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function CommandService.CreateConCommand(cmd)
concommand.Add("net_emm_".. cmd.name, function(sender, _, args)
cmd:Execute(sender, unpack(args))
end, nil, nil, FCVAR_UNREGISTERED)
end
3 changes: 3 additions & 0 deletions gamemode/command/hooks.cl.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hook.Add("OnPlayerChat", "CommandSerive.OnPlayerChat", function(ply, text)
return hook.Call("OnChat", nil, ply, text)
end)
12 changes: 12 additions & 0 deletions gamemode/command/hooks.sh.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function CommandService.ChatCommand(ply, text)
if CommandService.prefix:find(text:sub(1, 1)) then
local args = text:sub(2):Split(" ")
local cmd = CommandService.FindCommand(table.remove(args, 1))

if cmd then
cmd:Execute(ply, unpack(args))
return true
end
end
end
hook.Add("OnChat", "CommandService.ChatCommand", CommandService.ChatCommand)
3 changes: 3 additions & 0 deletions gamemode/command/hooks.sv.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hook.Add("PlayerSay", "CommandSerive.PlayerSay", function(ply, text)
hook.Call("OnChat", nil, ply, text)
end)
9 changes: 7 additions & 2 deletions gamemode/core/gamemode.sh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ GM.Author = "arkade"
EMM.Include {
"settings/settings",

"util/class",

"command/command",
"command/class",
"command/hooks",

"util/variables",
"util/util",
"util/palette",
"util/class",
"util/net",
"util/cubic-bezier",
"util/anim-value",
"util/net",
"util/pred-sound",
"util/stamina",
"util/time-associated-map",
Expand Down
20 changes: 1 addition & 19 deletions gamemode/util/spectate.cl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,8 @@ SpectateService.unspectate_keys = bit.bor(IN_JUMP, IN_MOVELEFT, IN_MOVERIGHT, IN
SpectateService.buttons = 0


-- # Utils

function SpectateService.AutoComplete(cmd, args)
local tbl = {}

for _, v in pairs(player.GetAll()) do
if string.find(string.lower(" " .. v:Nick()), args:lower()) then
table.insert(tbl, "emm_spectate " .. v:Nick())
end
end

return tbl
end


-- # Spectating

concommand.Add("emm_spectate", function(ply, cmd, args)
RunConsoleCommand("sv_emm_spectate", unpack(args))
end, SpectateService.AutoComplete)
CommandService.AddCommand({name = "spectate", varargs = {"player"}})

function SpectateService.TargetKeyDown(key)
return bit.band(SpectateService.buttons, key)
Expand Down
Loading