From 3ed336cb3fd50dbf796d9478e0a8d3c123ec92d9 Mon Sep 17 00:00:00 2001 From: Zack Piispanen Date: Fri, 27 Feb 2015 13:18:53 -0500 Subject: [PATCH] Invoke NPC Spawn hook after members are initialized, and return the npc id to the caller. Users should be aware that if they want to ignore the spawn they need to set active=false, and return 200. NPCSpawnEventArgs now take an int for the npc id, instead of a reference to an Npc, allowing us to return the npc id as 200 in the event we cancel it. Ticked API as this changes a hook. --- Terraria/NPC.cs | 12 +++++++----- TerrariaApi.Server/EventArgs/NpcSpawnEventArgs.cs | 5 ++--- TerrariaApi.Server/HookManager.cs | 5 +++-- TerrariaApi.Server/ServerApi.cs | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Terraria/NPC.cs b/Terraria/NPC.cs index d7cae7d..ea277dd 100755 --- a/Terraria/NPC.cs +++ b/Terraria/NPC.cs @@ -34050,11 +34050,7 @@ public static int NewNPC(int X, int Y, int Type, int Start = 0) { Main.npc[num] = new NPC(); Main.npc[num].SetDefaults(Type, -1f); - if (ServerApi.Hooks.InvokeNpcSpawn(Main.npc[num])) - { - Main.npc[num].active = false; - return 200; - } + if (NPC.TypeToNum(Type) != -1) { Main.npc[num].displayName = NPC.getNewNPCName(Type); @@ -34064,6 +34060,12 @@ public static int NewNPC(int X, int Y, int Type, int Start = 0) Main.npc[num].active = true; Main.npc[num].timeLeft = (int)((double)NPC.activeTime * 1.25); Main.npc[num].wet = Collision.WetCollision(Main.npc[num].position, Main.npc[num].width, Main.npc[num].height); + + if (ServerApi.Hooks.InvokeNpcSpawn(ref num)) + { + return num; + } + if (Type == 50) { if (Main.netMode == 0) diff --git a/TerrariaApi.Server/EventArgs/NpcSpawnEventArgs.cs b/TerrariaApi.Server/EventArgs/NpcSpawnEventArgs.cs index 29d3d2f..8151f47 100644 --- a/TerrariaApi.Server/EventArgs/NpcSpawnEventArgs.cs +++ b/TerrariaApi.Server/EventArgs/NpcSpawnEventArgs.cs @@ -6,10 +6,9 @@ namespace TerrariaApi.Server { public class NpcSpawnEventArgs : HandledEventArgs { - public NPC Npc + public int NpcId { - get; - internal set; + get; set; } } } diff --git a/TerrariaApi.Server/HookManager.cs b/TerrariaApi.Server/HookManager.cs index 3015d45..770f142 100644 --- a/TerrariaApi.Server/HookManager.cs +++ b/TerrariaApi.Server/HookManager.cs @@ -625,14 +625,15 @@ public HandlerCollection NpcSpawn get { return this.npcSpawn; } } - internal bool InvokeNpcSpawn(NPC npc) + internal bool InvokeNpcSpawn(ref int npcId) { NpcSpawnEventArgs args = new NpcSpawnEventArgs { - Npc = npc + NpcId = npcId }; this.NpcSpawn.Invoke(args); + npcId = args.NpcId; return args.Handled; } #endregion diff --git a/TerrariaApi.Server/ServerApi.cs b/TerrariaApi.Server/ServerApi.cs index d6cb064..8a41850 100644 --- a/TerrariaApi.Server/ServerApi.cs +++ b/TerrariaApi.Server/ServerApi.cs @@ -17,7 +17,7 @@ public static class ServerApi { public const string PluginsPath = "ServerPlugins"; - public static readonly Version ApiVersion = new Version(1, 16, 0, 0); + public static readonly Version ApiVersion = new Version(1, 17, 0, 0); private static Main game; private static readonly Dictionary loadedAssemblies = new Dictionary(); private static readonly List plugins = new List();