esx_core icon indicating copy to clipboard operation
esx_core copied to clipboard

(feat): add /setplaytime

Open 9zku opened this issue 5 months ago • 10 comments

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.

9zku avatar Jul 13 '25 14:07 9zku

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.

9zku avatar Jul 13 '25 14:07 9zku

it would be better to support s, m, h, and d indicators (for seconds, minutes, hours, and days) instead of forcing everything to hours

iSentrie avatar Jul 18 '25 14:07 iSentrie

I can edit the pr later today for you guy's.

9zku avatar Jul 18 '25 14:07 9zku

@rk3gaming any news?

Kenshiin13 avatar Jul 24 '25 21:07 Kenshiin13

My fault, have been busy irl. I'll work on it tonight

9zku avatar Jul 26 '25 01:07 9zku

Sorry for the wait. Here you go https://github.com/rk3gaming/esx_core/commit/b8cdca8b5569b7ec9a1bee08880a495204e6cd41

9zku avatar Jul 26 '25 01:07 9zku

[core]/es_extended/locales/en.lua


[core]/es_extended/server/classes/player.lua

  • lastPlaytime stores the player’s playtime before joining the current session.

  • getPlayTime() returns lastPlaytime + 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

["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",

Localized time string:

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, ', ')

Localized suggestions:

{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"},

Also consider localizing notifications

feelfreetofee avatar Jul 27 '25 12:07 feelfreetofee

@9zku

Kenshiin13 avatar Aug 04 '25 11:08 Kenshiin13

out traveling.

9zku avatar Aug 05 '25 11:08 9zku

CLA assistant check
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.

CLAassistant avatar Nov 16 '25 20:11 CLAassistant