forked from Merlin33069/MCForge-Vanilla-1
/
Server.cs
219 lines (200 loc) · 8.31 KB
/
Server.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
/*
Copyright 2011 MCForge
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Collections.Generic;
using System.Timers;
using System.IO;
namespace MCForge
{
public static class Server
{
/// <summary>
/// Get whether the server is currently shutting down
/// </summary>
public static bool shuttingDown;
/// <summary>
/// Get whether the server is currently fully started or not
/// </summary>
public static bool Started = false;
private static System.Timers.Timer UpdateTimer;
private static System.Timers.Timer HeartbeatTimer;
internal static List<Player> Connections = new List<Player>();
/// <summary>
/// Get the current list of online players, note that if your doing a foreach on this always add .ToArray() to the end, it solves a LOT of issues
/// </summary>
public static List<Player> Players = new List<Player>();
/// <summary>
/// Get the current list of banned ip addresses, note that if your doing a foreach on this (or any other public list) you should always add .ToArray() to the end so that you avoid errors!
/// </summary>
public static List<string> BannedIP = new List<string>();
/// <summary>
/// The list of MCForge developers.
/// </summary>
public static List<string> devs = new List<string>(new string[] { "EricKilla", "Merlin33069", "Snowl", "gamezgalaxy", "Gamemakergm", "cazzar", "hirsty", "givo", "jasonbay13", "Alem_Zupa", "7imekeeper", "Shade2010", "TheMusiKid" });
/// <summary>
/// List of players that agreed to the rules
/// </summary>
public static List<string> agreed = new List<string>();
public static List<string> jokermessages = new List<string>();
/// <summary>
/// The main level of the server, where players spawn when they first join
/// </summary>
public static Level Mainlevel;
/// <summary>
/// Determines if the chat moderation is enabled
/// </summary>
public static bool moderation;
//Voting
/// <summary>
/// Is the server in voting mode?
/// </summary>
public static bool voting;
/// <summary>
/// Is it a kickvote?
/// </summary>
public static bool kickvote;
/// <summary>
/// Amount of yes votes.
/// </summary>
public static int YesVotes;
/// <summary>
/// Amount of no votes.
/// </summary>
public static int NoVotes;
/// <summary>
/// The player who's getting, if it's /votekick
/// </summary>
public static Player kicker;
/// <summary>
/// The server's default color.
/// </summary>
public static string DefaultColor = "&e";
/// <summary>
/// The minecraft.net URL of the server
/// </summary>
///
public static string URL = "";
internal static void Init()
{
StartListening();
Mainlevel = Level.CreateLevel(new Point3(256, 256, 64), Level.LevelTypes.Flat);
UpdateTimer = new System.Timers.Timer(100);
UpdateTimer.Elapsed += delegate { Update(); };
UpdateTimer.Start();
HeartbeatTimer = new System.Timers.Timer(30000); //every 30 seconds
HeartbeatTimer.Elapsed += delegate { Heartbeat.sendHeartbeat(); };
HeartbeatTimer.Start();
LoadAllDlls.Init();
Log("[Important]: Server Started.", ConsoleColor.Black, ConsoleColor.White);
Started = true;
CmdReloadCmds reload = new CmdReloadCmds();
reload.Initialize();
//Create the directories we need...
if (!Directory.Exists("text")) { Directory.CreateDirectory("text"); Log("Created text directory...", ConsoleColor.White, ConsoleColor.Black); }
if (!File.Exists("text/agreed.txt")) { File.Create("text/agreed.txt").Close(); Log("[File] Created agreed.txt", ConsoleColor.White, ConsoleColor.Black); }
if (!File.Exists("text/jokermessages.txt"))
{
File.Create("text/jokermessages.txt").Close();
Log("[File] Created jokermessages.txt", ConsoleColor.White, ConsoleColor.Black);
string text = "I am a pony" + Environment.NewLine + "Rainbow Dash <3" + Environment.NewLine + "I like trains!";
File.WriteAllText("text/jokermessages.txt", text);
Log("[File] Added default messages to jokermessages.txt", ConsoleColor.White, ConsoleColor.Black);
}
try
{
string[] lines = File.ReadAllLines("text/agreed.txt");
foreach (string pl in lines) { agreed.Add(pl); }
}
catch { Log("[Error] Error reading agreed players!", ConsoleColor.Red, ConsoleColor.Black); }
}
static void Update()
{
Player.GlobalUpdate();
}
#region Socket Stuff
private static TcpListener listener;
private static void StartListening()
{
startretry:
try
{
listener = new TcpListener(System.Net.IPAddress.Any, ServerSettings.port);
listener.Start();
IAsyncResult ar = listener.BeginAcceptTcpClient(new AsyncCallback(AcceptCallback), listener);
}
catch (SocketException E)
{
Server.Log(E);
}
catch (Exception E)
{
Server.Log(E);
goto startretry;
}
}
private static void AcceptCallback(IAsyncResult ar)
{
TcpListener listener2 = (TcpListener)ar.AsyncState;
try
{
TcpClient clientSocket = listener2.EndAcceptTcpClient(ar);
new Player(clientSocket);
}
catch { }
if (!shuttingDown)
{
listener.BeginAcceptTcpClient(new AsyncCallback(AcceptCallback), listener);
}
}
#endregion
#region Log Stuff
/// <summary>
/// Write A message to the Console and the GuiLog using default (white on black) colors.
/// </summary>
/// <param name="message">The message to show</param>
public static void Log(string message)
{
Log(message, ConsoleColor.White, ConsoleColor.Black);
}
/// <summary>
/// Write an error to the Console and the GuiLog using Red on black colors
/// </summary>
/// <param name="E">The error exception to write.</param>
public static void Log(Exception E)
{
Log("[ERROR]: ", ConsoleColor.Red, ConsoleColor.Black);
Log(E.Message, ConsoleColor.Red, ConsoleColor.Black);
Log(E.StackTrace, ConsoleColor.Red, ConsoleColor.Black);
}
/// <summary>
/// Write a message to the console and GuiLog using a specified TextColor and BackGround Color
/// </summary>
/// <param name="message">The Message to show</param>
/// <param name="TextColor">The color of the text to show</param>
/// <param name="BackgroundColor">The color behind the text.</param>
public static void Log(string message, ConsoleColor TextColor, ConsoleColor BackgroundColor)
{
Console.ForegroundColor = TextColor;
Console.BackgroundColor = BackgroundColor;
Console.WriteLine(message.PadRight(Console.WindowWidth - 1));
Console.ResetColor();
}
#endregion
}
}