Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
PlayerDeathReason.cs
Go to the documentation of this file.
1using System.IO;
3
5
6public class PlayerDeathReason
7{
8 private int _sourcePlayerIndex = -1;
9
10 private int _sourceNPCIndex = -1;
11
13
14 private int _sourceOtherIndex = -1;
15
17
18 private int _sourceItemType;
19
20 private int _sourceItemPrefix;
21
22 private string _sourceCustomReason;
23
25 {
26 get
27 {
29 {
30 return null;
31 }
33 }
34 }
35
36 public bool TryGetCausingEntity(out Entity entity)
37 {
38 entity = null;
39 if (Main.npc.IndexInRange(_sourceNPCIndex))
40 {
41 entity = Main.npc[_sourceNPCIndex];
42 return true;
43 }
45 {
47 return true;
48 }
49 if (Main.player.IndexInRange(_sourcePlayerIndex))
50 {
52 return true;
53 }
54 return false;
55 }
56
58 {
59 return new PlayerDeathReason
60 {
62 };
63 }
64
66 {
67 return new PlayerDeathReason
68 {
70 };
71 }
72
73 public static PlayerDeathReason ByNPC(int index)
74 {
75 return new PlayerDeathReason
76 {
77 _sourceNPCIndex = index
78 };
79 }
80
81 public static PlayerDeathReason ByCustomReason(string reasonInEnglish)
82 {
83 return new PlayerDeathReason
84 {
85 _sourceCustomReason = reasonInEnglish
86 };
87 }
88
89 public static PlayerDeathReason ByPlayer(int index)
90 {
91 return new PlayerDeathReason
92 {
93 _sourcePlayerIndex = index,
94 _sourceItemType = Main.player[index].inventory[Main.player[index].selectedItem].type,
95 _sourceItemPrefix = Main.player[index].inventory[Main.player[index].selectedItem].prefix
96 };
97 }
98
99 public static PlayerDeathReason ByOther(int type)
100 {
101 return new PlayerDeathReason
102 {
103 _sourceOtherIndex = type
104 };
105 }
106
107 public static PlayerDeathReason ByProjectile(int playerIndex, int projectileIndex)
108 {
109 PlayerDeathReason playerDeathReason = new PlayerDeathReason
110 {
111 _sourcePlayerIndex = playerIndex,
112 _sourceProjectileLocalIndex = projectileIndex,
113 _sourceProjectileType = Main.projectile[projectileIndex].type
114 };
115 if (playerIndex >= 0 && playerIndex <= 255)
116 {
117 playerDeathReason._sourceItemType = Main.player[playerIndex].inventory[Main.player[playerIndex].selectedItem].type;
118 playerDeathReason._sourceItemPrefix = Main.player[playerIndex].inventory[Main.player[playerIndex].selectedItem].prefix;
119 }
120 return playerDeathReason;
121 }
122
131
132 public void WriteSelfTo(BinaryWriter writer)
133 {
134 BitsByte bitsByte = (byte)0;
135 bitsByte[0] = _sourcePlayerIndex != -1;
136 bitsByte[1] = _sourceNPCIndex != -1;
137 bitsByte[2] = _sourceProjectileLocalIndex != -1;
138 bitsByte[3] = _sourceOtherIndex != -1;
139 bitsByte[4] = _sourceProjectileType != 0;
140 bitsByte[5] = _sourceItemType != 0;
141 bitsByte[6] = _sourceItemPrefix != 0;
142 bitsByte[7] = _sourceCustomReason != null;
143 writer.Write(bitsByte);
144 if (bitsByte[0])
145 {
146 writer.Write((short)_sourcePlayerIndex);
147 }
148 if (bitsByte[1])
149 {
150 writer.Write((short)_sourceNPCIndex);
151 }
152 if (bitsByte[2])
153 {
154 writer.Write((short)_sourceProjectileLocalIndex);
155 }
156 if (bitsByte[3])
157 {
158 writer.Write((byte)_sourceOtherIndex);
159 }
160 if (bitsByte[4])
161 {
162 writer.Write((short)_sourceProjectileType);
163 }
164 if (bitsByte[5])
165 {
166 writer.Write((short)_sourceItemType);
167 }
168 if (bitsByte[6])
169 {
170 writer.Write((byte)_sourceItemPrefix);
171 }
172 if (bitsByte[7])
173 {
174 writer.Write(_sourceCustomReason);
175 }
176 }
177
179 {
180 PlayerDeathReason playerDeathReason = new PlayerDeathReason();
181 BitsByte bitsByte = reader.ReadByte();
182 if (bitsByte[0])
183 {
184 playerDeathReason._sourcePlayerIndex = reader.ReadInt16();
185 }
186 if (bitsByte[1])
187 {
188 playerDeathReason._sourceNPCIndex = reader.ReadInt16();
189 }
190 if (bitsByte[2])
191 {
192 playerDeathReason._sourceProjectileLocalIndex = reader.ReadInt16();
193 }
194 if (bitsByte[3])
195 {
196 playerDeathReason._sourceOtherIndex = reader.ReadByte();
197 }
198 if (bitsByte[4])
199 {
200 playerDeathReason._sourceProjectileType = reader.ReadInt16();
201 }
202 if (bitsByte[5])
203 {
204 playerDeathReason._sourceItemType = reader.ReadInt16();
205 }
206 if (bitsByte[6])
207 {
208 playerDeathReason._sourceItemPrefix = reader.ReadByte();
209 }
210 if (bitsByte[7])
211 {
212 playerDeathReason._sourceCustomReason = reader.ReadString();
213 }
214 return playerDeathReason;
215 }
216}
virtual byte ReadByte()
virtual string ReadString()
virtual short ReadInt16()
NetworkText GetDeathText(string deadPlayerName)
static PlayerDeathReason ByCustomReason(string reasonInEnglish)
static PlayerDeathReason FromReader(BinaryReader reader)
static PlayerDeathReason ByOther(int type)
static PlayerDeathReason ByPlayer(int index)
static PlayerDeathReason ByNPC(int index)
static PlayerDeathReason ByProjectile(int playerIndex, int projectileIndex)
static NetworkText CreateDeathMessage(string deadPlayerName, int plr=-1, int npc=-1, int proj=-1, int other=-1, int projType=0, int plrItemType=0)
Definition Lang.cs:997
static NetworkText FromLiteral(string text)
static Projectile[] projectile
Definition Main.cs:1691
static NPC[] npc
Definition Main.cs:1685
static Player[] player
Definition Main.cs:1803