001    package net.minecraft.src;
002    
003    import cpw.mods.fml.common.Side;
004    import cpw.mods.fml.common.asm.SideOnly;
005    import java.io.IOException;
006    import java.net.InetAddress;
007    import java.net.NetworkInterface;
008    import java.util.Collections;
009    
010    import com.google.common.collect.Iterators;
011    import com.google.common.collect.Lists;
012    
013    import net.minecraft.server.MinecraftServer;
014    
015    @SideOnly(Side.CLIENT)
016    public class IntegratedServerListenThread extends NetworkListenThread
017    {
018        private final MemoryConnection field_71760_c = new MemoryConnection((NetHandler)null);
019        private MemoryConnection field_71758_d;
020        private String field_71759_e;
021        private boolean field_71756_f = false;
022        private ServerListenThread myServerListenThread;
023    
024        public IntegratedServerListenThread(IntegratedServer par1IntegratedServer) throws IOException
025        {
026            super(par1IntegratedServer);
027        }
028    
029        public void func_71754_a(MemoryConnection par1MemoryConnection, String par2Str)
030        {
031            this.field_71758_d = par1MemoryConnection;
032            this.field_71759_e = par2Str;
033        }
034    
035        public String func_71755_c() throws IOException
036        {
037            if (this.myServerListenThread == null)
038            {
039                int var1 = -1;
040    
041                try
042                {
043                    var1 = HttpUtil.func_76181_a();
044                }
045                catch (IOException var4)
046                {
047                    ;
048                }
049    
050                if (var1 <= 0)
051                {
052                    var1 = 25564;
053                }
054    
055                try
056                {
057                    InetAddress add = null;
058                    NetworkInterface notLocal = null;
059                    for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces()))
060                    {
061                        if (!ni.isLoopback() && ni.isUp())
062                        {
063                            notLocal = ni;
064                            break;
065                        }
066                    }
067                    if (notLocal != null)
068                    {
069                        for (InetAddress inadd : Collections.list(notLocal.getInetAddresses()))
070                        {
071                            if (inadd.getAddress().length == 4)
072                            {
073                                add = inadd;
074                                break;
075                            }
076                        }
077                    }
078                    if (add == null)
079                    {
080                        add = InetAddress.getLocalHost();
081                    }
082                    this.myServerListenThread = new ServerListenThread(this, add, var1);
083                    this.myServerListenThread.start();
084                }
085                catch (IOException var3)
086                {
087                    throw var3;
088                }
089            }
090    
091            return this.myServerListenThread.getInetAddress().getHostAddress() + ":" + this.myServerListenThread.func_71765_d();
092        }
093    
094        public void stopListening()
095        {
096            super.stopListening();
097    
098            if (this.myServerListenThread != null)
099            {
100                System.out.println("Stopping server connection");
101                this.myServerListenThread.func_71768_b();
102                this.myServerListenThread.interrupt();
103                this.myServerListenThread = null;
104            }
105        }
106    
107        /**
108         * processes packets and pending connections
109         */
110        public void networkTick()
111        {
112            if (this.field_71758_d != null)
113            {
114                EntityPlayerMP var1 = this.func_71753_e().getConfigurationManager().createPlayerForUser(this.field_71759_e);
115    
116                if (var1 != null)
117                {
118                    this.field_71760_c.pairWith(this.field_71758_d);
119                    this.field_71756_f = true;
120                    this.func_71753_e().getConfigurationManager().initializeConnectionToPlayer(this.field_71760_c, var1);
121                }
122    
123                this.field_71758_d = null;
124                this.field_71759_e = null;
125            }
126    
127            if (this.myServerListenThread != null)
128            {
129                this.myServerListenThread.processPendingConnections();
130            }
131    
132            super.networkTick();
133        }
134    
135        public IntegratedServer func_71753_e()
136        {
137            return (IntegratedServer)super.getServer();
138        }
139    
140        public boolean func_71752_f()
141        {
142            return this.field_71756_f && this.field_71760_c.getPairedConnection().isConnectionActive() && this.field_71760_c.getPairedConnection().isGamePaused();
143        }
144    
145        public MinecraftServer getServer()
146        {
147            return this.func_71753_e();
148        }
149    }