using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Terraria.ModLoader;
using Terraria;
namespace CalamityMod
{
public static partial class CalamityUtils
{
///
/// Actual "Thread-Safe" version of TryGetGlobalNPC
///
/// GlobalNPC type to get
/// Input NPC
/// GlobalNPC output
/// true If we successfully get GlobalNPC. otherwise false
public static bool TryGetGlobalNPCSafer(this NPC npc, out T globalNPC) where T : GlobalNPC
{
if (npc is null)
{
globalNPC = null;
return false;
}
int slot = ModContent.GetInstance()?.PerEntityIndex ?? -1;
int length = npc.EntityGlobals.Length;
if (slot < 0 || slot >= length)
{
globalNPC = null;
return false;
}
npc.TryGetGlobalNPC(out globalNPC);
return globalNPC != null;
}
}
}