tModLoader v0.11.8.9
A mod to make and play Terraria mods
BuffLoader.cs
Go to the documentation of this file.
1using Microsoft.Xna.Framework;
2using Microsoft.Xna.Framework.Graphics;
3using System;
4using System.Linq;
5using System.Collections.Generic;
6using Terraria.ID;
7using Terraria.Localization;
8
9namespace Terraria.ModLoader
10{
14 public static class BuffLoader
15 {
16 internal static int extraPlayerBuffCount;
17 private static int nextBuff = BuffID.Count;
18 internal static readonly IList<ModBuff> buffs = new List<ModBuff>();
19 internal static readonly IList<GlobalBuff> globalBuffs = new List<GlobalBuff>();
20 private static readonly bool[] vanillaLongerExpertDebuff = new bool[BuffID.Count];
21 private static readonly bool[] vanillaCanBeCleared = new bool[BuffID.Count];
22
23 private delegate void DelegateUpdatePlayer(int type, Player player, ref int buffIndex);
25 private delegate void DelegateUpdateNPC(int type, NPC npc, ref int buffIndex);
27 private static Func<int, Player, int, int, bool>[] HookReApplyPlayer;
28 private static Func<int, NPC, int, int, bool>[] HookReApplyNPC;
29 private delegate void DelegateModifyBuffTip(int type, ref string tip, ref int rare);
31 private static Action<string, List<Vector2>>[] HookCustomBuffTipSize;
32 private static Action<string, SpriteBatch, int, int>[] HookDrawCustomBuffTip;
33
34 static BuffLoader() {
35 for (int k = 0; k < BuffID.Count; k++) {
36 vanillaCanBeCleared[k] = true;
37 }
38 vanillaLongerExpertDebuff[BuffID.Poisoned] = true;
39 vanillaLongerExpertDebuff[BuffID.Darkness] = true;
40 vanillaLongerExpertDebuff[BuffID.Cursed] = true;
41 vanillaLongerExpertDebuff[BuffID.OnFire] = true;
42 vanillaLongerExpertDebuff[BuffID.Bleeding] = true;
43 vanillaLongerExpertDebuff[BuffID.Confused] = true;
44 vanillaLongerExpertDebuff[BuffID.Slow] = true;
45 vanillaLongerExpertDebuff[BuffID.Weak] = true;
46 vanillaLongerExpertDebuff[BuffID.Silenced] = true;
47 vanillaLongerExpertDebuff[BuffID.BrokenArmor] = true;
48 vanillaLongerExpertDebuff[BuffID.CursedInferno] = true;
49 vanillaLongerExpertDebuff[BuffID.Frostburn] = true;
50 vanillaLongerExpertDebuff[BuffID.Chilled] = true;
51 vanillaLongerExpertDebuff[BuffID.Frozen] = true;
52 vanillaLongerExpertDebuff[BuffID.Ichor] = true;
53 vanillaLongerExpertDebuff[BuffID.Venom] = true;
54 vanillaLongerExpertDebuff[BuffID.Blackout] = true;
55 vanillaCanBeCleared[BuffID.PotionSickness] = false;
56 vanillaCanBeCleared[BuffID.Werewolf] = false;
57 vanillaCanBeCleared[BuffID.Merfolk] = false;
58 vanillaCanBeCleared[BuffID.WaterCandle] = false;
59 vanillaCanBeCleared[BuffID.Campfire] = false;
60 vanillaCanBeCleared[BuffID.HeartLamp] = false;
61 vanillaCanBeCleared[BuffID.NoBuilding] = false;
62 }
63
64 internal static int ReserveBuffID() {
65 if (ModNet.AllowVanillaClients) throw new Exception("Adding buffs breaks vanilla client compatibility");
66
67 int reserveID = nextBuff;
68 nextBuff++;
69 return reserveID;
70 }
71
72 public static int BuffCount => nextBuff;
73
77 public static ModBuff GetBuff(int type) {
78 return type >= BuffID.Count && type < BuffCount ? buffs[type - BuffID.Count] : null;
79 }
80
81 internal static void ResizeArrays() {
82 Array.Resize(ref Main.pvpBuff, nextBuff);
83 Array.Resize(ref Main.persistentBuff, nextBuff);
84 Array.Resize(ref Main.vanityPet, nextBuff);
85 Array.Resize(ref Main.lightPet, nextBuff);
86 Array.Resize(ref Main.meleeBuff, nextBuff);
87 Array.Resize(ref Main.debuff, nextBuff);
88 Array.Resize(ref Main.buffNoSave, nextBuff);
89 Array.Resize(ref Main.buffNoTimeDisplay, nextBuff);
90 Array.Resize(ref Main.buffDoubleApply, nextBuff);
91 Array.Resize(ref Main.buffAlpha, nextBuff);
92 Array.Resize(ref Main.buffTexture, nextBuff);
93 Array.Resize(ref Lang._buffNameCache, nextBuff);
94 Array.Resize(ref Lang._buffDescriptionCache, nextBuff);
95 for (int k = BuffID.Count; k < nextBuff; k++) {
96 Lang._buffNameCache[k] = LocalizedText.Empty;
97 Lang._buffDescriptionCache[k] = LocalizedText.Empty;
98 }
99 extraPlayerBuffCount = ModLoader.Mods.Any() ? ModLoader.Mods.Max(m => (int)m.ExtraPlayerBuffSlots) : 0;
100
101 ModLoader.BuildGlobalHook(ref HookUpdatePlayer, globalBuffs, g => g.Update);
102 ModLoader.BuildGlobalHook(ref HookUpdateNPC, globalBuffs, g => g.Update);
103 ModLoader.BuildGlobalHook(ref HookReApplyPlayer, globalBuffs, g => g.ReApply);
104 ModLoader.BuildGlobalHook(ref HookReApplyNPC, globalBuffs, g => g.ReApply);
105 ModLoader.BuildGlobalHook(ref HookModifyBuffTip, globalBuffs, g => g.ModifyBuffTip);
106 ModLoader.BuildGlobalHook(ref HookCustomBuffTipSize, globalBuffs, g => g.CustomBuffTipSize);
107 ModLoader.BuildGlobalHook(ref HookDrawCustomBuffTip, globalBuffs, g => g.DrawCustomBuffTip);
108 }
109
110 internal static void Unload() {
111 buffs.Clear();
112 nextBuff = BuffID.Count;
113 globalBuffs.Clear();
114 }
115
116 internal static bool IsModBuff(int type) {
117 return type >= BuffID.Count;
118 }
119 //in Terraria.Player.UpdateBuffs at end of if else chain add BuffLoader.Update(this.buffType[k], this, ref k);
120 public static void Update(int buff, Player player, ref int buffIndex) {
121 int originalIndex = buffIndex;
122 if (IsModBuff(buff)) {
123 GetBuff(buff).Update(player, ref buffIndex);
124 }
125 foreach (var hook in HookUpdatePlayer) {
126 if (buffIndex != originalIndex) {
127 break;
128 }
129 hook(buff, player, ref buffIndex);
130 }
131 }
132
133 public static void Update(int buff, NPC npc, ref int buffIndex) {
134 if (IsModBuff(buff)) {
135 GetBuff(buff).Update(npc, ref buffIndex);
136 }
137 foreach (var hook in HookUpdateNPC) {
138 hook(buff, npc, ref buffIndex);
139 }
140 }
141
142 public static bool ReApply(int buff, Player player, int time, int buffIndex) {
143 foreach (var hook in HookReApplyPlayer) {
144 if (hook(buff, player, time, buffIndex)) {
145 return true;
146 }
147 }
148 if (IsModBuff(buff)) {
149 return GetBuff(buff).ReApply(player, time, buffIndex);
150 }
151 return false;
152 }
153
154 public static bool ReApply(int buff, NPC npc, int time, int buffIndex) {
155 foreach (var hook in HookReApplyNPC) {
156 if (hook(buff, npc, time, buffIndex)) {
157 return true;
158 }
159 }
160 if (IsModBuff(buff)) {
161 return GetBuff(buff).ReApply(npc, time, buffIndex);
162 }
163 return false;
164 }
165
166 public static bool LongerExpertDebuff(int buff) {
168 }
169
170 public static bool CanBeCleared(int buff) {
171 return GetBuff(buff)?.canBeCleared ?? vanillaCanBeCleared[buff];
172 }
173
174 public static void ModifyBuffTip(int buff, ref string tip, ref int rare) {
175 if (IsModBuff(buff)) {
176 GetBuff(buff).ModifyBuffTip(ref tip, ref rare);
177 }
178 foreach (var hook in HookModifyBuffTip) {
179 hook(buff, ref tip, ref rare);
180 }
181 }
182
183 public static void CustomBuffTipSize(string buffTip, List<Vector2> sizes) {
184 foreach (var hook in HookCustomBuffTipSize) {
185 hook(buffTip, sizes);
186 }
187 }
188
189 public static void DrawCustomBuffTip(string buffTip, SpriteBatch spriteBatch, int originX, int originY) {
190 foreach (var hook in HookDrawCustomBuffTip) {
191 hook(buffTip, spriteBatch, originX, originY);
192 }
193 }
194 }
195}
This serves as the central class from which buff-related functions are supported and carried out.
Definition: BuffLoader.cs:15
static Func< int, Player, int, int, bool >[] HookReApplyPlayer
Definition: BuffLoader.cs:27
static DelegateModifyBuffTip[] HookModifyBuffTip
Definition: BuffLoader.cs:30
static bool ReApply(int buff, NPC npc, int time, int buffIndex)
Definition: BuffLoader.cs:154
static Func< int, NPC, int, int, bool >[] HookReApplyNPC
Definition: BuffLoader.cs:28
static bool ReApply(int buff, Player player, int time, int buffIndex)
Definition: BuffLoader.cs:142
static Action< string, SpriteBatch, int, int >[] HookDrawCustomBuffTip
Definition: BuffLoader.cs:32
static Action< string, List< Vector2 > >[] HookCustomBuffTipSize
Definition: BuffLoader.cs:31
static void CustomBuffTipSize(string buffTip, List< Vector2 > sizes)
Definition: BuffLoader.cs:183
static ModBuff GetBuff(int type)
Gets the ModBuff instance with the given type. If no ModBuff with the given type exists,...
Definition: BuffLoader.cs:77
static void ModifyBuffTip(int buff, ref string tip, ref int rare)
Definition: BuffLoader.cs:174
delegate void DelegateUpdateNPC(int type, NPC npc, ref int buffIndex)
static readonly bool[] vanillaLongerExpertDebuff
Definition: BuffLoader.cs:20
static void DrawCustomBuffTip(string buffTip, SpriteBatch spriteBatch, int originX, int originY)
Definition: BuffLoader.cs:189
static void Update(int buff, Player player, ref int buffIndex)
Definition: BuffLoader.cs:120
static readonly bool[] vanillaCanBeCleared
Definition: BuffLoader.cs:21
delegate void DelegateUpdatePlayer(int type, Player player, ref int buffIndex)
static void Update(int buff, NPC npc, ref int buffIndex)
Definition: BuffLoader.cs:133
static bool CanBeCleared(int buff)
Definition: BuffLoader.cs:170
static DelegateUpdateNPC[] HookUpdateNPC
Definition: BuffLoader.cs:26
delegate void DelegateModifyBuffTip(int type, ref string tip, ref int rare)
static DelegateUpdatePlayer[] HookUpdatePlayer
Definition: BuffLoader.cs:24
static bool LongerExpertDebuff(int buff)
Definition: BuffLoader.cs:166
This class serves as a place for you to define a new buff and how that buff behaves.
Definition: ModBuff.cs:7
bool canBeCleared
Whether or not it is always safe to call Player.DelBuff on this buff. Setting this to false will prev...
Definition: ModBuff.cs:52
virtual void ModifyBuffTip(ref string tip, ref int rare)
Allows you to modify the tooltip that displays when the mouse hovers over the buff icon,...
Definition: ModBuff.cs:104
virtual bool ReApply(Player player, int time, int buffIndex)
Allows to you make special things happen when adding this buff to a player when the player already ha...
Definition: ModBuff.cs:90
bool longerExpertDebuff
If this buff is a debuff, setting this to true will make this buff last twice as long on players in e...
Definition: ModBuff.cs:50
virtual void Update(Player player, ref int buffIndex)
Allows you to make this buff give certain effects to the given player. If you remove the buff from th...
Definition: ModBuff.cs:78
static bool AllowVanillaClients
Definition: ModNet.cs:53