Path: blob/main/sp-server/src_aux/DedicatedServer.java
8641 views
package net.minecraft.src;12import java.io.File;3import java.io.IOException;4import java.net.InetAddress;5import java.util.ArrayList;6import java.util.Collections;7import java.util.List;8import java.util.Random;9import net.minecraft.server.MinecraftServer;1011public class DedicatedServer extends MinecraftServer implements IServer {12private final List pendingCommandList = Collections.synchronizedList(new ArrayList());13private final ILogAgent field_98131_l;14private RConThreadQuery theRConThreadQuery;15private RConThreadMain theRConThreadMain;16private PropertyManager settings;17private boolean canSpawnStructures;18private EnumGameType gameType;19private NetworkListenThread networkThread;20private boolean guiIsEnabled = false;2122public DedicatedServer(File par1File) {23super(par1File);24this.field_98131_l = new LogAgent("Minecraft-Server", (String) null,25(new File(par1File, "server.log")).getAbsolutePath());26new DedicatedServerSleepThread(this);27}2829/**30* Initialises the server and starts it.31*/32protected boolean startServer() throws IOException {33DedicatedServerCommandThread var1 = new DedicatedServerCommandThread(this);34var1.setDaemon(true);35var1.start();36this.getLogAgent().func_98233_a("Starting minecraft server version 1.5.2");3738if (Runtime.getRuntime().maxMemory() / 1024L / 1024L < 512L) {39this.getLogAgent().func_98236_b(40"To start the server with more ram, launch it as \"java -Xmx1024M -Xms1024M -jar minecraft_server.jar\"");41}4243this.getLogAgent().func_98233_a("Loading properties");44this.settings = new PropertyManager(new File("server.properties"), this.getLogAgent());4546if (this.isSinglePlayer()) {47this.setHostname("127.0.0.1");48} else {49this.setOnlineMode(this.settings.getBooleanProperty("online-mode", true));50this.setHostname(this.settings.getStringProperty("server-ip", ""));51}5253this.setCanSpawnAnimals(this.settings.getBooleanProperty("spawn-animals", true));54this.setCanSpawnNPCs(this.settings.getBooleanProperty("spawn-npcs", true));55this.setAllowPvp(this.settings.getBooleanProperty("pvp", true));56this.setAllowFlight(this.settings.getBooleanProperty("allow-flight", false));57this.setTexturePack(this.settings.getStringProperty("texture-pack", ""));58this.setMOTD(this.settings.getStringProperty("motd", "A Minecraft Server"));59this.func_104055_i(this.settings.getBooleanProperty("force-gamemode", false));6061if (this.settings.getIntProperty("difficulty", 1) < 0) {62this.settings.setProperty("difficulty", Integer.valueOf(0));63} else if (this.settings.getIntProperty("difficulty", 1) > 3) {64this.settings.setProperty("difficulty", Integer.valueOf(3));65}6667this.canSpawnStructures = this.settings.getBooleanProperty("generate-structures", true);68int var2 = this.settings.getIntProperty("gamemode", EnumGameType.SURVIVAL.getID());69this.gameType = WorldSettings.getGameTypeById(var2);70this.getLogAgent().func_98233_a("Default game type: " + this.gameType);71InetAddress var3 = null;7273if (this.getServerHostname().length() > 0) {74var3 = InetAddress.getByName(this.getServerHostname());75}7677if (this.getServerPort() < 0) {78this.setServerPort(this.settings.getIntProperty("server-port", 25565));79}8081this.getLogAgent().func_98233_a("Generating keypair");82this.setKeyPair(CryptManager.generateKeyPair());83this.getLogAgent()84.func_98233_a("Starting Minecraft server on "85+ (this.getServerHostname().length() == 0 ? "*" : this.getServerHostname()) + ":"86+ this.getServerPort());8788try {89this.networkThread = new DedicatedServerListenThread(this, var3, this.getServerPort());90} catch (IOException var16) {91this.getLogAgent().func_98236_b("**** FAILED TO BIND TO PORT!");92this.getLogAgent().logWarningFormatted("The exception was: {0}", new Object[] { var16.toString() });93this.getLogAgent().func_98236_b("Perhaps a server is already running on that port?");94return false;95}9697if (!this.isServerInOnlineMode()) {98this.getLogAgent().func_98236_b("**** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!");99this.getLogAgent().func_98236_b("The server will make no attempt to authenticate usernames. Beware.");100this.getLogAgent().func_98236_b(101"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.");102this.getLogAgent()103.func_98236_b("To change this, set \"online-mode\" to \"true\" in the server.properties file.");104}105106this.setConfigurationManager(new DedicatedPlayerList(this));107long var4 = System.nanoTime();108109if (this.getFolderName() == null) {110this.setFolderName(this.settings.getStringProperty("level-name", "world"));111}112113String var6 = this.settings.getStringProperty("level-seed", "");114String var7 = this.settings.getStringProperty("level-type", "DEFAULT");115String var8 = this.settings.getStringProperty("generator-settings", "");116long var9 = (new Random()).nextLong();117118if (var6.length() > 0) {119try {120long var11 = Long.parseLong(var6);121122if (var11 != 0L) {123var9 = var11;124}125} catch (NumberFormatException var15) {126var9 = (long) var6.hashCode();127}128}129130WorldType var17 = WorldType.parseWorldType(var7);131132if (var17 == null) {133var17 = WorldType.DEFAULT;134}135136this.setBuildLimit(this.settings.getIntProperty("max-build-height", 256));137this.setBuildLimit((this.getBuildLimit() + 8) / 16 * 16);138this.setBuildLimit(MathHelper.clamp_int(this.getBuildLimit(), 64, 256));139this.settings.setProperty("max-build-height", Integer.valueOf(this.getBuildLimit()));140this.getLogAgent().func_98233_a("Preparing level \"" + this.getFolderName() + "\"");141this.loadAllWorlds(this.getFolderName(), this.getFolderName(), var9, var17, var8);142long var12 = System.nanoTime() - var4;143String var14 = String.format("%.3fs", new Object[] { Double.valueOf((double) var12 / 1.0E9D) });144this.getLogAgent().func_98233_a("Done (" + var14 + ")! For help, type \"help\" or \"?\"");145146if (this.settings.getBooleanProperty("enable-query", false)) {147this.getLogAgent().func_98233_a("Starting GS4 status listener");148this.theRConThreadQuery = new RConThreadQuery(this);149this.theRConThreadQuery.startThread();150}151152if (this.settings.getBooleanProperty("enable-rcon", false)) {153this.getLogAgent().func_98233_a("Starting remote control listener");154this.theRConThreadMain = new RConThreadMain(this);155this.theRConThreadMain.startThread();156}157158return true;159}160161public boolean canStructuresSpawn() {162return this.canSpawnStructures;163}164165public EnumGameType getGameType() {166return this.gameType;167}168169/**170* Defaults to "1" (Easy) for the dedicated server, defaults to "2" (Normal) on171* the client.172*/173public int getDifficulty() {174return this.settings.getIntProperty("difficulty", 1);175}176177/**178* Defaults to false.179*/180public boolean isHardcore() {181return this.settings.getBooleanProperty("hardcore", false);182}183184/**185* Called on exit from the main run() loop.186*/187protected void finalTick(CrashReport par1CrashReport) {188while (this.isServerRunning()) {189this.executePendingCommands();190191try {192Thread.sleep(10L);193} catch (InterruptedException var3) {194var3.printStackTrace();195}196}197}198199/**200* Adds the server info, including from theWorldServer, to the crash report.201*/202public CrashReport addServerInfoToCrashReport(CrashReport par1CrashReport) {203par1CrashReport = super.addServerInfoToCrashReport(par1CrashReport);204par1CrashReport.func_85056_g().addCrashSectionCallable("Is Modded", new CallableType(this));205par1CrashReport.func_85056_g().addCrashSectionCallable("Type", new CallableServerType(this));206return par1CrashReport;207}208209/**210* Directly calls System.exit(0), instantly killing the program.211*/212protected void systemExitNow() {213System.exit(0);214}215216public void updateTimeLightAndEntities() {217super.updateTimeLightAndEntities();218this.executePendingCommands();219}220221public boolean getAllowNether() {222return this.settings.getBooleanProperty("allow-nether", true);223}224225public boolean allowSpawnMonsters() {226return this.settings.getBooleanProperty("spawn-monsters", true);227}228229public void addServerStatsToSnooper(PlayerUsageSnooper par1PlayerUsageSnooper) {230par1PlayerUsageSnooper.addData("whitelist_enabled",231Boolean.valueOf(this.getDedicatedPlayerList().isWhiteListEnabled()));232par1PlayerUsageSnooper.addData("whitelist_count",233Integer.valueOf(this.getDedicatedPlayerList().getWhiteListedPlayers().size()));234super.addServerStatsToSnooper(par1PlayerUsageSnooper);235}236237/**238* Returns whether snooping is enabled or not.239*/240public boolean isSnooperEnabled() {241return this.settings.getBooleanProperty("snooper-enabled", true);242}243244public void addPendingCommand(String par1Str, ICommandSender par2ICommandSender) {245this.pendingCommandList.add(new ServerCommand(par1Str, par2ICommandSender));246}247248public void executePendingCommands() {249while (!this.pendingCommandList.isEmpty()) {250ServerCommand var1 = (ServerCommand) this.pendingCommandList.remove(0);251this.getCommandManager().executeCommand(var1.sender, var1.command);252}253}254255public boolean isDedicatedServer() {256return true;257}258259public DedicatedPlayerList getDedicatedPlayerList() {260return (DedicatedPlayerList) super.getConfigurationManager();261}262263public NetworkListenThread getNetworkThread() {264return this.networkThread;265}266267/**268* Gets an integer property. If it does not exist, set it to the specified269* value.270*/271public int getIntProperty(String par1Str, int par2) {272return this.settings.getIntProperty(par1Str, par2);273}274275/**276* Gets a string property. If it does not exist, set it to the specified value.277*/278public String getStringProperty(String par1Str, String par2Str) {279return this.settings.getStringProperty(par1Str, par2Str);280}281282/**283* Gets a boolean property. If it does not exist, set it to the specified value.284*/285public boolean getBooleanProperty(String par1Str, boolean par2) {286return this.settings.getBooleanProperty(par1Str, par2);287}288289/**290* Saves an Object with the given property name.291*/292public void setProperty(String par1Str, Object par2Obj) {293this.settings.setProperty(par1Str, par2Obj);294}295296/**297* Saves all of the server properties to the properties file.298*/299public void saveProperties() {300this.settings.saveProperties();301}302303/**304* Returns the filename where server properties are stored305*/306public String getSettingsFilename() {307File var1 = this.settings.getPropertiesFile();308return var1 != null ? var1.getAbsolutePath() : "No settings file";309}310311public void enableGui() {312ServerGUI.initGUI(this);313this.guiIsEnabled = true;314}315316public boolean getGuiEnabled() {317return this.guiIsEnabled;318}319320/**321* On dedicated does nothing. On integrated, sets commandsAllowedForAll,322* gameType and allows external connections.323*/324public String shareToLAN(EnumGameType par1EnumGameType, boolean par2) {325return "";326}327328/**329* Return whether command blocks are enabled.330*/331public boolean isCommandBlockEnabled() {332return this.settings.getBooleanProperty("enable-command-block", false);333}334335/**336* Return the spawn protection area's size.337*/338public int getSpawnProtectionSize() {339return this.settings.getIntProperty("spawn-protection", super.getSpawnProtectionSize());340}341342public boolean func_96290_a(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer) {343if (par1World.provider.dimensionId != 0) {344return false;345} else if (this.getDedicatedPlayerList().getOps().isEmpty()) {346return false;347} else if (this.getDedicatedPlayerList().areCommandsAllowed(par5EntityPlayer.username)) {348return false;349} else if (this.getSpawnProtectionSize() <= 0) {350return false;351} else {352ChunkCoordinates var6 = par1World.getSpawnPoint();353int var7 = MathHelper.abs_int(par2 - var6.posX);354int var8 = MathHelper.abs_int(par4 - var6.posZ);355int var9 = Math.max(var7, var8);356return var9 <= this.getSpawnProtectionSize();357}358}359360public ILogAgent getLogAgent() {361return this.field_98131_l;362}363364public ServerConfigurationManager getConfigurationManager() {365return this.getDedicatedPlayerList();366}367}368369370