-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExtraLoadoutsMod.Net.cs
More file actions
176 lines (138 loc) · 6.48 KB
/
ExtraLoadoutsMod.Net.cs
File metadata and controls
176 lines (138 loc) · 6.48 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System.IO;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
namespace ExtraLoadouts;
public sealed partial class ExtraLoadoutsMod : Mod {
public const byte PACKET_SYNC_ENTIRE_LOADOUT = 0;
public const byte PACKET_SYNC_LOADOUT_SLOT = 1;
public const byte PACKET_SYNC_LOADOUT_SELECTION = 2;
public const byte PACKET_SYNC_LOADOUT_HIDE = 3;
public override void HandlePacket(BinaryReader reader, int whoAmI) {
switch (reader.ReadByte()) {
case PACKET_SYNC_ENTIRE_LOADOUT:
Receieve_SyncEntireLoadout(reader, whoAmI);
break;
case PACKET_SYNC_LOADOUT_SLOT:
Recieve_SyncLoadoutSlot(reader, whoAmI);
break;
case PACKET_SYNC_LOADOUT_SELECTION:
Receive_SyncLoadoutSelection(reader, whoAmI);
break;
case PACKET_SYNC_LOADOUT_HIDE:
Receive_SyncLoadoutHide(reader, whoAmI);
break;
}
}
#region SyncEntireLoadout
public static void Send_SyncEntireLoadout(int toWho, Player fromPlayer, int loadoutIndex) {
ModPacket packet = ModContent.GetInstance<ExtraLoadoutsMod>().GetPacket();
packet.Write(PACKET_SYNC_ENTIRE_LOADOUT);
packet.Write7BitEncodedInt(fromPlayer.whoAmI);
packet.Write7BitEncodedInt(loadoutIndex);
fromPlayer.GetModPlayer<LoadoutPlayer>().ExtraLoadouts[loadoutIndex].Send(packet);
packet.Send(toWho, fromPlayer.whoAmI);
}
public static void Receieve_SyncEntireLoadout(BinaryReader reader, int fromWho) {
int whosLoadout = reader.Read7BitEncodedInt();
int loadout = reader.Read7BitEncodedInt();
var player = Main.player[whosLoadout];
player.GetModPlayer<LoadoutPlayer>().ExtraLoadouts[loadout].Receieve(reader);
if (Main.netMode == NetmodeID.Server) {
Send_SyncEntireLoadout(-1, player, loadout);
}
}
#endregion
#region SyncLoadoutSlot
public static void Send_SyncLoadoutSlot(int toWho, Player fromPlayer, int extraLoadoutIndex, bool modded, bool dye, int slot) {
ModPacket packet = ModContent.GetInstance<ExtraLoadoutsMod>().GetPacket();
packet.Write(PACKET_SYNC_LOADOUT_SLOT);
packet.Write7BitEncodedInt(fromPlayer.whoAmI);
packet.Write7BitEncodedInt(extraLoadoutIndex);
packet.WriteFlags(modded, dye);
packet.Write7BitEncodedInt(slot);
Item item = SyncLoadoutSlot_GetItem(fromPlayer, extraLoadoutIndex, modded, dye, slot);
ItemIO.Send(item, packet, writeStack: true);
packet.Send(toWho, fromPlayer.whoAmI);
}
public static void Recieve_SyncLoadoutSlot(BinaryReader reader, int fromWho) {
int whosLoadout = reader.Read7BitEncodedInt();
int extraLoadoutIndex = reader.Read7BitEncodedInt();
reader.ReadFlags(out bool modded, out bool dye);
int slot = reader.Read7BitEncodedInt();
Item item = ItemIO.Receive(reader, readStack: true);
var player = Main.player[whosLoadout];
SyncLoadoutSlot_GetItem(player, extraLoadoutIndex, modded, dye, slot) = item;
if (Main.netMode == NetmodeID.Server) {
Send_SyncLoadoutSlot(-1, player, extraLoadoutIndex, modded, dye, slot);
}
}
private static ref Item SyncLoadoutSlot_GetItem(Player player, int extraLoadoutIndex, bool modded, bool dye, int slot) {
var modPlayer = player.GetModPlayer<LoadoutPlayer>().ExtraLoadouts[extraLoadoutIndex];
if (!modded) {
if (!dye) {
return ref modPlayer.Vanilla.Armor[slot];
} else {
return ref modPlayer.Vanilla.Dye[slot];
}
} else {
if (!dye) {
return ref modPlayer.ModLoader.ExAccessorySlot[slot];
} else {
return ref modPlayer.ModLoader.ExDyesAccessory[slot];
}
}
}
#endregion
#region SyncLoadoutSelection
public static void Send_SyncLoadoutSelection(int toWho, Player ofPlayer) {
ModPacket packet = ModContent.GetInstance<ExtraLoadoutsMod>().GetPacket();
packet.Write(PACKET_SYNC_LOADOUT_SELECTION);
packet.Write7BitEncodedInt(ofPlayer.whoAmI);
packet.Write7BitEncodedInt(ofPlayer.GetModPlayer<LoadoutPlayer>().CurrentExtraLoadoutIndex);
packet.Send(toWho, ofPlayer.whoAmI);
}
public static void Receive_SyncLoadoutSelection(BinaryReader reader, int fromWho) {
int whosLoadout = reader.Read7BitEncodedInt();
int loadoutIndex = reader.Read7BitEncodedInt();
var player = Main.player[whosLoadout];
player.GetModPlayer<LoadoutPlayer>().CurrentExtraLoadoutIndex = loadoutIndex;
if (Main.netMode == NetmodeID.Server) {
Send_SyncLoadoutSelection(-1, player);
}
}
#endregion
#region SyncLoadoutHide
public static void Send_SyncLoadoutHide(int toWho, Player fromPlayer, int loadoutIndex, bool modded, int slot) {
ModPacket packet = ModContent.GetInstance<ExtraLoadoutsMod>().GetPacket();
packet.Write(PACKET_SYNC_LOADOUT_HIDE);
var modPlayer = fromPlayer.GetModPlayer<LoadoutPlayer>();
packet.Write7BitEncodedInt(fromPlayer.whoAmI);
packet.Write7BitEncodedInt(loadoutIndex);
packet.Write(modded, SyncLoadoutHide_GetFlag(fromPlayer, loadoutIndex, modded, slot));
packet.Write7BitEncodedInt(slot);
packet.Send(toWho, ignoreClient: fromPlayer.whoAmI);
}
public static void Receive_SyncLoadoutHide(BinaryReader reader, int fromWho) {
int whosLoadout = reader.Read7BitEncodedInt();
int loadoutIndex = reader.Read7BitEncodedInt();
reader.ReadFlags(out bool modded, out bool hide);
int slot = reader.Read7BitEncodedInt();
var player = Main.player[whosLoadout];
var loadout = player.GetModPlayer<LoadoutPlayer>().ExtraLoadouts[loadoutIndex];
SyncLoadoutHide_GetFlag(player, loadoutIndex, modded, slot) = hide;
if (Main.netMode == NetmodeID.Server) {
Send_SyncLoadoutHide(-1, player, loadoutIndex, modded, slot);
}
}
private static ref bool SyncLoadoutHide_GetFlag(Player player, int loadoutIndex, bool modded, int slot) {
var modPlayer = player.GetModPlayer<LoadoutPlayer>().ExtraLoadouts[loadoutIndex];
if (!modded) {
return ref modPlayer.Vanilla.Hide[slot];
} else {
return ref modPlayer.ModLoader.ExHideAccessory[slot];
}
}
#endregion
}