2 Commits

Author SHA1 Message Date
154ffe2fed working blacklist system and apply list command 2025-08-10 21:03:20 +07:00
dd0d995f3f add blacklist settings 2025-08-10 15:16:20 +07:00

View File

@@ -1,61 +1,90 @@
local npcs = list.Get("NPC") local function loadBlacklistFile()
local citizenEnemy = npcs["npc_citizen_rebel_enemy"] local json = file.Read("npc_blacklist.json", "DATA")
local originalListSet = list.Set if not json or json == "" then return {} end
local data = util.JSONToTable(json)
function list.Set(listType, class, npc) return istable(data) and data or {}
if listType == "NPC" then
if npc["Class"] == "npc_combine_s" and npc["Category"] ~= "#spawnmenu.category.combine" then
if !npc["KeyValues"] then
npc["KeyValues"] = {}
end
npc["KeyValues"]["SquadName"] = "m_EnemyRebel"
end
end
originalListSet(listType, class, npc)
end end
hook.Add("OnEntityCreated", "ForceReplaceCombine", function(ent) local function resetHostile()
timer.Simple(0, function() local npcs = list.Get("NPC")
if !SERVER then return end for class, npc in pairs(npcs) do
if not IsValid(ent) then return end if npc["m_OldData"] then
local oldData = util.JSONToTable(npc["m_OldData"])
if ent:GetClass() == "npc_citizen" then if oldData then
list.Set("NPC", class, oldData)
end
end end
end
end
if ent:GetKeyValues()["squadname"] == "m_EnemyRebel" then local function replaceHostile()
local pos = ent:GetPos() local blacklist = loadBlacklistFile()
local ang = ent:GetAngles() local npcs = list.Get("NPC")
local mdl = ent:GetModel() local citizenEnemy = npcs["npc_citizen_rebel_enemy"]
local weps = ent:GetWeapons()
local health = ent:GetMaxHealth()
local keyValues = ent:GetKeyValues()
ent:Remove() for class, npc in pairs(npcs) do
if table.HasValue(blacklist, class) then continue end
local npc = ents.Create("npc_citizen") if npc["Class"] == "npc_combine_s" and npc["Category"] ~= "#spawnmenu.category.combine" then
if not IsValid(npc) then return end npc["m_OldData"] = npc["m_OldData"] or util.TableToJSON(npc, false)
npc["Class"] = citizenEnemy["Class"]
for key, value in pairs(keyValues) do npc["KeyValues"] = {
npc:SetKeyValue(key, tostring(value)) Hostile = 1,
-- if value ~= "" then SquadName = "overwatch",
-- end citizentype = 4
end }
list.Set("NPC", class, npc)
npc:SetHealth(health) end
npc:SetMaxHealth(health) end
end
npc:SetKeyValue("hostile", 1) CreateClientConVar("replacehostile_blacklist", "", false, false, "Blacklist NPC Classes")
npc:SetKeyValue("squadname", "overwatch")
npc:SetKeyValue("citizentype", 4)
for _, wep in ipairs(weps) do concommand.Add("replacehostile_reload", function()
npc:Give(wep:GetClass()) resetHostile()
replaceHostile()
print("NPC Hostile Reloaded")
end)
hook.Add("PopulateToolMenu", "ReplaceHostileMenu", function()
local blacklist = loadBlacklistFile()
local defaultBlacklistStr = table.concat(blacklist, ",")
RunConsoleCommand("replacehostile_blacklist", defaultBlacklistStr)
spawnmenu.AddToolMenuOption("Options", "Replace Hostile", "ReplaceHostileConfig", "Settings", "", "", function(panel)
panel:ClearControls()
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 ""
local newBlacklist = {}
for e in string.gmatch(val, '([^,]+)') do
local trimmed = string.Trim(e)
if trimmed ~= "" then
table.insert(newBlacklist, trimmed)
end
end end
npc:SetPos(pos) local valid = true
npc:SetAngles(ang) for _, v in ipairs(newBlacklist) do
npc:SetModel(mdl) if type(v) ~= "string" or v == "" then
npc:Spawn() valid = false
break
end
end
if valid then
file.Write("npc_blacklist.json", util.TableToJSON(newBlacklist, false))
end
end end
end) end)
end)
timer.Simple(0, function()
replaceHostile()
end) end)