-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_api.lua
226 lines (206 loc) · 7.19 KB
/
job_api.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
CurrentEditMode = 1
InEditMode = false
CurrentEditedObject = nil
SelectMode = false
function OnPackageStart()
CreateTimer(function()
for _,object in pairs(GetStreamedObjects(false)) do
if GetObjectPropertyValue(object, "harvestable") == 1 then
ShowHarvestObject(object)
end
if GetObjectPropertyValue(object, "no_collision") ~= nil then
SetObjectCollision(object)
end
if GetObjectPropertyValue(object, "editable") ~= nil then
SetEditableObject(object)
end
if GetObjectPropertyValue(object, "canBeWear") == 1 then
ShowWearableObject(object)
end
end
end, 1000)
end
AddEvent("OnPackageStart", OnPackageStart)
AddEvent("OnObjectStreamOut", function(object)
-- Disable edition
if CurrentEditedObject ~= nil then
if InEditMode then
SetObjectEditable(CurrentEditedObject, 0)
SetInputMode(0)
ShowMouseCursor(false)
IsMouseCursorEnabled(false)
InEditMode = false
CurrentEditedObject = nil
end
end
end)
function ShowHarvestObject(object)
local x,y,z = GetPlayerLocation(GetPlayerId())
local x2,y2,z2 = GetObjectLocation(object)
if GetDistance3D(x, y, z, x2, y2, z2) <= GetObjectPropertyValue(object, "harvestableInteractDistance") then
SetObjectOutline(object, true)
else
SetObjectOutline(object, false)
end
end
function ShowWearableObject(object)
local x,y,z = GetPlayerLocation(GetPlayerId())
local x2,y2,z2 = GetObjectLocation(object)
if GetDistance3D(x, y, z, x2, y2, z2) <= 150 then
SetObjectOutline(object, true)
else
SetObjectOutline(object, false)
end
end
function GetNearbyWearableObject()
local x,y,z = GetPlayerLocation(GetPlayerId())
n = nil
nD = 9999999
for _,o in pairs(GetStreamedObjects()) do
if GetObjectPropertyValue(o, "canBeWear") == 1 then
local x2, y2, z2 = GetObjectLocation(o)
if GetDistance3D(x,y,z,x2,y2,z2) <= 150 then
if n == nil then
n = o
nD = GetDistance3D(x,y,z,x2,y2,z2)
elseif(nD > GetDistance3D(x,y,z,x2,y2,z2)) then
n = o
nD = GetDistance3D(x,y,z,x2,y2,z2)
end
end
end
end
return n
end
function GetNearbyJobToolObject()
local x,y,z = GetPlayerLocation(GetPlayerId())
n = nil
nD = 9999999
for _,o in pairs(GetStreamedObjects()) do
if GetObjectPropertyValue(o, "isJobTool") == 1 then
local x2, y2, z2 = GetObjectLocation(o)
if GetDistance3D(x,y,z,x2,y2,z2) <= 250 then
if n == nil then
n = o
nD = GetDistance3D(x,y,z,x2,y2,z2)
elseif(nD > GetDistance3D(x,y,z,x2,y2,z2)) then
n = o
nD = GetDistance3D(x,y,z,x2,y2,z2)
end
end
end
end
return n
end
function SetEditableObject(object)
local x,y,z = GetPlayerLocation(GetPlayerId())
local x2, y2, z2 = GetObjectLocation(object)
if GetObjectPropertyValue(object, "editable_by") ~= GetPlayerId() then
local ObjectActor = GetObjectActor(object)
ObjectActor:SetActorHiddenInGame(true)
return
else
local ObjectActor = GetObjectActor(object)
ObjectActor:SetActorHiddenInGame(false)
end
if GetObjectPropertyValue(object, "editable") == 1 and GetDistance3D(x,y,z,x2,y2,z2) < 800 then
if CurrentEditedObject ~= nil then
return
end
if not InEditMode then
CallRemoteEvent("Object:EditPlacement", GetObjectPropertyValue(object, "uuid"))
SetObjectEditable(object, CurrentEditMode)
InEditMode = true
CurrentEditedObject = object
SetInputMode(1)
ShowMouseCursor(true)
IsMouseCursorEnabled(true)
SetObjectOutline(object, true)
end
else
if InEditMode then
CallRemoteEvent("Object:EditPlacementCancel")
SetObjectEditable(object, 0)
SetObjectOutline(object, false)
SetInputMode(0)
ShowMouseCursor(false)
IsMouseCursorEnabled(false)
InEditMode = false
CurrentEditedObject = nil
end
end
end
function SetObjectCollision(object)
local ObjectActor = GetObjectActor(object)
if GetObjectPropertyValue(object, "no_collision") == 1 then
ObjectActor:SetActorEnableCollision(false)
else
ObjectActor:SetActorEnableCollision(true)
end
end
function SubmitEditableObjectPlacement()
if CurrentEditedObject == nil then
return
end
local x, y, z = GetObjectLocation(CurrentEditedObject)
local rx, ry, rz = GetObjectRotation(CurrentEditedObject)
CallRemoteEvent("Object:EditPlacementDone", x,y,z,rx,ry,rz)
if SelectMode then
ToogleSelectState()
end
end
function ToogleSelectState()
if SelectMode then
SetInputMode(0)
EnableFirstPersonCamera(false)
ShowMouseCursor(false)
SelectMode = false
SetNearClipPlane(0)
else
SetInputMode(1)
EnableFirstPersonCamera(true)
ShowMouseCursor(true)
SelectMode = true
SetNearClipPlane(15)
end
end
AddEvent("OnKeyPress", function(key)
if not IsCtrlPressed() then
if(key == "E" and GetNearbyWearableObject() ~= nil) then
CallRemoteEvent("Job:WearObject", GetObjectPropertyValue(GetNearbyWearableObject(), "uuid"))
elseif(key == "E" and GetNearbyJobToolObject() ~= nil) then
CallRemoteEvent("Object:Interact")
CallRemoteEvent("Job:UseJobTool", GetObjectPropertyValue(GetNearbyJobToolObject(), "uuid"))
elseif key == "E" and GetNearbyWearableObject() == nil then
CallRemoteEvent("Object:Interact")
end
else -- ctrl pressed
if(key == "E") then
ToogleSelectState()
end
end
-- Select object to edit
if SelectMode and key == 'Left Mouse Button' and not InEditMode then
local EntityType, EntityId = GetMouseHitEntity()
if IsValidObject(EntityId) and EntityType == HIT_OBJECT and GetObjectPropertyValue(EntityId, "houseItemId") ~= nil then
CallRemoteEvent("Object:EditExistingPlacement", GetObjectPropertyValue(EntityId, "houseItemId"))
end
end
-- Rotate or Move
if key == "O" then
if InEditMode and CurrentEditedObject ~= nil then
if CurrentEditMode == 1 then
CurrentEditMode = 2
else
CurrentEditMode = 1
end
SetObjectEditable(CurrentEditedObject, CurrentEditMode)
end
end
-- Validate placement
if key == "V" then
if InEditMode and CurrentEditedObject ~= nil then
SubmitEditableObjectPlacement()
end
end
end)