Skip to content

Commit

Permalink
fix: performance issue caused by Enum.values()
Browse files Browse the repository at this point in the history
  • Loading branch information
My-Name-Is-Jeff committed Dec 14, 2023
1 parent 74c4a29 commit 42a4efe
Show file tree
Hide file tree
Showing 28 changed files with 101 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ object HollowWaypointCommand : BaseCommand("skytilshollowwaypoint", listOf("sthw
override fun processCommand(player: EntityPlayerSP, args: Array<String>) {
if (args.isEmpty()) {
val message = UMessage("$prefix §eWaypoints:\n")
for (loc in MiningFeatures.CrystalHollowsMap.Locations.values()) {
for (loc in MiningFeatures.CrystalHollowsMap.Locations.entries) {
if (!loc.loc.exists()) continue
message.append("${loc.displayName} ")
message.append(copyMessage("${loc.cleanName}: ${loc.loc}"))
Expand Down Expand Up @@ -77,7 +77,7 @@ object HollowWaypointCommand : BaseCommand("skytilshollowwaypoint", listOf("sthw
y = match.groups["y"]!!.value.toDouble()
z = match.groups["z"]!!.value.toDouble()
}
val internalLoc = MiningFeatures.CrystalHollowsMap.Locations.values().find { it.id == loc }?.loc
val internalLoc = MiningFeatures.CrystalHollowsMap.Locations.entries.find { it.id == loc }?.loc
if (internalLoc != null) {
internalLoc.locX = (x - 200).coerceIn(0.0, 624.0)
internalLoc.locY = y
Expand All @@ -91,7 +91,7 @@ object HollowWaypointCommand : BaseCommand("skytilshollowwaypoint", listOf("sthw
"remove", "delete" -> {
if (args.size >= 2) {
val name = args.drop(1).joinToString(" ")
if (MiningFeatures.CrystalHollowsMap.Locations.values()
if (MiningFeatures.CrystalHollowsMap.Locations.entries
.find { it.id == name }?.loc?.reset() != null
)
UChat.chat("$successPrefix §aSuccessfully removed waypoint ${name}!")
Expand All @@ -104,7 +104,7 @@ object HollowWaypointCommand : BaseCommand("skytilshollowwaypoint", listOf("sthw
}

"clear" -> {
MiningFeatures.CrystalHollowsMap.Locations.values().forEach { it.loc.reset() }
MiningFeatures.CrystalHollowsMap.Locations.entries.forEach { it.loc.reset() }
MiningFeatures.waypoints.clear()
UChat.chat("$successPrefix §aSuccessfully cleared all waypoints.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ object UpdateChecker {
val specialVersionType by lazy {
val typeString = matched!!.groups["type"]?.value ?: return@lazy UpdateType.RELEASE

return@lazy UpdateType.values().find { typeString == it.prefix } ?: UpdateType.UNKNOWN
return@lazy UpdateType.entries.find { typeString == it.prefix } ?: UpdateType.UNKNOWN
}
val specialVersion by lazy {
if (specialVersionType == UpdateType.RELEASE) return@lazy null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ object ChestProfit {
val inv = event.container.lowerChestInventory

if (event.chestName == "Croesus") {
DungeonChest.values().forEach(DungeonChest::reset)
DungeonChest.entries.forEach(DungeonChest::reset)
return
}

Expand Down Expand Up @@ -196,7 +196,7 @@ object ChestProfit {

@SubscribeEvent
fun onWorldChange(event: WorldEvent.Load) {
DungeonChest.values().forEach(DungeonChest::reset)
DungeonChest.entries.forEach(DungeonChest::reset)
rerollBypass = false
}

Expand Down Expand Up @@ -247,7 +247,7 @@ object ChestProfit {
companion object {
fun getFromName(name: String?): DungeonChest? {
if (name.isNullOrBlank()) return null
return values().find {
return entries.find {
it.displayText == name
}
}
Expand All @@ -261,7 +261,7 @@ object ChestProfit {
val leftAlign = scaleX < sr.scaledWidth / 2f
GlStateManager.color(1f, 1f, 1f, 1f)
GlStateManager.disableLighting()
DungeonChest.values().filter { it.items.isNotEmpty() }.forEachIndexed { i, chest ->
DungeonChest.entries.filter { it.items.isNotEmpty() }.forEachIndexed { i, chest ->
val profit = chest.value - chest.price
ScreenRenderer.fontRenderer.drawString(
"${chest.displayText}§f: §${(if (profit > 0) "a" else "c")}${NumberUtil.format(profit.toLong())}",
Expand All @@ -276,11 +276,11 @@ object ChestProfit {
}

override fun demoRender() {
RenderUtil.drawAllInList(this, DungeonChest.values().map { "${it.displayText}: §a+300M" })
RenderUtil.drawAllInList(this, DungeonChest.entries.map { "${it.displayText}: §a+300M" })
}

override val height: Int
get() = ScreenRenderer.fontRenderer.FONT_HEIGHT * DungeonChest.values().size
get() = ScreenRenderer.fontRenderer.FONT_HEIGHT * DungeonChest.entries.size
override val width: Int
get() = ScreenRenderer.fontRenderer.getStringWidth("Obsidian Chest: 300M")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ object MasterMode7Features {
}
}
if ((event.pos.y == 18 || event.pos.y == 19) && event.update.block === Blocks.air && event.old.block === Blocks.stone_slab) {
val dragon = WitherKingDragons.values().find { it.bottomChin == event.pos } ?: return
val dragon = WitherKingDragons.entries.find { it.bottomChin == event.pos } ?: return
dragon.isDestroyed = true
}
}
Expand All @@ -95,14 +95,14 @@ object MasterMode7Features {
val z = event.packet.func_149049_f() / 32.0
if (x % 1 != 0.0 || y % 1 != 0.0 || z % 1 != 0.0) return
val drag =
WitherKingDragons.values().find { it.blockPos.x == x.toInt() && it.blockPos.z == z.toInt() } ?: return
WitherKingDragons.entries.find { it.blockPos.x == x.toInt() && it.blockPos.z == z.toInt() } ?: return
if (spawningDragons.add(drag)) {
printDevMessage("${drag.name} spawning $x $y $z", "witherkingdrags")
}
} else if (event.packet is S2APacketParticles) {
event.packet.apply {
if (count != 20 || y != WitherKingDragons.particleYConstant || type != EnumParticleTypes.FLAME || xOffset != 2f || yOffset != 3f || zOffset != 2f || speed != 0f || !isLongDistance || x % 1 != 0.0 || z % 1 != 0.0) return
val owner = WitherKingDragons.values().find {
val owner = WitherKingDragons.entries.find {
it.particleLocation.x == x.toInt() && it.particleLocation.z == z.toInt()
} ?: return
if (owner !in dragonSpawnTimes) {
Expand All @@ -118,7 +118,7 @@ object MasterMode7Features {
fun onMobSpawned(entity: Entity) {
if (DungeonTimer.phase4ClearTime != -1L && entity is EntityDragon) {
val type =
dragonMap[entity.entityId] ?: WitherKingDragons.values()
dragonMap[entity.entityId] ?: WitherKingDragons.entries
.minByOrNull { entity.getXZDistSq(it.blockPos) } ?: return
(entity as ExtensionEntityLivingBase).skytilsHook.colorMultiplier = type.color
(entity as ExtensionEntityLivingBase).skytilsHook.masterDragonType = type
Expand All @@ -143,7 +143,7 @@ object MasterMode7Features {
killedDragons.clear()
dragonMap.clear()
glowstones.clear()
WitherKingDragons.values().forEach { it.isDestroyed = false }
WitherKingDragons.entries.forEach { it.isDestroyed = false }
}

@SubscribeEvent
Expand Down Expand Up @@ -194,7 +194,7 @@ object MasterMode7Features {
@SubscribeEvent
fun onRenderWorld(event: RenderWorldLastEvent) {
if (Skytils.config.showWitherKingStatueBox && DungeonTimer.phase4ClearTime != -1L) {
for (drag in WitherKingDragons.values()) {
for (drag in WitherKingDragons.entries) {
if (drag.isDestroyed) continue
RenderUtil.drawOutlinedBoundingBox(drag.bb, drag.color, 3.69f, event.partialTicks)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ object PartyFinderStats {

private val partyFinderRegex = Regex(
"^Party Finder > (?<name>\\w+) joined the dungeon group! \\((?<class>${
DungeonClass.values().joinToString("|") { it.className }
DungeonClass.entries.joinToString("|") { it.className }
}) Level (?<classLevel>\\d+)\\)$"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ object WaterBoardSolver {
fun onRenderWorld(event: RenderWorldLastEvent) {
if (!Skytils.config.waterBoardSolver || !DungeonListener.missingPuzzles.contains("Water Board")) return
if (chestPos == null || roomFacing == null || variant == -1) return
val leverStates = LeverBlock.values().associateWithTo(EnumMap(LeverBlock::class.java)) {
val leverStates = LeverBlock.entries.associateWithTo(EnumMap(LeverBlock::class.java)) {
getLeverToggleState(it.leverPos)
}
val renderTimes = HashMap<LeverBlock, Int>()
var matching = 0
val matrixStack = UMatrixStack()
for (color in WoolColor.values()) {
for (color in WoolColor.entries) {
val renderColor = Color(color.dyeColor.mapColor.colorValue).brighter()
if (color.isExtended) {
val solution = solutions[color] ?: continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object SelectAllColorSolver {
"Select all the"
)
) {
val promptColor = EnumDyeColor.values().find {
val promptColor = EnumDyeColor.entries.find {
event.chestName.contains(it.getName().replace("_", " ").uppercase())
}?.unlocalizedName
if (promptColor != colorNeeded) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ object GriffinBurrows {
companion object {
fun getParticleType(packet: S2APacketParticles): ParticleType? {
if (!packet.isLongDistance) return null
for (type in values()) {
for (type in entries) {
if (type.check(packet)) {
return type
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object ChatTabs {
if (style.chatTabType == null) {
val cc = event.packet.chatComponent
val formatted = cc.formattedText
style.chatTabType = ChatTab.values().filter { it.isValid(cc, formatted) }.toTypedArray()
style.chatTabType = ChatTab.entries.filter { it.isValid(cc, formatted) }.toTypedArray()
}
}

Expand All @@ -73,7 +73,7 @@ object ChatTabs {
style as ExtensionChatStyle
if (style.chatTabType == null) {
style.chatTabType =
ChatTab.values().filter { it.isValid(component, component.formattedText) }.toTypedArray()
ChatTab.entries.filter { it.isValid(component, component.formattedText) }.toTypedArray()
}
return style.chatTabType!!.contains(selectedTab)
}
Expand Down Expand Up @@ -240,7 +240,7 @@ object ChatTabs {
val button = CleanButton(-69420, 2 + 22 * ordinal, 0, 20, 20, text)

companion object {
val buttons by lazy { values().associateWith { it.button } }
val buttons by lazy { entries.associateWith { it.button } }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ object KeyShortcuts : PersistentSave(File(Skytils.modDir, "keyshortcuts.json"))
fun inBitfield(field: Int) = (field and bitValue) == bitValue

companion object {
fun getPressed() = values().filter { it.pressed() }
fun getPressed() = entries.filter { it.pressed() }
fun getBitfield(modifiers: List<Modifiers>): Int {
var bits = 0
for (modifier in modifiers) {
Expand All @@ -119,7 +119,7 @@ object KeyShortcuts : PersistentSave(File(Skytils.modDir, "keyshortcuts.json"))
return bits
}

fun fromBitfield(field: Int) = values().filter { it.inBitfield(field) }
fun fromBitfield(field: Int) = entries.filter { it.inBitfield(field) }

fun fromUCraftBitfield(modifiers: UKeyboard.Modifiers) = getBitfield(fromUCraft(modifiers))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object SpiritLeap : PersistentSave(File(Skytils.modDir, "spiritleap.json")) {
private const val bloodOpenedString = "§r§cThe §r§c§lBLOOD DOOR§r§c has been opened!§r"
private var doorOpener: String? = null
val names = HashMap<String, Boolean>()
val classes = DungeonClass.values()
val classes = DungeonClass.entries
.associateWithTo(EnumMap(DungeonClass::class.java)) { false }
private val shortenedNameCache = WeakHashMap<String, String>()
private val nameSlotCache = HashMap<Int, String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ object Waypoints : PersistentSave(File(Skytils.modDir, "waypoints.json")) {
}
}
} else if (sbeWaypointFormat.containsMatchIn(str)) {
val island = SkyblockIsland.values().find { it.mode == SBInfo.mode } ?: SkyblockIsland.CrystalHollows
val island = SkyblockIsland.entries.find { it.mode == SBInfo.mode } ?: SkyblockIsland.CrystalHollows
val waypoints = sbeWaypointFormat.findAll(str.trim().replace("\n", "")).map {
Waypoint(
it.groups["name"]!!.value,
Expand Down Expand Up @@ -124,7 +124,7 @@ object Waypoints : PersistentSave(File(Skytils.modDir, "waypoints.json")) {
visibleWaypoints = emptyList()
return
}
val isUnknownIsland = SkyblockIsland.values().none { it.mode == SBInfo.mode }
val isUnknownIsland = SkyblockIsland.entries.none { it.mode == SBInfo.mode }
visibleWaypoints = categories.filter {
it.island.mode == SBInfo.mode || (isUnknownIsland && it.island == SkyblockIsland.Unknown)
}.flatMap { category ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ object MiningFeatures {
val x = cleaned.groups["x"]!!.value
val y = cleaned.groups["y"]!!.value
val z = cleaned.groups["z"]!!.value
CrystalHollowsMap.Locations.values().find { it.cleanName == stringLocation }
CrystalHollowsMap.Locations.entries.find { it.cleanName == stringLocation }
?.takeIf { !it.loc.exists() }?.let { loc ->
/**
* Sends the waypoints message except it suggests which one should be used based on
Expand Down Expand Up @@ -260,7 +260,7 @@ object MiningFeatures {
val message = UMessage(
"$prefix §eFound coordinates in a chat message, click a button to set a waypoint.\n"
)
for (loc in CrystalHollowsMap.Locations.values()) {
for (loc in CrystalHollowsMap.Locations.entries) {
if (loc.loc.exists()) continue
message.append(
UTextComponent("${loc.displayName.substring(0, 2)}[${loc.displayName}] ")
Expand Down Expand Up @@ -342,7 +342,7 @@ object MiningFeatures {
}
if (Skytils.config.crystalHollowWaypoints && SBInfo.mode == SkyblockIsland.CrystalHollows.mode) {
GlStateManager.disableDepth()
for (loc in CrystalHollowsMap.Locations.values()) {
for (loc in CrystalHollowsMap.Locations.entries) {
loc.loc.drawWaypoint(loc.cleanName, event.partialTicks, matrixStack)
}
RenderUtil.renderWaypointText("Crystal Nucleus", 513.5, 107.0, 513.5, event.partialTicks, matrixStack)
Expand Down Expand Up @@ -402,7 +402,7 @@ object MiningFeatures {
lastJukebox = null
raffleBox = null
inRaffle = false
CrystalHollowsMap.Locations.values().forEach { it.loc.reset() }
CrystalHollowsMap.Locations.entries.forEach { it.loc.reset() }
waypoints.clear()
}

Expand All @@ -423,7 +423,7 @@ object MiningFeatures {
val cleanName = displayName.stripControlCodes()

companion object {
val cleanNameToLocation = values().associateBy { it.cleanName }
val cleanNameToLocation = entries.associateBy { it.cleanName }
}
}

Expand All @@ -436,7 +436,7 @@ object MiningFeatures {
stack.runWithGlobalState {
RenderUtil.renderTexture(mapLocation, 0, 0, 624, 624, false)
if (Skytils.config.crystalHollowMapPlaces) {
Locations.values().forEach {
Locations.entries.forEach {
it.loc.drawOnMap(it.size, it.color)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ object LockOrb {
PLASMAFLUX("§d§lPlasmaflux", 20.0, "PLASMAFLUX_POWER_ORB");

companion object {
fun getPowerOrbMatchingName(name: String): PowerOrbs? = values().find { name.startsWith(it.orbName) }
fun getPowerOrbMatchingName(name: String): PowerOrbs? = entries.find { name.startsWith(it.orbName) }

fun getPowerOrbMatchingItemId(itemId: String): PowerOrbs? = values().find { it.itemId == itemId }
fun getPowerOrbMatchingItemId(itemId: String): PowerOrbs? = entries.find { it.itemId == itemId }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ enum class Damage(val symbol: String, val color: CustomColor) {

companion object {
fun fromSymbol(symbol: String): Damage? {
return values().find { it.symbol == symbol }
return entries.find { it.symbol == symbol }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ class Location(x: Double, y: Double, z: Double) : Point3d(x, y, z) {
return "[${x.roundToInt()}, ${y.roundToInt()}, ${z.roundToInt()}]"
}

override fun hashCode(): Int {
return super.hashCode()
}
}
Loading

0 comments on commit 42a4efe

Please sign in to comment.