mirror of
https://gitlab.com/cable-mc/cobblemon.git
synced 2026-10-20 22:26:24 -04:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6aaca2b302 | ||
|
|
0dcb9c5802 | ||
|
|
5a3d47bf2e | ||
|
|
1c4934fa53 |
@@ -60,6 +60,8 @@ import com.cobblemon.mod.common.api.storage.pc.PCStore
|
||||
import com.cobblemon.mod.common.api.storage.pc.link.PCLinkManager
|
||||
import com.cobblemon.mod.common.api.storage.player.PlayerInstancedDataStoreManager
|
||||
import com.cobblemon.mod.common.api.storage.player.PlayerInstancedDataStoreTypes
|
||||
import com.cobblemon.mod.common.api.storage.player.adapter.BadgeMongoBackend
|
||||
import com.cobblemon.mod.common.api.storage.player.adapter.BadgeNbtBackend
|
||||
import com.cobblemon.mod.common.api.storage.player.adapter.DexDataMongoBackend
|
||||
import com.cobblemon.mod.common.api.storage.player.adapter.DexDataNbtBackend
|
||||
import com.cobblemon.mod.common.api.storage.player.adapter.PlayerDataJsonBackend
|
||||
@@ -391,10 +393,13 @@ object Cobblemon {
|
||||
|
||||
val tmMoveFactory = CachedPlayerDataStoreFactory(TMMoveNbtBackend())
|
||||
tmMoveFactory.setup(server)
|
||||
val badgeFactory = CachedPlayerDataStoreFactory(BadgeNbtBackend())
|
||||
badgeFactory.setup(server)
|
||||
|
||||
playerDataManager.setFactory(generalJsonFactory, PlayerInstancedDataStoreTypes.GENERAL)
|
||||
playerDataManager.setFactory(pokedexNbtFactory, PlayerInstancedDataStoreTypes.POKEDEX)
|
||||
playerDataManager.setFactory(tmMoveFactory, PlayerInstancedDataStoreTypes.TM_MOVES)
|
||||
playerDataManager.setFactory(badgeFactory, PlayerInstancedDataStoreTypes.BADGES)
|
||||
|
||||
if (config.storageFormat == "nbt") {
|
||||
NBTStoreAdapter(pokemonStoreRoot.absolutePath, useNestedFolders = true, folderPerClass = true)
|
||||
@@ -425,10 +430,15 @@ object Cobblemon {
|
||||
TMMoveMongoBackend(mongoClient, config.mongoDBDatabaseName, "TMMovesCollection")
|
||||
)
|
||||
tmMovesMongoFactory.setup(server)
|
||||
val badgesMongoFactory = CachedPlayerDataStoreFactory(
|
||||
BadgeMongoBackend(mongoClient, config.mongoDBDatabaseName, "BadgesCollection")
|
||||
)
|
||||
badgesMongoFactory.setup(server)
|
||||
|
||||
playerDataManager.setFactory(generalMongoFactory, PlayerInstancedDataStoreTypes.GENERAL)
|
||||
playerDataManager.setFactory(pokedexMongoFactory, PlayerInstancedDataStoreTypes.POKEDEX)
|
||||
playerDataManager.setFactory(tmMovesMongoFactory, PlayerInstancedDataStoreTypes.TM_MOVES)
|
||||
playerDataManager.setFactory(badgesMongoFactory, PlayerInstancedDataStoreTypes.BADGES)
|
||||
MongoDBStoreAdapter(mongoClient, config.mongoDBDatabaseName)
|
||||
} catch (e: ClassNotFoundException) {
|
||||
LOGGER.error("MongoDB driver not found.")
|
||||
@@ -613,6 +623,7 @@ object Cobblemon {
|
||||
this.implementation.registerCommandArgument(cobblemonResource("transform_type"), TransformTypeArgumentType::class, SingletonArgumentInfo.contextFree(TransformTypeArgumentType::transformType))
|
||||
this.implementation.registerCommandArgument(cobblemonResource("model_part"), ModelPartArgumentType::class, SingletonArgumentInfo.contextFree(ModelPartArgumentType::modelPart))
|
||||
this.implementation.registerCommandArgument(cobblemonResource("tm"), TmArgumentType::class, SingletonArgumentInfo.contextFree(TmArgumentType::tm))
|
||||
this.implementation.registerCommandArgument(cobblemonResource("badge"), BadgeArgumentType::class, SingletonArgumentInfo.contextFree(BadgeArgumentType::badge))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -73,6 +73,7 @@ object CobblemonCommands {
|
||||
PcTakeCommand.register(dispatcher)
|
||||
GiveTmCommand.register(dispatcher)
|
||||
TmCommand.register(dispatcher)
|
||||
BadgeCommand.register(dispatcher)
|
||||
|
||||
// Possibly lock down registration if and only if under dev environment or running in an environment
|
||||
// with a certain system environment variable set
|
||||
|
||||
@@ -432,6 +432,7 @@ object CobblemonNetwork {
|
||||
list.add(PacketRegisterInfo(SpeciesFeatureAssignmentSyncPacket.ID, SpeciesFeatureAssignmentSyncPacket::decode, DataRegistrySyncPacketHandler()))
|
||||
list.add(PacketRegisterInfo(NaturalMaterialRegistrySyncPacket.ID, NaturalMaterialRegistrySyncPacket::decode, DataRegistrySyncPacketHandler()))
|
||||
list.add(PacketRegisterInfo(FossilRegistrySyncPacket.ID, FossilRegistrySyncPacket::decode, DataRegistrySyncPacketHandler()))
|
||||
list.add(PacketRegisterInfo(BadgeRegistrySyncPacket.ID, BadgeRegistrySyncPacket::decode, DataRegistrySyncPacketHandler()))
|
||||
list.add(PacketRegisterInfo(NPCRegistrySyncPacket.ID, NPCRegistrySyncPacket::decode, DataRegistrySyncPacketHandler()))
|
||||
list.add(PacketRegisterInfo(PokeRodRegistrySyncPacket.ID, PokeRodRegistrySyncPacket::decode, DataRegistrySyncPacketHandler()))
|
||||
list.add(PacketRegisterInfo(ScriptRegistrySyncPacket.ID, ScriptRegistrySyncPacket::decode, DataRegistrySyncPacketHandler()))
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.cobblemon.mod.common.api.badges
|
||||
|
||||
import net.minecraft.resources.ResourceLocation
|
||||
import net.minecraft.util.StringRepresentable
|
||||
|
||||
class Badge(
|
||||
identifier: ResourceLocation,
|
||||
val name: String,
|
||||
val texture: ResourceLocation,
|
||||
val order: Int
|
||||
): StringRepresentable {
|
||||
|
||||
@Transient
|
||||
var identifier: ResourceLocation = identifier
|
||||
internal set
|
||||
|
||||
override fun getSerializedName(): String {
|
||||
return identifier.toString()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.cobblemon.mod.common.api.badges
|
||||
|
||||
import com.cobblemon.mod.common.Cobblemon
|
||||
import com.cobblemon.mod.common.CobblemonNetwork.sendPacket
|
||||
import com.cobblemon.mod.common.api.storage.player.InstancedPlayerData
|
||||
import com.cobblemon.mod.common.api.storage.player.PlayerInstancedDataStoreTypes
|
||||
import com.cobblemon.mod.common.api.storage.player.client.ClientBadgeManager
|
||||
import com.cobblemon.mod.common.net.messages.client.SetClientPlayerDataPacket
|
||||
import com.cobblemon.mod.common.util.getPlayer
|
||||
import com.mojang.serialization.Codec
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder
|
||||
import net.minecraft.resources.ResourceLocation
|
||||
import java.util.UUID
|
||||
|
||||
class BadgeManager(
|
||||
override val uuid: UUID,
|
||||
val obtainedBadges: MutableSet<ResourceLocation> = mutableSetOf()
|
||||
) : InstancedPlayerData {
|
||||
|
||||
val storeType = PlayerInstancedDataStoreTypes.BADGES
|
||||
|
||||
override fun toClientData() = ClientBadgeManager(obtainedBadges.toMutableSet())
|
||||
|
||||
fun give(badge: Badge): Boolean {
|
||||
if (!obtainedBadges.add(badge.identifier)) return false
|
||||
syncAndSave()
|
||||
return true
|
||||
}
|
||||
|
||||
fun remove(badge: Badge): Boolean {
|
||||
if (!obtainedBadges.remove(badge.identifier)) return false
|
||||
syncAndSave()
|
||||
return true
|
||||
}
|
||||
|
||||
private fun syncAndSave() {
|
||||
uuid.getPlayer()?.sendPacket(SetClientPlayerDataPacket(storeType, toClientData()))
|
||||
Cobblemon.playerDataManager.saveSingle(this, storeType)
|
||||
}
|
||||
|
||||
companion object {
|
||||
val CODEC: Codec<BadgeManager> = RecordCodecBuilder.create { instance ->
|
||||
instance.group(
|
||||
Codec.STRING.xmap(UUID::fromString, UUID::toString)
|
||||
.fieldOf("uuid")
|
||||
.forGetter { it.uuid },
|
||||
Codec.list(ResourceLocation.CODEC)
|
||||
.xmap({ it.toMutableSet() }, { it.toList() })
|
||||
.fieldOf("obtainedBadges")
|
||||
.forGetter { it.obtainedBadges }
|
||||
).apply(instance, ::BadgeManager)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.cobblemon.mod.common.api.badges
|
||||
|
||||
import com.cobblemon.mod.common.Cobblemon
|
||||
import com.cobblemon.mod.common.api.data.JsonDataRegistry
|
||||
import com.cobblemon.mod.common.api.reactive.SimpleObservable
|
||||
import com.cobblemon.mod.common.net.messages.client.data.BadgeRegistrySyncPacket
|
||||
import com.cobblemon.mod.common.util.adapters.IdentifierAdapter
|
||||
import com.cobblemon.mod.common.util.cobblemonResource
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import net.minecraft.resources.ResourceLocation
|
||||
import net.minecraft.server.level.ServerPlayer
|
||||
import net.minecraft.server.packs.PackType
|
||||
|
||||
object Badges : JsonDataRegistry<Badge> {
|
||||
|
||||
override val id: ResourceLocation = cobblemonResource("badges")
|
||||
override val type: PackType = PackType.SERVER_DATA
|
||||
override val observable = SimpleObservable<Badges>()
|
||||
|
||||
override val gson: Gson = GsonBuilder()
|
||||
.disableHtmlEscaping()
|
||||
.setPrettyPrinting()
|
||||
.registerTypeAdapter(ResourceLocation::class.java, IdentifierAdapter)
|
||||
.create()
|
||||
|
||||
override val typeToken: TypeToken<Badge> = TypeToken.get(Badge::class.java)
|
||||
override val resourcePath: String = "badges"
|
||||
|
||||
private val badges = hashMapOf<ResourceLocation, Badge>()
|
||||
|
||||
override fun reload(data: Map<ResourceLocation, Badge>) {
|
||||
this.badges.clear()
|
||||
data.forEach { (id, badge) ->
|
||||
try {
|
||||
badge.identifier = id
|
||||
this.badges[id] = badge
|
||||
} catch (e: Exception) {
|
||||
Cobblemon.LOGGER.error("Skipped loading the {} badge", id, e)
|
||||
}
|
||||
}
|
||||
Cobblemon.LOGGER.info("Loaded {} badges", this.badges.size)
|
||||
this.observable.emit(this)
|
||||
}
|
||||
|
||||
override fun sync(player: ServerPlayer) {
|
||||
BadgeRegistrySyncPacket(this.all()).sendToPlayer(player)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all loaded [Badge]s.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun all() = this.badges.values.toList()
|
||||
|
||||
/**
|
||||
* Gets a [Badge] by its [ResourceLocation].
|
||||
* @param identifier The identifier of the badge.
|
||||
* @return The [Badge], if loaded, otherwise null.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun getByIdentifier(identifier: ResourceLocation): Badge? = this.badges[identifier]
|
||||
}
|
||||
@@ -145,6 +145,8 @@ object CobblemonPermissions {
|
||||
val SPECTATE_BATTLE = this.create("${COMMAND_PREFIX}spectatebattle", PermissionLevel.ALL_COMMANDS)
|
||||
@JvmStatic
|
||||
val TECHNICAL_MACHINE = this.create("${COMMAND_PREFIX}technicalmachine", PermissionLevel.CHEAT_COMMANDS_AND_COMMAND_BLOCKS)
|
||||
@JvmStatic
|
||||
val BADGE = this.create("${COMMAND_PREFIX}badge", PermissionLevel.CHEAT_COMMANDS_AND_COMMAND_BLOCKS)
|
||||
|
||||
@JvmStatic
|
||||
val SEE_HIDDEN_NPCS = this.create("seehiddennpcs", PermissionLevel.CHEAT_COMMANDS_AND_COMMAND_BLOCKS)
|
||||
|
||||
@@ -29,8 +29,12 @@ object CobblemonStats {
|
||||
@JvmField
|
||||
val BATTLES_WON: CobblemonStat = CobblemonStat("battles_won")
|
||||
@JvmField
|
||||
val PVP_BATTLES_WON: CobblemonStat = CobblemonStat("pvp_battles_won")
|
||||
@JvmField
|
||||
val BATTLES_LOST: CobblemonStat = CobblemonStat("battles_lost")
|
||||
@JvmField
|
||||
val PVP_BATTLES_LOST: CobblemonStat = CobblemonStat("pvp_battles_lost")
|
||||
@JvmField
|
||||
val BATTLES_FLED: CobblemonStat = CobblemonStat("battles_fled")
|
||||
@JvmField
|
||||
val BATTLES_TOTAL: CobblemonStat = CobblemonStat("battles_total")
|
||||
@@ -68,7 +72,9 @@ object CobblemonStats {
|
||||
stats["evolved"] = EVOLVED
|
||||
stats["level_up"] = LEVEL_UP
|
||||
stats["battles_won"] = BATTLES_WON
|
||||
stats["pvp_battles_won"] = PVP_BATTLES_WON
|
||||
stats["battles_lost"] = BATTLES_LOST
|
||||
stats["pvp_battles_lost"] = PVP_BATTLES_LOST
|
||||
stats["battles_fled"] = BATTLES_FLED
|
||||
stats["battles_total"] = BATTLES_TOTAL
|
||||
stats["dex_entries"] = DEX_ENTRIES
|
||||
|
||||
+14
@@ -10,6 +10,7 @@ package com.cobblemon.mod.common.api.storage.player
|
||||
|
||||
import com.cobblemon.mod.common.Cobblemon
|
||||
import com.cobblemon.mod.common.Cobblemon.MODID
|
||||
import com.cobblemon.mod.common.api.badges.BadgeManager
|
||||
import com.cobblemon.mod.common.api.pokedex.PokedexManager
|
||||
import com.cobblemon.mod.common.api.scheduling.ScheduledTask
|
||||
import com.cobblemon.mod.common.api.scheduling.ServerTaskTracker
|
||||
@@ -76,6 +77,14 @@ open class PlayerInstancedDataStoreManager {
|
||||
.infiniteIterations()
|
||||
.tracker(ServerTaskTracker)
|
||||
.build()
|
||||
|
||||
saveTasks[PlayerInstancedDataStoreTypes.BADGES] = ScheduledTask.Builder()
|
||||
.execute { saveAllOfOneType(PlayerInstancedDataStoreTypes.BADGES) }
|
||||
.delay(30F)
|
||||
.interval(120F)
|
||||
.infiniteIterations()
|
||||
.tracker(ServerTaskTracker)
|
||||
.build()
|
||||
}
|
||||
|
||||
open fun get(playerId: UUID, dataType: PlayerInstancedDataStoreType): InstancedPlayerData {
|
||||
@@ -148,4 +157,9 @@ open class PlayerInstancedDataStoreManager {
|
||||
}
|
||||
return data as TMMoveManager
|
||||
}
|
||||
|
||||
fun getBadgeData(player: ServerPlayer): BadgeManager = getBadgeData(player.uuid)
|
||||
|
||||
fun getBadgeData(playerId: UUID): BadgeManager =
|
||||
get(playerId, PlayerInstancedDataStoreTypes.BADGES) as BadgeManager
|
||||
}
|
||||
|
||||
+7
@@ -9,6 +9,7 @@
|
||||
package com.cobblemon.mod.common.api.storage.player
|
||||
|
||||
import com.cobblemon.mod.common.Cobblemon
|
||||
import com.cobblemon.mod.common.api.storage.player.client.ClientBadgeManager
|
||||
import com.cobblemon.mod.common.api.storage.player.client.ClientPokedexManager
|
||||
import com.cobblemon.mod.common.api.storage.player.client.ClientGeneralPlayerData
|
||||
import com.cobblemon.mod.common.api.storage.player.client.ClientTMMoveManager
|
||||
@@ -35,6 +36,12 @@ object PlayerInstancedDataStoreTypes {
|
||||
ClientTMMoveManager::runAction,
|
||||
ClientTMMoveManager::runIncremental
|
||||
))
|
||||
val BADGES = register(PlayerInstancedDataStoreType(
|
||||
cobblemonResource("badge_collection"),
|
||||
ClientBadgeManager::decode,
|
||||
ClientBadgeManager::runAction,
|
||||
ClientBadgeManager::runIncremental
|
||||
))
|
||||
|
||||
fun register(type: PlayerInstancedDataStoreType): PlayerInstancedDataStoreType {
|
||||
Cobblemon.LOGGER.debug("Registering PlayerInstancedDataStoreType: {}", type.id)
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Cobblemon Contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
package com.cobblemon.mod.common.api.storage.player.adapter
|
||||
|
||||
import com.cobblemon.mod.common.api.badges.BadgeManager
|
||||
import com.cobblemon.mod.common.api.storage.player.PlayerInstancedDataStoreTypes
|
||||
import com.cobblemon.mod.common.util.adapters.CodecBackedAdapter
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import com.mongodb.client.MongoClient
|
||||
import java.util.UUID
|
||||
|
||||
class BadgeMongoBackend(
|
||||
mongoClient: MongoClient,
|
||||
databaseName: String,
|
||||
collectionName: String
|
||||
) : MongoBackedPlayerDataStoreBackend<BadgeManager>(
|
||||
mongoClient,
|
||||
databaseName,
|
||||
collectionName,
|
||||
PlayerInstancedDataStoreTypes.BADGES
|
||||
) {
|
||||
override val gson = GsonBuilder()
|
||||
.registerTypeAdapter(BadgeManager::class.java, CodecBackedAdapter(BadgeManager.CODEC))
|
||||
.create()
|
||||
override val classToken = TypeToken.get(BadgeManager::class.java)
|
||||
override val defaultData = defaultDataFunc
|
||||
|
||||
companion object {
|
||||
val defaultDataFunc = { uuid: UUID -> BadgeManager(uuid) }
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Cobblemon Contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
package com.cobblemon.mod.common.api.storage.player.adapter
|
||||
|
||||
import com.cobblemon.mod.common.api.badges.BadgeManager
|
||||
import com.cobblemon.mod.common.api.storage.player.PlayerInstancedDataStoreTypes
|
||||
import java.util.UUID
|
||||
|
||||
class BadgeNbtBackend : NbtBackedPlayerData<BadgeManager>("badges", PlayerInstancedDataStoreTypes.BADGES) {
|
||||
override val codec = BadgeManager.CODEC
|
||||
override val defaultData = defaultDataFunc
|
||||
|
||||
companion object {
|
||||
val defaultDataFunc = { uuid: UUID -> BadgeManager(uuid) }
|
||||
}
|
||||
}
|
||||
+6
-4
@@ -52,7 +52,7 @@ abstract class FileBasedPlayerDataStoreBackend<T : InstancedPlayerData>(
|
||||
val playerFile = filePath(uuid)
|
||||
val playerFileOld = filePath(uuid, OLD_FILE_EXTENSION)
|
||||
playerFile.parentFile.mkdirs()
|
||||
val loadFallback: () -> T = {
|
||||
val loadFallback: (Boolean) -> T = { primaryFileCorrupt ->
|
||||
playerFile.delete()
|
||||
if (playerFileOld.exists() && playerFileOld.length() > 0L) {
|
||||
try {
|
||||
@@ -68,7 +68,9 @@ abstract class FileBasedPlayerDataStoreBackend<T : InstancedPlayerData>(
|
||||
}
|
||||
}
|
||||
else {
|
||||
Cobblemon.LOGGER.error(".old File $subfolder for $uuid is corrupt or missing. Data is lost")
|
||||
if (primaryFileCorrupt) {
|
||||
Cobblemon.LOGGER.error(".old File $subfolder for $uuid is corrupt or missing. Data is lost")
|
||||
}
|
||||
playerFileOld.delete()
|
||||
defaultData.invoke(uuid).also(::save)
|
||||
}
|
||||
@@ -80,10 +82,10 @@ abstract class FileBasedPlayerDataStoreBackend<T : InstancedPlayerData>(
|
||||
catch (e: Exception) {
|
||||
Cobblemon.LOGGER.warn("Failed to load $subfolder for $uuid due to ${e.message}")
|
||||
Cobblemon.LOGGER.warn(e)
|
||||
loadFallback()
|
||||
loadFallback(true)
|
||||
}
|
||||
} else {
|
||||
loadFallback()
|
||||
loadFallback(false)
|
||||
}.also { it.initialize() }
|
||||
}
|
||||
|
||||
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.cobblemon.mod.common.api.storage.player.client
|
||||
|
||||
import com.cobblemon.mod.common.api.storage.player.PlayerInstancedDataStoreTypes
|
||||
import com.cobblemon.mod.common.client.CobblemonClient
|
||||
import com.cobblemon.mod.common.net.messages.client.SetClientPlayerDataPacket
|
||||
import com.cobblemon.mod.common.util.readString
|
||||
import com.cobblemon.mod.common.util.writeString
|
||||
import net.minecraft.network.RegistryFriendlyByteBuf
|
||||
import net.minecraft.resources.ResourceLocation
|
||||
|
||||
class ClientBadgeManager(
|
||||
val obtainedBadges: MutableSet<ResourceLocation>
|
||||
) : ClientInstancedPlayerData {
|
||||
|
||||
override fun encode(buf: RegistryFriendlyByteBuf) {
|
||||
buf.writeCollection(obtainedBadges) { _, id, -> buf.writeString(id.toString()) }
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun decode(buf: RegistryFriendlyByteBuf): SetClientPlayerDataPacket {
|
||||
val badges = buf.readCollection(
|
||||
{ mutableSetOf<ResourceLocation>() },
|
||||
{ buf.readString().let(ResourceLocation::tryParse) ?: ResourceLocation("minecraft", "empty") }
|
||||
)
|
||||
return SetClientPlayerDataPacket(
|
||||
PlayerInstancedDataStoreTypes.BADGES,
|
||||
ClientBadgeManager(badges)
|
||||
)
|
||||
}
|
||||
|
||||
fun runAction(data: ClientInstancedPlayerData) {
|
||||
if (data !is ClientBadgeManager) return
|
||||
CobblemonClient.clientBadgeData = data
|
||||
}
|
||||
|
||||
fun runIncremental(data: ClientInstancedPlayerData) {
|
||||
if (data !is ClientBadgeManager) return
|
||||
CobblemonClient.clientBadgeData.obtainedBadges.addAll(data.obtainedBadges)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,12 +17,12 @@ import com.cobblemon.mod.common.CobblemonItems
|
||||
import com.cobblemon.mod.common.CobblemonMenuType
|
||||
import com.cobblemon.mod.common.api.berry.Berries
|
||||
import com.cobblemon.mod.common.api.scheduling.ClientTaskTracker
|
||||
import com.cobblemon.mod.common.api.storage.player.client.ClientBadgeManager
|
||||
import com.cobblemon.mod.common.api.storage.player.client.ClientGeneralPlayerData
|
||||
import com.cobblemon.mod.common.api.storage.player.client.ClientPokedexManager
|
||||
import com.cobblemon.mod.common.api.storage.player.client.ClientTMMoveManager
|
||||
import com.cobblemon.mod.common.api.tags.CobblemonItemTags
|
||||
import com.cobblemon.mod.common.block.entity.TintBlockEntity
|
||||
import com.cobblemon.mod.common.block.sign.CobblemonSignBlock
|
||||
import com.cobblemon.mod.common.client.battle.ClientBattle
|
||||
import com.cobblemon.mod.common.client.gui.PartyOverlay
|
||||
import com.cobblemon.mod.common.client.gui.PartyOverlayDataControl
|
||||
@@ -101,6 +101,7 @@ object CobblemonClient {
|
||||
var clientPlayerData = ClientGeneralPlayerData()
|
||||
var clientPokedexData = ClientPokedexManager(mutableMapOf())
|
||||
var clientTMMoveData = ClientTMMoveManager(mutableSetOf())
|
||||
var clientBadgeData = ClientBadgeManager(mutableSetOf())
|
||||
|
||||
/** If true then we won't bother them anymore about choosing a starter even if it's a thing they can do. */
|
||||
var checkedStarterScreen = false
|
||||
@@ -118,6 +119,7 @@ object CobblemonClient {
|
||||
teamData = ClientPlayerTeamData()
|
||||
clientPokedexData = ClientPokedexManager(mutableMapOf())
|
||||
clientTMMoveData = ClientTMMoveManager(mutableSetOf())
|
||||
clientBadgeData = ClientBadgeManager(mutableSetOf())
|
||||
storage.onLogin()
|
||||
// CobblemonDataProvider.canReload = false
|
||||
}
|
||||
|
||||
+167
@@ -0,0 +1,167 @@
|
||||
package com.cobblemon.mod.common.client.gui.trainercard
|
||||
|
||||
import com.cobblemon.mod.common.api.badges.Badge
|
||||
import com.cobblemon.mod.common.api.gui.blitk
|
||||
import com.cobblemon.mod.common.client.CobblemonClient
|
||||
import com.cobblemon.mod.common.client.gui.ScrollingWidget
|
||||
import com.cobblemon.mod.common.client.gui.pokedex.renderTooltip
|
||||
import com.cobblemon.mod.common.util.lang
|
||||
import net.minecraft.client.gui.GuiGraphics
|
||||
import net.minecraft.network.chat.Component
|
||||
import net.minecraft.network.chat.MutableComponent
|
||||
import net.minecraft.util.FastColor
|
||||
import net.minecraft.util.Mth
|
||||
|
||||
class BadgesScrollingWidget(
|
||||
x: Int,
|
||||
y: Int,
|
||||
width: Int,
|
||||
height: Int,
|
||||
private val columns: Int
|
||||
) : ScrollingWidget<BadgesScrollingWidget.BadgeRow>(
|
||||
width = width,
|
||||
height = height,
|
||||
left = x,
|
||||
top = y - height,
|
||||
slotHeight = SLOT_SIZE + SLOT_SPACING
|
||||
) {
|
||||
|
||||
private var tooltip: MutableComponent? = null
|
||||
private var tooltipX = 0
|
||||
private var tooltipY = 0
|
||||
|
||||
companion object {
|
||||
const val SLOT_SIZE = 16
|
||||
const val SLOT_SPACING = 3
|
||||
const val MAX_VISIBLE_ROWS = 2
|
||||
}
|
||||
|
||||
fun createEntries(badges: List<Badge>) {
|
||||
|
||||
val obtainedBadgeList = badges.filter { CobblemonClient.clientBadgeData.obtainedBadges.contains(it.identifier) }
|
||||
.sortedBy { it.order }
|
||||
val unobtainedBadgesList = badges.filter { !obtainedBadgeList.contains(it) }
|
||||
.sortedBy { it.order }
|
||||
val sortedBadgeList = obtainedBadgeList.plus(unobtainedBadgesList)
|
||||
|
||||
sortedBadgeList.chunked(columns).forEach { listChunk ->
|
||||
addEntry(BadgeRow(this, listChunk))
|
||||
}
|
||||
}
|
||||
|
||||
private val visibleRows: Int
|
||||
get() = (height / itemHeight).coerceAtLeast(1)
|
||||
|
||||
private fun hasScrollbar(): Boolean = children().size > visibleRows
|
||||
|
||||
override fun getRowLeft(): Int = if (hasScrollbar()) left + 1 else left
|
||||
override fun getScrollbarPosition(): Int = rowLeft + width - 3
|
||||
|
||||
override fun renderWidget(context: GuiGraphics, mouseX: Int, mouseY: Int, delta: Float) {
|
||||
tooltip = null
|
||||
super.renderWidget(context, mouseX, mouseY, delta)
|
||||
tooltip?.let { renderTooltip(context, it, tooltipX, tooltipY, delta) }
|
||||
}
|
||||
|
||||
fun setTooltip(text: MutableComponent, mouseX: Int, mouseY: Int) {
|
||||
if (mouseX in left until left + width && mouseY in this.y until bottom) {
|
||||
tooltip = text
|
||||
tooltipX = mouseX + 20
|
||||
tooltipY = mouseY - 10
|
||||
}
|
||||
}
|
||||
|
||||
override fun renderScrollbar(context: GuiGraphics, mouseX: Int, mouseY: Int, delta: Float) {
|
||||
if (hasScrollbar()) {
|
||||
val xLeft = this.scrollbarPosition
|
||||
val xRight = xLeft + 3
|
||||
|
||||
val barHeight = this.bottom - this.y
|
||||
|
||||
var yBottom = ((barHeight * barHeight).toFloat() / this.maxPosition.toFloat()).toInt()
|
||||
yBottom = Mth.clamp(yBottom, minOf(32, barHeight), maxOf(minOf(32, barHeight), barHeight - 8))
|
||||
var yTop = scrollAmount.toInt() * (barHeight - yBottom) / this.maxScroll + this.y
|
||||
if (yTop < this.y) {
|
||||
yTop = this.y
|
||||
}
|
||||
|
||||
context.fill(xLeft, this.y, xRight, this.bottom, FastColor.ARGB32.color(255, 75, 75, 75))
|
||||
context.fill(xLeft, yTop, xRight, yTop + yBottom, FastColor.ARGB32.color(255, 141, 141, 141))
|
||||
}
|
||||
}
|
||||
|
||||
private fun renderEntry(context: GuiGraphics, mouseX: Int, mouseY: Int, index: Int, x: Int, y: Int) {
|
||||
getEntry(index).renderRow(context, y, x, mouseX, mouseY)
|
||||
}
|
||||
|
||||
override fun renderListItems(context: GuiGraphics, mouseX: Int, mouseY: Int, delta: Float) {
|
||||
val rowX = rowLeft
|
||||
|
||||
for (index in 0 until this.itemCount) {
|
||||
val rowY = this.getRowTop(index)
|
||||
val o = this.getRowBottom(index)
|
||||
if (o >= this.y && rowY <= this.bottom) {
|
||||
this.renderEntry(context, mouseX, mouseY, index, rowX, rowY)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun mouseDragged(mouseX: Double, mouseY: Double, button: Int, dragX: Double, dragY: Double): Boolean =
|
||||
if (hasScrollbar()) super.mouseDragged(mouseX, mouseY, button, dragX, dragY) else true
|
||||
|
||||
override fun mouseScrolled(mouseX: Double, mouseY: Double, scrollX: Double, scrollY: Double): Boolean =
|
||||
if (hasScrollbar()) super.mouseScrolled(mouseX, mouseY, scrollX, scrollY) else true
|
||||
|
||||
class BadgeRow(
|
||||
val parent: BadgesScrollingWidget,
|
||||
val badgeList: List<Badge?>
|
||||
) : Slot<BadgeRow>() {
|
||||
|
||||
fun renderRow(context: GuiGraphics, y: Int, x: Int, mouseX: Int, mouseY: Int) {
|
||||
badgeList.forEachIndexed { index, badge ->
|
||||
val startPosX = x + ((SLOT_SPACING + SLOT_SIZE) * index)
|
||||
val startPosY = y + SLOT_SPACING
|
||||
|
||||
if (badge != null) {
|
||||
val obtained = CobblemonClient.clientBadgeData.obtainedBadges.contains(badge.identifier)
|
||||
blitk(
|
||||
matrixStack = context.pose(),
|
||||
texture = badge.texture,
|
||||
x = startPosX,
|
||||
y = startPosY,
|
||||
width = SLOT_SIZE,
|
||||
height = SLOT_SIZE,
|
||||
red = if (obtained) 1 else 0,
|
||||
green = if (obtained) 1 else 0,
|
||||
blue = if (obtained) 1 else 0
|
||||
)
|
||||
if (mouseX in startPosX until startPosX + SLOT_SIZE &&
|
||||
mouseY in startPosY until startPosY + SLOT_SIZE
|
||||
) {
|
||||
parent.setTooltip(
|
||||
if (obtained) Component.translatable(badge.name) else lang("badge.unobtained"),
|
||||
mouseX, mouseY
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getNarration(): Component? {
|
||||
return badgeList.firstOrNull()?.let { Component.translatable(it.name) }
|
||||
}
|
||||
|
||||
override fun render(
|
||||
guiGraphics: GuiGraphics,
|
||||
index: Int,
|
||||
top: Int,
|
||||
left: Int,
|
||||
width: Int,
|
||||
height: Int,
|
||||
mouseX: Int,
|
||||
mouseY: Int,
|
||||
hovering: Boolean,
|
||||
partialTick: Float
|
||||
) {}
|
||||
}
|
||||
}
|
||||
+221
@@ -0,0 +1,221 @@
|
||||
package com.cobblemon.mod.common.client.gui.trainercard
|
||||
|
||||
import com.cobblemon.mod.common.Cobblemon
|
||||
import com.cobblemon.mod.common.api.badges.Badges
|
||||
import com.cobblemon.mod.common.api.pokedex.CaughtCount
|
||||
import com.cobblemon.mod.common.api.stats.CobblemonStats.getStat
|
||||
import com.cobblemon.mod.common.api.text.text
|
||||
import com.cobblemon.mod.common.client.CobblemonClient
|
||||
import com.cobblemon.mod.common.client.gui.CobblemonRenderable
|
||||
import com.cobblemon.mod.common.client.render.drawScaledText
|
||||
import com.cobblemon.mod.common.util.cobblemonResource
|
||||
import com.cobblemon.mod.common.util.lang
|
||||
import net.minecraft.client.Minecraft
|
||||
import net.minecraft.client.gui.GuiGraphics
|
||||
import net.minecraft.client.gui.components.Button
|
||||
import net.minecraft.client.gui.screens.Screen
|
||||
import net.minecraft.client.gui.screens.inventory.InventoryScreen
|
||||
import net.minecraft.client.sounds.SoundManager
|
||||
import net.minecraft.network.chat.Component
|
||||
import net.minecraft.network.protocol.game.ServerboundClientCommandPacket
|
||||
import net.minecraft.stats.Stats
|
||||
import net.minecraft.util.CommonColors
|
||||
import kotlin.math.ceil
|
||||
|
||||
class TrainerCardMenu : Screen(
|
||||
Component.translatable("cobblemon.ui.trainercard.title")
|
||||
), CobblemonRenderable {
|
||||
|
||||
private var elapsedTicks = 0
|
||||
private var lastSyncedTimePlayed = 0
|
||||
private var panelLeft = 0
|
||||
private var panelTop = 0
|
||||
private lateinit var badgeScrollList: BadgesScrollingWidget
|
||||
private lateinit var leftPageArrow: TrainerCardPageArrow
|
||||
private lateinit var rightPageArrow: TrainerCardPageArrow
|
||||
private var currentPage = 0
|
||||
private val maxPages = 3
|
||||
|
||||
override fun isPauseScreen(): Boolean = false
|
||||
|
||||
override fun init() {
|
||||
super.init()
|
||||
elapsedTicks = 0
|
||||
lastSyncedTimePlayed = getTimePlayed()
|
||||
// stats need to update when we open the menu, otherwise time played only updates when stats menu is opened
|
||||
Minecraft.getInstance().connection?.send(
|
||||
ServerboundClientCommandPacket(ServerboundClientCommandPacket.Action.REQUEST_STATS)
|
||||
)
|
||||
|
||||
val badges = Badges.all().sortedBy { it.order }
|
||||
val panelWidth = minOf(220, (width - 16).coerceAtLeast(1))
|
||||
panelLeft = (width - panelWidth) / 2
|
||||
val leftColumnWidth = panelWidth / 2
|
||||
val badgeAreaWidth = panelWidth - leftColumnWidth - 8
|
||||
val columns = ((badgeAreaWidth + BadgesScrollingWidget.SLOT_SPACING) /
|
||||
(BadgesScrollingWidget.SLOT_SIZE + BadgesScrollingWidget.SLOT_SPACING)).coerceIn(1, 4)
|
||||
val badgeWidth = columns * BadgesScrollingWidget.SLOT_SIZE +
|
||||
(columns - 1) * BadgesScrollingWidget.SLOT_SPACING + 4
|
||||
val rowCount = ceil(badges.size.toDouble() / columns).toInt().coerceAtLeast(1)
|
||||
val rowHeight = BadgesScrollingWidget.SLOT_SIZE + BadgesScrollingWidget.SLOT_SPACING
|
||||
val badgeHeight = minOf(rowCount, BadgesScrollingWidget.MAX_VISIBLE_ROWS) * rowHeight
|
||||
val panelHeight = maxOf(92, badgeHeight + 20)
|
||||
panelTop = ((height - panelHeight) / 2).coerceAtLeast(8)
|
||||
|
||||
badgeScrollList = BadgesScrollingWidget(
|
||||
x = panelLeft + panelWidth - badgeWidth,
|
||||
y = panelTop + 10,
|
||||
width = badgeWidth,
|
||||
height = badgeHeight,
|
||||
columns = columns
|
||||
)
|
||||
badgeScrollList.createEntries(badges)
|
||||
addRenderableWidget(badgeScrollList)
|
||||
|
||||
leftPageArrow = TrainerCardPageArrow(
|
||||
buttonX = panelLeft - 10,
|
||||
buttonY = panelTop,
|
||||
width = 16,
|
||||
height = 16,
|
||||
onPress = {},
|
||||
parent = this,
|
||||
isRight = false
|
||||
)
|
||||
rightPageArrow = TrainerCardPageArrow(
|
||||
buttonX = panelLeft + panelWidth + 10,
|
||||
buttonY = panelTop,
|
||||
width = 16,
|
||||
height = 16,
|
||||
onPress = {},
|
||||
parent = this,
|
||||
isRight = true
|
||||
)
|
||||
|
||||
addRenderableWidget(leftPageArrow)
|
||||
addRenderableWidget(rightPageArrow)
|
||||
}
|
||||
|
||||
override fun tick() {
|
||||
super.tick()
|
||||
// make our own live playtime update, otherwise we have to send a packet a second to retrieve the playtime stat
|
||||
val timePlayed = getTimePlayed()
|
||||
if (timePlayed != lastSyncedTimePlayed) {
|
||||
lastSyncedTimePlayed = timePlayed
|
||||
elapsedTicks = 0
|
||||
} else { elapsedTicks++ }
|
||||
}
|
||||
|
||||
override fun render(context: GuiGraphics, mouseX: Int, mouseY: Int, partialTick: Float) {
|
||||
when(currentPage) {
|
||||
0 -> {
|
||||
val portraitX = panelLeft
|
||||
val portraitY = minOf(panelTop + 48, (height - 32).coerceAtLeast(0))
|
||||
val portraitSize = 30
|
||||
val statsTextWidth = (badgeScrollList.x - panelLeft - 8).coerceAtLeast(1)
|
||||
|
||||
val player = Minecraft.getInstance().player ?: return // unsure of why this would ever not return a player but if you're here maybe it's because it did
|
||||
val entity = Minecraft.getInstance().level?.getPlayerByUUID(player.uuid) ?: return
|
||||
|
||||
context.enableScissor(portraitX, portraitY, portraitX + portraitSize, portraitY + portraitSize)
|
||||
InventoryScreen.renderEntityInInventoryFollowsMouse(context,
|
||||
portraitX, portraitY, portraitX + portraitSize, portraitY + portraitSize, 28, 0.5f, mouseX.toFloat(), mouseY.toFloat(),
|
||||
entity
|
||||
)
|
||||
|
||||
context.disableScissor()
|
||||
|
||||
val totalSeconds = (getTimePlayed() + elapsedTicks) / 20
|
||||
val timePlayed = "%d:%02d:%02d:%02d".format(
|
||||
totalSeconds / 86_400, // days
|
||||
totalSeconds % 86_400 / 3_600, // hours
|
||||
totalSeconds % 3_600 / 60, // minutes
|
||||
totalSeconds % 60 // seconds
|
||||
)
|
||||
|
||||
drawScaledText(
|
||||
context = context,
|
||||
text = lang("ui.trainermenu.timeplayed", timePlayed),
|
||||
x = panelLeft,
|
||||
y = panelTop + 4,
|
||||
maxCharacterWidth = statsTextWidth
|
||||
)
|
||||
|
||||
drawScaledText(
|
||||
context = context,
|
||||
text = lang("ui.trainermenu.pokemon_registered",
|
||||
CobblemonClient.clientPokedexData.getDexCalculatedValue(cobblemonResource("national"), CaughtCount)),
|
||||
x = panelLeft,
|
||||
y = panelTop + 14,
|
||||
maxCharacterWidth = statsTextWidth
|
||||
)
|
||||
|
||||
val playerBattlesWon = player.stats.getValue(Stats.CUSTOM.get(getStat(Cobblemon.statistics.PVP_BATTLES_WON)))
|
||||
val playerBattlesLost = player.stats.getValue(Stats.CUSTOM.get(getStat(Cobblemon.statistics.PVP_BATTLES_LOST)))
|
||||
|
||||
if (playerBattlesWon > 0 || playerBattlesLost > 0) {
|
||||
drawScaledText(
|
||||
context = context,
|
||||
text = lang("ui.trainermenu.battle_stats",
|
||||
playerBattlesWon,
|
||||
playerBattlesLost
|
||||
),
|
||||
x = panelLeft,
|
||||
y = panelTop + 24,
|
||||
maxCharacterWidth = statsTextWidth
|
||||
)
|
||||
}
|
||||
|
||||
badgeScrollList.render(context, mouseX, mouseY, partialTick)
|
||||
}
|
||||
}
|
||||
|
||||
drawScaledText(
|
||||
context = context,
|
||||
text = Component.literal("$currentPage"),
|
||||
x = panelLeft - 30,
|
||||
y = panelTop
|
||||
)
|
||||
|
||||
if (leftPageArrow.shouldExist()) {
|
||||
leftPageArrow.render(context, mouseX, mouseY, partialTick)
|
||||
}
|
||||
|
||||
if (rightPageArrow.shouldExist()) {
|
||||
rightPageArrow.render(context, mouseX, mouseY, partialTick)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class TrainerCardPageArrow(
|
||||
buttonX: Int,
|
||||
buttonY: Int, width: Int, height: Int, onPress: OnPress,
|
||||
val parent: TrainerCardMenu,
|
||||
val isRight: Boolean
|
||||
): Button(
|
||||
buttonX, buttonY, width, height, "".text(), onPress, DEFAULT_NARRATION
|
||||
) {
|
||||
|
||||
fun shouldExist(): Boolean {
|
||||
if (isRight && parent.currentPage == parent.maxPages - 1) return false
|
||||
if (!isRight && parent.currentPage == 0) return false
|
||||
return true
|
||||
}
|
||||
|
||||
override fun renderWidget(context: GuiGraphics, mouseX: Int, mouseY: Int, partialTick: Float) {
|
||||
context.fill(x, y, x + width, y + height, CommonColors.WHITE)
|
||||
}
|
||||
|
||||
override fun playDownSound(handler: SoundManager) {
|
||||
if (shouldExist()) super.playDownSound(handler)
|
||||
}
|
||||
|
||||
override fun onClick(mouseX: Double, mouseY: Double) {
|
||||
if (!shouldExist()) return
|
||||
parent.currentPage += if (isRight) 1 else -1
|
||||
}
|
||||
}
|
||||
|
||||
private fun getTimePlayed(): Int =
|
||||
Minecraft.getInstance().player?.stats?.getValue(Stats.CUSTOM.get(Stats.PLAY_TIME)) ?: 0
|
||||
|
||||
}
|
||||
@@ -39,6 +39,7 @@ object CobblemonKeyBinds {
|
||||
val PARTY_OVERLAY_UP = this.queue(UpShiftPartyBinding)
|
||||
val SEND_OUT_POKEMON = this.queue(PartySendBinding)
|
||||
val RIDING_FREELOOK = this.queue(RidingFreelookBinding)
|
||||
val TRAINER_MENU = this.queue(TrainerMenuBinding)
|
||||
|
||||
fun register(registrar: (KeyMapping) -> Unit) {
|
||||
this.keyBinds.forEach(registrar::invoke)
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Cobblemon Contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
package com.cobblemon.mod.common.client.keybind.keybinds
|
||||
|
||||
import com.cobblemon.mod.common.Cobblemon
|
||||
import com.cobblemon.mod.common.client.CobblemonClient
|
||||
import com.cobblemon.mod.common.client.gui.summary.Summary
|
||||
import com.cobblemon.mod.common.client.gui.trainercard.TrainerCardMenu
|
||||
import com.cobblemon.mod.common.client.keybind.CobblemonPartyLockedKeyBinding
|
||||
import com.cobblemon.mod.common.client.keybind.KeybindCategories
|
||||
import com.mojang.blaze3d.platform.InputConstants
|
||||
import net.minecraft.client.Minecraft
|
||||
|
||||
object TrainerMenuBinding : CobblemonPartyLockedKeyBinding(
|
||||
"key.cobblemon.trainermenu",
|
||||
InputConstants.Type.KEYSYM,
|
||||
InputConstants.KEY_N,
|
||||
KeybindCategories.COBBLEMON_CATEGORY
|
||||
) {
|
||||
override fun onPress() {
|
||||
Minecraft.getInstance().setScreen(TrainerCardMenu())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.cobblemon.mod.common.command
|
||||
|
||||
import com.cobblemon.mod.common.Cobblemon
|
||||
import com.cobblemon.mod.common.api.permission.CobblemonPermissions
|
||||
import com.cobblemon.mod.common.command.argument.BadgeArgumentType
|
||||
import com.cobblemon.mod.common.util.permission
|
||||
import com.mojang.brigadier.Command
|
||||
import com.mojang.brigadier.CommandDispatcher
|
||||
import com.mojang.brigadier.context.CommandContext
|
||||
import net.minecraft.commands.CommandSourceStack
|
||||
import net.minecraft.commands.Commands
|
||||
import net.minecraft.commands.arguments.EntityArgument
|
||||
import net.minecraft.network.chat.Component
|
||||
import net.minecraft.server.level.ServerPlayer
|
||||
|
||||
object BadgeCommand {
|
||||
|
||||
private const val NAME = "badge"
|
||||
private const val GIVE = "give"
|
||||
private const val REMOVE = "remove"
|
||||
private const val PLAYER = "player"
|
||||
|
||||
fun register(dispatcher: CommandDispatcher<CommandSourceStack>) {
|
||||
val command =
|
||||
Commands.literal(NAME)
|
||||
.permission(CobblemonPermissions.BADGE)
|
||||
.then(Commands.literal(GIVE)
|
||||
.then(Commands.argument(PLAYER, EntityArgument.player())
|
||||
.then(Commands.argument(BADGE, BadgeArgumentType.badge())
|
||||
.executes { executeGive(it, EntityArgument.getPlayer(it, PLAYER)) }
|
||||
)
|
||||
)
|
||||
)
|
||||
.then(Commands.literal(REMOVE)
|
||||
.then(Commands.argument(PLAYER, EntityArgument.player())
|
||||
.then(Commands.argument(BADGE, BadgeArgumentType.badge())
|
||||
.executes { executeRemove(it, EntityArgument.getPlayer(it, PLAYER)) }
|
||||
)
|
||||
)
|
||||
)
|
||||
dispatcher.register(command)
|
||||
}
|
||||
|
||||
private fun executeGive(context: CommandContext<CommandSourceStack>, player: ServerPlayer): Int {
|
||||
val badge = BadgeArgumentType.getBadge(context, BADGE)
|
||||
val changed = Cobblemon.playerDataManager.getBadgeData(player).give(badge)
|
||||
val message = if (changed) {
|
||||
"Gave ${badge.name} to ${player.name.string}."
|
||||
} else {
|
||||
"${player.name.string} already has ${badge.name}."
|
||||
}
|
||||
context.source.sendSuccess({ Component.literal(message) }, true)
|
||||
return Command.SINGLE_SUCCESS
|
||||
}
|
||||
|
||||
private fun executeRemove(context: CommandContext<CommandSourceStack>, player: ServerPlayer): Int {
|
||||
val badge = BadgeArgumentType.getBadge(context, BADGE)
|
||||
val changed = Cobblemon.playerDataManager.getBadgeData(player).remove(badge)
|
||||
val message = if (changed) {
|
||||
"Removed ${badge} from ${player.name.string}." // TODO move this to lang
|
||||
} else {
|
||||
"${player.name.string} does not have ${badge.name}."
|
||||
}
|
||||
context.source.sendSuccess({ Component.literal(message) }, true)
|
||||
return Command.SINGLE_SUCCESS
|
||||
}
|
||||
|
||||
private const val BADGE = "badge"
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Cobblemon Contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
package com.cobblemon.mod.common.command.argument
|
||||
|
||||
import com.cobblemon.mod.common.api.badges.Badge
|
||||
import com.cobblemon.mod.common.api.badges.Badges
|
||||
import com.cobblemon.mod.common.util.asIdentifierDefaultingNamespace
|
||||
import com.mojang.brigadier.StringReader
|
||||
import com.mojang.brigadier.arguments.ArgumentType
|
||||
import com.mojang.brigadier.context.CommandContext
|
||||
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType
|
||||
import com.mojang.brigadier.suggestion.Suggestions
|
||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder
|
||||
import net.minecraft.commands.SharedSuggestionProvider
|
||||
import net.minecraft.network.chat.Component
|
||||
import java.util.concurrent.CompletableFuture
|
||||
|
||||
class BadgeArgumentType : ArgumentType<Badge> {
|
||||
|
||||
override fun parse(reader: StringReader): Badge {
|
||||
val identifier = try {
|
||||
reader.asIdentifierDefaultingNamespace()
|
||||
} catch (exception: Exception) {
|
||||
throw SimpleCommandExceptionType(INVALID_BADGE).createWithContext(reader)
|
||||
}
|
||||
return Badges.getByIdentifier(identifier)
|
||||
?: throw SimpleCommandExceptionType(INVALID_BADGE).createWithContext(reader)
|
||||
}
|
||||
|
||||
override fun <S : Any> listSuggestions(
|
||||
context: CommandContext<S>,
|
||||
builder: SuggestionsBuilder
|
||||
): CompletableFuture<Suggestions> {
|
||||
return SharedSuggestionProvider.suggest(Badges.all().map { it.identifier.toString() }, builder)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val INVALID_BADGE = Component.translatable("cobblemon.command.badge.invalid")
|
||||
|
||||
fun badge() = BadgeArgumentType()
|
||||
|
||||
fun <S> getBadge(context: CommandContext<S>, name: String): Badge =
|
||||
context.getArgument(name, Badge::class.java)
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ package com.cobblemon.mod.common.data
|
||||
import com.cobblemon.mod.common.*
|
||||
import com.cobblemon.mod.common.Cobblemon.LOGGER
|
||||
import com.cobblemon.mod.common.api.abilities.Abilities
|
||||
import com.cobblemon.mod.common.api.badges.Badges
|
||||
import com.cobblemon.mod.common.api.berry.Berries
|
||||
import com.cobblemon.mod.common.api.cooking.Seasonings
|
||||
import com.cobblemon.mod.common.api.data.DataProvider
|
||||
@@ -120,6 +121,7 @@ object CobblemonDataProvider : DataProvider {
|
||||
this.register(SpawnBaitEffects, reloadable = false)
|
||||
this.register(CobblemonRideSettings, reloadable = true)
|
||||
this.register(TechnicalMachines, reloadable = false)
|
||||
this.register(Badges, reloadable = true)
|
||||
SpawnBait.Effects.setupEffects()
|
||||
|
||||
PlatformEvents.SERVER_PLAYER_LOGOUT.subscribe {
|
||||
|
||||
@@ -78,10 +78,21 @@ object StatHandler : EventHandler {
|
||||
|
||||
fun onWinBattle(event : BattleVictoryEvent) {
|
||||
if (!event.wasWildCapture) {
|
||||
if(event.battle.isPvP) {
|
||||
event.winners
|
||||
.flatMap { it.getPlayerUUIDs().mapNotNull(UUID::getPlayer) }
|
||||
.forEach { player -> player.awardStat(getStat(Cobblemon.statistics.PVP_BATTLES_WON)) }
|
||||
event.losers
|
||||
.flatMap { it.getPlayerUUIDs().mapNotNull(UUID::getPlayer) }
|
||||
.forEach { player -> player.awardStat(getStat(Cobblemon.statistics.PVP_BATTLES_LOST)) }
|
||||
}
|
||||
if (event.battle.isPvW) {
|
||||
event.winners
|
||||
.flatMap { it.getPlayerUUIDs().mapNotNull(UUID::getPlayer) }
|
||||
.forEach { player -> player.awardStat(getStat(Cobblemon.statistics.BATTLES_WON)) }
|
||||
event.losers
|
||||
.flatMap { it.getPlayerUUIDs().mapNotNull(UUID::getPlayer) }
|
||||
.forEach { player -> player.awardStat(getStat(Cobblemon.statistics.BATTLES_LOST)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Cobblemon Contributors
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
package com.cobblemon.mod.common.net.messages.client.data
|
||||
|
||||
import com.cobblemon.mod.common.api.badges.Badge
|
||||
import com.cobblemon.mod.common.api.badges.Badges
|
||||
import com.cobblemon.mod.common.util.cobblemonResource
|
||||
import com.cobblemon.mod.common.util.readIdentifier
|
||||
import com.cobblemon.mod.common.util.readString
|
||||
import com.cobblemon.mod.common.util.writeIdentifier
|
||||
import com.cobblemon.mod.common.util.writeString
|
||||
import net.minecraft.network.RegistryFriendlyByteBuf
|
||||
|
||||
class BadgeRegistrySyncPacket(badges: List<Badge>) : DataRegistrySyncPacket<Badge, BadgeRegistrySyncPacket>(badges) {
|
||||
companion object {
|
||||
val ID = cobblemonResource("badges")
|
||||
fun decode(buffer: RegistryFriendlyByteBuf) = BadgeRegistrySyncPacket(emptyList()).apply { decodeBuffer(buffer) }
|
||||
}
|
||||
|
||||
|
||||
override val id = ID
|
||||
override fun encodeEntry(buffer: RegistryFriendlyByteBuf, entry: Badge) {
|
||||
buffer.writeIdentifier(entry.identifier)
|
||||
buffer.writeString(entry.name)
|
||||
buffer.writeResourceLocation(entry.texture)
|
||||
buffer.writeInt(entry.order)
|
||||
}
|
||||
|
||||
override fun decodeEntry(buffer: RegistryFriendlyByteBuf): Badge {
|
||||
return Badge(
|
||||
order = buffer.readInt(),
|
||||
texture = buffer.readResourceLocation(),
|
||||
name = buffer.readString(),
|
||||
identifier = buffer.readIdentifier()
|
||||
)
|
||||
}
|
||||
|
||||
override fun synchronizeDecoded(entries: Collection<Badge>) {
|
||||
Badges.reload(entries.associateBy { it.identifier })
|
||||
}
|
||||
}
|
||||
@@ -844,6 +844,19 @@
|
||||
"cobblemon.fossil.root": "Root",
|
||||
"cobblemon.fossil.sail": "Sail",
|
||||
"cobblemon.fossil.skull": "Skull",
|
||||
|
||||
"cobblemon.badge.unobtained": "?????",
|
||||
"cobblemon.badge.awesome_badge": "Awesome Badge",
|
||||
"cobblemon.badge.awesome_badge2": "Awesome Badge 2",
|
||||
"cobblemon.badge.dummy_badge": "Dummy Badge",
|
||||
"cobblemon.badge.dummy_badge2": "Dummy Badge 2",
|
||||
"cobblemon.badge.funny_badge": "Funny Badge",
|
||||
"cobblemon.badge.funny_badge2": "Funny Badge 2",
|
||||
"cobblemon.badge.interesting_badge": "Interesting Badge",
|
||||
"cobblemon.badge.interesting_badge2": "Interesting Badge 2",
|
||||
"cobblemon.badge.weird_badge": "Weird Badge",
|
||||
"cobblemon.badge.weird_badge2": "Weird Badge 2",
|
||||
|
||||
"cobblemon.gender.female": "Female",
|
||||
"cobblemon.gender.male": "Male",
|
||||
"cobblemon.gender.genderless": "Genderless",
|
||||
@@ -1199,10 +1212,13 @@
|
||||
"cobblemon.ui.edit.habitat.validation.min_light": "Min Light must be between 0 and 15.",
|
||||
"cobblemon.ui.edit.habitat.validation.max_light": "Max Light must be between 0 and 15.",
|
||||
|
||||
|
||||
"cobblemon.ui.pokenav.title": "PokéNav",
|
||||
"cobblemon.ui.pokenav.narrator.backbutton": "PokéNav button pressed",
|
||||
|
||||
"cobblemon.ui.trainermenu.timeplayed": "Time played: %1$s",
|
||||
"cobblemon.ui.trainermenu.pokemon_registered": "Pokémon registered: %1$s",
|
||||
"cobblemon.ui.trainermenu.battle_stats": "Win/loss rate: %1$s/%2$s",
|
||||
|
||||
"cobblemon.ui.summary.title": "Summary",
|
||||
"cobblemon.ui.starter.title": "Starter Selection",
|
||||
"cobblemon.ui.starter.choosebutton": "I choose you!",
|
||||
@@ -2387,6 +2403,7 @@
|
||||
"cobblemon.command.takemark": "%1$s was taken from %2$s's %3$s.",
|
||||
"cobblemon.command.takemark.does_not_have": "%1$s's %2$s does not have a %3$s!",
|
||||
"cobblemon.command.mark.invalid": "Invalid mark name.",
|
||||
"cobblemon.command.badge.invalid": "Invalid badge identifier.",
|
||||
"cobblemon.command.molang.out": "MoLang output: %1$s",
|
||||
"cobblemon.command.pokeheal.in_battle": "You cannot heal your party while in a battle!",
|
||||
"cobblemon.command.spawnpokemonfrompool": "A %1$s has spawned in the area.",
|
||||
@@ -9584,8 +9601,10 @@
|
||||
"cobblemon.snapshots.warning.dont_show_again_tip": "This setting may be ignored on newer Cobblemon snapshots (e.g. 1.7.0 vs 1.8.0)",
|
||||
"stat.cobblemon.battles_fled": "Pokémon Battles Fled",
|
||||
"stat.cobblemon.battles_lost": "Pokémon Battles Lost",
|
||||
"stat.cobblemon.pvp_battles_lost": "Trainer Battles Lost",
|
||||
"stat.cobblemon.battles_total": "Pokémon Total Battles",
|
||||
"stat.cobblemon.battles_won": "Pokémon Battles Won",
|
||||
"stat.cobblemon.pvp_battles_won": "Trainer Battles Won",
|
||||
"stat.cobblemon.captured": "Pokémon Captured",
|
||||
"stat.cobblemon.dex_entries": "Pokémon Pokédex Entries",
|
||||
"stat.cobblemon.eggs_collected": "Pokémon Eggs Collected",
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "cobblemon.badge.awesome_badge",
|
||||
"texture": "cobblemon:textures/gui/mark/mark_alpha.png",
|
||||
"order": 2
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "cobblemon.badge.awesome_badge2",
|
||||
"texture": "cobblemon:textures/gui/mark/mark_alpha.png",
|
||||
"order": 6
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "cobblemon.badge.dummy_badge",
|
||||
"texture": "cobblemon:textures/gui/mark/mark_gourmand.png",
|
||||
"order": 1
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "cobblemon.badge.dummy_badge",
|
||||
"texture": "cobblemon:textures/gui/mark/mark_gourmand.png",
|
||||
"order": 7
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "cobblemon.badge.funny_badge",
|
||||
"texture": "cobblemon:textures/gui/mark/mark_destiny.png",
|
||||
"order": 5
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "cobblemon.badge.funny_badge",
|
||||
"texture": "cobblemon:textures/gui/mark/mark_destiny.png",
|
||||
"order": 8
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "cobblemon.badge.interesting_badge",
|
||||
"texture": "cobblemon:textures/gui/mark/mark_fishing.png",
|
||||
"order": 4
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "cobblemon.badge.interesting_badge",
|
||||
"texture": "cobblemon:textures/gui/mark/mark_fishing.png",
|
||||
"order": 9
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "cobblemon.badge.weird_badge",
|
||||
"texture": "cobblemon:textures/gui/mark/mark_curry.png",
|
||||
"order": 3
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "cobblemon.badge.weird_badge",
|
||||
"texture": "cobblemon:textures/gui/mark/mark_curry.png",
|
||||
"order": 10
|
||||
}
|
||||
Reference in New Issue
Block a user