-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DO A WORLD BACKUP OR TEST ON NEW WORLDS! This version implements a better method of registering Mons, the old one has been completely removed! (meaning that you lost all your data) Added Collection search bar Added Collection entry tooltip Added Highlight effect when hovering an entry
- Loading branch information
1 parent
2ce6ac0
commit 8febb0c
Showing
21 changed files
with
524 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
common/src/main/kotlin/com/rafacasari/mod/cobbledex/api/CobbledexDiscovery.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.rafacasari.mod.cobbledex.api | ||
|
||
import com.cobblemon.mod.common.Cobblemon.playerData | ||
import com.cobblemon.mod.common.api.storage.player.PlayerDataExtension | ||
import com.cobblemon.mod.common.pokemon.FormData | ||
import com.cobblemon.mod.common.util.party | ||
import com.cobblemon.mod.common.util.pc | ||
import com.google.gson.GsonBuilder | ||
import com.google.gson.JsonObject | ||
import com.rafacasari.mod.cobbledex.Cobbledex | ||
import com.rafacasari.mod.cobbledex.api.classes.DiscoveryRegister | ||
import com.rafacasari.mod.cobbledex.network.client.packets.ReceiveCollectionDataPacket | ||
import com.rafacasari.mod.cobbledex.utils.logInfo | ||
import net.minecraft.server.network.ServerPlayerEntity | ||
|
||
class CobbledexDiscovery(val registers: MutableMap<String, MutableMap<String, DiscoveryRegister>> = mutableMapOf()): PlayerDataExtension { | ||
|
||
companion object { | ||
const val NAME_KEY = "${Cobbledex.MOD_ID}_discovery" | ||
|
||
private val GSON = GsonBuilder() | ||
.disableHtmlEscaping() | ||
.create() | ||
|
||
fun addOrUpdatePlayer(player: ServerPlayerEntity, form: FormData, isShiny: Boolean, status: DiscoveryRegister.RegisterType, update: (DiscoveryRegister) -> (Unit)): Boolean { | ||
val data = playerData.get(player) | ||
|
||
val cobbledexData = data.extraData.getOrPut(NAME_KEY) { | ||
val discovery = CobbledexDiscovery() | ||
|
||
val pc = player.pc() | ||
pc.forEach { pokemon -> | ||
discovery.addOrUpdate(pokemon.species.showdownId(), pokemon.form.formOnlyShowdownId(), pokemon.shiny, DiscoveryRegister.RegisterType.CAUGHT, null) | ||
} | ||
|
||
val party = player.party() | ||
party.forEach { pokemon -> | ||
discovery.addOrUpdate(pokemon.species.showdownId(), pokemon.form.formOnlyShowdownId(), pokemon.shiny, DiscoveryRegister.RegisterType.CAUGHT, null) | ||
} | ||
|
||
ReceiveCollectionDataPacket(discovery.registers).sendToPlayer(player) | ||
logInfo("Added ${discovery.registers.size} entries in ${player.entityName}'s Cobbledex") | ||
return@getOrPut discovery | ||
} as CobbledexDiscovery | ||
|
||
val isNewRegister = cobbledexData.addOrUpdate(form.species.showdownId(), form.formOnlyShowdownId(), isShiny, status, update) | ||
|
||
playerData.saveSingle(data) | ||
return isNewRegister | ||
} | ||
} | ||
|
||
private fun getRegister(showdownId: String): MutableMap<String, DiscoveryRegister>? { | ||
return registers[showdownId] | ||
} | ||
|
||
fun addOrUpdate(species: String, form: String, isShiny: Boolean, status: DiscoveryRegister.RegisterType, update: ((DiscoveryRegister) -> Unit)?): Boolean { | ||
val currentRegister = getRegister(species) | ||
|
||
val discoverTimestamp = System.currentTimeMillis() | ||
val caughtTimestamp = if(status == DiscoveryRegister.RegisterType.CAUGHT) discoverTimestamp else null | ||
|
||
if (currentRegister != null) { | ||
val formRegister = currentRegister[form] | ||
if (formRegister != null) { | ||
// Update only if needed | ||
if (!formRegister.isShiny && isShiny) | ||
formRegister.isShiny = true | ||
|
||
// Update only if needed | ||
if (formRegister.status == DiscoveryRegister.RegisterType.SEEN && status == DiscoveryRegister.RegisterType.CAUGHT) { | ||
formRegister.status = DiscoveryRegister.RegisterType.CAUGHT | ||
formRegister.caughtTimestamp = caughtTimestamp | ||
} | ||
|
||
update?.invoke(formRegister) | ||
return false | ||
} else { | ||
// New form | ||
val newRegister = DiscoveryRegister(isShiny, status, discoverTimestamp, caughtTimestamp) | ||
currentRegister[form] = newRegister | ||
update?.invoke(newRegister) | ||
return true | ||
} | ||
} else { | ||
// New pokemon | ||
val newRegister = DiscoveryRegister(isShiny, status, discoverTimestamp, caughtTimestamp) | ||
registers[species] = mutableMapOf(form to newRegister) | ||
update?.invoke(newRegister) | ||
return true | ||
} | ||
} | ||
|
||
override fun name(): String { | ||
return NAME_KEY | ||
} | ||
|
||
override fun serialize(): JsonObject { | ||
val jsonObject = GSON.toJsonTree(this).asJsonObject | ||
jsonObject.addProperty(PlayerDataExtension.NAME_KEY, this.name()) | ||
return jsonObject | ||
} | ||
|
||
override fun deserialize(json: JsonObject): PlayerDataExtension { | ||
return GSON.fromJson(json, CobbledexDiscovery::class.java) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
common/src/main/kotlin/com/rafacasari/mod/cobbledex/api/adapters/DiscoveryRegisterAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.rafacasari.mod.cobbledex.api.adapters | ||
// | ||
//import com.google.gson.* | ||
//import com.rafacasari.mod.cobbledex.api.classes.DiscoveryRegister | ||
//import com.rafacasari.mod.cobbledex.utils.logInfo | ||
//import java.lang.reflect.Type | ||
//import java.util.AbstractMap.SimpleEntry | ||
// | ||
//class DiscoveryRegisterAdapter : JsonSerializer<Map.Entry<String, DiscoveryRegister>>, JsonDeserializer<Map.Entry<String, DiscoveryRegister>> { | ||
// override fun serialize(src: Map.Entry<String, DiscoveryRegister>, typeOfSrc: Type, context: JsonSerializationContext): JsonElement { | ||
// logInfo("Serializing") | ||
// | ||
// val jsonObject = JsonObject() | ||
// jsonObject.addProperty("speciesWithForm", src.key) | ||
// jsonObject.addProperty("isShiny", src.value.isShiny) | ||
// jsonObject.addProperty("status", src.value.status.name) | ||
// return jsonObject | ||
// } | ||
// | ||
// override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): Map.Entry<String, DiscoveryRegister> { | ||
// logInfo("De-serializing") | ||
// | ||
// val jsonObject = json.asJsonObject | ||
// val speciesWithForm = jsonObject.get("speciesWithForm").asString | ||
// val isShiny = jsonObject.get("isShiny").asBoolean | ||
// val status = DiscoveryRegister.RegisterType.valueOf(jsonObject.get("status").asString) | ||
// | ||
// return SimpleEntry(speciesWithForm, DiscoveryRegister(isShiny, status)) | ||
// } | ||
//} |
53 changes: 53 additions & 0 deletions
53
common/src/main/kotlin/com/rafacasari/mod/cobbledex/api/classes/DiscoveryRegister.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.rafacasari.mod.cobbledex.api.classes | ||
|
||
import com.rafacasari.mod.cobbledex.network.IEncodable | ||
import com.rafacasari.mod.cobbledex.utils.PacketUtils.readNullableLong | ||
import com.rafacasari.mod.cobbledex.utils.PacketUtils.writeNullableLong | ||
import net.minecraft.network.PacketByteBuf | ||
import net.minecraft.text.Text | ||
import net.minecraft.util.Formatting | ||
import java.time.Instant | ||
import java.time.LocalDateTime | ||
import java.time.ZoneId | ||
import java.time.format.DateTimeFormatter | ||
import java.time.format.FormatStyle | ||
|
||
class DiscoveryRegister(var isShiny: Boolean, var status: RegisterType, var discoveredTimestamp: Long?, var caughtTimestamp: Long?) : IEncodable { | ||
enum class RegisterType { | ||
SEEN, CAUGHT | ||
} | ||
|
||
fun getDiscoveredTimestamp(): Text? { | ||
if (discoveredTimestamp == null) return null | ||
|
||
val localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(discoveredTimestamp!!), ZoneId.systemDefault()) | ||
return Text.literal(localDateTime.format(TIME_FORMAT)).formatted(Formatting.ITALIC, Formatting.GRAY) | ||
} | ||
|
||
fun getCaughtTimestamp(): Text? { | ||
if (caughtTimestamp == null) return null | ||
|
||
val localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(caughtTimestamp!!), ZoneId.systemDefault()) | ||
return Text.literal(localDateTime.format(TIME_FORMAT)).formatted(Formatting.ITALIC, Formatting.GRAY) | ||
} | ||
|
||
override fun encode(buffer: PacketByteBuf) { | ||
buffer.writeBoolean(isShiny) | ||
buffer.writeString(status.name) | ||
buffer.writeNullableLong(discoveredTimestamp) | ||
buffer.writeNullableLong(caughtTimestamp) | ||
} | ||
|
||
companion object { | ||
private val TIME_FORMAT = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT) | ||
|
||
fun decode(reader: PacketByteBuf) : DiscoveryRegister { | ||
val isShiny = reader.readBoolean() | ||
val status = RegisterType.valueOf(reader.readString()) | ||
val discoveredTimestamp = reader.readNullableLong() | ||
val caughtTimestamp = reader.readNullableLong() | ||
|
||
return DiscoveryRegister(isShiny, status, discoveredTimestamp, caughtTimestamp) | ||
} | ||
} | ||
} |
Oops, something went wrong.