    001package net.minecraft.client.multiplayer;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.io.IOException;
    006import java.net.DatagramPacket;
    007import java.net.DatagramSocket;
    008import java.net.InetAddress;
    009import java.util.logging.Level;
    010import java.util.logging.Logger;
    011
    012@SideOnly(Side.CLIENT)
    013public class ThreadLanServerPing extends Thread
    014{
    015    private static Logger logger = Logger.getLogger("Minecraft");
    016    private final String motd;
    017
    018    /** The socket we're using to send packets on. */
    019    private final DatagramSocket socket;
    020    private boolean isStopping = true;
    021    private final String address;
    022
    023    public ThreadLanServerPing(String par1Str, String par2Str) throws IOException
    024    {
    025        super("LanServerPinger");
    026        this.motd = par1Str;
    027        this.address = par2Str;
    028        this.setDaemon(true);
    029        this.socket = new DatagramSocket();
    030    }
    031
    032    public void run()
    033    {
    034        String var1 = getPingResponse(this.motd, this.address);
    035        byte[] var2 = var1.getBytes();
    036
    037        while (!this.isInterrupted() && this.isStopping)
    038        {
    039            try
    040            {
    041                InetAddress var3 = InetAddress.getByName("224.0.2.60");
    042                DatagramPacket var4 = new DatagramPacket(var2, var2.length, var3, 4445);
    043                this.socket.send(var4);
    044            }
    045            catch (IOException var6)
    046            {
    047                logger.log(Level.WARNING, "LanServerPinger: " + var6.getMessage());
    048                break;
    049            }
    050
    051            try
    052            {
    053                sleep(1500L);
    054            }
    055            catch (InterruptedException var5)
    056            {
    057                ;
    058            }
    059        }
    060    }
    061
    062    public void interrupt()
    063    {
    064        super.interrupt();
    065        this.isStopping = false;
    066    }
    067
    068    public static String getPingResponse(String par0Str, String par1Str)
    069    {
    070        return "[MOTD]" + par0Str + "[/MOTD][AD]" + par1Str + "[/AD]";
    071    }
    072
    073    public static String getMotdFromPingResponse(String par0Str)
    074    {
    075        int var1 = par0Str.indexOf("[MOTD]");
    076
    077        if (var1 < 0)
    078        {
    079            return "missing no";
    080        }
    081        else
    082        {
    083            int var2 = par0Str.indexOf("[/MOTD]", var1 + "[MOTD]".length());
    084            return var2 < var1 ? "missing no" : par0Str.substring(var1 + "[MOTD]".length(), var2);
    085        }
    086    }
    087
    088    public static String getAdFromPingResponse(String par0Str)
    089    {
    090        int var1 = par0Str.indexOf("[/MOTD]");
    091
    092        if (var1 < 0)
    093        {
    094            return null;
    095        }
    096        else
    097        {
    098            int var2 = par0Str.indexOf("[/MOTD]", var1 + "[/MOTD]".length());
    099
    100            if (var2 >= 0)
    101            {
    102                return null;
    103            }
    104            else
    105            {
    106                int var3 = par0Str.indexOf("[AD]", var1 + "[/MOTD]".length());
    107
    108                if (var3 < 0)
    109                {
    110                    return null;
    111                }
    112                else
    113                {
    114                    int var4 = par0Str.indexOf("[/AD]", var3 + "[AD]".length());
    115                    return var4 < var3 ? null : par0Str.substring(var3 + "[AD]".length(), var4);
    116                }
    117            }
    118        }
    119    }
    120}