package net.minecraft.server.integrated;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.io.File;
import java.io.IOException;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ThreadLanServerPing;
import net.minecraft.crash.CrashReport;
import net.minecraft.network.NetworkListenThread;
import net.minecraft.profiler.PlayerUsageSnooper;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.CryptManager;
import net.minecraft.world.EnumGameType;
import net.minecraft.world.WorldManager;
import net.minecraft.world.WorldServer;
import net.minecraft.world.WorldServerMulti;
import net.minecraft.world.WorldSettings;
import net.minecraft.world.WorldType;
import net.minecraft.world.demo.DemoWorldServer;
import net.minecraft.world.storage.ISaveHandler;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;
@SideOnly(Side.CLIENT)
public class IntegratedServer extends MinecraftServer
{
/** The Minecraft instance. */
private final Minecraft mc;
private final WorldSettings theWorldSettings;
/** Instance of IntegratedServerListenThread. */
private IntegratedServerListenThread theServerListeningThread;
private boolean isGamePaused = false;
private boolean isPublic;
private ThreadLanServerPing lanServerPing;
public IntegratedServer(Minecraft par1Minecraft, String par2Str, String par3Str, WorldSettings par4WorldSettings)
{
super(new File(Minecraft.getMinecraftDir(), "saves"));
this.setServerOwner(par1Minecraft.session.username);
this.setFolderName(par2Str);
this.setWorldName(par3Str);
this.setDemo(par1Minecraft.isDemo());
this.canCreateBonusChest(par4WorldSettings.isBonusChestEnabled());
this.setBuildLimit(256);
this.setConfigurationManager(new IntegratedPlayerList(this));
this.mc = par1Minecraft;
this.theWorldSettings = par4WorldSettings;
try
{
this.theServerListeningThread = new IntegratedServerListenThread(this);
}
catch (IOException var6)
{
throw new Error();
}
}
protected void loadAllWorlds(String par1Str, String par2Str, long par3, WorldType par5WorldType, String par6Str)
{
this.convertMapIfNeeded(par1Str);
ISaveHandler var7 = this.getActiveAnvilConverter().getSaveLoader(par1Str, true);
WorldServer overWorld = (isDemo() ? new DemoWorldServer(this, var7, par2Str, 0, theProfiler) : new WorldServer(this, var7, par2Str, 0, theWorldSettings, theProfiler));
for (int dim : DimensionManager.getStaticDimensionIDs())
{
WorldServer world = (dim == 0 ? overWorld : new WorldServerMulti(this, var7, par2Str, dim, theWorldSettings, overWorld, theProfiler));
world.addWorldAccess(new WorldManager(this, world));
if (!this.isSinglePlayer())
{
world.getWorldInfo().setGameType(this.getGameType());
}
MinecraftForge.EVENT_BUS.post(new WorldEvent.Load(world));
}
this.getConfigurationManager().setPlayerManager(new WorldServer[]{ overWorld });
this.setDifficultyForAllWorlds(this.getDifficulty());
this.initialWorldChunkLoad();
}
/**
* Initialises the server and starts it.
*/
protected boolean startServer() throws IOException
{
logger.info("Starting integrated minecraft server version 1.4.7");
this.setOnlineMode(false);
this.setCanSpawnAnimals(true);
this.setCanSpawnNPCs(true);
this.setAllowPvp(true);
this.setAllowFlight(true);
logger.info("Generating keypair");
this.setKeyPair(CryptManager.createNewKeyPair());
this.loadAllWorlds(this.getFolderName(), this.getWorldName(), this.theWorldSettings.getSeed(), this.theWorldSettings.getTerrainType(), this.theWorldSettings.func_82749_j());
this.setMOTD(this.getServerOwner() + " - " + this.worldServers[0].getWorldInfo().getWorldName());
FMLCommonHandler.instance().handleServerStarting(this);
return true;
}
/**
* Main function called by run() every loop.
*/
public void tick()
{
boolean var1 = this.isGamePaused;
this.isGamePaused = this.theServerListeningThread.isGamePaused();
if (!var1 && this.isGamePaused)
{
logger.info("Saving and pausing game...");
this.getConfigurationManager().saveAllPlayerData();
this.saveAllWorlds(false);
}
if (!this.isGamePaused)
{
super.tick();
}
}
public boolean canStructuresSpawn()
{
return false;
}
public EnumGameType getGameType()
{
return this.theWorldSettings.getGameType();
}
/**
* Defaults to "1" (Easy) for the dedicated server, defaults to "2" (Normal) on the client.
*/
public int getDifficulty()
{
return this.mc.gameSettings.difficulty;
}
/**
* Defaults to false.
*/
public boolean isHardcore()
{
return this.theWorldSettings.getHardcoreEnabled();
}
protected File getDataDirectory()
{
return this.mc.mcDataDir;
}
public boolean isDedicatedServer()
{
return false;
}
/**
* Gets the IntergratedServerListenThread.
*/
public IntegratedServerListenThread getServerListeningThread()
{
return this.theServerListeningThread;
}
/**
* Called on exit from the main run() loop.
*/
protected void finalTick(CrashReport par1CrashReport)
{
this.mc.crashed(par1CrashReport);
}
/**
* Adds the server info, including from theWorldServer, to the crash report.
*/
public CrashReport addServerInfoToCrashReport(CrashReport par1CrashReport)
{
par1CrashReport = super.addServerInfoToCrashReport(par1CrashReport);
par1CrashReport.func_85056_g().addCrashSectionCallable("Type", new CallableType3(this));
par1CrashReport.func_85056_g().addCrashSectionCallable("Is Modded", new CallableIsModded(this));
return par1CrashReport;
}
public void addServerStatsToSnooper(PlayerUsageSnooper par1PlayerUsageSnooper)
{
super.addServerStatsToSnooper(par1PlayerUsageSnooper);
par1PlayerUsageSnooper.addData("snooper_partner", this.mc.getPlayerUsageSnooper().getUniqueID());
}
/**
* Returns whether snooping is enabled or not.
*/
public boolean isSnooperEnabled()
{
return Minecraft.getMinecraft().isSnooperEnabled();
}
/**
* On dedicated does nothing. On integrated, sets commandsAllowedForAll, gameType and allows external connections.
*/
public String shareToLAN(EnumGameType par1EnumGameType, boolean par2)
{
try
{
String var3 = this.theServerListeningThread.func_71755_c();
System.out.println("Started on " + var3);
this.isPublic = true;
this.lanServerPing = new ThreadLanServerPing(this.getMOTD(), var3);
this.lanServerPing.start();
this.getConfigurationManager().setGameType(par1EnumGameType);
this.getConfigurationManager().setCommandsAllowedForAll(par2);
return var3;
}
catch (IOException var4)
{
return null;
}
}
/**
* Saves all necessary data as preparation for stopping the server.
*/
public void stopServer()
{
super.stopServer();
if (this.lanServerPing != null)
{
this.lanServerPing.interrupt();
this.lanServerPing = null;
}
}
/**
* Sets the serverRunning variable to false, in order to get the server to shut down.
*/
public void initiateShutdown()
{
super.initiateShutdown();
if (this.lanServerPing != null)
{
this.lanServerPing.interrupt();
this.lanServerPing = null;
}
}
/**
* Returns true if this integrated server is open to LAN
*/
public boolean getPublic()
{
return this.isPublic;
}
/**
* Sets the game type for all worlds.
*/
public void setGameType(EnumGameType par1EnumGameType)
{
this.getConfigurationManager().setGameType(par1EnumGameType);
}
/**
* Return whether command blocks are enabled.
*/
public boolean isCommandBlockEnabled()
{
return true;
}
public NetworkListenThread getNetworkThread()
{
return this.getServerListeningThread();
}
}