Skip to content

Commit

Permalink
Backend: Remove Neu Constant (#1191)
Browse files Browse the repository at this point in the history
Co-authored-by: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com>
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 12, 2024
1 parent 7960d2a commit 5ff5796
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 114 deletions.
1 change: 1 addition & 0 deletions .github/workflows/illegal-imports.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
# <directory without src/main/java prefix> <illegal import>

at/hannibal2/skyhanni/ scala.
at/hannibal2/skyhanni/ io.github.moulberry.notenoughupdates.util.Constants
at/hannibal2/skyhanni/ io.github.moulberry.notenoughupdates.events.SlotClickEvent
40 changes: 5 additions & 35 deletions src/main/java/at/hannibal2/skyhanni/api/GetFromSackAPI.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package at.hannibal2.skyhanni.api

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigManager
import at.hannibal2.skyhanni.data.jsonobjects.other.NeuSacksJson
import at.hannibal2.skyhanni.data.SackAPI
import at.hannibal2.skyhanni.events.GuiContainerEvent
import at.hannibal2.skyhanni.events.InventoryCloseEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.LorenzToolTipEvent
import at.hannibal2.skyhanni.events.MessageSendToServerEvent
import at.hannibal2.skyhanni.events.NeuRepositoryReloadEvent
import at.hannibal2.skyhanni.features.commands.tabcomplete.GetFromSacksTabComplete
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.ChatUtils.isCommand
import at.hannibal2.skyhanni.utils.ChatUtils.senderIsSkyhanni
import at.hannibal2.skyhanni.utils.ItemUtils.itemNameWithoutColor
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NEUInternalName
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
Expand All @@ -25,9 +22,7 @@ import at.hannibal2.skyhanni.utils.PrimitiveItemStack.Companion.makePrimitiveSta
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.fromJson
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import io.github.moulberry.notenoughupdates.util.Utils
import net.minecraft.inventory.Slot
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.Deque
Expand Down Expand Up @@ -75,11 +70,11 @@ object GetFromSackAPI {

private var lastItemStack: PrimitiveItemStack? = null

var sackListInternalNames = emptySet<String>()
private set
@Deprecated("", ReplaceWith("SackAPI.sackListInternalNames"))
val sackListInternalNames get() = SackAPI.sackListInternalNames

var sackListNames = emptySet<String>()
private set
@Deprecated("", ReplaceWith("SackAPI.sackListNames"))
val sackListNames get() = SackAPI.sackListNames

private fun addToQueue(items: List<PrimitiveItemStack>) = queue.addAll(items)

Expand Down Expand Up @@ -222,29 +217,4 @@ object GetFromSackAPI {
WRONG_AMOUNT,
INTERNAL_ERROR
}

@SubscribeEvent
fun onNeuRepoReload(event: NeuRepositoryReloadEvent) {
val data = event.getConstant("sacks") ?: ErrorManager.skyHanniError("NEU sacks data is null.")
try {
val sacksData = ConfigManager.gson.fromJson<NeuSacksJson>(data).sacks
val uniqueSackItems = mutableSetOf<NEUInternalName>()

sacksData.values.forEach { sackInfo ->
sackInfo.contents.forEach { content ->
uniqueSackItems.add(content)
}
}

sackListInternalNames = uniqueSackItems.map { it.asString() }.toSet()
sackListNames = uniqueSackItems.map { it.itemNameWithoutColor.uppercase() }.toSet()

} catch (e: Exception) {
ErrorManager.logErrorWithData(
e, "Error getting NEU sacks data, make sure your neu repo is updated.",
"sacksJson" to data
)
Utils.showOutdatedRepoNotification()
}
}
}
32 changes: 11 additions & 21 deletions src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package at.hannibal2.skyhanni.api

import at.hannibal2.skyhanni.data.ProfileStorageData
import at.hannibal2.skyhanni.data.jsonobjects.repo.neu.NeuSkillLevelJson
import at.hannibal2.skyhanni.events.ActionBarUpdateEvent
import at.hannibal2.skyhanni.events.ConfigLoadEvent
import at.hannibal2.skyhanni.events.DebugDataCollectEvent
import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent
import at.hannibal2.skyhanni.events.NeuRepositoryReloadEvent
import at.hannibal2.skyhanni.events.SecondPassedEvent
import at.hannibal2.skyhanni.events.SkillOverflowLevelupEvent
import at.hannibal2.skyhanni.features.skillprogress.SkillProgress
Expand All @@ -16,7 +17,6 @@ import at.hannibal2.skyhanni.features.skillprogress.SkillUtil.calculateOverFlow
import at.hannibal2.skyhanni.features.skillprogress.SkillUtil.getLevel
import at.hannibal2.skyhanni.features.skillprogress.SkillUtil.getLevelExact
import at.hannibal2.skyhanni.features.skillprogress.SkillUtil.getSkillInfo
import at.hannibal2.skyhanni.features.skillprogress.SkillUtil.levelArray
import at.hannibal2.skyhanni.features.skillprogress.SkillUtil.xpRequiredForLevel
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.ItemUtils.cleanName
Expand All @@ -30,11 +30,7 @@ import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.TabListData
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import com.google.gson.GsonBuilder
import com.google.gson.annotations.Expose
import com.google.gson.reflect.TypeToken
import io.github.moulberry.notenoughupdates.util.Constants
import io.github.moulberry.notenoughupdates.util.Utils
import net.minecraft.command.CommandBase
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.LinkedList
Expand Down Expand Up @@ -69,6 +65,7 @@ object SkillAPI {
val storage get() = ProfileStorageData.profileSpecific?.skillData
var exactLevelingMap = mapOf<Int, Int>()
var levelingMap = mapOf<Int, Int>()
var levelArray = listOf<Int>()
var activeSkill: SkillType? = null

// TODO Use a map maxSkillLevel and move it into the repo
Expand Down Expand Up @@ -135,14 +132,10 @@ object SkillAPI {
}

@SubscribeEvent
fun onConfigLoad(event: ConfigLoadEvent) {
val gson = GsonBuilder().create()
val xpList: List<Int> = gson.fromJson(
Utils.getElement(Constants.LEVELING, "leveling_xp").asJsonArray.toString(),
object : TypeToken<List<Int>>() {}.type
)
levelingMap = xpList.withIndex().associate { (index, xp) -> index to xp }
exactLevelingMap = xpList.withIndex().associate { (index, xp) -> xp to index }
fun onNEURepoReload(event: NeuRepositoryReloadEvent) {
levelArray = event.readConstant<NeuSkillLevelJson>("leveling").levelingXp
levelingMap = levelArray.withIndex().associate { (index, xp) -> index to xp }
exactLevelingMap = levelArray.withIndex().associate { (index, xp) -> xp to index }
}

@SubscribeEvent
Expand Down Expand Up @@ -190,8 +183,7 @@ object SkillAPI {
val splitProgress = progress.split("/")
val currentXp = splitProgress.first().formatLong()
val neededXp = splitProgress.last().formatLong()
val levelingArray = levelArray()
val levelXp = calculateLevelXp(levelingArray, skillLevel - 1).toLong()
val levelXp = calculateLevelXp(skillLevel - 1).toLong()

skillInfo?.apply {
this.currentXp = currentXp
Expand Down Expand Up @@ -310,9 +302,8 @@ object SkillAPI {
}
val existingLevel = getSkillInfo(skillType) ?: SkillInfo()
val xpPercentage = matcher.group("progress").formatDouble()
val levelingArray = levelArray()
val levelXp = calculateLevelXp(levelingArray, existingLevel.level - 1)
val nextLevelDiff = levelingArray[tablistLevel]?.asDouble ?: 7_600_000.0
val levelXp = calculateLevelXp(existingLevel.level - 1)
val nextLevelDiff = levelArray.getOrNull(tablistLevel)?.toDouble() ?: 7_600_000.0
val nextLevelProgress = nextLevelDiff * xpPercentage / 100
val totalXp = levelXp + nextLevelProgress
val (_, currentOverflow, currentMaxOverflow, totalOverflow) = getSkillInfo(
Expand Down Expand Up @@ -341,8 +332,7 @@ object SkillAPI {
val currentXp = matcher.group("current").formatLong()
val maxXp = matcher.group("needed").formatLong()
val level = getLevelExact(maxXp)
val levelingArray = levelArray()
val levelXp = calculateLevelXp(levelingArray, level - 1).toLong() + currentXp
val levelXp = calculateLevelXp(level - 1).toLong() + currentXp
val (currentLevel, currentOverflow, currentMaxOverflow, totalOverflow) = getSkillInfo(
level,
currentXp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class ConfigManager {
}

override fun read(reader: JsonReader): LorenzRarity {
return LorenzRarity.valueOf(reader.nextString().uppercase())
return LorenzRarity.valueOf(reader.nextString().uppercase().replace(" ", "_"))
}
}.nullSafe())
.registerTypeAdapter(IslandType::class.java, object : TypeAdapter<IslandType>() {
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/data/SackAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package at.hannibal2.skyhanni.data
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigFileType
import at.hannibal2.skyhanni.config.features.inventory.SackDisplayConfig.PriceFrom
import at.hannibal2.skyhanni.data.jsonobjects.repo.neu.NeuSacksJson
import at.hannibal2.skyhanni.events.InventoryCloseEvent
import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.NeuRepositoryReloadEvent
import at.hannibal2.skyhanni.events.SackChangeEvent
import at.hannibal2.skyhanni.events.SackDataUpdateEvent
import at.hannibal2.skyhanni.features.fishing.FishingAPI
Expand All @@ -14,6 +16,7 @@ import at.hannibal2.skyhanni.features.inventory.SackDisplay
import at.hannibal2.skyhanni.utils.CollectionUtils.editCopy
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.ItemUtils.itemNameWithoutColor
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.NEUInternalName
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
Expand Down Expand Up @@ -62,6 +65,12 @@ object SackAPI {
val gemstoneItem = mutableMapOf<String, SackGemstone>()
private val stackList = mutableMapOf<Int, ItemStack>()

var sackListInternalNames = emptySet<String>()
private set

var sackListNames = emptySet<String>()
private set

@SubscribeEvent
fun onInventoryClose(event: InventoryCloseEvent) {
inSackInventory = false
Expand Down Expand Up @@ -235,6 +244,21 @@ object SackAPI {
}
}

@SubscribeEvent
fun onNeuRepoReload(event: NeuRepositoryReloadEvent) {
val sacksData = event.readConstant<NeuSacksJson>("sacks").sacks
val uniqueSackItems = mutableSetOf<NEUInternalName>()

sacksData.values.forEach { sackInfo ->
sackInfo.contents.forEach { content ->
uniqueSackItems.add(content)
}
}

sackListInternalNames = uniqueSackItems.map { it.asString() }.toSet()
sackListNames = uniqueSackItems.map { it.itemNameWithoutColor.uppercase() }.toSet()
}

private fun updateSacks(changes: SackChangeEvent) {
sackData = ProfileStorageData.sackProfiles?.sackContents ?: return

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package at.hannibal2.skyhanni.data.jsonobjects.repo.neu

import at.hannibal2.skyhanni.utils.NEUInternalName
import com.google.gson.annotations.Expose

data class NeuRNGScore(
@Expose val catacombs: Map<String, Map<NEUInternalName, Long>>,
@Expose val slayer: Map<String, Map<NEUInternalName, Long>>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package at.hannibal2.skyhanni.data.jsonobjects.repo.neu;

import at.hannibal2.skyhanni.utils.LorenzRarity
import at.hannibal2.skyhanni.utils.NEUInternalName
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName

data class NeuReforgeStoneJson(
@Expose val internalName: NEUInternalName,
@Expose val reforgeName: String,
@Expose @SerializedName("itemTypes") val rawItemTypes: Any,
@Expose val requiredRarities: List<LorenzRarity>,
@Expose val reforgeCosts: Map<LorenzRarity, Long>,
@Expose val reforgeStats: Map<LorenzRarity, Map<String, Double>>,
@Expose @SerializedName("reforgeAbility") val rawReforgeAbility: Any?,
) {

private lateinit var reforgeAbilityField: Map<LorenzRarity, String>

val reforgeAbility
get() = if (this::reforgeAbilityField.isInitialized) reforgeAbilityField
else {
reforgeAbilityField = when (this.rawReforgeAbility) {
is String -> {
this.requiredRarities.associateWith { this.rawReforgeAbility }
}

is Map<*, *> -> (this.rawReforgeAbility as? Map<String, String>)?.mapKeys {
LorenzRarity.valueOf(
it.key.uppercase().replace(" ", "_")
)
} ?: emptyMap()

else -> emptyMap()
}
reforgeAbilityField
}

/* used in ReforgeAPI which isn't in beta yet
val itemType: Pair<String, List<NEUInternalName>> by lazy {
val any = this.rawItemTypes
return@lazy when (any) {
is String -> {
any.replace("/", "_AND_").uppercase() to emptyList()
}
is Map<*, *> -> {
val type = "SPECIAL_ITEMS"
val map = any as? Map<String, List<String>> ?: return@lazy type to emptyList()
val internalNames = map["internalName"]?.map { it.asInternalName() } ?: emptyList()
val itemType = map["itemid"]?.map {
NEUItems.getInternalNamesForItemId(Item.getByNameOrId(it))
}?.flatten()
?: emptyList()
type to (internalNames + itemType)
}
else -> throw IllegalStateException()
}
}
*/
}

Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package at.hannibal2.skyhanni.data.jsonobjects.other
package at.hannibal2.skyhanni.data.jsonobjects.repo.neu

import at.hannibal2.skyhanni.utils.NEUInternalName
import com.google.gson.annotations.Expose


data class NeuSacksJson(
@Expose val sacks: Map<String, SackInfo>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package at.hannibal2.skyhanni.data.jsonobjects.repo.neu

import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName

data class NeuSkillLevelJson(
@Expose @SerializedName("leveling_xp") val levelingXp: List<Int>
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
package at.hannibal2.skyhanni.events

import at.hannibal2.skyhanni.config.ConfigManager
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.NEUItems.manager
import at.hannibal2.skyhanni.utils.fromJson
import com.google.gson.JsonObject
import com.google.gson.JsonSyntaxException
import java.io.File

class NeuRepositoryReloadEvent : LorenzEvent() {
fun getConstant(file: String): JsonObject? {
return manager.getJsonFromFile(File(manager.repoLocation, "constants/$file.json"))
}

inline fun <reified T : Any> readConstant(file: String): T {
val data = getConstant(file) ?: ErrorManager.skyHanniError("$file failed to load from neu repo!")
return try {
ConfigManager.gson.fromJson<T>(data)
} catch (e: JsonSyntaxException) {
ErrorManager.logErrorWithData(
e, "$file failed to read from neu repo!",
"data" to data
)
throw e
}
}
}
Loading

0 comments on commit 5ff5796

Please sign in to comment.