diff --git a/CalamityNetcode.cs b/CalamityNetcode.cs index 88e29c1b2a..ca5e9224b7 100644 --- a/CalamityNetcode.cs +++ b/CalamityNetcode.cs @@ -1,4 +1,5 @@ using CalamityMod.Events; +using CalamityMod.Items; using CalamityMod.NPCs; using CalamityMod.NPCs.NormalNPCs; using CalamityMod.NPCs.Providence; @@ -110,6 +111,10 @@ namespace CalamityMod if (Main.netMode != NetmodeID.MultiplayerClient) NPC.NewNPC(x, y, ModContent.NPCType()); break; + case CalamityModMessageType.DeleteAllSuperDummies: + if (Main.netMode != NetmodeID.MultiplayerClient) + SuperDummy.DeleteDummies(); + break; case CalamityModMessageType.ServersideSpawnOldDuke: byte playerIndex2 = reader.ReadByte(); CalamityUtils.SpawnOldDuke(playerIndex2); @@ -271,7 +276,8 @@ namespace CalamityMod // Syncs for specific bosses or entities SyncCalamityNPCAIArray, - SpawnSuperDummy, // TODO -- super dummies STILL don't work in multiplayer + SpawnSuperDummy, + DeleteAllSuperDummies, ServersideSpawnOldDuke, DoGCountdownSync, // TODO -- this gets written in about six thousand places which all need to be individually evaluated ArmoredDiggerCountdownSync, // TODO -- remove this mechanic entirely diff --git a/Items/SuperDummy.cs b/Items/SuperDummy.cs index e692e34262..ef79d67a15 100644 --- a/Items/SuperDummy.cs +++ b/Items/SuperDummy.cs @@ -35,21 +35,41 @@ namespace CalamityMod.Items return true; } + public static void DeleteDummies() + { + for (int i = 0; i < Main.maxNPCs; i++) + { + NPC npc = Main.npc[i]; + if (npc.type == ModContent.NPCType() && npc.active) + { + npc.life = 0; + npc.active = false; + + if (Main.netMode == NetmodeID.Server) + NetMessage.SendData(MessageID.SyncNPC, -1, -1, null, i); + } + } + } + public override bool UseItem(Player player) { if (player.altFunctionUse == 2) { - for (int i = 0; i < Main.maxNPCs; i++) + if (Main.myPlayer == player.whoAmI) { - NPC npc = Main.npc[i]; - if (npc.type == ModContent.NPCType() && npc.active) + if (Main.netMode == NetmodeID.SinglePlayer) + DeleteDummies(); + + // A custom packet must be sent so that the deletion can be done on the server. This hook does not run there. + // Why? Well, netUpdate/MessageID.SyncNPC packets do send data to the server. The only send data to other clients. What this means is that prior to this fix + // what would happen was every client EXCEPT THE SERVER would be told by this player that the dummies disappeared. + // However, since the server isn't notified, it is inevitable that the server will do a sync of its own, unaware that the + // dummies are gone, and cause them to reappear, making the deletion moot. + else { - if (player.whoAmI == Main.myPlayer) - { - npc.active = false; - npc.netUpdate = true; - } - Main.PlaySound(SoundID.NPCDeath2, npc.Center); + var netMessage = mod.GetPacket(); + netMessage.Write((byte)CalamityModMessageType.DeleteAllSuperDummies); + netMessage.Send(); } } }