Skip to content

Commit

Permalink
Merge pull request #456 from j10a1n15/technical/island-cache
Browse files Browse the repository at this point in the history
Backend: Cache Current Island
  • Loading branch information
Su386yt authored Aug 12, 2024
2 parents b4daa44 + 1b3f4c3 commit 380fb2b
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ import me.partlysanestudios.partlysaneskies.features.themes.ThemeManager
import me.partlysanestudios.partlysaneskies.render.gui.hud.BannerRenderer
import me.partlysanestudios.partlysaneskies.render.gui.hud.cooldown.CooldownManager
import me.partlysanestudios.partlysaneskies.utils.ChatUtils.sendClientMessage
import me.partlysanestudios.partlysaneskies.utils.HypixelUtils
import me.partlysanestudios.partlysaneskies.utils.SystemUtils.log
import net.minecraft.client.Minecraft
import net.minecraft.event.ClickEvent
Expand Down Expand Up @@ -242,6 +243,7 @@ class PartlySaneSkies {

// Registers all the events
registerEvent(this)
registerEvent(HypixelUtils)
registerEvent(PartyManager())
registerEvent(PartyFriendManager())
registerEvent(MinionData())
Expand Down Expand Up @@ -399,10 +401,10 @@ class PartlySaneSkies {
PetData.tick()
VisitorLogbookData.scanForVisitors()
HealthAlert.checkPlayerTick()
RequiredSecretsFound.tick()
NoCookieWarning.checkCoinsTick()
Prank.checkPrankTick()
AuctionHouseGui.tick()
EventManager.tick()
}

@SubscribePSSEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import me.partlysanestudios.partlysaneskies.data.skyblockdata.Rarity
import me.partlysanestudios.partlysaneskies.data.skyblockdata.Rarity.Companion.getRarityFromColorCode
import me.partlysanestudios.partlysaneskies.events.SubscribePSSEvent
import me.partlysanestudios.partlysaneskies.events.minecraft.PSSChatEvent
import me.partlysanestudios.partlysaneskies.events.minecraft.TablistUpdateEvent
import me.partlysanestudios.partlysaneskies.utils.MathUtils
import me.partlysanestudios.partlysaneskies.utils.MinecraftUtils
import me.partlysanestudios.partlysaneskies.utils.MinecraftUtils.containerInventory
import me.partlysanestudios.partlysaneskies.utils.StringUtils.nextAfter
import me.partlysanestudios.partlysaneskies.utils.StringUtils.removeColorCodes
Expand All @@ -41,7 +41,6 @@ object PetData {

fun tick() {
parsePetGuiForCache()
parsePetFromTablist()
if (MathUtils.onCooldown(lastSaveTime, (60 * 1000L * .25).toLong())) {
return
}
Expand Down Expand Up @@ -164,10 +163,9 @@ object PetData {
}
}

private fun parsePetFromTablist() {
val tablist = MinecraftUtils.getTabList()

val pet = tablist.nextAfter("§e§lPet:")?.removeResets()?.trim() ?: return
@SubscribePSSEvent
fun parsePetFromTablist(event: TablistUpdateEvent) {
val pet = event.list.nextAfter("§e§lPet:")?.removeResets()?.trim() ?: return

petNameRegex.find(pet)?.let {
val (petLevel, colorCode, petName) = it.destructured
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

package me.partlysanestudios.partlysaneskies.data.skyblockdata

import me.partlysanestudios.partlysaneskies.utils.MinecraftUtils
import me.partlysanestudios.partlysaneskies.utils.StringUtils.removeColorCodes
import me.partlysanestudios.partlysaneskies.utils.HypixelUtils

enum class IslandType(val islandName: String) {
HUB("Hub"),
Expand All @@ -32,26 +31,13 @@ enum class IslandType(val islandName: String) {
NONE(""),
;

fun onIsland() = this == IslandType.getCurrentIsland()
fun onIsland() = this == HypixelUtils.currentIsland

companion object {
/**
* Gets the current island type from the tablist
* @return The current island type
* @param islandTypes The island to check for
* @return True if the player is on any of the specified islands
*/
fun getCurrentIsland(): IslandType {
for (line in MinecraftUtils.getTabList()) {
if (line.removeColorCodes().startsWith("Area: ") || line.removeColorCodes().startsWith("Dungeon: ")) {
val islandName = line.removeColorCodes()
.replace("Area: ", "")
.replace("Dungeon: ", "")
.trim()

return IslandType.entries.firstOrNull { it.islandName.equals(islandName, ignoreCase = true) } ?: NONE
}
}

return NONE
}
fun onIslands(vararg islandTypes: IslandType) = islandTypes.any { it.onIsland() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
package me.partlysanestudios.partlysaneskies.events

import me.partlysanestudios.partlysaneskies.events.minecraft.PSSChatEvent
import me.partlysanestudios.partlysaneskies.events.minecraft.TablistUpdateEvent
import me.partlysanestudios.partlysaneskies.events.minecraft.render.RenderWaypointEvent
import me.partlysanestudios.partlysaneskies.events.skyblock.dungeons.DungeonEndEvent
import me.partlysanestudios.partlysaneskies.events.skyblock.dungeons.DungeonStartEvent
import me.partlysanestudios.partlysaneskies.events.skyblock.mining.MinesEvent
import me.partlysanestudios.partlysaneskies.utils.MinecraftUtils
import me.partlysanestudios.partlysaneskies.utils.SystemUtils.log
import net.minecraftforge.client.event.ClientChatReceivedEvent
import net.minecraftforge.client.event.RenderWorldLastEvent
Expand All @@ -22,6 +24,9 @@ import kotlin.reflect.full.hasAnnotation
import kotlin.reflect.full.memberFunctions

object EventManager {

private var oldTablist = emptyList<String>()

internal val registeredFunctions = HashMap<KClass<*>, ArrayList<EventFunction>>()

fun register(obj: Any) {
Expand Down Expand Up @@ -49,6 +54,15 @@ object EventManager {
}
}

fun tick() {
val tablist = MinecraftUtils.getTabList()

if (tablist != oldTablist) {
TablistUpdateEvent.onUpdate(registeredFunctions[TablistUpdateEvent::class] ?: ArrayList(), tablist)
oldTablist = tablist
}
}

@SubscribeEvent
fun onScreenRender(event: RenderWorldLastEvent) {
RenderWaypointEvent.onEventCall(event.partialTicks, registeredFunctions[RenderWaypointEvent::class] ?: ArrayList())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.partlysanestudios.partlysaneskies.events.minecraft

import me.partlysanestudios.partlysaneskies.events.EventManager

class TablistUpdateEvent(val list: List<String>) {
companion object {
internal fun onUpdate(functionList: List<EventManager.EventFunction>, list: List<String>) {
functionList.forEach {
try {
it.function.call(it.obj, TablistUpdateEvent(list))
} catch (exception: Exception) {
exception.printStackTrace()
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import me.partlysanestudios.partlysaneskies.PartlySaneSkies.Companion.config
import me.partlysanestudios.partlysaneskies.PartlySaneSkies.Companion.minecraft
import me.partlysanestudios.partlysaneskies.PartlySaneSkies.Companion.time
import me.partlysanestudios.partlysaneskies.data.cache.StatsData
import me.partlysanestudios.partlysaneskies.data.skyblockdata.IslandType
import me.partlysanestudios.partlysaneskies.data.skyblockdata.Rarity
import me.partlysanestudios.partlysaneskies.events.SubscribePSSEvent
import me.partlysanestudios.partlysaneskies.events.minecraft.PSSChatEvent
Expand All @@ -30,6 +29,7 @@ import me.partlysanestudios.partlysaneskies.render.gui.hud.PSSBanner
import me.partlysanestudios.partlysaneskies.render.waypoint.Waypoint
import me.partlysanestudios.partlysaneskies.system.SystemNotification
import me.partlysanestudios.partlysaneskies.utils.ChatUtils.sendClientMessage
import me.partlysanestudios.partlysaneskies.utils.HypixelUtils
import me.partlysanestudios.partlysaneskies.utils.MinecraftUtils.containerInventory
import me.partlysanestudios.partlysaneskies.utils.MinecraftUtils.getItemstackList
import me.partlysanestudios.partlysaneskies.utils.MinecraftUtils.xSize
Expand Down Expand Up @@ -85,7 +85,7 @@ object DebugKey {
}

if (config.debugPrintCurrentLocationFromIslandType) {
sendClientMessage("Island Type: ${IslandType.getCurrentIsland()}")
sendClientMessage("Island Type: ${HypixelUtils.currentIsland}")
}

if (config.debugLogCachedF7Puzzles) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,56 @@ package me.partlysanestudios.partlysaneskies.features.dungeons

import me.partlysanestudios.partlysaneskies.PartlySaneSkies.Companion.config
import me.partlysanestudios.partlysaneskies.PartlySaneSkies.Companion.minecraft
import me.partlysanestudios.partlysaneskies.PartlySaneSkies.Companion.time
import me.partlysanestudios.partlysaneskies.data.skyblockdata.IslandType
import me.partlysanestudios.partlysaneskies.events.SubscribePSSEvent
import me.partlysanestudios.partlysaneskies.events.minecraft.TablistUpdateEvent
import me.partlysanestudios.partlysaneskies.events.skyblock.dungeons.DungeonStartEvent
import me.partlysanestudios.partlysaneskies.render.gui.hud.BannerRenderer.renderNewBanner
import me.partlysanestudios.partlysaneskies.render.gui.hud.PSSBanner
import me.partlysanestudios.partlysaneskies.utils.HypixelUtils.isSkyblock
import me.partlysanestudios.partlysaneskies.utils.MinecraftUtils.getTabList
import net.minecraft.client.Minecraft
import net.minecraft.client.audio.PositionedSoundRecord
import net.minecraft.util.ResourceLocation

object RequiredSecretsFound {
private var alreadySendThisRun = false
private var lastCheckTime = time

@SubscribePSSEvent
fun onDungeonStart(event: DungeonStartEvent?) {
alreadySendThisRun = false
}

fun tick() {
if (!isSkyblock()) {
return
}
if (!IslandType.CATACOMBS.onIsland()) {
return
}
if (alreadySendThisRun) {
return
@SubscribePSSEvent
fun onTablistUpdate(event: TablistUpdateEvent) {
if (!isSkyblock()) return
if (!IslandType.CATACOMBS.onIsland()) return

if (alreadySendThisRun) return

if (event.list.none { it.contains("Secrets Found: §r§a") }) return

if (config.secretsBanner) {
renderNewBanner(
PSSBanner(
"Required Secrets Found!",
(config.secretsBannerTime * 1000).toLong(),
3.0f,
config.secretsBannerColor.toJavaColor(),
),
)
}
if (lastCheckTime + 100 > time) { // checks every 100ms
return
if (config.secretsChatMessage) {
Minecraft.getMinecraft().thePlayer.sendChatMessage("/pc " + config.secretsChatMessageString)
}
lastCheckTime = time

for (line in getTabList()) {
if (line.contains("Secrets Found: §r§a")) {
if (config.secretsBanner) {
renderNewBanner(
PSSBanner(
"Required Secrets Found!",
(config.secretsBannerTime * 1000).toLong(),
3.0f,
config.secretsBannerColor.toJavaColor(),
),
)
}
if (config.secretsChatMessage) {
Minecraft.getMinecraft().thePlayer.sendChatMessage("/pc " + config.secretsChatMessageString)
}
if (config.secretsSound) {
if (config.secretsAirRaidSiren) {
minecraft.soundHandler.playSound(PositionedSoundRecord.create(ResourceLocation("partlysaneskies", "airraidsiren")))
} else {
minecraft.soundHandler.playSound(PositionedSoundRecord.create(ResourceLocation("partlysaneskies", "bell")))
}
}
alreadySendThisRun = true
break
}
if (config.secretsSound) {
minecraft.soundHandler.playSound(
if (config.secretsAirRaidSiren) {
PositionedSoundRecord.create(ResourceLocation("partlysaneskies", "airraidsiren"))
} else {
PositionedSoundRecord.create(ResourceLocation("partlysaneskies", "bell"))
},
)
}
alreadySendThisRun = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object WrongToolCropWarning {
fun onBlockBreak(event: PlayerBreakBlockEvent) {
if (!config.wrongToolForCropEnabled) return
if (onCooldown(lastWarnTime, (config.wrongToolForCropCooldown * 1000).toLong())) return
if (IslandType.CATACOMBS.onIsland()) return
if (!IslandType.onIslands(IslandType.GARDEN, IslandType.FARMING_ISLAND, IslandType.HUB, IslandType.CRIMSON_ISLE)) return

val block = minecraft.theWorld.getBlockState(event.point.toBlockPos())?.block
val unlocalizedName = block?.unlocalizedName ?: return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package me.partlysanestudios.partlysaneskies.utils

import me.partlysanestudios.partlysaneskies.PartlySaneSkies
import me.partlysanestudios.partlysaneskies.data.skyblockdata.IslandType
import me.partlysanestudios.partlysaneskies.events.SubscribePSSEvent
import me.partlysanestudios.partlysaneskies.events.minecraft.TablistUpdateEvent
import me.partlysanestudios.partlysaneskies.utils.StringUtils.removeColorCodes
import me.partlysanestudios.partlysaneskies.utils.StringUtils.stripLeading
import me.partlysanestudios.partlysaneskies.utils.StringUtils.stripTrailing
Expand All @@ -15,6 +17,9 @@ import net.minecraft.nbt.NBTTagCompound
import java.util.Locale

object HypixelUtils {

var currentIsland: IslandType? = null

// Returns if the current gamemode is skyblock
fun isSkyblock(): Boolean = MinecraftUtils.getScoreboardName().lowercase(Locale.getDefault()).contains("skyblock")

Expand Down Expand Up @@ -134,6 +139,17 @@ object HypixelUtils {
*/
fun ItemStack.getItemAttributes(): NBTTagCompound? = this.tagCompound?.getCompoundTag("ExtraAttributes")

fun inAdvancedMiningIsland() =
IslandType.DWARVEN_MINES.onIsland() || IslandType.CRYSTAL_HOLLOWS.onIsland() || IslandType.MINESHAFT.onIsland()
fun inAdvancedMiningIsland() = IslandType.onIslands(IslandType.DWARVEN_MINES, IslandType.CRYSTAL_HOLLOWS, IslandType.MINESHAFT)


@SubscribePSSEvent
fun onTablistUpdate(event: TablistUpdateEvent) {
currentIsland = event.list
.map { it.removeColorCodes().trim() }
.firstOrNull { it.startsWith("Area: ") || it.startsWith("Dungeon: ") }
?.let { line ->
val islandName = line.replace("Area: ", "").replace("Dungeon: ", "").trim()
IslandType.entries.firstOrNull { it.islandName.equals(islandName, ignoreCase = true) }
}
}
}

0 comments on commit 380fb2b

Please sign in to comment.