Skip to content

Commit bb46ac1

Browse files
authored
Instant paint sync (#653)
"The paint color pickers sync out immediately to all clients, and any traffic vehicles sync their new color when they teleport and reset." Updated version of #649 by @StanleyDudek. This PR uses a dedicated packet which is yet to be added to the server BeamMP/BeamMP-Server#381
2 parents b3d9ef7 + 8506cec commit bb46ac1

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

lua/ge/extensions/MPVehicleGE.lua

+41
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,19 @@ local function onVehicleResetted(gameVehicleID)
13931393
end
13941394
end
13951395

1396+
--============================ ON VEHICLE COLOR CHANGED (CLIENT) ============================
1397+
local function onVehicleColorChanged(gameVehicleID, index, paint)
1398+
if not MPCoreNetwork.isMPSession() then return end -- do nothing if singleplayer
1399+
local vehicle = getVehicleByGameID(gameVehicleID) -- get vehicle table for this vehicle
1400+
if vehicle and vehicle.serverVehicleString and vehicle.isLocal then -- If serverVehicleID not null and player own vehicle
1401+
1402+
local veh = be:getObjectByID(gameVehicleID) -- get vehicle as object
1403+
local paintData = MPHelpers.getColorsFromVehObj(veh)
1404+
paintData[index] = paint --insert new paint at index as chosen from color picker
1405+
1406+
MPGameNetwork.send('Op:'..vehicle.serverVehicleString..":"..jsonEncode(paintData).."")
1407+
end
1408+
end
13961409

13971410

13981411
-- server events
@@ -1596,6 +1609,24 @@ local function onServerCameraSwitched(playerID, serverVehicleID)
15961609
vehicles[serverVehicleID].spectators[playerID] = true
15971610
end
15981611

1612+
local function onServerVehicleColorChanged(serverVehicleID, data)
1613+
local gameVehicleID = getGameVehicleID(serverVehicleID) -- Get game ID
1614+
local vehicle = getVehicleByGameID(gameVehicleID) -- get vehicle table for this vehicle
1615+
if vehicle and vehicle.serverVehicleString and not vehicle.isLocal and not vehicle.editQueue then -- If serverVehicleID not null and not player own vehicle
1616+
if gameVehicleID then
1617+
local veh = be:getObjectByID(gameVehicleID) -- Get associated vehicle
1618+
if veh then
1619+
local paint = jsonDecode(data) -- Decoded data
1620+
if paint then -- if there's paint data
1621+
veh:queueLuaCommand("extensions.hook(\"onBeamMPVehicleColorChange\")")
1622+
for k, v in pairs(paint) do -- apply paint
1623+
extensions.core_vehicle_manager.liveUpdateVehicleColors(gameVehicleID, veh, k, v)
1624+
end
1625+
end
1626+
end
1627+
end
1628+
end
1629+
end
15991630

16001631
local HandleNetwork = {
16011632
['s'] = function(rawData) -- spawn
@@ -1652,6 +1683,15 @@ local HandleNetwork = {
16521683
else
16531684
-- public version has missing playerID
16541685
end
1686+
end,
1687+
['p'] = function(rawData) -- live paint update
1688+
local serverVehicleID, data = string.match(rawData,"^(%d+%-%d+)%:(%[.+%])") -- '0-0:[jsonstring]'
1689+
1690+
if serverVehicleID ~= nil then
1691+
onServerVehicleColorChanged(serverVehicleID, data)
1692+
else
1693+
log('E', "HandleNetwork", "Color pattern match failed")
1694+
end
16551695
end
16561696
}
16571697

@@ -2258,6 +2298,7 @@ M.onVehicleSpawned = onVehicleSpawned
22582298
M.onVehicleDestroyed = onVehicleDestroyed
22592299
M.onVehicleSwitched = onVehicleSwitched
22602300
M.onVehicleResetted = onVehicleResetted
2301+
M.onVehicleColorChanged = onVehicleColorChanged
22612302
M.onPlayerLeft = onPlayerLeft
22622303
M.onClientPostStartMission = onDisconnect
22632304
M.onUIInitialised = onUIInitialised

0 commit comments

Comments
 (0)