The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Help a complete moron at java understand how to run a thread and get a value back

EinEin CaliforniaRegistered User regular
edited June 2011 in Help / Advice Forum
I'm working on making a client mod for Minecraft. Basically it just changes where the client looks for skins for other players - by default, it simply looked at minecraft.net for skins. My changes make it look on MY host first for a file, and in the event of that file not existing, then default to minecraft.net.

Here's an example of how it works with skins for cloaks:
    public void updateCloak()
    {
    	String s = username;
    	s = s.replace(' ', '_');
        if(s.charAt(0) == 167){
            s = s.substring(2,username.length());
        } 
        testUrl = (new StringBuilder()).append("http://nawskins.s3.amazonaws.com/mcskins/").append(s.toLowerCase()).append("/cape.png").toString();
        if(!exists(testUrl))
        {
            playerCloakUrl = (new StringBuilder()).append("http://nawskins.s3.amazonaws.com/mcskins/").append(s.toLowerCase()).append("/cape.png").toString();
        } else
        {
            trieditc2 = 2;
            playerCloakUrl = (new StringBuilder()).append("http://s3.amazonaws.com/MinecraftCloaks/").append(username).append(".png").toString();
        }
        cloakUrl = playerCloakUrl;
    }

And the corresponding exists method:
    public static boolean exists(String s)
    {
        try
        {
            HttpURLConnection.setFollowRedirects(false);
            HttpURLConnection httpurlconnection = (HttpURLConnection)(new URL(s)).openConnection();
            httpurlconnection.setRequestMethod("HEAD");
            httpurlconnection.setConnectTimeout(3000);
            return httpurlconnection.getResponseCode() == 404;
        }
        catch(Exception exception)
        {
            return false;
        }
    }

Works fine, no problems - it feeds the right URLs to the minecraft client, and the correct skin loads.

However, I have a problem - the try/catch stuff that the exists method is doing is lagging the fuck out of the client. If you enter a town of players, your client turns into a screenshot for about 10 seconds while it does all this stuff for each player in the area around you.

So, it would seem to me, the natural solution to this problem would be threading, which I only understand in the abstract sense that 'it lets me do things simultaneously'. I basically want to have some way of doing the stuff that the exists method above does within a thread, and then somehow take that boolean value from the thread and feed it back to my updateCloak method.

I've been reading on threads, and I made a new class - CheckUrl - to try and implement Runnable, but Runnable doesn't return a value. I kind've know there's something called Future that lets me do that, but I have no idea how to do any of that and tutorials that I've skimmed are so far over my head it's ridiculous.

I am fucking retarded at java. It's a miracle that I've gotten as far as I have, so you need to explain things to me very slowly, as though you were a pop-up book.

Ein on

Posts

  • EinEin CaliforniaRegistered User regular
    edited June 2011
    So I have a CheckUrls class:


    package net.minecraft.src;
    import java.net.*;
    import java.util.*;
    
    class CheckUrls implements Runnable{
    	boolean urlExists;
    	String url;
    	CheckUrls(String url){
    		this.url = url;
    	}
    	
    	public void run(){
    		try{
    			 HttpURLConnection.setFollowRedirects(false);
    	         HttpURLConnection httpurlconnection = (HttpURLConnection)(new URL(url)).openConnection();
    	         httpurlconnection.setRequestMethod("HEAD");
    	         httpurlconnection.setConnectTimeout(3000);
    	         if(httpurlconnection.getResponseCode() == 404){
    	        	 urlExists = true;
    	         }
    		} catch (Exception e){
    			urlExists = false;
    		}
    	}
    }
    

    Basically my question now boils down to, how do I get the value of urlExists out of there and back into my updateCloak function in a different class?

    Ein on
  • oldsakoldsak Registered User regular
    edited June 2011
    My coding is super rusty, so my suggestions may be worthless.

    Why even use urlExists? Why not have run() return true or false?

    If you want access to urlExists, don't you have to make it public or something?

    edit: you could also just make a function that returns urlExists, but that seems redundant.

    oldsak on
  • EinEin CaliforniaRegistered User regular
    edited June 2011
    run() can't actually return anything, afaik.

    However, I may have just cracked this by using Callable instead of Runnable

    Ein on
  • oldsakoldsak Registered User regular
    edited June 2011
    Ein wrote: »
    run() can't actually return anything, afaik.

    However, I may have just cracked this by using Callable instead of Runnable

    if you make it a boolean instead of a void you can set it to return a boolean value

    ie:

    public boolean run()

    edit: nevermind i is dum

    oldsak on
Sign In or Register to comment.