Skip to content

Commit

Permalink
Make suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico8340 committed Jan 8, 2025
1 parent 74dc4d1 commit c438f60
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 45 deletions.
2 changes: 1 addition & 1 deletion [gameplay]/headshot/meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<min_mta_version server="1.6.0" />

<settings>
<setting name="*decap" desc="determines whether the player becomes headless after a headshot" value="true" accept="false, true" />
<setting name="*decap" desc="determines whether the player becomes headless after a headshot" value="[true]" accept="false, true" />
</settings>

<script type="server" src="src/headshotSettings.lua" />
Expand Down
109 changes: 65 additions & 44 deletions [gameplay]/headshot/src/headshotSettings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@ local headshotSettingsAvailable = {
{"decap", "boolean"}
}

local function headshotSettingsNumberize(headshotValue)
if not headshotValue then
return
end

if type(headshotValue) == "number" then
return headshotValue
end

if type(headshotValue) == "string" then
return tonumber(headshotValue)
end

return 0
end

local function headshotSettingsBooleanize(headshotValue)
if type(headshotValue) == "boolean" then
return headshotValue
end

if type(headshotValue) == "string" then
return headshotValue == "[true]"
end

return false
end

function headshotSettingsGet(headshotSetting)
if not headshotSetting or type(headshotSetting) ~= "string" then
return
Expand All @@ -11,67 +39,60 @@ function headshotSettingsGet(headshotSetting)
return headshotSettings[headshotSetting]
end

function headshotSettingsBooleanize(headshotValue)
if not headshotValue or type(headshotValue) ~= "string" then
function headshotSettingsSet(headshotSetting, headshotValue)
if not headshotSetting or type(headshotSetting) ~= "string" then
return
end

if string.find(headshotValue, "true") then
return true
local headshotDot = string.find(headshotSetting, "%.")

if headshotDot and type(headshotDot) == "number" then
headshotSetting = string.sub(headshotSetting, headshotDot + 1)
end

return false
local headshotFound

for _, headshotEntry in ipairs(headshotSettingsAvailable) do
if headshotEntry[1] == headshotSetting then
headshotFound = headshotEntry
break
end
end

if not headshotFound then
return
end

local headshotType = headshotFound[2]

if headshotType == "string" and type(headshotValue) == "string" then
headshotSettings[headshotSetting] = headshotValue
return
end

if headshotType == "number" and type(headshotValue) == "string" then
headshotSettings[headshotSetting] = headshotSettingsNumberize(headshotValue)
return
end

if headshotType == "boolean" then
headshotSettings[headshotSetting] = headshotSettingsBooleanize(headshotValue)
end
end

addEventHandler("onResourceStart", resourceRoot,
function()
for _, headshotEntry in pairs(headshotSettingsAvailable) do
for _, headshotEntry in ipairs(headshotSettingsAvailable) do
local headshotSetting = headshotEntry[1]
local headshotType = headshotEntry[2]
local headshotValue = get(headshotSetting)

if headshotValue and type(headshotValue) == "string" then
if headshotType == "boolean" then
headshotSettings[headshotSetting] = headshotSettingsBooleanize(headshotValue)
else
headshotSettings[headshotSetting] = headshotValue
end
end
headshotSettingsSet(headshotSetting, headshotValue)
end
end
)

addEventHandler("onSettingChange", root,
function(headshotSetting, _, headshotValue)
local headshotDot = string.find(headshotSetting, "%.")

if headshotDot and type(headshotDot) == "number" then
headshotSetting = string.sub(headshotSetting, headshotDot + 1)
end

local headshotFound

for _, headshotEntry in pairs(headshotSettingsAvailable) do
if headshotEntry[1] == headshotSetting then
headshotFound = headshotEntry
break
end
end

if not headshotFound then
return
end

local headshotType = headshotFound[2]

if not headshotValue or type(headshotValue) ~= "string" then
return
end

if headshotType == "boolean" then
headshotSettings[headshotSetting] = headshotSettingsBooleanize(headshotValue)
else
headshotSettings[headshotSetting] = headshotValue
end
headshotSettingsSet(headshotSetting, headshotValue)
end
)

0 comments on commit c438f60

Please sign in to comment.