Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TileEntity.cs
Go to the documentation of this file.
1using System;
3using System.IO;
7
9
10public abstract class TileEntity
11{
13
14 public const int MaxEntitiesPerChunk = 1000;
15
16 public static object EntityCreationLock = new object();
17
19
21
22 public static int TileEntitiesNextID;
23
24 public int ID;
25
27
28 public byte type;
29
30 public static event Action _UpdateStart;
31
32 public static event Action _UpdateEnd;
33
34 public static int AssignNewID()
35 {
36 return TileEntitiesNextID++;
37 }
38
39 public static void Clear()
40 {
41 ByID.Clear();
42 ByPosition.Clear();
44 }
45
46 public static void UpdateStart()
47 {
48 if (TileEntity._UpdateStart != null)
49 {
51 }
52 }
53
54 public static void UpdateEnd()
55 {
56 if (TileEntity._UpdateEnd != null)
57 {
59 }
60 }
61
62 public static void InitializeAll()
63 {
65 manager.RegisterAll();
66 }
67
68 public static void PlaceEntityNet(int x, int y, int type)
69 {
70 if (WorldGen.InWorld(x, y) && !ByPosition.ContainsKey(new Point16(x, y)))
71 {
72 manager.NetPlaceEntity(type, x, y);
73 }
74 }
75
76 public virtual void Update()
77 {
78 }
79
80 public static void Write(BinaryWriter writer, TileEntity ent, bool networkSend = false)
81 {
82 writer.Write(ent.type);
83 ent.WriteInner(writer, networkSend);
84 }
85
86 public static TileEntity Read(BinaryReader reader, bool networkSend = false)
87 {
88 byte id = reader.ReadByte();
89 TileEntity tileEntity = manager.GenerateInstance(id);
90 tileEntity.type = id;
91 tileEntity.ReadInner(reader, networkSend);
92 return tileEntity;
93 }
94
96 {
97 if (!networkSend)
98 {
99 writer.Write(ID);
100 }
101 writer.Write(Position.X);
102 writer.Write(Position.Y);
104 }
105
106 private void ReadInner(BinaryReader reader, bool networkSend)
107 {
108 if (!networkSend)
109 {
110 ID = reader.ReadInt32();
111 }
112 Position = new Point16(reader.ReadInt16(), reader.ReadInt16());
113 ReadExtraData(reader, networkSend);
114 }
115
117 {
118 }
119
120 public virtual void ReadExtraData(BinaryReader reader, bool networkSend)
121 {
122 }
123
124 public virtual void OnPlayerUpdate(Player player)
125 {
126 }
127
128 public static bool IsOccupied(int id, out int interactingPlayer)
129 {
131 for (int i = 0; i < 255; i++)
132 {
133 Player player = Main.player[i];
134 if (player.active && !player.dead && player.tileEntityAnchor.interactEntityID == id)
135 {
137 return true;
138 }
139 }
140 return false;
141 }
142
143 public virtual void OnInventoryDraw(Player player, SpriteBatch spriteBatch)
144 {
145 }
146
147 public virtual string GetItemGamepadInstructions(int slot = 0)
148 {
149 return "";
150 }
151
152 public virtual bool TryGetItemGamepadOverrideInstructions(Item[] inv, int context, int slot, out string instruction)
153 {
154 instruction = null;
155 return false;
156 }
157
158 public virtual bool OverrideItemSlotHover(Item[] inv, int context = 0, int slot = 0)
159 {
160 return false;
161 }
162
163 public virtual bool OverrideItemSlotLeftClick(Item[] inv, int context = 0, int slot = 0)
164 {
165 return false;
166 }
167
168 public static void BasicOpenCloseInteraction(Player player, int x, int y, int id)
169 {
170 player.CloseSign();
172 if (Main.netMode != 1)
173 {
174 Main.stackSplit = 600;
177 {
178 if (interactingPlayer == player.whoAmI)
179 {
182 player.tileEntityAnchor.Clear();
183 }
184 }
185 else
186 {
187 SetInteractionAnchor(player, x, y, id);
188 }
189 return;
190 }
191 Main.stackSplit = 600;
194 {
195 if (interactingPlayer == player.whoAmI)
196 {
199 player.tileEntityAnchor.Clear();
200 NetMessage.SendData(122, -1, -1, null, -1, Main.myPlayer);
201 }
202 }
203 else
204 {
205 NetMessage.SendData(122, -1, -1, null, id, Main.myPlayer);
206 }
207 }
208
209 public static void SetInteractionAnchor(Player player, int x, int y, int id)
210 {
211 player.chest = -1;
212 player.SetTalkNPC(-1);
213 if (player.whoAmI == Main.myPlayer)
214 {
215 Main.playerInventory = true;
216 Main.recBigList = false;
217 Main.CreativeMenu.CloseMenu();
219 {
220 PlayerInput.Triggers.JustPressed.Grapple = false;
221 }
222 if (player.tileEntityAnchor.interactEntityID != -1)
223 {
225 }
226 else
227 {
229 }
230 }
231 player.tileEntityAnchor.Set(id, x, y);
232 }
233
234 public virtual void RegisterTileEntityID(int assignedID)
235 {
236 }
237
238 public virtual void NetPlaceEntityAttempt(int x, int y)
239 {
240 }
241
242 public virtual bool IsTileValidForEntity(int x, int y)
243 {
244 return false;
245 }
246
248 {
249 return null;
250 }
251}
virtual byte ReadByte()
virtual int ReadInt32()
virtual short ReadInt16()
static void PlaySound(int type, Vector2 position, int style=1)
virtual string GetItemGamepadInstructions(int slot=0)
virtual void WriteExtraData(BinaryWriter writer, bool networkSend)
static Dictionary< int, TileEntity > ByID
Definition TileEntity.cs:18
static void BasicOpenCloseInteraction(Player player, int x, int y, int id)
void WriteInner(BinaryWriter writer, bool networkSend)
Definition TileEntity.cs:95
static TileEntitiesManager manager
Definition TileEntity.cs:12
virtual void RegisterTileEntityID(int assignedID)
static Dictionary< Point16, TileEntity > ByPosition
Definition TileEntity.cs:20
static bool IsOccupied(int id, out int interactingPlayer)
virtual TileEntity GenerateInstance()
virtual void NetPlaceEntityAttempt(int x, int y)
static void Write(BinaryWriter writer, TileEntity ent, bool networkSend=false)
Definition TileEntity.cs:80
static void PlaceEntityNet(int x, int y, int type)
Definition TileEntity.cs:68
virtual bool TryGetItemGamepadOverrideInstructions(Item[] inv, int context, int slot, out string instruction)
virtual void OnInventoryDraw(Player player, SpriteBatch spriteBatch)
virtual bool OverrideItemSlotLeftClick(Item[] inv, int context=0, int slot=0)
static TileEntity Read(BinaryReader reader, bool networkSend=false)
Definition TileEntity.cs:86
virtual void OnPlayerUpdate(Player player)
virtual bool OverrideItemSlotHover(Item[] inv, int context=0, int slot=0)
virtual bool IsTileValidForEntity(int x, int y)
void ReadInner(BinaryReader reader, bool networkSend)
static void SetInteractionAnchor(Player player, int x, int y, int id)
virtual void ReadExtraData(BinaryReader reader, bool networkSend)
static int myPlayer
Definition Main.cs:1801
static int netMode
Definition Main.cs:2095
static CreativeUI CreativeMenu
Definition Main.cs:369
static Player[] player
Definition Main.cs:1803
static void SendData(int msgType, int remoteClient=-1, int ignoreClient=-1, NetworkText text=null, int number=0, float number2=0f, float number3=0f, float number4=0f, int number5=0, int number6=0, int number7=0)
Definition NetMessage.cs:88
void SetTalkNPC(int npcIndex, bool fromNet=false)
Definition Player.cs:3725
void GamepadEnableGrappleCooldown()
Definition Player.cs:23162
void CloseSign()
Definition Player.cs:31262
PlayerInteractionAnchor tileEntityAnchor
Definition Player.cs:2469
static void FindRecipes(bool canDelayCheck=false)
Definition Recipe.cs:453
static bool InWorld(int x, int y, int fluff=0)
Definition WorldGen.cs:5816