Created
August 31, 2020 09:55
-
-
Save BadgerCode/06d94b90327d6b92956a135d0c9689e8 to your computer and use it in GitHub Desktop.
Garry's Mod code snippet for finding out information about weapons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Commands: | |
-- weapons_list | |
-- Prints out a basic list of all weapons | |
-- weapon_info <classname> | |
-- Prints out detailed information about a specific weapon | |
function table.Select(tbl, f) | |
local t = {} | |
for k,v in pairs(tbl) do | |
t[k] = f(v) | |
end | |
return t | |
end | |
function table.FirstOrDefault(tbl, f) | |
for k,v in pairs(tbl) do | |
if(f(v)) then | |
return v | |
end | |
end | |
return nil | |
end | |
function table.Find(tbl, f) | |
local t = {} | |
for k,v in pairs(tbl) do | |
if(f(v)) then | |
table.insert(t, v) | |
end | |
end | |
return t | |
end | |
local weaponData = weapons.GetList() | |
concommand.Remove("weapons_list") | |
concommand.Add("weapons_list", function() | |
PrintTable(table.Select(weaponData, function(weapon) | |
return { | |
Author = weapon.Author, | |
Contact = weapon.Contact, | |
Base = weapon.Base, | |
ClassName = weapon.ClassName | |
} | |
end)) | |
end) | |
local function AutoComplete(cmd, stringargs) | |
local args = string.Split(stringargs, " ") | |
local suggestions = {} | |
local classname = args[2] | |
if #args == 2 then | |
local matches = table.Find(weaponData, function(weapon) return string.match(weapon.ClassName, classname) ~= nil end) | |
return table.Select(matches, function(weapon) return cmd .. " " .. weapon.ClassName end) | |
end | |
return suggestions | |
end | |
concommand.Remove("weapon_info") | |
concommand.Add("weapon_info", function(ply, cmd, args) | |
local classname = args[1] | |
if(classname == nil) then print("Expected weapon classname argument") return end | |
local weapon = table.FirstOrDefault(weaponData, function(weapon) | |
return weapon.ClassName == classname | |
end) | |
if weapon == nil then | |
print("No weapon found with classname " .. classname) | |
return | |
end | |
PrintTable(weapon) | |
end, AutoComplete) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment