diff --git a/CShopper/src/io/reisub/dreambot/cshopper/CShopper.java b/CShopper/src/io/reisub/dreambot/cshopper/CShopper.java index 2d73bd4..442bdae 100644 --- a/CShopper/src/io/reisub/dreambot/cshopper/CShopper.java +++ b/CShopper/src/io/reisub/dreambot/cshopper/CShopper.java @@ -1,24 +1,59 @@ package io.reisub.dreambot.cshopper; +import io.reisub.dreambot.cshopper.tasks.Buy; +import io.reisub.dreambot.cshopper.tasks.Hop; +import io.reisub.dreambot.cshopper.tasks.Open; +import io.reisub.dreambot.cshopper.tasks.Stop; +import io.reisub.dreambot.util.CTaskScript; import io.reisub.dreambot.util.Constants; +import org.dreambot.api.methods.container.impl.Inventory; import org.dreambot.api.script.Category; import org.dreambot.api.script.ScriptManifest; import org.dreambot.api.script.impl.TaskScript; +import org.dreambot.api.wrappers.items.Item; + +import java.awt.*; @SuppressWarnings("unused") @ScriptManifest(category = Category.MISC, name = "CShopper", description = "Shops like a woman with an unlimited Visa card", author = Constants.AUTHOR, version = 1.0) -public class CShopper extends TaskScript { +public class CShopper extends CTaskScript { private static final Configuration configuration = new Configuration( Constants.LUNDAIL, - new ItemWrapper(Constants.NATURE_RUNE, 6000), - new ItemWrapper(Constants.COSMIC_RUNE, -1) + new ItemWrapper(Constants.COSMIC_RUNE, -1), + new ItemWrapper(Constants.NATURE_RUNE, 6000) ); + private Buy buyTask; + public static Configuration getConfiguration() { return configuration; } + @Override public void onStart() { + getUI().setCustomLines(configuration.getItems().length); + buyTask = new Buy(); + + addNodes( + new Open(), + buyTask, + new Hop(), + new Stop() + ); + } + + @Override + public void onPaint(Graphics g) { + super.onPaint(g); + + if (buyTask == null) return; + + for (ItemWrapper iw : configuration.getItems()) { + String name = iw.getName(); + Item item = Inventory.get(iw.getName()); + int current = item == null ? 0 : item.getAmount(); + getUI().drawString("Bought " + buyTask.getBought(name) + " " + name + " (" + current + ")"); + } } } diff --git a/CShopper/src/io/reisub/dreambot/cshopper/tasks/Buy.java b/CShopper/src/io/reisub/dreambot/cshopper/tasks/Buy.java index 6015a7a..8cf7129 100644 --- a/CShopper/src/io/reisub/dreambot/cshopper/tasks/Buy.java +++ b/CShopper/src/io/reisub/dreambot/cshopper/tasks/Buy.java @@ -2,11 +2,23 @@ package io.reisub.dreambot.cshopper.tasks; import io.reisub.dreambot.cshopper.CShopper; import io.reisub.dreambot.cshopper.ItemWrapper; +import org.dreambot.api.methods.Calculations; +import org.dreambot.api.methods.MethodProvider; import org.dreambot.api.methods.container.impl.Inventory; import org.dreambot.api.methods.container.impl.Shop; import org.dreambot.api.script.TaskNode; +import org.dreambot.api.wrappers.items.Item; + +import java.util.HashMap; +import java.util.Map; public class Buy extends TaskNode { + private final Map boughtMap; + + public Buy() { + this.boughtMap = new HashMap<>(); + } + @Override public boolean accept() { if (!Shop.isOpen()) return false; @@ -28,19 +40,53 @@ public class Buy extends TaskNode { @Override public int execute() { for (ItemWrapper iw : CShopper.getConfiguration().getItems()) { - int shopCount = Shop.get(iw.getName()).getAmount(); + Item shopItem = Shop.get(iw.getName()); + int shopCount = shopItem.getAmount(); int inventoryCount = 0; if (Inventory.get(iw.getName()) != null) { inventoryCount = Inventory.get(iw.getName()).getAmount(); } - if (shopCount > iw.getMinimumInShop() && inventoryCount < iw.getBuyTotal()) { - int toBuy = shopCount - iw.getMinimumInShop(); - // TODO finish while testing at shop + int buyTotal = iw.getBuyTotal() == -1 ? Integer.MAX_VALUE : iw.getBuyTotal(); + + if (shopCount > iw.getMinimumInShop() && inventoryCount < buyTotal) { + int toBuy = buyTotal - inventoryCount; + int canBuy = iw.getMinimumInShop() == 0 ? 50 : shopCount - iw.getMinimumInShop(); + + if (toBuy >= 50 && canBuy >= 50) { + shopItem.interact("Buy 50"); + } else if (toBuy >= 10 && canBuy >= 10) { + shopItem.interact("Buy 10"); + } else if (toBuy >= 5 && canBuy >= 5) { + shopItem.interact("Buy 5"); + } else { + shopItem.interact("Buy 1"); + } + + int finalInventoryCount = inventoryCount; + if (MethodProvider.sleepUntil(() -> { + Item inventoryItem = Inventory.get(iw.getName()); + return inventoryItem != null && inventoryItem.getAmount() > finalInventoryCount; + }, Calculations.random(2000, 2500))) { + int count = Inventory.get(iw.getName()).getAmount(); + bought(iw.getName(), count - inventoryCount); + } } } return 0; } + + private void bought(String item, int amount) { + int old = getBought(item); + boughtMap.put(item, old + amount); + } + + public int getBought(String item) { + Integer i = boughtMap.get(item); + if (i == null) return 0; + + return i; + } } diff --git a/CShopper/src/io/reisub/dreambot/cshopper/tasks/Hop.java b/CShopper/src/io/reisub/dreambot/cshopper/tasks/Hop.java index 5f006fe..ce5da51 100644 --- a/CShopper/src/io/reisub/dreambot/cshopper/tasks/Hop.java +++ b/CShopper/src/io/reisub/dreambot/cshopper/tasks/Hop.java @@ -2,14 +2,20 @@ package io.reisub.dreambot.cshopper.tasks; import io.reisub.dreambot.cshopper.CShopper; import io.reisub.dreambot.cshopper.ItemWrapper; +import org.dreambot.api.Client; +import org.dreambot.api.data.GameState; import org.dreambot.api.methods.Calculations; +import org.dreambot.api.methods.MethodProvider; import org.dreambot.api.methods.container.impl.Inventory; import org.dreambot.api.methods.container.impl.Shop; +import org.dreambot.api.methods.interactive.Players; +import org.dreambot.api.methods.skills.Skills; import org.dreambot.api.methods.world.World; import org.dreambot.api.methods.world.Worlds; import org.dreambot.api.methods.worldhopper.WorldHopper; import org.dreambot.api.script.TaskNode; +import java.util.Comparator; import java.util.LinkedList; import java.util.List; import java.util.Queue; @@ -18,9 +24,17 @@ public class Hop extends TaskNode { private final Queue worlds; public Hop(boolean members) { - List worldsList = Worlds.all(world -> world.isMembers() == members && world.isNormal() && !world.isHighRisk() && !world.isPVP()); + List worldsList = Worlds.all(world -> world.isMembers() == members && world.isNormal() && !world.isHighRisk() && !world.isPVP() && world.getMinimumLevel() <= Skills.getTotalLevel()); + worldsList.sort(Comparator.comparingInt(World::getWorld)); worlds = new LinkedList<>(worldsList); + + while (true) { + World w = worlds.poll(); + worlds.add(w); + + if (w == null || w.getWorld() == Worlds.getCurrentWorld()) break; + } } public Hop() { @@ -47,13 +61,18 @@ public class Hop extends TaskNode { @Override public int execute() { + Shop.close(); + MethodProvider.sleepUntil(() -> !Shop.isOpen(), Calculations.random(2000, 2500)); + World world = worlds.poll(); worlds.add(world); if (world != null) { WorldHopper.hopWorld(world); - } + + MethodProvider.sleepUntil(() -> Client.getGameState() == GameState.LOGGED_IN, Calculations.random(10000, 11000)); + return Calculations.random(200, 400); } } diff --git a/CShopper/src/io/reisub/dreambot/cshopper/tasks/Stop.java b/CShopper/src/io/reisub/dreambot/cshopper/tasks/Stop.java new file mode 100644 index 0000000..78642fd --- /dev/null +++ b/CShopper/src/io/reisub/dreambot/cshopper/tasks/Stop.java @@ -0,0 +1,32 @@ +package io.reisub.dreambot.cshopper.tasks; + +import io.reisub.dreambot.cshopper.CShopper; +import io.reisub.dreambot.cshopper.ItemWrapper; +import org.dreambot.api.methods.MethodProvider; +import org.dreambot.api.methods.container.impl.Inventory; +import org.dreambot.api.script.ScriptManager; +import org.dreambot.api.script.TaskNode; + +public class Stop extends TaskNode { + @Override + public boolean accept() { + for (ItemWrapper iw : CShopper.getConfiguration().getItems()) { + int inventoryCount = 0; + if (Inventory.get(iw.getName()) != null) { + inventoryCount = Inventory.get(iw.getName()).getAmount(); + } + + if (inventoryCount < iw.getBuyTotal()) return false; + } + + return true; + } + + @Override + public int execute() { + MethodProvider.log("Finished buying"); + ScriptManager.getScriptManager().stop(); + + return 0; + } +}