Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lDEVinux
GitHub Repository: lDEVinux/eaglercraft
Path: blob/main/sp-server/src_aux/DedicatedServer.java
8641 views
1
package net.minecraft.src;
2
3
import java.io.File;
4
import java.io.IOException;
5
import java.net.InetAddress;
6
import java.util.ArrayList;
7
import java.util.Collections;
8
import java.util.List;
9
import java.util.Random;
10
import net.minecraft.server.MinecraftServer;
11
12
public class DedicatedServer extends MinecraftServer implements IServer {
13
private final List pendingCommandList = Collections.synchronizedList(new ArrayList());
14
private final ILogAgent field_98131_l;
15
private RConThreadQuery theRConThreadQuery;
16
private RConThreadMain theRConThreadMain;
17
private PropertyManager settings;
18
private boolean canSpawnStructures;
19
private EnumGameType gameType;
20
private NetworkListenThread networkThread;
21
private boolean guiIsEnabled = false;
22
23
public DedicatedServer(File par1File) {
24
super(par1File);
25
this.field_98131_l = new LogAgent("Minecraft-Server", (String) null,
26
(new File(par1File, "server.log")).getAbsolutePath());
27
new DedicatedServerSleepThread(this);
28
}
29
30
/**
31
* Initialises the server and starts it.
32
*/
33
protected boolean startServer() throws IOException {
34
DedicatedServerCommandThread var1 = new DedicatedServerCommandThread(this);
35
var1.setDaemon(true);
36
var1.start();
37
this.getLogAgent().func_98233_a("Starting minecraft server version 1.5.2");
38
39
if (Runtime.getRuntime().maxMemory() / 1024L / 1024L < 512L) {
40
this.getLogAgent().func_98236_b(
41
"To start the server with more ram, launch it as \"java -Xmx1024M -Xms1024M -jar minecraft_server.jar\"");
42
}
43
44
this.getLogAgent().func_98233_a("Loading properties");
45
this.settings = new PropertyManager(new File("server.properties"), this.getLogAgent());
46
47
if (this.isSinglePlayer()) {
48
this.setHostname("127.0.0.1");
49
} else {
50
this.setOnlineMode(this.settings.getBooleanProperty("online-mode", true));
51
this.setHostname(this.settings.getStringProperty("server-ip", ""));
52
}
53
54
this.setCanSpawnAnimals(this.settings.getBooleanProperty("spawn-animals", true));
55
this.setCanSpawnNPCs(this.settings.getBooleanProperty("spawn-npcs", true));
56
this.setAllowPvp(this.settings.getBooleanProperty("pvp", true));
57
this.setAllowFlight(this.settings.getBooleanProperty("allow-flight", false));
58
this.setTexturePack(this.settings.getStringProperty("texture-pack", ""));
59
this.setMOTD(this.settings.getStringProperty("motd", "A Minecraft Server"));
60
this.func_104055_i(this.settings.getBooleanProperty("force-gamemode", false));
61
62
if (this.settings.getIntProperty("difficulty", 1) < 0) {
63
this.settings.setProperty("difficulty", Integer.valueOf(0));
64
} else if (this.settings.getIntProperty("difficulty", 1) > 3) {
65
this.settings.setProperty("difficulty", Integer.valueOf(3));
66
}
67
68
this.canSpawnStructures = this.settings.getBooleanProperty("generate-structures", true);
69
int var2 = this.settings.getIntProperty("gamemode", EnumGameType.SURVIVAL.getID());
70
this.gameType = WorldSettings.getGameTypeById(var2);
71
this.getLogAgent().func_98233_a("Default game type: " + this.gameType);
72
InetAddress var3 = null;
73
74
if (this.getServerHostname().length() > 0) {
75
var3 = InetAddress.getByName(this.getServerHostname());
76
}
77
78
if (this.getServerPort() < 0) {
79
this.setServerPort(this.settings.getIntProperty("server-port", 25565));
80
}
81
82
this.getLogAgent().func_98233_a("Generating keypair");
83
this.setKeyPair(CryptManager.generateKeyPair());
84
this.getLogAgent()
85
.func_98233_a("Starting Minecraft server on "
86
+ (this.getServerHostname().length() == 0 ? "*" : this.getServerHostname()) + ":"
87
+ this.getServerPort());
88
89
try {
90
this.networkThread = new DedicatedServerListenThread(this, var3, this.getServerPort());
91
} catch (IOException var16) {
92
this.getLogAgent().func_98236_b("**** FAILED TO BIND TO PORT!");
93
this.getLogAgent().logWarningFormatted("The exception was: {0}", new Object[] { var16.toString() });
94
this.getLogAgent().func_98236_b("Perhaps a server is already running on that port?");
95
return false;
96
}
97
98
if (!this.isServerInOnlineMode()) {
99
this.getLogAgent().func_98236_b("**** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!");
100
this.getLogAgent().func_98236_b("The server will make no attempt to authenticate usernames. Beware.");
101
this.getLogAgent().func_98236_b(
102
"While this makes the game possible to play without internet access, it also opens up the ability for hackers to connect with any username they choose.");
103
this.getLogAgent()
104
.func_98236_b("To change this, set \"online-mode\" to \"true\" in the server.properties file.");
105
}
106
107
this.setConfigurationManager(new DedicatedPlayerList(this));
108
long var4 = System.nanoTime();
109
110
if (this.getFolderName() == null) {
111
this.setFolderName(this.settings.getStringProperty("level-name", "world"));
112
}
113
114
String var6 = this.settings.getStringProperty("level-seed", "");
115
String var7 = this.settings.getStringProperty("level-type", "DEFAULT");
116
String var8 = this.settings.getStringProperty("generator-settings", "");
117
long var9 = (new Random()).nextLong();
118
119
if (var6.length() > 0) {
120
try {
121
long var11 = Long.parseLong(var6);
122
123
if (var11 != 0L) {
124
var9 = var11;
125
}
126
} catch (NumberFormatException var15) {
127
var9 = (long) var6.hashCode();
128
}
129
}
130
131
WorldType var17 = WorldType.parseWorldType(var7);
132
133
if (var17 == null) {
134
var17 = WorldType.DEFAULT;
135
}
136
137
this.setBuildLimit(this.settings.getIntProperty("max-build-height", 256));
138
this.setBuildLimit((this.getBuildLimit() + 8) / 16 * 16);
139
this.setBuildLimit(MathHelper.clamp_int(this.getBuildLimit(), 64, 256));
140
this.settings.setProperty("max-build-height", Integer.valueOf(this.getBuildLimit()));
141
this.getLogAgent().func_98233_a("Preparing level \"" + this.getFolderName() + "\"");
142
this.loadAllWorlds(this.getFolderName(), this.getFolderName(), var9, var17, var8);
143
long var12 = System.nanoTime() - var4;
144
String var14 = String.format("%.3fs", new Object[] { Double.valueOf((double) var12 / 1.0E9D) });
145
this.getLogAgent().func_98233_a("Done (" + var14 + ")! For help, type \"help\" or \"?\"");
146
147
if (this.settings.getBooleanProperty("enable-query", false)) {
148
this.getLogAgent().func_98233_a("Starting GS4 status listener");
149
this.theRConThreadQuery = new RConThreadQuery(this);
150
this.theRConThreadQuery.startThread();
151
}
152
153
if (this.settings.getBooleanProperty("enable-rcon", false)) {
154
this.getLogAgent().func_98233_a("Starting remote control listener");
155
this.theRConThreadMain = new RConThreadMain(this);
156
this.theRConThreadMain.startThread();
157
}
158
159
return true;
160
}
161
162
public boolean canStructuresSpawn() {
163
return this.canSpawnStructures;
164
}
165
166
public EnumGameType getGameType() {
167
return this.gameType;
168
}
169
170
/**
171
* Defaults to "1" (Easy) for the dedicated server, defaults to "2" (Normal) on
172
* the client.
173
*/
174
public int getDifficulty() {
175
return this.settings.getIntProperty("difficulty", 1);
176
}
177
178
/**
179
* Defaults to false.
180
*/
181
public boolean isHardcore() {
182
return this.settings.getBooleanProperty("hardcore", false);
183
}
184
185
/**
186
* Called on exit from the main run() loop.
187
*/
188
protected void finalTick(CrashReport par1CrashReport) {
189
while (this.isServerRunning()) {
190
this.executePendingCommands();
191
192
try {
193
Thread.sleep(10L);
194
} catch (InterruptedException var3) {
195
var3.printStackTrace();
196
}
197
}
198
}
199
200
/**
201
* Adds the server info, including from theWorldServer, to the crash report.
202
*/
203
public CrashReport addServerInfoToCrashReport(CrashReport par1CrashReport) {
204
par1CrashReport = super.addServerInfoToCrashReport(par1CrashReport);
205
par1CrashReport.func_85056_g().addCrashSectionCallable("Is Modded", new CallableType(this));
206
par1CrashReport.func_85056_g().addCrashSectionCallable("Type", new CallableServerType(this));
207
return par1CrashReport;
208
}
209
210
/**
211
* Directly calls System.exit(0), instantly killing the program.
212
*/
213
protected void systemExitNow() {
214
System.exit(0);
215
}
216
217
public void updateTimeLightAndEntities() {
218
super.updateTimeLightAndEntities();
219
this.executePendingCommands();
220
}
221
222
public boolean getAllowNether() {
223
return this.settings.getBooleanProperty("allow-nether", true);
224
}
225
226
public boolean allowSpawnMonsters() {
227
return this.settings.getBooleanProperty("spawn-monsters", true);
228
}
229
230
public void addServerStatsToSnooper(PlayerUsageSnooper par1PlayerUsageSnooper) {
231
par1PlayerUsageSnooper.addData("whitelist_enabled",
232
Boolean.valueOf(this.getDedicatedPlayerList().isWhiteListEnabled()));
233
par1PlayerUsageSnooper.addData("whitelist_count",
234
Integer.valueOf(this.getDedicatedPlayerList().getWhiteListedPlayers().size()));
235
super.addServerStatsToSnooper(par1PlayerUsageSnooper);
236
}
237
238
/**
239
* Returns whether snooping is enabled or not.
240
*/
241
public boolean isSnooperEnabled() {
242
return this.settings.getBooleanProperty("snooper-enabled", true);
243
}
244
245
public void addPendingCommand(String par1Str, ICommandSender par2ICommandSender) {
246
this.pendingCommandList.add(new ServerCommand(par1Str, par2ICommandSender));
247
}
248
249
public void executePendingCommands() {
250
while (!this.pendingCommandList.isEmpty()) {
251
ServerCommand var1 = (ServerCommand) this.pendingCommandList.remove(0);
252
this.getCommandManager().executeCommand(var1.sender, var1.command);
253
}
254
}
255
256
public boolean isDedicatedServer() {
257
return true;
258
}
259
260
public DedicatedPlayerList getDedicatedPlayerList() {
261
return (DedicatedPlayerList) super.getConfigurationManager();
262
}
263
264
public NetworkListenThread getNetworkThread() {
265
return this.networkThread;
266
}
267
268
/**
269
* Gets an integer property. If it does not exist, set it to the specified
270
* value.
271
*/
272
public int getIntProperty(String par1Str, int par2) {
273
return this.settings.getIntProperty(par1Str, par2);
274
}
275
276
/**
277
* Gets a string property. If it does not exist, set it to the specified value.
278
*/
279
public String getStringProperty(String par1Str, String par2Str) {
280
return this.settings.getStringProperty(par1Str, par2Str);
281
}
282
283
/**
284
* Gets a boolean property. If it does not exist, set it to the specified value.
285
*/
286
public boolean getBooleanProperty(String par1Str, boolean par2) {
287
return this.settings.getBooleanProperty(par1Str, par2);
288
}
289
290
/**
291
* Saves an Object with the given property name.
292
*/
293
public void setProperty(String par1Str, Object par2Obj) {
294
this.settings.setProperty(par1Str, par2Obj);
295
}
296
297
/**
298
* Saves all of the server properties to the properties file.
299
*/
300
public void saveProperties() {
301
this.settings.saveProperties();
302
}
303
304
/**
305
* Returns the filename where server properties are stored
306
*/
307
public String getSettingsFilename() {
308
File var1 = this.settings.getPropertiesFile();
309
return var1 != null ? var1.getAbsolutePath() : "No settings file";
310
}
311
312
public void enableGui() {
313
ServerGUI.initGUI(this);
314
this.guiIsEnabled = true;
315
}
316
317
public boolean getGuiEnabled() {
318
return this.guiIsEnabled;
319
}
320
321
/**
322
* On dedicated does nothing. On integrated, sets commandsAllowedForAll,
323
* gameType and allows external connections.
324
*/
325
public String shareToLAN(EnumGameType par1EnumGameType, boolean par2) {
326
return "";
327
}
328
329
/**
330
* Return whether command blocks are enabled.
331
*/
332
public boolean isCommandBlockEnabled() {
333
return this.settings.getBooleanProperty("enable-command-block", false);
334
}
335
336
/**
337
* Return the spawn protection area's size.
338
*/
339
public int getSpawnProtectionSize() {
340
return this.settings.getIntProperty("spawn-protection", super.getSpawnProtectionSize());
341
}
342
343
public boolean func_96290_a(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer) {
344
if (par1World.provider.dimensionId != 0) {
345
return false;
346
} else if (this.getDedicatedPlayerList().getOps().isEmpty()) {
347
return false;
348
} else if (this.getDedicatedPlayerList().areCommandsAllowed(par5EntityPlayer.username)) {
349
return false;
350
} else if (this.getSpawnProtectionSize() <= 0) {
351
return false;
352
} else {
353
ChunkCoordinates var6 = par1World.getSpawnPoint();
354
int var7 = MathHelper.abs_int(par2 - var6.posX);
355
int var8 = MathHelper.abs_int(par4 - var6.posZ);
356
int var9 = Math.max(var7, var8);
357
return var9 <= this.getSpawnProtectionSize();
358
}
359
}
360
361
public ILogAgent getLogAgent() {
362
return this.field_98131_l;
363
}
364
365
public ServerConfigurationManager getConfigurationManager() {
366
return this.getDedicatedPlayerList();
367
}
368
}
369
370