public override void Initialize() { TShockAPI.Commands.ChatCommands.Add(new Command("npcfilter.use", FilterNPC, "npcfilter")); ServerApi.Hooks.NpcSpawn.Register(this, OnSpawn); ServerApi.Hooks.NpcTransform.Register(this, OnTransform); if (File.Exists(path)) { filterStorage = FilterStorage.Read(path); } filterStorage.Write(path); }
private void FilterNPC(CommandArgs args) { if (args.Parameters.Count == 1 && args.Parameters[0] == "list") { args.Player.SendInfoMessage("Banned NPC IDs: " + string.Join(", ", filterStorage.FilteredNPCs) + "."); return; } else if (args.Parameters.Count == 1 && args.Parameters[0] == "reload") { FilterStorage.Read(path); args.Player.SendSuccessMessage("Attempted to reload NPCFliter list!"); return; } else if (args.Parameters.Count == 2) { NPC npc; List <NPC> matchedNPCs = TShock.Utils.GetNPCByIdOrName(args.Parameters[1]); if (matchedNPCs.Count == 0) { args.Player.SendErrorMessage("Invalid NPC: '{0}'!", args.Parameters[1]); return; } else if (matchedNPCs.Count > 1) { TShock.Utils.SendMultipleMatchError(args.Player, matchedNPCs.Select(i => i.name)); return; } else { npc = matchedNPCs[0]; } switch (args.Parameters[0]) { case "add": { if (filterStorage.FilteredNPCs.Contains(npc.netID)) { args.Player.SendErrorMessage("NPC ID {0} is already on the filter list!", npc.netID); return; } filterStorage.FilteredNPCs.Add(npc.netID); File.WriteAllText(path, JsonConvert.SerializeObject(filterStorage, Formatting.Indented)); args.Player.SendSuccessMessage("Successfully added NPC ID to filter list: {0}!", npc.netID); break; } case "delete": case "del": case "remove": { if (!filterStorage.FilteredNPCs.Contains(npc.netID)) { args.Player.SendErrorMessage("NPC ID {0} is not on the filter list!", npc.netID); return; } filterStorage.FilteredNPCs.Remove(npc.netID); File.WriteAllText(path, JsonConvert.SerializeObject(filterStorage, Formatting.Indented)); args.Player.SendSuccessMessage("Successfully removed NPC ID from filter list: {0}!", npc.netID); break; } default: { args.Player.SendErrorMessage("Syntax: /npcfilter <add/remove> [name or ID]"); break; } } } else { args.Player.SendInfoMessage("Available NPCFilter commands:"); args.Player.SendInfoMessage("/npcfilter <add/remove> [name or ID]"); args.Player.SendInfoMessage("/npcfilter list"); args.Player.SendInfoMessage("/npcfilter reload"); return; } }