forked from OMARATION/mcforge
/
Window.cs
599 lines (528 loc) · 21.3 KB
/
Window.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/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.osedu.org/licenses/ECL-2.0
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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Text.RegularExpressions;
using MCLawl;
namespace MCLawl.Gui
{
public partial class Window : Form
{
Regex regex = new Regex(@"^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\." +
"([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$");
// for cross thread use
delegate void StringCallback(string s);
delegate void PlayerListCallback(List<Player> players);
delegate void ReportCallback(Report r);
delegate void VoidDelegate();
public static event EventHandler Minimize;
public NotifyIcon notifyIcon1 = new NotifyIcon();
// public static bool Minimized = false;
internal static Server s;
bool shuttingDown = false;
public Window() {
InitializeComponent();
}
private void Window_Minimize(object sender, EventArgs e)
{
/* if (!Minimized)
{
Minimized = true;
ntf.Text = "MCZall";
ntf.Icon = this.Icon;
ntf.Click += delegate
{
try
{
Minimized = false;
this.ShowInTaskbar = true;
this.Show();
WindowState = FormWindowState.Normal;
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
};
ntf.Visible = true;
this.ShowInTaskbar = false;
} */
}
public static Window thisWindow;
private void Window_Load(object sender, EventArgs e) {
thisWindow = this;
MaximizeBox = false;
this.Text = "<server name here>";
this.Icon = new Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MCLawl.Lawl.ico"));
this.Show();
this.BringToFront();
WindowState = FormWindowState.Normal;
s = new Server();
s.OnLog += WriteLine;
s.OnCommand += newCommand;
s.OnError += newError;
s.OnSystem += newSystem;
foreach (TabPage tP in tabControl1.TabPages)
tabControl1.SelectTab(tP);
tabControl1.SelectTab(tabControl1.TabPages[0]);
s.HeartBeatFail += HeartBeatFail;
s.OnURLChange += UpdateUrl;
s.OnPlayerListChange += UpdateClientList;
s.OnSettingsUpdate += SettingsUpdate;
s.Start();
notifyIcon1.Text = ("MCForge Server: " + Server.name);
this.notifyIcon1.ContextMenuStrip = this.iconContext;
this.notifyIcon1.Icon = this.Icon;
this.notifyIcon1.Visible = true;
this.notifyIcon1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseClick);
System.Timers.Timer MapTimer = new System.Timers.Timer(10000);
MapTimer.Elapsed += delegate {
UpdateMapList("'");
}; MapTimer.Start();
//if (File.Exists(Logger.ErrorLogPath))
//txtErrors.Lines = File.ReadAllLines(Logger.ErrorLogPath);
if (File.Exists("extra/Changelog.txt"))
{
txtChangelog.Text = "Changelog for " + Server.Version + ":";
foreach (string line in File.ReadAllLines(("extra/Changelog.txt")))
{
txtChangelog.AppendText("\r\n " + line);
}
}
}
void SettingsUpdate()
{
if (shuttingDown) return;
if (txtLog.InvokeRequired)
{
VoidDelegate d = new VoidDelegate(SettingsUpdate);
this.Invoke(d);
} else {
this.Text = Server.name + " MCForge Version: " + Server.Version;
}
}
void HeartBeatFail() {
WriteLine("Recent Heartbeat Failed");
}
void newError(string message)
{
try
{
if (txtErrors.InvokeRequired)
{
LogDelegate d = new LogDelegate(newError);
this.Invoke(d, new object[] { message });
}
else
{
txtErrors.AppendText(Environment.NewLine + message);
}
} catch { }
}
void newSystem(string message)
{
try
{
if (txtSystem.InvokeRequired)
{
LogDelegate d = new LogDelegate(newSystem);
this.Invoke(d, new object[] { message });
}
else
{
txtSystem.AppendText(Environment.NewLine + message);
}
} catch { }
}
delegate void LogDelegate(string message);
/// <summary>
/// Does the same as Console.Write() only in the form
/// </summary>
/// <param name="s">The string to write</param>
public void Write(string s) {
if (shuttingDown) return;
if (txtLog.InvokeRequired) {
LogDelegate d = new LogDelegate(Write);
this.Invoke(d, new object[] { s });
} else {
txtLog.AppendText(s);
}
}
/// <summary>
/// Does the same as Console.WriteLine() only in the form
/// </summary>
/// <param name="s">The line to write</param>
public void WriteLine(string s)
{
if (shuttingDown) return;
if (this.InvokeRequired) {
LogDelegate d = new LogDelegate(WriteLine);
this.Invoke(d, new object[] { s });
} else {
txtLog.AppendText("\r\n" + s);
}
}
/// <summary>
/// Updates the list of client names in the window
/// </summary>
/// <param name="players">The list of players to add</param>
public void UpdateClientList(List<Player> players) {
if (this.InvokeRequired) {
PlayerListCallback d = new PlayerListCallback(UpdateClientList);
this.Invoke(d, new object[] { players });
} else {
liClients.Items.Clear();
Player.players.ForEach(delegate(Player p) { liClients.Items.Add(p.name); });
}
}
public void UpdateMapList(string blah) {
if (this.InvokeRequired) {
LogDelegate d = new LogDelegate(UpdateMapList);
this.Invoke(d, new object[] { blah });
} else {
liMaps.Items.Clear();
foreach (Level level in Server.levels) {
liMaps.Items.Add(level.name + " - " + level.physics);
}
}
}
/// <summary>
/// Places the server's URL at the top of the window
/// </summary>
/// <param name="s">The URL to display</param>
public void UpdateUrl(string s)
{
if (this.InvokeRequired)
{
StringCallback d = new StringCallback(UpdateUrl);
this.Invoke(d, new object[] { s });
}
else
txtUrl.Text = s;
}
private void Window_FormClosing(object sender, FormClosingEventArgs e) {
if (notifyIcon1 != null) {
notifyIcon1.Visible = false;
}
MCLawl_.Gui.Program.ExitProgram(false);
}
private void txtInput_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (txtInput.Text == null || txtInput.Text.Trim()=="") { return; }
string text = txtInput.Text.Trim();
string newtext = text;
if (txtInput.Text[0] == '#')
{
newtext = text.Remove(0, 1).Trim();
Player.GlobalMessageOps("To Ops &f-"+Server.DefaultColor +"Console [&a" + Server.ZallState + Server.DefaultColor + "]&f- " + newtext);
Server.s.Log("(OPs): Console: " + newtext);
IRCBot.Say("Console: " + newtext, true);
// WriteLine("(OPs):<CONSOLE> " + txtInput.Text);
txtInput.Clear();
}
else
{
Player.GlobalMessage("Console [&a" + Server.ZallState + Server.DefaultColor + "]: &f" + txtInput.Text);
IRCBot.Say("Console [" + Server.ZallState + "]: " + txtInput.Text);
WriteLine("<CONSOLE> " + txtInput.Text);
txtInput.Clear();
}
}
}
private void txtCommands_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter) {
string sentCmd = "", sentMsg = "";
if (txtCommands.Text == null || txtCommands.Text.Trim() == "")
{
newCommand("CONSOLE: Whitespace commands are not allowed.");
txtCommands.Clear();
return;
}
if (txtCommands.Text[0] == '/')
if (txtCommands.Text.Length > 1)
txtCommands.Text = txtCommands.Text.Substring(1);
if (txtCommands.Text.IndexOf(' ') != -1) {
sentCmd = txtCommands.Text.Split(' ')[0];
sentMsg = txtCommands.Text.Substring(txtCommands.Text.IndexOf(' ') + 1);
} else if (txtCommands.Text != "") {
sentCmd = txtCommands.Text;
} else {
return;
}
try {
Command.all.Find(sentCmd).Use(null, sentMsg);
newCommand("CONSOLE: USED /" + sentCmd + " " + sentMsg);
} catch (Exception ex) {
Server.ErrorLog(ex);
newCommand("CONSOLE: Failed command.");
}
txtCommands.Clear();
}
}
private void btnClose_Click_1(object sender, EventArgs e) {
if (notifyIcon1 != null) {
notifyIcon1.Visible = false;
}
MCLawl_.Gui.Program.ExitProgram(false);
}
public void newCommand(string p) {
if (txtCommandsUsed.InvokeRequired)
{
LogDelegate d = new LogDelegate(newCommand);
this.Invoke(d, new object[] { p });
}
else
{
txtCommandsUsed.AppendText("\r\n" + p);
}
}
void ChangeCheck(string newCheck) { Server.ZallState = newCheck; }
private void txtHost_TextChanged(object sender, EventArgs e)
{
if (txtHost.Text != "") ChangeCheck(txtHost.Text);
}
private void btnProperties_Click_1(object sender, EventArgs e) {
if (!prevLoaded) { PropertyForm = new PropertyWindow(); prevLoaded = true; }
PropertyForm.Show();
}
private void btnUpdate_Click_1(object sender, EventArgs e) {
if (!MCLawl_.Gui.Program.CurrentUpdate)
MCLawl_.Gui.Program.UpdateCheck();
else {
Thread messageThread = new Thread(new ThreadStart(delegate {
MessageBox.Show("Already checking for updates.");
})); messageThread.Start();
}
}
public static bool prevLoaded = false;
Form PropertyForm;
Form UpdateForm;
private void gBChat_Enter(object sender, EventArgs e)
{
}
private void btnExtra_Click_1(object sender, EventArgs e) {
if (!prevLoaded) { PropertyForm = new PropertyWindow(); prevLoaded = true; }
PropertyForm.Show();
PropertyForm.Top = this.Top + this.Height - txtCommandsUsed.Height;
PropertyForm.Left = this.Left;
}
private void Window_Resize(object sender, EventArgs e) {
this.Hide();
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) {
this.Show();
this.BringToFront();
WindowState = FormWindowState.Normal;
}
private void button1_Click_1(object sender, EventArgs e)
{
UpdateForm = new UpdateWindow();
UpdateForm.Show();
}
private void tmrRestart_Tick(object sender, EventArgs e)
{
if (Server.autorestart)
{
if (DateTime.Now.TimeOfDay.CompareTo(Server.restarttime.TimeOfDay) > 0 && (DateTime.Now.TimeOfDay.CompareTo(Server.restarttime.AddSeconds(1).TimeOfDay)) < 0) {
Player.GlobalMessage("The time is now " + DateTime.Now.TimeOfDay);
Player.GlobalMessage("The server will now begin auto restart procedures.");
Server.s.Log("The time is now " + DateTime.Now.TimeOfDay);
Server.s.Log("The server will now begin auto restart procedures.");
if (notifyIcon1 != null) {
notifyIcon1.Icon = null;
notifyIcon1.Visible = false;
}
MCLawl_.Gui.Program.ExitProgram(true);
}
}
}
private void openConsole_Click(object sender, EventArgs e)
{
// Yes, it's a hacky fix. Don't ask :v
this.Show();
this.BringToFront();
WindowState = FormWindowState.Normal;
this.Show();
this.BringToFront();
WindowState = FormWindowState.Normal;
}
private void shutdownServer_Click(object sender, EventArgs e)
{
if (notifyIcon1 != null)
{
notifyIcon1.Visible = false;
}
MCLawl_.Gui.Program.ExitProgram(false);
}
private void voiceToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.liClients.SelectedIndex != -1)
{
Command.all.Find("voice").Use(null, this.liClients.SelectedItem.ToString());
}
}
private void whoisToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.liClients.SelectedIndex != -1)
{
Command.all.Find("whois").Use(null, this.liClients.SelectedItem.ToString());
}
}
private void kickToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.liClients.SelectedIndex != -1)
{
Command.all.Find("kick").Use(null, this.liClients.SelectedItem.ToString() + " You have been kicked by the console.");
}
}
private void banToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.liClients.SelectedIndex != -1)
{
Command.all.Find("ban").Use(null, this.liClients.SelectedItem.ToString());
}
}
private void liClients_MouseDown(object sender, MouseEventArgs e)
{
int i;
i = liClients.IndexFromPoint(e.X, e.Y);
liClients.SelectedIndex = i;
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
if (this.liMaps.SelectedIndex != -1)
{
Command.all.Find("physics").Use(null, this.liMaps.SelectedItem.ToString().Remove((this.liMaps.SelectedItem.ToString().Length - 4)) + " 0");
}
}
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
if (this.liMaps.SelectedIndex != -1)
{
Command.all.Find("physics").Use(null, this.liMaps.SelectedItem.ToString().Remove((this.liMaps.SelectedItem.ToString().Length - 4)) + " 1");
}
}
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
if (this.liMaps.SelectedIndex != -1)
{
Command.all.Find("physics").Use(null, this.liMaps.SelectedItem.ToString().Remove((this.liMaps.SelectedItem.ToString().Length - 4)) + " 2");
}
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
if (this.liMaps.SelectedIndex != -1)
{
Command.all.Find("physics").Use(null, this.liMaps.SelectedItem.ToString().Remove((this.liMaps.SelectedItem.ToString().Length - 4)) + " 3");
}
}
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{
if (this.liMaps.SelectedIndex != -1)
{
Command.all.Find("physics").Use(null, this.liMaps.SelectedItem.ToString().Remove((this.liMaps.SelectedItem.ToString().Length - 4)) + " 4");
}
}
private void unloadToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.liMaps.SelectedIndex != -1)
{
Command.all.Find("unload").Use(null, this.liMaps.SelectedItem.ToString().Remove((this.liMaps.SelectedItem.ToString().Length - 4)));
}
}
private void finiteModeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.liMaps.SelectedIndex != -1)
{
Command.all.Find("map").Use(null, this.liMaps.SelectedItem.ToString().Remove((this.liMaps.SelectedItem.ToString().Length - 4)) + " finite");
}
}
private void animalAIToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.liMaps.SelectedIndex != -1)
{
Command.all.Find("map").Use(null, this.liMaps.SelectedItem.ToString().Remove((this.liMaps.SelectedItem.ToString().Length - 4)) + " ai");
}
}
private void edgeWaterToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.liMaps.SelectedIndex != -1)
{
Command.all.Find("map").Use(null, this.liMaps.SelectedItem.ToString().Remove((this.liMaps.SelectedItem.ToString().Length - 4)) + " edge");
}
}
private void growingGrassToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.liMaps.SelectedIndex != -1)
{
Command.all.Find("map").Use(null, this.liMaps.SelectedItem.ToString().Remove((this.liMaps.SelectedItem.ToString().Length - 4)) + " grass");
}
}
private void survivalDeathToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.liMaps.SelectedIndex != -1)
{
Command.all.Find("map").Use(null, this.liMaps.SelectedItem.ToString().Remove((this.liMaps.SelectedItem.ToString().Length - 4)) + " death");
}
}
private void killerBlocksToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.liMaps.SelectedIndex != -1)
{
Command.all.Find("map").Use(null, this.liMaps.SelectedItem.ToString().Remove((this.liMaps.SelectedItem.ToString().Length - 4)) + " killer");
}
}
private void rPChatToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.liMaps.SelectedIndex != -1)
{
Command.all.Find("map").Use(null, this.liMaps.SelectedItem.ToString().Remove((this.liMaps.SelectedItem.ToString().Length - 4)) + " chat");
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.liMaps.SelectedIndex != -1)
{
Command.all.Find("save").Use(null, this.liMaps.SelectedItem.ToString().Remove((this.liMaps.SelectedItem.ToString().Length - 4)));
}
}
private void liMaps_MouseDown(object sender, MouseEventArgs e)
{
int i;
i = liMaps.IndexFromPoint(e.X, e.Y);
liMaps.SelectedIndex = i;
}
private void tabControl1_Click(object sender, EventArgs e)
{
foreach (TabPage tP in tabControl1.TabPages)
{
foreach (Control ctrl in tP.Controls)
{
if (ctrl is TextBox)
{
TextBox txtBox = (TextBox)ctrl;
txtBox.Update();
}
}
}
}
}
}