Ulysses Stuff > Releases for ULib v1.*

USwep Beta

(1/4) > >>

spbogie:
USwep
By: spbogie

Current Version: Beta 1.01

Description:
USwep is an enhanced SWEP menu designed to use ULib's UCL to allow multiple levels of access.

Features:

* Multiple Access groups
* SubFolder support
* Wildcard folder inclusion filters
* Spawn sweps, or give directly to self or others
Requirements:
ULib v1.2

Code:

--- Code: -----USwep beta 1.01
--by Spbogie

--Requires ULib v1.2 or higher.
assert( _file.Exists( "lua/ULib/init.lua" ), "USwep needs ULib to run!" )
_OpenScript( "ULib/init.lua" )
assert( ULib.ULIB_VERSION >= 1.2, "USwep requires ULib version 1.2 or higher to run!" )

USwep = {}
------------
-- CONFIG --
------------
-- Groups list. Groups take priority by order in list.
USwep.Groups =
{
{
name = "Admin", --Name of the group.
folders = {"*"}, --Folders to give access to. Use * for all folders/sub-folders.
exfolders = {"build"}, --Optional. Exclude folders in this list (NO wildcards allowed)
spawn = true, --Allow this group to spawn SWEPs on the ground.
give = true, --Allow this group to give SWEPs to any player.
giveself = true, --Allow this group to give SWEPs to themselves.
access = ACCESS_BAN --Access required to be in this group
},
{
name = "SubAdmin",
folders = {"Counter-Strike", "gm_laserdance"},
spawn = false,
give = true,
giveself = true,
access = ACCESS_KICK
},
{
name = "Public",
folders = {"Construction"},
spawn = false,
give = false,
giveself = true,
access = ACCESS_ALL
}
}
USwep.recurseLevel = 2 --Number of sub-folder levels to explore
USwep.ucl = ULib.mainUcl --UCL to use (Only change if you use a custom UCL)

----------
-- MAIN --
----------

function USwep.getGroup(userid)
for i=1, table.getn(USwep.Groups) do
if USwep.ucl:query(userid, USwep.Groups[i].access) then
return i
end
end
end

function USwep.getFolders(dir, level)
dir = dir or "lua/weapons"
level = level or -1
if level == USwep.recurseLevel then return {} end
local list = {}
local files = _file.Find(dir .. "/*") or {}
table.remove(files, ULib.findInTable(files, "."))
table.remove(files, ULib.findInTable(files, ".."))
for _,v in ipairs(files) do
if _file.IsDir(dir .. "/" .. v) then
table.insert(list, dir .. "/" .. v)
list = ULib.mergeTable(list, USwep.getFolders(dir .. "/" .. v, level + 1))
end
end
return list
end

function USwep.getFiles(dir)
local list = _file.Find(dir .. "/*.lua")
local ret = {}
for _,v in ipairs(list) do
if not _file.IsDir(dir .. "/" .. v) then
table.insert(ret, string.sub(dir .. "/" .. v, 5))
end
end
return ret
end

function USwep.checkFolder(group, dir)
for i=1, table.getn(USwep.Groups[group].folders) do
if string.find(dir, "lua/weapons/" .. string.gsub(ULib.makePatternSafe(USwep.Groups[group].folders[i]),"%%%*","[^/]+")) then
if USwep.Groups[group].exfolders ~= nil then
for j=1, table.getn(USwep.Groups[group].exfolders) do
if string.find(dir, "lua/weapons/" .. ULib.makePatternSafe(USwep.Groups[group].exfolders[1])) then
return false
end
end
end
return true
end
end
return false
end

function USwep.getList(group)
local sList = {}
for _,v in USwep.getFolders() do
if USwep.checkFolder(group, v) then
local files = USwep.getFiles(v)
if files ~= {} then
table.insert(sList, {string.gsub(string.sub(v, 13), "[^/]+/", "  "), files})
end
end
end
return sList
end

--------------
-- COMMANDS --
--------------
function USwep.cc_giveSWEP(userid, args, argv, argc)
if argc < 1 then
ULib.tsay( userid, ulx.LOW_ARGS )
return
end
if not _file.Exists("lua/"..argv[1]) then
ULib.tsay(userid, "Invalid SWEP specified")
return
end
local group = USwep.getGroup(userid)
if group == nil or not USwep.Groups[group].give then
ULib.tsay(userid, "You do not have access to this command, " .. _PlayerInfo( userid, "name" ) .. "." )
return
end
if not USwep.checkFolder(group, "lua/" .. argv[1]) then
ULib.tsay(userid, "You do not have access to this SWEP, " .. _PlayerInfo( userid, "name" ) .. "." )
return
end

if argc == 1 then
local f = function (userid, target)
if target == ULib.ID_EXIT then return
elseif target == ULib.ID_ALLPLAYERS then
for i=1,_MaxPlayers() do
if _PlayerInfo(i,"alive") then
_PlayerGiveSWEP(i, argv[1])
end
end
else
_PlayerGiveSWEP(target, argv[1])
end
end

local menu = ULib.Menu:new("Choose a Player", f, USwep.menu_skin)
menu:addPlayerOptions(true)
menu:showMenu(userid)
else
targets, err = ULib.getUsers( argv[ 2 ], true, true, USwep.ucl, userid ) -- Enable keywords, ignore immunity
if not targets then
ULib.tsay( userid, err )
return
end

for _,v in ipairs(targets) do
_PlayerGiveSWEP(v, argv[1])
end
end
end
ULib.CONCOMMAND("giveSWEP", USwep.cc_giveSWEP, "Give a player a SWEP.")

function USwep.cc_giveSWEPself(userid, args, argv, argc)
if argc < 1 then
ULib.tsay( userid, ulx.LOW_ARGS )
return
end
if not _file.Exists("lua/"..argv[1]) then
ULib.tsay(userid, "Invalid SWEP specified")
return
end
local group = USwep.getGroup(userid)
if group == nil or not USwep.Groups[group].giveself then
ULib.tsay(userid, "You do not have access to this command, " .. _PlayerInfo( userid, "name" ) .. "." )
return
end
if not USwep.checkFolder(group, "lua/" .. argv[1]) then
ULib.tsay(userid, "You do not have access to this SWEP, " .. _PlayerInfo( userid, "name" ) .. "." )
return
end

_PlayerGiveSWEP(userid, argv[1])
end
ULib.CONCOMMAND("giveSWEPself", USwep.cc_giveSWEPself, "Give a SWEP to yourself.")

function USwep.cc_spawnSWEP(userid, args, argv, argc)
if argc < 1 then
ULib.tsay( userid, ulx.LOW_ARGS )
return
end
if not _file.Exists("lua/"..argv[1]) then
ULib.tsay(userid, "Invalid SWEP specified")
return
end
local group = USwep.getGroup(userid)
if group == nil or not USwep.Groups[group].spawn then
ULib.tsay(userid, "You do not have access to this command, " .. _PlayerInfo( userid, "name" ) .. "." )
return
end
if not USwep.checkFolder(group, "lua/" .. argv[1]) then
ULib.tsay(userid, "You do not have access to this SWEP, " .. _PlayerInfo( userid, "name" ) .. "." )
return
end

PlayerLookTrace(userid, 200)
if not _TraceHit() then return end
local spawnPos = _TraceEndPos();
local normal = _TraceGetSurfaceNormal();
spawnPos = vecAdd( spawnPos, vecMul( normal, 50 ) )
local ent = _EntCreate("weapon_swep")
_EntSetPos(ent, spawnPos)
_EntSetKeyValue(ent, "Script", argv[1])
_EntSpawn(ent)
_util.DropToFloor(ent)
end
ULib.CONCOMMAND("spawnSWEP", USwep.cc_spawnSWEP, "Spawn a SWEP.")

----------
-- MENU --
----------
function USwep.sendMenu(userid)
local group = USwep.getGroup(userid)
local category = "USwep (" .. USwep.Groups[group].name .. ")"
for _,v in ipairs(USwep.getList(group)) do
_spawnmenu.AddItem(userid, category, "@" .. v[1])
for _,f in ipairs(v[2]) do
if USwep.Groups[group].giveself then
_spawnmenu.AddItem(userid, category, "+" .. string.sub(string.gsub(f, "[^/]+/", ""), 1, -5), "giveSWEPself \"" .. f .. "\"")
end
if USwep.Groups[group].spawn then
_spawnmenu.AddItem(userid, category, "+S " .. string.sub(string.gsub(f, "[^/]+/", ""), 1, -5), "spawnSWEP \"" .. f .. "\"")
end
if USwep.Groups[group].give then
_spawnmenu.AddItem(userid, category, "+G " .. string.sub(string.gsub(f, "[^/]+/", ""),1 ,-5), "giveSWEP \"" .. f .. "\"")
end
end
end
end
USwep.ucl:addAccessCallback(USwep.sendMenu)
--- End code ---

This is currently in beta, and as such I am looking for comments/suggestions. Please don't hesitate to post your ideas.

JamminR:
I understand its beta now, but having the config in a separate file in the future would be great. This way, the general folks of the world don't have to update the configs everytime a code base is released and overwrites the .lua file. :)

Golden-Death:
Just a tip about a section you missed: What is it?



(I'm too lazy to check it myself :))

JamminR:
HAHA.
True, I knew what it was from previous conversations.

GD, others, USwep allows Ulib to control access to who has what SWEPS on your server.
Think Eswep, but written totally for ULib. :)

Golden-Death:
Ah, i see.

Navigation

[0] Message Index

[#] Next page

Go to full version