Skip to content

Commit

Permalink
Added a few restrictions
Browse files Browse the repository at this point in the history
Only one arrow can be spawned on the map at a time. Players cannot spawn the arrow if there is more than one player online, but admins can.
  • Loading branch information
viral32111 committed Jul 14, 2020
1 parent 41e134a commit cfec811
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
13 changes: 13 additions & 0 deletions lua/autorun/client/cl_gravity_arrow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,16 @@ properties.Add( "gravityarrow", {
end

} )

net.Receive( "gravityArrowNotify", function( length )

-- Read the notification text
local message = net.ReadString()

-- Add the notification for 3 seconds
notification.AddLegacy( message, NOTIFY_ERROR, 3 )

-- Play a sound
surface.PlaySound("buttons/button10.wav")

end )
62 changes: 60 additions & 2 deletions lua/autorun/server/sv_gravity_arrow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,73 @@ along with this program. If not, see https://www.gnu.org/licenses.
resource.AddSingleFile( "materials/vgui/entities/gravity_arrow.vtf" )
resource.AddSingleFile( "materials/vgui/entities/gravity_arrow.vmt" )

-- Add the network string for enabling or disabling the arrow
util.AddNetworkString( "gravityArrowToggle" )
-- Add the network strings
util.AddNetworkString( "gravityArrowToggle" ) -- Enabling or disabling the arrow
util.AddNetworkString( "gravityArrowNotify" ) -- Sending notifiations to the client

-- Runs when the client requests that the arrow be enabled or disabled
net.Receive( "gravityArrowToggle", function( length, player )

-- Read the network variables
local entity = net.ReadEntity()
local state = net.ReadBool()

-- Update the arrow's network var
entity:SetEnabled( state )

end )

-- Runs when an entity is attempting to be spawned by a player
hook.Add( "PlayerSpawnSENT", "gravityArrowSpawn", function( ply, entityClass )

-- Prevent further execution if this isn't for the gravity arrow
if entityClass ~= "gravity_arrow" then return end

-- Is there at least one arrow already on the map?
if #ents.FindByClass( "gravity_arrow" ) > 0 then

-- Send them a notification
net.Start( "gravityArrowNotify" )
net.WriteString( "Only one arrow can be spawned on the map, please remove the existing one first." )
net.Send( ply )

-- Prevent them from spawning it
return false

end

-- Is there more than one player & are they not an admin?
if #player.GetHumans() > 1 and not ply:IsAdmin() then

-- Send them a notification
net.Start( "gravityArrowNotify" )
net.WriteString( "You cannot spawn this when there are multiple players online." )
net.Send( ply )

-- Prevent them from spawning it
return false

end

end )

-- Runs when a player initally spawns in the server
hook.Add( "PlayerInitialSpawn", "gravityArrowRemove", function( ply )

-- Is there now more than one player?
if #player.GetHumans() > 1 then

-- Find all the spawned gravity arrows
local arrows = ents.FindByClass( "gravity_arrow" )

-- Loop through them
for _, arrow in ipairs( arrows ) do

-- Remove the arrow
arrow:Remove()

end

end

end )

0 comments on commit cfec811

Please # to comment.