Skip to content

Commit

Permalink
feat(SavePlayerDeck): saves the player's deck to dunga dunga
Browse files Browse the repository at this point in the history
remove debug logs

remove debug logs

use NBT data to determine if the item is a deck or a card
  • Loading branch information
CaseyMichael committed Jan 7, 2024
1 parent c506ea8 commit a5c877a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
6 changes: 2 additions & 4 deletions src/main/kotlin/org/trackedout/citadel/Citadel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.bukkit.plugin.java.JavaPlugin
import org.bukkit.scheduler.BukkitRunnable
import org.trackedout.citadel.commands.GiveShulkerCommand
import org.trackedout.citadel.commands.InventoryCommand
import org.trackedout.citadel.commands.LogEventCommand
import org.trackedout.citadel.commands.TakeShulkerCommand
import org.trackedout.citadel.commands.*
import org.trackedout.citadel.listeners.PlayedJoinedListener
import org.trackedout.client.apis.EventsApi
import org.trackedout.client.apis.InventoryApi
Expand Down Expand Up @@ -58,6 +55,7 @@ class Citadel : JavaPlugin() {
manager.registerCommand(GiveShulkerCommand(eventsApi, inventoryApi))
manager.registerCommand(InventoryCommand(eventsApi, inventoryApi))
manager.registerCommand(LogEventCommand(eventsApi))
manager.registerCommand(SavePlayerDeckCommand(inventoryApi))
manager.setDefaultExceptionHandler { _, _, sender, _, throwable ->
sender.sendMessage("Error executing command: ${throwable.message}")

Expand Down
8 changes: 7 additions & 1 deletion src/main/kotlin/org/trackedout/citadel/PlayerExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.trackedout.citadel

import com.saicone.rtag.RtagItem
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.NamedTextColor
import org.bukkit.Material
import org.bukkit.command.CommandSender
import org.bukkit.inventory.ItemStack
import org.trackedout.citadel.commands.DECK_NAME
import org.trackedout.citadel.commands.TAG_CARD_KEY

fun CommandSender.sendGreenMessage(message: String) {
this.sendMessage(Component.text().color(NamedTextColor.GREEN).content(message).build())
Expand All @@ -18,4 +21,7 @@ fun CommandSender.sendGreyMessage(message: String) {
this.sendMessage(Component.text().color(NamedTextColor.GRAY).content(message).build())
}

fun ItemStack.isDeckedOutShulker() = this.type == Material.CYAN_SHULKER_BOX // TODO: Validate using NBT data; validate that this player owns it
fun ItemStack.isDeckedOutShulker() =
RtagItem(this).get<String>("display", "Name") == DECK_NAME && this.type == Material.CYAN_SHULKER_BOX

fun ItemStack.isDeckedOutCard() = !RtagItem(this).get<String>(TAG_CARD_KEY).isNullOrEmpty()
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import org.trackedout.client.apis.InventoryApi
import org.trackedout.client.models.Card
import org.trackedout.data.Cards

const val TAG_CARD_KEY = "decked-out-card-key"
const val DECK_NAME = "{\"text\":\"❄☠ Frozen Assets ☠❄\"}"
@CommandAlias("gief-shulker|give-shulker") // Spelling is intentional
class GiveShulkerCommand(
private val eventsApi: EventsApi,
Expand Down Expand Up @@ -95,11 +97,9 @@ class GiveShulkerCommand(
shulker.itemMeta = blockStateMeta

shulker = RtagItem.edit(shulker, fun(tag: RtagItem): ItemStack {
val nameJson = "{\"text\":\"❄☠ Frozen Assets ☠❄\"}"
tag.set(nameJson, "display", "Name")
tag.set(DECK_NAME, "display", "Name")
tag.set(player.name, "owner")
tag.set(player.identity().uuid().toString(), "owner-id")

return tag.loadCopy();
})

Expand All @@ -115,8 +115,7 @@ class GiveShulkerCommand(
val nameJson = "{\"color\":\"${it.colour}\",\"text\":\"${it.displayName}\"}"
tag.set(nameJson, "display", "Name")
tag.set("{\"color\":\"${it.colour}\",\"OriginalName\":\"${nameJson}\"}", "display", "NameFormat");
// NameFormat: {color: "gray", OriginalName: '{"color":"gray","text":"✲ Moment of Clarity ✲"}',
// ModifiedName: '{"color":"gray","text":"✲ Moment of Clarity ✲"}'}
tag.set(it.key, TAG_CARD_KEY)

return tag.loadCopy()
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.trackedout.citadel.commands

import co.aikar.commands.BaseCommand
import co.aikar.commands.annotation.CommandAlias
import co.aikar.commands.annotation.CommandPermission
import co.aikar.commands.annotation.Default
import co.aikar.commands.annotation.Description
import net.kyori.adventure.text.TextComponent
import org.bukkit.block.ShulkerBox
import org.bukkit.entity.Player
import org.bukkit.inventory.meta.BlockStateMeta
import org.trackedout.citadel.isDeckedOutCard
import org.trackedout.citadel.isDeckedOutShulker
import org.trackedout.client.apis.InventoryApi
import org.trackedout.data.Cards

@CommandAlias("save-shulker")
class SavePlayerDeckCommand(
private val inventoryAPI: InventoryApi
): BaseCommand() {

@Default
@CommandPermission("decked-out.inventory.save-player-deck")
@Description("Saves the player's deck with the current cards in their shulker")
fun savePlayerDeck(player: Player) {
val shulkerItemStack = player.inventory.storageContents.find { item -> item?.isDeckedOutShulker() == true }
if (shulkerItemStack == null) {
return
}
if (shulkerItemStack.itemMeta !is BlockStateMeta) {
return
}
val blockMeta = shulkerItemStack.itemMeta as BlockStateMeta
if (blockMeta.blockState !is ShulkerBox) {
return
}
val deck = blockMeta.blockState as ShulkerBox
val cards = deck.inventory.storageContents
.filterNotNull()
.filter { item -> item.isDeckedOutCard() }
.flatMap { item ->
val text = item.itemMeta.displayName() as TextComponent
val cardDisplayName = text.content()
val card = Cards.findCard(cardDisplayName)
val cards = mutableListOf<String>()
repeat(item.amount) {
if (card != null) {
cards.add(card.key)
}
}
cards
}
.map { item -> item }
.toTypedArray()

inventoryAPI.inventoryOverwritePlayersDeck(player = player.name, deckId = "1", cards = cards)
}
}

0 comments on commit a5c877a

Please sign in to comment.