-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExtraPylons.cs
More file actions
109 lines (95 loc) · 3.2 KB
/
ExtraPylons.cs
File metadata and controls
109 lines (95 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System.IO;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
namespace ExtraPylons
{
public class ExtraPylons : Mod
{
public static ExtraPylons Instance => ModContent.GetInstance<ExtraPylons>();
// Network opcodes
private const byte OpRegistrySync = 1; // Server -> Client : full registry
private const byte OpRegistryRequest = 2; // Client -> Server : request registry
private const byte OpPylonChangeRequest = 3; // Client -> Server : inform server of place/remove (delta)
private const byte OpPylonChangeBroadcast = 4; // Server -> Client : small delta (itemType,newCount) - kept for compatibility
public override void HandlePacket(BinaryReader reader, int whoAmI)
{
byte opcode = reader.ReadByte();
// Server -> client: receive full registry
if (opcode == OpRegistrySync)
{
PylonCodes.PylonsCheckList.PylonRegistry.Clear();
ushort count = reader.ReadUInt16();
for (int i = 0; i < count; i++)
{
int itemType = reader.ReadInt32();
int tileType = reader.ReadInt32();
int placed = reader.ReadInt32();
string source = reader.ReadString();
int style = reader.ReadInt32();
PylonCodes.PylonsCheckList.PylonRegistry.RegisterPylon(itemType, tileType, source, style);
PylonCodes.PylonsCheckList.PylonRegistry.SetPlacedInWorldAmountByItem(itemType, placed);
}
return;
}
// Client -> server: registry request
if (opcode == OpRegistryRequest)
{
if (Main.netMode == NetmodeID.Server)
SendPylonRegistry(whoAmI);
return;
}
// Client -> server: place/remove notification (delta)
if (opcode == OpPylonChangeRequest)
{
if (Main.netMode != NetmodeID.Server) return;
int itemType = reader.ReadInt32();
int delta = reader.ReadInt32();
if (delta > 0)
PylonCodes.PylonsCheckList.PylonRegistry.IncrementPlacedInWorldAmountByItem(itemType);
else
PylonCodes.PylonsCheckList.PylonRegistry.DecrementPlacedInWorldAmountByItem(itemType);
// Broadcast authoritative full registry to all clients to keep everyone consistent.
SendPylonRegistry();
return;
}
// Server -> client: small authoritative change (kept for compatibility with other code paths)
if (opcode == OpPylonChangeBroadcast)
{
if (Main.netMode == NetmodeID.MultiplayerClient)
{
int itemType = reader.ReadInt32();
int newCount = reader.ReadInt32();
PylonCodes.PylonsCheckList.PylonRegistry.SetPlacedInWorldAmountByItem(itemType, newCount);
}
return;
}
}
/// <summary>
/// Sends the authoritative registry to either all clients (toClient = -1) or a single client index.
/// </summary>
public void SendPylonRegistry(int toClient = -1)
{
var entries = PylonCodes.PylonsCheckList.PylonRegistry.Entries;
var packet = GetPacket();
packet.Write(OpRegistrySync);
ushort count = (ushort)(entries?.Count ?? 0);
packet.Write(count);
if (entries != null)
{
foreach (var e in entries)
{
packet.Write(e.ItemType);
packet.Write(e.TileType);
packet.Write(e.PlacedInWorldAmount);
packet.Write(e.Source ?? string.Empty);
packet.Write(e.Style);
}
}
if (toClient >= 0)
packet.Send(toClient);
else
packet.Send();
}
}
}