Skip to content

fix/terraria 1.4.5.7 port - #283

Open
bulkinglb wants to merge 2 commits into
Pryaxis:general-develfrom
bulkinglb:fix/terraria-1457-npc-spawning
Open

fix/terraria 1.4.5.7 port#283
bulkinglb wants to merge 2 commits into
Pryaxis:general-develfrom
bulkinglb:fix/terraria-1457-npc-spawning

Conversation

@bulkinglb

Copy link
Copy Markdown

Stacked on #282 (@hufang360) — that PR gets 1.4.5.7 building, this makes NPCs actually spawn. Reduces to one commit once #282 merges.

Bug (from #281): server boots and clients connect, but no enemies ever spawn. Town NPCs are fine.

Cause: OTAPI 3.3.12 changed Hooks.NPC.InvokeCreate to take the slot and generation, so the hook is now responsible for placing the NPC in Main.npc. 3.3.11 did that inside NewNPC (Main.npc[index] = nPC; nPC.whoAmI = index; nPC.ResetForNewNPC();). 3.3.12 dropped those lines but its fallback still returns a bare new NPC(). Nothing subscribes to Hooks.NPC.Create, so every spawn applied SetDefaults/active/timeLeft to a detached instance that was never stored Main.npc[slot] stayed empty and GetAvailableNPCSlot handed back the same slot forever.

Fix: subscribe to Hooks.NPC.Create and use the engine's own factory NPC.NewNPCInstanceInSlot(slot, generation) — it stores the instance, sets whoAmI, bumps the generation clients use for recycled slots, and clears stale cross-references via ResetNPCSlotData. OTAPI should arguably fix its fallback too; this makes TSAPI correct either way. Tested: .NET 9, linux-arm64, vanilla 1.4.5.7 client. Before: zero non-town NPCs, NewNPC called repeatedly always returning the same slot. After: enemies spawn, despawn and sync normally.

Thanks @hufang360 for #282, @ZakFahey for the TShock-side port in Pryaxis/TShock#3299, and @bryldd for digging into the PR before.

@bulkinglb bulkinglb changed the title Fix/terraria 1457 npc spawning fix/terraria 1.4.5.7 npc spawning Aug 22, 2026
@bulkinglb
bulkinglb force-pushed the fix/terraria-1457-npc-spawning branch from 42dec5f to 240129d Compare August 22, 2026 16:42
@bulkinglb bulkinglb changed the title fix/terraria 1.4.5.7 npc spawning fix/terraria 1.4.5.7 port Aug 22, 2026
Comment on lines +74 to +80
static void OnCreate(object sender, Hooks.NPC.CreateEventArgs e)
{
// OTAPI's fallback returns a detached NPC; this one lands in the slot with a fresh generation.
if (e.Npc == null && e.Slot >= 0 && e.Slot < Main.npc.Length)
e.Npc = NPC.NewNPCInstanceInSlot(e.Slot, (byte)e.Generation);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be removed - otapi 3.3.13 fixes this internally.

@hufang360

Copy link
Copy Markdown

Hi @SignatureBeef,

I'm the author of #282, which overlaps heavily with this PR. I'd like to fold my changes into #283 so we don't keep two parallel 1.4.5.7 ports open.

What I've verified so far with OTAPI.Upcoming 3.3.13 (the fix for the NPC spawning issue):

  • TSAPI builds clean on net9 with 3.3.13.
  • The full TShock stack boots against the vanilla 1.4.5.7 server: Protocol v1.4.5.7 (325), world generation and listen OK.

Since 3.3.13 fixes the NPC create path internally, the OnCreate workaround in NpcHooks.cs can be dropped once we bump — consistent with your earlier comment.

How would you prefer to proceed?

  1. Keep fix/terraria 1.4.5.7 port #283 as the base, bump OTAPI to 3.3.13 and remove the OnCreate fallback — I'm happy to push those changes to this branch or provide a patch;
  2. Or close both and consolidate into one clean PR.

I'll close #282 either way. Happy to help with whatever shape you prefer.

3.3.13 restores the NPC create fallback internally, so the OnCreate
workaround this branch added is no longer needed.
@bulkinglb
bulkinglb force-pushed the fix/terraria-1457-npc-spawning branch from 240129d to 54fdfaf Compare August 23, 2026 08:51
@bulkinglb

bulkinglb commented Aug 23, 2026

Copy link
Copy Markdown
Author

@hufang360 option 1 works for me, and it's done — #283 is now your commit plus a one-line bump to 3.3.13, with the OnCreate workaround removed per @SignatureBeef's review. NpcHooks.cs is untouched from your version again.

Thanks for verifying 3.3.13.

TShock side is Pryaxis/TShock#3301, also bumped to 3.3.13.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants