(feat): add /setplaytime
Description
This pull request adds a /setplaytime command that allows admins to set a players playtime that's stored.
Motivation
Some of the server's I develop, they have playtime scripts which control what the players can do, Example; You can't use weapons without 24 hours of playtime. And there is times where they need to set a players playtime for whatever reasons.
Implementation Details
This Implements /setplaytime [playerId] [hours] for admins to manually set a player's total playtime. The new value is saved immediately.
Usage Example
local serverId = 1
local xPlayer = ESX.GetPlayerFromId(serverId)
if xPlayer then
-- Set the player's playtime to 50 hours (converted to seconds)
xPlayer.setPlayTime(50 * 3600)
end
PR Checklist
- [x] My commit messages and PR title follow the Conventional Commits standard.
- [x] My changes have been tested locally and function as expected.
- [x] My PR does not introduce any breaking changes.
- [x] I have provided a clear explanation of what my PR does, including the reasoning behind the changes and any relevant context.
I can understand if this is seemed useless for most server's or not what ESX had intended playtime to be for, so if this PR gets denied thats understandable. Just thought I'd try to contribute something that I use quite often.
it would be better to support s, m, h, and d indicators (for seconds, minutes, hours, and days) instead of forcing everything to hours
I can edit the pr later today for you guy's.
@rk3gaming any news?
My fault, have been busy irl. I'll work on it tonight
Sorry for the wait. Here you go https://github.com/rk3gaming/esx_core/commit/b8cdca8b5569b7ec9a1bee08880a495204e6cd41
[core]/es_extended/locales/en.lua
"command_setpt_playtime"appears to be unused.
[core]/es_extended/server/classes/player.lua
-
lastPlaytimestores the player’s playtime before joining the current session. -
getPlayTime()returnslastPlaytime + GetPlayerTimeOnline(self.source).
For consistency, consider subtracting the current session time:
playtime = ESX.Math.Round(playtime) - GetPlayerTimeOnline(self.source)
if playtime < 0 then
playtime = 0
end
- Metadata update is likely redundant since it's already handled periodically by the server.
I’d remove that line altogether. But if kept, use the value returned by getter instead.
[core]/es_extended/server/modules/commands.lua
-
Time arguments defaulting to
0is unnecessary sincevalidate = trueis already set. -
Localization for days, hours, minutes, seconds:
["day"] = "day",
["days"] = "days",
["number_day"] = "%d day",
["number_days"] = "%d days",
["hour"] = "hour",
["hours"] = "hours",
["number_hour"] = "%d hour",
["number_hours"] = "%d hours",
["minute"] = "minute",
["minutes"] = "minutes",
["number_minute"] = "%d minute",
["number_minutes"] = "%d minutes",
["second"] = "second",
["seconds"] = "seconds",
["number_second"] = "%d second",
["number_seconds"] = "%d seconds",
local timeString = {}
if days > 0 then
table.insert(
timeString,
TranslateCap(
days == 1
and 'number_day'
or 'number_days',
days
)
)
end
if hours > 0 then
table.insert(
timeString,
TranslateCap(
hours == 1
and 'number_hour'
or 'number_hours',
hours
)
)
end
if minutes > 0 then
table.insert(
timeString,
TranslateCap(
minutes == 1
and 'number_minute'
or 'number_minutes',
minutes
)
)
end
if seconds > 0 then
table.insert(
timeString,
TranslateCap(
seconds == 1
and 'number_second'
or 'number_seconds',
seconds
)
)
end
timeString = table.concat(timeString, ', ')
{name = "days", help = TranslateCap("days"), type = "number"},
{name = "hours", help = TranslateCap("hours"), type = "number"},
{name = "minutes", help = TranslateCap("minutes"), type = "number"},
{name = "seconds", help = TranslateCap("seconds"), type = "number"},
@9zku
out traveling.
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.