Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TEFoodPlatter.cs
Go to the documentation of this file.
1using System.IO;
3using Terraria.ID;
4
6
8{
9 private static byte _myEntityID;
10
11 public Item item;
12
13 public override void RegisterTileEntityID(int assignedID)
14 {
15 _myEntityID = (byte)assignedID;
16 }
17
18 public override void NetPlaceEntityAttempt(int x, int y)
19 {
20 NetPlaceEntity(x, y);
21 }
22
23 public static void NetPlaceEntity(int x, int y)
24 {
25 int number = Place(x, y);
26 NetMessage.SendData(86, -1, -1, null, number, x, y);
27 }
28
29 public override TileEntity GenerateInstance()
30 {
31 return new TEFoodPlatter();
32 }
33
35 {
36 item = new Item();
37 }
38
39 public static int Place(int x, int y)
40 {
41 TEFoodPlatter tEFoodPlatter = new TEFoodPlatter();
42 tEFoodPlatter.Position = new Point16(x, y);
43 tEFoodPlatter.ID = TileEntity.AssignNewID();
44 tEFoodPlatter.type = _myEntityID;
46 {
47 TileEntity.ByID[tEFoodPlatter.ID] = tEFoodPlatter;
48 TileEntity.ByPosition[tEFoodPlatter.Position] = tEFoodPlatter;
49 }
50 return tEFoodPlatter.ID;
51 }
52
53 public override bool IsTileValidForEntity(int x, int y)
54 {
55 return ValidTile(x, y);
56 }
57
58 public static int Hook_AfterPlacement(int x, int y, int type = 520, int style = 0, int direction = 1, int alternate = 0)
59 {
60 if (Main.netMode == 1)
61 {
63 NetMessage.SendData(87, -1, -1, null, x, y, (int)_myEntityID);
64 return -1;
65 }
66 return Place(x, y);
67 }
68
69 public static void Kill(int x, int y)
70 {
71 if (TileEntity.ByPosition.TryGetValue(new Point16(x, y), out var value) && value.type == _myEntityID)
72 {
74 {
75 TileEntity.ByID.Remove(value.ID);
76 TileEntity.ByPosition.Remove(new Point16(x, y));
77 }
78 }
79 }
80
81 public static int Find(int x, int y)
82 {
83 if (TileEntity.ByPosition.TryGetValue(new Point16(x, y), out var value) && value.type == _myEntityID)
84 {
85 return value.ID;
86 }
87 return -1;
88 }
89
90 public static bool ValidTile(int x, int y)
91 {
92 if (!Main.tile[x, y].active() || Main.tile[x, y].type != 520 || Main.tile[x, y].frameY != 0)
93 {
94 return false;
95 }
96 return true;
97 }
98
99 public override void WriteExtraData(BinaryWriter writer, bool networkSend)
100 {
101 writer.Write((short)item.netID);
102 writer.Write(item.prefix);
103 writer.Write((short)item.stack);
104 }
105
106 public override void ReadExtraData(BinaryReader reader, bool networkSend)
107 {
108 item = new Item();
109 item.netDefaults(reader.ReadInt16());
110 item.Prefix(reader.ReadByte());
111 item.stack = reader.ReadInt16();
112 }
113
114 public override string ToString()
115 {
116 return Position.X + "x " + Position.Y + "y item: " + item;
117 }
118
119 public void DropItem()
120 {
121 if (Main.netMode != 1)
122 {
123 Item.NewItem(new EntitySource_TileBreak(Position.X, Position.Y), Position.X * 16, Position.Y * 16, 16, 16, item.netID, 1, noBroadcast: false, item.prefix);
124 }
125 item = new Item();
126 }
127
128 public static void TryPlacing(int x, int y, int netid, int prefix, int stack)
129 {
130 WorldGen.RangeFrame(x, y, x + 1, y + 1);
131 int num = Find(x, y);
132 if (num == -1)
133 {
134 int num2 = Item.NewItem(new EntitySource_TileBreak(x, y), x * 16, y * 16, 16, 16, 1);
135 Main.item[num2].netDefaults(netid);
136 Main.item[num2].Prefix(prefix);
137 Main.item[num2].stack = stack;
138 NetMessage.SendData(21, -1, -1, null, num2);
139 return;
140 }
141 TEFoodPlatter tEFoodPlatter = (TEFoodPlatter)TileEntity.ByID[num];
142 if (tEFoodPlatter.item.stack > 0)
143 {
144 tEFoodPlatter.DropItem();
145 }
146 tEFoodPlatter.item = new Item();
147 tEFoodPlatter.item.netDefaults(netid);
148 tEFoodPlatter.item.Prefix(prefix);
149 tEFoodPlatter.item.stack = stack;
150 NetMessage.SendData(86, -1, -1, null, tEFoodPlatter.ID, x, y);
151 }
152
153 public static void OnPlayerInteraction(Player player, int clickX, int clickY)
154 {
155 if (FitsFoodPlatter(player.inventory[player.selectedItem]) && !player.inventory[player.selectedItem].favorited)
156 {
158 PlaceItemInFrame(player, clickX, clickY);
160 return;
161 }
162 int num = Find(clickX, clickY);
163 if (num != -1 && ((TEFoodPlatter)TileEntity.ByID[num]).item.stack > 0)
164 {
166 WorldGen.KillTile(clickX, clickY, fail: true);
167 if (Main.netMode == 1)
168 {
169 NetMessage.SendData(17, -1, -1, null, 0, clickX, clickY, 1f);
170 }
171 }
172 }
173
174 public static bool FitsFoodPlatter(Item i)
175 {
176 if (i.stack > 0)
177 {
178 return ItemID.Sets.IsFood[i.type];
179 }
180 return false;
181 }
182
183 public static void PlaceItemInFrame(Player player, int x, int y)
184 {
185 if (!player.ItemTimeIsZero)
186 {
187 return;
188 }
189 int num = Find(x, y);
190 if (num == -1)
191 {
192 return;
193 }
194 if (((TEFoodPlatter)TileEntity.ByID[num]).item.stack > 0)
195 {
196 WorldGen.KillTile(x, y, fail: true);
197 if (Main.netMode == 1)
198 {
199 NetMessage.SendData(17, -1, -1, null, 0, Player.tileTargetX, y, 1f);
200 }
201 }
202 if (Main.netMode == 1)
203 {
204 NetMessage.SendData(133, -1, -1, null, x, y, player.selectedItem, player.whoAmI, 1);
205 }
206 else
207 {
208 TryPlacing(x, y, player.inventory[player.selectedItem].netID, player.inventory[player.selectedItem].prefix, 1);
209 }
210 player.inventory[player.selectedItem].stack--;
211 if (player.inventory[player.selectedItem].stack <= 0)
212 {
213 player.inventory[player.selectedItem].SetDefaults();
214 Main.mouseItem.SetDefaults();
215 }
216 if (player.selectedItem == 58)
217 {
218 Main.mouseItem = player.inventory[player.selectedItem].Clone();
219 }
220 player.releaseUseItem = false;
221 player.mouseInterface = true;
222 WorldGen.RangeFrame(x, y, x + 1, y + 1);
223 }
224
225 public void FixLoadedData()
226 {
228 }
229}
virtual byte ReadByte()
virtual short ReadInt16()
static Dictionary< int, TileEntity > ByID
Definition TileEntity.cs:18
static Dictionary< Point16, TileEntity > ByPosition
Definition TileEntity.cs:20
static void PlaceItemInFrame(Player player, int x, int y)
override void NetPlaceEntityAttempt(int x, int y)
override void RegisterTileEntityID(int assignedID)
static void OnPlayerInteraction(Player player, int clickX, int clickY)
override void ReadExtraData(BinaryReader reader, bool networkSend)
static void TryPlacing(int x, int y, int netid, int prefix, int stack)
static int Hook_AfterPlacement(int x, int y, int type=520, int style=0, int direction=1, int alternate=0)
override void WriteExtraData(BinaryWriter writer, bool networkSend)
override bool IsTileValidForEntity(int x, int y)
static bool[] IsFood
Definition ItemID.cs:233
Item Clone()
Definition Item.cs:49916
void netDefaults(int type)
Definition Item.cs:1088
int stack
Definition Item.cs:149
bool Prefix(int prefixWeWant)
Definition Item.cs:487
bool favorited
Definition Item.cs:135
int netID
Definition Item.cs:291
void SetDefaults(int Type=0)
Definition Item.cs:47332
static int NewItem(IEntitySource source, Vector2 pos, Vector2 randomBox, int Type, int Stack=1, bool noBroadcast=false, int prefixGiven=0, bool noGrabDelay=false, bool reverseLookup=false)
Definition Item.cs:49697
byte prefix
Definition Item.cs:295
void FixAgainstExploit()
Definition Item.cs:48328
static Item[] item
Definition Main.cs:1681
static int myPlayer
Definition Main.cs:1801
static int netMode
Definition Main.cs:2095
static Item mouseItem
Definition Main.cs:1773
static Tile[,] tile
Definition Main.cs:1675
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
static void SendTileSquare(int whoAmi, int tileX, int tileY, int xSize, int ySize, TileChangeType changeType=TileChangeType.None)
void GamepadEnableGrappleCooldown()
Definition Player.cs:23162
bool ItemTimeIsZero
Definition Player.cs:3525
static int tileTargetX
Definition Player.cs:2083
Item[] inventory
Definition Player.cs:1257
static void FindRecipes(bool canDelayCheck=false)
Definition Recipe.cs:453
static void KillTile(int i, int j, bool fail=false, bool effectOnly=false, bool noItem=false)
static void RangeFrame(int startX, int startY, int endX, int endY)