Skip to content

Commit

Permalink
Fix adapters not loading without Geyser
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Nov 27, 2023
1 parent 00df246 commit 77a44c4
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class DropItemActionEntry(
}

@EntryMigration(DropItemActionEntry::class, "0.4.0")
private fun migrate040(json: JsonObject, context: EntryMigratorContext): JsonObject {
fun migrate040DropItemAction(json: JsonObject, context: EntryMigratorContext): JsonObject {
val data = JsonObject()
data.copyAllBut(json, "material", "amount", "displayName", "lore")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GiveItemActionEntry(
}

@EntryMigration(GiveItemActionEntry::class, "0.4.0")
private fun migrate040(json: JsonObject, context: EntryMigratorContext): JsonObject {
fun migrate040GiveItemAction(json: JsonObject, context: EntryMigratorContext): JsonObject {
val data = JsonObject()
data.copyAllBut(json, "material", "amount", "displayName", "lore")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class RemoveItemActionEntry(
}

@EntryMigration(RemoveItemActionEntry::class, "0.4.0")
private fun migrate040(json: JsonObject, context: EntryMigratorContext): JsonObject {
fun migrate040RemoveItemAction(json: JsonObject, context: EntryMigratorContext): JsonObject {
val data = JsonObject()
data.copyAllBut(json, "material", "amount", "itemName")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,51 @@ import me.gabber235.typewriter.extensions.placeholderapi.parsePlaceholders
import me.gabber235.typewriter.utils.isFloodgate
import me.gabber235.typewriter.utils.legacy
import org.bukkit.entity.Player
import org.geysermc.cumulus.form.CustomForm
import org.geysermc.floodgate.api.FloodgateApi

@Messenger(OptionDialogueEntry::class, priority = 5)
class BedrockOptionDialogueDialogueMessenger(player: Player, entry: OptionDialogueEntry) :
DialogueMessenger<OptionDialogueEntry>(player, entry) {

companion object : MessengerFilter {
override fun filter(player: Player, entry: DialogueEntry): Boolean = player.isFloodgate
}

private var selectedIndex = 0
private val selected get() = usableOptions[selectedIndex]

private var usableOptions: List<Option> = emptyList()

override val triggers: List<String>
get() = entry.triggers + selected.triggers

override val modifiers: List<Modifier>
get() = entry.modifiers + selected.modifiers


override fun init() {
super.init()
usableOptions = entry.options.filter { it.criteria.matches(player.uniqueId) }
FloodgateApi.getInstance().sendForm(
player.uniqueId,
CustomForm.builder()
.title("<bold>${entry.speakerDisplayName}</bold>".legacy())
.label("${entry.text.parsePlaceholders(player).legacy()}\n\n\n")
.dropdown(
"Select Response",
usableOptions.map { it.text.parsePlaceholders(player).legacy() })
.label("\n\n\n\n")
.closedOrInvalidResultHandler { _, _ ->
state = MessengerState.CANCELLED
}
.validResultHandler { responds ->
val dropdown = responds.asDropdown()
selectedIndex = dropdown
state = MessengerState.FINISHED
}
)
}

override fun end() {
// Do nothing as we don't need to resend the messages.
}
DialogueMessenger<OptionDialogueEntry>(player, entry) {

companion object : MessengerFilter {
override fun filter(player: Player, entry: DialogueEntry): Boolean = player.isFloodgate
}

private var selectedIndex = 0
private val selected get() = usableOptions[selectedIndex]

private var usableOptions: List<Option> = emptyList()

override val triggers: List<String>
get() = entry.triggers + selected.triggers

override val modifiers: List<Modifier>
get() = entry.modifiers + selected.modifiers


override fun init() {
super.init()
usableOptions = entry.options.filter { it.criteria.matches(player.uniqueId) }
org.geysermc.floodgate.api.FloodgateApi.getInstance().sendForm(
player.uniqueId,
org.geysermc.cumulus.form.CustomForm.builder()
.title("<bold>${entry.speakerDisplayName}</bold>".legacy())
.label("${entry.text.parsePlaceholders(player).legacy()}\n\n\n")
.dropdown(
"Select Response",
usableOptions.map { it.text.parsePlaceholders(player).legacy() })
.label("\n\n\n\n")
.closedOrInvalidResultHandler { _, _ ->
state = MessengerState.CANCELLED
}
.validResultHandler { responds ->
val dropdown = responds.asDropdown()
selectedIndex = dropdown
state = MessengerState.FINISHED
}
)
}

override fun end() {
// Do nothing as we don't need to resend the messages.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,31 @@ import me.gabber235.typewriter.extensions.placeholderapi.parsePlaceholders
import me.gabber235.typewriter.utils.isFloodgate
import me.gabber235.typewriter.utils.legacy
import org.bukkit.entity.Player
import org.geysermc.cumulus.form.SimpleForm
import org.geysermc.floodgate.api.FloodgateApi

@Messenger(RandomSpokenDialogueEntry::class, priority = 5)

class BedrockRandomSpokenDialogueDialogueMessenger(player: Player, entry: RandomSpokenDialogueEntry) :
DialogueMessenger<RandomSpokenDialogueEntry>(player, entry) {
DialogueMessenger<RandomSpokenDialogueEntry>(player, entry) {

companion object : MessengerFilter {
override fun filter(player: Player, entry: DialogueEntry): Boolean = player.isFloodgate
}
companion object : MessengerFilter {
override fun filter(player: Player, entry: DialogueEntry): Boolean = player.isFloodgate
}

override fun init() {
super.init()
val message = entry.messages.randomOrNull() ?: return
FloodgateApi.getInstance().sendForm(
player.uniqueId,
SimpleForm.builder()
.title("<bold>${entry.speakerDisplayName}</bold>".legacy())
.content("${message.parsePlaceholders(player).legacy()}\n\n")
.button("Continue")
.closedOrInvalidResultHandler { _, _ ->
state = MessengerState.CANCELLED
}
.validResultHandler { _, _ ->
state = MessengerState.FINISHED
}
)
}
override fun init() {
super.init()
val message = entry.messages.randomOrNull() ?: return
org.geysermc.floodgate.api.FloodgateApi.getInstance().sendForm(
player.uniqueId,
org.geysermc.cumulus.form.SimpleForm.builder()
.title("<bold>${entry.speakerDisplayName}</bold>".legacy())
.content("${message.parsePlaceholders(player).legacy()}\n\n")
.button("Continue")
.closedOrInvalidResultHandler { _, _ ->
state = MessengerState.CANCELLED
}
.validResultHandler { _, _ ->
state = MessengerState.FINISHED
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,33 @@ import me.gabber235.typewriter.extensions.placeholderapi.parsePlaceholders
import me.gabber235.typewriter.utils.isFloodgate
import me.gabber235.typewriter.utils.legacy
import org.bukkit.entity.Player
import org.geysermc.cumulus.form.SimpleForm
import org.geysermc.floodgate.api.FloodgateApi

@Messenger(SpokenDialogueEntry::class, priority = 5)
class BedrockSpokenDialogueDialogueMessenger(player: Player, entry: SpokenDialogueEntry) :
DialogueMessenger<SpokenDialogueEntry>(player, entry) {
DialogueMessenger<SpokenDialogueEntry>(player, entry) {

companion object : MessengerFilter {
override fun filter(player: Player, entry: DialogueEntry): Boolean = player.isFloodgate
}
companion object : MessengerFilter {
override fun filter(player: Player, entry: DialogueEntry): Boolean = player.isFloodgate
}

override fun init() {
super.init()
FloodgateApi.getInstance().sendForm(
player.uniqueId,
SimpleForm.builder()
.title("<bold>${entry.speakerDisplayName}</bold>".legacy())
.content("${entry.text.parsePlaceholders(player).legacy()}\n\n")
.button("Continue")
.closedOrInvalidResultHandler { _, _ ->
state = MessengerState.CANCELLED
}
.validResultHandler { _, _ ->
state = MessengerState.FINISHED
}
)
}
override fun init() {
super.init()
org.geysermc.floodgate.api.FloodgateApi.getInstance().sendForm(
player.uniqueId,
org.geysermc.cumulus.form.SimpleForm.builder()
.title("<bold>${entry.speakerDisplayName}</bold>".legacy())
.content("${entry.text.parsePlaceholders(player).legacy()}\n\n")
.button("Continue")
.closedOrInvalidResultHandler { _, _ ->
state = MessengerState.CANCELLED
}
.validResultHandler { _, _ ->
state = MessengerState.FINISHED
}
)
}

override fun end() {
// Do nothing as we don't need to resend the messages.
}
override fun end() {
// Do nothing as we don't need to resend the messages.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fun onPickupItem(event: EntityPickupItemEvent, query: Query<PickupItemEventEntry
}

@EntryMigration(PickupItemEventEntry::class, "0.4.0")
private fun migrate040(json: JsonObject, context: EntryMigratorContext): JsonObject {
fun migrate040PickupItemEvent(json: JsonObject, context: EntryMigratorContext): JsonObject {
val data = JsonObject()
data.copyAllBut(json, "material")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class InventoryItemCountFact(
}

@EntryMigration(InventoryItemCountFact::class, "0.4.0")
private fun migrate040(json: JsonObject, context: EntryMigratorContext): JsonObject {
fun migrate040InventoryItemCount(json: JsonObject, context: EntryMigratorContext): JsonObject {
val data = JsonObject()
data.copyAllBut(json, "material", "itemName")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import me.gabber235.typewriter.entry.dialogue.DialogueMessenger
import me.gabber235.typewriter.entry.entries.DialogueEntry
import me.gabber235.typewriter.logger
import me.gabber235.typewriter.plugin
import me.gabber235.typewriter.utils.Icons
import me.gabber235.typewriter.utils.RuntimeTypeAdapterFactory
import me.gabber235.typewriter.utils.get
import me.gabber235.typewriter.utils.*
import org.koin.core.component.KoinComponent
import java.io.File
import java.net.URLClassLoader
Expand Down Expand Up @@ -89,6 +87,34 @@ class AdapterLoaderImpl : AdapterLoader, KoinComponent {
jsonArray.add(gson.toJsonTree(it))
}

if (adapters.isEmpty()) {
logger.warning(
"""
|
|${"-".repeat(15)}{ No Adapters Loaded }${"-".repeat(15)}
|
|No adapters were loaded.
|You should always have at least the BasicAdapter loaded.
|
|${"-".repeat(50)}
""".trimMargin()
)
} else {
val maxAdapterLength = adapters.maxOf { it.name.length }
val maxVersionLength = adapters.maxOf { it.version.length }
val maxDigits = adapters.maxOf { it.entries.size.digits }
logger.info(
"""
|
|${"-".repeat(15)}{ Loaded Adapters }${"-".repeat(15)}
|
|${adapters.joinToString("\n") { it.displayString(maxAdapterLength, maxVersionLength, maxDigits) }}
|
|${"-".repeat(50)}
""".trimMargin()
)
}

adaptersJson = jsonArray
}

Expand Down Expand Up @@ -194,7 +220,7 @@ class AdapterLoaderImpl : AdapterLoader, KoinComponent {
private fun constructCapturers(captureClasses: List<Class<*>>) =
captureClasses.map { captureClass ->
captureClass.kotlin as KClass<out Capturer<*>>
} + staticCaptureClasses
}

//TODO: Make compatible with java.
private fun findFilterForMessenger(messengerClass: Class<*>) =
Expand Down Expand Up @@ -234,7 +260,7 @@ class AdapterLoaderImpl : AdapterLoader, KoinComponent {
}

override fun getCaptureClasses(): List<KClass<out Capturer<*>>> {
return adapters.asSequence().flatMap { it.captureClasses }.toList()
return adapters.asSequence().flatMap { it.captureClasses }.toList() + staticCaptureClasses
}

override fun getEntryMigrators(): List<EntryMigrator> {
Expand All @@ -257,7 +283,27 @@ data class AdapterData(
val entryMigrators: List<EntryMigrator>,
@Transient
val clazz: Class<*>,
)
) {
/**
* Returns a string that can be used to display information about the adapter.
* It is nicely formatted to align the information between adapters.
*/
fun displayString(maxAdapterLength: Int, maxVersionLength: Int, maxDigits: Int): String {
var display = "${name}Adapter".rightPad(maxAdapterLength + "Adapter".length)
display += " (${version})".rightPad(maxVersionLength + 2)
display += padCount("📚", entries.size, maxDigits)
display += padCount("👂", eventListeners.size, maxDigits)
display += padCount("💬", messengers.size, maxDigits)
display += padCount("📸", captureClasses.size, maxDigits)
display += padCount("🚚", entryMigrators.size, maxDigits)

return display
}

private fun padCount(prefix: String, count: Int, maxDigits: Int): String {
return " ${prefix}: ${" ".repeat((maxDigits - count.digits).coerceAtLeast(0))}$count"
}
}

// Annotation for marking a class as an adapter
@Target(AnnotationTarget.CLASS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package me.gabber235.typewriter.entry
import com.google.gson.Gson
import com.google.gson.JsonObject
import lirand.api.extensions.other.set
import lirand.api.utilities.allMethods
import me.gabber235.typewriter.adapters.AdapterLoader
import org.koin.core.qualifier.named
import org.koin.java.KoinJavaComponent.get
Expand Down Expand Up @@ -122,7 +121,7 @@ object EntryMigrations {

fun constructEntryMigrators(classes: List<Class<*>>): List<EntryMigrator> {
return classes.asSequence()
.flatMap { it.allMethods.asSequence() }
.flatMap { it.methods.asSequence() }
.filter { Modifier.isStatic(it.modifiers) }
.filter { it.isAnnotationPresent(EntryMigration::class.java) }
.map(::constructEntryMigrator)
Expand Down
10 changes: 10 additions & 0 deletions plugin/src/main/kotlin/me/gabber235/typewriter/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import org.koin.java.KoinJavaComponent.get
import java.io.File
import java.time.Duration
import java.util.*
import kotlin.math.abs
import kotlin.math.log10
import kotlin.math.round

operator fun File.get(name: String): File = File(this, name)
Expand Down Expand Up @@ -56,6 +58,14 @@ fun Float.round(decimals: Int): Float {
return (round(this * multiplier) / multiplier).toFloat()
}

val Int.digits: Int
get() = if (this == 0) 1 else log10(abs(this.toDouble())).toInt() + 1


fun String.rightPad(length: Int, padChar: Char = ' '): String {
return if (this.length >= length) this else this + padChar.toString().repeat(length - this.length)
}


val <T : Any> Optional<T>?.optional: Optional<T> get() = Optional.ofNullable(this?.orElse(null))
val <T : Any> T?.optional: Optional<T> get() = Optional.ofNullable(this)

0 comments on commit 77a44c4

Please sign in to comment.