local eventHandler = {} commandHandler = require("commandHandler") local consoleKickMessage = " has been kicked for using the console despite not having the permission to do so.\n" eventHandler.InitializeDefaultValidators = function() -- Don't validate object deletions for currently unusable containers (such as -- dying actors whose corpses players try to dispose of too early) -- -- Additionally, don't delete objects mentioned in config.disallowedDeleteRefIds customEventHooks.registerValidator("OnObjectDelete", function(eventStatus, pid, cellDescription, objects) local cell = LoadedCells[cellDescription] local unusableContainerUniqueIndexes = cell.unusableContainerUniqueIndexes for uniqueIndex, object in pairs(objects) do if tableHelper.containsValue(unusableContainerUniqueIndexes, uniqueIndex) then return customEventHooks.makeEventStatus(false, false) elseif tableHelper.containsValue(config.disallowedDeleteRefIds, object.refId) then tes3mp.LogAppend(enumerations.log.INFO, "- Rejected attempt at deleting " .. object.refId .. " " .. object.uniqueIndex .. " because it is disallowed in the server config") return customEventHooks.makeEventStatus(false, false) end end end) -- Don't create objects mentioned in config.disallowedCreateRefIds local defaultCreationValidator = function(eventStatus, pid, cellDescription, objects) for uniqueIndex, object in pairs(objects) do if tableHelper.containsValue(config.disallowedCreateRefIds, object.refId) then tes3mp.LogAppend(enumerations.log.INFO, "- Rejected attempt at creating " .. object.refId .. " " .. object.uniqueIndex .. " because it is disallowed in the server config") return customEventHooks.makeEventStatus(false, false) end end end customEventHooks.registerValidator("OnObjectPlace", defaultCreationValidator) customEventHooks.registerValidator("OnObjectSpawn", defaultCreationValidator) -- Don't validate scales larger than the maximum set in the config customEventHooks.registerValidator("OnObjectScale", function(eventStatus, pid, cellDescription, objects) for uniqueIndex, object in pairs(objects) do if object.scale >= config.maximumObjectScale then tes3mp.LogAppend(enumerations.log.INFO, "- Rejected attempt at setting scale of " .. object.refId .. " " .. object.uniqueIndex .. " to " .. object.scale .. " because it exceeds the server's " .. "maximum of " .. config.maximumObjectScale) return customEventHooks.makeEventStatus(false, false) end end end) -- Don't change lock levels for objects mentioned in config.disallowedLockRefIds customEventHooks.registerValidator("OnObjectLock", function(eventStatus, pid, cellDescription, objects, targetPlayers) for uniqueIndex, object in pairs(objects) do if tableHelper.containsValue(config.disallowedLockRefIds, object.refId) then tes3mp.LogAppend(enumerations.log.INFO, "- Rejected attempt at changing lock for " .. object.refId .. " " .. object.uniqueIndex .. " because it is disallowed in the server config") return customEventHooks.makeEventStatus(false, false) end end end) -- Don't change traps for objects mentioned in config.disallowedTrapRefIds customEventHooks.registerValidator("OnObjectTrap", function(eventStatus, pid, cellDescription, objects, targetPlayers) for uniqueIndex, object in pairs(objects) do if tableHelper.containsValue(config.disallowedTrapRefIds, object.refId) then tes3mp.LogAppend(enumerations.log.INFO, "- Rejected attempt at changing trap for " .. object.refId .. " " .. object.uniqueIndex .. " because it is disallowed in the server config") return customEventHooks.makeEventStatus(false, false) end end end) -- Don't change states for objects mentioned in config.disallowedStateRefIds customEventHooks.registerValidator("OnObjectState", function(eventStatus, pid, cellDescription, objects, targetPlayers) for uniqueIndex, object in pairs(objects) do if tableHelper.containsValue(config.disallowedStateRefIds, object.refId) then tes3mp.LogAppend(enumerations.log.INFO, "- Rejected attempt at changing state for " .. object.refId .. " " .. object.uniqueIndex .. " because it is disallowed in the server config") return customEventHooks.makeEventStatus(false, false) end end end) -- Don't change door states for objects mentioned in config.disallowedDoorStateRefIds customEventHooks.registerValidator("OnDoorState", function(eventStatus, pid, cellDescription, objects, targetPlayers) for uniqueIndex, object in pairs(objects) do if tableHelper.containsValue(config.disallowedDoorStateRefIds, object.refId) then tes3mp.LogAppend(enumerations.log.INFO, "- Rejected attempt at changing door state for " .. object.refId .. " " .. object.uniqueIndex .. " because it is disallowed in the server config") return customEventHooks.makeEventStatus(false, false) end end end) -- Don't activate objects that are supposed to already be deleted according to the -- server, preventing item duping -- -- Additionally, don't activate objects mentioned in config.disallowedActivateRefIds customEventHooks.registerValidator("OnObjectActivate", function(eventStatus, pid, cellDescription, objects, targetPlayers) for uniqueIndex, object in pairs(objects) do local debugMessage = "- Rejected attempt at activating " .. object.refId .. " " .. object.uniqueIndex .. " because it" local splitIndex = uniqueIndex:split("-") local refNum = tonumber(splitIndex[1]) local mpNum = tonumber(splitIndex[2]) -- If this is a preexisting object from the data files, make sure it doesn't -- have a Delete packet recorded for it if refNum ~= 0 and tableHelper.containsValue(LoadedCells[cellDescription].data.packets.delete, uniqueIndex) then tes3mp.LogAppend(enumerations.log.INFO, debugMessage .. " is a preexisting object that is already " .. "tracked as being deleted") return customEventHooks.makeEventStatus(false, false) elseif mpNum ~= 0 and LoadedCells[cellDescription].data.objectData[uniqueIndex] == nil then tes3mp.LogAppend(enumerations.log.INFO, debugMessage .. " is a server-created object that is " .. "no longer supposed to exist") return customEventHooks.makeEventStatus(false, false) elseif tableHelper.containsValue(config.disallowedActivateRefIds, object.refId) then tes3mp.LogAppend(enumerations.log.INFO, debugMessage .. " is disallowed in the server config") return customEventHooks.makeEventStatus(false, false) end end end) -- Ignore packets with global variables that are listed under clientVariableScopes.globals.ignored customEventHooks.registerValidator("OnClientScriptGlobal", function(eventStatus, pid, variables) for id, variable in pairs(variables) do if tableHelper.containsValue(clientVariableScopes.globals.ignored, id) then tes3mp.LogAppend(enumerations.log.INFO, "- Ignoring attempt at setting global variable " .. id .. " because it is listed as an ignored variable in clientVariableScopes") return customEventHooks.makeEventStatus(false, false) end end end) end eventHandler.InitializeDefaultHandlers = function() -- Upon receiving an actor death: -- 1) Add 1 to the kill count for its ID and send it to players -- 2) Add it to the cell's currently unusable containers -- 3) Request its container -- Note: points 2 and 3 are temporary and will be handled better when -- servers load up .esm data by default. customEventHooks.registerHandler("OnActorDeath", function(eventStatus, pid, cellDescription, actors) local cell = LoadedCells[cellDescription] tes3mp.ClearKillChanges() for uniqueIndex, actor in pairs(actors) do if WorldInstance.data.kills[actor.refId] == nil then WorldInstance.data.kills[actor.refId] = 0 end WorldInstance.data.kills[actor.refId] = WorldInstance.data.kills[actor.refId] + 1 tes3mp.AddKill(actor.refId, WorldInstance.data.kills[actor.refId]) table.insert(cell.unusableContainerUniqueIndexes, uniqueIndex) end tes3mp.SendWorldKillCount(pid, true) cell:RequestContainers(pid, tableHelper.getArrayFromIndexes(actors)) end) -- Upon accepting an object placement, request its container if it has one customEventHooks.registerHandler("OnObjectPlace", function(eventStatus, pid, cellDescription, objects) local containerUniqueIndexesRequested = {} local cell = LoadedCells[cellDescription] for uniqueIndex, object in pairs(objects) do if object.hasContainer == true then table.insert(containerUniqueIndexesRequested, uniqueIndex) end end if not tableHelper.isEmpty(containerUniqueIndexesRequested) then cell:RequestContainers(pid, containerUniqueIndexesRequested) end end) -- Upon accepting an object spawn, request its container customEventHooks.registerHandler("OnObjectSpawn", function(eventStatus, pid, cellDescription, objects) local cell = LoadedCells[cellDescription] cell:RequestContainers(pid, tableHelper.getArrayFromIndexes(objects)) end) -- Don't allow state spam from clients customEventHooks.registerHandler("OnObjectState", function(eventStatus, pid, cellDescription, objects) local cell = LoadedCells[cellDescription] for uniqueIndex, object in pairs(objects) do if object.state == false then local player = Players[pid] if player.stateSpam == nil then player.stateSpam = {} end -- Track the number of ObjectState packets received from this player that have attempted -- to disable this object if player.stateSpam[uniqueIndex] == nil then player.stateSpam[uniqueIndex] = 0 else player.stateSpam[uniqueIndex] = player.stateSpam[uniqueIndex] + 1 -- Kick a player that continues the spam if player.stateSpam[uniqueIndex] >= 25 then player:Kick() tes3mp.LogAppend(enumerations.log.INFO, "- Kicked player " .. logicHandler.GetChatName(pid) .. " for continuing state spam") -- If the player has sent 5 false object states for the same uniqueIndex, delete the object elseif player.stateSpam[uniqueIndex] >= 5 then logicHandler.DeleteObjectForPlayer(pid, cellDescription, uniqueIndex) tes3mp.LogAppend(enumerations.log.INFO, "- Deleting state spam object") end end end end end) -- Print object activations and send an ObjectActivate packet back to the player customEventHooks.registerHandler("OnObjectActivate", function(eventStatus, pid, cellDescription, objects, targetPlayers) if eventStatus.validDefaultHandler == false then return end local debugMessage = nil for uniqueIndex, object in pairs(objects) do debugMessage = "- " debugMessage = debugMessage .. uniqueIndex .. " has been activated by " if object.activatingPid == nil then debugMessage = debugMessage .. object.activatingRefId .. " " .. object.activatingUniqueIndex else debugMessage = debugMessage .. logicHandler.GetChatName(object.activatingPid) end tes3mp.LogAppend(enumerations.log.INFO, debugMessage) end for targetPid, targetPlayer in pairs(targetPlayers) do debugMessage = "- " debugMessage = debugMessage .. logicHandler.GetChatName(targetPid) .. " has been activated by " if targetPlayer.activatingPid == nil then debugMessage = debugMessage .. targetPlayer.activatingRefId .. " " .. targetPlayer.activatingUniqueIndex else debugMessage = debugMessage .. logicHandler.GetChatName(targetPlayer.activatingPid) end tes3mp.LogAppend(enumerations.log.INFO, debugMessage) end tes3mp.CopyReceivedObjectListToStore() -- Objects can't be activated clientside without the server's approval, so we send -- the packet back to the player who sent it, but we avoid sending it to other -- players because OpenMW barely has any code for handling activations not from -- the local player -- i.e. sendToOtherPlayers is false and skipAttachedPlayer is false tes3mp.SendObjectActivate(false, false) end) -- Print object hits customEventHooks.registerHandler("OnObjectHit", function(eventStatus, pid, cellDescription, objects, targetPlayers) if eventStatus.validDefaultHandler == false then return end local debugMessage = nil for uniqueIndex, object in pairs(objects) do debugMessage = "- " if object.hittingPid == nil then debugMessage = debugMessage .. object.hittingRefId .. " " .. object.hittingUniqueIndex else debugMessage = debugMessage .. logicHandler.GetChatName(object.hittingPid) end if object.hit.success == true then debugMessage = debugMessage .. " has successfully hit " else debugMessage = debugMessage .. " has missed hitting " end debugMessage = debugMessage .. object.refId .. " " .. uniqueIndex tes3mp.LogAppend(enumerations.log.INFO, debugMessage) end for targetPid, targetPlayer in pairs(targetPlayers) do debugMessage = "- " if targetPlayer.hittingPid == nil then debugMessage = debugMessage .. targetPlayer.hittingRefId .. " " .. targetPlayer.hittingUniqueIndex else debugMessage = debugMessage .. logicHandler.GetChatName(targetPlayer.hittingPid) end if targetPlayer.hit.success == true then debugMessage = debugMessage .. " has successfully hit " else debugMessage = debugMessage .. " has missed hitting " end debugMessage = debugMessage .. logicHandler.GetChatName(targetPid) tes3mp.LogAppend(enumerations.log.VERBOSE, debugMessage) end end) -- Print object sounds and send an ObjectSound packet to other players customEventHooks.registerHandler("OnObjectSound", function(eventStatus, pid, cellDescription, objects, targetPlayers) if eventStatus.validDefaultHandler == false then return end local debugMessage = nil for uniqueIndex, object in pairs(objects) do debugMessage = "- " .. uniqueIndex .. " played sound " .. object.soundId tes3mp.LogAppend(enumerations.log.INFO, debugMessage) end for targetPid, targetPlayer in pairs(targetPlayers) do debugMessage = "- " .. logicHandler.GetChatName(targetPid) .. " played sound " .. targetPlayer.soundId tes3mp.LogAppend(enumerations.log.INFO, debugMessage) end tes3mp.CopyReceivedObjectListToStore() -- Sounds are played unilaterally clientside before being sent to the server, so we -- send the packet to other players, but we avoid sending it to the original player -- i.e. sendToOtherPlayers is true and skipAttachedPlayer is true tes3mp.SendObjectSound(true, true) end) -- Print object restocking and send an ObjectRestock packet back to the player customEventHooks.registerHandler("OnObjectRestock", function(eventStatus, pid, cellDescription, objects, targetPlayers) if eventStatus.validDefaultHandler == false then return end local debugMessage = nil for uniqueIndex, object in pairs(objects) do tes3mp.LogAppend(enumerations.log.INFO, "- Accepting restock request for " .. object.refId .. " " .. uniqueIndex) end tes3mp.CopyReceivedObjectListToStore() -- Objects can't be restocked clientside without the server's approval, so we send -- the packet back to the player who sent it, but we avoid sending it to other -- players because the Container packet resulting from the restocking will get -- sent to them instead -- i.e. sendToOtherPlayers is false and skipAttachedPlayer is false tes3mp.SendObjectRestock(false, false) end) end eventHandler.OnPlayerConnect = function(pid, playerName) tes3mp.SetDifficulty(pid, config.difficulty) tes3mp.SetConsoleAllowed(pid, config.allowConsole) tes3mp.SetBedRestAllowed(pid, config.allowBedRest) tes3mp.SetWildernessRestAllowed(pid, config.allowWildernessRest) tes3mp.SetWaitAllowed(pid, config.allowWait) tes3mp.SetPhysicsFramerate(pid, config.physicsFramerate) tes3mp.SetEnforcedLogLevel(pid, config.enforcedLogLevel) tes3mp.SendSettings(pid) logicHandler.SendClientScriptDisables(pid, false) logicHandler.SendClientScriptSettings(pid, false) tes3mp.SetPlayerCollisionState(config.enablePlayerCollision) tes3mp.SetActorCollisionState(config.enableActorCollision) tes3mp.SetPlacedObjectCollisionState(config.enablePlacedObjectCollision) tes3mp.UseActorCollisionForPlacedObjects(config.useActorCollisionForPlacedObjects) logicHandler.SendConfigCollisionOverrides(pid, false) WorldInstance:LoadTime(pid, false) Players[pid] = Player(pid, playerName) Players[pid].name = playerName local eventStatus = customEventHooks.triggerValidators("OnPlayerConnect", {pid}) if eventStatus.validDefaultHandler then local message = logicHandler.GetChatName(pid) .. " has joined the server" local ipAddress = tes3mp.GetIP(pid) Players[pid].ipAddress = ipAddress if pidsByIpAddress[ipAddress] == nil then pidsByIpAddress[ipAddress] = {} end if not tableHelper.isEmpty(pidsByIpAddress[ipAddress]) then local otherPlayerNames = {} for _, otherPid in pairs(pidsByIpAddress[ipAddress]) do table.insert(otherPlayerNames, logicHandler.GetChatName(otherPid)) end message = message .. ", from the same IP address as " .. tableHelper.concatenateArrayValues(otherPlayerNames, 1, ", ") end message = message .. ".\n" tes3mp.SendMessage(pid, message, true) if tableHelper.getCount(pidsByIpAddress[ipAddress]) + 1 > config.maxClientsPerIP then message = logicHandler.GetChatName(pid) .. " has been kicked because this server allows a maximum of " .. config.maxClientsPerIP .. " clients from the same IP address.\n" tes3mp.SendMessage(pid, message, true) tes3mp.Kick(pid) Players[pid] = nil return else table.insert(pidsByIpAddress[ipAddress], pid) end message = "Welcome " .. playerName .. "\nYou have " .. tostring(config.loginTime) .. " seconds to" if Players[pid]:HasAccount() then message = message .. " log in.\n" guiHelper.ShowLogin(pid) else message = message .. " register.\n" guiHelper.ShowRegister(pid) end tes3mp.SendMessage(pid, message, false) Players[pid].loginTimerId = tes3mp.CreateTimerEx("OnLoginTimeExpiration", time.seconds(config.loginTime), "is", pid, Players[pid].accountName) tes3mp.StartTimer(Players[pid].loginTimerId) end customEventHooks.triggerHandlers("OnPlayerConnect", eventStatus, {pid}) end eventHandler.OnPlayerDisconnect = function(pid) local message = logicHandler.GetChatName(pid) .. " has left the server.\n" tes3mp.SendMessage(pid, message, true) -- If this player has disconnected before properly logging in, remove their pid -- from the table tracking IP addresses if tes3mp.GetIP(pid) == "UNASSIGNED_SYSTEM_ADDRESS" then for ipAddress, pids in pairs(pidsByIpAddress) do if tableHelper.containsValue(pids, pid) then tableHelper.removeValue(pids, pid) end end end if Players[pid] ~= nil then if Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerDisconnect", {pid}) if eventStatus.validDefaultHandler then local ipAddress = Players[pid].ipAddress if tableHelper.containsValue(pidsByIpAddress[ipAddress], pid) then tableHelper.removeValue(pidsByIpAddress[ipAddress], pid) end Players[pid]:DeleteSummons() -- Was this player confiscating from someone? If so, clear that if Players[pid].confiscationTargetName ~= nil then local targetName = Players[pid].confiscationTargetName local targetPlayer = logicHandler.GetPlayerByName(targetName) targetPlayer:SetConfiscationState(false) end Players[pid]:SaveCell() Players[pid]:SaveStatsDynamic() tes3mp.LogMessage(enumerations.log.INFO, "Saving player " .. logicHandler.GetChatName(pid)) Players[pid]:SaveToDrive() -- Unload every cell for this player for index, loadedCellDescription in pairs(Players[pid].cellsLoaded) do local eventStatus = customEventHooks.triggerValidators("OnCellUnload", {pid, loadedCellDescription}) if eventStatus.validDefaultHandler then logicHandler.UnloadCellForPlayer(pid, loadedCellDescription) end customEventHooks.triggerHandlers("OnCellUnload", eventStatus, {pid, loadedCellDescription}) end if Players[pid].data.location.regionName ~= nil then logicHandler.UnloadRegionForPlayer(pid, Players[pid].data.location.regionName) end Players[pid]:Destroy() Players[pid] = nil end customEventHooks.triggerHandlers("OnPlayerDisconnect", eventStatus, {pid}) end end -- If the server is now empty, quick saving of data isn't important anymore, so do a slower save of -- the world and record store data to human-readable JSON if tableHelper.isEmpty(Players) then WorldInstance:SaveToDrive() for _, recordStore in pairs(RecordStores) do recordStore:DeleteUnlinkedRecords() recordStore:SaveToDrive() end end end eventHandler.OnGUIAction = function(pid, idGui, data) if Players[pid] ~= nil then data = tostring(data) -- data can be numeric, but we should convert it to a string local eventStatus = customEventHooks.triggerValidators("OnGUIAction", {pid, idGui, data}) if eventStatus.validDefaultHandler then if Players[pid]:IsLoggedIn() then if idGui == config.customMenuIds.confiscate and Players[pid].confiscationTargetName ~= nil then local targetName = Players[pid].confiscationTargetName local targetPlayer = logicHandler.GetPlayerByName(targetName) -- Because the window's item index starts from 0 while the Lua table for -- inventories starts from 1, adjust the former here local inventoryItemIndex = data + 1 local item = targetPlayer.data.inventory[inventoryItemIndex] if item ~= nil then inventoryHelper.addItem(Players[pid].data.inventory, item.refId, item.count, item.charge, item.enchantmentCharge, item.soul) Players[pid]:LoadItemChanges({item}, enumerations.inventory.ADD) -- If the item is equipped by the target, unequip it first if inventoryHelper.containsItem(targetPlayer.data.equipment, item.refId, item.charge) then local equipmentItemIndex = inventoryHelper.getItemIndex(targetPlayer.data.equipment, item.refId, item.charge) targetPlayer.data.equipment[equipmentItemIndex] = nil end targetPlayer.data.inventory[inventoryItemIndex] = nil tableHelper.cleanNils(targetPlayer.data.inventory) Players[pid]:Message("You've confiscated " .. item.refId .. " from " .. targetName .. "\n") if targetPlayer:IsLoggedIn() then targetPlayer:LoadItemChanges({item}, enumerations.inventory.REMOVE) end else Players[pid]:Message("Invalid item index\n") end targetPlayer:SetConfiscationState(false) targetPlayer:QuicksaveToDrive() Players[pid].confiscationTargetName = nil elseif idGui == config.customMenuIds.menuHelper and Players[pid].currentCustomMenu ~= nil then local buttonIndex = tonumber(data) + 1 local buttonPressed = Players[pid].displayedMenuButtons[buttonIndex] local destination = menuHelper.GetButtonDestination(pid, buttonPressed) Players[pid].previousCustomMenu = Players[pid].currentCustomMenu menuHelper.ProcessEffects(pid, destination.effects) if destination.targetMenu ~= nil then menuHelper.DisplayMenu(pid, destination.targetMenu) Players[pid].currentCustomMenu = destination.targetMenu end end else if idGui == guiHelper.ID.LOGIN then if data == nil then Players[pid]:Message("Incorrect password!\n") guiHelper.ShowLogin(pid) return true end Players[pid]:LoadFromDrive() local passwordSalt = Players[pid].data.login.passwordSalt if Players[pid].data.login.passwordHash ~= tes3mp.GetSHA256Hash(data .. passwordSalt) then Players[pid]:Message("Incorrect password!\n") guiHelper.ShowLogin(pid) return true end -- Is this player on the banlist? If so, store their new IP and ban them if tableHelper.containsValue(banList.playerNames, string.lower(Players[pid].accountName)) == true then Players[pid]:SaveIpAddress() Players[pid]:Message(Players[pid].accountName .. " is banned from this server.\n") tes3mp.BanAddress(tes3mp.GetIP(pid)) else Players[pid]:FinishLogin() Players[pid]:Message("You have successfully logged in.\n" .. config.chatWindowInstructions) end elseif idGui == guiHelper.ID.REGISTER then if data == nil then Players[pid]:Message("Password can not be empty\n") guiHelper.ShowRegister(pid) return true end Players[pid]:Register(data) Players[pid]:Message("You have successfully registered.\n" .. config.chatWindowInstructions) end end end customEventHooks.triggerHandlers("OnGUIAction", eventStatus, {pid, idGui, data}) end return false end eventHandler.OnPlayerSendMessage = function(pid, message) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then tes3mp.LogMessage(enumerations.log.INFO, logicHandler.GetChatName(pid) .. ": " .. message) local eventStatus = customEventHooks.triggerValidators("OnPlayerSendMessage", {pid, message}) if eventStatus.validDefaultHandler then -- Is this a chat command? If so, pass it over to the commandHandler if message:sub(1, 1) == '/' then local command = (message:sub(2, #message)):split(" ") commandHandler.ProcessCommand(pid, command) else local message = color.White .. logicHandler.GetChatName(pid) .. ": " .. message .. "\n" -- Check for chat overrides that add extra text if Players[pid]:IsServerStaff() then if Players[pid]:IsServerOwner() then message = config.rankColors.serverOwner .. "[Owner] " .. message elseif Players[pid]:IsAdmin() then message = config.rankColors.admin .. "[Admin] " .. message elseif Players[pid]:IsModerator() then message = config.rankColors.moderator .. "[Mod] " .. message end end tes3mp.SendMessage(pid, message, true) end end customEventHooks.triggerHandlers("OnPlayerSendMessage", eventStatus, {pid, message}) end end eventHandler.OnPlayerDeath = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerDeath", {pid}) if eventStatus.validDefaultHandler then Players[pid]:ProcessDeath() end customEventHooks.triggerHandlers("OnPlayerDeath", eventStatus, {pid}) end end eventHandler.OnPlayerAttribute = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerAttribute", {pid}) if eventStatus.validDefaultHandler then Players[pid]:SaveAttributes() end customEventHooks.triggerHandlers("OnPlayerAttribute", eventStatus, {pid}) end end eventHandler.OnPlayerSkill = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerSkill", {pid}) if eventStatus.validDefaultHandler then Players[pid]:SaveSkills() end customEventHooks.triggerHandlers("OnPlayerSkill", eventStatus, {pid}) end end eventHandler.OnPlayerLevel = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerLevel", {pid}) if eventStatus.validDefaultHandler then Players[pid]:SaveLevel() Players[pid]:SaveStatsDynamic() end customEventHooks.triggerHandlers("OnPlayerLevel", eventStatus, {pid}) end end eventHandler.OnPlayerShapeshift = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerShapeshift", {pid}) if eventStatus.validDefaultHandler then Players[pid]:SaveShapeshift() end customEventHooks.triggerHandlers("OnPlayerShapeshift", eventStatus, {pid}) end end eventHandler.OnPlayerCellChange = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local currentCellDescription = tes3mp.GetCell(pid) if not tableHelper.containsValue(config.forbiddenCells, currentCellDescription) then local previousCellDescription = Players[pid].data.location.cell local eventStatus = customEventHooks.triggerValidators("OnPlayerCellChange", {pid, previousCellDescription, currentCellDescription}) if eventStatus.validDefaultHandler then -- If this player is changing their region, add them to the visitors of the new -- region while removing them from the visitors of their old region if tes3mp.IsChangingRegion(pid) then local regionName = string.lower(tes3mp.GetRegion(pid)) if regionName ~= "" then local debugMessage = logicHandler.GetChatName(pid) .. " has " local hasFinishedInitialTeleportation = Players[pid].hasFinishedInitialTeleportation local previousCellIsStillLoaded = tableHelper.containsValue(Players[pid].cellsLoaded, previousCellDescription) -- It's possible we've been teleported to a cell we had already loaded when -- spawning on the server, so also check whether this is the player's first -- cell change since joining local isTeleported = not previousCellIsStillLoaded or not hasFinishedInitialTeleportation if isTeleported then debugMessage = debugMessage .. "teleported" else debugMessage = debugMessage .. "walked" end debugMessage = debugMessage .. " to region " .. regionName .. "\n" tes3mp.LogMessage(enumerations.log.INFO, debugMessage) logicHandler.LoadRegionForPlayer(pid, regionName, isTeleported) end local previousRegionName = Players[pid].data.location.regionName if previousRegionName ~= nil and previousRegionName ~= regionName then logicHandler.UnloadRegionForPlayer(pid, previousRegionName) end Players[pid].data.location.regionName = regionName Players[pid].hasFinishedInitialTeleportation = true end Players[pid]:SaveCell() Players[pid]:SaveStatsDynamic() Players[pid]:QuicksaveToDrive() -- Exchange generated records with the other players who have this cell loaded if LoadedCells[currentCellDescription] ~= nil then logicHandler.ExchangeGeneratedRecords(pid, LoadedCells[currentCellDescription].visitors) end if config.shareMapExploration == true then WorldInstance:SaveMapExploration(pid) WorldInstance:QuicksaveToDrive() end end customEventHooks.triggerHandlers("OnPlayerCellChange", eventStatus, {pid, previousCellDescription, currentCellDescription}) else Players[pid].data.location.posX = tes3mp.GetPreviousCellPosX(pid) Players[pid].data.location.posY = tes3mp.GetPreviousCellPosY(pid) Players[pid].data.location.posZ = tes3mp.GetPreviousCellPosZ(pid) Players[pid]:LoadCell() tes3mp.MessageBox(pid, -1, "You are forbidden from entering that area.") end end end eventHandler.OnPlayerEndCharGen = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerEndCharGen", {pid}) if eventStatus.validDefaultHandler then Players[pid]:EndCharGen() end customEventHooks.triggerHandlers("OnPlayerEndCharGen", eventStatus, {pid}) customEventHooks.triggerHandlers("OnPlayerAuthentified", eventStatus, {pid}) end end eventHandler.OnPlayerEquipment = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerEquipment", {pid}) if eventStatus.validDefaultHandler then Players[pid]:SaveEquipment() end customEventHooks.triggerHandlers("OnPlayerEquipment", eventStatus, {pid}) end end eventHandler.OnPlayerInventory = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerInventory", {pid}) if eventStatus.validDefaultHandler then Players[pid]:SaveInventory() end customEventHooks.triggerHandlers("OnPlayerInventory", eventStatus, {pid}) end end eventHandler.OnPlayerSpellbook = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerSpellbook", {pid}) if eventStatus.validDefaultHandler then Players[pid]:SaveSpellbook() end customEventHooks.triggerHandlers("OnPlayerSpellbook", eventStatus, {pid}) end end eventHandler.OnPlayerSpellsActive = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerSpellsActive", {pid}) if eventStatus.validDefaultHandler then Players[pid]:SaveSpellsActive() end customEventHooks.triggerHandlers("OnPlayerSpellsActive", eventStatus, {pid}) end end eventHandler.OnPlayerQuickKeys = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerQuickKeys", {pid}) if eventStatus.validDefaultHandler then Players[pid]:SaveQuickKeys() end customEventHooks.triggerHandlers("OnPlayerQuickKeys", eventStatus, {pid}) end end eventHandler.OnPlayerJournal = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local journalItemArray = packetReader.GetPlayerJournalItemArray(pid) local eventStatus = customEventHooks.triggerValidators("OnPlayerJournal", {pid, journalItemArray}) if eventStatus.validDefaultHandler then if config.shareJournal == true then WorldInstance:SaveJournal(journalItemArray) -- Send this PlayerJournal packet to other players (sendToOthersPlayers is true), -- but skip sending it to the player we got it from (skipAttachedPlayer is true) tes3mp.SendJournalChanges(pid, true, true) else Players[pid]:SaveJournal(journalItemArray) end end customEventHooks.triggerHandlers("OnPlayerJournal", eventStatus, {pid, journalItemArray}) end end eventHandler.OnPlayerFaction = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local action = tes3mp.GetFactionChangesAction(pid) local eventStatus = customEventHooks.triggerValidators("OnPlayerFaction", {pid, action}) if eventStatus.validDefaultHandler then if action == enumerations.faction.RANK then if config.shareFactionRanks == true then WorldInstance:SaveFactionRanks(pid) -- Send this PlayerFaction packet to other players (sendToOthersPlayers is true), -- but skip sending it to the player we got it from (skipAttachedPlayer is true) tes3mp.SendFactionChanges(pid, true, true) else Players[pid]:SaveFactionRanks() end elseif action == enumerations.faction.EXPULSION then if config.shareFactionExpulsion == true then WorldInstance:SaveFactionExpulsion(pid) -- As above, send this to everyone other than the original sender tes3mp.SendFactionChanges(pid, true, true) else Players[pid]:SaveFactionExpulsion() end elseif action == enumerations.faction.REPUTATION then if config.shareFactionReputation == true then WorldInstance:SaveFactionReputation(pid) -- As above, send this to everyone other than the original sender tes3mp.SendFactionChanges(pid, true, true) else Players[pid]:SaveFactionReputation() end end end customEventHooks.triggerHandlers("OnPlayerFaction", eventStatus, {pid, action}) end end eventHandler.OnPlayerTopic = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerTopic", {pid}) if eventStatus.validDefaultHandler then if config.shareTopics == true then WorldInstance:SaveTopics(pid) -- Send this PlayerTopic packet to other players (sendToOthersPlayers is true), -- but skip sending it to the player we got it from (skipAttachedPlayer is true) tes3mp.SendTopicChanges(pid, true, true) else Players[pid]:SaveTopics() end end customEventHooks.triggerHandlers("OnPlayerTopic", eventStatus, {pid}) end end eventHandler.OnPlayerBounty = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerBounty", {pid}) if eventStatus.validDefaultHandler then if config.shareBounty == true then WorldInstance:SaveBounty(pid) -- Bounty packets are special in that they are always sent -- to all players, but only affect their target player on -- any given client -- -- To set the same bounty for each LocalPlayer, we need -- to separately set each player as the target and -- send the packet local bountyValue = tes3mp.GetBounty(pid) for playerIndex, player in pairs(Players) do if player.pid ~= pid then tes3mp.SetBounty(player.pid, bountyValue) tes3mp.SendBounty(player.pid) end end else Players[pid]:SaveBounty() end end customEventHooks.triggerHandlers("OnPlayerBounty", eventStatus, {pid}) end end eventHandler.OnPlayerReputation = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerReputation", {pid}) if eventStatus.validDefaultHandler then if config.shareReputation == true then WorldInstance:SaveReputation(pid) -- Send this PlayerReputation packet to other players (sendToOthersPlayers is true), -- but skip sending it to the player we got it from (skipAttachedPlayer is true) tes3mp.SendReputation(pid, true, true) else Players[pid]:SaveReputation() end end customEventHooks.triggerHandlers("OnPlayerReputation", eventStatus, {pid}) end end eventHandler.OnPlayerBook = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnPlayerBook", {pid}) if eventStatus.validDefaultHandler then Players[pid]:AddBooks() end customEventHooks.triggerHandlers("OnPlayerBook", eventStatus, {pid}) end end eventHandler.OnPlayerItemUse = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local itemRefId = tes3mp.GetUsedItemRefId(pid) local eventStatus = customEventHooks.triggerValidators("OnPlayerItemUse", {pid, itemRefId}) if eventStatus.validDefaultHandler then tes3mp.LogMessage(enumerations.log.INFO, logicHandler.GetChatName(pid) .. " used inventory item " .. itemRefId) -- Unilateral use of items is disabled on clients, so we need to send -- this packet back to the player before they can use the item tes3mp.SendItemUse(pid) end customEventHooks.triggerHandlers("OnPlayerItemUse", eventStatus, {pid, itemRefId}) end end eventHandler.OnPlayerMiscellaneous = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local changeType = tes3mp.GetMiscellaneousChangeType(pid) if changeType == enumerations.miscellaneous.MARK_LOCATION then local eventStatus = customEventHooks.triggerValidators("OnPlayerMarkLocation", {pid}) if eventStatus.validDefaultHandler then Players[pid]:SaveMarkLocation() end customEventHooks.triggerHandlers("OnPlayerMarkLocation", eventStatus, {pid}) elseif changeType == enumerations.miscellaneous.SELECTED_SPELL then local eventStatus = customEventHooks.triggerValidators("OnPlayerSelectedSpell", {pid}) if eventStatus.validDefaultHandler then Players[pid]:SaveSelectedSpell() end customEventHooks.triggerHandlers("OnPlayerSelectedSpell", eventStatus, {pid}) end end end eventHandler.OnCellLoad = function(pid, cellDescription) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnCellLoad", {pid, cellDescription}) if eventStatus.validDefaultHandler then logicHandler.LoadCellForPlayer(pid, cellDescription) end customEventHooks.triggerHandlers("OnCellLoad", eventStatus, {pid, cellDescription}) else tes3mp.LogMessage(enumerations.log.WARN, "Undefined behavior: invalid player " .. pid .. " loaded cell " .. cellDescription) end end eventHandler.OnCellUnload = function(pid, cellDescription) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnCellUnload", {pid, cellDescription}) if eventStatus.validDefaultHandler then logicHandler.UnloadCellForPlayer(pid, cellDescription) end customEventHooks.triggerHandlers("OnCellUnload", eventStatus, {pid, cellDescription}) end end eventHandler.OnCellDeletion = function(cellDescription) local eventStatus = customEventHooks.triggerValidators("OnCellDeletion", {cellDescription}) if eventStatus.validDefaultHandler then logicHandler.UnloadCell(cellDescription) end customEventHooks.triggerHandlers("OnCellDeletion", eventStatus, {cellDescription}) end eventHandler.OnGenericActorEvent = function(pid, cellDescription, packetType) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then if LoadedCells[cellDescription] ~= nil then tes3mp.ReadReceivedActorList() local actors = packetReader.GetActorPacketTables(packetType).actors local eventStatus = customEventHooks.triggerValidators("On" .. packetType, {pid, cellDescription, actors}) if eventStatus.validDefaultHandler then tes3mp.LogMessage(enumerations.log.INFO, "Saving " .. packetType .. " from " .. logicHandler.GetChatName(pid) .. " about " .. cellDescription) LoadedCells[cellDescription]:SaveActorsByPacketType(packetType, actors) end customEventHooks.triggerHandlers("On" .. packetType, eventStatus, {pid, cellDescription, actors}) else tes3mp.LogMessage(enumerations.log.WARN, "Undefined behavior: " .. logicHandler.GetChatName(pid) .. " sent " .. packetType .. " for unloaded " .. cellDescription) end else tes3mp.Kick(pid) end end eventHandler.OnActorList = function(pid, cellDescription) eventHandler.OnGenericActorEvent(pid, cellDescription, "ActorList") end eventHandler.OnActorEquipment = function(pid, cellDescription) eventHandler.OnGenericActorEvent(pid, cellDescription, "ActorEquipment") end eventHandler.OnActorAI = function(pid, cellDescription) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then if LoadedCells[cellDescription] ~= nil then local eventStatus = customEventHooks.triggerValidators("OnActorAI", {pid, cellDescription}) if eventStatus.validDefaultHandler then tes3mp.ReadReceivedActorList() tes3mp.CopyReceivedActorListToStore() -- Actor AI packages are currently enabled unilaterally on the client -- that has sent them, so we only need to send them to other players, -- and can skip the original sender -- i.e. sendToOtherVisitors is true and skipAttachedPlayer is true tes3mp.SendActorAI(true, true) end customEventHooks.triggerHandlers("OnActorAI", eventStatus, {pid, cellDescription}) else tes3mp.LogMessage(enumerations.log.WARN, "Undefined behavior: " .. logicHandler.GetChatName(pid) .. " sent ActorAI for unloaded " .. cellDescription) end else tes3mp.Kick(pid) end end eventHandler.OnActorDeath = function(pid, cellDescription) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then if LoadedCells[cellDescription] ~= nil then tes3mp.ReadReceivedActorList() local actors = packetReader.GetActorPacketTables("death").actors local eventStatus = customEventHooks.triggerValidators("OnActorDeath", {pid, cellDescription, actors}) if eventStatus.validDefaultHandler then tes3mp.LogMessage(enumerations.log.INFO, "Saving ActorDeath from " .. logicHandler.GetChatName(pid) .. " about " .. cellDescription) for uniqueIndex, actor in pairs(actors) do local deathReason = "committed suicide" local debugMessage = "- " .. uniqueIndex .. ", deathReason: " if actor.killerPid ~= nil then deathReason = "killed by player " .. logicHandler.GetChatName(actor.killerPid) elseif actor.killerName ~= "" then deathReason = "killed by actor " .. actor.killerRefId .. " " .. actor.killerUniqueIndex end tes3mp.LogAppend(enumerations.log.INFO, debugMessage .. deathReason) end LoadedCells[cellDescription]:SaveActorsByPacketType("ActorDeath", actors) end customEventHooks.triggerHandlers("OnActorDeath", eventStatus, {pid, cellDescription, actors}) else tes3mp.LogMessage(enumerations.log.WARN, "Undefined behavior: " .. logicHandler.GetChatName(pid) .. " sent ActorDeath for unloaded " .. cellDescription) end end end eventHandler.OnActorCellChange = function(pid, cellDescription) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then if LoadedCells[cellDescription] ~= nil then local eventStatus = customEventHooks.triggerValidators("OnActorCellChange", {pid, cellDescription}) if eventStatus.validDefaultHandler then LoadedCells[cellDescription]:SaveActorCellChanges(pid) end customEventHooks.triggerHandlers("OnActorCellChange", eventStatus, {pid, cellDescription}) else tes3mp.LogMessage(enumerations.log.WARN, "Undefined behavior: " .. logicHandler.GetChatName(pid) .. " sent ActorCellChange for unloaded " .. cellDescription) end else tes3mp.Kick(pid) end end eventHandler.OnGenericObjectEvent = function(pid, cellDescription, packetType) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then tes3mp.ReadReceivedObjectList() local packetOrigin = tes3mp.GetObjectListOrigin() tes3mp.LogAppend(enumerations.log.INFO, "- packetOrigin was " .. tableHelper.getIndexByValue(enumerations.packetOrigin, packetOrigin)) if logicHandler.IsPacketFromConsole(packetOrigin) and not logicHandler.IsPlayerAllowedConsole(pid) then tes3mp.Kick(pid) tes3mp.SendMessage(pid, logicHandler.GetChatName(pid) .. consoleKickMessage, true) return end local isCellLoaded = LoadedCells[cellDescription] ~= nil if not isCellLoaded and logicHandler.DoesPacketOriginRequireLoadedCell(packetOrigin) then tes3mp.LogMessage(enumerations.log.WARN, "Invalid " .. packetType .. logicHandler.GetChatName(pid) .. " used impossible packetOrigin for unloaded " .. cellDescription) return end local packetTables = packetReader.GetObjectPacketTables(packetType) local objects = packetTables.objects local targetPlayers = packetTables.players if not tableHelper.isEmpty(objects) or not tableHelper.isEmpty(targetPlayers) then if not isCellLoaded then logicHandler.LoadCell(cellDescription) end local eventStatus = customEventHooks.triggerValidators("On" .. packetType, {pid, cellDescription, objects, targetPlayers}) if eventStatus.validDefaultHandler then local debugMessage = "Accepted " .. packetType .. " from " .. logicHandler.GetChatName(pid) .. " about " .. cellDescription .. " for " if not tableHelper.isEmpty(objects) then debugMessage = debugMessage .. "objects: " local includeComma = false for uniqueIndex, object in pairs(objects) do if includeComma then debugMessage = debugMessage .. ", " end debugMessage = debugMessage .. object.refId .. " " .. uniqueIndex includeComma = true end end if not tableHelper.isEmpty(targetPlayers) then local chatNames = logicHandler.GetChatNames(tableHelper.getArrayFromIndexes(targetPlayers)) debugMessage = debugMessage .. "players: " .. tableHelper.concatenateArrayValues(chatNames, 1, ", ") end tes3mp.LogMessage(enumerations.log.INFO, debugMessage) LoadedCells[cellDescription]:SaveObjectsByPacketType(packetType, objects) LoadedCells[cellDescription]:LoadObjectsByPacketType(packetType, pid, objects, tableHelper.getArrayFromIndexes(objects), true) end customEventHooks.triggerHandlers("On" .. packetType, eventStatus, {pid, cellDescription, objects, targetPlayers}) if not isCellLoaded then logicHandler.UnloadCell(cellDescription) end end else tes3mp.Kick(pid) end end eventHandler.OnObjectActivate = function(pid, cellDescription) eventHandler.OnGenericObjectEvent(pid, cellDescription, "ObjectActivate") end eventHandler.OnObjectHit = function(pid, cellDescription) eventHandler.OnGenericObjectEvent(pid, cellDescription, "ObjectHit") end eventHandler.OnObjectSound = function(pid, cellDescription) eventHandler.OnGenericObjectEvent(pid, cellDescription, "ObjectSound") end eventHandler.OnObjectPlace = function(pid, cellDescription) eventHandler.OnGenericObjectEvent(pid, cellDescription, "ObjectPlace") end eventHandler.OnObjectSpawn = function(pid, cellDescription) eventHandler.OnGenericObjectEvent(pid, cellDescription, "ObjectSpawn") end eventHandler.OnObjectDelete = function(pid, cellDescription) eventHandler.OnGenericObjectEvent(pid, cellDescription, "ObjectDelete") end eventHandler.OnObjectLock = function(pid, cellDescription) eventHandler.OnGenericObjectEvent(pid, cellDescription, "ObjectLock") end eventHandler.OnObjectMiscellaneous = function(pid, cellDescription) eventHandler.OnGenericObjectEvent(pid, cellDescription, "ObjectMiscellaneous") end eventHandler.OnObjectRestock = function(pid, cellDescription) eventHandler.OnGenericObjectEvent(pid, cellDescription, "ObjectRestock") end eventHandler.OnObjectTrap = function(pid, cellDescription) eventHandler.OnGenericObjectEvent(pid, cellDescription, "ObjectTrap") end eventHandler.OnObjectScale = function(pid, cellDescription) eventHandler.OnGenericObjectEvent(pid, cellDescription, "ObjectScale") end eventHandler.OnObjectState = function(pid, cellDescription) eventHandler.OnGenericObjectEvent(pid, cellDescription, "ObjectState") end eventHandler.OnDoorState = function(pid, cellDescription) eventHandler.OnGenericObjectEvent(pid, cellDescription, "DoorState") end eventHandler.OnClientScriptLocal = function(pid, cellDescription) eventHandler.OnGenericObjectEvent(pid, cellDescription, "ClientScriptLocal") end eventHandler.OnConsoleCommand = function(pid, cellDescription) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then tes3mp.ReadReceivedObjectList() local packetTables = packetReader.GetObjectPacketTables("consoleCommand") local objects = packetTables.objects local targetPlayers = packetTables.players local consoleCommand = tes3mp.GetObjectListConsoleCommand() local eventStatus = customEventHooks.triggerValidators("OnConsoleCommand", {pid, cellDescription, consoleCommand, objects, targetPlayers}) if eventStatus.validDefaultHandler then local debugMessage = "Accepted ConsoleCommand from " .. logicHandler.GetChatName(pid) .. " about " .. cellDescription debugMessage = debugMessage .. "\n- consoleCommand: " .. consoleCommand for uniqueIndex, object in pairs(objects) do debugMessage = debugMessage .. "\n- object target: " .. object.refId .. " " .. uniqueIndex end for targetPid, targetPlayer in pairs(targetPlayers) do debugMessage = debugMessage .. "\n- player target: " .. logicHandler.GetChatName(targetPid) end tes3mp.LogMessage(enumerations.log.INFO, debugMessage) end customEventHooks.triggerHandlers("OnConsoleCommand", eventStatus, {pid, cellDescription, consoleCommand, objects, targetPlayers}) else tes3mp.Kick(pid) end end eventHandler.OnContainer = function(pid, cellDescription) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then tes3mp.ReadReceivedObjectList() local packetOrigin = tes3mp.GetObjectListOrigin() tes3mp.LogAppend(enumerations.log.INFO, "- packetOrigin was " .. tableHelper.getIndexByValue(enumerations.packetOrigin, packetOrigin)) if logicHandler.IsPacketFromConsole(packetOrigin) and not logicHandler.IsPlayerAllowedConsole(pid) then tes3mp.Kick(pid) tes3mp.SendMessage(pid, logicHandler.GetChatName(pid) .. consoleKickMessage, true) return end local isCellLoaded = LoadedCells[cellDescription] ~= nil if not config.allowOnContainerForUnloadedCells and not isCellLoaded and logicHandler.DoesPacketOriginRequireLoadedCell(packetOrigin) then tes3mp.LogMessage(enumerations.log.WARN, "Invalid Container: " .. logicHandler.GetChatName(pid) .. " used impossible packetOrigin for unloaded " .. cellDescription) return end -- Iterate through the objects in the Container packet and only sync and save the -- ones whose refIds are valid --local objects = packetReader.GetObjectPacketTables("container").objects --local acceptedObjects, rejectedObjects = {}, {} local isAllowed = true local rejectedObjects = {} -- Don't allow container changes in currently dying actors local unusableContainerUniqueIndexes = {} if isCellLoaded then unusableContainerUniqueIndexes = LoadedCells[cellDescription].unusableContainerUniqueIndexes end local subAction = tes3mp.GetObjectListContainerSubAction() local objects = {} for index = 0, tes3mp.GetObjectListSize() - 1 do local object = {} object.refId = tes3mp.GetObjectRefId(index) object.uniqueIndex = tes3mp.GetObjectRefNum(index) .. "-" .. tes3mp.GetObjectMpNum(index) if tableHelper.containsValue(unusableContainerUniqueIndexes, object.uniqueIndex) then if subAction == enumerations.containerSub.REPLY_TO_REQUEST then tableHelper.removeValue(unusableContainerUniqueIndexes, object.uniqueIndex) tes3mp.LogMessage(enumerations.log.INFO, "Making container " .. object.uniqueIndex .. " usable as a result of request reply") table.insert(objects, object) else table.insert(rejectedObjects, object.refId .. " " .. object.uniqueIndex) isAllowed = false Players[pid]:Message("That container is currently unusable for synchronization reasons.\n") end else table.insert(objects, object) end end if isAllowed then local eventStatus = customEventHooks.triggerValidators("OnContainer", {pid, cellDescription, objects}) if eventStatus.validDefaultHandler then local useTemporaryLoad = false if not isCellLoaded then logicHandler.LoadCell(cellDescription) useTemporaryLoad = true end -- Don't sync this packet here; BaseCell():SaveContainers will have to -- deal with it LoadedCells[cellDescription]:SaveContainers(pid) if useTemporaryLoad then logicHandler.UnloadCell(cellDescription) end end customEventHooks.triggerHandlers("OnContainer", eventStatus, {pid, cellDescription, objects}) else tes3mp.LogMessage(enumerations.log.INFO, "Rejected Container from " .. logicHandler.GetChatName(pid) .. " about " .. tableHelper.concatenateArrayValues(rejectedObjects, 1, ", ")) end else tes3mp.Kick(pid) end end eventHandler.OnVideoPlay = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then tes3mp.ReadReceivedObjectList() local packetOrigin = tes3mp.GetObjectListOrigin() tes3mp.LogAppend(enumerations.log.INFO, "- packetOrigin was " .. tableHelper.getIndexByValue(enumerations.packetOrigin, packetOrigin)) if logicHandler.IsPacketFromConsole(packetOrigin) and not logicHandler.IsPlayerAllowedConsole(pid) then tes3mp.Kick(pid) tes3mp.SendMessage(pid, logicHandler.GetChatName(pid) .. consoleKickMessage, true) return end if config.shareVideos == true then tes3mp.LogMessage(enumerations.log.INFO, "Sharing VideoPlay from " .. logicHandler.GetChatName(pid)) local videos = {} for i = 0, tes3mp.GetObjectListSize() - 1 do local videoFilename = tes3mp.GetVideoFilename(i) table.insert(videos, videoFilename) tes3mp.LogAppend(enumerations.log.WARN, "- videoFilename " .. videoFilename) end local eventStatus = customEventHooks.triggerValidators("OnVideoPlay", {pid, videos}) if eventStatus.validDefaultHandler then tes3mp.CopyReceivedObjectListToStore() -- Send this VideoPlay packet to other players (sendToOthersPlayers is true), -- but skip sending it to the player we got it from (skipAttachedPlayer is true) tes3mp.SendVideoPlay(true, true) end customEventHooks.triggerHandlers("OnVideoPlay", eventStatus, {pid, videos}) end end end eventHandler.OnRecordDynamic = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then tes3mp.ReadReceivedWorldstate() local recordNumericalType = tes3mp.GetRecordType(pid) -- Iterate through the records in the RecordDynamic packet and only sync and save them -- if all their names are allowed local isAllowed = true local rejectedRecords = {} local recordArray = packetReader.GetRecordDynamicArray(pid) local recordTable = {} if recordNumericalType ~= enumerations.recordType.ENCHANTMENT then for _, record in pairs(recordArray) do if not logicHandler.IsNameAllowed(record.name) then isAllowed = false Players[pid]:Message("You are not allowed to create a record called " .. record.name .. "\n") end end end if not isAllowed then tes3mp.LogMessage(enumerations.log.INFO, "Rejected RecordDynamic from " .. logicHandler.GetChatName(pid) .. " about " .. tableHelper.concatenateArrayValues(rejectedRecords, 1, ", ")) return end local storeType = string.lower(tableHelper.getIndexByValue(enumerations.recordType, recordNumericalType)) local recordStore = RecordStores[storeType] local isEnchantable if recordStore == nil then tes3mp.LogMessage(enumerations.log.WARN, "Rejected RecordDynamic for invalid record store of type " .. recordNumericalType) return else isEnchantable = tableHelper.containsValue(config.enchantableRecordTypes, storeType) end local eventStatus = customEventHooks.triggerValidators("OnRecordDynamic", {pid, recordArray, storeType}) if eventStatus.validDefaultHandler then for _, record in ipairs(recordArray) do local recordId -- Is there already a record exactly like this one, icon and model aside? -- If so, we'll just reuse it the way OpenMW would if storeType == "potion" then recordId = recordStore:GetMatchingRecordId(record, recordStore.data.generatedRecords, Players[pid].data.recordLinks[storeType], {"icon", "model"}, true, 25) end if recordId == nil then recordId = recordStore:GenerateRecordId() end if storeType == "enchantment" then -- We need to store this enchantment's original client-generated id -- on this player so we can match it with its server-generated correct -- id once the player sends the record of the enchanted item they've -- used it on Players[pid].unresolvedEnchantments[record.clientsideEnchantmentId] = recordId record.clientsideEnchantmentId = nil end recordTable[recordId] = record end recordStore:SaveGeneratedRecords(recordTable) recordStore:LoadGeneratedRecords(pid, recordTable, tableHelper.getArrayFromIndexes(recordTable), true) for _, player in pairs(Players) do for recordId, record in pairs(recordTable) do table.insert(player.generatedRecordsReceived, recordId) end end -- Add the final spell to the player's spellbook if storeType == "spell" then tes3mp.ClearSpellbookChanges(pid) tes3mp.SetSpellbookChangesAction(pid, enumerations.spellbook.ADD) for recordId, record in pairs(recordTable) do table.insert(Players[pid].data.spellbook, recordId) tes3mp.AddSpell(pid, recordId) Players[pid]:AddLinkToRecord(storeType, recordId) end recordStore:QuicksaveToDrive() Players[pid]:QuicksaveToDrive() tes3mp.SendSpellbookChanges(pid) -- Add the final items to the player's inventory elseif storeType == "potion" or isEnchantable then local enchantmentStore if isEnchantable then enchantmentStore = RecordStores["enchantment"] end local itemArray = {} for recordId, record in pairs(recordTable) do local item = { refId = recordId, count = record.quantity, charge = -1, enchantmentCharge = -1, soul = "" } inventoryHelper.addItem(Players[pid].data.inventory, item.refId, item.count, item.charge, item.enchantmentCharge, item.soul) table.insert(itemArray, item) Players[pid]:AddLinkToRecord(storeType, recordId) -- If this is an enchantable item record, add a link to it from its associated -- enchantment record if isEnchantable then enchantmentStore:AddLinkToRecord(record.enchantmentId, recordId, storeType) end end if isEnchantable then enchantmentStore:QuicksaveToDrive() end recordStore:QuicksaveToDrive() Players[pid]:QuicksaveToDrive() Players[pid]:LoadItemChanges(itemArray, enumerations.inventory.ADD) end end customEventHooks.triggerHandlers("OnRecordDynamic", eventStatus, {pid, recordTable, storeType}) end end eventHandler.OnWorldKillCount = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnWorldKillCount", {pid}) if eventStatus.validDefaultHandler then WorldInstance:SaveKills(pid) tes3mp.CopyReceivedWorldstateToStore() -- Send this WorldKillCount packet to other players (sendToOthersPlayers is true), -- but skip sending it to the player we got it from (skipAttachedPlayer is true) tes3mp.SendWorldKillCount(pid, true, true) end customEventHooks.triggerHandlers("OnWorldKillCount", eventStatus, {pid}) end end eventHandler.OnWorldMap = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then tes3mp.ReadReceivedWorldstate() local mapTileArray = packetReader.GetWorldMapTileArray() local eventStatus = customEventHooks.triggerValidators("OnWorldMap", {pid, mapTileArray}) if eventStatus.validDefaultHandler then WorldInstance:SaveMapTiles(mapTileArray) if config.shareMapExploration == true then tes3mp.CopyReceivedWorldstateToStore() -- Send this WorldMap packet to other players (sendToOthersPlayers is true), -- but skip sending it to the player we got it from (skipAttachedPlayer is true) tes3mp.SendWorldMap(pid, true, true) end end customEventHooks.triggerHandlers("OnWorldMap", eventStatus, {pid, mapTileArray}) end end eventHandler.OnWorldWeather = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then local eventStatus = customEventHooks.triggerValidators("OnWorldWeather", {pid}) if eventStatus.validDefaultHandler then tes3mp.ReadReceivedWorldstate() local regionName = string.lower(tes3mp.GetWeatherRegion()) -- Track current weather in each region if WorldInstance.storedRegions[regionName] ~= nil then WorldInstance:SaveRegionWeather(regionName) end -- Go through the other players on the server and send them this weather update for _, otherPlayer in pairs(Players) do local otherPid = otherPlayer.pid -- Ignore the player we got the weather from if otherPid ~= pid then -- If this player has been marked as requiring a force weather update for -- this region, provide them with one if WorldInstance:IsForcedWeatherUpdatePid(otherPid, regionName) then WorldInstance:LoadRegionWeather(regionName, otherPid, false, true) WorldInstance:RemoveForcedWeatherUpdatePid(otherPid, regionName) else WorldInstance:LoadRegionWeather(regionName, otherPid, false, false) end end end end customEventHooks.triggerHandlers("OnWorldWeather", eventStatus, {pid}) end end eventHandler.OnClientScriptGlobal = function(pid) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() then tes3mp.ReadReceivedWorldstate() local variables = packetReader.GetClientScriptGlobalPacketTable() local eventStatus = customEventHooks.triggerValidators("OnClientScriptGlobal", {pid, variables}) if eventStatus.validDefaultHandler then local shouldSync = false -- Iterate through the global IDs in the ClientScriptGlobal packet and only sync and save them -- when applicable for id, variable in pairs(variables) do local isKillSync, isQuestSync, isFactionSync, isWorldwideSync = false, false, false, false isKillSync = tableHelper.containsValue(clientVariableScopes.globals.kills, id) if not isKillSync then isQuestSync = config.shareJournal == true and tableHelper.containsValue(clientVariableScopes.globals.quest, id) end if not isQuestSync then isFactionSync = config.shareFactionRanks == true and tableHelper.containsValue(clientVariableScopes.globals.faction, id) end if not isFactionSync then isWorldwideSync = tableHelper.containsValue(clientVariableScopes.globals.worldwide, id) end if isKillSync or isQuestSync or isFactionSync or isWorldwideSync then WorldInstance:SaveClientScriptGlobal(variables) shouldSync = true else Players[pid]:SaveClientScriptGlobal(variables) end end if shouldSync then tes3mp.CopyReceivedWorldstateToStore() -- The client already has this global value on their client, so we -- only send it to other players -- i.e. sendToOtherPlayers is true and skipAttachedPlayer is true tes3mp.SendClientScriptGlobal(pid, true, true) tes3mp.LogMessage(enumerations.log.INFO, "Synchronized ClientScriptGlobal from " .. logicHandler.GetChatName(pid) .. " about " .. tableHelper.concatenateTableIndexes(variables, ", ")) end end customEventHooks.triggerHandlers("OnClientScriptGlobal", eventStatus, {pid, variables}) else tes3mp.Kick(pid) end end eventHandler.OnMpNumIncrement = function(currentMpNum) WorldInstance:SetCurrentMpNum(currentMpNum) end eventHandler.OnLoginTimeExpiration = function(pid, accountName) if Players[pid] ~= nil and Players[pid].accountName == accountName then local eventStatus = customEventHooks.triggerValidators("OnLoginTimeExpiration", {pid}) if eventStatus.validDefaultHandler then logicHandler.AuthCheck(pid) end customEventHooks.triggerHandlers("OnLoginTimeExpiration", eventStatus, {pid}) end end eventHandler.OnDeathTimeExpiration = function(pid, accountName) if Players[pid] ~= nil and Players[pid]:IsLoggedIn() and Players[pid].accountName == accountName then local eventStatus = customEventHooks.triggerValidators("OnDeathTimeExpiration", {pid}) if eventStatus.validDefaultHandler then Players[pid]:Resurrect() end customEventHooks.triggerHandlers("OnDeathTimeExpiration", eventStatus, {pid}) end end eventHandler.OnObjectLoopTimeExpiration = function(loopIndex) if ObjectLoops[loopIndex] ~= nil then local loop = ObjectLoops[loopIndex] local pid = loop.targetPid local loopEnded = false if Players[pid] ~= nil and Players[pid]:IsLoggedIn() and Players[pid].accountName == loop.targetName then local eventStatus = customEventHooks.triggerValidators("OnObjectLoopTimeExpiration", {pid, loopIndex}) if eventStatus.validDefaultHandler then if loop.packetType == "place" or loop.packetType == "spawn" then logicHandler.CreateObjectAtPlayer(pid, loop.refId, loop.packetType) elseif loop.packetType == "console" then logicHandler.RunConsoleCommandOnPlayer(pid, loop.consoleCommand) end loop.count = loop.count - 1 if loop.count > 0 then ObjectLoops[loopIndex] = loop tes3mp.RestartTimer(loop.timerId, loop.interval) else loopEnded = true end end customEventHooks.triggerHandlers("OnObjectLoopTimeExpiration", eventStatus, {pid, loopIndex}) else loopEnded = true end if loopEnded == true then ObjectLoops[loopIndex] = nil end end end return eventHandler