PageRenderTime 107ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/src/net/minecraft/src/DedicatedServer.java

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