Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kotlin extension #25

Merged
merged 8 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 75 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@ A message library, which focuses on simplicity and flexibility.

### Get started

#### Gradle (kts)
#### Gradle DSL (kts) || std

##### Add repository

```kotlin
maven("https://repo.shiza.dev/releases")
```

```groovy
maven { url 'https://repo.shiza.dev/releases' }
```

##### Add dependency

```kotlin
implementation("dev.shiza:honey:2.0.0")
```

```groovy
implementation 'dev.shiza:honey:2.0.0'
```

#### Maven

##### Add repository
Expand All @@ -41,32 +49,86 @@ implementation("dev.shiza:honey:2.0.0")
</dependency>
```

### Use case
![test-plugin showcase](assets/image.png)

### Use case Java

A showcase of how to use *honey*, can be found in [honey-test-plugin](honey-test-plugin) module.

## With Formatter placeholder:
```java
// * sending a message as title with all honey features
AdventureMessageDispatcher.createTitle()

/* for titles::audience */
dispatcher.createTitle()
.recipient(event.getPlayer())
.title(it -> it.template(formatter, "Hello!"))
.subtitle(it -> it.template(formatter, "It is a pleasure to see you there {{player.getName}}")
.title(it -> it.template("Hello!"))
.subtitle(it -> it.template("It is a pleasure to see you there {{player.getName}}")
.variable("player", event.getPlayer()))
.times(2, 4, 2)
.dispatch();

// * sending a message as chat message with all honey features
AdventureMessageDispatcher.createChat()
/* for chat::audience */
dispatcher.createChat()
.recipient(Bukkit.getServer())
.template(formatter, "{{player.getName}} has joined the server!")
.template("{{player.getName}} has joined the server!")
.variable("player", event.getPlayer())
.dispatch();

// * sending a message as action bar with predefined message without any placeholder support
AdventureMessageDispatcher.createActionBar()
/* for actionbar::audience */
dispatcher.createActionBar()
.recipient(event.getPlayer())
.template(Component.text("Honey is great, isn't it?"))
.template("Honey is great, isn't it?")
.dispatch();
```

![test-plugin showcase](assets/image.png)
## Without formatter, plain template:
```java
AdventureMessageDispatcher.createChat()
.recipient(Bukkit.getServer())
.template(Component.text("Somebody joined to the server!").color(NamedTextColor.RED))
.dispatch();

AdventureMessageDispatcher.createActionBar()
.recipient(event.getPlayer())
.template(formatter, "Honey is great, isn't it?")
.dispatch();
}
```

### Use case Kotlin
```kotlin
AdventureMessageDispatcher.createChat()
.recipient(event.player)
.template(Component.text("Hello!"))
.dispatch()

AdventureMessageDispatcher.createActionBar()
.recipient(event.player)
.template(Component.text("This is an action bar message!"))
.dispatch()

/* player::audience */
player.createChat()
.template(Component.text("A custom chat message"))
.dispatch()

player.createActionBar()
.template(Component.text("This is a custom action bar"))
.dispatch()

AdventureMessageDispatcher.createTitle()
.recipient(event.player)
.title { it.template(Component.text("Hello!")) }
.subtitle { it.template(Component.text("It's great to see you!")) }
.times(2, 4, 2)
.dispatch()
```

### Dispatcher Synchronously vs Asynchronously:
- [dispatch](https://github.com/rchomczyk/honey-common/src/dev/shiza/honey/message/dispatcher/MessageBaseDispatcher.java#L71)
This method immediately delivers the message synchronously. It calls the deliver function with the rendered message and the recipient, and the action is completed immediately.

- [dispatchAsync](https://github.com/rchomczyk/honey-common/src/dev/shiza/honey/message/dispatcher/MessageBaseDispatcher.java#L76)
This method delivers the message asynchronously. It returns a CompletableFuture that performs the message rendering in the background and then delivers the result once it's ready. It allows non-blocking behavior and handles exceptions asynchronously.
```

14 changes: 14 additions & 0 deletions buildSrc/src/main/kotlin/honey-kotlin.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
`java-library`
}

sourceSets {
main {
java.setSrcDirs(listOf("src"))
resources.setSrcDirs(emptyList<String>())
}
test {
java.setSrcDirs(emptyList<String>())
resources.setSrcDirs(emptyList<String>())
}
}
23 changes: 23 additions & 0 deletions honey-kt-extension/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
plugins {
`honey-kotlin`
`honey-publish`
`honey-repositories`
kotlin("jvm") version "2.0.21"
}

repositories {
maven("https://repo.papermc.io/repository/maven-public/")
}

dependencies {
compileOnly(project(":honey-common"))
compileOnly("io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT")
}

honeyPublish {
artifactId = "honey-kt-extension"
}

kotlin {
jvmToolchain(17)
}
28 changes: 28 additions & 0 deletions honey-kt-extension/src/dev/shiza/honey/audience.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import dev.shiza.honey.TypedMessageDispatcher
import dev.shiza.honey.adventure.message.dispatcher.AdventureMessageDispatcher
import dev.shiza.honey.message.dispatcher.MessageBaseDispatcher
import net.kyori.adventure.audience.Audience

fun AdventureMessageDispatcher.createChat(): TypedMessageDispatcher =
MessageBaseDispatcher(
Audience.empty(),
Audience::sendMessage
)

fun AdventureMessageDispatcher.createActionBar(): TypedMessageDispatcher =
MessageBaseDispatcher(
Audience.empty(),
Audience::sendActionBar
)

fun Audience.createChat(): TypedMessageDispatcher =
MessageBaseDispatcher(
this,
Audience::sendMessage
)

fun Audience.createActionBar(): TypedMessageDispatcher =
MessageBaseDispatcher(
this,
Audience::sendActionBar
)
16 changes: 16 additions & 0 deletions honey-kt-extension/src/dev/shiza/honey/player.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import dev.shiza.honey.TypedMessageDispatcher
import dev.shiza.honey.message.dispatcher.MessageBaseDispatcher
import net.kyori.adventure.audience.Audience
import org.bukkit.entity.Player

fun Player.createChat(): TypedMessageDispatcher =
MessageBaseDispatcher(
this,
Audience::sendMessage
)

fun Player.createActionBar(): TypedMessageDispatcher =
MessageBaseDispatcher(
this,
Audience::sendActionBar
)
50 changes: 50 additions & 0 deletions honey-kt-extension/src/dev/shiza/honey/title.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import dev.shiza.honey.MessageConfigurer
import dev.shiza.honey.adventure.message.dispatcher.AdventureMessageDispatcher
import dev.shiza.honey.message.dispatcher.TitleMessageDispatcher
import net.kyori.adventure.audience.Audience
import net.kyori.adventure.text.Component
import org.bukkit.entity.Player

fun times(fadeIn: Int, stay: Int, fadeOut: Int): Triple<Int, Int, Int> =
Triple(fadeIn, stay, fadeOut)

fun AdventureMessageDispatcher.createTitle(
recipient: Audience,
titleConfig: MessageConfigurer,
subtitleConfig: MessageConfigurer,
fadeIn: Int,
stay: Int,
fadeOut: Int
): TitleMessageDispatcher<Audience, Component> =
AdventureMessageDispatcher.createTitle()
.recipient(recipient)
.title { base -> base.apply(titleConfig) }
.subtitle { base -> base.apply(subtitleConfig) }
.times(fadeIn, stay, fadeOut)

fun Audience.createTitle(
titleConfig: MessageConfigurer,
subtitleConfig: MessageConfigurer,
fadeIn: Int,
stay: Int,
fadeOut: Int
): TitleMessageDispatcher<Audience, Component> =
AdventureMessageDispatcher.createTitle()
.recipient(this)
.title { base -> base.apply(titleConfig) }
.subtitle { base -> base.apply(subtitleConfig) }
.times(fadeIn, stay, fadeOut)

fun Player.createTitle(
titleConfig: MessageConfigurer,
subtitleConfig: MessageConfigurer,
fadeIn: Int,
stay: Int,
fadeOut: Int
): TitleMessageDispatcher<Audience, Component> =
AdventureMessageDispatcher.createTitle()
.recipient(this)
.title { base -> base.apply(titleConfig) }
.subtitle { base -> base.apply(subtitleConfig) }
.times(fadeIn, stay, fadeOut)

9 changes: 9 additions & 0 deletions honey-kt-extension/src/dev/shiza/honey/types.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.shiza.honey

import dev.shiza.honey.message.dispatcher.MessageDispatcher
import net.kyori.adventure.audience.Audience
import net.kyori.adventure.text.Component

typealias TypedMessageDispatcher = MessageDispatcher<Audience, Component>

typealias MessageConfigurer = TypedMessageDispatcher.() -> Unit
4 changes: 3 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
rootProject.name = "honey"
include(":honey-common")
include(":honey-kt-extension")

// Uncomment in case if you would like to run test plugin
include(":honey-test-plugin")
// include(":honey-test-plugin")
Loading