Potential Fix for "Game Crash After WoF Defeated"

This commit is contained in:
Flowaria committed 2024-09-15 12:45:38 +09:00
1 parent 47ed656f5b
commit 5f637f72d4
2 files changed
+41 -1

No files matched your search

@@ -69,7 +69,7 @@ namespace CalamityMod.Graphics.Renderers.CalamityRenderers
return false;
// Safety check for weird MP bug when getting global npcs.
if (!npc.TryGetGlobalNPC<CalamityGlobalNPC>(out var calNPC) || !npc.TryGetGlobalNPC<CalamityPolarityNPC>(out var polNPC))
if (!npc.TryGetGlobalNPCSafer<CalamityGlobalNPC>(out var calNPC) || !npc.TryGetGlobalNPCSafer<CalamityPolarityNPC>(out var polNPC))
return false;
// Do not draw if the npc does not have miracle blight, or has the polarity effect.
+40
View File
@@ -0,0 +1,40 @@
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
{
/// <summary>
/// Actual "Thread-Safe" version of TryGetGlobalNPC
/// </summary>
/// <typeparam name="T">GlobalNPC type to get</typeparam>
/// <param name="npc">Input NPC</param>
/// <param name="globalNPC">GlobalNPC output</param>
/// <returns>true If we successfully get GlobalNPC. otherwise false</returns>
public static bool TryGetGlobalNPCSafer<T>(this NPC npc, out T globalNPC) where T : GlobalNPC
{
if (npc is null)
{
globalNPC = null;
return false;
}
int slot = ModContent.GetInstance<T>()?.PerEntityIndex ?? -1;
int length = npc.EntityGlobals.Length;
if (slot < 0 || slot >= length)
{
globalNPC = null;
return false;
}
npc.TryGetGlobalNPC(out globalNPC);
return globalNPC != null;
}
}
}