Updates and rework to use CScript

This commit is contained in:
2022-01-19 12:35:50 +01:00
parent 78786709eb
commit 7efda7494a
79 changed files with 1295 additions and 818 deletions

View File

@ -29,7 +29,7 @@ import net.runelite.client.config.ConfigGroup;
import net.runelite.client.config.ConfigItem;
import net.runelite.client.config.ConfigSection;
@ConfigGroup("ChaosShopperConfig")
@ConfigGroup("chaosshopper")
public interface Config extends net.runelite.client.config.Config {
@ConfigItem(
@ -54,6 +54,7 @@ public interface Config extends net.runelite.client.config.Config {
keyName = "itemOneEnabled",
name = "Enable",
description = "Enable the buying of this item",
section = "itemOne",
position = 11
)
default boolean itemOneEnabled() {
@ -61,19 +62,21 @@ public interface Config extends net.runelite.client.config.Config {
}
@ConfigItem(
keyName = "itemOneName",
name = "Name",
description = "Name of the item",
keyName = "itemOneId",
name = "ID",
description = "ID of the item",
section = "itemOne",
position = 12
)
default String itemOneName() {
return "";
default int itemOneId() {
return 0;
}
@ConfigItem(
keyName = "itemOneAmount",
name = "Amount",
description = "Amount of the item to buy",
section = "itemOne",
position = 13
)
default int itemOneAmount() {
@ -84,6 +87,7 @@ public interface Config extends net.runelite.client.config.Config {
keyName = "itemOneMinInStore",
name = "Min in store",
description = "Amount to keep in store to prevent overpaying",
section = "itemOne",
position = 14
)
default int itemOneMinInStore() {
@ -103,6 +107,7 @@ public interface Config extends net.runelite.client.config.Config {
keyName = "itemTwoEnabled",
name = "Enable",
description = "Enable the buying of this item",
section = "itemTwo",
position = 21
)
default boolean itemTwoEnabled() {
@ -110,19 +115,21 @@ public interface Config extends net.runelite.client.config.Config {
}
@ConfigItem(
keyName = "itemTwoName",
name = "Name",
description = "Name of the item",
keyName = "itemTwoId",
name = "ID",
description = "ID of the item",
section = "itemTwo",
position = 22
)
default String itemTwoName() {
return "";
default int itemTwoId() {
return 0;
}
@ConfigItem(
keyName = "itemTwoAmount",
name = "Amount",
description = "Amount of the item to buy",
section = "itemTwo",
position = 23
)
default int itemTwoAmount() {
@ -133,6 +140,7 @@ public interface Config extends net.runelite.client.config.Config {
keyName = "itemTwoMinInStore",
name = "Min in store",
description = "Amount to keep in store to prevent overpaying",
section = "itemTwo",
position = 24
)
default int itemTwoMinInStore() {
@ -152,6 +160,7 @@ public interface Config extends net.runelite.client.config.Config {
keyName = "itemThreeEnabled",
name = "Enable",
description = "Enable the buying of this item",
section = "itemThree",
position = 31
)
default boolean itemThreeEnabled() {
@ -159,19 +168,21 @@ public interface Config extends net.runelite.client.config.Config {
}
@ConfigItem(
keyName = "itemThreeName",
name = "Name",
description = "Name of the item",
keyName = "itemThreeId",
name = "ID",
description = "ID of the item",
section = "itemThree",
position = 32
)
default String itemThreeName() {
return "";
default int itemThreeId() {
return 0;
}
@ConfigItem(
keyName = "itemThreeAmount",
name = "Amount",
description = "Amount of the item to buy",
section = "itemThree",
position = 33
)
default int itemThreeAmount() {
@ -182,6 +193,7 @@ public interface Config extends net.runelite.client.config.Config {
keyName = "itemThreeMinInStore",
name = "Min in store",
description = "Amount to keep in store to prevent overpaying",
section = "itemThree",
position = 34
)
default int itemThreeMinInStore() {
@ -201,6 +213,7 @@ public interface Config extends net.runelite.client.config.Config {
keyName = "itemFourEnabled",
name = "Enable",
description = "Enable the buying of this item",
section = "itemFour",
position = 41
)
default boolean itemFourEnabled() {
@ -208,19 +221,21 @@ public interface Config extends net.runelite.client.config.Config {
}
@ConfigItem(
keyName = "itemFourName",
name = "Name",
description = "Name of the item",
keyName = "itemFourId",
name = "ID",
description = "ID of the item",
section = "itemFour",
position = 42
)
default String itemFourName() {
return "";
default int itemFourId() {
return 0;
}
@ConfigItem(
keyName = "itemFourAmount",
name = "Amount",
description = "Amount of the item to buy",
section = "itemFour",
position = 43
)
default int itemFourAmount() {
@ -231,6 +246,7 @@ public interface Config extends net.runelite.client.config.Config {
keyName = "itemFourMinInStore",
name = "Min in store",
description = "Amount to keep in store to prevent overpaying",
section = "itemFour",
position = 44
)
default int itemFourMinInStore() {
@ -250,6 +266,7 @@ public interface Config extends net.runelite.client.config.Config {
keyName = "itemFiveEnabled",
name = "Enable",
description = "Enable the buying of this item",
section = "itemFive",
position = 51
)
default boolean itemFiveEnabled() {
@ -257,19 +274,21 @@ public interface Config extends net.runelite.client.config.Config {
}
@ConfigItem(
keyName = "itemFiveName",
name = "Name",
description = "Name of the item",
keyName = "itemFiveId",
name = "ID",
description = "ID of the item",
section = "itemFive",
position = 52
)
default String itemFiveName() {
return "";
default int itemFiveId() {
return 0;
}
@ConfigItem(
keyName = "itemFiveAmount",
name = "Amount",
description = "Amount of the item to buy",
section = "itemFive",
position = 53
)
default int itemFiveAmount() {
@ -280,6 +299,7 @@ public interface Config extends net.runelite.client.config.Config {
keyName = "itemFiveMinInStore",
name = "Min in store",
description = "Amount to keep in store to prevent overpaying",
section = "itemFive",
position = 54
)
default int itemFiveMinInStore() {

View File

@ -2,15 +2,15 @@ package io.reisub.openosrs.shopper;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@AllArgsConstructor
@Getter
public class Item {
@Getter
private final String name;
@Getter
private final int amount;
@Getter
private final int id;
private final int amountToBuy;
private final int minInShop;
@Setter
private int amountBought;
}

View File

@ -1,21 +1,22 @@
package io.reisub.openosrs.shopper;
import com.google.inject.Provides;
import io.reisub.openosrs.util.Task;
import io.reisub.openosrs.shopper.tasks.*;
import io.reisub.openosrs.util.CScript;
import io.reisub.openosrs.util.Util;
import io.reisub.openosrs.util.tasks.Run;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.events.ConfigButtonClicked;
import net.runelite.api.mixins.Inject;
import net.runelite.api.events.GameTick;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.plugins.PluginDependency;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.iutils.iUtils;
import net.runelite.client.plugins.iutils.scripts.iScript;
import org.pf4j.Extension;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
@ -28,12 +29,10 @@ import java.util.List;
enabledByDefault = false
)
@Slf4j
public class Shopper extends iScript {
public class Shopper extends CScript {
@Inject
private Config config;
private List<Task> tasks;
@Getter
private List<Item> items;
@ -41,69 +40,61 @@ public class Shopper extends iScript {
@Setter
private boolean hop;
private Hop hopTask;
@Provides
@SuppressWarnings("unused")
Config provideConfig(ConfigManager configManager) {
return configManager.getConfig(Config.class);
}
@Override
protected void loop() {
for (Task t : tasks) {
if (t.validate()) {
log.info(t.getStatus());
t.execute();
break;
}
}
game.sleepDelay();
}
@Override
protected void onStart() {
log.info("Starting Chaos Shopper");
super.onStart();
loadItems();
tasks = new ArrayList<>();
}
Run runTask = injector.getInstance(Run.class);
runTask.setInterval(70, 95);
@Override
protected void onStop() {
log.info("Stopping Chaos Shopper");
if (tasks != null) {
tasks.clear();
}
hopTask = injector.getInstance(Hop.class);
tasks.add(hopTask);
tasks.add(runTask);
addTask(Buy.class);
addTask(HandleBank.class);
addTask(CloseShop.class);
addTask(OpenShop.class);
}
@Subscribe
@SuppressWarnings("unused")
private void onConfigButtonPressed(ConfigButtonClicked configButtonClicked) {
if (configButtonClicked.getKey().equals("startButton")) {
execute();
}
private void onGameTick(GameTick event) {
if (!isLoggedIn()) return;
if (hopTask != null) hopTask.onGameTick();
}
private void loadItems() {
items = new ArrayList<>();
if (config.itemOneEnabled()) {
items.add(new Item(config.itemOneName(), config.itemOneAmount(), config.itemOneMinInStore()));
items.add(new Item(config.itemOneId(), config.itemOneAmount(), config.itemOneMinInStore(), 0));
}
if (config.itemTwoEnabled()) {
items.add(new Item(config.itemTwoName(), config.itemTwoAmount(), config.itemTwoMinInStore()));
items.add(new Item(config.itemTwoId(), config.itemTwoAmount(), config.itemTwoMinInStore(), 0));
}
if (config.itemThreeEnabled()) {
items.add(new Item(config.itemThreeName(), config.itemThreeAmount(), config.itemThreeMinInStore()));
items.add(new Item(config.itemThreeId(), config.itemThreeAmount(), config.itemThreeMinInStore(), 0));
}
if (config.itemFourEnabled()) {
items.add(new Item(config.itemFourName(), config.itemFourAmount(), config.itemFourMinInStore()));
items.add(new Item(config.itemFourId(), config.itemFourAmount(), config.itemFourMinInStore(), 0));
}
if (config.itemFiveEnabled()) {
items.add(new Item(config.itemFiveName(), config.itemFiveAmount(), config.itemFiveMinInStore()));
items.add(new Item(config.itemFiveId(), config.itemFiveAmount(), config.itemFiveMinInStore(), 0));
}
}
}

View File

@ -1,11 +1,13 @@
package io.reisub.openosrs.shopper.tasks;
import io.reisub.openosrs.shopper.Item;
import io.reisub.openosrs.shopper.Shopper;
import io.reisub.openosrs.util.Task;
import net.runelite.api.mixins.Inject;
import net.runelite.api.widgets.WidgetInfo;
import net.runelite.api.widgets.Widget;
import net.runelite.client.plugins.iutils.game.iWidget;
import javax.inject.Inject;
public class Buy extends Task {
@Inject
private Shopper plugin;
@ -15,17 +17,57 @@ public class Buy extends Task {
return "Buying";
}
private int currentItemId;
@Override
public boolean validate() {
iWidget shop = game.widget(WidgetInfo.SHOP_ITEMS_CONTAINER);
iWidget shop = game.widget(300, 1);
if (shop == null || shop.hidden()) return false;
return shop != null
&& !shop.hidden()
currentItemId = -1;
for (Item item : plugin.getItems()) {
if (item.getAmountBought() > item.getAmountToBuy()) continue;
int shopCount = getShopCount(item.getId());
if (shopCount > item.getMinInShop()) {
currentItemId = item.getId();
break;
}
}
return currentItemId != -1
&& !game.inventory().full();
}
@Override
public void execute() {
Widget widget = game.client().getWidget(300, 16);
for (Widget child : widget.getChildren()) {
if (child.getItemId() == currentItemId) {
iWidget itemWidget = game.widget(300, 16, child.getIndex());
if (itemWidget == null) return;
itemWidget.interact("Buy 50");
break;
}
}
game.tick();
}
private int getShopCount(int id) {
Widget widget = game.client().getWidget(300, 16);
for (Widget child : widget.getChildren()) {
if (child.getItemId() == id) {
return child.getItemQuantity();
}
}
return 0;
}
}

View File

@ -0,0 +1,37 @@
package io.reisub.openosrs.shopper.tasks;
import io.reisub.openosrs.shopper.Shopper;
import io.reisub.openosrs.util.Task;
import net.runelite.api.widgets.WidgetInfo;
import net.runelite.client.plugins.iutils.game.iWidget;
import javax.inject.Inject;
public class CloseShop extends Task {
@Inject
private Shopper plugin;
@Override
public String getStatus() {
return "Closing shop";
}
@Override
public boolean validate() {
iWidget shop = game.widget(300, 1);
return shop != null && !shop.hidden();
}
@Override
public void execute() {
iWidget close = game.widget(300,1, 11);
if (close == null) return;
close.interact("Close");
game.tick();
if (!game.inventory().full()) {
plugin.setHop(true);
}
}
}

View File

@ -0,0 +1,59 @@
package io.reisub.openosrs.shopper.tasks;
import io.reisub.openosrs.shopper.Item;
import io.reisub.openosrs.shopper.Shopper;
import io.reisub.openosrs.util.Task;
import net.runelite.api.ItemID;
import net.runelite.client.plugins.iutils.game.iObject;
import javax.inject.Inject;
import java.time.Duration;
import java.time.Instant;
public class HandleBank extends Task {
@Inject
private Shopper plugin;
private Instant lastBanking = Instant.EPOCH;
@Override
public String getStatus() {
return "Banking";
}
@Override
public boolean validate() {
return game.inventory().full()
&& Duration.between(lastBanking, Instant.now()).getSeconds() > 3;
}
@Override
public void execute() {
if (!bank.isOpen()) {
iObject bankObj = game.objects().withName("Bank chest", "Bank booth", "Bank Chest-wreck", "Bank chest").nearest();
if (bankObj == null) return;
if (bankObj.actions().contains("Bank")) {
bankObj.interact("Bank");
} else if (bankObj.actions().contains("Use")) {
bankObj.interact("Use");
} else {
bankObj.interact(0);
}
game.waitUntil(() -> bank.isOpen(), 15);
}
for (Item item : plugin.getItems()) {
int quantity = bank.quantity(item.getId()) + (int) game.inventory().withId(item.getId()).count();
item.setAmountBought(quantity);
bank.depositExcept(false, ItemID.COINS, ItemID.COINS_995, ItemID.COINS_6964, ItemID.COINS_8890);
game.sleepDelay();
}
bank.close();
game.sleepDelay();
lastBanking = Instant.now();
}
}

View File

@ -0,0 +1,132 @@
package io.reisub.openosrs.shopper.tasks;
import io.reisub.openosrs.shopper.Config;
import io.reisub.openosrs.shopper.Shopper;
import io.reisub.openosrs.util.Task;
import net.runelite.api.GameState;
import net.runelite.api.widgets.WidgetInfo;
import net.runelite.client.game.WorldService;
import net.runelite.client.util.WorldUtil;
import net.runelite.http.api.worlds.World;
import net.runelite.http.api.worlds.WorldType;
import javax.inject.Inject;
import java.time.Duration;
import java.time.Instant;
import java.util.LinkedList;
import java.util.Queue;
public class Hop extends Task {
@Inject
private Shopper plugin;
@Inject
public WorldService worldService;
private Queue<World> worldQueue;
private net.runelite.api.World quickHopTargetWorld;
private int displaySwitcherAttempts = 0;
private Instant lastAttempt = Instant.EPOCH;
@Override
public String getStatus() {
return "Hopping to next world";
}
@Override
public boolean validate() {
return plugin.isHop() && Duration.between(lastAttempt, Instant.now()).getSeconds() > 5;
}
@Override
public void execute() {
if (worldQueue == null) initializeWorldQueue();
World world = worldQueue.poll();
if (world == null) return;
worldQueue.add(world);
final net.runelite.api.World rsWorld = game.client().createWorld();
rsWorld.setActivity(world.getActivity());
rsWorld.setAddress(world.getAddress());
rsWorld.setId(world.getId());
rsWorld.setPlayerCount(world.getPlayers());
rsWorld.setLocation(world.getLocation());
rsWorld.setTypes(WorldUtil.toWorldTypes(world.getTypes()));
if (game.client().getGameState() == GameState.LOGIN_SCREEN) {
game.client().changeWorld(rsWorld);
return;
}
quickHopTargetWorld = rsWorld;
displaySwitcherAttempts = 0;
lastAttempt = Instant.now();
plugin.setHop(false);
game.tick(3);
}
public void onGameTick() {
if (quickHopTargetWorld == null) return;
if (game.client().getWidget(WidgetInfo.WORLD_SWITCHER_LIST) == null) {
game.client().openWorldHopper();
if (++displaySwitcherAttempts >= 5) {
logWarn("Failed to hop after 5 attempts");
displaySwitcherAttempts = 0;
quickHopTargetWorld = null;
}
} else {
game.client().hopToWorld(quickHopTargetWorld);
plugin.setLastHop(Instant.now());
quickHopTargetWorld = null;
}
}
private void initializeWorldQueue() {
if (worldService.getWorlds() == null) return;
int current = game.client().getWorld();
if (current == 0 || current == -1) return;
worldQueue = new LinkedList<>();
for (World world : worldService.getWorlds().getWorlds()) {
if (!world.getTypes().contains(WorldType.MEMBERS)) continue;
if (world.getTypes().contains(WorldType.BOUNTY)
|| world.getTypes().contains(WorldType.DEADMAN)
|| world.getTypes().contains(WorldType.HIGH_RISK)
|| world.getTypes().contains(WorldType.LAST_MAN_STANDING)
|| world.getTypes().contains(WorldType.NOSAVE_MODE)
|| world.getTypes().contains(WorldType.PVP)
|| world.getTypes().contains(WorldType.SEASONAL)
|| world.getTypes().contains(WorldType.TOURNAMENT)) {
continue;
}
if (world.getTypes().contains(WorldType.SKILL_TOTAL)) {
try {
int totalRequirement = Integer.parseInt(world.getActivity().substring(0, world.getActivity().indexOf(" ")));
if (game.client().getTotalLevel() < totalRequirement) {
continue;
}
} catch (NumberFormatException e) {
logWarn("Failed to parse total level requirement for world " + world.getId());
continue;
}
}
worldQueue.add(world);
}
while (true) {
World world = worldQueue.poll();
worldQueue.add(world);
if (world == null || world.getId() == current) break;
}
}
}

View File

@ -0,0 +1,43 @@
package io.reisub.openosrs.shopper.tasks;
import io.reisub.openosrs.shopper.Config;
import io.reisub.openosrs.util.Task;
import net.runelite.client.plugins.iutils.game.iNPC;
import net.runelite.client.plugins.iutils.game.iWidget;
import net.runelite.client.plugins.iutils.scene.Position;
import javax.inject.Inject;
public class OpenShop extends Task {
@Inject
private Config config;
@Override
public String getStatus() {
return "Opening shop";
}
@Override
public boolean validate() {
iWidget shop = game.widget(300, 1);
return !bank.isOpen()
&& !game.inventory().full()
&& (shop == null || shop.hidden());
}
@Override
public void execute() {
iNPC npc = game.npcs().withName(config.npcName()).nearest();
if (npc == null) {
walking.walkTo(new Position(2669, 3150, 0).areaWithin(2));
}
npc = game.npcs().withName(config.npcName()).nearest();
if (npc == null) return;
npc.interact("Trade");
game.waitUntil(() -> game.widget(300, 1) != null
&& !game.widget(300, 1).hidden(), 20);
}
}