Skip to content

Instantly share code, notes, and snippets.

@its-jackson
Created August 9, 2023 01:10
Show Gist options
  • Save its-jackson/cf534786dcca53278fdad7e964bd949b to your computer and use it in GitHub Desktop.
Save its-jackson/cf534786dcca53278fdad7e964bd949b to your computer and use it in GitHub Desktop.
package scripts.kotlin.api
import org.tribot.script.sdk.*
import org.tribot.script.sdk.antiban.Antiban
import org.tribot.script.sdk.input.Keyboard
import org.tribot.script.sdk.interfaces.Item
import org.tribot.script.sdk.query.GameObjectQuery
import org.tribot.script.sdk.query.Query
import org.tribot.script.sdk.query.WidgetItemQuery
import org.tribot.script.sdk.types.Widget
import kotlin.jvm.optionals.getOrNull
class DepositBoxAction {
private val depositAllAction = "Deposit-All"
private val depositOneAction = "Deposit-1"
private val depositFiveAction = "Deposit-5"
private val depositTenAction = "Deposit-10"
private val depositXAction = "Deposit-X"
private val depositBoxRootIndex = 192
private val depositBoxWidget = WidgetAddress.create(
depositBoxRootIndex
) { w: Widget ->
w.actions.contains("Close")
}
private val depositInvWidget = WidgetAddress.create(
depositBoxRootIndex
) { w: Widget ->
w.actions.contains("Deposit inventory")
}
private val depositWornItemsWidget = WidgetAddress.create(
depositBoxRootIndex
) { w: Widget ->
w.actions.contains("Deposit worn items")
}
private val depositLootWidget = WidgetAddress.create(
depositBoxRootIndex
) { w: Widget ->
w.actions.contains("Deposit loot")
}
private val enterAmountChatScreenWidget = WidgetAddress.create(
162
) { w: Widget ->
w.text.getOrNull()
?.contains("Enter amount:") == true
}
fun isNearbyAndReachable(): Boolean {
return getDepositBoxGameObjectQuery().isAny
}
fun isOpen(): Boolean {
return depositBoxWidget.lookup()
.getOrNull()
?.isVisible == true
}
fun open(): Boolean {
Widgets.closeAll()
return getDepositBoxGameObjectQuery()
.findBestInteractable()
.getOrNull()
?.let { b ->
val action = b.actions.firstOrNull { it.equals("deposit", true) }
?: return@let false
if (GameState.isAnyItemSelected() || Magic.isAnySpellSelected()) {
if (ChooseOption.ensureDeselected()) {
Waiting.waitNormal(500, 150)
}
}
return@let b.interact(action)
}
?.let {
it && Waiting.waitUntil { isOpen() }
}
?: false
}
fun ensureOpen(): Boolean {
if (isOpen()) {
return true
}
return open()
}
fun close(): Boolean {
if (!isOpen()) {
return true
}
if (Options.isEscapeClosingEnabled() && Antiban.shouldCloseWithEscape()) {
Keyboard.pressEscape()
return Waiting.waitUntil(1200) { !isOpen() }
}
return depositBoxWidget.lookup()
.getOrNull()
?.click("Close") == true && Waiting.waitUntil(1200) { !isOpen() }
}
fun depositInventory(): Boolean {
if (Inventory.isEmpty()) {
return true
}
return depositInvWidget.lookup()
.getOrNull()
?.let {
it.isVisible && it.click() && Waiting.waitUntil(1200) { Inventory.isEmpty() }
}
?: false
}
fun deposit(itemId: Int, amount: Int): Boolean {
return depositInternal({ Inventory.getCount(itemId) }, { getAllItems(itemId) }, amount)
}
fun deposit(itemName: String, amount: Int): Boolean {
return depositInternal({ Inventory.getCount(itemName) }, { getAllItems(itemName) }, amount)
}
fun depositAll(vararg itemIds: Int): Boolean {
return depositAllInternal { getAllItems(*itemIds) }
}
fun depositAll(vararg itemNames: String): Boolean {
return depositAllInternal { getAllItems(*itemNames) }
}
private fun depositInternal(
countFunction: () -> Int,
itemsFunction: () -> List<Item>,
amount: Int
): Boolean {
if (amount <= 0) {
return false
}
val itemsBefore = countFunction()
if (itemsBefore == 0) {
return true
}
val first = itemsFunction().firstOrNull() ?: return true
val futureValue = if (itemsBefore > amount) itemsBefore - amount else 0
val action = when (amount) {
1 -> depositOneAction
5 -> depositFiveAction
10 -> depositTenAction
else -> depositXAction
}
if (!first.click(action)) {
return false
}
if (action == depositXAction) {
if (!Waiting.waitUntil(1200) { enterAmountChatScreenWidget.lookup().getOrNull()?.isVisible == true }) {
return false
}
Keyboard.typeLine(amount.toString(10))
}
return Waiting.waitUntil(1200) { countFunction() == futureValue }
}
private fun depositAllInternal(itemsFunction: () -> List<Item>): Boolean {
return itemsFunction()
.distinctBy { it.id }
.all { it.click(depositAllAction) && Waiting.waitUntil(1200) { !Inventory.contains(it.id) } }
}
private fun getAllItems(vararg itemIds: Int): List<Item> {
return getItemWidgetQuery()
.idEquals(*itemIds)
.toList()
}
private fun getAllItems(vararg itemNames: String): List<Item> {
return getItemWidgetQuery()
.nameContains(*itemNames)
.toList()
}
private fun getItemWidgetQuery(): WidgetItemQuery {
return Query.widgets()
.inRoots(depositBoxRootIndex)
.isVisible
.toItemQuery()
}
private fun getDepositBoxGameObjectQuery(): GameObjectQuery {
return Query.gameObjects()
.maxDistance(15.0)
.actionEquals("Deposit")
.filter {
val name = it.name
return@filter name.contains("bank deposit box", true)
|| name.contains("deposit box", true)
}
.isReachable
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment