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;
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        this.netMemoryConnection = new MemoryConnection(par1IntegratedServer.func_98033_al(), (NetHandler)null);
029    }
030
031    public void func_71754_a(MemoryConnection par1MemoryConnection, String par2Str)
032    {
033        this.theMemoryConnection = par1MemoryConnection;
034        this.field_71759_e = par2Str;
035    }
036
037    public String func_71755_c() throws IOException
038    {
039        if (this.myServerListenThread == null)
040        {
041            int i = -1;
042
043            try
044            {
045                i = HttpUtil.func_76181_a();
046            }
047            catch (IOException ioexception)
048            {
049                ;
050            }
051
052            if (i <= 0)
053            {
054                i = 25564;
055            }
056
057            try
058            {
059                this.myServerListenThread = new ServerListenThread(this, (InetAddress)null, i);
060                this.myServerListenThread.start();
061            }
062            catch (IOException ioexception1)
063            {
064                throw ioexception1;
065            }
066        }
067
068        return FMLNetworkHandler.computeLocalHost().getHostAddress() + ":" + this.myServerListenThread.getMyPort();
069    }
070
071    public void stopListening()
072    {
073        super.stopListening();
074
075        if (this.myServerListenThread != null)
076        {
077            this.getIntegratedServer().func_98033_al().func_98233_a("Stopping server connection");
078            this.myServerListenThread.func_71768_b();
079            this.myServerListenThread.interrupt();
080            this.myServerListenThread = null;
081        }
082    }
083
084    /**
085     * processes packets and pending connections
086     */
087    public void networkTick()
088    {
089        if (this.theMemoryConnection != null)
090        {
091            EntityPlayerMP entityplayermp = this.getIntegratedServer().getConfigurationManager().createPlayerForUser(this.field_71759_e);
092
093            if (entityplayermp != null)
094            {
095                this.netMemoryConnection.pairWith(this.theMemoryConnection);
096                this.field_71756_f = true;
097                this.getIntegratedServer().getConfigurationManager().initializeConnectionToPlayer(this.netMemoryConnection, entityplayermp);
098            }
099
100            this.theMemoryConnection = null;
101            this.field_71759_e = null;
102        }
103
104        if (this.myServerListenThread != null)
105        {
106            this.myServerListenThread.processPendingConnections();
107        }
108
109        super.networkTick();
110    }
111
112    /**
113     * Gets MinecraftServer instance.
114     */
115    public IntegratedServer getIntegratedServer()
116    {
117        return (IntegratedServer)super.getServer();
118    }
119
120    public boolean isGamePaused()
121    {
122        return this.field_71756_f && this.netMemoryConnection.getPairedConnection().isConnectionActive() && this.netMemoryConnection.getPairedConnection().isGamePaused();
123    }
124
125    public MinecraftServer getServer()
126    {
127        return this.getIntegratedServer();
128    }
129}