-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEnemyModifiers.cs
More file actions
102 lines (83 loc) · 3.48 KB
/
EnemyModifiers.cs
File metadata and controls
102 lines (83 loc) · 3.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FargoEnemyModifiers.Modifiers;
using FargoEnemyModifiers.NetCode;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
namespace FargoEnemyModifiers
{
public class EnemyModifiers : Mod
{
public static Mod Instance;
//alphabetical list for forcing specific one
//public static List<Modifier> Modifiers;
public static Dictionary<ModifierID, Modifier> Modifiers;
public static TModifier GetModifier<TModifier>() where TModifier : Modifier =>
(TModifier) Modifiers.FirstOrDefault(x => x.Value.GetType() == typeof(TModifier)).Value;
public static Modifier GetModifier(Modifier modifier) =>
Modifiers.FirstOrDefault(x => x.Value.GetType() == modifier.GetType()).Value;
public override void PostSetupContent()
{
Instance = this;
Modifiers = new Dictionary<ModifierID, Modifier>();
foreach (Type type in this.Code.GetTypes().Where(x =>
!x.IsAbstract && x.IsSubclassOf(typeof(Modifier)) && x.GetConstructor(new Type[0]) != null))
{
if (Activator.CreateInstance(type) is Modifier modifier && modifier.AutoLoad())
{
Modifiers.Add(modifier.ModifierID, modifier);
}
}
}
public override void Unload()
{
Modifiers = null;
}
public override void HandlePacket(BinaryReader reader, int whoAmI)
{
PacketID id = (PacketID)reader.ReadByte();
switch (id)
{
// Packet:
// byte: packet id
// byte: npc whoAmI
// byte: modifier amount
// byte[]: modifiers
case PacketID.MobSpawn: // Server is sending modifier data to clients
if (Main.netMode != NetmodeID.MultiplayerClient) return;
int npcIndex = reader.ReadByte();
NPC npc = Main.npc[npcIndex]; // npc whoAmI
if (!npc.active)
{
reader.ReadBytes(reader.ReadByte()); // skip modifiers
return; // npc is dead
}
EnemyModifiersGlobalNPC modNpc = npc.GetGlobalNPC<EnemyModifiersGlobalNPC>();
int[] modifiers = new int[reader.ReadByte()]; // modifier amount
for (int i = 0; i < modifiers.Length; i++)
{
modifiers[i] = reader.ReadByte();
}
foreach (ModifierID modifier in modifiers)
{
modNpc.modifierTypes.Add(modifier);
modNpc.ApplyModifier(npc, modifier);
}
modNpc.FinalizeModifierName(npc);
break;
// Packet:
// byte: packet id
// byte: npc whoAmI
case PacketID.ClientCausedDespawn:
if (Main.netMode != NetmodeID.Server) return;
npcIndex = reader.ReadByte();
NPC npcToDespawn = Main.npc[npcIndex];
npcToDespawn.active = false;
break;
}
}
}
}