Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
Added OnChat Detection Support
Browse files Browse the repository at this point in the history
Added override for Getchat (you can now override what some npcs say)

Changed API Version to 1.19
Changed authors to give credit for the versions they worked on.

Fixed a Issue where projectiles where not firing after the Monster transformed
Created a workaround for Custom loot spawning (into the item drop hooks are fixed)
  • Loading branch information
Pychnight committed Jul 16, 2015
1 parent 9c0594e commit 5771e9b
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 56 deletions.
1 change: 1 addition & 0 deletions CustomNPC/CustomNPC/CustomNPC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<ItemGroup>
<Compile Include="CustomNPCConfig.cs" />
<Compile Include="CustomNPCInvasion.cs" />
<Compile Include="EventSystem\Events\ServerChatEvent.cs" />
<Compile Include="SpawnConditions.cs" />
<Compile Include="Biomes.cs" />
<Compile Include="CustomNPCDefinition.cs" />
Expand Down
4 changes: 4 additions & 0 deletions CustomNPC/CustomNPC/CustomNPCDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public virtual int customAI
{
get { return customBase.aiStyle; }
}
public virtual string GetChat
{
get { return null; }
}
public virtual bool isBoss
{
get { return customBase.boss; }
Expand Down
137 changes: 101 additions & 36 deletions CustomNPC/CustomNPC/CustomNPCPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace CustomNPC
{
[ApiVersion(1, 18)]
[ApiVersion(1, 19)]
public class CustomNPCPlugin : TerrariaPlugin
{
internal Random rand = new Random();
Expand Down Expand Up @@ -62,7 +62,7 @@ public CustomNPCPlugin(Main game)

public override string Author
{
get { return "IcyGaming"; }
get { return "IcyGaming(v1.0), Taeir(v1.1), Pychnight(v1.2)"; }
}

public override string Description
Expand All @@ -77,7 +77,7 @@ public override string Name

public override Version Version
{
get { return new Version("1.1"); }
get { return new Version("1.2"); }
}

public override void Initialize()
Expand All @@ -99,10 +99,13 @@ public override void Initialize()
//one OnUpdate is needed for updating mob positioning
ServerApi.Hooks.GameUpdate.Register(this, OnUpdate);
ServerApi.Hooks.NpcSpawn.Register(this, OnNPCSpawn);
ServerApi.Hooks.NpcLootDrop.Register(this, OnLootDrop);
// ServerApi.Hooks.NpcLootDrop.Register(this, OnItemDrop);
ServerApi.Hooks.ServerChat.Register(this, OnChat);
ServerApi.Hooks.NetGetData.Register(this, OnGetData);
}



#if USE_APPDOMAIN
private Assembly PluginDomain_OnAssemblyResolve(object sender, ResolveEventArgs args)
{
Expand All @@ -124,11 +127,47 @@ private void OnInitialize(EventArgs args)
mainLoop.Enabled = true;
}

void OnChat(ServerChatEventArgs args)
{
if (args.Handled)
{
return;
}

TSPlayer player = TShock.Players[args.Who];

if (player == null)
{
args.Handled = true;
return;
}

string[] chat = args.Text.Split();
string cmd = chat[0].Substring(0);

var ServerChatEventArgs = new ServerChatEvent();

int Who = args.Who;
String Text = args.Text;
MessageBuffer Buffer = args.Buffer;

Who = player.Index;
Text = cmd;
Buffer = args.Buffer;

ServerChatEventArgs.Who = Who;
ServerChatEventArgs.Text = Text;
ServerChatEventArgs.Buffer = Buffer;

eventManager.InvokeHandler(ServerChatEventArgs, EventType.ServerChat);
}

/*
/// <summary>
/// For Custom Loot
/// </summary>
/// <param name="args"></param>
private void OnLootDrop(NpcLootDropEventArgs args)
private void OnItemDrop(NpcLootDropEventArgs args)
{
CustomNPCVars npcvar = NPCManager.GetCustomNPCByIndex(args.NpcArrayIndex);
Expand Down Expand Up @@ -157,8 +196,9 @@ private void OnLootDrop(NpcLootDropEventArgs args)
npcvar.isDead = true;
npcvar.droppedLoot = true;
npcvar.OnDeath();
//npcvar.OnDeath();
}
*/

/// <summary>
/// Spawn custom npc using /csm &lt;id&gt; [amount] [&lt;x&gt; &lt;y&gt;]
Expand Down Expand Up @@ -373,17 +413,18 @@ private void OnUpdate(EventArgs args)

private void OnNPCSpawn(NpcSpawnEventArgs args)
{
//DEBUG
TShock.Log.ConsoleInfo("DEBUG [NPCSpawn] NPCIndex {0}", args.NpcId);
//DEBUG

//If the id falls outside the possible range, we can return.
if (args.NpcId < 0 || args.NpcId >= 200) return;

//This NPC is custom and not dead.
if (NPCManager.NPCs[args.NpcId] != null && !NPCManager.NPCs[args.NpcId].isDead) return;

NPC spawned = Main.npc[args.NpcId];

//DEBUG
TShock.Log.ConsoleInfo("DEBUG [NPCSpawn] NPCIndex {0}", args.NpcId);
//DEBUG

foreach (CustomNPCDefinition customnpc in NPCManager.Data.CustomNPCs.Values)
{
if (!customnpc.isReplacement || spawned.netID != customnpc.customBase.netID) continue;
Expand Down Expand Up @@ -427,13 +468,21 @@ private void OnGetData(GetDataEventArgs args)
critical = data.ReadBoolean();
}

OnNpcDamaged(player, npcIndex, damage, knockback, direction, critical);
CustomNPCVars npcvar = NPCManager.NPCs[npcIndex];
if (npcvar != null)

if (npcvar.customNPC.customID != null)
{
OnNpcDamaged(player, npcIndex, damage, knockback, direction, critical);
}
}

#region Event Dispatchers

private void OnNpcDamaged(TSPlayer player, int npcIndex, int damage, float knockback, byte direction, bool critical)
{
CustomNPCVars npcvar = NPCManager.NPCs[npcIndex];
if (npcvar != null)
//DEBUG
TShock.Log.ConsoleInfo("DEBUG [NPCDamage] NPCIndex {0}", npcIndex);
//DEBUG
Expand All @@ -448,6 +497,7 @@ private void OnNpcDamaged(TSPlayer player, int npcIndex, int damage, float knock
//Damage event
var e = new NpcDamageEvent
{
customID = npcvar.customNPC.customID,
NpcIndex = npcIndex,
PlayerIndex = player.Index,
Damage = damage,
Expand All @@ -462,30 +512,32 @@ private void OnNpcDamaged(TSPlayer player, int npcIndex, int damage, float knock
//This damage will kill the NPC.
if (npc.active && npc.life > 0 && damageDone >= npc.life)
{
CustomNPCVars npcvar = NPCManager.NPCs[npcIndex];
if (npcvar != null)
{
npcvar.markDead();
}
if (npcvar != null && npcvar.customNPC.customID != null)
{
npcvar.markDead();

//Kill event
var killedArgs = new NpcKilledEvent
{
NpcIndex = npcIndex,
PlayerIndex = player.Index,
Damage = damage,
Knockback = knockback,
Direction = direction,
CriticalHit = critical,
LastPosition = npc.position
};

eventManager.InvokeHandler(killedArgs, EventType.NpcKill);

if (npcvar != null && npcvar.isInvasion)
{
NPCManager.CustomNPCInvasion.WaveSize--;
}
//Kill event
var killedArgs = new NpcKilledEvent
{
customID = npcvar.customNPC.customID,
NpcIndex = npcIndex,
PlayerIndex = player.Index,
Damage = damage,
Knockback = knockback,
Direction = direction,
CriticalHit = critical,
LastPosition = npc.position
};

eventManager.InvokeHandler(killedArgs, EventType.NpcKill);
npcvar.isDead = true;
npcvar.OnDeath();

if (npcvar != null && npcvar.isInvasion)
{
NPCManager.CustomNPCInvasion.WaveSize--;
}
}
}
}

Expand All @@ -502,8 +554,9 @@ protected override void Dispose(bool disposing)

ServerApi.Hooks.GameUpdate.Deregister(this, OnUpdate);
ServerApi.Hooks.GameInitialize.Deregister(this, OnInitialize);
ServerApi.Hooks.NpcLootDrop.Deregister(this, OnLootDrop);
// ServerApi.Hooks.NpcLootDrop.Deregister(this, OnItemDrop);
ServerApi.Hooks.NetGetData.Deregister(this, OnGetData);
ServerApi.Hooks.ServerChat.Deregister(this, OnChat);
ServerApi.Hooks.NpcSpawn.Deregister(this, OnNPCSpawn);
}
}
Expand Down Expand Up @@ -562,6 +615,9 @@ private void CustomNPCUpdate(bool onlyCustom = true, bool updateDead = false)
//Dead
if (obj == null) continue;

//check custom id first before doing anything to ensure we never get normal monsters
if (obj.customNPC.customID == null) continue;

if (obj.isDead || obj.mainNPC == null || obj.mainNPC.life <= 0 || obj.mainNPC.type == 0)
{
if (updateDead) obj.markDead();
Expand Down Expand Up @@ -589,8 +645,12 @@ private void CollisionDetection()
{
foreach (CustomNPCVars obj in NPCManager.NPCs)
{
//check null and if dead
if (obj == null || obj.isDead) continue;

//check custom id first before doing anything to ensure we never get normal monsters
if (obj.customNPC.customID == null) continue;

Rectangle npcframe = new Rectangle((int)obj.mainNPC.position.X, (int)obj.mainNPC.position.Y, obj.mainNPC.width, obj.mainNPC.height);

foreach (TSPlayer player in TShock.Players)
Expand Down Expand Up @@ -623,6 +683,9 @@ private void ProjectileCheck()
//Check if they exists and are active
if (obj == null || obj.isDead || !obj.mainNPC.active) continue;

//check custom id first before doing anything to ensure we never get normal monsters
if (obj.customNPC.customID == null) continue;

//We only want the npcs with custom projectiles
if (obj.customNPC.customProjectiles == null) continue;

Expand Down Expand Up @@ -658,6 +721,7 @@ private void ProjectileCheck()
target = player;
//Set npcs target to the player its shooting at
obj.mainNPC.target = player.Index;
obj.mainNPC.friendly = false;
//Break since no need to find another target
break;
}
Expand Down Expand Up @@ -764,7 +828,8 @@ private void CheckActiveNPCs()

if ((obj.isDead && !obj.isUncounted) || obj.mainNPC == null || obj.mainNPC.life <= 0 || obj.mainNPC.type == 0)
{
obj.markDead();
obj.markDead();
obj.Customloot();
}
else if (!obj.isDead)
{
Expand Down
Loading

0 comments on commit 5771e9b

Please sign in to comment.