Deep Slayer 63 Posted January 9, 2024 Here's a pretty simple cooking bot I made. Maybe it can help someone just starting out to see how script runs Don't use it extensively, I'm 99% sure you'll get banned if you use this in game as there's no antiban or randomness added to it via the script itself.
Deep Slayer 63 Author Posted January 9, 2024 (edited) import org.dreambot.api.methods.Calculations; import org.dreambot.api.methods.container.impl.Inventory; import org.dreambot.api.methods.container.impl.bank.Bank; import org.dreambot.api.methods.interactive.GameObjects; import org.dreambot.api.methods.interactive.Players; import org.dreambot.api.methods.map.Area; import org.dreambot.api.methods.walking.impl.Walking; import org.dreambot.api.methods.widget.Widgets; import org.dreambot.api.script.AbstractScript; import org.dreambot.api.script.Category; import org.dreambot.api.script.ScriptManifest; import org.dreambot.api.utilities.Sleep; import org.dreambot.api.wrappers.interactive.GameObject; import org.dreambot.api.wrappers.interactive.Player; @ScriptManifest(author = "Anyone", name = "Simple Raw chicken cooker", version = 1.0, description = "cooks raw chicken", category = Category.COOKING ) public class CookingBot extends AbstractScript { public final String rawFood = "Raw chicken"; Player myPlayer = Players.getLocal(); Area cookingArea = new Area(3236, 3411, 3240, 3409); private State currentState; private enum State { COOK, WALK_TO_COOKING_RANGE, BANK_FOOD, DROP_BURNT, WITHDRAW_FOOD } @Override public void onStart() { currentState = State.WITHDRAW_FOOD; } @Override public int onLoop() { switch (currentState) { case WITHDRAW_FOOD: log("WITHDRAW_FOOD:"); withdrawShrimp(); if(Inventory.contains(rawFood)){ currentState = State.WALK_TO_COOKING_RANGE; } break; case WALK_TO_COOKING_RANGE: log("WALK_TO_COOKING_RANGE:"); walkToCookingRange(); if(cookingArea.contains(myPlayer)){ currentState = State.COOK; } break; case COOK: log("COOK:"); cook(); if(!Inventory.contains(rawFood)){ currentState = State.DROP_BURNT; } break; case DROP_BURNT: log("DROP_BURNT:"); dropBurnt(); if(!Inventory.contains("burnt ")){ currentState = State.BANK_FOOD; } break; case BANK_FOOD: log("BANK_FOOD:"); bankFood(); if(Inventory.isEmpty()){ currentState = State.WITHDRAW_FOOD; } break; } return 600; } private void bankFood() { if (!Bank.isOpen()) { Bank.open(); } if (Bank.isOpen()) { Bank.depositAllItems(); Bank.close(); } } private void dropBurnt() { if(Inventory.contains("Burnt chicken")){ log("Inventory contains burnt..."); Inventory.dropAll("Burnt chicken"); } } private void cook() { if (Inventory.contains("Raw chicken")) { GameObject range = GameObjects.closest(gameObject -> gameObject != null && gameObject.getName().equals("Range")); if (range != null && range.interact("Cook")) { Sleep.sleepUntil(() -> myPlayer.isAnimating(), Calculations.random(2000, 3000)); if(Inventory.contains("Cooked chicken")){ if (Widgets.getWidgetChild(270, 15) != null) { //if you have cooked chicken in inventory and go to cook, widget changes Widgets.getWidgetChild(270, 15).interact(); } } else{ if (Widgets.getWidgetChild(270, 14) != null) { //widget without cooked chicken in inventory Widgets.getWidgetChild(270, 14).interact(); } } sleep(2000,3000); Sleep.sleepUntil(()->!myPlayer.isAnimating(), Calculations.random(60000, 70000)); } } } private void walkToCookingRange () { if (!cookingArea.contains(myPlayer)) { Walking.walk(cookingArea); sleep(500,5000); } } private void withdrawShrimp () { if (!Bank.isOpen()) { Bank.open(); } if (Bank.isOpen()) { if(!Inventory.isEmpty()){ Bank.depositAllItems(); sleep(500,1000); } Bank.withdrawAll(rawFood); Bank.close(); } } @Override public void onExit () { } } Edited January 9, 2024 by Deep Slayer
apnasus 59 Posted January 9, 2024 I think your code would be more readable if you used return conditions in your methods. For example, instead of, 6 hours ago, Deep Slayer said: private void cook() { if (Inventory.contains("Raw chicken")) { GameObject range = GameObjects.closest(gameObject -> gameObject != null && gameObject.getName().equals("Range")); you could have, private void cook() { if (!Inventory.contains("Raw chicken")) { return; } GameObject range = GameObjects.closest(gameObject -> gameObject != null && gameObject.getName().equals("Range")); which would reduce the levels of indentation in your methods and make them easier to follow. PorkyPie and Deep Slayer 2
PorkyPie 4 Posted January 9, 2024 40 minutes ago, apnasus said: I think your code would be more readable if you used return conditions in your methods. For example, instead of, you could have, private void cook() { if (!Inventory.contains("Raw chicken")) { return; } GameObject range = GameObjects.closest(gameObject -> gameObject != null && gameObject.getName().equals("Range")); which would reduce the levels of indentation in your methods and make them easier to follow. (+ 1) I agree Not changing it to this would get flagged at code review stage in an enterprise environment. Furthermore you can increase the code quality by removing the magic numbers such as: getWidgetChild(270, 15) and sleep(500,1000) Imagine how hard it would be to maintain a large script if those widget numbers changed. At a minimum you could wrap the getWdigetChild in a method which describes the widget you are getting and reuse that. For the sleep put the 500,1000 into variables such as minSleep and maxSleep. Examples: int maxSleep = 1000; int minSleep = 500; private WidgetChild getCookWidget() { Widgets.getWidgetChild(270, 15); } private void exampleMethodWithSleep() { //code Sleep(minSleep, maxSleep); //code } Also if you find the need to add comments, generally in 90% of cases, your code isn't readable and therefore can be improved so you don't need comments.
Deep Slayer 63 Author Posted January 9, 2024 thanks for the feedback, will consider the early return approach next time for readability. and use of naming variables instead of directly throwing numbers in there. hope u enjoyed the script apnasus 1
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