Skip to content

Commit

Permalink
1.0.21
Browse files Browse the repository at this point in the history
Preliminary addition of Server Settings: Disabled features will return an empty list to client
Added IconEntry, now some Evolution conditions have a lil icon (more icons coming soon)
Fixed some bugs
  • Loading branch information
Rafacasari committed Jun 21, 2024
1 parent e91b13f commit f880168
Show file tree
Hide file tree
Showing 18 changed files with 283 additions and 95 deletions.
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ plugins {

id("dev.architectury.loom") version ("1.6-SNAPSHOT") apply false
id("architectury-plugin") version ("3.4-SNAPSHOT") apply false

id("net.kyori.blossom") version "2.1.0" apply false
id("org.jetbrains.gradle.plugin.idea-ext") version ("1.1.8") apply false
}

group = "${property("maven_group")}"
Expand All @@ -22,5 +25,4 @@ allprojects {
maven ("https://cursemaven.com")
maven("https://thedarkcolour.github.io/KotlinForForge/")
}
}

}
13 changes: 13 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
plugins {
id("dev.architectury.loom")
id("architectury-plugin")

id("net.kyori.blossom")
id("org.jetbrains.gradle.plugin.idea-ext")
}

architectury {
Expand All @@ -19,3 +22,13 @@ dependencies {
modCompileOnly("net.fabricmc:fabric-loader:${property("fabric_loader_version")}")
modCompileOnly("net.fabricmc.fabric-api:fabric-api:${property("fabric_version")}")
}

sourceSets {
main {
blossom {
kotlinSources {
property("version", project.version.toString())
}
}
}
}
5 changes: 5 additions & 0 deletions common/src/main/kotlin-templates/CobbledexBuildDetails.kt.peb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.rafacasari.mod.cobbledex

object CobbledexBuildDetails {
const val VERSION = "{{ version }}"
}
48 changes: 46 additions & 2 deletions common/src/main/kotlin/com/rafacasari/mod/cobbledex/Cobbledex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,29 @@ import com.rafacasari.mod.cobbledex.cobblemon.extensions.PlayerDiscovery
import com.rafacasari.mod.cobbledex.items.CobbledexItem
import com.rafacasari.mod.cobbledex.network.client.packets.PokedexDiscoveredUpdated
import net.minecraft.server.network.ServerPlayerEntity
import java.io.File
import java.io.FileReader
import java.io.FileWriter

object Cobbledex {
private lateinit var config: CobbledexConfig
fun getConfig() : CobbledexConfig = config

const val MOD_ID : String = "cobbledex"

private const val VERSION = CobbledexBuildDetails.VERSION
private const val CONFIG_PATH = "config/$MOD_ID/settings.json"

val LOGGER: Logger = LoggerFactory.getLogger("Cobbledex")
lateinit var implementation: CobbledexImplementation


private var eventsCreated: Boolean = false
fun preInitialize(implementation: CobbledexImplementation) {
LOGGER.info("Initializing Cobbledex...")
LOGGER.info("Initializing Cobbledex $VERSION...")
Cobbledex.implementation = implementation

implementation.registerItems()
loadConfig()

// TODO: Make our own event so we don't need to depend on Cobblemon PlatformEvents
PlatformEvents.SERVER_STARTED.subscribe { _ ->
Expand Down Expand Up @@ -112,4 +121,39 @@ object Cobbledex {

return ActionResult.SUCCESS
}

fun loadConfig() {
val configFile = File(CONFIG_PATH)
configFile.parentFile.mkdirs()

if (configFile.exists()) {
try {
val fileReader = FileReader(configFile)
this.config = CobbledexConfig.GSON.fromJson(fileReader, CobbledexConfig::class.java)
fileReader.close()
} catch (error: Exception) {
LOGGER.error("Failed to load the config! Using default config")
this.config = CobbledexConfig()
error.printStackTrace()
}
} else {
this.config = CobbledexConfig()
}

config.lastSavedVersion = VERSION
this.saveConfig()
}

fun saveConfig() {
try {
val fileWriter = FileWriter(File(CONFIG_PATH))
CobbledexConfig.GSON.toJson(this.config, fileWriter)
fileWriter.flush()
fileWriter.close()
} catch (exception: Exception) {
LOGGER.error("Failed to save the config! Please consult the following stack trace:")
exception.printStackTrace()
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.rafacasari.mod.cobbledex

import com.google.gson.Gson
import com.google.gson.GsonBuilder

class CobbledexConfig {


companion object {
val GSON: Gson = GsonBuilder()
.disableHtmlEscaping()
.setPrettyPrinting()
.create()
}

var lastSavedVersion: String = "0.0.1"

var howToFindEnabled = true
var showEvolutions = true
var itemDropsEnabled = true


}
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,12 @@ class CobbledexGUI(var selectedPokemon: FormData?, var selectedAspects: Set<Stri
} ?: setOf()

// Request Pokémon Info to server and load into cache in Packet Handler (See ReceiveCobbledexPacketHandler)
// if (pokemon != null
// && ((lastLoadedSpecies == null || lastLoadedSpecies != pokemon.species)
// || (lastLoadedAspects == null || lastLoadedAspects != pokemonAspects))) {
if (pokemon != null
&& ((lastLoadedSpecies == null || lastLoadedSpecies != pokemon.species)
|| (lastLoadedAspects == null || lastLoadedAspects != pokemonAspects))) {

|| (lastLoadedAspects == null || lastLoadedAspects != pokemonAspects))) {
logInfo("Requested Cobbledex Packet")
lastLoadedAspects = aspects

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ package com.rafacasari.mod.cobbledex.client.widget

import com.cobblemon.mod.common.api.text.text
import com.cobblemon.mod.common.pokemon.Species
import com.rafacasari.mod.cobbledex.client.widget.entries.EmptyEntry
import com.rafacasari.mod.cobbledex.client.widget.entries.ItemEntry
import com.rafacasari.mod.cobbledex.client.widget.entries.PokemonEntry
import com.rafacasari.mod.cobbledex.client.widget.entries.TextEntry
import com.rafacasari.mod.cobbledex.client.widget.entries.*
import net.minecraft.client.MinecraftClient
import net.minecraft.client.gui.DrawContext
import net.minecraft.client.gui.widget.AlwaysSelectedEntryListWidget
import net.minecraft.item.ItemStack
import net.minecraft.text.MutableText
import net.minecraft.text.OrderedText
import net.minecraft.text.Text
import net.minecraft.util.Identifier
import net.minecraft.util.Language

class LongTextDisplay (
Expand Down Expand Up @@ -67,7 +64,7 @@ class LongTextDisplay (
}
}

fun addItemEntry(item: ItemStack, entry: Text, breakLine: Boolean = true) {
fun addItemEntry(item: ItemStack, entry: Text, breakLine: Boolean = true, disableTooltip: Boolean = false) {

if (breakLine && super.getEntryCount() > 0)
addEmptyEntry()
Expand All @@ -78,16 +75,28 @@ class LongTextDisplay (

reorderedTexts.forEach {
if (reorderedTexts.first() == it)
addItemEntryInternal(item, it)
super.addEntry(ItemEntry(item, it, disableTooltip))
else addText(TextEntry(it, false))
}
}

private fun addItemEntryInternal(item: ItemStack, text: OrderedText) : Int
{
return super.addEntry(ItemEntry(item, text))
fun addIcon(icon: Identifier, entry: Text, width: Int, height: Int, xOffset: Number = 0, yOffset: Number = 0, scale: Float = 1f, breakLine: Boolean = true) {

if (breakLine && super.getEntryCount() > 0)
addEmptyEntry()

val textRenderer = MinecraftClient.getInstance().textRenderer
val reorderedTexts = Language.getInstance()
.reorder(textRenderer.textHandler.wrapLines(entry, rowWidth - SCROLLBAR_PADDING - 11, entry.style))

reorderedTexts.forEach {
if (reorderedTexts.first() == it)
super.addEntry(IconEntry(icon, it, xOffset, yOffset, width, height, scale))
else addText(TextEntry(it, false))
}
}


fun addPokemon(pokemon: Species, aspects: Set<String>, translatedName: MutableText, breakLine: Boolean = false) {

if (breakLine && super.getEntryCount() > 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.rafacasari.mod.cobbledex.client.widget.entries

import com.cobblemon.mod.common.api.gui.blitk
import com.rafacasari.mod.cobbledex.client.widget.LongTextDisplay
import net.minecraft.client.MinecraftClient
import net.minecraft.client.gui.DrawContext
import net.minecraft.text.OrderedText
import net.minecraft.util.Colors
import net.minecraft.util.Identifier

class IconEntry(val icon: Identifier, val text: OrderedText, val xOffset: Number, val yOffset: Number, val iconOriginalWidth: Int, val iconOriginalHeight: Int, val iconScale: Float = 1f) : LongTextDisplay.TextDisplayEntry() {
private fun drawItemName(context: DrawContext, text: OrderedText, x: Number, y: Number, pMouseX: Int? = null, pMouseY: Int? = null): Boolean {
val textRenderer = MinecraftClient.getInstance().textRenderer
val width = textRenderer.getWidth(text)

context.drawText(textRenderer, text, x.toInt(), y.toInt(), Colors.WHITE, false)

// Return isHovered
return pMouseY != null && pMouseX != null &&
pMouseX.toInt() >= x.toInt() && pMouseX.toInt() <= x.toInt() + width &&
pMouseY.toInt() >= y.toInt() && pMouseY.toInt() <= y.toInt() + textRenderer.fontHeight
}

private fun drawItem(context: DrawContext, x: Int, y: Int) {
blitk(
matrixStack = context.matrices,
texture = icon,
x = x / iconScale + xOffset.toFloat(),
y = y / iconScale + yOffset.toFloat(),
height = iconOriginalHeight,
width = iconOriginalWidth,
scale = iconScale
)
}


override fun render(
context: DrawContext?,
index: Int,
y: Int,
x: Int,
entryWidth: Int,
entryHeight: Int,
mouseX: Int,
mouseY: Int,
hovered: Boolean,
tickDelta: Float
) {
if (context == null) return

drawItem(context, x, y)
drawItemName(context, text, x + 10.5f, y, mouseX, mouseY)
}

override fun drawTooltip(context: DrawContext, mouseX: Int, mouseY: Int) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import net.minecraft.text.OrderedText
import net.minecraft.util.Colors
import org.joml.Matrix4f

class ItemEntry(val item: ItemStack, val text: OrderedText) : LongTextDisplay.TextDisplayEntry() {
class ItemEntry(val item: ItemStack, val text: OrderedText, val disableTooltip: Boolean = false) : LongTextDisplay.TextDisplayEntry() {
companion object
{
const val ITEM_SIZE = 10.5F
Expand Down Expand Up @@ -92,7 +92,8 @@ class ItemEntry(val item: ItemStack, val text: OrderedText) : LongTextDisplay.Te
}

override fun drawTooltip(context: DrawContext, mouseX: Int, mouseY: Int) {
context.drawItemTooltip(MinecraftClient.getInstance().textRenderer, item, mouseX, mouseY)
if (!disableTooltip)
context.drawItemTooltip(MinecraftClient.getInstance().textRenderer, item, mouseX, mouseY)
}

override fun isMouseOver(mouseX: Double, mouseY: Double): Boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rafacasari.mod.cobbledex.network.server.handlers

import com.cobblemon.mod.common.api.pokemon.PokemonSpecies
import com.rafacasari.mod.cobbledex.Cobbledex
import com.rafacasari.mod.cobbledex.network.client.packets.ReceiveCobbledexPacket
import com.rafacasari.mod.cobbledex.network.server.IServerNetworkPacketHandler
import com.rafacasari.mod.cobbledex.network.server.packets.RequestCobbledexPacket
Expand All @@ -16,23 +17,15 @@ object RequestCobbledexPacketHandler : IServerNetworkPacketHandler<RequestCobble
val pokemon = PokemonSpecies.getByIdentifier(packet.pokemon)

if (pokemon != null ) {

// val evolutions = pokemon.evolutions.filter {
// it.result.species != null
// }.mapNotNull {
// val identifier = PokemonSpecies.getByName(it.result.species!!)?.resourceIdentifier
// if (identifier != null) identifier to it.result.aspects
// else null
// }

val evolutions = pokemon.evolutions.mapNotNull {
val serverConfig = Cobbledex.getConfig()
val evolutions = if (serverConfig.showEvolutions) pokemon.evolutions.mapNotNull {
it.result.species?.let { evoSpeciesName ->
val evoSpecies = PokemonSpecies.getByName(evoSpeciesName)
if(evoSpecies != null)
SerializablePokemonEvolution(it)
else null
}
}
} else listOf()

// Select all pre-evolution forms or just the default form
val preEvolutions = pokemon.preEvolution?.let { preEvolution ->
Expand All @@ -51,15 +44,15 @@ object RequestCobbledexPacketHandler : IServerNetworkPacketHandler<RequestCobble
identifier to it.aspects.toSet()
}

val spawnDetails = CobblemonUtils.getSpawnDetails(pokemon, packet.aspects)
val spawnDetails = if(serverConfig.howToFindEnabled) CobblemonUtils.getSpawnDetails(pokemon, packet.aspects) else listOf()

val serializableSpawnDetails = spawnDetails.map {
SerializablePokemonSpawnDetail(it)
}

val drops = CobblemonUtils.getPokemonDrops(pokemon).map {
val drops: List<SerializableItemDrop> = if(serverConfig.itemDropsEnabled) CobblemonUtils.getPokemonDrops(pokemon).map {
SerializableItemDrop(it)
}
} else listOf()

ReceiveCobbledexPacket(pokemon, evolutions, preEvolutions, species, serializableSpawnDetails, drops).sendToPlayer(player)
}
Expand Down
Loading

0 comments on commit f880168

Please sign in to comment.