From cfec8113efa84ed14b90b3751b223b46cb615b05 Mon Sep 17 00:00:00 2001 From: viral32111 <19510403+viral32111@users.noreply.github.com> Date: Tue, 14 Jul 2020 11:13:32 +0100 Subject: [PATCH] Added a few restrictions 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. --- lua/autorun/client/cl_gravity_arrow.lua | 13 ++++++ lua/autorun/server/sv_gravity_arrow.lua | 62 ++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/lua/autorun/client/cl_gravity_arrow.lua b/lua/autorun/client/cl_gravity_arrow.lua index 19cf2e8..b9dfd73 100644 --- a/lua/autorun/client/cl_gravity_arrow.lua +++ b/lua/autorun/client/cl_gravity_arrow.lua @@ -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 ) diff --git a/lua/autorun/server/sv_gravity_arrow.lua b/lua/autorun/server/sv_gravity_arrow.lua index 1a25d19..5c64ff6 100644 --- a/lua/autorun/server/sv_gravity_arrow.lua +++ b/lua/autorun/server/sv_gravity_arrow.lua @@ -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 )