Skip to content

Commit

Permalink
Add element data compatibility layer
Browse files Browse the repository at this point in the history
and few smaller changes
  • Loading branch information
ds1-e committed Nov 22, 2024
1 parent b67e43c commit 7ec514e
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 24 deletions.
16 changes: 8 additions & 8 deletions [gameplay]/superman/CHandleSuperman.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ local IDLE_ANIMATION = "Coplook_loop"
local IDLE_ANIM_LOOP = true
local MAX_Y_ROTATION = 70
local ROTATION_Y_SPEED = 3.8
local warningTextColor = tocolor(255, 0, 0, 255)

-- Static variables

Expand Down Expand Up @@ -141,7 +142,7 @@ function showWarning()
local sx, sy, dist = getScreenFromWorldPosition(x, y, z + 0.3)

if sx and sy and dist and dist < 100 then
dxDrawText("You can not warp into a vehicle when superman is activated.", sx, sy, sx, sy, tocolor(255, 0, 0, 255), 1.1, "default-bold", "center")
dxDrawText("You can not warp into a vehicle when superman is activated.", sx, sy, sx, sy, warningTextColor, 1.1, "default-bold", "center")
end
end

Expand All @@ -165,7 +166,7 @@ end
function Superman.onStreamOut()
local self = Superman

if source and isElement(source) and getElementType(source) == "player" and isPlayerFlying(source) then
if isPlayerFlying(source) then
self.rotations[source] = nil
self.previousVelocity[source] = nil
end
Expand All @@ -182,12 +183,12 @@ function onClientSupermanDataChange(dataKey, _, newValue)
Superman:restorePlayer(source)
end
end
addEvent("onClientSupermanDataChange", false)
addEventHandler("onClientSupermanDataChange", root, onClientSupermanDataChange)
if (not SUPERMAN_USE_ELEMENT_DATA) then addEvent("onClientSupermanDataChange", false) end
addEventHandler(SUPERMAN_USE_ELEMENT_DATA and "onClientElementDataChange" or "onClientSupermanDataChange", root, onClientSupermanDataChange)

-- onJump: Combo to start flight without any command
function Superman.onJump(key, keyState)
local self = Superman

function Superman.onJump()
local task = getPedSimplestTask(localPlayer)

if not isPlayerFlying(localPlayer) then
Expand All @@ -199,8 +200,6 @@ function Superman.onJump(key, keyState)
end

function Superman.cmdSuperman()
local self = Superman

if isPedInVehicle(localPlayer) or isPlayerFlying(localPlayer) then
return
end
Expand All @@ -226,6 +225,7 @@ function Superman.startFlight()
end

-- Controls processing

local jump, oldJump = false, false

function Superman.processControls()
Expand Down
12 changes: 8 additions & 4 deletions [gameplay]/superman/data/CData.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
local function onClientSupermanSync(supermanServerData)
syncSupermansData(supermanServerData)
end
addEvent("onClientSupermanSync", true)
addEventHandler("onClientSupermanSync", localPlayer, onClientSupermanSync)
if (not SUPERMAN_USE_ELEMENT_DATA) then
addEvent("onClientSupermanSync", true)
addEventHandler("onClientSupermanSync", localPlayer, onClientSupermanSync)
end

local function onClientSupermanSetData(dataKey, dataValue)
setSupermanData(source, dataKey, dataValue)
end
addEvent("onClientSupermanSetData", true)
addEventHandler("onClientSupermanSetData", root, onClientSupermanSetData)
if (not SUPERMAN_USE_ELEMENT_DATA) then
addEvent("onClientSupermanSetData", true)
addEventHandler("onClientSupermanSetData", root, onClientSupermanSetData)
end
59 changes: 55 additions & 4 deletions [gameplay]/superman/data/SData.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,53 @@ local function tableToElementsArray(tableWithElements)
return arrayTable
end

local function canElementDataBeChanged(clientElement, sourceElement, dataKey, newValue)
local matchingPlayer = (clientElement == sourceElement)

if (not matchingPlayer) then
return false
end

local supermanDataKey = SUPERMAN_ALLOWED_DATA_KEYS[dataKey]

if (not supermanDataKey) then
return true
end

local newValueDataType = type(newValue)
local newValueBool = (newValueDataType == "boolean")

if (not newValueBool) then
return false
end

return true
end

local function onServerSupermanSetData(dataKey, dataValue)
if (not client) then
return false
end

setSupermanData(client, dataKey, dataValue)
end
addEvent("onServerSupermanSetData", true)
addEventHandler("onServerSupermanSetData", root, onServerSupermanSetData)
if (not SUPERMAN_USE_ELEMENT_DATA) then
addEvent("onServerSupermanSetData", true)
addEventHandler("onServerSupermanSetData", root, onServerSupermanSetData)
end

local function onElementDataChangeSuperman(dataKey, oldValue, newValue)
if (not client) then
return false
end

local allowElementDataChange = canElementDataBeChanged(client, source, dataKey, newValue)

if (not allowElementDataChange) then
setElementData(source, dataKey, oldValue)
end
end
if (SUPERMAN_USE_ELEMENT_DATA) then addEventHandler("onElementDataChange", root, onElementDataChangeSuperman) end

local function onPlayerResourceStartSyncSuperman(startedResource)
local matchingResource = (startedResource == resource)
Expand All @@ -40,12 +78,25 @@ local function onPlayerResourceStartSyncSuperman(startedResource)
triggerClientEvent(source, "onClientSupermanSync", source, supermansData)
supermanReceivers[source] = true
end
addEventHandler("onPlayerResourceStart", root, onPlayerResourceStartSyncSuperman)
if (not SUPERMAN_USE_ELEMENT_DATA) then addEventHandler("onPlayerResourceStart", root, onPlayerResourceStartSyncSuperman) end

local function onResourceStopClearSupermanElementData()
local playersTable = getElementsByType("player")

for playerID = 1, #playersTable do
local playerElement = playersTable[playerID]

for dataKey, _ in pairs(SUPERMAN_ALLOWED_DATA_KEYS) do
removeElementData(playerElement, dataKey)
end
end
end
if (SUPERMAN_USE_ELEMENT_DATA) then addEventHandler("onResourceStop", resourceRoot, onResourceStopClearSupermanElementData) end

local function onPlayerQuitClearSupermanReceiver()
supermanReceivers[source] = nil
end
addEventHandler("onPlayerQuit", root, onPlayerQuitClearSupermanReceiver)
if (not SUPERMAN_USE_ELEMENT_DATA) then addEventHandler("onPlayerQuit", root, onPlayerQuitClearSupermanReceiver) end

function getSupermanReceivers()
local supermanListeners = tableToElementsArray(supermanReceivers)
Expand Down
37 changes: 29 additions & 8 deletions [gameplay]/superman/data/ShData.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
local isServer = (not triggerServerEvent)
local supermansData = {}

SUPERMAN_USE_ELEMENT_DATA = true -- decides whether script will use built-in MTA data system (setElementData) or custom one, shipped with superman resource

-- in general element data is bad, and shouldn't be used, hence it should be set to false, unless you want to have backwards compatibility

SUPERMAN_ALLOWED_DATA_KEYS = {
[SUPERMAN_FLY_DATA_KEY] = true,
[SUPERMAN_TAKE_OFF_DATA_KEY] = true,
Expand All @@ -20,12 +24,6 @@ function getSupermanData(playerElement, dataKey)
return false
end

local supermanData = supermansData[playerElement]

if (not supermanData) then
return false
end

local dataKeyType = type(dataKey)
local dataKeyString = (dataKeyType == "string")

Expand All @@ -39,6 +37,16 @@ function getSupermanData(playerElement, dataKey)
return false
end

if (SUPERMAN_USE_ELEMENT_DATA) then
return getElementData(playerElement, dataKey)
end

local supermanData = supermansData[playerElement]

if (not supermanData) then
return false
end

local playerSupermanData = supermanData[dataKey]

return playerSupermanData
Expand Down Expand Up @@ -78,6 +86,19 @@ function setSupermanData(playerElement, dataKey, dataValue)
return false
end

if (SUPERMAN_USE_ELEMENT_DATA) then
local oldElementData = getElementData(playerElement, dataKey)
local updateElementData = (oldElementData ~= dataValue)

if (updateElementData) then
local syncElementData = (isServer or not isServer and localPlayer == playerElement)

return setElementData(playerElement, dataKey, dataValue, syncElementData)
end

return false
end

local supermanData = supermansData[playerElement]

if (not supermanData) then
Expand All @@ -101,7 +122,7 @@ function setSupermanData(playerElement, dataKey, dataValue)
else
local syncToServer = (playerElement == localPlayer)

triggerEvent("onClientSupermanDataChange", playerElement, dataKey, oldDataValue, dataValue)
triggerEvent(isServer and "onSupermanDataChange" or "onClientSupermanDataChange", playerElement, dataKey, oldDataValue, dataValue)

if (syncToServer) then
triggerServerEvent("onServerSupermanSetData", localPlayer, dataKey, dataValue)
Expand All @@ -122,4 +143,4 @@ end
local function onClientServerPlayerQuitClearSupermanData()
supermansData[source] = nil
end
addEventHandler(isServer and "onPlayerQuit" or "onClientPlayerQuit", root, onClientServerPlayerQuitClearSupermanData)
if (not SUPERMAN_USE_ELEMENT_DATA) then addEventHandler(isServer and "onPlayerQuit" or "onClientPlayerQuit", root, onClientServerPlayerQuitClearSupermanData) end
17 changes: 17 additions & 0 deletions [gameplay]/superman/meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,21 @@

<script src="CHandleSuperman.lua" type="client"/>
<script src="SHandleSuperman.lua" type="server"/>

<export function="getSupermanData" type="shared"/> <!-- do not use in render events or fast triggering logic, otherwise your CPU will suffer heavily, for data get you can use custom event "onClientSupermanDataChange" or "onSupermanDataChange", for example:
function onClientSupermanDataChange(dataKey, oldValue, newValue)
end
addEvent("onClientSupermanDataChange", false)
addEventHandler("onClientSupermanDataChange", root, onClientSupermanDataChange)
function onSupermanDataChange(dataKey, oldValue, newValue)
end
addEvent("onSupermanDataChange", false)
addEventHandler("onSupermanDataChange", root, onSupermanDataChange)
-->
<export function="setSupermanData" type="shared"/> <!-- do not use in render events or fast triggering logic, otherwise your CPU will suffer heavily -->
</meta>

0 comments on commit 7ec514e

Please sign in to comment.