-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinecraftServer.cs
145 lines (126 loc) · 4.2 KB
/
MinecraftServer.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MineSharp
{
class MinecraftServer
{
public event OutputEventHandler OnOutputReceived;
private Process ServerProcess;
bool serverRunning = false;
private StreamWriter InputStreamWriter;
private DataReceivedEventHandler TheDataReceievedEventHandler;
public MinecraftServer()
{
OnOutputReceived = new OutputEventHandler(OnOutputReceived_method);
}
private void OnOutputReceived_method(OutputEventArgs e){}
public bool Launch(string _serverPath, string jvmExePath, string ramAllocation)
{
bool ready = false;
if (!serverRunning)
{
try
{
string jarFile = _serverPath + "/server.jar";
ProcessStartInfo ssi = new ProcessStartInfo(jvmExePath, ramAllocation+" -jar \"" + jarFile + "\" nogui -nojline")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = _serverPath
};
if((ServerProcess = Process.Start(ssi)) != null)
{
ready = true;
serverRunning = true;
ServerProcess.EnableRaisingEvents = true;
ServerProcess.Exited += new EventHandler(ServerProcess_Exited);
InputStreamWriter = ServerProcess.StandardInput;
TheDataReceievedEventHandler = new DataReceivedEventHandler(StandardOutputHandler); //todo
ServerProcess.OutputDataReceived += TheDataReceievedEventHandler;
ServerProcess.BeginOutputReadLine();
ServerProcess.ErrorDataReceived += TheDataReceievedEventHandler;
ServerProcess.BeginErrorReadLine();
}
}
catch
{
}
}
else
{
ready = true;
}
return ready;
}
public bool Stop()
{
if (serverRunning)
{
try
{
SendCommand("stop");
SendCommand("");
return ServerProcess.WaitForExit(20000);
}
catch
{
}
return false;
}
else
{
return true;
}
}
public bool SendCommand(string Command)
{
try
{
if (serverRunning)
{
InputStreamWriter.WriteLine(Command);
return true;
}
}
catch
{
}
return false;
}
private void StandardOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!(String.IsNullOrEmpty(outLine.Data)))
{
OnOutputReceived.Invoke(new OutputEventArgs(outLine.Data));
}
}
private void ServerProcess_Exited(object sender, EventArgs e)
{
try
{
if (serverRunning)
{
serverRunning = false;
ServerProcess.CancelOutputRead();
ServerProcess.CancelErrorRead();
ServerProcess.OutputDataReceived -= TheDataReceievedEventHandler;
ServerProcess.ErrorDataReceived -= TheDataReceievedEventHandler;
ServerProcess.Close();
ServerProcess.Dispose();
ServerProcess = null;
}
}
catch
{
}
}
}
}