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.
-
)
-
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? -
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. -
-
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?
-
Ideally a C# plugin would be used using the OnPlayerInput hook though. -
can't I set the interval below 30?
-
-
So if I set it 5 it wont be a problem
-
-
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
Code: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 -
The hook only takes one argument, the player.
Code:OnPlayerSleepEnded(player)
-
The rest is correct, isn't it?
-
rust.UserIDFromPlayer(arg.connection.player)
to
rust.UserIDFromPlayer(player)
Then it should work yes.