forked from Project-Sloth/ps-housing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcl_property.lua
767 lines (604 loc) · 20.9 KB
/
cl_property.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
Property = {
property_id = nil,
propertyData = nil,
shell = nil,
shellData = nil,
inProperty = false,
shellObj = nil,
has_access = false,
owner = false,
storageTarget = nil,
clothingTarget = nil,
furnitureObjs = {},
garageZone = nil,
doorbellPool = {},
entranceTarget = nil, -- needed for ox target
exitTarget = nil, -- needed for ox target
blip = nil,
}
Property.__index = Property
function Property:new(propertyData)
local self = setmetatable({}, Property)
self.property_id = tostring(propertyData.property_id)
-- Remove furnitures from property data for memory purposes
propertyData.furnitures = {}
self.propertyData = propertyData
local citizenid = PlayerData.citizenid
self.owner = propertyData.owner == citizenid
self.has_access = lib.table.contains(self.propertyData.has_access, citizenid)
if propertyData.apartment then
local aptName = propertyData.apartment
local apartment = ApartmentsTable[aptName]
if not apartment and Config.Apartments[aptName] then
ApartmentsTable[aptName] = Apartment:new(Config.Apartments[aptName])
apartment = ApartmentsTable[aptName]
elseif not apartment then
Debug(aptName .. " not found in Config")
return
end
apartment:AddProperty(self.property_id)
else
self:RegisterPropertyEntrance()
self:RegisterGarageZone()
end
return self
end
function Property:GetDoorCoords()
local coords = nil
local dataApartment = self.propertyData.apartment
if dataApartment then
local apartment = dataApartment
coords = Config.Apartments[apartment].door
else
coords = self.propertyData.door_data
end
return coords
end
function Property:CreateShell()
local coords = self:GetDoorCoords()
coords = vec3(coords.x, coords.y, coords.z - 25.0)
self.shell = Shell:CreatePropertyShell(self.propertyData.shell, coords)
self.shellObj = self.shell.entity
local doorOffset = self.shellData.doorOffset
local offset = GetOffsetFromEntityInWorldCoords(self.shellObj, doorOffset.x, doorOffset.y, doorOffset.z)
self:RegisterDoorZone(offset)
SetEntityCoordsNoOffset(cache.ped, offset.x, offset.y, offset.z, false, false, true)
SetEntityHeading(cache.ped, self.shellData.doorOffset.h)
end
function Property:RegisterDoorZone(offset)
local function leave()
self:LeaveShell()
end
local function checkDoor()
self:OpenDoorbellMenu()
end
local coords = offset
local size = vector3(1.0, self.shellData.doorOffset.width, 3.0)
local heading = self.shellData.doorOffset.h
self.exitTarget = Framework[Config.Target].AddDoorZoneInside(coords, size, heading, leave, checkDoor)
end
function Property:RegisterPropertyEntrance()
local door = self.propertyData.door_data
local size = vector3(door.length, door.width, 2.5)
local heading = door.h
--Can be anon functions but I like to keep them named its more readable
local function enter()
TriggerServerEvent("ps-housing:server:enterProperty", self.property_id)
end
local function raid()
TriggerServerEvent("ps-housing:server:raidProperty", self.property_id)
end
local function showcase()
TriggerServerEvent("ps-housing:server:showcaseProperty", self.property_id)
end
local function showData()
local data = lib.callback.await("ps-housing:cb:getPropertyInfo", source, self.property_id)
if not data then return end
local content = "**Owner:** " .. data.owner .. " \n" .. "**Description:** " .. data.description .. " \n" .. "**Street:** " .. data.street .. " \n" .. "**Region:** " .. data.region .. " \n" .. "**Shell:** " .. data.shell .. " \n" .. "**For Sale:** " .. (data.for_sale and "Yes" or "No")
if data.for_sale then
content = content .. " \n" .. "**Price:** " .. data.price
end
lib.alertDialog({
header = data.street .. " " .. data.property_id,
content = content,
centered = true,
})
end
local targetName = string.format("%s_%s", self.propertyData.street, self.property_id)
self.entranceTarget = Framework[Config.Target].AddEntrance(door, size, heading, self.property_id, enter, raid, showcase, showData, targetName)
if self.owner or self.has_access then
self:CreateBlip()
end
end
function Property:UnregisterPropertyEntrance()
if not self.entranceTarget then return end
Framework[Config.Target].RemoveTargetZone(self.entranceTarget)
self.entranceTarget = nil
end
function Property:RegisterGarageZone()
if not next(self.propertyData.garage_data) then return end
if not (self.has_access or self.owner) or not self.owner then
return
end
local garageData = self.propertyData.garage_data
local garageName = string.format("property-%s-garage", self.property_id)
local data = {
takeVehicle = {
x = garageData.x,
y = garageData.y,
z = garageData.z,
w = garageData.h
},
type = "house",
label = self.propertyData.street .. self.property_id .. " Garage",
}
TriggerEvent("qb-garages:client:addHouseGarage", self.property_id, data)
self.garageZone = lib.zones.box({
coords = vec3(garageData.x, garageData.y, garageData.z),
size = vector3(garageData.length + 5.0, garageData.width + 5.0, 3.5),
rotation = garageData.h,
debug = Config.DebugMode,
onEnter = function()
TriggerEvent('qb-garages:client:setHouseGarage', self.property_id, true)
end,
})
end
function Property:UnregisterGarageZone()
if not self.garageZone then return end
TriggerEvent("qb-garages:client:removeHouseGarage", self.property_id)
self.garageZone:remove()
self.garageZone = nil
end
function Property:EnterShell()
DoScreenFadeOut(250)
TriggerServerEvent("InteractSound_SV:PlayOnSource", "houses_door_open", 0.25)
Wait(250)
self.inProperty = true
self.shellData = Config.Shells[self.propertyData.shell]
self:CreateShell()
self:LoadFurnitures()
self:GiveMenus()
Wait(250)
DoScreenFadeIn(250)
end
function Property:LeaveShell()
if not self.inProperty then return end
DoScreenFadeOut(250)
TriggerServerEvent("InteractSound_SV:PlayOnSource", "houses_door_open", 0.25)
Wait(250)
local coords = self:GetDoorCoords()
SetEntityCoordsNoOffset(cache.ped, coords.x, coords.y, coords.z, false, false, true)
TriggerServerEvent("ps-housing:server:leaveProperty", self.property_id)
self:UnloadFurnitures()
self.propertyData.furnitures = {}
self.shell:DespawnShell()
self.shell = nil
if self.exitTarget then
Framework[Config.Target].RemoveTargetZone(self.exitTarget)
self.exitTarget = nil
end
self:RemoveBlip()
self:RemoveMenus()
self.doorbellPool = {}
self.inProperty = false
Wait(250)
DoScreenFadeIn(250)
end
function Property:GiveMenus()
if not self.inProperty then return end
local accessAndConfig = self.has_access and Config.AccessCanEditFurniture
if self.owner or accessAndConfig then
Framework[Config.Radial].AddRadialOption(
"furniture_menu",
"Furniture Menu",
"house",
function()
Modeler:OpenMenu(self.property_id)
end,
"ps-housing:client:openFurnitureMenu",
{ propertyId = self.property_id }
)
end
if self.owner then
Framework[Config.Radial].AddRadialOption(
"access_menu",
"Manage Property",
"key",
function()
self:ManageAccessMenu()
end,
"ps-housing:client:openManagePropertyAccessMenu",
{ propertyId = self.property_id }
)
end
end
function Property:RemoveMenus()
if not self.inProperty then return end
Framework[Config.Radial].RemoveRadialOption("furniture_menu")
if self.owner then
Framework[Config.Radial].RemoveRadialOption("access_menu")
end
end
function Property:ManageAccessMenu()
if not self.inProperty then return end
if not self.owner then
Framework[Config.Notify].Notify("Only the owner can do this.", "error")
return
end
--Fuck qb-menu
local id = "property-" .. self.property_id .. "-access"
local menu = {
id = id,
title = "Manage Access",
options = {},
}
menu.options[#menu.options + 1] = {
title = "Give Access",
onSelect = function()
self:GiveAccessMenu()
end,
}
menu.options[#menu.options + 1] = {
title = "Revoke Access",
onSelect = function()
self:RevokeAccessMenu()
end,
}
lib.registerContext(menu)
lib.showContext(id)
end
function Property:GiveAccessMenu()
if not self.inProperty then return end
if not self.owner then
return
end
local id = "property-" .. self.property_id .. "-access-give"
local menu = {
id = id,
title = "Give Access",
options = {},
}
local players = lib.callback.await("ps-housing:cb:getPlayersInProperty", source, self.property_id) or {}
if #players > 0 then
for i = 1, #players do
local v = players[i]
menu.options[#menu.options + 1] = {
title = v.name,
description = "Give Access",
onSelect = function()
TriggerServerEvent("ps-housing:server:addAccess", self.property_id, v.src)
end,
}
end
lib.registerContext(menu)
lib.showContext(id)
else
Framework[Config.Notify].Notify("No one is in the property", "error")
end
end
function Property:RevokeAccessMenu()
if not self.owner then
return
end
local id = "property-" .. self.property_id .. "-access-already"
local alreadyAccessMenu = {
id = id,
title = "Revoke Access",
options = {},
}
local playersWithAccess = lib.callback.await("ps-housing:cb:getPlayersWithAccess", source, self.property_id) or {}
-- only stores names and citizenids in a table so if their offline you can still remove them
if #playersWithAccess > 0 then
for i = 1, #playersWithAccess do
local v = playersWithAccess[i]
alreadyAccessMenu.options[#alreadyAccessMenu.options + 1] = {
title = v.name,
description = "Remove Access",
onSelect = function()
TriggerServerEvent("ps-housing:server:removeAccess", self.property_id, v.citizenid)
end,
}
end
lib.registerContext(alreadyAccessMenu)
lib.showContext(id)
else
Framework[Config.Notify].Notify("No one has access to this property", "error")
end
end
function Property:OpenDoorbellMenu()
if not self.inProperty then return end
if not next(self.doorbellPool) then
Framework[Config.Notify].Notify("No one is at the door", "error")
return
end
local id = string.format("property-%s-doorbell", self.property_id)
local menu = {
id = id,
title = "People at the door",
options = {},
}
for k, v in pairs(self.doorbellPool) do
menu.options[#menu.options + 1] = {
title = v.name,
onSelect = function()
TriggerServerEvent(
"ps-housing:server:doorbellAnswer",
{ targetSrc = v.src, property_id = self.property_id }
)
end,
}
end
lib.registerContext(menu)
lib.showContext(id)
end
function Property:LoadFurniture(furniture)
local coords = GetOffsetFromEntityInWorldCoords(self.shellObj, furniture.position.x, furniture.position.y, furniture.position.z)
local hash = furniture.object
lib.requestModel(hash)
local entity = CreateObjectNoOffset(hash, coords.x, coords.y, coords.z, false, true, false)
SetModelAsNoLongerNeeded(hash)
SetEntityRotation(entity, furniture.rotation.x, furniture.rotation.y, furniture.rotation.z, 2, true)
if furniture.type == 'door' and Config.DynamicDoors then
Debug("Object: "..furniture.label.." wont be frozen")
else
FreezeEntityPosition(entity, true)
end
if furniture.type and Config.FurnitureTypes[furniture.type] then
Config.FurnitureTypes[furniture.type](entity, self.property_id, self.propertyData.shell)
end
self.furnitureObjs[#self.furnitureObjs + 1] = {
entity = entity,
id = furniture.id,
label = furniture.label,
object = furniture.object,
position = {
x = coords.x,
y = coords.y,
z = coords.z,
},
rotation = furniture.rotation,
type = furniture.type,
}
end
function Property:LoadFurnitures()
self.propertyData.furnitures = lib.callback.await('ps-housing:cb:getFurnitures', source, self.property_id) or {}
for i = 1, #self.propertyData.furnitures do
local furniture = self.propertyData.furnitures[i]
self:LoadFurniture(furniture)
end
end
function Property:UnloadFurniture(furniture, index)
local entity = furniture?.entity
if not entity then
for i = 1, #self.furnitureObjs do
if self.furnitureObjs[i]?.id and furniture?.id and self.furnitureObjs[i].id == furniture.id then
entity = self.furnitureObjs[i]?.entity
break
end
end
end
if self.clothingTarget == entity or self.storageTarget == entity then
Framework[Config.Target].RemoveTargetEntity(entity)
if self.clothingTarget == entity then
self.clothingTarget = nil
elseif self.storageTarget == entity then
self.storageTarget = nil
end
end
if index and self.furnitureObjs?[index] then
table.remove(self.furnitureObjs, index)
else
for i = 1, #self.furnitureObjs do
if self.furnitureObjs[i]?.id and furniture?.id and self.furnitureObjs[i].id == furniture.id then
table.remove(self.furnitureObjs, i)
break
end
end
end
DeleteObject(entity)
end
function Property:UnloadFurnitures()
for i = 1, #self.furnitureObjs do
local furniture = self.furnitureObjs[i]
self:UnloadFurniture(furniture, i)
end
self.furnitureObjs = {}
end
function Property:CreateBlip()
local door_data = self.propertyData.door_data
local blip = AddBlipForCoord(door_data.x, door_data.y, door_data.z)
if self.propertyData.garage_data.x ~= nil then
SetBlipSprite(blip, 492)
else
SetBlipSprite(blip, 40)
end
SetBlipScale(blip, 0.8)
SetBlipColour(blip, 2)
SetBlipAsShortRange(blip, true)
BeginTextCommandSetBlipName("STRING")
AddTextComponentString(self.propertyData.street .. " " .. self.property_id)
EndTextCommandSetBlipName(blip)
self.blip = blip
end
function Property:RemoveBlip()
if not self.blip then return end
RemoveBlip(self.blip)
self.blip = nil
end
function Property:RemoveProperty()
if Config.Target == "ox" then
Framework[Config.Target].RemoveTargetZone(self.entranceTarget)
else
local targetName = string.format("%s_%s", self.propertyData.street, self.property_id)
Framework[Config.Target].RemoveTargetZone(targetName)
end
self:RemoveBlip()
self:LeaveShell()
--@@ comeback to this
-- Think it works now
if self.propertyData.apartment then
ApartmentsTable[self.propertyData.apartment]:RemoveProperty()
end
self = nil
end
local function findFurnitureDifference(new, old)
local added = {}
local removed = {}
local edited = {}
for i = 1, #new do
local found = false
for j = 1, #old do
if new[i].id == old[j].id then
found = true
break
end
end
if not found then
added[#added + 1] = new[i]
end
end
for i = 1, #old do
local found = false
for j = 1, #new do
if old[i].id == new[j].id then
found = true
break
end
end
if not found then
removed[#removed + 1] = old[i]
end
end
for i = 1, #new do
if new[i].movedObject then
edited[#edited + 1] = new[i]
end
end
return added, removed, edited
end
-- I think this whole furniture sync is a bit shit, but I cbf thinking
function Property:UpdateFurnitures(newFurnitures)
if not self.inProperty then return end
local oldFurnitures = self.propertyData.furnitures
local added, removed, edited = findFurnitureDifference(newFurnitures, oldFurnitures)
for i = 1, #added do
local furniture = added[i]
self:LoadFurniture(furniture)
end
for i = 1, #removed do
local furniture = removed[i]
self:UnloadFurniture(furniture)
end
for i = 1, #edited do
local furniture = edited[i]
self:UnloadFurniture(furniture)
self:LoadFurniture(furniture)
end
local furnitures = {}
for i = 1, #newFurnitures do
furnitures[i] = {
id = newFurnitures[i].id,
label = newFurnitures[i].label,
object = newFurnitures[i].object,
position = newFurnitures[i].position,
rotation = newFurnitures[i].rotation,
type = newFurnitures[i].type
}
end
self.propertyData.furnitures = furnitures
Modeler:UpdateFurnitures()
end
function Property:UpdateDescription(newDescription)
self.propertyData.description = newDescription
end
function Property:UpdatePrice(newPrice)
self.propertyData.price = newPrice
end
function Property:UpdateForSale(forSale)
self.propertyData.for_sale = forSale
end
function Property:UpdateShell(newShell)
self:LeaveShell()
self.propertyData.shell = newShell
if self.inProperty then
self:EnterShell()
end
end
function Property:UpdateOwner(newOwner)
self.propertyData.owner = newOwner
local citizenid = PlayerData.citizenid
self.owner = newOwner == citizenid
self:UnregisterGarageZone()
self:RegisterGarageZone()
self:CreateBlip()
if not self.inProperty then return end
self:RemoveMenus()
self:GiveMenus()
end
function Property:UpdateImgs(newImgs)
self.propertyData.imgs = newImgs
end
function Property:UpdateDoor(newDoor, newStreet, newRegion)
self.propertyData.door_data = newDoor
self.propertyData.street = newStreet
self.propertyData.region = newRegion
self:UnregisterPropertyEntrance()
self:RegisterPropertyEntrance()
end
function Property:UpdateHas_access(newHas_access)
local citizenid = PlayerData.citizenid
self.propertyData.has_access = newHas_access
self.has_access = lib.table.contains(newHas_access, citizenid)
if not self.inProperty then return end
self:RemoveMenus()
self:GiveMenus()
end
function Property:UpdateGarage(newGarage)
self.propertyData.garage_data = newGarage
self:UnregisterGarageZone()
self:RegisterGarageZone()
end
function Property:UpdateApartment(newApartment)
self:LeaveShell()
local oldAptName = self.propertyData.apartment
local oldApt = ApartmentsTable[oldAptName]
if oldApt then
oldApt:RemoveProperty(self.property_id)
end
self.propertyData.apartment = newApartment
local newApt = ApartmentsTable[newApartment]
if newApt then
newApt:AddProperty(self.property_id)
end
TriggerEvent("ps-housing:client:updateApartment", oldAptName, newApartment)
end
function Property.Get(property_id)
return PropertiesTable[tostring(property_id)]
end
RegisterNetEvent("ps-housing:client:enterProperty", function(property_id)
local property = Property.Get(property_id)
property:EnterShell()
end)
RegisterNetEvent("ps-housing:client:updateDoorbellPool", function(property_id, data)
local property = Property.Get(property_id)
property.doorbellPool = data
end)
RegisterNetEvent("ps-housing:client:updateFurniture", function(property_id, furnitures)
local property = Property.Get(property_id)
if not property then return end
property:UpdateFurnitures(furnitures)
end)
RegisterNetEvent("ps-housing:client:updateProperty", function(type, property_id, data)
local property = Property.Get(property_id)
if not property then return end
property[type](property, data)
TriggerEvent("ps-housing:client:updatedProperty", property_id)
end)
RegisterNetEvent("ps-housing:client:openFurnitureMenu", function(data)
Modeler:OpenMenu(data.options.propertyId)
end)
RegisterNetEvent("ps-housing:client:openManagePropertyAccessMenu", function(data)
local property = Property.Get(data.options.propertyId)
if not property then return end
property:ManageAccessMenu()
end)