001    package net.minecraft.server.integrated;
002    
003    import cpw.mods.fml.common.Side;
004    import cpw.mods.fml.common.asm.SideOnly;
005    import cpw.mods.fml.common.network.FMLNetworkHandler;
006    
007    import java.io.IOException;
008    import java.net.InetAddress;
009    import net.minecraft.entity.player.EntityPlayerMP;
010    import net.minecraft.network.MemoryConnection;
011    import net.minecraft.network.NetworkListenThread;
012    import net.minecraft.network.packet.NetHandler;
013    import net.minecraft.server.MinecraftServer;
014    import net.minecraft.server.ServerListenThread;
015    import net.minecraft.util.HttpUtil;
016    
017    @SideOnly(Side.CLIENT)
018    public class IntegratedServerListenThread extends NetworkListenThread
019    {
020        private final MemoryConnection netMemoryConnection = new MemoryConnection((NetHandler)null);
021        private MemoryConnection theMemoryConnection;
022        private String field_71759_e;
023        private boolean field_71756_f = false;
024        private ServerListenThread myServerListenThread;
025    
026        public IntegratedServerListenThread(IntegratedServer par1IntegratedServer) throws IOException
027        {
028            super(par1IntegratedServer);
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 var1 = -1;
042    
043                try
044                {
045                    var1 = HttpUtil.func_76181_a();
046                }
047                catch (IOException var4)
048                {
049                    ;
050                }
051    
052                if (var1 <= 0)
053                {
054                    var1 = 25564;
055                }
056    
057                try
058                {
059                    this.myServerListenThread = new ServerListenThread(this, (InetAddress)null, var1);
060                    this.myServerListenThread.start();
061                }
062                catch (IOException var3)
063                {
064                    throw var3;
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                System.out.println("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 var1 = this.getIntergatedServerMinecraftServerInstance().getConfigurationManager().createPlayerForUser(this.field_71759_e);
092    
093                if (var1 != null)
094                {
095                    this.netMemoryConnection.pairWith(this.theMemoryConnection);
096                    this.field_71756_f = true;
097                    this.getIntergatedServerMinecraftServerInstance().getConfigurationManager().initializeConnectionToPlayer(this.netMemoryConnection, var1);
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 getIntergatedServerMinecraftServerInstance()
116        {
117            return (IntegratedServer)super.getServer();
118        }
119    
120        public boolean func_71752_f()
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.getIntergatedServerMinecraftServerInstance();
128        }
129    }