diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a28bff2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.gradle +build +.idea/ +.project +.settings/ +.classpath +nbactions.xml +nb-configuration.xml +nbproject/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..cf4e4cf --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Dude, Where's My Stuff? +A WIP RuneLite plugin. + +TODO: Write README.md diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..7a76a9a --- /dev/null +++ b/build.gradle @@ -0,0 +1,32 @@ +plugins { + id 'java' +} + +repositories { + mavenLocal() + maven { + url = 'https://repo.runelite.net' + } + mavenCentral() +} + +def runeLiteVersion = '1.8.16' + +dependencies { + compileOnly group: 'net.runelite', name:'client', version: runeLiteVersion + + compileOnly 'org.projectlombok:lombok:1.18.20' + annotationProcessor 'org.projectlombok:lombok:1.18.20' + + testImplementation 'junit:junit:4.12' + testImplementation group: 'net.runelite', name:'client', version: runeLiteVersion + testImplementation group: 'net.runelite', name:'jshell', version: runeLiteVersion +} + +group = 'dev.thource.runelite.dudewheresmystuff' +version = '1.0-SNAPSHOT' +sourceCompatibility = '1.8' + +tasks.withType(JavaCompile) { + options.encoding = 'UTF-8' +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..41d9927 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..dcf5e2c --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionSha256Sum=cd5c2958a107ee7f0722004a12d0f8559b4564c34daad7df06cffd4d12a426d0 +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..1b6c787 --- /dev/null +++ b/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..ac1b06f --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/runelite-plugin.properties b/runelite-plugin.properties new file mode 100644 index 0000000..54826df --- /dev/null +++ b/runelite-plugin.properties @@ -0,0 +1,6 @@ +displayName=Dude, Where's My Stuff? +author=Thource +support=https://github.com/Thource/dude-wheres-my-stuff +description=Find your items, gp and minigame points with ease. +tags=uim,storage +plugins=DudeWheresMyStuffPlugin \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..7d9052b --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'dudewheresmystuff' diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/CarryableManager.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/CarryableManager.java new file mode 100644 index 0000000..25b86c1 --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/CarryableManager.java @@ -0,0 +1,72 @@ +package dev.thource.runelite.dudewheresmystuff; + +import com.google.inject.Inject; +import com.google.inject.Singleton; +import lombok.extern.slf4j.Slf4j; +import net.runelite.api.Client; +import net.runelite.api.events.ItemContainerChanged; +import net.runelite.client.Notifier; +import net.runelite.client.config.ConfigManager; +import net.runelite.client.game.ItemManager; + +import java.util.ArrayList; +import java.util.List; + +@Slf4j +@Singleton +public class CarryableManager { + private final Client client; + private final ItemManager itemManager; + private final ConfigManager configManager; + private final DudeWheresMyStuffConfig config; + private final Notifier notifier; + + final List storages = new ArrayList<>(); + + @Inject + private CarryableManager(Client client, ItemManager itemManager, ConfigManager configManager, DudeWheresMyStuffConfig config, Notifier notifier) { + this.client = client; + this.itemManager = itemManager; + this.configManager = configManager; + this.config = config; + this.notifier = notifier; + + reset(); + } + + public long getTotalValue() { + return storages.stream().mapToLong(CarryableStorage::getTotalValue).sum(); + } + + boolean updateVarbits() { + boolean updated = false; + + for (CarryableStorage storage : storages) { + if (storage.updateVarbits()) updated = true; + } + + return updated; + } + + public boolean updateItemContainer(ItemContainerChanged itemContainerChanged) { + boolean updated = false; + + for (CarryableStorage storage : storages) { + if (storage.updateItemContainer(itemContainerChanged)) updated = true; + } + + return updated; + } + + void reset() { + storages.clear(); + + for (CarryableStorageType type : CarryableStorageType.values()) { + if (type.getItemContainerId() != -1) { + storages.add(new CarryableStorage(type, client, itemManager)); + } + } + + storages.add(new RunePouch(CarryableStorageType.RUNE_POUCH, client, itemManager)); + } +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/CarryableStorage.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/CarryableStorage.java new file mode 100644 index 0000000..8e29f96 --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/CarryableStorage.java @@ -0,0 +1,58 @@ +package dev.thource.runelite.dudewheresmystuff; + +import lombok.Getter; +import net.runelite.api.Client; +import net.runelite.api.Item; +import net.runelite.api.ItemComposition; +import net.runelite.api.ItemContainer; +import net.runelite.api.events.ItemContainerChanged; +import net.runelite.client.game.ItemManager; + +import java.util.ArrayList; +import java.util.List; + +@Getter +public class CarryableStorage { + CarryableStorageType type; + List items = new ArrayList<>(); + protected Client client; + protected ItemManager itemManager; + + CarryableStorage(CarryableStorageType type, Client client, ItemManager itemManager) { + this.type = type; + this.client = client; + this.itemManager = itemManager; + } + + long getTotalValue() { + return items.stream().mapToLong(ItemStack::getTotalGePrice).sum(); + } + + boolean updateVarbits() { + return false; + } + + public boolean updateItemContainer(ItemContainerChanged itemContainerChanged) { + if (type.getItemContainerId() == -1 || type.getItemContainerId() != itemContainerChanged.getContainerId()) + return false; + + ItemContainer itemContainer = client.getItemContainer(type.getItemContainerId()); + if (itemContainer == null) return false; + + items.clear(); + for (Item item : itemContainer.getItems()) { + if (item.getId() == -1) continue; + + ItemStack itemStack = items.stream().filter(i -> i.getId() == item.getId()).findFirst().orElse(null); + if (itemStack != null) { + itemStack.setQuantity(itemStack.getQuantity() + item.getQuantity()); + continue; + } + + ItemComposition itemComposition = itemManager.getItemComposition(item.getId()); + items.add(new ItemStack(item.getId(), itemComposition.getName(), item.getQuantity(), itemManager.getItemPrice(item.getId()), itemComposition.getHaPrice(), itemComposition.isStackable())); + } + + return true; + } +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/CarryableStorageType.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/CarryableStorageType.java new file mode 100644 index 0000000..05931f0 --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/CarryableStorageType.java @@ -0,0 +1,20 @@ +package dev.thource.runelite.dudewheresmystuff; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import net.runelite.api.InventoryID; + +@RequiredArgsConstructor +@Getter +public enum CarryableStorageType { + INVENTORY("Inventory", InventoryID.INVENTORY.getId(), true), + EQUIPMENT("Equipment", InventoryID.EQUIPMENT.getId(), true), + LOOTING_BAG("Looting Bag", 516, false), + SEED_BOX("Seed Box", 573, false), + RUNE_POUCH("Rune Pouch", -1, true); + + private final String name; + private final int itemContainerId; + // Whether the storage can be updated with no action required by the player + private final boolean automatic; +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/CarryableTabPanel.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/CarryableTabPanel.java new file mode 100644 index 0000000..4fb86dc --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/CarryableTabPanel.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2018, Daniel Teo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package dev.thource.runelite.dudewheresmystuff; + +import net.runelite.client.game.ItemManager; +import net.runelite.client.ui.ColorScheme; + +import javax.swing.*; +import java.util.Comparator; + +class CarryableTabPanel extends TabContentPanel { + private final DudeWheresMyStuffConfig config; + private final CarryableManager carryableManager; + private final ItemManager itemManager; + + CarryableTabPanel(ItemManager itemManager, DudeWheresMyStuffConfig config, DudeWheresMyStuffPanel pluginPanel, CarryableManager carryableManager) { + this.itemManager = itemManager; + this.config = config; + this.carryableManager = carryableManager; + + setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + setBackground(ColorScheme.DARK_GRAY_COLOR); + + rebuildList(); + } + + private void rebuildList() { + removeAll(); + + carryableManager.storages.stream().sorted(Comparator.comparingLong(CarryableStorage::getTotalValue) + .reversed() + .thenComparing(s->s.getType().getName())).forEach((storage) -> { + ItemsBox itemsBox = new ItemsBox(itemManager, storage.getType().getName(), null, false); + for (ItemStack itemStack : storage.getItems()) { + itemsBox.getItems().add(itemStack); + } + itemsBox.rebuild(); + add(itemsBox); + }); + + revalidate(); + } + + @Override + public int getUpdateInterval() { + return 50; // 10 seconds + } + + @Override + public void update() { + rebuildList(); + } +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/CoinStorage.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/CoinStorage.java new file mode 100644 index 0000000..733909d --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/CoinStorage.java @@ -0,0 +1,48 @@ +package dev.thource.runelite.dudewheresmystuff; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import net.runelite.api.Client; +import net.runelite.api.ItemContainer; +import net.runelite.api.events.ItemContainerChanged; +import net.runelite.client.game.ItemManager; + +@RequiredArgsConstructor +@Getter +public class CoinStorage { + CoinStorageType type; + protected Client client; + protected ItemManager itemManager; + + int coins; + + CoinStorage(CoinStorageType type, Client client, ItemManager itemManager) { + this.type = type; + this.client = client; + this.itemManager = itemManager; + } + + boolean updateVarbits() { + if (type.getVarbitId() == -1) return false; + + int coins = client.getVarbitValue(type.getVarbitId()) * type.getMultiplier(); + if (this.coins == coins) return false; + + this.coins = coins; + return true; + } + + boolean updateItemContainer(ItemContainerChanged itemContainerChanged) { + if (type.getItemContainerId() == -1 || type.getItemContainerId() != itemContainerChanged.getContainerId()) + return false; + + ItemContainer itemContainer = client.getItemContainer(type.getItemContainerId()); + if (itemContainer == null) return false; + + int coins = itemContainer.count(995); + if (this.coins == coins) return false; + + this.coins = coins; + return true; + } +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/CoinStorageType.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/CoinStorageType.java new file mode 100644 index 0000000..1e53e4b --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/CoinStorageType.java @@ -0,0 +1,32 @@ +package dev.thource.runelite.dudewheresmystuff; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import net.runelite.api.InventoryID; +import net.runelite.api.Varbits; + +@RequiredArgsConstructor +@Getter +public enum CoinStorageType { + NMZ("Nightmare Zone", 3948, -1, true, 1000), + LMS("Last Man Standing", 5305, -1, true, 1000), +// SERVANT_MONEYBAG("Servant's Moneybag", 5305, -1, 100), + BF("Blast Furnace", Varbits.BLAST_FURNACE_COFFER.getId(), -1, true), + INVENTORY("Inventory", -1, InventoryID.INVENTORY.getId(), true), + LOOTING_BAG("Looting Bag", -1, 516, false); + + CoinStorageType(String name, int varbitId, int itemContainerId, boolean automatic) { + this.name = name; + this.varbitId = varbitId; + this.itemContainerId = itemContainerId; + this.automatic = automatic; + this.multiplier = 1; + } + + private final String name; + private final int varbitId; + private final int itemContainerId; + // Whether the storage can be updated with no action required by the player + private final boolean automatic; + private final int multiplier; +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/CoinsManager.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/CoinsManager.java new file mode 100644 index 0000000..a52c8af --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/CoinsManager.java @@ -0,0 +1,68 @@ +package dev.thource.runelite.dudewheresmystuff; + +import com.google.inject.Inject; +import com.google.inject.Singleton; +import lombok.extern.slf4j.Slf4j; +import net.runelite.api.Client; +import net.runelite.api.events.ItemContainerChanged; +import net.runelite.client.Notifier; +import net.runelite.client.config.ConfigManager; +import net.runelite.client.game.ItemManager; + +import java.util.ArrayList; +import java.util.List; + +@Slf4j +@Singleton +public class CoinsManager { + private final Client client; + private final ItemManager itemManager; + private final ConfigManager configManager; + private final DudeWheresMyStuffConfig config; + private final Notifier notifier; + + final List storages = new ArrayList<>(); + + @Inject + private CoinsManager(Client client, ItemManager itemManager, ConfigManager configManager, DudeWheresMyStuffConfig config, Notifier notifier) { + this.client = client; + this.itemManager = itemManager; + this.configManager = configManager; + this.config = config; + this.notifier = notifier; + + reset(); + } + + public long getTotalValue() { + return storages.stream().mapToLong(CoinStorage::getCoins).sum(); + } + + boolean updateVarbits() { + boolean updated = false; + + for (CoinStorage coinStorage : storages) { + if (coinStorage.updateVarbits()) updated = true; + } + + return updated; + } + + boolean updateItemContainer(ItemContainerChanged itemContainerChanged) { + boolean updated = false; + + for (CoinStorage coinStorage : storages) { + if (coinStorage.updateItemContainer(itemContainerChanged)) updated = true; + } + + return updated; + } + + void reset() { + storages.clear(); + + for (CoinStorageType type : CoinStorageType.values()) { + storages.add(new CoinStorage(type, client, itemManager)); + } + } +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/CoinsTabPanel.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/CoinsTabPanel.java new file mode 100644 index 0000000..2005433 --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/CoinsTabPanel.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2018, Daniel Teo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package dev.thource.runelite.dudewheresmystuff; + +import net.runelite.client.game.ItemManager; +import net.runelite.client.ui.ColorScheme; + +import javax.swing.*; +import java.util.Comparator; + +class CoinsTabPanel extends TabContentPanel { + private final DudeWheresMyStuffConfig config; + private final CoinsManager coinsManager; + private final ItemManager itemManager; + + CoinsTabPanel(ItemManager itemManager, DudeWheresMyStuffConfig config, DudeWheresMyStuffPanel pluginPanel, CoinsManager coinsManager) { + this.itemManager = itemManager; + this.config = config; + this.coinsManager = coinsManager; + + setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + setBackground(ColorScheme.DARK_GRAY_COLOR); + + rebuildList(); + } + + private void rebuildList() { + removeAll(); + + coinsManager.storages.stream().sorted(Comparator.comparingLong(CoinStorage::getCoins) + .reversed() + .thenComparing(s -> s.getType().getName())).forEach((storage) -> { + ItemsBox itemsBox = new ItemsBox(itemManager, storage.getType().getName(), null, false); + if (storage.getCoins() > 0) { + itemsBox.getItems().add(new ItemStack(995, "Coins", storage.getCoins(), 1, 0, true)); + } + itemsBox.rebuild(); + add(itemsBox); + }); + + revalidate(); + } + + @Override + public int getUpdateInterval() { + return 50; // 10 seconds + } + + @Override + public void update() { + rebuildList(); + } +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/DudeWheresMyStuffConfig.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/DudeWheresMyStuffConfig.java new file mode 100644 index 0000000..298ccc0 --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/DudeWheresMyStuffConfig.java @@ -0,0 +1,8 @@ +package dev.thource.runelite.dudewheresmystuff; + +import net.runelite.client.config.Config; +import net.runelite.client.config.ConfigGroup; + +@ConfigGroup("dudewheresmystuff") +public interface DudeWheresMyStuffConfig extends Config { +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/DudeWheresMyStuffPanel.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/DudeWheresMyStuffPanel.java new file mode 100644 index 0000000..c93b97b --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/DudeWheresMyStuffPanel.java @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2018 Abex + * Copyright (c) 2018, Psikoi + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package dev.thource.runelite.dudewheresmystuff; + +import com.google.inject.Inject; +import com.google.inject.name.Named; +import net.runelite.client.config.ConfigManager; +import net.runelite.client.game.ItemManager; +import net.runelite.client.ui.ColorScheme; +import net.runelite.client.ui.PluginPanel; +import net.runelite.client.ui.components.materialtabs.MaterialTab; +import net.runelite.client.ui.components.materialtabs.MaterialTabGroup; +import net.runelite.client.util.AsyncBufferedImage; + +import javax.annotation.Nullable; +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.HashMap; +import java.util.Map; + +class DudeWheresMyStuffPanel extends PluginPanel { + private final ItemManager itemManager; + private final DudeWheresMyStuffConfig config; + + /* This is the panel the tabs' respective panels will be displayed on. */ + private final JPanel display = new JPanel(); + private final Map uiTabs = new HashMap<>(); + private final MaterialTabGroup tabGroup = new MaterialTabGroup(display); + + private boolean active; + + @Nullable + private TabContentPanel activeTabPanel = null; + + @Inject + DudeWheresMyStuffPanel(ItemManager itemManager, DudeWheresMyStuffConfig config, ConfigManager configManager, CoinsManager coinsManager, CarryableManager carryableManager, + @Named("developerMode") boolean developerMode) { + super(false); + + this.itemManager = itemManager; + this.config = config; + + setLayout(new BorderLayout()); + setBackground(ColorScheme.DARK_GRAY_COLOR); + + display.setBorder(new EmptyBorder(6, 6, 6, 6)); + + tabGroup.setLayout(new GridLayout(0, 6, 7, 7)); + tabGroup.setBorder(new EmptyBorder(6, 6, 0, 6)); + + add(tabGroup, BorderLayout.NORTH); + add(display, BorderLayout.CENTER); + + addTab(Tab.OVERVIEW, new OverviewTabPanel(itemManager, config, this, coinsManager, carryableManager)); + + addTab(Tab.COINS, new CoinsTabPanel(itemManager, config, this, coinsManager)); + addTab(Tab.CARRYABLE_STORAGE, new CarryableTabPanel(itemManager, config, this, carryableManager)); + +// for (Tab tab : Tab.TABS) { +// addTab(tab, new OverviewTabPanel(itemManager, config, this, coinsManager)); +// } + } + + private void addTab(Tab tab, TabContentPanel tabContentPanel) { + JPanel wrapped = new JPanel(new BorderLayout()); + wrapped.add(tabContentPanel, BorderLayout.NORTH); + wrapped.setBackground(ColorScheme.DARK_GRAY_COLOR); + + JScrollPane scroller = new JScrollPane(wrapped); + scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); + scroller.getVerticalScrollBar().setPreferredSize(new Dimension(16, 0)); + scroller.getVerticalScrollBar().setBorder(new EmptyBorder(0, 9, 0, 0)); + scroller.setBackground(ColorScheme.DARK_GRAY_COLOR); + + // Use a placeholder icon until the async image gets loaded + MaterialTab materialTab = new MaterialTab(new ImageIcon(), tabGroup, scroller); + materialTab.setPreferredSize(new Dimension(30, 27)); + materialTab.setName(tab.getName()); + materialTab.setToolTipText(tab.getName()); + + AsyncBufferedImage icon = itemManager.getImage(tab.getItemID(), tab.getItemQuantity(), false); + Runnable resize = () -> + { + BufferedImage subIcon = icon.getSubimage(0, 0, 32, 32); + materialTab.setIcon(new ImageIcon(subIcon.getScaledInstance(24, 24, Image.SCALE_SMOOTH))); + }; + icon.onLoaded(resize); + resize.run(); + + materialTab.setOnSelectEvent(() -> + { + activeTabPanel = tabContentPanel; + + tabContentPanel.update(); + return true; + }); + + uiTabs.put(tab, materialTab); + tabGroup.addTab(materialTab); + + if (tab == Tab.OVERVIEW) { + tabGroup.select(materialTab); + } + } + + void switchTab(Tab tab) { + tabGroup.select(uiTabs.get(tab)); + } + + /** + * Gets the update interval of the active tab panel, in units of 200 milliseconds. + */ + int getUpdateInterval() { + return activeTabPanel == null ? Integer.MAX_VALUE : activeTabPanel.getUpdateInterval(); + } + + /** + * Updates the active tab panel, if this plugin panel is displayed. + */ + void update() { + if (!active || activeTabPanel == null) { + return; + } + + SwingUtilities.invokeLater(activeTabPanel::update); + } + + @Override + public void onActivate() { + active = true; + update(); + } + + @Override + public void onDeactivate() { + active = false; + } +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/DudeWheresMyStuffPlugin.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/DudeWheresMyStuffPlugin.java new file mode 100644 index 0000000..c41823a --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/DudeWheresMyStuffPlugin.java @@ -0,0 +1,116 @@ +package dev.thource.runelite.dudewheresmystuff; + +import com.google.inject.Provides; +import lombok.extern.slf4j.Slf4j; +import net.runelite.api.*; +import net.runelite.api.events.ActorDeath; +import net.runelite.api.events.GameStateChanged; +import net.runelite.api.events.ItemContainerChanged; +import net.runelite.api.events.VarbitChanged; +import net.runelite.client.config.ConfigManager; +import net.runelite.client.eventbus.Subscribe; +import net.runelite.client.plugins.Plugin; +import net.runelite.client.plugins.PluginDescriptor; +import net.runelite.client.ui.ClientToolbar; +import net.runelite.client.ui.NavigationButton; +import net.runelite.client.util.ImageUtil; + +import javax.inject.Inject; +import java.awt.image.BufferedImage; + +@Slf4j +@PluginDescriptor( + name = "Dude, Where's My Stuff?" +) +public class DudeWheresMyStuffPlugin extends Plugin { + @Inject + private ClientToolbar clientToolbar; + + @Inject + private Client client; + + @Inject + private DudeWheresMyStuffConfig config; + + @Inject + private CoinsManager coinsManager; + + @Inject + private CarryableManager carryableManager; + + private DudeWheresMyStuffPanel panel; + + private NavigationButton navButton; + + @Override + protected void startUp() throws Exception { + final BufferedImage icon = ImageUtil.loadImageResource(getClass(), "icon.png"); + + panel = injector.getInstance(DudeWheresMyStuffPanel.class); + + navButton = NavigationButton.builder() + .tooltip("Dude, Where's My Stuff?") + .icon(icon) + .panel(panel) + .priority(4) + .build(); + + clientToolbar.addNavigation(navButton); + } + + @Override + protected void shutDown() throws Exception { + clientToolbar.removeNavigation(navButton); + } + + @Subscribe + public void onActorDeath(ActorDeath actorDeath) { + if (actorDeath.getActor() == client.getLocalPlayer()) { + log.info("OH NO, YOU HAVE DIED!"); + HashTable itemContainers = client.getItemContainers(); + + log.info(client.getLocalPlayer().getWorldLocation().toString()); + + log.info("Items:"); + for (ItemContainer itemContainer : itemContainers) { + Item[] items = itemContainer.getItems(); + if (items.length == 0) continue; + + log.info(" Container " + itemContainer.getId() + ":"); + for (Item item : items) { + log.info(" " + item.getId() + " x " + item.getQuantity()); + } + } + } + } + + @Subscribe + public void onGameStateChanged(GameStateChanged gameStateChanged) { + if (gameStateChanged.getGameState() == GameState.LOGIN_SCREEN) { + coinsManager.reset(); + carryableManager.reset(); + } + } + + @Subscribe + public void onVarbitChanged(VarbitChanged varbitChanged) { + boolean isPanelDirty = coinsManager.updateVarbits(); + if (carryableManager.updateVarbits()) isPanelDirty = true; + + if (isPanelDirty) panel.update(); + } + + @Subscribe + public void onItemContainerChanged(ItemContainerChanged itemContainerChanged) + { + boolean isPanelDirty = coinsManager.updateItemContainer(itemContainerChanged); + if (carryableManager.updateItemContainer(itemContainerChanged)) isPanelDirty = true; + + if (isPanelDirty) panel.update(); + } + + @Provides + DudeWheresMyStuffConfig provideConfig(ConfigManager configManager) { + return configManager.getConfig(DudeWheresMyStuffConfig.class); + } +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/ItemStack.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/ItemStack.java new file mode 100644 index 0000000..3192ccd --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/ItemStack.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2018, Tomas Slusny + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package dev.thource.runelite.dudewheresmystuff; + +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; + +@AllArgsConstructor +@Getter +@EqualsAndHashCode +class ItemStack +{ + private final int id; + private final String name; + private int quantity; + private final int gePrice; + private final int haPrice; + private final boolean stackable; + + long getTotalGePrice() + { + return (long) gePrice * quantity; + } + long getTotalHaPrice() + { + return (long) haPrice * quantity; + } + + void setQuantity(int quantity) { + this.quantity = quantity; + } +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/ItemsBox.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/ItemsBox.java new file mode 100644 index 0000000..156b9cc --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/ItemsBox.java @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2018, Psikoi + * Copyright (c) 2018, Tomas Slusny + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package dev.thource.runelite.dudewheresmystuff; + +import com.google.common.base.Strings; +import lombok.AccessLevel; +import lombok.Getter; +import net.runelite.api.ItemID; +import net.runelite.client.game.ItemManager; +import net.runelite.client.ui.ColorScheme; +import net.runelite.client.ui.FontManager; +import net.runelite.client.util.AsyncBufferedImage; +import net.runelite.client.util.QuantityFormatter; +import net.runelite.client.util.Text; + +import javax.annotation.Nullable; +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.awt.*; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.function.ToLongFunction; + +class ItemsBox extends JPanel { + private static final int ITEMS_PER_ROW = 5; + private static final int TITLE_PADDING = 5; + + private final JPanel itemContainer = new JPanel(); + private final JLabel priceLabel = new JLabel(); + private final JLabel subTitleLabel = new JLabel(); + private final JPanel logTitle = new JPanel(); + private final ItemManager itemManager; + @Getter(AccessLevel.PACKAGE) + private final String id; + @Getter(AccessLevel.PACKAGE) + private final boolean showAlchPrices; + + @Getter + private final List items = new ArrayList<>(); + + private long totalPrice; + + ItemsBox( + final ItemManager itemManager, + final String id, + @Nullable final String subtitle, + final boolean showAlchPrices) { + this.id = id; + this.itemManager = itemManager; + this.showAlchPrices = showAlchPrices; + + setLayout(new BorderLayout(0, 1)); + setBorder(new EmptyBorder(5, 0, 0, 0)); + + logTitle.setLayout(new BoxLayout(logTitle, BoxLayout.X_AXIS)); + logTitle.setBorder(new EmptyBorder(7, 7, 7, 7)); + logTitle.setBackground(ColorScheme.DARKER_GRAY_COLOR.darker()); + + JLabel titleLabel = new JLabel(); + titleLabel.setText(Text.removeTags(id)); + titleLabel.setFont(FontManager.getRunescapeSmallFont()); + titleLabel.setForeground(Color.WHITE); + // Set a size to make BoxLayout truncate the name + titleLabel.setMinimumSize(new Dimension(1, titleLabel.getPreferredSize().height)); + logTitle.add(titleLabel); + + subTitleLabel.setFont(FontManager.getRunescapeSmallFont()); + subTitleLabel.setForeground(ColorScheme.LIGHT_GRAY_COLOR); + + if (!Strings.isNullOrEmpty(subtitle)) { + subTitleLabel.setText(subtitle); + } + + logTitle.add(Box.createRigidArea(new Dimension(TITLE_PADDING, 0))); + logTitle.add(subTitleLabel); + logTitle.add(Box.createHorizontalGlue()); + logTitle.add(Box.createRigidArea(new Dimension(TITLE_PADDING, 0))); + + priceLabel.setFont(FontManager.getRunescapeSmallFont()); + priceLabel.setForeground(ColorScheme.LIGHT_GRAY_COLOR); + logTitle.add(priceLabel); + + add(logTitle, BorderLayout.NORTH); + add(itemContainer, BorderLayout.CENTER); + } + + void rebuild() { + buildItems(); + + priceLabel.setText(QuantityFormatter.quantityToStackSize(totalPrice) + " gp"); + priceLabel.setToolTipText(QuantityFormatter.formatNumber(totalPrice) + " gp"); + + revalidate(); + } + + void collapse() { + if (!isCollapsed()) { + itemContainer.setVisible(false); + applyDimmer(false, logTitle); + } + } + + void expand() { + if (items.isEmpty()) return; + + if (isCollapsed()) { + itemContainer.setVisible(true); + applyDimmer(true, logTitle); + } + } + + boolean isCollapsed() { + return !itemContainer.isVisible(); + } + + private void applyDimmer(boolean brighten, JPanel panel) { + for (Component component : panel.getComponents()) { + Color color = component.getForeground(); + + component.setForeground(brighten ? color.brighter() : color.darker()); + } + } + + /** + * This method creates stacked items from the item list, calculates total price and then + * displays all the items in the UI. + */ + private void buildItems() { + totalPrice = 0; + + List items = this.items; + + ToLongFunction getPrice = showAlchPrices + ? ItemStack::getTotalHaPrice + : ItemStack::getTotalGePrice; + + totalPrice = items.stream() + .mapToLong(getPrice) + .sum(); + + itemContainer.removeAll(); + + items.sort(Comparator.comparingLong(getPrice).reversed()); + + // Calculates how many rows need to be display to fit all items + final int rowSize = ((items.size() % ITEMS_PER_ROW == 0) ? 0 : 1) + items.size() / ITEMS_PER_ROW; + + itemContainer.setLayout(new GridLayout(rowSize, ITEMS_PER_ROW, 1, 1)); + + for (int i = 0; i < rowSize * ITEMS_PER_ROW; i++) { + final JPanel slotContainer = new JPanel(); + slotContainer.setBackground(ColorScheme.DARKER_GRAY_COLOR); + + if (i < items.size()) { + final ItemStack item = items.get(i); + final JLabel imageLabel = new JLabel(); + imageLabel.setToolTipText(buildToolTip(item)); + imageLabel.setVerticalAlignment(SwingConstants.CENTER); + imageLabel.setHorizontalAlignment(SwingConstants.CENTER); + + AsyncBufferedImage itemImage = itemManager.getImage(item.getId(), item.getQuantity(), item.isStackable() || item.getQuantity() > 1); + itemImage.addTo(imageLabel); + + slotContainer.add(imageLabel); + } + + itemContainer.add(slotContainer); + } + + itemContainer.revalidate(); + } + + private static String buildToolTip(ItemStack item) { + final String name = item.getName(); + final int quantity = item.getQuantity(); + final long gePrice = item.getTotalGePrice(); + final long haPrice = item.getTotalHaPrice(); + final StringBuilder sb = new StringBuilder(""); + sb.append(name).append(" x ").append(QuantityFormatter.formatNumber(quantity)); + if (item.getId() == ItemID.COINS_995) { + sb.append(""); + return sb.toString(); + } + + sb.append("
GE: ").append(QuantityFormatter.quantityToStackSize(gePrice)); + if (quantity > 1) { + sb.append(" (").append(QuantityFormatter.quantityToStackSize(item.getGePrice())).append(" ea)"); + } + + if (item.getId() == ItemID.PLATINUM_TOKEN) { + sb.append(""); + return sb.toString(); + } + + sb.append("
HA: ").append(QuantityFormatter.quantityToStackSize(haPrice)); + if (quantity > 1) { + sb.append(" (").append(QuantityFormatter.quantityToStackSize(item.getHaPrice())).append(" ea)"); + } + sb.append(""); + return sb.toString(); + } +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/OverviewItemPanel.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/OverviewItemPanel.java new file mode 100644 index 0000000..3b96b87 --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/OverviewItemPanel.java @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2018, Daniel Teo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package dev.thource.runelite.dudewheresmystuff; + +import net.runelite.api.Constants; +import net.runelite.client.game.ItemManager; +import net.runelite.client.ui.ColorScheme; +import net.runelite.client.ui.FontManager; +import net.runelite.client.util.ImageUtil; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.awt.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.function.BooleanSupplier; + +class OverviewItemPanel extends JPanel +{ + private static final ImageIcon ARROW_RIGHT_ICON; + + private static final Color HOVER_COLOR = ColorScheme.DARKER_GRAY_HOVER_COLOR; + + private final JPanel textContainer; + private final JLabel statusLabel; + private final JLabel arrowLabel; + private final BooleanSupplier isSelectable; + + private boolean isHighlighted; + + static + { + ARROW_RIGHT_ICON = new ImageIcon(ImageUtil.loadImageResource(DudeWheresMyStuffPlugin.class, "/util/arrow_right.png")); + } + + OverviewItemPanel(ItemManager itemManager, DudeWheresMyStuffPanel pluginPanel, Tab tab, String title) + { + this(itemManager, () -> pluginPanel.switchTab(tab), () -> true, tab.getItemID(), tab.getItemQuantity(), title); + } + + OverviewItemPanel(ItemManager itemManager, Runnable onTabSwitched, BooleanSupplier isSelectable, int iconItemID, int iconItemQuantity, String title) + { + this.isSelectable = isSelectable; + + setBackground(ColorScheme.DARKER_GRAY_COLOR); + setLayout(new BorderLayout()); + setBorder(new EmptyBorder(7, 7, 7, 7)); + + JLabel iconLabel = new JLabel(); + iconLabel.setMinimumSize(new Dimension(Constants.ITEM_SPRITE_WIDTH, Constants.ITEM_SPRITE_HEIGHT)); + itemManager.getImage(iconItemID, iconItemQuantity, false).addTo(iconLabel); + add(iconLabel, BorderLayout.WEST); + + textContainer = new JPanel(); + textContainer.setBackground(ColorScheme.DARKER_GRAY_COLOR); + textContainer.setLayout(new GridLayout(2, 1)); + textContainer.setBorder(new EmptyBorder(5, 7, 5, 7)); + + addMouseListener(new MouseAdapter() + { + @Override + public void mousePressed(MouseEvent mouseEvent) + { + onTabSwitched.run(); + + setHighlighted(false); + } + + @Override + public void mouseReleased(MouseEvent e) + { + setHighlighted(true); + } + + @Override + public void mouseEntered(MouseEvent e) + { + setHighlighted(true); + } + + @Override + public void mouseExited(MouseEvent e) + { + setHighlighted(false); + } + }); + + JLabel titleLabel = new JLabel(title); + titleLabel.setForeground(Color.WHITE); + titleLabel.setFont(FontManager.getRunescapeSmallFont()); + + statusLabel = new JLabel(); + statusLabel.setForeground(Color.GRAY); + statusLabel.setFont(FontManager.getRunescapeSmallFont()); + + textContainer.add(titleLabel); + textContainer.add(statusLabel); + + add(textContainer, BorderLayout.CENTER); + + arrowLabel = new JLabel(ARROW_RIGHT_ICON); + arrowLabel.setVisible(isSelectable.getAsBoolean()); + add(arrowLabel, BorderLayout.EAST); + } + + void updateStatus(String text, Color color) + { + statusLabel.setText(text); + statusLabel.setForeground(color); + + arrowLabel.setVisible(isSelectable.getAsBoolean()); + + if (isHighlighted && !isSelectable.getAsBoolean()) + { + setHighlighted(false); + } + } + + private void setHighlighted(boolean highlighted) + { + if (highlighted && !isSelectable.getAsBoolean()) + { + return; + } + + setBackground(highlighted ? HOVER_COLOR : ColorScheme.DARKER_GRAY_COLOR); + setCursor(new Cursor(highlighted && getMousePosition(true) != null ? Cursor.HAND_CURSOR : Cursor.DEFAULT_CURSOR)); + textContainer.setBackground(highlighted ? HOVER_COLOR : ColorScheme.DARKER_GRAY_COLOR); + + isHighlighted = highlighted; + } +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/OverviewTabPanel.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/OverviewTabPanel.java new file mode 100644 index 0000000..9988ee3 --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/OverviewTabPanel.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2018, Daniel Teo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package dev.thource.runelite.dudewheresmystuff; + +import com.google.common.collect.ImmutableMap; +import net.runelite.client.game.ItemManager; +import net.runelite.client.ui.ColorScheme; + +import java.awt.*; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Stream; + +class OverviewTabPanel extends TabContentPanel { + private final DudeWheresMyStuffConfig config; + + private final Map overviews; + private final CoinsManager coinsManager; + private final CarryableManager carryableManager; + + OverviewTabPanel(ItemManager itemManager, DudeWheresMyStuffConfig config, DudeWheresMyStuffPanel pluginPanel, CoinsManager coinsManager, CarryableManager carryableManager) { + this.config = config; + this.coinsManager = coinsManager; + this.carryableManager = carryableManager; + + setLayout(new GridLayout(0, 1, 0, 8)); + setBackground(ColorScheme.DARK_GRAY_COLOR); + + overviews = Stream.of(Tab.TABS) + .filter(v -> v != Tab.OVERVIEW) + .collect(ImmutableMap.toImmutableMap( + Function.identity(), + t -> + { + OverviewItemPanel p = new OverviewItemPanel(itemManager, pluginPanel, t, t.getName()); + add(p); + return p; + } + )); + } + + @Override + public int getUpdateInterval() { + return 50; // 10 seconds + } + + @Override + public void update() { + overviews.get(Tab.COINS).updateStatus(String.format("%,d gp", coinsManager.getTotalValue()), Color.LIGHT_GRAY); + overviews.get(Tab.CARRYABLE_STORAGE).updateStatus(String.format("%,d gp", carryableManager.getTotalValue()), Color.LIGHT_GRAY); + } +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/RunePouch.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/RunePouch.java new file mode 100644 index 0000000..003ec15 --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/RunePouch.java @@ -0,0 +1,71 @@ +package dev.thource.runelite.dudewheresmystuff; + +import lombok.Getter; +import net.runelite.api.Client; +import net.runelite.api.ItemComposition; +import net.runelite.api.Varbits; +import net.runelite.client.game.ItemManager; +import net.runelite.client.game.RunepouchRune; + +@Getter +public class RunePouch extends CarryableStorage { + private int rune1Type; + private int rune2Type; + private int rune3Type; + + private int rune1Quantity; + private int rune2Quantity; + private int rune3Quantity; + + RunePouch(CarryableStorageType type, Client client, ItemManager itemManager) { + super(type, client, itemManager); + } + + boolean updateVarbits() { + int newRune1Type = client.getVar(Varbits.RUNE_POUCH_RUNE1); + int newRune2Type = client.getVar(Varbits.RUNE_POUCH_RUNE2); + int newRune3Type = client.getVar(Varbits.RUNE_POUCH_RUNE3); + int newRune1Quantity = client.getVar(Varbits.RUNE_POUCH_AMOUNT1); + int newRune2Quantity = client.getVar(Varbits.RUNE_POUCH_AMOUNT2); + int newRune3Quantity = client.getVar(Varbits.RUNE_POUCH_AMOUNT3); + + if (newRune1Type == rune1Type && newRune2Type == rune2Type && newRune3Type == rune3Type + && newRune1Quantity == rune1Quantity && newRune2Quantity == rune2Quantity && newRune3Quantity == rune3Quantity) + return false; + + rune1Type = newRune1Type; + rune2Type = newRune2Type; + rune3Type = newRune3Type; + + rune1Quantity = newRune1Quantity; + rune2Quantity = newRune2Quantity; + rune3Quantity = newRune3Quantity; + + refreshItems(); + + return true; + } + + private void refreshItems() { + items.clear(); + + RunepouchRune rune1 = RunepouchRune.getRune(rune1Type); + RunepouchRune rune2 = RunepouchRune.getRune(rune2Type); + RunepouchRune rune3 = RunepouchRune.getRune(rune3Type); + + if (rune1 != null) { + ItemComposition itemComposition = itemManager.getItemComposition(rune1.getItemId()); + items.add(new ItemStack(rune1.getItemId(), rune1.getName(), rune1Quantity, itemManager.getItemPrice(rune1.getItemId()), itemComposition.getHaPrice(), true)); + } + + if (rune2 != null) { + ItemComposition itemComposition = itemManager.getItemComposition(rune2.getItemId()); + items.add(new ItemStack(rune2.getItemId(), rune2.getName(), rune2Quantity, itemManager.getItemPrice(rune2.getItemId()), itemComposition.getHaPrice(), true)); + } + + if (rune3 != null) { + ItemComposition itemComposition = itemManager.getItemComposition(rune3.getItemId()); + items.add(new ItemStack(rune3.getItemId(), rune3.getName(), rune3Quantity, itemManager.getItemPrice(rune3.getItemId()), itemComposition.getHaPrice(), true)); + } + } +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/Tab.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/Tab.java new file mode 100644 index 0000000..2fb2f76 --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/Tab.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2018 Abex + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package dev.thource.runelite.dudewheresmystuff; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import net.runelite.api.ItemID; + +@RequiredArgsConstructor +@Getter +public enum Tab +{ + OVERVIEW("Overview", ItemID.OLD_NOTES), + COINS("Coins", ItemID.COINS_995, 0xBADCA7), + CARRYABLE_STORAGE("Carry-able Storage", ItemID.LOOTING_BAG), + MINIGAMES("Minigames", ItemID.PROGRESS_HAT), + DEATH_BANKS("Death Banks", ItemID.CASKET), + DEATH_PILES("Death Piles", ItemID.GRAVESTONE), + POH_STORAGE("POH Storage", ItemID.EXIT_PORTAL), + STASH_UNITS("Stash Units", ItemID.CLUE_SCROLL_MASTER), + LEPRECHAUN("Leprechaun", ItemID.LEPRECHAUN_HAT); + + public static final Tab[] TABS = {COINS, CARRYABLE_STORAGE, MINIGAMES}; + + Tab(String name, int itemID) { + this.name = name; + this.itemID = itemID; + this.itemQuantity = 1; + } + + private final String name; + private final int itemID; + private final int itemQuantity; +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/TabContentPanel.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/TabContentPanel.java new file mode 100644 index 0000000..1dc08a5 --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/TabContentPanel.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2018, Daniel Teo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package dev.thource.runelite.dudewheresmystuff; + +import lombok.extern.slf4j.Slf4j; + +import javax.swing.*; +import java.time.DateTimeException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.TextStyle; +import java.time.temporal.ChronoUnit; +import java.util.Locale; + +@Slf4j +public abstract class TabContentPanel extends JPanel +{ + private static final DateTimeFormatter DATETIME_FORMATTER_24H = DateTimeFormatter.ofPattern("HH:mm"); + private static final DateTimeFormatter DATETIME_FORMATTER_12H = DateTimeFormatter.ofPattern("h:mm a"); + + /** + * Gets the update interval of this panel, in units of 200 milliseconds + * (the plugin panel checks if its contents should be updated every 200 ms; + * this can be considered its "tick rate"). + */ + public abstract int getUpdateInterval(); + + public abstract void update(); + + public static String getFormattedEstimate(long remainingSeconds, TimeFormatMode mode) + { + DateTimeFormatter formatter = getDateTimeFormatter(mode); + + if (formatter == null) + { + StringBuilder sb = new StringBuilder("in "); + long duration = (remainingSeconds + 59) / 60; + long minutes = duration % 60; + long hours = (duration / 60) % 24; + long days = duration / (60 * 24); + if (days > 0) + { + sb.append(days).append("d "); + } + if (hours > 0) + { + sb.append(hours).append("h "); + } + if (minutes > 0) + { + sb.append(minutes).append("m "); + } + return sb.toString(); + } + else + { + try + { + StringBuilder sb = new StringBuilder(); + LocalDateTime endTime = LocalDateTime.now().plus(remainingSeconds, ChronoUnit.SECONDS); + LocalDateTime currentTime = LocalDateTime.now(); + if (endTime.getDayOfWeek() != currentTime.getDayOfWeek()) + { + sb.append(endTime.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.getDefault())).append(" "); + } + sb.append("at "); + sb.append(formatter.format(endTime)); + + return sb.toString(); + } + catch (DateTimeException e) + { + log.warn("error formatting absolute time: now + {}", remainingSeconds, e); + return "Invalid"; + } + } + } + + private static DateTimeFormatter getDateTimeFormatter(TimeFormatMode mode) + { + switch (mode) + { + case ABSOLUTE_12H: + return DATETIME_FORMATTER_12H; + case ABSOLUTE_24H: + return DATETIME_FORMATTER_24H; + default: + return null; + } + } +} diff --git a/src/main/java/dev/thource/runelite/dudewheresmystuff/TimeFormatMode.java b/src/main/java/dev/thource/runelite/dudewheresmystuff/TimeFormatMode.java new file mode 100644 index 0000000..1cdeb0d --- /dev/null +++ b/src/main/java/dev/thource/runelite/dudewheresmystuff/TimeFormatMode.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2020, Hydrox6 + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package dev.thource.runelite.dudewheresmystuff; + +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public enum TimeFormatMode +{ + RELATIVE("Relative"), + ABSOLUTE_12H("12 Hour"), + ABSOLUTE_24H("24 Hour"); + + private final String name; + + @Override + public String toString() + { + return name; + } +} diff --git a/src/main/resources/dev/thource/runelite/dudewheresmystuff/icon.png b/src/main/resources/dev/thource/runelite/dudewheresmystuff/icon.png new file mode 100644 index 0000000..a4b05c1 Binary files /dev/null and b/src/main/resources/dev/thource/runelite/dudewheresmystuff/icon.png differ diff --git a/src/test/java/dev/thource/runelite/dudewheresmystuff/DudeWheresMyStuffPluginTest.java b/src/test/java/dev/thource/runelite/dudewheresmystuff/DudeWheresMyStuffPluginTest.java new file mode 100644 index 0000000..63f84ab --- /dev/null +++ b/src/test/java/dev/thource/runelite/dudewheresmystuff/DudeWheresMyStuffPluginTest.java @@ -0,0 +1,13 @@ +package dev.thource.runelite.dudewheresmystuff; + +import net.runelite.client.RuneLite; +import net.runelite.client.externalplugins.ExternalPluginManager; + +public class DudeWheresMyStuffPluginTest +{ + public static void main(String[] args) throws Exception + { + ExternalPluginManager.loadBuiltin(DudeWheresMyStuffPlugin.class); + RuneLite.main(args); + } +} \ No newline at end of file