Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
Invoke NPC Spawn hook after members are initialized, and return the n…
Browse files Browse the repository at this point in the history
…pc 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.
  • Loading branch information
Zack Piispanen committed Feb 27, 2015
1 parent 50a04c4 commit 3ed336c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
12 changes: 7 additions & 5 deletions Terraria/NPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions TerrariaApi.Server/EventArgs/NpcSpawnEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ namespace TerrariaApi.Server
{
public class NpcSpawnEventArgs : HandledEventArgs
{
public NPC Npc
public int NpcId
{
get;
internal set;
get; set;
}
}
}
5 changes: 3 additions & 2 deletions TerrariaApi.Server/HookManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -625,14 +625,15 @@ public HandlerCollection<NpcSpawnEventArgs> 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
Expand Down
2 changes: 1 addition & 1 deletion TerrariaApi.Server/ServerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Assembly> loadedAssemblies = new Dictionary<string, Assembly>();
private static readonly List<PluginContainer> plugins = new List<PluginContainer>();
Expand Down

0 comments on commit 3ed336c

Please sign in to comment.