Skip to content

Commit

Permalink
Some changes.
Browse files Browse the repository at this point in the history
- Allow Bedrock players to see interactive books as forms
- Replace separator spaces with dashes (-) when on Bedrock
- Misc changes
  • Loading branch information
NickAcPT committed May 4, 2021
1 parent b0d70ab commit 2b0980a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,8 @@ operator fun <T0, T1, T2, T3> KProperty1<T0, T1?>.div(p2: KProperty1<T2, T3?>):
@Suppress("INVISIBLE_MEMBER")
(KPropertyPath(this, p2))


val separator = text(" ".repeat(64), Style.style(TextDecoration.STRIKETHROUGH))

fun separator(color: TextColor = NamedTextColor.BLUE): Component {
return separator.style {
return text(" ".repeat(32), Style.style(TextDecoration.STRIKETHROUGH)).style {
it.decorate(TextDecoration.STRIKETHROUGH)
it.color(color)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.bukkit.Bukkit
import org.bukkit.ChatColor.getLastColors
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import org.geysermc.floodgate.api.FloodgateApi
import kotlin.time.Duration

open class ArcadePlayer(val data: ArcadePlayerData) : ArcadeSender(data.uuid) {
Expand All @@ -29,8 +30,10 @@ open class ArcadePlayer(val data: ArcadePlayerData) : ArcadeSender(data.uuid) {
} else false
}

private val floodgateAudience: FloodgateAudience by lazy { FloodgateAudience(this) }

open override val audience: Audience
get() = commandSender
get() = (if (isFloodgatePlayer) floodgateAudience else player) ?: Audience.empty()

override val extraData: MutableMap<String, ExtraDataValue>
get() = data.extraData
Expand Down Expand Up @@ -159,36 +162,7 @@ open class ArcadePlayer(val data: ArcadePlayerData) : ArcadeSender(data.uuid) {
override fun hashCode(): Int {
return uuid.hashCode()
}
}
/*
TODO: Player games / party data
fun getOrCreateParty(): Party {
if (getCurrentParty() == null) {
return PartyManager.createParty(this)
}
return getCurrentParty() as Party
}
fun getCurrentGame(): Game? {
return MiniGameManager.getCurrentGame(this)
}
fun getCurrentParty(showPrompt: Boolean = false): Party? {
return PartyManager.getParty(this).also {
if (it == null && showPrompt) {
audience.sendMessage(separator {
append(text("You are not currently in a party.", NamedTextColor.RED))
})
}
}
}
fun setCurrentParty(party: Party?) {
return PartyManager.setPlayerParty(this, party)
}

*/
val isFloodgatePlayer: Boolean
get() = player?.uniqueId?.let { FloodgateApi.getInstance().isFloodgatePlayer(it) } ?: false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.github.openminigameserver.nickarcade.core.data.sender.player

import net.kyori.adventure.audience.Audience
import net.kyori.adventure.audience.MessageType
import net.kyori.adventure.identity.Identity
import net.kyori.adventure.inventory.Book
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.Component.text
import net.kyori.adventure.text.event.ClickEvent
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer
import org.geysermc.cumulus.SimpleForm
import org.geysermc.floodgate.api.FloodgateApi

class FloodgateAudience(val player: ArcadePlayer) : Audience by player.audience {
override fun openBook(book: Book) {

val builder = SimpleForm.builder().apply {
title(PlainComponentSerializer.plain().serialize(book.title()))
val page = book.pages().first()

val (content, actions) = page.children().partition { it.clickEvent() == null }
val actionsMap = mapOf(*(actions.map {
LegacyComponentSerializer.legacySection().serialize(it.replaceText { textReplace ->
textReplace.matchLiteral("").replacement("")
}) to it
}).toTypedArray())

var contentResult = Component.empty()
content.forEach {
contentResult = contentResult.append(it)
}

val text = LegacyComponentSerializer.legacySection().serialize(contentResult)
content(text)

actionsMap.forEach { (text, _) ->
button(text)
}
this.responseHandler { form, resultStr ->
val result = form.parseResponse(resultStr)
if (result.isCorrect) {
val component = actionsMap[result.clickedButton?.text]
val clickEvent = component?.clickEvent() ?: return@responseHandler

if (clickEvent.action() == ClickEvent.Action.RUN_COMMAND) {
player.player?.chat(clickEvent.value())
}
}
}
}
FloodgateApi.getInstance().sendForm(player.uuid, builder)
}

override fun sendMessage(source: Identity, message: Component, type: MessageType) {
player.audience.sendMessage(source, message.replaceText {
it.matchLiteral(" ")
it.replacement { c ->
val build = c.build()
return@replacement text("--", build.color())
}
}, type)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.github.openminigameserver.nickarcade.core.ui

import com.github.stefvanschie.inventoryframework.gui.GuiItem
import com.github.stefvanschie.inventoryframework.gui.type.ChestGui
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.ComponentLike
import net.kyori.adventure.text.format.TextDecoration
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta
import org.checkerframework.checker.nullness.qual.NonNull
Expand All @@ -17,6 +19,10 @@ fun ItemStack.itemMeta(code: ItemMeta.() -> Unit): ItemStack {
return itemMeta<ItemMeta>(code)
}

fun guiItem(item: ItemStack, handler: InventoryClickEvent.() -> Unit = {isCancelled = true}): GuiItem {
return GuiItem(item, handler)
}

@JvmName("itemMetaGeneric")
fun <T : ItemMeta> ItemStack.itemMeta(code: T.() -> Unit): ItemStack {
return this.apply {
Expand Down

0 comments on commit 2b0980a

Please sign in to comment.