    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 net.minecraft.client.Minecraft;
    010
    011@SideOnly(Side.CLIENT)
    012public class ThreadLanServerPing extends Thread
    013{
    014    private final String motd;
    015
    016    /** The socket we're using to send packets on. */
    017    private final DatagramSocket socket;
    018    private boolean isStopping = true;
    019    private final String address;
    020
    021    public ThreadLanServerPing(String par1Str, String par2Str) throws IOException
    022    {
    023        super("LanServerPinger");
    024        this.motd = par1Str;
    025        this.address = par2Str;
    026        this.setDaemon(true);
    027        this.socket = new DatagramSocket();
    028    }
    029
    030    public void run()
    031    {
    032        String s = getPingResponse(this.motd, this.address);
    033        byte[] abyte = s.getBytes();
    034
    035        while (!this.isInterrupted() && this.isStopping)
    036        {
    037            try
    038            {
    039                InetAddress inetaddress = InetAddress.getByName("224.0.2.60");
    040                DatagramPacket datagrampacket = new DatagramPacket(abyte, abyte.length, inetaddress, 4445);
    041                this.socket.send(datagrampacket);
    042            }
    043            catch (IOException ioexception)
    044            {
    045                Minecraft.getMinecraft().getLogAgent().logWarning("LanServerPinger: " + ioexception.getMessage());
    046                break;
    047            }
    048
    049            try
    050            {
    051                sleep(1500L);
    052            }
    053            catch (InterruptedException interruptedexception)
    054            {
    055                ;
    056            }
    057        }
    058    }
    059
    060    public void interrupt()
    061    {
    062        super.interrupt();
    063        this.isStopping = false;
    064    }
    065
    066    public static String getPingResponse(String par0Str, String par1Str)
    067    {
    068        return "[MOTD]" + par0Str + "[/MOTD][AD]" + par1Str + "[/AD]";
    069    }
    070
    071    public static String getMotdFromPingResponse(String par0Str)
    072    {
    073        int i = par0Str.indexOf("[MOTD]");
    074
    075        if (i < 0)
    076        {
    077            return "missing no";
    078        }
    079        else
    080        {
    081            int j = par0Str.indexOf("[/MOTD]", i + "[MOTD]".length());
    082            return j < i ? "missing no" : par0Str.substring(i + "[MOTD]".length(), j);
    083        }
    084    }
    085
    086    public static String getAdFromPingResponse(String par0Str)
    087    {
    088        int i = par0Str.indexOf("[/MOTD]");
    089
    090        if (i < 0)
    091        {
    092            return null;
    093        }
    094        else
    095        {
    096            int j = par0Str.indexOf("[/MOTD]", i + "[/MOTD]".length());
    097
    098            if (j >= 0)
    099            {
    100                return null;
    101            }
    102            else
    103            {
    104                int k = par0Str.indexOf("[AD]", i + "[/MOTD]".length());
    105
    106                if (k < 0)
    107                {
    108                    return null;
    109                }
    110                else
    111                {
    112                    int l = par0Str.indexOf("[/AD]", k + "[AD]".length());
    113                    return l < k ? null : par0Str.substring(k + "[AD]".length(), l);
    114                }
    115            }
    116        }
    117    }
    118}