diff --git a/gamemodes/terrortown/gamemode/server/sv_main.lua b/gamemodes/terrortown/gamemode/server/sv_main.lua index c70ecc975..d98e4f23a 100644 --- a/gamemodes/terrortown/gamemode/server/sv_main.lua +++ b/gamemodes/terrortown/gamemode/server/sv_main.lua @@ -931,7 +931,6 @@ end -- @realm server function GM:TTT2PlayerFinishedReloading(ply) map.SyncToClient(ply) - button.SyncToClient(ply) end --- diff --git a/gamemodes/terrortown/gamemode/server/sv_player_ext.lua b/gamemodes/terrortown/gamemode/server/sv_player_ext.lua index b4b90acb9..0097217b9 100644 --- a/gamemodes/terrortown/gamemode/server/sv_player_ext.lua +++ b/gamemodes/terrortown/gamemode/server/sv_player_ext.lua @@ -1697,7 +1697,6 @@ local function SetPlayerReady(_, ply) entspawnscript.TransmitToPlayer(ply) map.SyncToClient(ply) - button.SyncToClient(ply) gameloop.PlayerReady(ply) diff --git a/gamemodes/terrortown/gamemode/shared/sh_entity.lua b/gamemodes/terrortown/gamemode/shared/sh_entity.lua index 2af71b834..6b95d1659 100644 --- a/gamemodes/terrortown/gamemode/shared/sh_entity.lua +++ b/gamemodes/terrortown/gamemode/shared/sh_entity.lua @@ -17,30 +17,12 @@ function entmeta:IsPlayerRagdoll() return self:IsRagdoll() and CORPSE.GetPlayerNick(self, false) ~= false end ---- --- Sets the information if this entity is a default button. --- @note: This function does not network the information. --- @param boolean state The new state --- @realm shared -function entmeta:SetDefaultButton(state) - self.isDefaultButton = state -end - --- -- Returns true if this entity is a default button. -- @return boolean Returns true if default button -- @realm shared function entmeta:IsDefaultButton() - return self.isDefaultButton or false -end - ---- --- Sets the information if this entity is a rotating button. --- @note: This function does not network the information. --- @param boolean state The new state --- @realm shared -function entmeta:SetRotatingButton(state) - self.isRotatingButton = state + return button.IsClass(self, "func_button") end --- @@ -48,7 +30,7 @@ end -- @return boolean Returns true if rotating button -- @realm shared function entmeta:IsRotatingButton() - return self.isRotatingButton or false + return button.IsClass(self, "func_rot_button") end --- diff --git a/lua/ttt2/libraries/buttons.lua b/lua/ttt2/libraries/buttons.lua index d49f82729..51dd04691 100644 --- a/lua/ttt2/libraries/buttons.lua +++ b/lua/ttt2/libraries/buttons.lua @@ -14,31 +14,7 @@ local validButtons = { "func_rot_button", } -local function UpdateStates(foundButtons) - for class, list in pairs(foundButtons) do - for i = 1, #list do - local ent = list[i] - - if not IsValid(ent) then - continue - end - - if class == "func_button" then - ent:SetDefaultButton(true) - end - - if class == "func_rot_button" then - ent:SetRotatingButton(true) - end - - ent:SetNotSolid(false) - end - end -end - if SERVER then - local foundButtons = {} - --- -- Setting up all buttons found on a map, this is done on every map reset (on prepare round). -- @internal @@ -46,27 +22,35 @@ if SERVER then function button.SetUp() for i = 1, #validButtons do local classButton = validButtons[i] + local buttonsTable = ents.FindByClass(classButton) - foundButtons[classButton] = ents.FindByClass(classButton) - end + for j = 1, #buttonsTable do + local foundButton = buttonsTable[j] - -- sync to client and set state - UpdateStates(foundButtons) - net.SendStream("TTT2SyncButtonEntities", foundButtons) - end + foundButton:SetNotSolid(false) + foundButton:SetSolid(SOLID_BSP) - --- - -- Resyncs button states to the client if they late connect or hotreload. - -- @param Player ply The player to sync to - -- @internal - -- @realm server - function button.SyncToClient(ply) - net.SendStream("TTT2SyncButtonEntities", foundButtons, ply) + foundButton:SetNWInt("button_class", i) + end + end end end -if CLIENT then - net.ReceiveStream("TTT2SyncButtonEntities", function(foundButtons) - UpdateStates(foundButtons) - end) +--- +-- Checks if a given button entity has the provided class. +-- @note This is checked like this, because buttons and levers +-- lose their specific class name on the client and therefore use custom +-- syncing here. +-- @param Entity ent The button entity that should be checked +-- @param string class The class name +-- @return boolean Returns true if the entity matches the provided class name +-- @realm shared +function button.IsClass(ent, class) + local classID = ent:GetNWInt("button_class") + + if not classID or classID > #validButtons then + return + end + + return validButtons[classID] == class end