diff --git a/CalamityNetcode.cs b/CalamityNetcode.cs index ce968bbffe..9cca06b026 100644 --- a/CalamityNetcode.cs +++ b/CalamityNetcode.cs @@ -204,21 +204,6 @@ namespace CalamityMod Main.player[reader.ReadInt32()].Calamity().HandleMousePosition(reader); break; - - // - // Difficulty syncs - // - - case CalamityModMessageType.SyncDifficulties: - int sender = reader.ReadInt32(); - CalamityWorld.revenge = reader.ReadBoolean(); - CalamityWorld.death = reader.ReadBoolean(); - //TODO - Something so that other mods that hijack the difficulty ui can also use the remainder of the reader to have their own shit - - if (Main.netMode == NetmodeID.Server) - SyncCalamityWorldDifficulties(sender); - break; - // // Music event syncs // @@ -230,47 +215,6 @@ namespace CalamityMod MusicEventSystem.ReceiveSyncResponse(reader); break; - // - // Bandit refund syncs - // - case CalamityModMessageType.SomeoneGotScammedByTinkerer: - int scammedOne = reader.ReadByte(); - int stolen = reader.Read7BitEncodedInt(); - - CalamityWorld.MoneyStolenByBandit += stolen; - CalamityWorld.Reforges++; - - // Broadcast back for tragic event - // WorldSync DO sync the MoneyStolenByBandit and Refores variable, But spamming SyncWorld is not a ideal action - if (Main.dedServ) - { - ModPacket packet = CalamityMod.Instance.GetPacket(); - packet.Write((byte)CalamityModMessageType.SomeoneGotScammedByTinkerer); - packet.Write((byte)scammedOne); - packet.Write7BitEncodedInt(stolen); - packet.Send(ignoreClient: scammedOne); - } - - break; - - case CalamityModMessageType.WantToRefundReforges: - int requester = reader.ReadByte(); - - // Only Server should handle this action! - if (!Main.dedServ) - break; - - int banditIdx = NPC.FindFirstNPC(ModContent.NPCType()); - if (banditIdx == -1) - break; - - NPC bandit = Main.npc[banditIdx]; - if (bandit == null || !bandit.active) - break; - - THIEF.DoRefund(bandit); - break; - // // Default case: with no idea how long the packet is, we can't safely read data. // Throw an exception now instead of allowing the network stream to corrupt. @@ -332,14 +276,7 @@ namespace CalamityMod if (Main.netMode == NetmodeID.SinglePlayer) return; - var netMessage = CalamityMod.Instance.GetPacket(); - netMessage.Write((byte)CalamityModMessageType.SyncDifficulties); - netMessage.Write(sender); - netMessage.Write(CalamityWorld.revenge); - netMessage.Write(CalamityWorld.death); - - //TODO - Let other mods also add their own bits in that sync. Ideally would be done through the difficultystem itself - netMessage.Send(-1, sender); + SyncDifficultiesPacket.Send(); } public static void NewNPC_ClientSide(Vector2 spawnPosition, int npcType, Player player) @@ -428,7 +365,7 @@ namespace CalamityMod MusicEventSyncResponse, // Bandit Reforge Refund - SomeoneGotScammedByTinkerer, + ScammedByTinkerer, WantToRefundReforges, Reserved = 150 diff --git a/Items/CalamityGlobalItem.cs b/Items/CalamityGlobalItem.cs index 800f18d622..c6d4222ddf 100644 --- a/Items/CalamityGlobalItem.cs +++ b/Items/CalamityGlobalItem.cs @@ -12,6 +12,7 @@ using CalamityMod.Items.Weapons.Melee; using CalamityMod.Items.Weapons.Summon; using CalamityMod.NPCs.Other; using CalamityMod.NPCs.TownNPCs; +using CalamityMod.Packets; using CalamityMod.Particles; using CalamityMod.Projectiles.Magic; using CalamityMod.Projectiles.Melee; @@ -1829,11 +1830,7 @@ namespace CalamityMod.Items if (Main.netMode == NetmodeID.MultiplayerClient) { - ModPacket packet = CalamityMod.Instance.GetPacket(); - packet.Write((byte)CalamityModMessageType.SomeoneGotScammedByTinkerer); - packet.Write((byte)p.whoAmI); - packet.Write7BitEncodedInt(stolen); - packet.Send(); + ScammedByTinkererPacket.Send(stolen); } } } diff --git a/NPCs/TownNPCs/THIEF.cs b/NPCs/TownNPCs/THIEF.cs index 5bf506ef43..1f46f4fe14 100644 --- a/NPCs/TownNPCs/THIEF.cs +++ b/NPCs/TownNPCs/THIEF.cs @@ -3,6 +3,7 @@ using CalamityMod.Items; using CalamityMod.Items.Accessories; using CalamityMod.Items.Pets; using CalamityMod.Items.Weapons.Rogue; +using CalamityMod.Packets; using CalamityMod.Projectiles.Rogue; using CalamityMod.World; using Microsoft.Xna.Framework; @@ -231,10 +232,7 @@ namespace CalamityMod.NPCs.TownNPCs // // Other way possible is to having bandit stolen inventory per player // But I didn't wanted to change system too much - ModPacket packet = CalamityMod.Instance.GetPacket(); - packet.Write((byte)CalamityModMessageType.WantToRefundReforges); - packet.Write((byte)Main.myPlayer); - packet.Send(); + WantToRefundReforgesPacket.Send(); } SoundEngine.PlaySound(SoundID.Coins); // Money dink sound diff --git a/Packets/Worlds/ScammedByTinkererPacket.cs b/Packets/Worlds/ScammedByTinkererPacket.cs new file mode 100644 index 0000000000..504789df6d --- /dev/null +++ b/Packets/Worlds/ScammedByTinkererPacket.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using CalamityMod.World; +using Terraria; + +namespace CalamityMod.Packets +{ + public sealed class ScammedByTinkererPacket : CalamityPacket + { + public static ScammedByTinkererPacket Instance { get; private set; } + + public override byte MessageType => (byte)CalamityModMessageType.ScammedByTinkerer; + + public static void Send(int amountStolenByBandit, int toClient = -1, int ignoreClient = -1) + { + var packet = Instance.CreateBasePacket(); + packet.Write7BitEncodedInt(amountStolenByBandit); + packet.Send(toClient, ignoreClient); + } + + public override void HandlePacket(in BinaryReader packet, int sender) + { + var amountStolenByBandit = packet.Read7BitEncodedInt(); + + CalamityWorld.MoneyStolenByBandit += amountStolenByBandit; + CalamityWorld.Reforges++; + + // Broadcast back for tragic event + // WorldSync DO sync the MoneyStolenByBandit and Refores variable, But spamming SyncWorld is not a ideal action + if (Main.dedServ) + Send(amountStolenByBandit, ignoreClient: sender); + } + } +} diff --git a/Packets/Worlds/SyncDifficultiesPacket.cs b/Packets/Worlds/SyncDifficultiesPacket.cs new file mode 100644 index 0000000000..d205f57d8a --- /dev/null +++ b/Packets/Worlds/SyncDifficultiesPacket.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using CalamityMod.World; +using Terraria; + +namespace CalamityMod.Packets +{ + public sealed class SyncDifficultiesPacket : CalamityPacket + { + // MIGRATED COMMENTS FROM: 'CalamityNetcode.cs' + //TODO - Something so that other mods that hijack the difficulty ui can also use the remainder of the reader to have their own shit + + public static SyncDifficultiesPacket Instance { get; private set; } + + public override byte MessageType => (byte)CalamityModMessageType.SyncDifficulties; + + public static void Send(int toClient = -1, int ignoreClient = -1) + { + var packet = Instance.CreateBasePacket(); + var difficultyFlags = new BitsByte(); + difficultyFlags[0] = CalamityWorld.revenge; + difficultyFlags[1] = CalamityWorld.death; + packet.Write((byte)difficultyFlags); + packet.Send(toClient, ignoreClient); + } + + public override void HandlePacket(in BinaryReader packet, int sender) + { + var difficultyFlags = (BitsByte)packet.ReadByte(); + CalamityWorld.revenge = difficultyFlags[0]; + CalamityWorld.death = difficultyFlags[1]; + + if (Main.dedServ) + Send(ignoreClient: sender); + } + } +} diff --git a/Packets/Worlds/WantToRefundReforgesPacket.cs b/Packets/Worlds/WantToRefundReforgesPacket.cs new file mode 100644 index 0000000000..fd019a0ab4 --- /dev/null +++ b/Packets/Worlds/WantToRefundReforgesPacket.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using CalamityMod.NPCs.TownNPCs; +using CalamityMod.World; +using Terraria; +using Terraria.ModLoader; + +namespace CalamityMod.Packets +{ + public sealed class WantToRefundReforgesPacket : CalamityPacket + { + public static WantToRefundReforgesPacket Instance { get; private set; } + + public override byte MessageType => (byte)CalamityModMessageType.WantToRefundReforges; + + public static void Send(int toClient = -1, int ignoreClient = -1) + { + var packet = Instance.CreateBasePacket(); + packet.Send(toClient, ignoreClient); + } + + public override void HandlePacket(in BinaryReader packet, int sender) + { + // Only Server should handle this action! + if (!Main.dedServ) + return; + + int banditIdx = NPC.FindFirstNPC(ModContent.NPCType()); + if (banditIdx == -1) + return; + + NPC bandit = Main.npc[banditIdx]; + if (bandit == null || !bandit.active) + return; + + THIEF.DoRefund(bandit); + } + } +}