Jump to content

[1.12] Need help render texture with DynamicTexture


lamp3345

Recommended Posts

I make the Custom Cape rendering with PNG image from URL. Current method seems bad performance (And its make game lag 2~ second after finished download texture).

 

Current Code :

 

    public static void setCapeURL(String url, boolean startup)
    {
        URL jurl = null;
        boolean noConnection = false;
        JsonUtil json = new JsonUtil();

        try
        {
            jurl = new URL(url);
        }
        catch (MalformedURLException e)
        {
            noConnection = true;
            e.printStackTrace();
            return;
        }

        if (CapeUtils.textureUploaded && !noConnection)
        {
            try
            {
                CapeUtils.CAPE_TEXTURE.put(GameProfileUtil.getUsername(), new DynamicTexture(ImageIO.read(jurl)));
                ExtendedConfig.CAPE_URL = Base64Utils.encode(url);
                ExtendedConfig.save();
                CapeUtils.textureUploaded = false;
            }
            catch (MalformedURLException e)
            {
                if (IndicatiaMod.MC.player != null)
                {
                    IndicatiaMod.MC.player.sendMessage(json.text("Missing protocol or wrong URL format").setStyle(json.red()));
                }
                e.printStackTrace();
                return;
            }

 

I've changed to use Thread, but I don't know whats wrong with it.

It throw message "No OpenGL context found in the current thread."

 

public class ThreadDownloadedCustomCape extends Thread
{
    @Override
    public void run()
    {
        URL capeUrl = null;

        try
        {
            capeUrl = new URL("http://i.imgur.com/vXgIBgM.png");
            BufferedImage image = ImageIO.read(capeUrl);
            File pngFile = new File(IndicatiaMod.CONFIG, "custom_cape.png");
            ImageIO.write(image, "png", pngFile);
            Thread.sleep(5000);
            new ThreadReadCustomCape().start();
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}

 

public class ThreadReadCustomCape extends Thread
{
    @Override
    public void run()
    {
        try
        {
            File pngFile = new File(IndicatiaMod.CONFIG, "custom_cape.png");

            if (pngFile.exists())
            {
                BufferedImage image = ImageIO.read(pngFile);
                CapeUtils.CAPE_TEXTURE.put(GameProfileUtil.getUsername(), new DynamicTexture(image));
            }
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.