tModLoader v0.11.8.9
A mod to make and play Terraria mods
ModTileEntity.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.IO;
4using Terraria.DataStructures;
6
7namespace Terraria.ModLoader
8{
13 public abstract class ModTileEntity : TileEntity
14 {
15 public const int numVanilla = 3;
16 private static int nextTileEntity = numVanilla;
17 internal static readonly List<ModTileEntity> tileEntities = new List<ModTileEntity>();
18 // TODO: public bool netUpdate;
19
23 public Mod mod {
24 get;
25 internal set;
26 }
27
31 public string Name {
32 get;
33 internal set;
34 }
35
39 public int Type {
40 get;
41 internal set;
42 }
43
44 internal static int ReserveTileEntityID() {
45 if (ModNet.AllowVanillaClients) throw new Exception("Adding tile entities breaks vanilla client compatibility");
46
47 int reserveID = nextTileEntity;
49 if (reserveID > Byte.MaxValue) {
50 throw new Exception("Too many tile entities have been added");
51 }
52 return reserveID;
53 }
54
58 public static ModTileEntity GetTileEntity(int type) {
59 return type >= numVanilla && type < nextTileEntity ? tileEntities[type - numVanilla] : null;
60 }
61
62 internal static void Unload() {
64 tileEntities.Clear();
65 }
66
70 public static int CountInWorld() {
71 int count = 0;
72 foreach (KeyValuePair<int, TileEntity> pair in ByID) {
73 if (pair.Value.type >= numVanilla) {
74 count++;
75 }
76 }
77 return count;
78 }
79
83 public static void Initialize() {
84 _UpdateStart += UpdateStartInternal;
85 _UpdateEnd += UpdateEndInternal;
86 _NetPlaceEntity += NetPlaceEntity;
87 }
88
89 private static void UpdateStartInternal() {
90 foreach (ModTileEntity tileEntity in tileEntities) {
91 tileEntity.PreGlobalUpdate();
92 }
93 }
94
95 private static void UpdateEndInternal() {
96 foreach (ModTileEntity tileEntity in tileEntities) {
97 tileEntity.PostGlobalUpdate();
98 }
99 }
100
104 public static void NetPlaceEntity(int i, int j, int type) {
105 ModTileEntity tileEntity = GetTileEntity(type);
106 if (tileEntity == null) {
107 return;
108 }
109 if (!tileEntity.ValidTile(i, j)) {
110 return;
111 }
112 int id = tileEntity.Place(i, j);
113 ModTileEntity newEntity = (ModTileEntity)ByID[id];
114 newEntity.OnNetPlace();
115 NetMessage.SendData(86, -1, -1, null, id, i, j, 0f, 0, 0, 0);
116 }
117
121 public static ModTileEntity ConstructFromType(int type) {
122 ModTileEntity tileEntity = GetTileEntity(type);
123 if (tileEntity == null) {
124 return null;
125 }
126 return ConstructFromBase(tileEntity);
127 }
128
133 ModTileEntity newEntity = (ModTileEntity)Activator.CreateInstance(tileEntity.GetType());
134 newEntity.mod = tileEntity.mod;
135 newEntity.Name = tileEntity.Name;
136 newEntity.Type = tileEntity.Type;
137 return newEntity;
138 }
139
143 public int Place(int i, int j) {
144 ModTileEntity newEntity = ConstructFromBase(this);
145 newEntity.Position = new Point16(i, j);
146 newEntity.ID = AssignNewID();
147 newEntity.type = (byte)Type;
148 ByID[newEntity.ID] = newEntity;
149 ByPosition[newEntity.Position] = newEntity;
150 return newEntity.ID;
151 }
152
156 public void Kill(int i, int j) {
157 Point16 pos = new Point16(i, j);
158 if (ByPosition.ContainsKey(pos)) {
159 TileEntity tileEntity = ByPosition[pos];
160 if (tileEntity.type == Type) {
161 ((ModTileEntity)tileEntity).OnKill();
162 ByID.Remove(tileEntity.ID);
163 ByPosition.Remove(pos);
164 }
165 }
166 }
167
171 public int Find(int i, int j) {
172 Point16 pos = new Point16(i, j);
173 if (ByPosition.ContainsKey(pos)) {
174 TileEntity tileEntity = ByPosition[pos];
175 if (tileEntity.type == Type) {
176 return tileEntity.ID;
177 }
178 }
179 return -1;
180 }
181
185 public sealed override void WriteExtraData(BinaryWriter writer, bool networkSend) {
186 NetSend(writer, networkSend);
187 }
188
192 public sealed override void ReadExtraData(BinaryReader reader, bool networkSend) {
193 NetReceive(reader, networkSend);
194 }
195
199 public virtual bool Autoload(ref string name) {
200 return mod.Properties.Autoload;
201 }
202
207 public virtual TagCompound Save() {
208 return null;
209 }
210
214 public virtual void Load(TagCompound tag) {
215 }
216
222 public virtual void NetSend(BinaryWriter writer, bool lightSend) {
223 }
224
230 public virtual void NetReceive(BinaryReader reader, bool lightReceive) {
231 }
232
236 public abstract bool ValidTile(int i, int j);
237
241 public virtual int Hook_AfterPlacement(int i, int j, int type, int style, int direction) {
242 return -1;
243 }
244
248 public virtual void OnNetPlace() {
249 }
250
254 public virtual void PreGlobalUpdate() {
255 }
256
260 public virtual void PostGlobalUpdate() {
261 }
262
266 public virtual void OnKill() {
267 }
268 }
269}
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
Tile Entities are Entities tightly coupled with tiles, allowing the possibility of tiles to exhibit c...
int Type
The numeric type used to identify this kind of tile entity.
sealed override void ReadExtraData(BinaryReader reader, bool networkSend)
Don't use this. It is included only for completion's sake.
static ModTileEntity ConstructFromBase(ModTileEntity tileEntity)
Returns a new ModTileEntity with the same class, mod, name, and type as the parameter....
virtual void PreGlobalUpdate()
Code that should be run before all tile entities in the world update.
abstract bool ValidTile(int i, int j)
Whether or not this tile entity is allowed to survive at the given coordinates. You should check whet...
static ModTileEntity GetTileEntity(int type)
Gets the base ModTileEntity object with the given type.
virtual void Load(TagCompound tag)
Allows you to load the custom data you have saved for this tile entity.
virtual void PostGlobalUpdate()
Code that should be run after all tile entities in the world update.
sealed override void WriteExtraData(BinaryWriter writer, bool networkSend)
Don't use this. It is included only for completion's sake.
virtual TagCompound Save()
Allows you to save custom data for this tile entity.
static ModTileEntity ConstructFromType(int type)
Returns a new ModTileEntity with the same class, mod, name, and type as the ModTileEntity with the gi...
virtual void OnNetPlace()
Code that should be run when this tile entity is placed by means of server-syncing....
static void Initialize()
You should never use this. It is only included here for completion's sake.
virtual void OnKill()
This method only gets called in the Kill method. If you plan to use that, you can put code here to ma...
string Name
The internal name of this ModTileEntity.
static int CountInWorld()
Returns the number of modded tile entities that exist in the world currently being played.
int Find(int i, int j)
Returns the entity ID of this kind of tile entity at the given coordinates for you.
Mod mod
The mod that added this ModTileEntity.
static void NetPlaceEntity(int i, int j, int type)
You should never use this. It is only included here for completion's sake.
virtual bool Autoload(ref string name)
Allows you to automatically load a tile entity instead of using Mod.AddTileEntity....
void Kill(int i, int j)
A helper method that removes this kind of tile entity from the given coordinates for you.
virtual void NetReceive(BinaryReader reader, bool lightReceive)
Receives the data sent in the NetSend hook. Called on MP Client when receiving tile data (!...
virtual void NetSend(BinaryWriter writer, bool lightSend)
Allows you to send custom data for this tile entity between client and server. This is called on the ...
virtual int Hook_AfterPlacement(int i, int j, int type, int style, int direction)
This method does not get called by tModLoader, and is only included for you convenience so you do not...
int Place(int i, int j)
A helper method that places this kind of tile entity in the given coordinates for you.
bool Autoload
Whether or not this mod will autoload content by default. Autoloading content means you do not need t...