-
Notifications
You must be signed in to change notification settings - Fork 1
/
LoadoutPlayer.cs
205 lines (168 loc) · 8.74 KB
/
LoadoutPlayer.cs
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using Terraria;
using Terraria.Audio;
using Terraria.GameContent.Drawing;
using Terraria.GameInput;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using Terraria.UI;
// All the commented out netcode is from previous iterations of the syncing system
// If you are brave, you may attempt to replace the hack
// in EnterWorld() with a proper fix, but after a day of working away at it I had
// no luck.
namespace ExtraLoadouts {
public sealed class LoadoutPlayer : ModPlayer {
public EquipmentLoadout CurrentExLoadout => ExLoadouts[CurrentExLoadoutIndex];
public readonly EquipmentLoadout[] ExLoadouts = [
new(),
new(),
new(),
];
public int CurrentExLoadoutIndex { get; set; }
public override void Initialize() {
CurrentExLoadoutIndex = -1;
}
public override void SaveData(TagCompound tag) {
if (CurrentExLoadoutIndex != -1) {
tag.Add(nameof(CurrentExLoadoutIndex), CurrentExLoadoutIndex);
}
for (int i = 0; i < ExLoadouts.Length; i++) {
tag.Add(nameof(ExLoadouts) + i, ExLoadouts[i]);
}
}
public override void LoadData(TagCompound tag) {
if (tag.TryGet(nameof(CurrentExLoadoutIndex), out int currentExLoadoutIndex)) {
CurrentExLoadoutIndex = currentExLoadoutIndex;
}
for (int i = 0; i < ExLoadouts.Length; i++) {
if (tag.TryGet(nameof(ExLoadouts) + i, out EquipmentLoadout loadout)) {
ExLoadouts[i] = loadout;
}
}
}
public override void OnEnterWorld() {
// Without this, the first switch from an ex loadout to a vanilla loadout does not sync
// This is an utterly awful hack, but I cannot find another fix
if (Main.netMode == NetmodeID.MultiplayerClient && CurrentExLoadoutIndex >= 0) {
int loadout = CurrentExLoadoutIndex;
Player.TrySwitchingLoadout(Player.CurrentLoadoutIndex);
TrySwitchToExLoadout(loadout);
}
}
public override void SyncPlayer(int toWho, int fromWho, bool newPlayer) {
// for (int i = 0; i < ExtraLoadoutsMod.EXTRA_LOADOUTS; i++) {
// LoadoutSyncing.SyncExLoadout(Player, i, toWho, fromWho);
// }
LoadoutSyncing.SyncExLoadoutSelection(Player, CurrentExLoadoutIndex, false, toWho, fromWho);
// SyncItemArrays(toWho, fromWho);
}
public override void ProcessTriggers(TriggersSet triggersSet) {
// for (int i = 0; i < Main.maxPlayers; i++) {
// if (!Main.player[i].active) {
// return;
// }
// Main.NewText($"{Main.player[i].name}: v{Main.player[i].CurrentLoadoutIndex}, x{Main.player[i].GetModPlayer<LoadoutPlayer>().CurrentExLoadoutIndex}");
// }
for (int i = 0; i < ExtraLoadoutsMod.EXTRA_LOADOUTS; i++) {
if (ModContent.GetInstance<LoadoutKeybinds>().ExLoadoutKeybinds[i].JustPressed) {
if (i < ModContent.GetInstance<LoadoutsConfig>().ExtraLoadouts) {
TrySwitchToExLoadout(i);
}
}
}
}
public void TrySwitchingVanillaToEx(int exLoadoutIndex) {
if (IsPlayerReadyToSwitchLoadouts() && IsExLoadoutIndexValid(exLoadoutIndex)) {
Player.Loadouts[Player.CurrentLoadoutIndex].Swap(Player);
ExLoadouts[exLoadoutIndex].Swap(Player);
CurrentExLoadoutIndex = exLoadoutIndex;
if (Player.whoAmI == Main.myPlayer) {
Main.mouseLeftRelease = false;
// I'm not sure _what_ vanilla's CloneLoadOuts(Player) does and
// this (see code below) crashes because Main.clientPlayer doesn't have ModPlayers
// CloneExLoadouts(Main.clientPlayer);
// CloneLoadouts(Main.clientPlayer);
// NetMessage.TrySendData(MessageID.SyncLoadout, -1, -1, null, Player.whoAmI, Player.CurrentLoadoutIndex);
// LoadoutSyncing.SyncExLoadout(Player, exLoadoutIndex, -1, Player.whoAmI);
LoadoutSyncing.SyncExLoadoutSelection(Player, CurrentExLoadoutIndex, true, -1, Player.whoAmI);
SoundEngine.PlaySound(SoundID.MenuTick);
ParticleOrchestrator.RequestParticleSpawn(clientOnly: false, ParticleOrchestraType.LoadoutChange, new ParticleOrchestraSettings {
PositionInWorld = Player.Center,
UniqueInfoPiece = 0
}, Player.whoAmI);
ItemSlot.RecordLoadoutChange();
}
}
}
public void TrySwitchingExToEx(int exLoadoutIndex) {
if (IsPlayerReadyToSwitchLoadouts() && IsExLoadoutIndexValid(exLoadoutIndex)) {
ExLoadouts[CurrentExLoadoutIndex].Swap(Player);
ExLoadouts[exLoadoutIndex].Swap(Player);
CurrentExLoadoutIndex = exLoadoutIndex;
if (Player.whoAmI == Main.myPlayer) {
Main.mouseLeftRelease = false;
// I'm not sure _what_ vanilla's CloneLayouts(Player) does and
// this (see code below) crashes because Main.clientPlayer doesn't have ModPlayers
// CloneExLoadouts(Main.clientPlayer);
// NetMessage.TrySendData(MessageID.SyncLoadout, -1, -1, null, Player.whoAmI, Player.CurrentLoadoutIndex);
// LoadoutSyncing.SyncExLoadout(Player, exLoadoutIndex, -1, Player.whoAmI);
// LoadoutSyncing.SyncExLoadoutSelection(Player, CurrentExLoadoutIndex, -1, Player.whoAmI);
SoundEngine.PlaySound(SoundID.MenuTick);
ParticleOrchestrator.RequestParticleSpawn(clientOnly: false, ParticleOrchestraType.LoadoutChange, new ParticleOrchestraSettings {
PositionInWorld = Player.Center,
UniqueInfoPiece = 0
}, Player.whoAmI);
ItemSlot.RecordLoadoutChange();
}
}
}
public void TrySwitchToExLoadout(int exLoadoutIndex) {
if (CurrentExLoadoutIndex < 0) {
// We're on a vanilla layout currently
TrySwitchingVanillaToEx(exLoadoutIndex);
}
else {
// We're already on a modded layout
TrySwitchingExToEx(exLoadoutIndex);
}
}
private bool IsPlayerReadyToSwitchLoadouts() {
return Player.whoAmI != Main.myPlayer || (!IsUsingItem() && !Player.CCed && !Player.dead);
}
private bool IsExLoadoutIndexValid(int exLoadoutIndex) {
return exLoadoutIndex != CurrentExLoadoutIndex && exLoadoutIndex >= 0 && exLoadoutIndex < ExLoadouts.Length;
}
private bool IsUsingItem() {
return Player.itemTime > 0 || Player.itemAnimation > 0;
}
public void ClearExForVanilla() {
CurrentExLoadout.Swap(Player);
// LoadoutSyncing.SyncExLoadout(Player, CurrentExLoadoutIndex, -1, Main.myPlayer);
// SyncItemArrays();
CurrentExLoadoutIndex = -1;
// LoadoutSyncing.SyncExLoadoutSelection(Player, CurrentExLoadoutIndex, -1, Main.myPlayer);
}
private void SyncItemArrays(int toWho = -1, int fromWho = -1) {
for (int i = 0; i < Player.armor.Length; i++) {
NetMessage.SendData(MessageID.SyncEquipment, toWho, fromWho, null, Player.whoAmI, PlayerItemSlotID.Armor0 + i, Player.armor[i].prefix);
}
for (int i = 0; i < Player.dye.Length; i++) {
NetMessage.SendData(MessageID.SyncEquipment, toWho, fromWho, null, Player.whoAmI, PlayerItemSlotID.Dye0 + i, Player.dye[i].prefix);
}
}
// private void CloneExLoadouts(Player player) {
// CloneItemArray(Player.armor, player.armor);
// CloneItemArray(Player.dye, player.dye);
// LoadoutPlayer modPlayer = player.GetModPlayer<LoadoutPlayer>();
// for (int i = 0; i < ExEquipmentLoadouts.Length; i++) {
// CloneItemArray(ExEquipmentLoadouts[i].Armor, modPlayer.ExEquipmentLoadouts[i].Armor);
// CloneItemArray(ExEquipmentLoadouts[i].Dye, modPlayer.ExEquipmentLoadouts[i].Dye);
// }
// }
// private void CloneItemArray(Item[] local, Item[] remote) {
// for (int i = 0; i < local.Length; i++) {
// remote[i] = local[i].Clone();
// }
// }
}
}