AliasBots 12 Share Posted March 13, 2019 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 More sharing options...
yeeter 491 Share Posted March 13, 2019 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 More sharing options...
AliasBots 12 Author Share Posted March 13, 2019 Thanks, @yeeter01, This is actually the closest I got. It ends up throwing a null pointer exception. Link to comment Share on other sites More sharing options...
Nex 2530 Share Posted March 13, 2019 https://dreambot.org/javadocs/org/dreambot/api/methods/MethodContext.html Link to comment Share on other sites More sharing options...
AliasBots 12 Author Share Posted March 13, 2019 Thanks, @Nex, can you elaborate though? Link to comment Share on other sites More sharing options...
Nex 2530 Share Posted March 13, 2019 (edited) 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 March 13, 2019 by Nex yeeter and AliasBots 2 Link to comment Share on other sites More sharing options...
AliasBots 12 Author Share Posted March 13, 2019 Ohh, I see how you are passing "this" as a MethodContext, then calling that. I will give that a try! Link to comment Share on other sites More sharing options...
AliasBots 12 Author Share Posted March 19, 2019 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); } } Nex 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now