okay, can also be my bad when writing it ^^
Thanks
[DOUBLEPOST=1427568568][/DOUBLEPOST]Cant I use rust.SendChatMessage
for that You are now AFK and stuff?
Also where can i add this You arent AFK anymore?
Solved Make players sleep? (Lua)
Discussion in 'Rust Development' started by LaserHydra, Mar 27, 2015.
-
You can do that yes, and for the message when a player wakes up you do that in the OnRunCommand hook after it checked that the command that was run is wakeup and after it checked if the player was afk (don't want to send players the message when they login
) -
How do I set the right person? so if I wanna give out: Player isn't AFK anymore!
I cant set that Player with the player.displayName variable, can I? -
Ah you want to broadcast, you'll need to use a different function to broadcast it to everyone and yes at that point you can use player.displayName to display the name.
rust.BroadcastChat("AFK", player.displayName .. " is no longer afk!") -
that works but it does not work with waking up. I thing the reason is because, also if I am using wakeup in the ingame console, it does not work...
So I think that does not work like that anymore. -
Where did you put it?
-
Code:
PLUGIN.Title = "AFK" PLUGIN.Description = "AFK System" PLUGIN.Author = "LaserHydra" PLUGIN.Version = V(1,0,0) PLUGIN.HasConfig = truelocal afkplayers = {}local afkcheckinterval = 30 local timeUntilAfk = 600 local timeUntilDisconnect = 1200 local afktimers = {} local afkbypassadmin = true local afkchecks = {}function PLUGIN:Init() command.AddChatCommand("afk", self.Object, "cmdAfk") command.AddChatCommand("afklist", self.Object, "cmdAfkList") self:LoadDefaultConfig() local itPlayerList = global.BasePlayer.activePlayerList:GetEnumerator() while itPlayerList:MoveNext() do local player = itPlayerList.Current self:OnPlayerInit(player) end end function PLUGIN:LoadDefaultConfig() self.Config.AfkListMsg = self.Config.AfkListMsg or "Currently AFK:" self.Config.NotAllowedMsg = self.Config.NotAllowedMsg or "You are not allowed to use this command." self.Config.PlayerAfkMsg = self.Config.PlayerAfkMsg or "is now AFK!" self.Config.PlayerAfkPrivateMsg = self.Config.PlayerAfkPrivateMsg or "You are now AFK!" self:SaveConfig() end function PLUGIN:cmdAfk(player) self:SetAfk(player) endfunction PLUGIN:OnPlayerInit(player) if afkbypassadmins then if player.net.connection.authLevel > 0 then return end end local steamid = rust.UserIDFromPlayer(player) afktimers[steamid] = timer.Repeat(afkcheckinterval, 0, function() self:AfkCheck(player) end, self.Plugin) endfunction OnPlayerDisconnected(player) local steamid = rust.UserIDFromPlayer(player) if afktimers[steamid] then afktimers[steamid]:Destroy() end endfunction PLUGIN:AfkCheck(player) local steamid = rust.UserIDFromPlayer(player) local position = player.transform.position if afkchecks[steamid] then if afkchecks[steamid].x ~= position.x or afkchecks[steamidk].y ~= position.y or afkchecks[steamid].z ~= position.z then if afkchecks[steamid].counter then afkchecks[steamid].counter = 1 else afkchecks[steamid].counter = afkchecks[steamid].counter + 1 end if afkchecks[steamid].counter >= (timeUntilAfk / afkcheckinterval) then self:SetAfk(player) end if afkchecks[steamid].counter >= (timeUntilDisconnect / afkcheckinterval) then self:Disconnect(player) end else afkchecks[steamid] = { x = position.x, y = position.y, z = position.z } afkchecks[steamid].counter = 0 end else afkchecks[steamid] = { x = position.x, y = position.y, z = position.z } end endfunction PLUGIN:SetAfk(player) local steamid = rust.UserIDFromPlayer(player) afkplayers[steamid] = player.displayName self:SendMessage(player, "AFK" .. self.Config.PlayerAfkPrivateMsg) player:StartSleeping() endfunction PLUGIN:OnRunCommand( arg ) if not arg then return end if not arg.cmd then return end if not arg.cmd.name then return end if not arg.connection then return end if not arg.connection.player then return end if arg.cmd.name == "wakeup" then local steamid = rust.UserIDFromPlayer(arg.connection.player) if afkplayers[steamid] then afkplayers[steamid] = nil self:EndAfk(player) end end endfunction PLUGIN:SendMessage(target, message) if not target then return end if not target:IsConnected() then return end message = UnityEngine.StringExtensions.QuoteSafe( message ) --target:SendConsoleCommand( "chat.add \"" .. "AFK " .. "\"" .. message ) rust.BroadcastChat("AFK", "" .. target.displayName .. " is now AFK!") endfunction PLUGIN:EndAfk(player) rust.BroadcastChat("AFK", player.displayName .. " is no longer AFK!") end -
This will fix the timer problem, just noticed with all the changes that were made that the wakeup command is no longer used so a hook would need to be added to check if a player wakes up so you can't check that for now actually.
Code:PLUGIN.Title = "AFK" PLUGIN.Description = "AFK System" PLUGIN.Author = "LaserHydra" PLUGIN.Version = V(1,0,0) PLUGIN.HasConfig = truelocal afkplayers = {} local afkcheckinterval = 30 local timeUntilAfk = 600 local timeUntilDisconnect = 1200 local afktimers = {} local afkbypassadmin = true local afkchecks = {}function PLUGIN:Init() command.AddChatCommand("afk", self.Object, "cmdAfk") command.AddChatCommand("afklist", self.Object, "cmdAfkList") self:LoadDefaultConfig() local itPlayerList = global.BasePlayer.activePlayerList:GetEnumerator() while itPlayerList:MoveNext() do local player = itPlayerList.Current self:OnPlayerInit(player) end end function PLUGIN:LoadDefaultConfig() self.Config.AfkListMsg = self.Config.AfkListMsg or "Currently AFK:" self.Config.NotAllowedMsg = self.Config.NotAllowedMsg or "You are not allowed to use this command." self.Config.PlayerAfkMsg = self.Config.PlayerAfkMsg or "is now AFK!" self.Config.PlayerAfkPrivateMsg = self.Config.PlayerAfkPrivateMsg or "You are now AFK!" self:SaveConfig() end function PLUGIN:cmdAfk(player) self:SetAfk(player) endfunction PLUGIN:OnPlayerInit(player) if afkbypassadmins then if player.net.connection.authLevel > 0 then return end end local steamid = rust.UserIDFromPlayer(player) afktimers[steamid] = timer.Repeat(afkcheckinterval, 0, function() self:AfkCheck(player) end, self.Plugin) endfunction OnPlayerDisconnected(player) local steamid = rust.UserIDFromPlayer(player) if afktimers[steamid] then afktimers[steamid]:Destroy() end endfunction PLUGIN:AfkCheck(player) local steamid = rust.UserIDFromPlayer(player) local position = player.transform.position if afkchecks[steamid] and afkchecks[steamid].x and afkchecks[steamid].y and afkchecks[steamid].z then if afkchecks[steamid].x == position.x or afkchecks[steamidk].y == position.y or afkchecks[steamid].z == position.z then if not afkchecks[steamid].counter then afkchecks[steamid].counter = 1 else afkchecks[steamid].counter = afkchecks[steamid].counter + 1 end if afkchecks[steamid].counter >= (timeUntilAfk / afkcheckinterval) then self:SetAfk(player) end if afkchecks[steamid].counter >= (timeUntilDisconnect / afkcheckinterval) then self:Disconnect(player) end else afkchecks[steamid] = { x = position.x, y = position.y, z = position.z } afkchecks[steamid].counter = 0 end else afkchecks[steamid] = { x = position.x, y = position.y, z = position.z } end print("Timer ticked") endfunction PLUGIN:SetAfk(player) local steamid = rust.UserIDFromPlayer(player) afkplayers[steamid] = player.displayName self:SendMessage(player, "AFK " .. self.Config.PlayerAfkPrivateMsg) rust.BroadcastChat("AFK", player.displayName .. " is now AFK!") player:StartSleeping() endfunction PLUGIN:SendMessage(target, message) if not target then return end if not target:IsConnected() then return end message = UnityEngine.StringExtensions.QuoteSafe( message ) target:SendConsoleCommand( "chat.add \"" .. "AFK " .. "\"" .. message ) endfunction PLUGIN:EndAfk(player) rust.BroadcastChat("AFK", player.displayName .. " is no longer AFK!") end -
So there is no possibility to call that "Player is no longer AFK!" message?
-
You could alter the afk check so that the player moves again and the counter is reset to 0 that the message is displayed at that point but this could result in a delay up to 30 seconds if the interval timer is set to 30.
Ideally a C# plugin would be used using the OnPlayerInput hook though. -
can't I set the interval below 30?
-
You can yes
-
So if I set it 5 it wont be a problem
-
It would reduce the delay but the timers would run frequently and imagine this on a server with 100 players, so that's 100 timers that tick every 5 seconds, I'm not sure how it will perform when doing that, haven't really run anything with that many timers in Lua recently.
-
What would you do in my place?
-
Settle for a shorter interval but not too short, 15 perhaps with the intention to rewrite the plugin eventually to C# to use actual player input to check if a player is afk instead of checking the position on interval and comparing it to the last.
-
Yeah the problem is, that I even know less of C# ^^
I would need to start up again from zero...
[DOUBLEPOST=1427593220][/DOUBLEPOST]how do I check if he moves again? XD I didn't understand the whole movement thingy...
[DOUBLEPOST=1427671443,1427589731][/DOUBLEPOST]
Wulf just added the OnPlayerSleepEnded() Hook :3
Doesn't work like this thoCode:function PLUGIN:OnPlayerSleepEnded( arg, player ) local steamid = rust.UserIDFromPlayer(arg.connection.player) if afkplayers[steamid] then rust.BroadcastChat("AFK", player.displayName .. " is no longer AFK!") afkplayers[steamid] = nil end end
[DOUBLEPOST=1427671672][/DOUBLEPOST]Well.... The fukin problem could be that I didnt update yet XXDDDD -
I still need to add the hook to the documentation but the hook works perfectly as I tested it before adding it

The hook only takes one argument, the player.
Also you need to make sure you have the latest oxide installedCode:OnPlayerSleepEnded(player)
-
The rest is correct, isn't it?
-
Mostly, you'll just have to modify the parameter of on the function where you get the steamid:
rust.UserIDFromPlayer(arg.connection.player)
to
rust.UserIDFromPlayer(player)
Then it should work yes.
