Skip to content

Commit

Permalink
added gain and percent
Browse files Browse the repository at this point in the history
  • Loading branch information
Thunderblade73 committed Jan 6, 2025
1 parent f90d105 commit 396328c
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,6 +66,10 @@ object ShTrack {
}

val arguments = listOf<CommandArgument<ContextObject>>(
CommandArgument("<> - Does save the tracker on game close", "-t") { _, c ->
c.shouldSave = true
0
},
CommandArgument(
"<> - Sets the tracking type to items",
"-i",
Expand Down Expand Up @@ -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
},
)
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,21 +26,24 @@ abstract class TrackingElement<T : Number> {
var shouldNotify = false
var shouldAutoDelete = false
var shouldSave = false
var showPercent = true
var showGain = true

abstract var current: T
abstract val target: T?

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)
}
Expand Down Expand Up @@ -65,9 +76,54 @@ abstract class TrackingElement<T : Number> {

abstract fun atAdd()

abstract fun generateLine(): List<Renderable>
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<Renderable> = 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<String> = listOf(
"$name §eTracker",
"§eHold §e§lLEFT CLICK §r§eto change order",
"§e§lRIGHT CLICK §r§eto §cdelete",
)

Expand All @@ -84,11 +140,15 @@ abstract class TrackingElement<T : Number> {
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<String, JsonElement>) {
shouldSave = true
shouldAutoDelete = read["shouldAutoDelete"]?.asBoolean ?: false
shouldNotify = read["shouldNotify"]?.asBoolean ?: false
showPercent = read["showPercent"]?.asBoolean ?: true
showGain = read["showGain"]?.asBoolean ?: true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.util.function.Predicate

class TrackingList : ArrayList<TrackingElement<*>>(), MutableList<TrackingElement<*>> {

var isActive = false
var isActive = true

fun activate() {
if (isActive) return
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/utils/NumberUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -272,4 +273,26 @@ object NumberUtil {
return interp
}

@Suppress("UNCHECKED_CAST")
operator fun <T : Number> 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 : Number> 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")
}
}

0 comments on commit 396328c

Please sign in to comment.