Skip to content

Commit

Permalink
Merge pull request #3 from MaxWasUnavailable/dev
Browse files Browse the repository at this point in the history
Version 1.1.0
  • Loading branch information
MaxWasUnavailable authored Oct 20, 2024
2 parents 412ae8f + e69b23e commit 21feb36
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ AwayFromZomboid = {}
AwayFromZomboid.modName = "AwayFromZomboid"
AwayFromZomboid.modVersion = "1.0.0"
AwayFromZomboid.modAuthor = "Max"
AwayFromZomboid.additionalCredits = {"Vorshim92"}
AwayFromZomboid.modDescription = "AwayFromZomboid is a mod that adds AFK detection & management systems."

-- Mod variables
Expand All @@ -23,6 +24,10 @@ AwayFromZomboid.previousCheckTime = nil
AwayFromZomboid.isAFK = false
--- Late addition to the AFKTimer to prevent reset on manual AFK.
AwayFromZomboid.lateTimerAddition = 0
--- Flag to check whether the system is active.
AwayFromZomboid.isActive = false
--- Original OnCommandEntered function
AwayFromZomboid.originalOnCommandEntered = ISChat.onCommandEntered

-- Misc methods

Expand Down Expand Up @@ -53,7 +58,18 @@ end
---@param message string
---@return void
AwayFromZomboid.sendChatNotification = function(message)
processGeneralMessage(message)
local chatChannel = AwayFromZomboid.getChatNotificationChannel()
if chatChannel == 1 then
return
end
if chatChannel == 2 then
processSayMessage(message)
return
end
if chatChannel == 3 then
processGeneralMessage(message)
return
end
end

-- Fetch sandbox vars
Expand Down Expand Up @@ -113,6 +129,17 @@ AwayFromZomboid.getDoPopup = function()
return value
end

--- Get the chat channel to send AFK notifications to.
---@return number
AwayFromZomboid.getChatNotificationChannel = function()
local value = SandboxVars.AwayFromZomboid.ChatNotificationChannel
if value == nil then
value = 1
AwayFromZomboid.log("Chat Notification Channel value not found in sandbox variables. Using default value of " .. value .. ".")
end
return value
end

--- Get whether to enable the AFK kick system.
---@return boolean
AwayFromZomboid.getDoKick = function()
Expand Down Expand Up @@ -223,18 +250,18 @@ end
--- Popup the AFK message.
---@return void
AwayFromZomboid.AFKOnPopup = function()
HaloTextHelper.addText(getPlayer(), AwayFromZomboid.getAFKOnPopupMessage(), HaloTextHelper.getColorRed())
local message = AwayFromZomboid.getAFKOnPopupMessage()
if AwayFromZomboid.getDoKick() then
message = message .. " (Kick in " .. AwayFromZomboid.getAFKKickTimeout() .. " seconds)"
end
getPlayer():setHaloNote(AwayFromZomboid.getAFKOnPopupMessage(), 255, 0, 0, (SandboxVars.AwayFromZomboid.AFKKickTimeout*60)+500)
AwayFromZomboid.sendChatNotification(message)
end

--- Popup the not AFK message.
---@return void
AwayFromZomboid.AFKOffPopup = function()
HaloTextHelper.addText(getPlayer(), AwayFromZomboid.getAFKOffPopupMessage(), HaloTextHelper.getColorGreen())
getPlayer():setHaloNote(AwayFromZomboid.getAFKOffPopupMessage(), 0, 255, 0, 500)
AwayFromZomboid.sendChatNotification(AwayFromZomboid.getAFKOffPopupMessage())
end

Expand Down Expand Up @@ -301,17 +328,26 @@ AwayFromZomboid.incrementAFKHook = function()
AwayFromZomboid.previousCheckTime = currentTime
end

--- Handle manual AFK.
---@param chatMessage ChatMessage
---@param tabId number
---@return void
AwayFromZomboid.manualAFKHook = function(chatMessage, tabId)
if AwayFromZomboid.getAllowManualAFK() then
if chatMessage:getText() == "afk" and chatMessage:getAuthor() == getPlayer():getUsername() then
AwayFromZomboid.sendChatNotification("You will become AFK in ~" .. AwayFromZomboid.getManualAFKDelay() .. " seconds.")
AwayFromZomboid.lateTimerAddition = AwayFromZomboid.getAFKTimeout() - AwayFromZomboid.getManualAFKDelay()
end
AwayFromZomboid.customOnCommandEntered = function()
local command = ISChat.instance.textEntry:getText();
if command:lower() == "/afk" then
local message = "You will become AFK in ~" .. AwayFromZomboid.getManualAFKDelay() .. " seconds."
AwayFromZomboid.sendChatNotification(message)
getPlayer():setHaloNote(message, 255, 255, 0, 500)
ISChat.instance:unfocus();

AwayFromZomboid.lateTimerAddition = AwayFromZomboid.getAFKTimeout() - AwayFromZomboid.getManualAFKDelay()
return;
end
AwayFromZomboid.originalOnCommandEntered();
end

function ISChat:onCommandEntered()
if AwayFromZomboid.getAllowManualAFK() and AwayFromZomboid.isActive then
AwayFromZomboid.customOnCommandEntered();
return;
end
AwayFromZomboid.originalOnCommandEntered();
end

--- Register the reset hooks.
Expand All @@ -334,19 +370,55 @@ AwayFromZomboid.deRegisterActivityHooks = function(method)
Events.OnMouseUp.Remove(method)
end

-- Init
-- Activate & Deactivate

--- Initialize the mod and add event hooks.
--- Activate the AFK system.
---@return void
AwayFromZomboid.init = function()
AwayFromZomboid.activate = function()
if AwayFromZomboid.isActive then
AwayFromZomboid.log("AFK system already active.")
return
end

AwayFromZomboid.isActive = true

AwayFromZomboid.resetAFKTimer()
AwayFromZomboid.isAFK = false

AwayFromZomboid.registerActivityHooks(AwayFromZomboid.resetAFKTimer)

Events.EveryOneMinute.Add(AwayFromZomboid.incrementAFKHook)

Events.OnAddMessage.Add(AwayFromZomboid.manualAFKHook)
AwayFromZomboid.log("AFK system activated.")
end

--- Deactivate the AFK system.
---@return void
AwayFromZomboid.deactivate = function()
AwayFromZomboid.resetAFKTimer()
AwayFromZomboid.isAFK = false

AwayFromZomboid.deRegisterActivityHooks(AwayFromZomboid.resetAFKTimer)

Events.EveryOneMinute.Remove(AwayFromZomboid.incrementAFKHook)

AwayFromZomboid.log("AFK system deactivated. (Likely due to player death)")

AwayFromZomboid.isActive = false
end

-- Init

--- Initialize the mod and add event hooks.
---@return void
AwayFromZomboid.init = function()
if AwayFromZomboid.isMultiplayerClient() == false then
AwayFromZomboid.log("Mod is not running on a multiplayer client. Not initializing.")
return
end

Events.OnCreatePlayer.Add(AwayFromZomboid.activate)
Events.OnPlayerDeath.Add(AwayFromZomboid.deactivate)

AwayFromZomboid.log(AwayFromZomboid.modVersion .. " initialized.")
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ Sandbox_EN = {
Sandbox_AwayFromZomboid_DoPopup = "Do Popup",
Sandbox_AwayFromZomboid_DoPopup_tooltip = "Display a popup when a player is (or is no longer) AFK.",

Sandbox_AwayFromZomboid_ChatNotificationChannel_Enums_option1 = "None",
Sandbox_AwayFromZomboid_ChatNotificationChannel_Enums_option2 = "Say",
Sandbox_AwayFromZomboid_ChatNotificationChannel_Enums_option3 = "General",

Sandbox_AwayFromZomboid_ChatNotificationChannel = "Chat Notification Channel",
Sandbox_AwayFromZomboid_ChatNotificationChannel_tooltip = "The channel to send chat notifications to, if any.",

Sandbox_AwayFromZomboid_DoKick = "Do Kick",
Sandbox_AwayFromZomboid_DoKick_tooltip = "Kick the player when they are AFK, after the additional AFK kick timeout.",

Expand Down
12 changes: 12 additions & 0 deletions Contents/mods/AwayFromZomboid/media/sandbox-options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ option AwayFromZomboid.DoPopup
translation = AwayFromZomboid_DoPopup,
}

option AwayFromZomboid.ChatNotificationChannel
{
type = enum,
default = 1,
numValues = 3,

valueTranslation = AwayFromZomboid_ChatNotificationChannel_Enums,

page = AwayFromZomboid,
translation = AwayFromZomboid_ChatNotificationChannel,
}

option AwayFromZomboid.DoKick
{
type = boolean,
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# AwayFromZomboid

AwayFromZomboid is a highly configurable AFK mod for Project Zomboid.
AwayFromZomboid is a highly configurable AFK mod for Project Zomboid.

## Additional Credits
- **AFK kick during character creation fix** by **[Vorshim92](https://github.com/Vorshim92)**

0 comments on commit 21feb36

Please sign in to comment.