diff --git a/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ItemGroupElement.kt b/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ItemGroupElement.kt index 00b6ccd2e4fe..a7b30d2beb94 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ItemGroupElement.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ItemGroupElement.kt @@ -43,14 +43,10 @@ class ItemGroupElement( } } - override fun generateLine() = listOf( - Renderable.itemStack(group.icon.getItemStack()), - Renderable.string(group.name), - Renderable.string(current.toString() + ((target?.let { " / $it" }).orEmpty())), - ) + override val icon: Renderable get() = Renderable.itemStack(group.icon.getItemStack()) override fun itemChange(item: PrimitiveItemStack) { val multiple = group.items[item.internalName] ?: throw IllegalStateException("You should not be here!") - update(item.amount * multiple) + update((item.amount * multiple).toLong()) } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ItemTrackingElement.kt b/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ItemTrackingElement.kt index 45d7d999bec6..3924516e4af3 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ItemTrackingElement.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ItemTrackingElement.kt @@ -36,18 +36,14 @@ class ItemTrackingElement( override fun internalUpdate(amount: Number) { current += amount.toLong() if (target != null && current >= target) { - handleDone("$name §adone") + handleDone() } } - override fun generateLine() = listOf( - Renderable.itemStack(item.getItemStack()), - Renderable.string(item.itemName), - Renderable.string(current.toString() + ((target?.let { " / $it" }).orEmpty())), - ) + override val icon: Renderable get() = Renderable.itemStack(item.getItemStack()) override fun itemChange(item: PrimitiveItemStack) { - update(item.amount) + update(item.amount.toLong()) } companion object { diff --git a/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ItemsStackElement.kt b/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ItemsStackElement.kt index afabc9d0f210..02c5f8af5cdf 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ItemsStackElement.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ItemsStackElement.kt @@ -6,6 +6,7 @@ import at.hannibal2.skyhanni.utils.NEUInternalName import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.toInternalName import at.hannibal2.skyhanni.utils.NEUItems import at.hannibal2.skyhanni.utils.NEUItems.getItemStack +import at.hannibal2.skyhanni.utils.NumberUtil.percentWithColorCode import at.hannibal2.skyhanni.utils.PrimitiveItemStack import at.hannibal2.skyhanni.utils.renderables.Renderable import com.google.gson.JsonElement @@ -46,20 +47,17 @@ class ItemsStackElement( override fun internalUpdate(amount: Number) { current += amount.toLong() if (target != null && mappedCurrent >= target) { - handleDone("$name §adone") + handleDone() } } - override fun generateLine() = listOf( - Renderable.itemStack(main.getItemStack()), - Renderable.string(main.itemName), - - Renderable.string(mappedCurrent.toString() + ((target?.let { " / $it" }).orEmpty())), - ) + override val icon get() = Renderable.itemStack(main.getItemStack()) + override val percentText get() = if (showPercent && target != null) mappedCurrent.percentWithColorCode(target, 1) else "" + override val amount get() = Renderable.string(mappedCurrent.toString() + ((target?.let { " / $it" }).orEmpty())) override fun itemChange(item: PrimitiveItemStack) { val multiple = map[item.internalName] ?: throw IllegalStateException("You should not be here!") - update(item.amount * multiple) + update((item.amount * multiple).toLong()) } companion object { diff --git a/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/PowderTrackingElement.kt b/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/PowderTrackingElement.kt index f462bd8ac24f..e6572872bb89 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/PowderTrackingElement.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/PowderTrackingElement.kt @@ -13,15 +13,11 @@ class PowderTrackingElement(val type: HotmAPI.PowderType, override var current: override fun internalUpdate(amount: Number) { current += amount.toLong() if (target != null && current >= target) { - handleDone("${type.displayName} §adone") + handleDone() } } - override fun generateLine() = listOf( - Renderable.itemStack(type.icon), - Renderable.string(type.displayName), - Renderable.string(current.toString() + ((target?.let { " / $it" }).orEmpty())), - ) + override val icon: Renderable get() = Renderable.itemStack(type.icon) override fun similarElement(other: TrackingElement<*>): Boolean { if (other !is PowderTrackingElement) return false diff --git a/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ShTrack.kt b/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ShTrack.kt index 21e291a23a2f..460551fbaf1f 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ShTrack.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/ShTrack.kt @@ -11,6 +11,7 @@ import at.hannibal2.skyhanni.data.ItemAddManager import at.hannibal2.skyhanni.data.ProfileStorageData import at.hannibal2.skyhanni.data.SackAPI.getAmountInSacks import at.hannibal2.skyhanni.events.GuiRenderEvent +import at.hannibal2.skyhanni.events.HypixelJoinEvent import at.hannibal2.skyhanni.events.ItemAddEvent import at.hannibal2.skyhanni.events.ProfileJoinEvent import at.hannibal2.skyhanni.events.ProfileLeaveEvent @@ -65,6 +66,10 @@ object ShTrack { } val arguments = listOf>( + CommandArgument("<> - Does save the tracker on game close", "-t") { _, c -> + c.shouldSave = true + 0 + }, CommandArgument( "<> - Sets the tracking type to items", "-i", @@ -148,8 +153,12 @@ object ShTrack { c.multiItem = true 0 }, - CommandArgument("<> - Does save the tracker on game close", "-t") { _, c -> - c.shouldSave = true + CommandArgument("<> - Removes the percent value", "-np") { _, c -> + c.showPercent = false + 0 + }, + CommandArgument("<> - Removes the gain value", "-ng") { _, c -> + c.showGain = false 0 }, ) @@ -167,6 +176,8 @@ object ShTrack { var notify = false var multiItem = false var shouldSave = false + var showPercent = true + var showGain = true var state: StateType? = null set(value) { @@ -295,6 +306,8 @@ object ShTrack { result.shouldNotify = notify result.shouldAutoDelete = autoDelete result.shouldSave = shouldSave + result.showPercent = showPercent + result.showGain = showGain result.line = result.generateLine() val tracker = tracker ?: run { errorMessage = NullPointerException("tracker").message @@ -418,6 +431,14 @@ object ShTrack { tracker?.activate() } + @HandleEvent + fun onHypixelJoin(event: HypixelJoinEvent) { + // Clear out values that where loaded via gson + ProfileStorageData.playerSpecific?.profiles?.forEach { (_, profile) -> + profile.tracking.deactivate() + } + } + @SubscribeEvent fun onGuiRenderGuiOverlayRender(event: GuiRenderEvent) { if (!isEnabled()) return diff --git a/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/TrackingElement.kt b/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/TrackingElement.kt index c97c332364ad..3b5b69319689 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/TrackingElement.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/TrackingElement.kt @@ -3,8 +3,16 @@ package at.hannibal2.skyhanni.features.gui.shtrack import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.utils.KeyboardManager.isKeyClicked import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.NumberUtil.getZero +import at.hannibal2.skyhanni.utils.NumberUtil.percentWithColorCode +import at.hannibal2.skyhanni.utils.NumberUtil.plus +import at.hannibal2.skyhanni.utils.NumberUtil.toStringWithPlusAndColor +import at.hannibal2.skyhanni.utils.RenderUtils +import at.hannibal2.skyhanni.utils.SimpleTimeMark +import at.hannibal2.skyhanni.utils.SimpleTimeMark.Companion.fromNow import at.hannibal2.skyhanni.utils.SoundUtils import at.hannibal2.skyhanni.utils.renderables.Renderable +import at.hannibal2.skyhanni.utils.renderables.RenderableUtils import com.google.gson.JsonElement import com.google.gson.stream.JsonWriter import kotlin.time.Duration.Companion.seconds @@ -18,6 +26,8 @@ abstract class TrackingElement { var shouldNotify = false var shouldAutoDelete = false var shouldSave = false + var showPercent = true + var showGain = true abstract var current: T abstract val target: T? @@ -25,14 +35,15 @@ abstract class TrackingElement { abstract val name: String abstract val saveName: String - fun update(amount: Number) { - if (amount == 0) return + fun update(amount: T) { + if (amount == current.getZero()) return internalUpdate(amount) + updateGain(amount) line = generateLine() ShTrack.updateDisplay() } - fun handleDone(notify: String) { + fun handleDone(notify: String = "Goal of §a${target} $name §areached") { if (shouldNotify) { notify(notify) } @@ -65,9 +76,54 @@ abstract class TrackingElement { abstract fun atAdd() - abstract fun generateLine(): List + abstract val icon: Renderable + open val nameText: Renderable get() = Renderable.string(name) + open val amount: Renderable get() = Renderable.string(current.toString() + ((target?.let { " / $it" }).orEmpty())) + open val percentText get() = if (showPercent && target != null) current.percentWithColorCode(target ?: current, 1) else "" + + fun generateLine(): List = listOf( + icon, nameText, amount, Renderable.string(percentText), gainText, + ) + + private lateinit var gain: T + private var sinceGain = SimpleTimeMark.farPast() + + private fun updateGain(amount: T) { + sinceGain = 5.0.seconds.fromNow() + gain += amount + } + + val gainText: Renderable + get() { + if (sinceGain.isInPast()) { + gain = current.getZero() + return Renderable.placeholder(0, 0) + } + val base = Renderable.string(gain.toStringWithPlusAndColor()) + return Renderable.fixedSizeBox( + object : Renderable { + override val width = 0 + override val height = 0 + override val horizontalAlign = RenderUtils.HorizontalAlignment.LEFT + override val verticalAlign = RenderUtils.VerticalAlignment.TOP + + override fun render(posX: Int, posY: Int) { + if (sinceGain.isInPast()) { + gain = current.getZero() + return + } + RenderableUtils.renderString(gain.toStringWithPlusAndColor()) + } + + }, + base.height, + base.width + 2, + ) + } + open fun generateHover(): List = listOf( "$name §eTracker", + "§eHold §e§lLEFT CLICK §r§eto change order", "§e§lRIGHT CLICK §r§eto §cdelete", ) @@ -84,11 +140,15 @@ abstract class TrackingElement { out.name("current").value(current) out.name("shouldAutoDelete").value(shouldAutoDelete) out.name("shouldNotify").value(shouldNotify) + out.name("showPercent").value(showPercent) + out.name("showGain").value(showGain) } fun applyMetaOptions(read: Map) { shouldSave = true shouldAutoDelete = read["shouldAutoDelete"]?.asBoolean ?: false shouldNotify = read["shouldNotify"]?.asBoolean ?: false + showPercent = read["showPercent"]?.asBoolean ?: true + showGain = read["showGain"]?.asBoolean ?: true } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/TrackingList.kt b/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/TrackingList.kt index 4fc9e112b889..4c5c73341ba9 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/TrackingList.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/gui/shtrack/TrackingList.kt @@ -4,7 +4,7 @@ import java.util.function.Predicate class TrackingList : ArrayList>(), MutableList> { - var isActive = false + var isActive = true fun activate() { if (isActive) return diff --git a/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt b/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt index a19c49457a8c..0ad0f19b541e 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt @@ -165,6 +165,7 @@ object NumberUtil { } fun Number.toStringWithPlus() = (if (this.toDouble() >= 0.0) "+" else "") + this.toString() + fun Number.toStringWithPlusAndColor() = (if (this.toDouble() >= 0.0) "§a+" else "§c") + this.toString() private fun processDecimal(decimal: Int, lastNumber: Int, lastDecimal: Int) = if (lastNumber > decimal) { lastDecimal - decimal @@ -272,4 +273,26 @@ object NumberUtil { return interp } + @Suppress("UNCHECKED_CAST") + operator fun T.plus(other: T): T = when { + this is Long -> this.plus(other as Long) as T + this is Int -> this.plus(other as Int) as T + this is Short -> this.plus(other as Short) as T + this is Byte -> this.plus(other as Byte) as T + this is Double -> this.plus(other as Double) as T + this is Float -> this.plus(other as Float) as T + else -> throw TypeCastException("Not a number") + } + + /** Returns 0 as T*/ + @Suppress("UNCHECKED_CAST") + fun T.getZero(): T = when { + this is Long -> 0L as T + this is Int -> 0 as T + this is Short -> 0.toShort() as T + this is Byte -> 0.toByte() as T + this is Double -> 0.0 as T + this is Float -> 0.0f as T + else -> throw TypeCastException("Not a number") + } }