-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
212 lines (164 loc) · 6.67 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( "shared.lua" )
-- Chat command config
spawnPointCommands = {
["unlinkSpawnPoint"] = {
["!unlinkspawn"] = true,
["!unlinkspawnpoint"] = true
},
["unlinkThisSpawnPoint"] = {
["!unlinkthis"] = true
}
}
-- Helper Functions
function createPlayerList( players )
local playerList = {}
for _, ply in pairs( players ) do
playerList[ply] = true
end
return playerList
end
local function isFriendly( ply, otherPly )
if ply == otherPly then return true end
local friends = ply:CPPIGetFriends()
if friends == CPPI.CPPI_DEFER then return false end
return table.HasValue( friends, otherPly )
end
function linkPlayerToSpawnPoint( ply, spawnPoint )
if not IsValid( ply ) then return end
if not IsValid( spawnPoint ) then return end
if not isFriendly( spawnPoint:CPPIGetOwner(), ply ) then return end
ply.LinkedSpawnPoint = spawnPoint
spawnPoint.LinkedPlayers[ply] = "Linked"
return true
end
function unlinkPlayerFromSpawnPoint( ply, spawnPoint )
if not IsValid( ply ) then return end
if not IsValid( spawnPoint ) then return end
ply.LinkedSpawnPoint = nil
spawnPoint.LinkedPlayers[ply] = nil
end
function unlinkAllPlayersFromSpawnPoint( spawnPoint, excludedPlayers )
if not IsValid( spawnPoint ) then return end
local linkedPlayers = spawnPoint.LinkedPlayers
local spawnPointOwner = spawnPoint:CPPIGetOwner()
for ply, _ in pairs( linkedPlayers ) do
if IsValid( ply ) then
local playerIsNotExcluded = excludedPlayers[ply] == nil
local playerIsNotSpawnPointOwner = spawnPointOwner ~= ply
if playerIsNotExcluded and playerIsNotSpawnPointOwner then
unlinkPlayerFromSpawnPoint( ply, spawnPoint )
ply:PrintMessage( 4, "You've been unlinked from a Spawn Point!" )
end
end
end
end
-- Chat commands
function unlinkSpawnPointCommand( ply, txt, _, _ )
-- Removes whitepace from text
local text = string.lower( txt ):gsub( "%s+", "" )
local unlinkSpawnCommands = spawnPointCommands.unlinkSpawnPoint
if not unlinkSpawnCommands[text] then return end
local linkedSpawnPoint = ply.LinkedSpawnPoint
unlinkPlayerFromSpawnPoint( ply, linkedSpawnPoint )
ply:PrintMessage( 4, "Spawn Point unlinked" )
end
hook.Remove( "PlayerSay", "UnlinkSpawnPointCommand" )
hook.Add( "PlayerSay", "UnlinkSpawnPointCommand", unlinkSpawnPointCommand )
function unlinkThisSpawnPointCommand( ply, txt, _, _ )
local text = string.lower( txt ):gsub( "%s+", "" )
local unlinkThisSpawnCommands = spawnPointCommands.unlinkThisSpawnPoint
if not unlinkThisSpawnCommands[text] then return end
local targetedEntity = ply:GetEyeTraceNoCursor().Entity
if not ( targetedEntity and targetedEntity:IsValid() ) then return end
local isSpawnPoint = targetedEntity:GetClass() == "sent_spawnpoint"
if not isSpawnPoint then return ply:PrintMessage( 4, "You must be looking at a Spawn Point to use this command" ) end
local spawnPoint = targetedEntity
local spawnPointOwner = spawnPoint:CPPIGetOwner()
local playerOwnsSpawnPoint = spawnPointOwner == ply
local playerIsAdmin = ply:IsAdmin()
if not ( playerOwnsSpawnPoint or playerIsAdmin ) then return ply:PrintMessage( 4, "That's not yours! You can't unlink others from this Spawn Point" ) end
local excludedPlayers = createPlayerList( { spawnPointOwner } )
unlinkAllPlayersFromSpawnPoint( spawnPoint, excludedPlayers )
ply:PrintMessage( 4, "All players except the owner have been unlinked from this Spawn Point" )
end
hook.Remove( "PlayerSay", "UnlinkThisSpawnPointCommand" )
hook.Add( "PlayerSay", "UnlinkThisSpawnPointCommand", unlinkThisSpawnPointCommand )
local function unlinkPlayerOnDisconnect( ply )
local linkedSpawnPoint = ply.LinkedSpawnPoint
if not linkedSpawnPoint then return end
unlinkPlayerFromSpawnPoint( ply, linkedSpawnPoint )
end
hook.Remove( "PlayerDisconnected", "UnlinkPlayerOnDisconnect" )
hook.Add( "PlayerDisconnected", "UnlinkPlayerOnDisconnect", unlinkPlayerOnDisconnect )
-- Entity Methods
function ENT:SpawnFunction( _, tr )
if not tr.Hit then return end
local SpawnPos = tr.HitPos
local ent = ents.Create( "sent_spawnpoint" )
ent:SetPos( SpawnPos )
ent:Spawn()
ent:Activate()
return ent
end
function ENT:Initialize()
local effectdata1 = EffectData()
effectdata1:SetOrigin( self:GetPos() )
util.Effect( "spawnpoint_start", effectdata1, true, true )
self:SetModel( "models/props_combine/combine_mine01.mdl" )
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
self:SetUseType( SIMPLE_USE )
self.LinkedPlayers = {}
local phys = self:GetPhysicsObject()
if not phys:IsValid() then return end
phys:Wake()
phys:EnableDrag( true )
phys:EnableMotion( false )
end
function ENT:OnRemove()
local effectdata1 = EffectData()
effectdata1:SetOrigin( self:GetPos() )
util.Effect( "spawnpoint_start", effectdata1, true, true )
unlinkAllPlayersFromSpawnPoint( self, {} )
end
function ENT:Use( ply )
local playerLinkedToSpawnPoint = ply.LinkedSpawnPoint == self
if playerLinkedToSpawnPoint then
unlinkPlayerFromSpawnPoint( ply, self )
ply:PrintMessage( 4, "Spawn Point unlinked" )
else
local success = linkPlayerToSpawnPoint( ply, self )
if success then
ply:PrintMessage( 4, "Spawn Point set. Say !unlinkspawn to unlink" )
else
ply:PrintMessage( 4, "Unable to set spawnpoint. You are not in the friends or in same faction with the owner.")
end
end
end
local heightOfSpawnPointPlusOne = 16
local function SpawnPointHook( ply )
local spawnPoint = ply.LinkedSpawnPoint
if not spawnPoint or not spawnPoint:IsValid() then return end
if not spawnPoint:IsInWorld() then
ply:ChatPrint( "Your linked spawn point is in an invalid location" )
return
end
local spawnPos = spawnPoint:GetPos() + Vector( 0, 0, heightOfSpawnPointPlusOne )
ply:SetPos( spawnPos )
end
hook.Remove( "PlayerSpawn", "SpawnPointHook" )
hook.Add( "PlayerSpawn", "SpawnPointHook", SpawnPointHook )
local function unlinkSpawnpointWhenEnteringPvp( ply )
if not IsValid( ply.LinkedSpawnPoint ) then return end
local linkedSpawnPoint = ply.LinkedSpawnPoint
unlinkPlayerFromSpawnPoint( ply, linkedSpawnPoint )
ply:ChatPrint( "You've been unlinked from a Spawn Point, because you entered PvP!" )
end
-- Stubs from here on
function ENT:Think() end
function ENT:OnTakeDamage() end
function ENT:PhysicsUpdate() end
function ENT:PhysicsCollide() end