ULX

Author Topic: Restricting Items To A Group.  (Read 1970 times)

0 Members and 2 Guests are viewing this topic.

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
Restricting Items To A Group.
« on: August 07, 2013, 04:33:58 PM »
Code: [Select]
AddCSLuaFile()

local PANEL = {}

local matOverlay_Normal = Material( "gui/ContentIcon-normal.png" )
local matOverlay_Hovered = Material( "gui/ContentIcon-hovered.png" )
local matOverlay_AdminOnly = Material( "icon16/shield.png" )

AccessorFunc( PANEL, "m_Color", "Color" )
AccessorFunc( PANEL, "m_Type", "ContentType" )
AccessorFunc( PANEL, "m_SpawnName", "SpawnName" )
AccessorFunc( PANEL, "m_NPCWeapon", "NPCWeapon" )

--[[---------------------------------------------------------
   Name: Paint
-----------------------------------------------------------]]
function PANEL:Init()

self:SetDrawBackground( false )
self:SetSize( 128, 128 )
self:SetText( "" )
self:SetDoubleClickingEnabled( false )

self.Image = self:Add( "DImage" )
self.Image:SetPos( 3, 3 )
self.Image:SetSize( 128 - 6, 128 - 6 )
self.Image:SetVisible( false )

self.Label = self:Add( "DLabel" )
self.Label:Dock( BOTTOM )
self.Label:SetContentAlignment( 2 )
self.Label:DockMargin( 4, 0, 4, 10 )
self.Label:SetTextColor( Color( 255, 255, 255, 255 ) )
self.Label:SetExpensiveShadow( 1, Color( 0, 0, 0, 200 ) )

self.Border = 0

end

function PANEL:SetName( name )

self:SetTooltip( name )
self.Label:SetText( name )
self.m_NiceName = name;


end

function PANEL:SetMaterial( name )

self.m_MaterialName = name;

local mat = Material( name )

-- Look for the old style material
if ( !mat || mat:IsError() ) then

name = name:Replace( "entities/", "VGUI/entities/" )
name = name:Replace( ".png", "" )
mat = Material( name );

end

-- Couldn't find any material.. just return
if ( !mat || mat:IsError() ) then
return
end

self.Image:SetMaterial( mat );

end

function PANEL:SetAdminOnly( b )
self.AdminOnly = b
end

function PANEL:DoRightClick()

local pCanvas = self:GetSelectionCanvas()
if ( IsValid( pCanvas ) && pCanvas:NumSelectedChildren() > 0 ) then
return hook.Run( "SpawnlistOpenGenericMenu", pCanvas )
end

self:OpenMenu()
end

function PANEL:DoClick()
end

function PANEL:OpenMenu()
end

function PANEL:OnDepressionChanged( b )

end

function PANEL:Paint( w, h )

if ( self.Depressed && !self.Dragging ) then
if ( self.Border != 8 ) then
self.Border = 8
self:OnDepressionChanged( true )
end
else
if ( self.Border != 0 ) then
self.Border = 0
self:OnDepressionChanged( false )
end
end

self.Image:PaintAt( 3 + self.Border, 3 + self.Border, 128-8-self.Border*2, 128-8-self.Border*2 )

surface.SetDrawColor( 255, 255, 255, 255 )

if ( !dragndrop.IsDragging() && (self:IsHovered() || self:IsChildHovered( 2 )) ) then

surface.SetMaterial( matOverlay_Hovered )
self.Label:Hide()

else

surface.SetMaterial( matOverlay_Normal )
self.Label:Show()

end

surface.DrawTexturedRect( self.Border, self.Border, w-self.Border*2, h-self.Border*2 )

if ( self.AdminOnly ) then
surface.SetMaterial( matOverlay_AdminOnly )
surface.DrawTexturedRect( self.Border + 8, self.Border + 8, 16, 16 )
end
end

function PANEL:PaintOver( w, h )

self:DrawSelections()

end

function PANEL:ToTable( bigtable )

local tab = {}

tab.type = self:GetContentType();
tab.nicename = self.m_NiceName;
tab.material = self.m_MaterialName;
tab.admin = self.AdminOnly;
tab.spawnname = self:GetSpawnName();
tab.weapon = self:GetNPCWeapon();

table.insert( bigtable, tab )

end

function PANEL:Copy()

local copy = vgui.Create( "ContentIcon", self:GetParent() )

copy:SetContentType( self:GetContentType() )
copy:SetSpawnName( self:GetSpawnName() )
copy:SetName( self.m_NiceName )
copy:SetMaterial( self.m_MaterialName )
copy:SetNPCWeapon( self:GetNPCWeapon() )
copy:SetAdminOnly( self.AdminOnly )
copy:CopyBase( self )
copy.DoClick = self.DoClick
copy.OpenMenu = self.OpenMenu

return copy;

end

vgui.Register( "ContentIcon", PANEL, "DButton" )


spawnmenu.AddContentType( "entity", function( container, obj )

if ( !obj.material ) then return end
if ( !obj.nicename ) then return end
if ( !obj.spawnname ) then return end

local icon = vgui.Create( "ContentIcon", container )
icon:SetContentType( "entity" )
icon:SetSpawnName( obj.spawnname )
icon:SetName( obj.nicename )
icon:SetMaterial( obj.material )
icon:SetAdminOnly( obj.admin )
icon:SetColor( Color( 205, 92, 92, 255 ) )
icon.DoClick = function()
RunConsoleCommand( "gm_spawnsent", obj.spawnname );
surface.PlaySound( "ui/buttonclickrelease.wav" )
end
icon.OpenMenu = function( icon )

local menu = DermaMenu()

local SubMenu = menu:AddSubMenu( "Restrict" ) -- Add a submenu
for k,_ in pairs(ULib.ucl.groups) do
SubMenu:AddOption( tostring(k), function() RunConsoleCommand( "ulx", "restrict", tostring(k), obj.spawnname ); end ):SetIcon( "icon16/group.png" )
end
menu:AddSpacer()

menu:AddOption( "Copy to Clipboard", function() SetClipboardText( obj.spawnname ) end )
menu:AddOption( "Spawn Using Toolgun", function() RunConsoleCommand( "gmod_tool", "creator" ); RunConsoleCommand( "creator_type", "0" ); RunConsoleCommand( "creator_name", obj.spawnname ) end )
menu:AddSpacer()
menu:AddOption( "Delete", function() icon:Remove(); hook.Run( "SpawnlistContentChanged", icon ) end )
menu:Open()

end

if ( IsValid( container ) ) then
container:Add( icon )
end

return icon;

end )

spawnmenu.AddContentType( "vehicle", function( container, obj )

if ( !obj.material ) then return end
if ( !obj.nicename ) then return end
if ( !obj.spawnname ) then return end

local icon = vgui.Create( "ContentIcon", container )
icon:SetContentType( "vehicle" )
icon:SetSpawnName( obj.spawnname )
icon:SetName( obj.nicename )
icon:SetMaterial( obj.material )
icon:SetAdminOnly( obj.admin )
icon:SetColor( Color( 0, 0, 0, 255 ) )
icon.DoClick = function()
RunConsoleCommand( "gm_spawnvehicle", obj.spawnname );
surface.PlaySound( "ui/buttonclickrelease.wav" )
end
icon.OpenMenu = function( icon )

local menu = DermaMenu()

local SubMenu = menu:AddSubMenu( "Restrict" ) -- Add a submenu
for k,_ in pairs(ULib.ucl.groups) do
SubMenu:AddOption( tostring(k), function() RunConsoleCommand( "ulx", "restrict", tostring(k), obj.spawnname ); end ):SetIcon( "icon16/group.png" )
end
menu:AddSpacer()

menu:AddOption( "Copy to Clipboard", function() SetClipboardText( obj.spawnname ) end )
menu:AddOption( "Spawn Using Toolgun", function() RunConsoleCommand( "gmod_tool", "creator" ); RunConsoleCommand( "creator_type", "1" ); RunConsoleCommand( "creator_name", obj.spawnname ) end )
menu:AddSpacer()
menu:AddOption( "Delete", function() icon:Remove(); hook.Run( "SpawnlistContentChanged", icon ) end )
menu:Open()

end

if ( IsValid( container ) ) then
container:Add( icon )
end

return icon;

end )

gmod_npcweapon = CreateConVar( "gmod_npcweapon", "", { FCVAR_ARCHIVE } );

spawnmenu.AddContentType( "npc", function( container, obj )

if ( !obj.material ) then return end
if ( !obj.nicename ) then return end
if ( !obj.spawnname ) then return end

if ( !obj.weapon ) then obj.weapon = gmod_npcweapon:GetString() end

local icon = vgui.Create( "ContentIcon", container )
icon:SetContentType( "npc" )
icon:SetSpawnName( obj.spawnname )
icon:SetName( obj.nicename )
icon:SetMaterial( obj.material )
icon:SetAdminOnly( obj.admin )
icon:SetNPCWeapon( obj.weapon )
icon:SetColor( Color( 244, 164, 96, 255 ) )

icon.DoClick = function()

local weapon = obj.weapon;
if ( gmod_npcweapon:GetString() != "" ) then weapon = gmod_npcweapon:GetString(); end

RunConsoleCommand( "gmod_spawnnpc", obj.spawnname, weapon );
surface.PlaySound( "ui/buttonclickrelease.wav" )
end

icon.OpenMenu = function( icon )

local menu = DermaMenu()

local SubMenu = menu:AddSubMenu( "Restrict" ) -- Add a submenu
for k,_ in pairs(ULib.ucl.groups) do
SubMenu:AddOption( tostring(k), function() RunConsoleCommand( "ulx", "restrict", tostring(k), obj.spawnname ); end ):SetIcon( "icon16/group.png" )
end
menu:AddSpacer()

local weapon = obj.weapon;
if ( gmod_npcweapon:GetString() != "" ) then weapon = gmod_npcweapon:GetString(); end

menu:AddOption( "Copy to Clipboard", function() SetClipboardText( obj.spawnname ) end )
menu:AddOption( "Spawn Using Toolgun", function() RunConsoleCommand( "gmod_tool", "creator" ); RunConsoleCommand( "creator_type", "2" ); RunConsoleCommand( "creator_name", obj.spawnname ); RunConsoleCommand( "creator_arg", weapon ); end )
menu:AddSpacer()
menu:AddOption( "Delete", function() icon:Remove(); hook.Run( "SpawnlistContentChanged", icon ) end )
menu:Open()

end



if ( IsValid( container ) ) then
container:Add( icon )
end

return icon;

end )

spawnmenu.AddContentType( "weapon", function( container, obj )

if ( !obj.material ) then return end
if ( !obj.nicename ) then return end
if ( !obj.spawnname ) then return end

local icon = vgui.Create( "ContentIcon", container )
icon:SetContentType( "weapon" )
icon:SetSpawnName( obj.spawnname )
icon:SetName( obj.nicename )
icon:SetMaterial( obj.material )
icon:SetAdminOnly( obj.admin )
icon:SetColor( Color( 135, 206, 250, 255 ) )
icon.DoClick = function()

RunConsoleCommand( "gm_giveswep", obj.spawnname );
surface.PlaySound( "ui/buttonclickrelease.wav" )

end

icon.DoMiddleClick = function()

RunConsoleCommand( "gm_spawnswep", obj.spawnname );
surface.PlaySound( "ui/buttonclickrelease.wav" )

end


icon.OpenMenu = function( icon )

local menu = DermaMenu()

local SubMenu = menu:AddSubMenu( "Restrict" ) -- Add a submenu
for k,_ in pairs(ULib.ucl.groups) do
SubMenu:AddOption( tostring(k), function() RunConsoleCommand( "ulx", "restrict", tostring(k), obj.spawnname ); end ):SetIcon( "icon16/group.png" )
end
menu:AddSpacer()

menu:AddOption( "Copy to Clipboard", function() SetClipboardText( obj.spawnname ) end )
menu:AddOption( "Spawn Using Toolgun", function() RunConsoleCommand( "gmod_tool", "creator" ); RunConsoleCommand( "creator_type", "3" ); RunConsoleCommand( "creator_name", obj.spawnname ) end )
menu:AddSpacer()
menu:AddOption( "Delete", function() icon:Remove(); hook.Run( "SpawnlistContentChanged", icon ) end )
menu:Open()

end


if ( IsValid( container ) ) then
container:Add( icon )
end

return icon;

end )
Code: [Select]
function UCanSpawnAllowedCheck( ply, ent, istool )
local usergroup = ply:GetNetworkedVar("UserGroup") or ply:GetUserGroup() or "user"
local groupTable = ULib.ucl.groups[usergroup].restrict
if( !istool ) then
if( table.HasValue(groupTable, ent:GetClass()) or table.HasValue(groupTable, ent:GetModel()) or table.HasValue(groupTable, ent:GetType()) ) then
return false
end
else
local tool = ent
if( table.HasValue(groupTable, tool) ) then
return false
end
end
end

hook.Add("PlayerSpawnEffect", "UCanPlayerSpawnEffect", function(ply, ent) UCanSpawnAllowedCheck(ply, ent, false) end)
hook.Add("PlayerSpawnNPC", "UCanPlayerSpawnNPC", function(ply, ent) UCanSpawnAllowedCheck(ply, ent, false) end)
hook.Add("PlayerSpawnObject", "UCanPlayerSpawnObject", function(ply, ent) UCanSpawnAllowedCheck(ply, ent, false) end)
hook.Add("PlayerSpawnProp", "UCanPlayerSpawnProp", function(ply, ent) UCanSpawnAllowedCheck(ply, ent, false) end)
hook.Add("PlayerSpawnRagdoll", "UCanPlayerSpawnRagdoll", function(ply, ent) UCanSpawnAllowedCheck(ply, ent, false) end)
hook.Add("PlayerSpawnSENT", "UCanPlayerSpawnSENT", function(ply, ent) UCanSpawnAllowedCheck(ply, ent, false) end)
hook.Add("PlayerSpawnSWEP", "UCanPlayerSpawnSWEP", function(ply, ent) UCanSpawnAllowedCheck(ply, ent, false) end)
hook.Add("PlayerSpawnVehicle", "UCanPlayerSpawnVehicle", function(ply, ent) UCanSpawnAllowedCheck(ply, ent, false) end)
hook.Add("CanTool", "UCanCanTool", function(ply, _, tool) UCanSpawnAllowedCheck(ply, tool, true) end)

function ulx.restrict( calling_ply, target, command )
if( ULib.ucl.groups[target] ~= nil ) then
if( ULib.ucl.groups[target].restrict ~= nil and type(ULib.ucl.groups[target].restrict) == "table" ) then
table.insert( ULib.ucl.groups[target].restrict, command )
else
ULib.ucl.groups[target].restrict = { command }
end
else
ULib.tsayError( calling_ply, "Error: Group Not Found" )
return false
end

ulx.fancyLogAdmin( calling_ply, "#A restricted #s from #T", command, target )
end
local restrict = ulx.command( "Utility", "ulx restrict", ulx.restrict, "!restrict" )
restrict:addParam{ type=ULib.cmds.StringArg, completes=ulx.group_names_no_user, hint="group", error="invalid group \"%s\" specified", ULib.cmds.restrictToCompletes }
restrict:addParam{ type=ULib.cmds.StringArg, hint="command", ULib.cmds.takeRestOfLine }
restrict:defaultAccess( ULib.ACCESS_SUPERADMIN )
restrict:help( "Restrict something from a target(s)." )

I was wondering if any one had any suggestions on what I can do with the above code, I know overrides are bad but I could not think of a way of doing what I wanted to do without overriding.

Any suggestions/code fixes would be appreciated. (This is untested.)

---
Also on a some how slightly related topic does URS still work?
« Last Edit: August 07, 2013, 05:02:47 PM by HeLLFox_15 »
I cry every time I see that I am not a respected member of this community.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Restricting Items To A Group.
« Reply #1 on: August 09, 2013, 07:34:56 AM »
It's not in the documentation (though it should be), but I'd recommend calling ULib.ucl.saveGroups() after modifying the groups table. Your use of hooks is good, it is what the hooks are intended for. You may want to give the user feedback on why they can't spawn certain things, though.
Experiencing God's grace one day at a time.