    001package net.minecraft.server.integrated;
    002
    003import cpw.mods.fml.common.network.FMLNetworkHandler;
    004import cpw.mods.fml.relauncher.Side;
    005import cpw.mods.fml.relauncher.SideOnly;
    006import java.io.IOException;
    007import java.net.InetAddress;
    008import net.minecraft.entity.player.EntityPlayerMP;
    009import net.minecraft.network.MemoryConnection;
    010import net.minecraft.network.NetworkListenThread;
    011import net.minecraft.network.packet.NetHandler;
    012import net.minecraft.server.MinecraftServer;
    013import net.minecraft.server.ServerListenThread;
    014import net.minecraft.util.HttpUtil;
    015
    016@SideOnly(Side.CLIENT)
    017public class IntegratedServerListenThread extends NetworkListenThread
    018{
    019    private final MemoryConnection netMemoryConnection = new MemoryConnection((NetHandler)null);
    020    private MemoryConnection theMemoryConnection;
    021    private String field_71759_e;
    022    private boolean field_71756_f = false;
    023    private ServerListenThread myServerListenThread;
    024
    025    public IntegratedServerListenThread(IntegratedServer par1IntegratedServer) throws IOException
    026    {
    027        super(par1IntegratedServer);
    028    }
    029
    030    public void func_71754_a(MemoryConnection par1MemoryConnection, String par2Str)
    031    {
    032        this.theMemoryConnection = par1MemoryConnection;
    033        this.field_71759_e = par2Str;
    034    }
    035
    036    public String func_71755_c() throws IOException
    037    {
    038        if (this.myServerListenThread == null)
    039        {
    040            int var1 = -1;
    041
    042            try
    043            {
    044                var1 = HttpUtil.func_76181_a();
    045            }
    046            catch (IOException var4)
    047            {
    048                ;
    049            }
    050
    051            if (var1 <= 0)
    052            {
    053                var1 = 25564;
    054            }
    055
    056            try
    057            {
    058                this.myServerListenThread = new ServerListenThread(this, (InetAddress)null, var1);
    059                this.myServerListenThread.start();
    060            }
    061            catch (IOException var3)
    062            {
    063                throw var3;
    064            }
    065        }
    066
    067        return FMLNetworkHandler.computeLocalHost().getHostAddress() + ":" + this.myServerListenThread.getMyPort();
    068    }
    069
    070    public void stopListening()
    071    {
    072        super.stopListening();
    073
    074        if (this.myServerListenThread != null)
    075        {
    076            System.out.println("Stopping server connection");
    077            this.myServerListenThread.func_71768_b();
    078            this.myServerListenThread.interrupt();
    079            this.myServerListenThread = null;
    080        }
    081    }
    082
    083    /**
    084     * processes packets and pending connections
    085     */
    086    public void networkTick()
    087    {
    088        if (this.theMemoryConnection != null)
    089        {
    090            EntityPlayerMP var1 = this.getIntergatedServerMinecraftServerInstance().getConfigurationManager().createPlayerForUser(this.field_71759_e);
    091
    092            if (var1 != null)
    093            {
    094                this.netMemoryConnection.pairWith(this.theMemoryConnection);
    095                this.field_71756_f = true;
    096                this.getIntergatedServerMinecraftServerInstance().getConfigurationManager().initializeConnectionToPlayer(this.netMemoryConnection, var1);
    097            }
    098
    099            this.theMemoryConnection = null;
    100            this.field_71759_e = null;
    101        }
    102
    103        if (this.myServerListenThread != null)
    104        {
    105            this.myServerListenThread.processPendingConnections();
    106        }
    107
    108        super.networkTick();
    109    }
    110
    111    /**
    112     * Gets MinecraftServer instance.
    113     */
    114    public IntegratedServer getIntergatedServerMinecraftServerInstance()
    115    {
    116        return (IntegratedServer)super.getServer();
    117    }
    118
    119    public boolean isGamePaused()
    120    {
    121        return this.field_71756_f && this.netMemoryConnection.getPairedConnection().isConnectionActive() && this.netMemoryConnection.getPairedConnection().isGamePaused();
    122    }
    123
    124    public MinecraftServer getServer()
    125    {
    126        return this.getIntergatedServerMinecraftServerInstance();
    127    }
    128}