Nym Posted May 26, 2017 Share Posted May 26, 2017 Could anyone share some improvements I can make to this script? public class Main extends Script { GUI gui = new GUI(); private NPC monster; private long startTime; public enum State { splash, splashAlch } @Override public int onLoop() throws InterruptedException { if (Settings.started) { if (!Settings.monsterName.isEmpty() && Settings.monsterName != null) { monster = npcs.closest(Settings.monsterName); } else { log("Enter a real monster name."); stop(); } if (!magic.canCast(Settings.spellToCast)) { log("Cant cast spell"); stop(); } switch (getState()) { case splash: if (monster != null && monster.isVisible()) { if (magic.castSpellOnEntity(Settings.spellToCast, monster)) { new ConditionalSleep(3000) { @Override public boolean condition() throws InterruptedException { return myPlayer().isAnimating(); } }.sleep(); new ConditionalSleep(3000) { @Override public boolean condition() throws InterruptedException { return !myPlayer().isAnimating(); } }.sleep(); } } else { camera.toEntity(monster); } break; case splashAlch: if (monster != null && monster.isVisible()) { if (magic.castSpellOnEntity(Settings.spellToCast, monster)) { sleep(random(150, 400)); if (magic.canCast(Spells.NormalSpells.HIGH_LEVEL_ALCHEMY)) { if (magic.castSpell(Spells.NormalSpells.HIGH_LEVEL_ALCHEMY)) { new ConditionalSleep(2000) { @Override public boolean condition() throws InterruptedException { return tabs.getOpen().equals(Tab.INVENTORY); } }.sleep(); sleep(random(50, 150)); if (mouse.click(inventory.getMouseDestination(inventory.getSlot(Settings.alchName)), false)) { new ConditionalSleep(3000) { @Override public boolean condition() throws InterruptedException { return tabs.getOpen().equals(Tab.MAGIC); } }.sleep(); sleep(random(150, 400)); } } } else { log("Can't cast High Alchemy"); stop(); } } } break; } } return 120; } public State getState() { return ((skills.getDynamic(Skill.MAGIC) >= 55) && Settings.alch) ? State.splashAlch : State.splash; } @Override public void onStart() throws InterruptedException { log("Nimmogel's 1-99 Magic has started :D"); gui.createGui(); experienceTracker.start(Skill.MAGIC); Methods methods = new Methods(this); Settings.spellToCast = methods.spellToCastFinder(); startTime = System.currentTimeMillis(); } public void onMessage(Message message) throws InterruptedException { String text = message.getMessage().toLowerCase(); if (text.contains("advanced a")) { Methods methods = new Methods(this); Settings.spellToCast = methods.spellToCastFinder(); } } @Override public void onExit() throws InterruptedException { log("Bye :D"); if (gui !=null) { gui.setVisible(false); gui.dispose(); } } @Override public void onPaint(Graphics2D g) { long runTime = System.currentTimeMillis() - startTime; long tillNextLevel = experienceTracker.getTimeToLevel(Skill.MAGIC); long second = runTime / 1000L % 60L; long minute = runTime / 60000L % 60L; long hour = runTime / 3600000L % 24L; long secondX = tillNextLevel / 1000L % 60L; long minuteX = tillNextLevel / 60000L % 60L; long hourX = tillNextLevel / 3600000L % 24L; g.setColor(Color.WHITE); g.drawString("Time running: " + String.format("%d:%02d:%02d", hour, minute, second), 300, 285); g.drawString("EXP: " + experienceTracker.getGainedXP(Skill.MAGIC) + " (" + experienceTracker.getGainedXPPerHour(Skill.MAGIC) + "/hr)", 300, 300); g.drawString("Level: " + skills.getDynamic(Skill.MAGIC) + "(" + String.format("%02d:%02d:%02d", hourX, minuteX, secondX) + ")", 300, 315); } } Quote Link to comment Share on other sites More sharing options...
dmmslaver Posted May 27, 2017 Share Posted May 27, 2017 remove all sleeps Quote Link to comment Share on other sites More sharing options...
Auron Posted May 27, 2017 Share Posted May 27, 2017 I can't really comment on the functionality of the script because it's hard to tell from just a brief look, if it works it works, good job pal. But from a coding point of view there can be a much better way of laying things out. In no particular order of things I picked up on: 1) All the settings checking at the beginning of the onLoop() are good, but only need to be checked once. The way you have it now, it will check these setting every loop. In the GUI, make the start button call a start method, which then checks all these things once, if everything is okay, then set Settings.started to true. 2) Conditional sleep can be separated into a method for neatness sake Spoiler public static void condSleep(int maximumMs, int deviationMs, BooleanSupplier condition) { new ConditionalSleep(maximumMs, deviationMs) { @Override public boolean condition() throws InterruptedException { return condition.getAsBoolean(); } }.sleep(); } 3) You have two states, imo it's simply pointless in creating an enum with a getState() function. And it's almost pointless having a getState function if it's a one liner. Not to mention I don't really understand your getState() function, wouldn't this always return Alching? Just do if(*getState code*) { alch(); } else { splash(); } 4) (java conventions) sound like a dick saying this, but for convention sake you should create getters in the Settings class rather than accessing variables directly. It's a good habit to get into. 5) That GUI gui = new GUI(); at the top. I'm sure you can't even do that outside a method. Put it in the constructor of Main. That said, does this code compile? Anyway, it's a great start dude, best of luck in the future. Quote Link to comment Share on other sites More sharing options...
Guest Posted May 27, 2017 Share Posted May 27, 2017 1 hour ago, Auron said: I can't really comment on the functionality of the script because it's hard to tell from just a brief look, if it works it works, good job pal. But from a coding point of view there can be a much better way of laying things out. In no particular order of things I picked up on: 1) All the settings checking at the beginning of the onLoop() are good, but only need to be checked once. The way you have it now, it will check these setting every loop. In the GUI, make the start button call a start method, which then checks all these things once, if everything is okay, then set Settings.started to true. 2) Conditional sleep can be separated into a method for neatness sake Reveal hidden contents public static void condSleep(int maximumMs, int deviationMs, BooleanSupplier condition) { new ConditionalSleep(maximumMs, deviationMs) { @Override public boolean condition() throws InterruptedException { return condition.getAsBoolean(); } }.sleep(); } 3) You have two states, imo it's simply pointless in creating an enum with a getState() function. And it's almost pointless having a getState function if it's a one liner. Not to mention I don't really understand your getState() function, wouldn't this always return Alching? Just do if(*getState code*) { alch(); } else { splash(); } 4) (java conventions) sound like a dick saying this, but for convention sake you should create getters in the Settings class rather than accessing variables directly. It's a good habit to get into. 5) That GUI gui = new GUI(); at the top. I'm sure you can't even do that outside a method. Put it in the constructor of Main. That said, does this code compile? Anyway, it's a great start dude, best of luck in the future. Can you criticize my script after? Quote Link to comment Share on other sites More sharing options...
Auron Posted May 27, 2017 Share Posted May 27, 2017 3 minutes ago, Noidlox said: Can you criticize my script after? Eh why the hell not, I find a sick pleasure in doing so. Pm me Quote Link to comment Share on other sites More sharing options...
Guest Posted May 27, 2017 Share Posted May 27, 2017 (edited) 1 minute ago, Auron said: Eh why the hell not, I find a sick pleasure in doing so. Pm me Once it's ready I will! Edit: Will send you a different one for now... Edited May 27, 2017 by Noidlox Quote Link to comment Share on other sites More sharing options...
Nym Posted May 28, 2017 Author Share Posted May 28, 2017 7 hours ago, Auron said: I can't really comment on the functionality of the script because it's hard to tell from just a brief look, if it works it works, good job pal. But from a coding point of view there can be a much better way of laying things out. In no particular order of things I picked up on: 1) All the settings checking at the beginning of the onLoop() are good, but only need to be checked once. The way you have it now, it will check these setting every loop. In the GUI, make the start button call a start method, which then checks all these things once, if everything is okay, then set Settings.started to true. 2) Conditional sleep can be separated into a method for neatness sake Hide contents public static void condSleep(int maximumMs, int deviationMs, BooleanSupplier condition) { new ConditionalSleep(maximumMs, deviationMs) { @Override public boolean condition() throws InterruptedException { return condition.getAsBoolean(); } }.sleep(); } 3) You have two states, imo it's simply pointless in creating an enum with a getState() function. And it's almost pointless having a getState function if it's a one liner. Not to mention I don't really understand your getState() function, wouldn't this always return Alching? Just do if(*getState code*) { alch(); } else { splash(); } 4) (java conventions) sound like a dick saying this, but for convention sake you should create getters in the Settings class rather than accessing variables directly. It's a good habit to get into. 5) That GUI gui = new GUI(); at the top. I'm sure you can't even do that outside a method. Put it in the constructor of Main. That said, does this code compile? Anyway, it's a great start dude, best of luck in the future. Thanks a lot, very helpful! Quote Link to comment Share on other sites More sharing options...