Skip to content
This repository has been archived by the owner on Oct 3, 2022. It is now read-only.

Commit

Permalink
Merge pull request #315 from TheFruxz/311-the-dsl-text-component-buil…
Browse files Browse the repository at this point in the history
…der-is-inconvenient

feature: upgrade unfold to new context based structure
  • Loading branch information
TheFruxz authored Aug 14, 2022
2 parents 836384a + 82db5b7 commit 2e7efca
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 106 deletions.
1 change: 1 addition & 0 deletions MoltenKT-Core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ tasks {
kotlinOptions.freeCompilerArgs += "-opt-in=kotlinx.serialization.ExperimentalSerializationApi"
kotlinOptions.freeCompilerArgs += "-opt-in=kotlin.io.path.ExperimentalPathApi"
kotlinOptions.freeCompilerArgs += "-opt-in=kotlinx.serialization.ExperimentalSerializationApi"
kotlinOptions.freeCompilerArgs += "-Xcontext-receivers"
}

}
Expand Down
27 changes: 27 additions & 0 deletions MoltenKT-Paper/src/main/kotlin/de/moltenKt/paper/app/MoltenApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import de.moltenKt.paper.app.interchange.DebugModeInterchange
import de.moltenKt.paper.app.interchange.MoltenKtInterchange
import de.moltenKt.paper.app.interchange.PlaygroundInterchange
import de.moltenKt.paper.extension.debugLog
import de.moltenKt.paper.extension.display.message
import de.moltenKt.paper.extension.display.notification
import de.moltenKt.paper.extension.mainLog
import de.moltenKt.paper.extension.objectBound.buildAndRegisterSandBox
import de.moltenKt.paper.mojang.MojangProfile
import de.moltenKt.paper.mojang.MojangProfileCape
import de.moltenKt.paper.mojang.MojangProfileRaw
Expand Down Expand Up @@ -70,12 +72,17 @@ import de.moltenKt.paper.tool.position.relative.LinearShape
import de.moltenKt.paper.tool.position.relative.PyramidalShape
import de.moltenKt.paper.tool.position.relative.Shape
import de.moltenKt.paper.tool.position.relative.SphereShape
import de.moltenKt.unfold.buildComponent
import de.moltenKt.unfold.extension.asComponent
import de.moltenKt.unfold.extension.asStyledComponent
import de.moltenKt.unfold.text
import de.moltenKt.unfold.unaryPlus
import kotlinx.coroutines.cancel
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.modules.polymorphic
import kotlinx.serialization.modules.subclass
import net.kyori.adventure.text.event.ClickEvent
import net.kyori.adventure.text.format.NamedTextColor
import org.bukkit.Location
import org.bukkit.NamespacedKey
Expand Down Expand Up @@ -229,6 +236,26 @@ class MoltenApp : App() {

add(AppComponent())

buildAndRegisterSandBox(this, "demo") {

buildComponent {

+ "Hello!"

+ text(" This is") {
+ " my Test"
}

+ text(" Word!") {
color(NamedTextColor.RED)
hoverEvent("Hey Hover!".asStyledComponent)
+ ClickEvent.suggestCommand("DID IT!")
}

}.message().broadcastPlayers()

}

}

override fun bye() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import de.moltenKt.paper.structure.command.structured.StructuredInterchange
import de.moltenKt.paper.structure.command.completion.buildInterchangeStructure
import de.moltenKt.paper.tool.display.message.Transmission.Level.GENERAL
import de.moltenKt.paper.tool.display.message.Transmission.Level.INFO
import de.moltenKt.unfold.newline
import de.moltenKt.unfold.text
import de.moltenKt.unfold.unaryPlus
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.Style.style
import net.kyori.adventure.text.format.TextDecoration.BOLD
Expand All @@ -19,24 +20,23 @@ internal class MoltenKtInterchange : StructuredInterchange("moltenkt", protected
concludedExecution {

text {

text("MoltenKT") {
+ text("MoltenKT") {
style(style(NamedTextColor.GOLD, BOLD))
}
text(" was developed by ") {
+ text(" was developed by ") {
color(NamedTextColor.GRAY)
}
text("TheFruxz") {
+ text("TheFruxz") {
color(NamedTextColor.YELLOW)
}
text(", and other contributors of the repository: ") {
+ text(", and other contributors of the repository: ") {
color(NamedTextColor.GRAY)
}
text(vendor.description.website ?: "FEHLER") {
+ text(vendor.description.website ?: "FEHLER") {
style(style(NamedTextColor.GOLD, BOLD))
}

newline()
+ Component.newline()

}.notification(INFO, executor).display()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package de.moltenKt.paper.extension.paper

import de.moltenKt.paper.extension.tasky.doSync
import de.moltenKt.paper.tool.annotation.RequiresSync
import org.bukkit.Material
import org.bukkit.Material.AIR
import org.bukkit.Material.TNT
import org.bukkit.block.Block
import org.bukkit.block.BlockState
import org.bukkit.block.Chest
import org.bukkit.block.Container
import org.bukkit.block.Sign
import org.bukkit.entity.EntityType
import org.bukkit.entity.FallingBlock
import org.bukkit.entity.TNTPrimed
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason
import org.bukkit.inventory.Inventory

val Block.sign: Sign
get() = this.state as Sign

/**
* This function casts a [BlockState] to the correct,
* editable block state.
Expand All @@ -24,6 +25,12 @@ import org.bukkit.inventory.Inventory
val BlockState.sign: Sign
get() = this as Sign

val Block.chest: Chest
get() = this.state as Chest

val BlockState.chest: Chest
get() = this as Chest

/**
* This function takes the sign's state and applies the
* [builder] process to it.
Expand Down

This file was deleted.

128 changes: 45 additions & 83 deletions MoltenKT-Unfold/src/main/kotlin/de/moltenKt/unfold/Unfold.kt
Original file line number Diff line number Diff line change
@@ -1,83 +1,45 @@
@file:Suppress("unused") // TODO use kotlin context API to avoid 'useless' seeming object extensions

package de.moltenKt.unfold

import de.moltenKt.unfold.extension.asStyledComponent
import io.ktor.http.*
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.ComponentLike
import net.kyori.adventure.text.TextComponent
import net.kyori.adventure.text.event.ClickEvent
import net.kyori.adventure.text.event.HoverEventSource
import java.io.File
import java.net.URL
import java.nio.file.Path

fun text(build: TextComponent.Builder.() -> Unit) =
Component.text().apply(build).build()

fun text(componentContent: String) =
componentContent.asStyledComponent

fun spaceText() = Component.space()

fun emptyText() = Component.empty()

fun newline() = Component.newline()

fun MoltenContext<HoverEventSource<*>>.text(componentContent: String) =
text { text(componentContent) }

fun MoltenContext<HoverEventSource<*>>.space() =
text { text(" ") }

fun MoltenContext<HoverEventSource<*>>.empty() =
text { text("") }

fun MoltenContext<HoverEventSource<*>>.newline() =
text { text("\n") }

operator fun TextComponent.Builder.plus(component: ComponentLike) =
append(component.asComponent())

fun TextComponent.Builder.text(componentContent: String) =
this + componentContent.asStyledComponent

fun TextComponent.Builder.text(component: TextComponent) =
this + component

fun TextComponent.Builder.text(componentContent: String, modify: TextComponent.Builder.() -> Unit) =
this.append(componentContent.asStyledComponent.toBuilder().apply(modify).build())

fun TextComponent.Builder.text(component: TextComponent, modify: TextComponent.Builder.() -> Unit) =
this.append(component.toBuilder().apply(modify).build())

fun TextComponent.Builder.hover(eventSource: MoltenContext<HoverEventSource<*>>.() -> HoverEventSource<*>) =
hoverEvent(eventSource(MoltenContext.contextOf()))

fun TextComponent.Builder.click(click: MoltenContext<ClickEvent>.() -> ClickEvent) =
clickEvent(click(MoltenContext.contextOf()))

fun MoltenContext<ClickEvent>.url(url: String) = ClickEvent.openUrl(url)

fun MoltenContext<ClickEvent>.url(url: URL) = ClickEvent.openUrl(url)

fun MoltenContext<ClickEvent>.url(url: Url) = ClickEvent.openUrl(url.toString())

fun MoltenContext<ClickEvent>.file(file: String) = ClickEvent.openFile(file)

fun MoltenContext<ClickEvent>.file(path: Path) = file("$path")

fun MoltenContext<ClickEvent>.file(file: File) = file(file.toPath())

fun MoltenContext<ClickEvent>.run(command: String) = ClickEvent.runCommand(command)

fun MoltenContext<ClickEvent>.suggest(command: String) = ClickEvent.suggestCommand(command)

fun MoltenContext<ClickEvent>.toPage(page: Int) = ClickEvent.changePage(page)

fun MoltenContext<ClickEvent>.toPage(page: String) = ClickEvent.changePage(page)

fun MoltenContext<ClickEvent>.copy(text: String) = ClickEvent.copyToClipboard(text)

fun MoltenContext<ClickEvent>.click(action: ClickEvent.Action, string: String) = ClickEvent.clickEvent(action, string)
package de.moltenKt.unfold

import de.moltenKt.core.extension.dump
import de.moltenKt.unfold.extension.asStyledComponent
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.ComponentLike
import net.kyori.adventure.text.TextComponent
import net.kyori.adventure.text.TextComponent.Builder
import net.kyori.adventure.text.event.ClickEvent
import net.kyori.adventure.text.event.HoverEventSource

@Unfold fun buildComponent(base: TextComponent = Component.empty(), builder: Builder.() -> Unit): TextComponent =
base.toBuilder().apply(builder).build()

context(Builder)
@Unfold operator fun String.unaryPlus(): Unit =
append(this.asStyledComponent).dump()

context(Builder)
@Unfold operator fun ComponentLike.unaryPlus(): Builder =
append(this)

context(Builder)
@Unfold operator fun <T : Iterable<ComponentLike>> T.unaryPlus(): Builder =
append(this)

context(Builder)
@Unfold operator fun ClickEvent.unaryPlus(): Builder =
clickEvent(this)

@Unfold
fun text(content: String, builder: Builder.() -> Unit = { }) = content.asStyledComponent(builder)

@Unfold
fun text(builder: Builder.() -> Unit) = text("", builder)

@Unfold
fun TextComponent.hover(process: () -> HoverEventSource<*>) = this.hoverEvent(process())

@Unfold
fun Builder.hover(process: () -> HoverEventSource<*>) = this.hoverEvent(process())

@DslMarker
@PublishedApi
internal annotation class Unfold
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package de.moltenKt.unfold.extension
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.ComponentLike
import net.kyori.adventure.text.TextComponent
import net.kyori.adventure.text.TextComponent.Builder
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.minimessage.MiniMessage
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
Expand Down Expand Up @@ -52,6 +53,9 @@ val ComponentLike.asString: String
val String.asComponent: TextComponent
get() = adventureSerializer.deserializeOr(this, Component.text("FAILED", NamedTextColor.RED))!!

fun String.asComponent(builder: Builder.() -> Unit) =
asComponent.toBuilder().apply(builder).build()

/**
* This computational value converts this [String] into a [TextComponent]
* list (every list entry is a line) by using the [LegacyComponentSerializer],
Expand Down Expand Up @@ -98,6 +102,9 @@ val ComponentLike.asStyledString: String
val String.asStyledComponent: TextComponent
get() = Component.text().append(miniMessageSerializer.deserializeOr(this, Component.text("FAILED", NamedTextColor.RED))!!).build()

fun String.asStyledComponent(builder: TextComponent.Builder.() -> Unit) =
asStyledComponent.toBuilder().apply(builder).build()

/**
* This computational value converts this [String] into a [TextComponent]
* list (every entry represents a line) by using the [MiniMessage], provided by the
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ allprojects {

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions.freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
kotlinOptions.freeCompilerArgs += "-Xcontext-receivers"
}

}
Expand Down

0 comments on commit 2e7efca

Please sign in to comment.