forked from umby24/Minebot
/
Form1.cs
623 lines (542 loc) · 22.8 KB
/
Form1.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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Winsock_Orcas;
using System.Text.RegularExpressions;
using System.Collections;
using C_Minebot.Classes;
using System.Reflection;
namespace C_Minebot
{
public partial class Form1 : Form
{
#region Variables
public bool following = false;
public string fname = "";
#region Bot Specific
public string prefix = "+";
public bool setopen = false;
public bool muted = false;
public bool onlineMode;
public Color windowColor, TextColor, TextAColor;
public Boolean flatten;
public Boolean colorize = true;
public List<string> admins;
public List<Classes.Item> inventory = new List<Classes.Item>();
#endregion
#region Server
public short health, hunger, selectedSlot;
public long time, worldAge;
public int[] spawnPoint;
public int EntityID;
public bool onground;
public float[] position;
public double[] location;
public string ServerID, serverHash, sessionId, sip, sport;
public List<Chunk> Chunks = new List<Chunk>();
public List<Entity> Entitys = new List<Entity>();
#endregion
#region Encryption
public byte[] PublicKey, token;
public byte[] sharedkey = new byte[16];
#endregion
#region Network
public string username;
public networkHandler nh;
#endregion
#region IRC Bot
public Winsock_Orcas.Winsock IRCSock;
public int ircmode = 0;
public int ircPort;
public string channel, ircname, ircIP;
public bool canTalk = false;
#endregion
#endregion
#region Colorized Chatbox
public const int EM_GETLINECOUNT = 0xBA;
public const int EM_LINESCROLL = 0xB6;
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
#endregion
#region Form Events
public Form1()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
string resourceName = new AssemblyName(args.Name).Name + ".dll";
string resource = Array.Find(this.GetType().Assembly.GetManifestResourceNames(), element => element.EndsWith(resourceName));
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Load interface Customizations
loadColors();
// Load bot allowed administrators.
RegistryControl Reg = new RegistryControl();
admins = new List<string>();
string AdminString = Reg.GetSetting("SH", "Minebot SMP", "admins", "").ToString();
if (AdminString != "") {
string[] mysplits = AdminString.Split('|');
for (int i = 0; i < mysplits.Length; i++) {
if (mysplits[i] != "") {
admins.Add(mysplits[i]);
}
}
}
// Load IRC settings
try
{
ircIP = (string)Reg.GetSetting("SH", "Minebot SMP", "ircIP", "irc.esper.net");
ircPort = int.Parse(Reg.GetSetting("SH", "Minebot SMP", "ircPort", 6667).ToString());
channel = (string)Reg.GetSetting("SH", "Minebot SMP", "ircChan", "#bot");
ircname = (string)Reg.GetSetting("SH", "Minebot SMP", "ircName", "VBMinebot");
}
catch (Exception f)
{
puts("An error occured while loading IRC settings. This shouldn't affect functionality of the bot. \n" + f.Message);
}
// Load command prefix
RegistryControl reg = new RegistryControl();
prefix = (string)reg.GetSetting("SH", "Minebot SMP", "prefix", "+");
admins.Add("Minebot");
putsc("=_=_=_= C# Minebot =_=_=_=", Color.Yellow);
putsc("=+=+=+= Version 1.5 =+=+=+=", Color.Blue);
putsc("======= by Umby24 ========", Color.Red);
putsc("-------- All settings loaded ---------", Color.Orange);
}
private void Form1_Close(object sender, EventArgs e)
{
if (nh != null)
{
Packets.Kick leaving = new Packets.Kick(nh.socket, this,true);
System.Threading.Thread.Sleep(200);
nh.stop();
}
}
#endregion
#region FormHelpers
public void loadColors()
{
//Retrieve color settings from registry.
RegistryControl reg = new RegistryControl();
windowColor = Color.FromArgb(int.Parse(reg.GetSetting("SH", "Minebot SMP", "bcr", 255).ToString()), int.Parse(reg.GetSetting("SH", "Minebot SMP", "bcg", 255).ToString()), int.Parse(reg.GetSetting("SH", "Minebot SMP", "bcb", 255).ToString()));
TextColor = Color.FromArgb(int.Parse(reg.GetSetting("SH", "Minebot SMP", "ter", 0).ToString()), int.Parse(reg.GetSetting("SH", "Minebot SMP", "teg", 0).ToString()), int.Parse(reg.GetSetting("SH", "Minebot SMP", "teb", 0).ToString()));
TextAColor = Color.FromArgb(int.Parse(reg.GetSetting("SH", "Minebot SMP", "bgr", 192).ToString()), int.Parse(reg.GetSetting("SH", "Minebot SMP", "bgg", 192).ToString()), int.Parse(reg.GetSetting("SH", "Minebot SMP", "bgb", 192).ToString()));
flatten = Boolean.Parse(reg.GetSetting("SH", "Minebot SMP", "flat", "false").ToString());
colorize = Boolean.Parse(reg.GetSetting("SH", "Minebot SMP", "colored", "true").ToString());
if (flatten == true)
{
btnSend.FlatStyle = FlatStyle.Flat;
}
else
{
btnSend.FlatStyle = FlatStyle.Standard;
}
//Set the colors from this form, the other forms will pull from this form to color themselves.
this.BackColor = windowColor;
this.ForeColor = TextColor;
btnSend.ForeColor = TextColor;
btnSend.BackColor = windowColor;
this.lstOnline.BackColor = TextAColor;
lstOnline.ForeColor = TextColor;
console.BackColor = TextAColor;
console.ForeColor = TextColor;
chat.BackColor = TextAColor;
chat.ForeColor = TextColor;
this.ResumeLayout(false);
this.PerformLayout();
}
public void beginConnect(string IP, string port, string UN, string PW, bool online)
{
lstOnline.Items.Clear();
username = UN;
onlineMode = online;
sip = IP;
sport = port;
if (online && sessionId == null)
{
puts("Logging in to Minecraft.net...");
Minecraft_Net_Interaction Login = new Minecraft_Net_Interaction();
string Response = Login.Login(UN, PW);
if (Response.Contains(':'))
{
string[] mysplit = Response.Split(':');
username = mysplit[2];
sessionId = mysplit[3];
puts("Done.");
}
else
{
if (sessionId != null)
{
puts("Error logging in to Minecraft.net!");
puts(Response);
}
else
{
puts("Attempting to log in with old credentials..");
}
}
}
else
sessionId = "epicw1n";
//Begin connection to server.
nh = new networkHandler(IP, port, this);
nh.start();
}
#endregion
#region Threadsafe
private delegate void putss(string text);
private delegate void putsc_(string text, Color tColor, bool append);
public void putsc(string text, Color tColor, bool append)
{
if (this.InvokeRequired)
{
this.Invoke(new putsc_(putsc), text, tColor, append);
}
else {
if (append == true)
{
int intLines = SendMessage(console.Handle, EM_GETLINECOUNT, 0, 0);
console.AppendText(text);
console.Select(console.Text.Length - text.Length, text.Length);
console.SelectionColor = tColor;
int linesToAdd = (SendMessage(console.Handle, EM_GETLINECOUNT, 0, 0) - intLines);
SendMessage(console.Handle, EM_LINESCROLL, 0, linesToAdd);
return;
}
int aintLines = SendMessage(console.Handle, EM_GETLINECOUNT, 0, 0);
console.AppendText(Environment.NewLine);
console.AppendText(text);
console.Select(console.Text.Length - text.Length, text.Length);
console.SelectionColor = tColor;
int blinesToAdd = (SendMessage(console.Handle, EM_GETLINECOUNT, 0, 0) - aintLines);
SendMessage(console.Handle, EM_LINESCROLL, 0, blinesToAdd);
}
}
public void putsc(string text, Color tColor)
{
bool append = false;
if (this.InvokeRequired)
{
this.Invoke(new putsc_(putsc), text, tColor, append);
}
else
{
if (append == true)
{
int intLines = SendMessage(console.Handle, EM_GETLINECOUNT, 0, 0);
console.AppendText(text);
console.Select(console.Text.Length - text.Length, text.Length);
console.SelectionColor = tColor;
int linesToAdd = (SendMessage(console.Handle, EM_GETLINECOUNT, 0, 0) - intLines);
SendMessage(console.Handle, EM_LINESCROLL, 0, linesToAdd);
return;
}
int aintLines = SendMessage(console.Handle, EM_GETLINECOUNT, 0, 0);
console.AppendText(Environment.NewLine);
console.AppendText(text);
console.Select(console.Text.Length - text.Length, text.Length);
console.SelectionColor = tColor;
int blinesToAdd = (SendMessage(console.Handle, EM_GETLINECOUNT, 0, 0) - aintLines);
SendMessage(console.Handle, EM_LINESCROLL, 0, blinesToAdd);
}
}
public void puts(string text)
{
if (this.InvokeRequired)
{
try {
this.Invoke(new putss(puts), text);
}
catch (ObjectDisposedException) { }
}
else
{
console.AppendText(Environment.NewLine + text);
console.Select(console.Text.Length, console.Text.Length);
console.ScrollToCaret();
}
}
public void add(string name)
{
if (this.InvokeRequired)
{
this.Invoke(new putss(add), name);
}
else
{
if (lstOnline.Items.Contains(name) == false)
lstOnline.Items.Add(name);
}
}
public void remove(string name)
{
if (this.InvokeRequired)
{
this.Invoke(new putss(remove), name);
}
else
{
if (lstOnline.Items.Contains(name) == false)
lstOnline.Items.Remove(name);
}
}
#endregion
#region IRC
public void stopIRC()
{
IRCSock.Close();
}
public void startIRC()
{
RegistryControl Reg = new RegistryControl();
ircIP = (string)Reg.GetSetting("SH", "Minebot SMP", "ircIP", "irc.esper.net");
int.TryParse((string)Reg.GetSetting("SH", "Minebot SMP", "ircPort", 6667), out ircPort);
channel = (string)Reg.GetSetting("SH", "Minebot SMP", "ircChan", "#bot");
ircname = (string)Reg.GetSetting("SH", "Minebot SMP", "ircName", "VBMimebot");
IRCSock = new Winsock();
IRCSock.BufferSize = 8192;
IRCSock.LegacySupport = true;
IRCSock.Protocol = Winsock_Orcas.WinsockProtocol.Tcp;
IRCSock.Connected += new Winsock_Orcas.IWinsock.ConnectedEventHandler(IRCSock_Connected);
IRCSock.DataArrival += new Winsock_Orcas.IWinsock.DataArrivalEventHandler(IRCSock_DataArrival);
IRCSock.Connect(ircIP, ircPort);
puts(Environment.NewLine + "Connecting.");
}
public void IRCSock_Connected(object sender, WinsockConnectedEventArgs e)
{
send("NICK " + ircname);
send("USER C C C :" + ircname);
send("MODE " + ircname + " +i");
send("JOIN " + channel);
puts("Connected.");
}
public string translate_colors(string text)
{
string smessage = text;
if (smessage.Contains("§"))
{
smessage = smessage.Replace("§0", ((Char) 3) + "00");
smessage = smessage.Replace("§1", ((Char)3) + "02");
smessage = smessage.Replace("§2", ((Char)3) + "03");
smessage = smessage.Replace("§3", ((Char)3) + "10");
smessage = smessage.Replace("§4", ((Char)3) + "05");
smessage = smessage.Replace("§5", ((Char)3) + "06");
smessage = smessage.Replace("§6", ((Char)3) + "07");
smessage = smessage.Replace("§7", ((Char)3) + "15");
smessage = smessage.Replace("§8", ((Char)3) + "14");
smessage = smessage.Replace("§9", ((Char)3) + "12");
smessage = smessage.Replace("§a", ((Char)3) + "09");
smessage = smessage.Replace("§b", ((Char)3) + "11");
smessage = smessage.Replace("§c", ((Char)3) + "04");
smessage = smessage.Replace("§d", ((Char)3) + "13");
smessage = smessage.Replace("§e", ((Char)3) + "08");
smessage = smessage.Replace("§f", ((Char)3) + "01");
smessage = smessage.Replace("§A", ((Char)3) + "09");
smessage = smessage.Replace("§B", ((Char)3) + "11");
smessage = smessage.Replace("§C", ((Char)3) + "04");
smessage = smessage.Replace("§D", ((Char)3) + "13");
smessage = smessage.Replace("§E", ((Char)3) + "08");
smessage = smessage.Replace("§F", ((Char)3) + "01");
}
return smessage;
}
public void IRCSock_DataArrival(object sender, WinsockDataArrivalEventArgs e)
{
string incoming = Encoding.UTF8.GetString((byte[])IRCSock.Get());
string host = "";
string dat = "";
string message = "";
string[] splits;
int index = 0;
splits = incoming.Split('\r','\n');
for (int b = 0; (splits.Length - 1) >= b;b++)
{
splits[b] = splits[b].Replace("\r\n","");
splits[b] = splits[b].Replace("\n","");
splits[b] = splits[b].Replace("\r","");
}
do
{
incoming = splits[index];
if (incoming.Contains(" "))
{
if (incoming.Contains("/NAMES list"))
canTalk = true;
if (incoming.Substring(0, 1) == ":")
host = incoming.Substring(1, incoming.IndexOf(" ") - 1);
else
host = incoming.Substring(0, incoming.IndexOf(" "));
dat = incoming.Substring(incoming.IndexOf(" ") + 1, incoming.Length - (incoming.IndexOf(" ") + 1));
if (dat.Contains(":"))
message = dat.Substring(dat.IndexOf(":") + 1, dat.Length - (dat.IndexOf(":") + 1));
if (host == "PING")
{
send("PONG " + dat);
return;
}
string second = dat.Substring(0, dat.IndexOf(" "));
string[] mysplits = dat.Split(new string[] { " " }, 4, StringSplitOptions.None);
message = message.Replace("\r\n", "");
switch (second)
{
case "PRIVMSG":
if (ircmode == 1 || ircmode == 3 && message.StartsWith("+") == false)
{
nh.socket.writeByte(3);
nh.socket.writeString("IRC: <" + host.Substring(0, host.IndexOf("!")) + "> " + message);
}
if (message.StartsWith("="))
{
string cmd = mysplits[2].Substring(1, mysplits[2].Length - 1);
switch (cmd.ToLower())
{
case "=say":
if (mysplits.Length > 3)
ircmessage(mysplits[3]);
break;
case "=ssay":
Packets.chatMessage mypacket;
if (mysplits.Length > 3)
mypacket = new Packets.chatMessage(nh.socket, this, mysplits[3], true);
break;
case "=help":
ircmessage("C# Minebot IRC Client, Version 1.1");
ircmessage("Only functional to relay messages between IRC channels and minecraft servers.");
ircmessage("Commands are =say, =ssay, and =help.");
break;
}
}
break;
case "376":
send("JOIN " + channel);
ircmessage("Current mode: " + ircmode);
break;
}
}
index++;
} while (index == splits.Length - 1);
incoming = "";
}
string stripillegal2(string text)
{
string final = "";
foreach (char b in text)
{
if ((Char.IsLetterOrDigit(b) && (b == '!' || b == '@' || b == '#' || b == '$' || b == '%' || b == '*' || b == '(' || b == ')' || b == '=' || b == '-' || b == '_' || b == '+' || b == '/' || b == '\\' || b == '<' || b == '>' || b == '?' || b == '.' || b == ',' || b == ' ')))
{
final += b;
}
}
return final;
}
public void send(string msg)
{
if (IRCSock == null)
return;
if (IRCSock.State == WinsockStates.Connected)
{
msg += "\r\n";
byte[] data = System.Text.ASCIIEncoding.UTF8.GetBytes(msg);
IRCSock.Send(data);
}
}
public void ircmessage(string text)
{
send("PRIVMSG " + channel + " :" + text);
}
#endregion
#region Button Clicks
private void trackerToolStripMenuItem_Click(object sender, EventArgs e) {
Entity_Tracker tracker = new Entity_Tracker();
tracker.Show();
tracker.startTracking(this);
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("This is the official successor to VB Minebot, written so I could learn C#, and to get this project up again, but cleaner.", "About C# Minebot");
}
private void connectToolStripMenuItem_Click(object sender, EventArgs e)
{
if (setopen == false)
{
Settings whatever = new Settings();
whatever.myform = this;
whatever.Show();
setopen = true;
}
}
private void btnSend_Click(object sender, EventArgs e)
{
if (chat.Text.StartsWith("+"))
{
commandHandler ch = new commandHandler(nh.socket, this, "Minebot: " + chat.Text);
chat.Clear();
return;
}
Packets.chatMessage chatmess = new Packets.chatMessage(nh.socket, this, chat.Text, true);
chat.Clear();
}
private void muteToolStripMenuItem_Click(object sender, EventArgs e)
{
if (muted)
{
muted = false;
muteToolStripMenuItem.Checked = false;
}
else
{
muted = true;
muteToolStripMenuItem.Checked = true;
}
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (setopen == false)
{
Settings whatever = new Settings();
whatever.myform = this;
whatever.Show();
setopen = true;
}
}
private void reconnectToolStripMenuItem_Click(object sender, EventArgs e)
{
if (nh != null)
{
nh.stop();
Entitys.Clear();
Chunks.Clear();
inventory.Clear();
following = false;
fname = "";
if (sip != null)
beginConnect(sip, sport, username, "asdf", onlineMode);
}
}
private void asdToolStripMenuItem_Click(object sender, EventArgs e)
{
functions thisLookup = new functions();
string build = "";
for (int i = 0; i < 44; i++) {
build += i.ToString() + ": " + thisLookup.getitembyslot(i,this) + "(" + thisLookup.getItemCount(i,this) + ")" + Environment.NewLine;
}
MessageBox.Show(build);
}
#endregion
}
}