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
  • Multiple Classes (Cannot make a static reference to the non-static method)


    AliasBots

    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();		
    		}		
    	}
    }

     

    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

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • 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.