From 154ffe2fed75bb50c1eef1d7bd0ff6c2282fc9e6 Mon Sep 17 00:00:00 2001 From: nomi-nonsz Date: Sun, 10 Aug 2025 21:03:20 +0700 Subject: [PATCH] working blacklist system and apply list command --- lua/autorun/hostile_npcs_tocitizen.lua | 57 +++++++++++++++----------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/lua/autorun/hostile_npcs_tocitizen.lua b/lua/autorun/hostile_npcs_tocitizen.lua index a95afb4..86e7e01 100644 --- a/lua/autorun/hostile_npcs_tocitizen.lua +++ b/lua/autorun/hostile_npcs_tocitizen.lua @@ -1,33 +1,52 @@ -local npcs = list.Get("NPC") -local citizenEnemy = npcs["npc_citizen_rebel_enemy"] -local originalListSet = list.Set - -local function loadNPCBlacklist() +local function loadBlacklistFile() local json = file.Read("npc_blacklist.json", "DATA") - print(json) if not json or json == "" then return {} end local data = util.JSONToTable(json) return istable(data) and data or {} end -function list.Set(listType, class, npc) - if listType == "NPC" then +local function resetHostile() + local npcs = list.Get("NPC") + for class, npc in pairs(npcs) do + if npc["m_OldData"] then + local oldData = util.JSONToTable(npc["m_OldData"]) + if oldData then + list.Set("NPC", class, oldData) + end + end + end +end + +local function replaceHostile() + local blacklist = loadBlacklistFile() + local npcs = list.Get("NPC") + local citizenEnemy = npcs["npc_citizen_rebel_enemy"] + + for class, npc in pairs(npcs) do + if table.HasValue(blacklist, class) then continue end if npc["Class"] == "npc_combine_s" and npc["Category"] ~= "#spawnmenu.category.combine" then + npc["m_OldData"] = npc["m_OldData"] or util.TableToJSON(npc, false) npc["Class"] = citizenEnemy["Class"] npc["KeyValues"] = { Hostile = 1, SquadName = "overwatch", citizentype = 4 } + list.Set("NPC", class, npc) end end - originalListSet(listType, class, npc) end CreateClientConVar("replacehostile_blacklist", "", false, false, "Blacklist NPC Classes") +concommand.Add("replacehostile_reload", function() + resetHostile() + replaceHostile() + print("NPC Hostile Reloaded") +end) + hook.Add("PopulateToolMenu", "ReplaceHostileMenu", function() - local blacklist = loadNPCBlacklist() + local blacklist = loadBlacklistFile() local defaultBlacklistStr = table.concat(blacklist, ",") RunConsoleCommand("replacehostile_blacklist", defaultBlacklistStr) @@ -38,6 +57,7 @@ hook.Add("PopulateToolMenu", "ReplaceHostileMenu", function() local blacklistEntry = panel:TextEntry("Blacklist Classes", "replacehostile_blacklist") panel:ControlHelp("Separate classes with commas.\nExample: npc_my_custom_hostile,npc_my_hostile,npc_hostile_test") + panel:Button("Reload NPCs", "replacehostile_reload") blacklistEntry.OnChange = function(self) local val = self:GetValue() or "" @@ -62,22 +82,9 @@ hook.Add("PopulateToolMenu", "ReplaceHostileMenu", function() file.Write("npc_blacklist.json", util.TableToJSON(newBlacklist, false)) end end - - panel:Help("NOTE: Restart the map to apply the changes.") end) end) --- Second stage. idk if one of these necessary -timer.Simple(0, function () - for class, npc in pairs(npcs) do - if npc["Class"] == "npc_combine_s" and npc["Category"] ~= "#spawnmenu.category.combine" then - npc["Class"] = citizenEnemy["Class"] - npc["KeyValues"] = { - Hostile = 1, - SquadName = "overwatch", - citizentype = 4 - } - originalListSet("NPC", class, npc) - end - end +timer.Simple(0, function() + replaceHostile() end) \ No newline at end of file