Skip to content

Commit

Permalink
Better "Where to Find"
Browse files Browse the repository at this point in the history
- Now it uses Tooltips to show you more information about each condition.
- Also shows if it spawns in a structure and the "weight" of each spawn (rarity)
  • Loading branch information
Rafacasari committed Jun 8, 2024
1 parent 77f6320 commit 4df85f2
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import com.cobblemon.mod.common.CobblemonSounds
import com.cobblemon.mod.common.api.gui.blitk
import com.cobblemon.mod.common.api.pokemon.PokemonSpecies
import com.cobblemon.mod.common.api.spawning.CobblemonSpawnPools
import com.cobblemon.mod.common.api.spawning.detail.SpawnDetail
import com.cobblemon.mod.common.api.spawning.detail.PokemonSpawnDetail
import com.cobblemon.mod.common.api.text.add
import com.cobblemon.mod.common.api.text.bold
import com.cobblemon.mod.common.api.text.darkGreen
import com.cobblemon.mod.common.api.text.text
import com.cobblemon.mod.common.api.types.ElementalTypes
import com.cobblemon.mod.common.client.CobblemonResources
Expand All @@ -15,7 +16,9 @@ import com.cobblemon.mod.common.client.gui.TypeIcon
import com.cobblemon.mod.common.client.gui.summary.widgets.ModelWidget
import com.cobblemon.mod.common.client.render.drawScaledText
import com.cobblemon.mod.common.pokemon.Pokemon
import com.cobblemon.mod.common.registry.BiomeTagCondition
import com.cobblemon.mod.common.util.asTranslated
import com.mojang.datafixers.util.Either
import net.minecraft.client.MinecraftClient
import net.minecraft.client.gui.DrawContext
import net.minecraft.client.gui.screen.Screen
Expand All @@ -28,9 +31,10 @@ import net.minecraft.world.World
import com.rafacasari.mod.cobbledex.Cobbledex
import com.rafacasari.mod.cobbledex.client.widget.LongTextDisplay
import com.rafacasari.mod.cobbledex.client.widget.PokemonEvolutionDisplay
import com.rafacasari.mod.cobbledex.utils.BiomeUtils
import com.rafacasari.mod.cobbledex.utils.TypeChart
import com.rafacasari.mod.cobbledex.utils.cobbledexTranslation
import com.rafacasari.mod.cobbledex.utils.*
import net.minecraft.registry.tag.TagKey
import net.minecraft.text.HoverEvent
import net.minecraft.world.gen.structure.Structure

class CobbledexGUI(private val selectedPokemon: Pokemon?) : Screen(cobbledexTranslation("texts.title.cobbledex_gui")) {

Expand Down Expand Up @@ -77,11 +81,15 @@ class CobbledexGUI(private val selectedPokemon: Pokemon?) : Screen(cobbledexTran

this.addDrawableChild(ExitButton(pX = x + 315, pY = y + 172) { this.close() })

evolutionDisplay = PokemonEvolutionDisplay(x + 260, y + 37)
addDrawableChild(evolutionDisplay)


longTextDisplay = LongTextDisplay(x + 79, y + 18, 179, 158, 2)
addDrawableChild(longTextDisplay)



super.init()

// Should be the last thing to do.
Expand All @@ -93,11 +101,16 @@ class CobbledexGUI(private val selectedPokemon: Pokemon?) : Screen(cobbledexTran
{
this.setPreviewPokemon(selectedPokemon)
}


}

var renderContext: DrawContext? = null

override fun render(context: DrawContext, mouseX: Int, mouseY: Int, delta: Float) {

this.renderContext = context

val matrices = context.matrices
renderBackground(context)

Expand All @@ -115,7 +128,6 @@ class CobbledexGUI(private val selectedPokemon: Pokemon?) : Screen(cobbledexTran

modelWidget?.render(context, mouseX, mouseY, delta)


// Render Background
blitk(
matrixStack = matrices,
Expand Down Expand Up @@ -271,35 +283,23 @@ class CobbledexGUI(private val selectedPokemon: Pokemon?) : Screen(cobbledexTran
mouseX,
mouseY)
}


}
}


}



override fun shouldPause(): Boolean {
return false
}

override fun shouldCloseOnEsc(): Boolean {
return true
}
override fun shouldPause(): Boolean = false
override fun shouldCloseOnEsc(): Boolean = true

override fun close() {
Instance = null
playSound(CobblemonSounds.PC_OFF)
super.close()
}

fun getSpawnDetails(pokemon: Pokemon) : List<SpawnDetail> {

private fun getSpawnDetails(pokemon: Pokemon) : List<PokemonSpawnDetail> {
val spawnDetails = CobblemonSpawnPools.WORLD_SPAWN_POOL.filter {
x -> x.id.startsWith("${pokemon.species.resourceIdentifier.path}-")
}
x -> x is PokemonSpawnDetail && x.pokemon.species != null && x.pokemon.species == pokemon.species.resourceIdentifier.path
}.map { x -> x as PokemonSpawnDetail }

return spawnDetails
}
Expand Down Expand Up @@ -379,30 +379,77 @@ class CobbledexGUI(private val selectedPokemon: Pokemon?) : Screen(cobbledexTran
val world: ClientWorld? = MinecraftClient.getInstance().world
if (world != null) {

val biomes = BiomeUtils.getAllBiomes(world as World)
val validBiomes = biomes.filter { biome ->
getSpawnDetails(pokemon).any() { s ->
s.conditions.any { c ->
BiomeUtils.canSpawnAt(biome.biome, world, c)
val biomeRegistry = BiomeUtils.getBiomesRegistry(world)

val biomeCheckList = getSpawnDetails(pokemon)
.flatMap { spawnDetail ->
spawnDetail.conditions.mapNotNull { y -> y.biomes }.flatten().map { condition ->
val antiConditions = spawnDetail.anticonditions.mapNotNull { y -> y.biomes }.flatten()

BiomeChecker(spawnDetail, condition, BiomeUtils.getAllBiomes(world as World).filter {
b -> condition.fits(b.biome, biomeRegistry) && !antiConditions.any { anti -> anti.fits(b.biome, biomeRegistry) }
}.map {
b -> "biome.${b.identifier.toTranslationKey()}".asTranslated()
})
}
}
}

longTextDisplay?.add(cobbledexTranslation("cobbledex.texts.biomes").bold(), true)
validBiomes.forEach { biome ->
longTextDisplay?.add("biome.${biome.identifier.toTranslationKey()}".asTranslated(), false)

if (biomeCheckList.isNotEmpty()) {
longTextDisplay?.add(cobbledexTranslation("cobbledex.texts.biomes").bold(), true)
biomeCheckList.forEach { el ->

if (el.biomeCondition is BiomeTagCondition) {
val condition = el.biomeCondition.tag.id.path
val conditionMutableText = condition.asTranslated()
val tooltipText = condition.asTranslated().bold().add("\n".text())
tooltipText.add("Weight: ${el.spawnDetail.weight}\n".text().setStyle(Style.EMPTY.withBold(false)))

if (condition != "is_overworld") {
el.biomeList.forEach { biome ->
tooltipText.add("\n".text())
tooltipText.add(biome.setStyle(Style.EMPTY.withBold(false)))
}
}

val structureConditions: List<Either<Identifier, TagKey<Structure>>> = el.spawnDetail.conditions.mapNotNull {
structureCondition -> structureCondition.structures
}.flatten()

if (structureConditions.isNotEmpty()) {
tooltipText.add("\nNeed structure:".text().bold().darkGreen())
structureConditions.forEach { structure ->

val structureName = structure.fold(
{ left -> left.toShortTranslationKey() },
{ right -> right.id.toShortTranslationKey() }
)

if (structureName.isNotEmpty()) {
tooltipText.add("\n".text())
tooltipText.add(
"feature.$structureName".asTranslated()
.setStyle(Style.EMPTY.withBold(false))
)
}
}
}

val hoverEvent = HoverEvent(HoverEvent.Action.SHOW_TEXT, tooltipText)

longTextDisplay?.add(
conditionMutableText.setStyle(Style.EMPTY.withHoverEvent(hoverEvent)), false
)
}
}
}
}
}

val x = (width - BASE_WIDTH) / 2
val y = (height - BASE_HEIGHT) / 2

// re-initialize
if (evolutionDisplay == null) {
evolutionDisplay = PokemonEvolutionDisplay(x + 260, y + 37)
addDrawableChild(evolutionDisplay)
}

evolutionDisplay?.selectPokemon(pokemon)


Expand Down Expand Up @@ -434,10 +481,6 @@ class CobbledexGUI(private val selectedPokemon: Pokemon?) : Screen(cobbledexTran
centeredX = true
)





} else {
previewPokemon = null
modelWidget = null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.rafacasari.mod.cobbledex.client.widget



import com.cobblemon.mod.common.api.text.text
import net.minecraft.client.MinecraftClient
import net.minecraft.client.gui.DrawContext
Expand All @@ -19,7 +17,8 @@ class LongTextDisplay (
private val y: Int = 0,
private val frameWidth: Int,
private val frameHeight: Int,
private val padding : Int = 3
private val padding : Int = 3,
private val scrollbarSize: Int = 2
): AlwaysSelectedEntryListWidget<LongTextDisplay.DialogueLine>(
MinecraftClient.getInstance(),
frameWidth,
Expand Down Expand Up @@ -74,7 +73,7 @@ class LongTextDisplay (
}

override fun getScrollbarPositionX(): Int {
return left + width - 5
return left + width - padding - scrollbarSize
}

override fun render(context: DrawContext, mouseX: Int, mouseY: Int, partialTicks: Float) {
Expand All @@ -90,7 +89,10 @@ class LongTextDisplay (
super.render(context, mouseX, mouseY, partialTicks)
context.disableScissor()


if (hoveredEntry?.pendingHover != null) {
val textRenderer = MinecraftClient.getInstance().textRenderer
context.drawHoverEvent(textRenderer, hoveredEntry?.pendingHover, mouseX, mouseY)
}
}

override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean {
Expand Down Expand Up @@ -173,16 +175,23 @@ class LongTextDisplay (
}

hoveredStyle?.let {
context.drawHoverEvent(textRenderer, it, pMouseX!!, pMouseY!!)
newHover = it
//context.drawHoverEvent(textRenderer, it, pMouseX!!, pMouseY!!)
}
}

}

private var newHover: Style? = null
var pendingHover: Style? = null

override fun render(context: DrawContext, index: Int, rowTop: Int, rowLeft: Int, rowWidth: Int, rowHeight: Int, mouseX: Int, mouseY: Int, isHovered: Boolean, partialTicks: Float) {
newHover = null

line?.let {
drawOrderedText(context, it, rowLeft, rowTop, pMouseX = mouseX, pMouseY = mouseY)
}

pendingHover = newHover
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ class CobbledexItem(settings: Settings) : Item(settings) {


override fun useOnEntity(
itemStack: ItemStack?, player: PlayerEntity?, target: LivingEntity?, hand: Hand?
): ActionResult {
itemStack: ItemStack?, player: PlayerEntity?, target: LivingEntity?, hand: Hand?): ActionResult {

if (player == null || target == null || player.world == null) {
return ActionResult.FAIL
Expand All @@ -38,31 +37,11 @@ class CobbledexItem(settings: Settings) : Item(settings) {
return ActionResult.PASS
}



// val pokemon = target.pokemon
// val storeCoordinates = pokemon.storeCoordinates.get()
// val ownership = when {
// storeCoordinates == null -> Ownership.WILD
// storeCoordinates.store.uuid == player.uuid -> Ownership.OWNER
// else -> Ownership.OWNED_ANOTHER
// }

// if (ownership != Ownership.OWNER) {
// player.sendSystemMessage(Component.translatable(CobbledexReferences.NotYourPokemon))
// return InteractionResult.FAIL
// }

return Cobbledex.registerPlayerDiscovery(player, target.pokemon.species)
}


override fun appendTooltip(
stack: ItemStack?,
world: World?,
tooltip: MutableList<Text>?,
context: TooltipContext?
)
override fun appendTooltip(stack: ItemStack?, world: World?, tooltip: MutableList<Text>?, context: TooltipContext?)
{
tooltip?.add(Text.literal("Discovered: §a0§r/100"))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
package com.rafacasari.mod.cobbledex.utils

import com.cobblemon.mod.common.api.spawning.condition.SpawningCondition
import com.cobblemon.mod.common.api.conditional.RegistryLikeCondition
import com.cobblemon.mod.common.api.spawning.detail.PokemonSpawnDetail
import net.minecraft.registry.Registry
import net.minecraft.registry.RegistryKeys
import net.minecraft.text.MutableText
import net.minecraft.util.Identifier
import net.minecraft.world.World
import net.minecraft.world.biome.Biome

data class CobbledexBiome(val identifier: Identifier, val biome: Biome)
data class BiomeChecker(val spawnDetail: PokemonSpawnDetail, val biomeCondition: RegistryLikeCondition<Biome>, val biomeList : List<MutableText>)

object BiomeUtils {

fun getBiomesRegistry(world: World) : Registry<Biome> {
return world.registryManager.get(RegistryKeys.BIOME)
}

fun getAllBiomes(world: World) : List<CobbledexBiome> {
val registry: Registry<Biome> = world.registryManager.get(RegistryKeys.BIOME)
return registry.entrySet.map { entry -> CobbledexBiome(entry.key.value, entry.value) }
}

fun canSpawnAt (biome: Biome, world: World, condition: SpawningCondition<*>) : Boolean {

val registry: Registry<Biome> = world.registryManager.get(RegistryKeys.BIOME)
if (condition.biomes == null) return false

val count = condition.biomes!!.count { biomeCondition ->
biomeCondition.fits(
biome,
registry
)
}

return count > 0
}
// fun canSpawnAt (biome: Biome, world: World, condition: SpawningCondition<*>) : Boolean {
//
// val registry: Registry<Biome> = world.registryManager.get(RegistryKeys.BIOME)
// if (condition.biomes == null) return false
//
// val count = condition.biomes!!.count { biomeCondition ->
// biomeCondition.fits(
// biome,
// registry
// )
// }
//
// return count > 0
// }
}
Loading

0 comments on commit 4df85f2

Please sign in to comment.