/
ServerPortals.cs
232 lines (199 loc) · 5.48 KB
/
ServerPortals.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Terraria;
using Terraria.ModLoader;
using Terraria.UI;
using ServerPortals.UI;
using System.IO;
using Terraria.DataStructures;
using Terraria.ID;
using ServerPortals.Tiles;
using System;
namespace ServerPortals
{
public class ServerPortals : Mod
{
public static ServerPortals Instance;
internal static GateLabelMenu GateLabel;
internal static ServerTransferCreationMenu ServerTransferMenu;
private static UserInterface _gateLabelUI;
private static UserInterface _serverTransferUI;
public static bool ShowText;
public static string Text;
public static Vector2 Pos;
public ServerPortals()
{
Pos = Vector2.Zero;
}
public static void OpenMenu()
{
if(_serverTransferUI.CurrentState != ServerTransferMenu)
_serverTransferUI.SetState(ServerTransferMenu);
}
public static void CloseMenu()
{
if (_serverTransferUI.CurrentState != null)
_serverTransferUI.SetState(null);
}
public static void ShowLabel()
=> _gateLabelUI.SetState(GateLabel);
public static void HideLabel()
=> _gateLabelUI.SetState(null);
public static bool DataIsValid()
{
int.TryParse(ServerTransferMenu.InputServerPort.Text, out int OutPort);
if (OutPort == 0
|| ServerTransferMenu.InputServerIP.Text == "" || ServerTransferMenu.InputServerIP.Text == null
|| ServerTransferMenu.InputServerName.Text == "" || ServerTransferMenu.InputServerName.Text == null)
{
return false;
}
return true;
}
public override void Load()
{
Instance = this;
GateLabel = new GateLabelMenu();
GateLabel.Activate();
ServerTransferMenu = new ServerTransferCreationMenu();
ServerTransferMenu.Activate();
_serverTransferUI = new UserInterface();
_gateLabelUI = new UserInterface();
base.Load();
}
public override void Unload()
{
base.Unload();
_gateLabelUI = null;
ServerTransferMenu = null;
GateLabel = null;
}
public override void PostSetupContent()
{
base.PostSetupContent();
}
public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
{
int inventoryIndex = layers.FindIndex(layer => layer.Name.Equals("Vanilla: Inventory"));
if (inventoryIndex != -1)
{
layers.Insert(inventoryIndex, new LegacyGameInterfaceLayer(
"WorldLink: UI",
delegate
{
_gateLabelUI.Draw(Main.spriteBatch, new GameTime());
_serverTransferUI.Draw(Main.spriteBatch, new GameTime());
return true;
},
InterfaceScaleType.UI)
);
}
}
public override void UpdateUI(GameTime gameTime)
{
_gateLabelUI?.Update(gameTime);
_serverTransferUI?.Update(gameTime);
HideLabel();
}
// Everything below is courtesy of the amazing Blushiemagic and her Magic Storage GitHub repo
public override void HandlePacket(BinaryReader reader, int whoAmI)
{
MessageType type = (MessageType)reader.ReadByte();
if (type == MessageType.ClientSendTEUpdate)
ReceiveClientSendTEUpdate(reader, whoAmI);
else if (type == MessageType.ClientSendPortalPlacement)
ReceiveClientPortalPlacement(reader, whoAmI);
base.HandlePacket(reader, whoAmI);
}
public static void ReceiveClientSendTEUpdate(BinaryReader reader, int sender)
{
if (Main.netMode == 2)
{
int id = reader.ReadInt32();
TileEntity te = TileEntity.Read(reader, true);
te.ID = id;
TileEntity.ByID[id] = te;
TileEntity.ByPosition[te.Position] = te;
NetMessage.SendData(MessageID.TileEntitySharing, -1, sender, null, id, te.Position.X, te.Position.Y);
}
else if (Main.netMode == 1)
{
reader.ReadInt32();
}
}
public static void ClientSendTEUpdate(int id)
{
if (Main.netMode == 1)
{
ModPacket packet = Instance.GetPacket();
packet.Write((byte)MessageType.ClientSendTEUpdate);
packet.Write(id);
TileEntity.Write(packet, TileEntity.ByID[id], true);
packet.Send();
}
}
public static void ReceiveClientPortalPlacement(BinaryReader reader, int whoAmI)
{
if (Main.netMode == 2)
{
try
{
string IP = reader.ReadString();
int Port = reader.ReadInt32();
string Name = reader.ReadString();
string Desc = reader.ReadString();
int x = reader.ReadInt32();
int y = reader.ReadInt32();
var instance = ModContent.GetInstance<PortalTileEntity>();
int id = instance.Find(x, y);
if (id == -1)
{
id = ModContent.GetInstance<PortalTileEntity>().Place(x, y);
}
PortalTileEntity tileEntity = (PortalTileEntity)TileEntity.ByID[id];
tileEntity.SetData(new Server()
{
IP = IP,
Port = Port,
Name = Name,
Description = Desc
});
NetMessage.SendData(MessageID.TileEntitySharing, -1, -1, null, id, x, y);
}
catch
{
Console.WriteLine("- Error receiving Portal Placement");
}
}
else if (Main.netMode == 1)
{
reader.ReadString();
reader.ReadInt32();
reader.ReadString();
reader.ReadString();
reader.ReadInt32();
reader.ReadInt32();
}
}
public static void ClientSendPortalPlacement(Server server, int tileX, int tileY)
{
if (Main.netMode == 1)
{
ModPacket packet = Instance.GetPacket();
packet.Write((byte)MessageType.ClientSendPortalPlacement);
packet.Write(server.IP);
packet.Write(server.Port);
packet.Write(server.Name);
packet.Write(server.Description);
packet.Write(tileX);
packet.Write(tileY);
packet.Send();
}
}
public enum MessageType : byte
{
ClientSendTEUpdate,
ClientSendPortalPlacement
}
}
}