tModLoader v0.11.8.9
A mod to make and play Terraria mods
ModPrefix.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text.RegularExpressions;
5using Terraria.ID;
6using Terraria.Utilities;
7
8namespace Terraria.ModLoader
9{
10 public abstract class ModPrefix
11 {
12 private static byte nextPrefix = PrefixID.Count;
13
14 // TODO storing twice? could see a better implementation
15 internal static readonly IList<ModPrefix> prefixes = new List<ModPrefix>();
16 internal static readonly IDictionary<PrefixCategory, IList<ModPrefix>> categoryPrefixes;
17
18 static ModPrefix() {
19 categoryPrefixes = new Dictionary<PrefixCategory, IList<ModPrefix>>();
20 foreach (PrefixCategory category in Enum.GetValues(typeof(PrefixCategory))) {
21 categoryPrefixes[category] = new List<ModPrefix>();
22 }
23 }
24
25 internal static byte ReservePrefixID() {
26 if (ModNet.AllowVanillaClients) throw new Exception("Adding items breaks vanilla client compatibility");
27 if (nextPrefix == 0) throw new Exception("Prefix ID limit has been broken");
28
29 byte reserveID = nextPrefix;
30 nextPrefix++;
31 return reserveID;
32 }
33
40 public static ModPrefix GetPrefix(byte type) {
41 return type >= PrefixID.Count && type < PrefixCount ? prefixes[type - PrefixID.Count] : null;
42 }
43
49 public static List<ModPrefix> GetPrefixesInCategory(PrefixCategory category) {
50 return new List<ModPrefix>(categoryPrefixes[category]);
51 }
52
53 public static byte PrefixCount => nextPrefix;
54
55 internal static void ResizeArrays() {
56 Array.Resize(ref Lang.prefix, nextPrefix);
57 }
58
59 internal static void Unload() {
60 prefixes.Clear();
61 nextPrefix = PrefixID.Count;
62 foreach (PrefixCategory category in Enum.GetValues(typeof(PrefixCategory))) {
63 categoryPrefixes[category].Clear();
64 }
65 }
66
70 internal static void Roll(Item item, ref int prefix, int vanillaWeight, params PrefixCategory[] categories) {
71 WeightedRandom<byte> wr = new WeightedRandom<byte>();
72 foreach (PrefixCategory category in categories)
73 foreach (ModPrefix modPrefix in categoryPrefixes[category].Where(x => x.CanRoll(item)))
74 wr.Add(modPrefix.Type, modPrefix.RollChance(item));
75
76 if (vanillaWeight > 0)
77 wr.Add((byte)prefix, vanillaWeight);
78
79 prefix = wr.Get();
80 }
81
82 public Mod mod {
83 get;
84 internal set;
85 }
86
87 public string Name {
88 get;
89 internal set;
90 }
91
92 public byte Type {
93 get;
94 internal set;
95 }
96
98 get;
99 internal set;
100 }
101
107 public virtual float RollChance(Item item) {
108 return 1f;
109 }
110
117 public virtual bool CanRoll(Item item) {
118 return RollChance(item) > 0;
119 }
120
124 public virtual PrefixCategory Category => PrefixCategory.Custom;
125
126 public virtual bool Autoload(ref string name) {
127 return mod.Properties.Autoload;
128 }
129
130 public virtual void AutoDefaults() {
132 DisplayName.SetDefault(Regex.Replace(Name, "([A-Z])", " $1").Trim());
133 }
134
138 public virtual void SetDefaults() {
139 }
140
144 public virtual void SetStats(ref float damageMult, ref float knockbackMult, ref float useTimeMult,
145 ref float scaleMult, ref float shootSpeedMult, ref float manaMult, ref int critBonus) {
146 }
147
152 public virtual void ValidateItem(Item item, ref bool invalid) {
153 }
154
159 public virtual void Apply(Item item) {
160 }
161
165 public virtual void ModifyValue(ref float valueMult) {
166 }
167 }
168
169 public enum PrefixCategory
170 {
174 Melee,
178 Ranged,
182 Magic,
183 AnyWeapon,
184 Accessory,
188 Custom
189 }
190}
Mod is an abstract class that you will override. It serves as a central place from which the mod's co...
Definition: Mod.cs:25
ModProperties Properties
Definition: Mod.cs:52
static bool AllowVanillaClients
Definition: ModNet.cs:53
static List< ModPrefix > GetPrefixesInCategory(PrefixCategory category)
Returns a list of all modded prefixes of a certain category.
Definition: ModPrefix.cs:49
virtual bool CanRoll(Item item)
Returns if your ModPrefix can roll on the given item By default returns RollChance(item) > 0
Definition: ModPrefix.cs:117
virtual void ValidateItem(Item item, ref bool invalid)
Validates whether this prefix with the custom data stats set from SetStats is allowed on the given it...
Definition: ModPrefix.cs:152
virtual void SetStats(ref float damageMult, ref float knockbackMult, ref float useTimeMult, ref float scaleMult, ref float shootSpeedMult, ref float manaMult, ref int critBonus)
Sets the stat changes for this prefix. If data is not already pre-stored, it is best to store custom ...
Definition: ModPrefix.cs:144
virtual float RollChance(Item item)
The roll chance of your prefix relative to a vanilla prefix, 1f by default.
Definition: ModPrefix.cs:107
virtual void ModifyValue(ref float valueMult)
Allows you to modify the sell price of the item based on the prefix or changes in custom data stats....
Definition: ModPrefix.cs:165
virtual bool Autoload(ref string name)
Definition: ModPrefix.cs:126
virtual PrefixCategory Category
The category your prefix belongs to, PrefixCategory.Custom by default
Definition: ModPrefix.cs:124
virtual void AutoDefaults()
Definition: ModPrefix.cs:130
static ModPrefix GetPrefix(byte type)
Returns the ModPrefix associated with specified type If not a ModPrefix, returns null.
Definition: ModPrefix.cs:40
ModTranslation DisplayName
Definition: ModPrefix.cs:97
virtual void Apply(Item item)
Applies the custom data stats set in SetStats to the given item.
Definition: ModPrefix.cs:159
virtual void SetDefaults()
Allows you to set the prefix's name/translations and to set its category.
Definition: ModPrefix.cs:138
@ Magic
Can modify the mana usage of the weapon
@ Custom
Will not appear by default. Useful as prefixes for your own damage type.
@ Ranged
Can modify the shoot speed of the weapon
@ Melee
Can modify the size of the weapon
bool Autoload
Whether or not this mod will autoload content by default. Autoloading content means you do not need t...