Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox
  • Multiple Classes (Cannot make a static reference to the non-static method)


    AliasBots
     Share

    Recommended Posts

    Hello,

    I think this is a really basic Java question (unless DreamBot is weird about classes), but I'm struggling here and could use a hand. How do I make this example work?

    I'll spare myself the embarrassment of posting everything I've tried. Instead, I'll post some base code with no attempts at using constructors or anything else.

    Main Class

    public class Main extends AbstractScript{
    
    	@Override
    	public void onStart() {
    		Logger.log("Log Successful");
    	}
    
    }

    Secondary Class

    public class Logger extends Main{
    	
    	public static void log (String info) {
    		log(getLocalPlayer().getName()+": "+info);
    	}
    	
    }

    Thanks guys!

    Link to comment
    Share on other sites

    Not sure if this is the optimal way to do this but in your logger class create a reference to Main.

     

    So it would look like this 

    public class Logger extends Main {
    	static Main mainObj = new Main();
    	
    	public static void log (String info) {
    		log(mainObj.getLocalPlayer().getName()+": "+info);
    	}
    }
    
    OR
    
    public class Logger extends Main {
    	public static void log (String info) {
    		Main mainObj = new Main();
    		log(mainObj.getLocalPlayer().getName()+": "+info);
    	}
    }

    This will allow you to reference stuff in main such as getLocalPlayer() and anything else you can think of. 

    Link to comment
    Share on other sites

    18 minutes ago, AliasBots said:

    Thanks, @Nex, can you elaborate though?

    in ur case probably easyest to do this:

    public class Main extends AbstractScript {
    	    
    	@Override
    	public int onLoop() {
    		log(Logger.get(this, "Name: "));
    		return 5000;
    	}
    	
    	public static class Logger {
    		
    		public static String get(MethodContext m, String info) {
    			return info + m.getLocalPlayer().getName();		
    		}		
    	}
    }

     

    Edited by Nex
    Link to comment
    Share on other sites

    Thanks @Nex , The following worked and should help me split my code up between classes.

     

    public class Manager extends AbstractScript{
    	
    	@Override
    	public void onStart() {
    		log("Logging In.");
    	}
    
    	@Override
    	public int onLoop() {
    		Logger.makeLog(this,"Test Log!");
    		onExit();
    		
    		return 600;
    	}
    	
    	@Override
    	public void onExit() {
    
    		stop();
    	}
    	
    	@Override
    	public void onPaint(Graphics graphics) {
    		
    	}
    	
    
    }
    public class Logger extends Manager{
    
    	public static void makeLog(Manager m, String info) {
    		log(m.getLocalPlayer().getName()+": "+info);
    		
    	}
    
    }

     

    Link to comment
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
     Share

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.