    001package net.minecraft.client.gui;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.net.URI;
    006import java.net.URISyntaxException;
    007import java.util.regex.Matcher;
    008import java.util.regex.Pattern;
    009import net.minecraft.client.Minecraft;
    010import net.minecraft.util.StringUtils;
    011
    012@SideOnly(Side.CLIENT)
    013public class ChatClickData
    014{
    015    public static final Pattern pattern = Pattern.compile("^(?:(https?)://)?([-\\w_\\.]{2,}\\.[a-z]{2,4})(/\\S*)?$");
    016    private final FontRenderer fontR;
    017    private final ChatLine line;
    018    private final int field_78312_d;
    019    private final int field_78313_e;
    020    private final String field_78310_f;
    021
    022    /** The URL which was clicked on. */
    023    private final String clickedUrl;
    024
    025    public ChatClickData(FontRenderer par1FontRenderer, ChatLine par2ChatLine, int par3, int par4)
    026    {
    027        this.fontR = par1FontRenderer;
    028        this.line = par2ChatLine;
    029        this.field_78312_d = par3;
    030        this.field_78313_e = par4;
    031        this.field_78310_f = par1FontRenderer.trimStringToWidth(par2ChatLine.getChatLineString(), par3);
    032        this.clickedUrl = this.findClickedUrl();
    033    }
    034
    035    /**
    036     * Gets the URL which was clicked on.
    037     */
    038    public String getClickedUrl()
    039    {
    040        return this.clickedUrl;
    041    }
    042
    043    /**
    044     * computes the URI from the clicked chat data object
    045     */
    046    public URI getURI()
    047    {
    048        String s = this.getClickedUrl();
    049
    050        if (s == null)
    051        {
    052            return null;
    053        }
    054        else
    055        {
    056            Matcher matcher = pattern.matcher(s);
    057
    058            if (matcher.matches())
    059            {
    060                try
    061                {
    062                    String s1 = matcher.group(0);
    063
    064                    if (matcher.group(1) == null)
    065                    {
    066                        s1 = "http://" + s1;
    067                    }
    068
    069                    return new URI(s1);
    070                }
    071                catch (URISyntaxException urisyntaxexception)
    072                {
    073                    Minecraft.getMinecraft().getLogAgent().logSevereException("Couldn\'t create URI from chat", urisyntaxexception);
    074                }
    075            }
    076
    077            return null;
    078        }
    079    }
    080
    081    private String findClickedUrl()
    082    {
    083        int i = this.field_78310_f.lastIndexOf(" ", this.field_78310_f.length()) + 1;
    084
    085        if (i < 0)
    086        {
    087            i = 0;
    088        }
    089
    090        int j = this.line.getChatLineString().indexOf(" ", i);
    091
    092        if (j < 0)
    093        {
    094            j = this.line.getChatLineString().length();
    095        }
    096
    097        return StringUtils.stripControlCodes(this.line.getChatLineString().substring(i, j));
    098    }
    099}