tModLoader v2024.10
A mod to make and play Terraria mods
Mod Class Reference

Mod is an abstract class that you will override. It serves as a central place from which the mod's contents are stored. It provides methods for you to use or override. More...

Inherited by ModLoaderMod.

Public Member Functions

int AddBossHeadTexture (string texture, int npcType=-1)
 Assigns a head texture that can be used by NPCs on the map. More...
 
void AddConfig (string name, ModConfig mc)
 
bool AddContent (ILoadable instance)
 Call this to manually add the given content instance to the game. More...
 
bool AddContent< T > ()
 Call this to manually add a content instance of the specified type (with a parameterless constructor) to the game. More...
 
int AddNPCHeadTexture (int npcType, string texture)
 Assigns a head texture to the given town NPC type. More...
 
virtual void AddRecipeGroups ()
 Override this method to add recipe groups to this mod. You must add recipe groups by calling the RecipeGroup.RegisterGroup method here. A recipe group is a set of items that can be used interchangeably in the same recipe.
 
virtual void AddRecipes ()
 Override this method to add recipes to the game. It is recommended that you do so through instances of Recipe, since it provides methods that simplify recipe creation.
 
virtual object Call (params object[] args)
 Used for weak inter-mod communication. This allows you to interact with other mods without having to reference their types or namespaces, provided that they have implemented this method.
The Expert Cross Mod Content Guideexplains how to use this hook to implement and utilize cross-mod capabilities.
 
Recipe CloneRecipe (Recipe recipe)
 
virtual void Close ()
 Close is called before Unload, and may be called at any time when mod unloading is imminent (such as when downloading an update, or recompiling) Use this to release any additional file handles, or stop streaming music. Make sure to call base.Close() at the end May be called multiple times before Unload
 
virtual IContentSource CreateDefaultContentSource ()
 
Recipe CreateRecipe (int result, int amount=1)
 
bool FileExists (string name)
 Returns whether or not a file with the specified name exists. Note that this includes file extension and the folder path: "Folders/FileNameWithExtension" More...
 
Find< T > (string name)
 Attempts to find the template instance from this mod with the specified name (not the clone/new instance which gets added to Items/Players/NPCs etc. as the game is played). Caching the result is recommended.This will throw exceptions on failure. More...
 
ModConfig GetConfig (string name)
 
IEnumerable< ILoadableGetContent ()
 Returns all registered content instances that are added by this mod.
This only includes the 'template' instance for each piece of content, not all the clones/new instances which get added to Items/Players/NPCs etc. as the game is played
 
IEnumerable< T > GetContent< T > ()
 Returns all registered content instances that derive from the provided type that are added by this mod.
This only includes the 'template' instance for each piece of content, not all the clones/new instances which get added to Items/Players/NPCs etc. as the game is played More...
 
byte[] GetFileBytes (string name)
 Retrieves the contents of a file packaged within the .tmod file as a byte array. Should be used mainly for non-Asset<T> files. The name should be in the format of "Folders/FileNameWithExtension" starting from your mod's source code folder. Returns null if the file does not exist within the mod. A typical usage of this might be to load a text file containing structured data included within your mod. Make sure the txt file is UTF8 encoded and use the following to retrieve file's text contents: string pointsFileContents = Encoding.UTF8.GetString(Mod.GetFileBytes("data/points.txt")); More...
 
List< string > GetFileNames ()
 Retrieves the names of every file packaged into this mod. Note that this includes extensions, and for images the extension will always be .rawimg. More...
 
Stream GetFileStream (string name, bool newFileStream=false)
 Retrieve contents of files within the .tmod file. More...
 
LocalizedText GetLocalization (string suffix, Func< string > makeDefaultValue=null)
 Returns a LocalizedText for this Mod with the provided suffix . The suffix will be used to generate a key by providing it to GetLocalizationKey(string).
If no existing localization exists for the key, it will be defined so it can be exported to a matching mod localization file. More...
 
string GetLocalizationKey (string suffix)
 Creates a localization key following the pattern of "Mods.{ModName}.{suffix}". Use this with Language.GetOrRegister(string, Func<string>) to retrieve a LocalizedText for custom localization keys. Alternatively GetLocalization(string, Func<string>) can be used directly instead. Custom localization keys need to be registered during the mod loading process to appear automatically in the localization files. More...
 
ModPacket GetPacket (int capacity=256)
 Creates a ModPacket object that you can write to and then send between servers and clients. More...
 
virtual void HandlePacket (BinaryReader reader, int whoAmI)
 Called whenever a net message / packet pertaining to this mod is received from a client (if this is a server) or the server (if this is a client). whoAmI is the ID of whomever sent the packet (equivalent to the Main.myPlayer of the sender), and reader is used to read the binary data of the packet. Note that many packets are sent from a client to the server and then relayed to the remaining clients. The whoAmI when the packet arrives at the remaining clients will be the servers Main.myPlayer, not the original clients Main.myPlayer. For packets only sent from a client to the server, relying on whoAmI to identify the clients player is fine, but for packets that are relayed, the clients player index will need to be part of the packet itself to correctly identify the client that sent the original packet. Use packet.Write((byte) Main.myPlayer); to write and int player = reader.ReadByte(); to read. The ModSystem.HijackGetData(ref byte, ref BinaryReader, int) hook can be used to intercept any packet used by Terraria. More...
 
bool HasAsset (string assetName)
 
virtual void Load ()
 Override this method to run code after all content has been autoloaded. Here additional content can be manually loaded and Mod-wide tasks and setup can be done. For organization, it may be more suitable to split some things into various ModType.Load methods, such as in ModSystem classes, instead of doing everything here.
Beware that mod content has not finished loading here, things like ModContent lookup tables or ID Sets are not fully populated. Use PostSetupContent for any logic that needs to act on all content being fully loaded.
 
virtual void PostAddRecipes ()
 This provides a hook into the mod-loading process immediately after recipes have been added. You can use this to edit recipes added by other mods.
 
virtual void PostSetupContent ()
 Allows you to load things in your mod after its content has been setup (arrays have been resized to fit the content, etc).
 
bool RequestAssetIfExists< T > (string assetName, out Asset< T > asset)
 
bool TryFind< T > (string name, out T value)
 Safely attempts to find the template instance from this mod with the specified name (not the clone/new instance which gets added to Items/Players/NPCs etc. as the game is played). Caching the result is recommended. More...
 
virtual void Unload ()
 This is called whenever this mod is unloaded from the game. Use it to undo changes that you've made in Load that aren't automatically handled (for example, modifying the texture of a vanilla item). Mods are guaranteed to be unloaded in the reverse order they were loaded in.
 

Public Attributes

GameContent.Bestiary.ModSourceBestiaryInfoElement ModSourceBestiaryInfoElement
 

Properties

AssetRepository Assets [get]
 Provides access to assets (textures, sounds, shaders, etc) contained within this mod. The main usage is to call the AssetRepository.Request<T>(string) method to retrieve an Asset<T> instance: More...
 
bool BackgroundAutoloadingEnabled = true [get]
 Whether or not this mod will automatically add images in the "Backgrounds" folder as background textures to the game. This means you do not need to manually call BackgroundTextureLoader.AddBackgroundTexture(Mod, string).
 
bool CloudAutoloadingEnabled = true [get]
 Whether or not this mod will automatically add images in the "Clouds" folder as clouds to the game. This means you do not need to manually call CloudLoader.AddCloudFromTexture(Mod, string, float, bool) or make a ModCloud class to add them to the game, but they will have the default spawn chance and be counted as normal clouds if autoloaded in this manner. Defaults to true.
 
Assembly Code [get, set]
 The assembly code this is loaded when tModLoader loads this mod.
Do NOT call Assembly.GetTypes on this as it will error out if the mod uses the ExtendsFromModAttribute attribute to inherit from weakly referenced mods. Use AssemblyManager.GetLoadableTypes(Assembly) instead.
 
bool ContentAutoloadingEnabled = true [get]
 Whether or not this mod will autoload content by default. Autoloading content means you do not need to manually add content through methods.
 
string DisplayName [get, set]
 The display name of this mod in the Mods menu.
 
string DisplayNameClean [get]
 Same as DisplayName, but chat tags are removed. This can be used for more readable logging and console output. It is also useful for code that searches or filters by mod name.
 
virtual uint ExtraPlayerBuffSlots [get]
 The amount of extra buff slots this mod desires for Players. This value is checked after Mod.Load but before Mod.PostSetupContent. The actual number of buffs the player can use will be 22 plus the max value of all enabled mods. In-game use Player.MaxBuffs to check the maximum number of buffs.
 
bool GoreAutoloadingEnabled = true [get]
 Whether or not this mod will automatically add images in the "Gores" folder as gores to the game, along with any ModGore classes that share names with the images. This means you do not need to manually call GoreLoader.AddGoreFromTexture<TGore>(Mod, string).
 
bool IsNetSynced [get]
 
ILog Logger [get, set]
 A logger with this mod's name for easy logging.
 
bool MusicAutoloadingEnabled = true [get]
 Whether or not this mod will automatically add music to the game. All supported audio files in a folder or subfolder of a folder named "Music" will be autoloaded as music.
 
virtual string Name [get]
 Stores the name of the mod. This name serves as the mod's identification, and also helps with saving everything your mod adds. By default this returns the name of the folder that contains all your code and stuff.
 
short NetID [get]
 
PreJITFilter PreJITFilter = new PreJITFilter() [get, protected set]
 
IContentSource RootContentSource [get]
 
ModSide Side [get, set]
 The ModSide that controls how this mod is synced between client and server.
 
Version TModLoaderVersion [get, set]
 The version of tModLoader that was being used when this mod was built.
 
List< string > TranslationForMods [get, set]
 
virtual Version Version [get]
 This version number of this mod.
 

Detailed Description

Mod is an abstract class that you will override. It serves as a central place from which the mod's contents are stored. It provides methods for you to use or override.

Member Function Documentation

◆ AddBossHeadTexture()

int Mod.AddBossHeadTexture ( string  texture,
int  npcType = -1 
)

Assigns a head texture that can be used by NPCs on the map.

Parameters
textureThe texture.
npcTypeAn optional npc id for NPCID.Sets.BossHeadTextures
Returns
The boss head texture slot

◆ AddContent()

bool Mod.AddContent ( ILoadable  instance)

Call this to manually add the given content instance to the game.

Parameters
instanceThe content instance to add
Returns
true if the instance was successfully added

◆ AddContent< T >()

bool Mod.AddContent< T > ( )

Call this to manually add a content instance of the specified type (with a parameterless constructor) to the game.

Returns
true if the instance was successfully added
Type Constraints
T :ILoadable 
T :new() 
T :AddContent 
T :new 
T :T() 

◆ AddNPCHeadTexture()

int Mod.AddNPCHeadTexture ( int  npcType,
string  texture 
)

Assigns a head texture to the given town NPC type.

Parameters
npcTypeType of the NPC.
textureThe texture.
Returns
The boss head texture slot
Exceptions
MissingResourceException

◆ FileExists()

bool Mod.FileExists ( string  name)

Returns whether or not a file with the specified name exists. Note that this includes file extension and the folder path: "Folders/FileNameWithExtension"

Parameters
name
Returns

◆ Find< T >()

T Mod.Find< T > ( string  name)

Attempts to find the template instance from this mod with the specified name (not the clone/new instance which gets added to Items/Players/NPCs etc. as the game is played). Caching the result is recommended.This will throw exceptions on failure.

Exceptions
KeyNotFoundException
Type Constraints
T :IModType 
T :ModContent.Find<T> 
T :Name 
T :name 

◆ GetContent< T >()

IEnumerable< T > Mod.GetContent< T > ( )

Returns all registered content instances that derive from the provided type that are added by this mod.
This only includes the 'template' instance for each piece of content, not all the clones/new instances which get added to Items/Players/NPCs etc. as the game is played

Type Constraints
T :ILoadable 
T :Content.GetContent<T>() 

◆ GetFileBytes()

byte[] Mod.GetFileBytes ( string  name)

Retrieves the contents of a file packaged within the .tmod file as a byte array. Should be used mainly for non-Asset<T> files. The name should be in the format of "Folders/FileNameWithExtension" starting from your mod's source code folder. Returns null if the file does not exist within the mod. A typical usage of this might be to load a text file containing structured data included within your mod. Make sure the txt file is UTF8 encoded and use the following to retrieve file's text contents: string pointsFileContents = Encoding.UTF8.GetString(Mod.GetFileBytes("data/points.txt"));

Parameters
nameThe name.
Returns

◆ GetFileNames()

List< string > Mod.GetFileNames ( )

Retrieves the names of every file packaged into this mod. Note that this includes extensions, and for images the extension will always be .rawimg.

Returns

◆ GetFileStream()

Stream Mod.GetFileStream ( string  name,
bool  newFileStream = false 
)

Retrieve contents of files within the .tmod file.

Parameters
nameThe name.
newFileStream
Returns

◆ GetLocalization()

LocalizedText Mod.GetLocalization ( string  suffix,
Func< string >  makeDefaultValue = null 
)

Returns a LocalizedText for this Mod with the provided suffix . The suffix will be used to generate a key by providing it to GetLocalizationKey(string).
If no existing localization exists for the key, it will be defined so it can be exported to a matching mod localization file.

Parameters
suffix
makeDefaultValueA factory method for creating the default value, used to update localization files with missing entries
Returns

◆ GetLocalizationKey()

string Mod.GetLocalizationKey ( string  suffix)

Creates a localization key following the pattern of "Mods.{ModName}.{suffix}". Use this with Language.GetOrRegister(string, Func<string>) to retrieve a LocalizedText for custom localization keys. Alternatively GetLocalization(string, Func<string>) can be used directly instead. Custom localization keys need to be registered during the mod loading process to appear automatically in the localization files.

Parameters
suffix
Returns

◆ GetPacket()

ModPacket Mod.GetPacket ( int  capacity = 256)

Creates a ModPacket object that you can write to and then send between servers and clients.

Parameters
capacityThe capacity.
Returns
Exceptions
System.ExceptionCannot get packet for " + Name + " because it does not exist on the other side

◆ HandlePacket()

virtual void Mod.HandlePacket ( BinaryReader  reader,
int  whoAmI 
)
virtual

Called whenever a net message / packet pertaining to this mod is received from a client (if this is a server) or the server (if this is a client). whoAmI is the ID of whomever sent the packet (equivalent to the Main.myPlayer of the sender), and reader is used to read the binary data of the packet. Note that many packets are sent from a client to the server and then relayed to the remaining clients. The whoAmI when the packet arrives at the remaining clients will be the servers Main.myPlayer, not the original clients Main.myPlayer. For packets only sent from a client to the server, relying on whoAmI to identify the clients player is fine, but for packets that are relayed, the clients player index will need to be part of the packet itself to correctly identify the client that sent the original packet. Use packet.Write((byte) Main.myPlayer); to write and int player = reader.ReadByte(); to read. The ModSystem.HijackGetData(ref byte, ref BinaryReader, int) hook can be used to intercept any packet used by Terraria.

Parameters
readerThe reader.
whoAmIThe player the message is from. Only relevant for server code. For clients it will always be 255, the server. For the server it will be the whoAmI of the client.

◆ RequestAssetIfExists< T >()

bool Mod.RequestAssetIfExists< T > ( string  assetName,
out Asset< T >  asset 
)
Type Constraints
T :class 

◆ TryFind< T >()

bool Mod.TryFind< T > ( string  name,
out T  value 
)

Safely attempts to find the template instance from this mod with the specified name (not the clone/new instance which gets added to Items/Players/NPCs etc. as the game is played). Caching the result is recommended.

Returns
Whether or not the requested instance has been found.
Type Constraints
T :IModType 
T :ModContent.TryFind 
T :Name 
T :name 
T :out 
T :value 

Property Documentation

◆ Assets

AssetRepository Mod.Assets
get

Provides access to assets (textures, sounds, shaders, etc) contained within this mod. The main usage is to call the AssetRepository.Request<T>(string) method to retrieve an Asset<T> instance:

Asset<Texture2D> balloonTexture = Mod.Assets.Request<Texture2D>("Content/Items/Armor/SimpleAccessory_Balloon"); Do not include the mod name in the Request method call, the path supplied should not include the mod name. This is different from using ModContent.Request<T>(string, AssetRequestMode) where the mod name is required.

Read the Assets guide on the wiki

for more information.