Skip to content
This repository was archived by the owner on Aug 7, 2025. It is now read-only.

Commit 279e85c

Browse files
committed
Properly encapsulate adventure implementation as separate module
1 parent e86c911 commit 279e85c

File tree

109 files changed

+2517
-2537
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+2517
-2537
lines changed

.idea/gradle.xml

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/palantir-java-format.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,8 @@ A message library, which focuses on simplicity and flexibility.
77
*honey* was created to allow for developers to create easily customizable messages for end users, by giving them flexibility with placeholders.
88

99
### Get started
10-
11-
#### Gradle DSL (kts) || std
12-
13-
##### Add repository
14-
15-
```kotlin
16-
maven("https://repo.shiza.dev/releases")
17-
```
18-
19-
```groovy
20-
maven { url 'https://repo.shiza.dev/releases' }
21-
```
22-
23-
##### Add dependency
24-
25-
```kotlin
26-
implementation("dev.shiza:honey:2.2.2")
27-
```
28-
29-
```groovy
30-
implementation 'dev.shiza:honey:2.2.2'
31-
```
32-
33-
#### Maven
34-
35-
##### Add repository
36-
```xml
37-
<repository>
38-
<id>shiza-releases</id>
39-
<url>https://repo.shiza.dev/releases</url>
40-
</repository>
41-
```
42-
43-
##### Add dependency
44-
```xml
45-
<dependency>
46-
<groupId>dev.shiza</groupId>
47-
<artifactId>honey</artifactId>
48-
<version>2.2.2</version>
49-
</dependency>
50-
```
10+
You can build dependency and append it to your local .m2 directory, by using:
11+
`./gradlew publishToMavenLocal`
5112

5213
![test-plugin showcase](assets/image.png)
5314

honey-adventure/build.gradle.kts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
plugins {
2+
`honey-java`
3+
`honey-unit-test`
4+
`honey-publish`
5+
`honey-repositories`
6+
}
7+
8+
dependencies {
9+
api(project(":honey-common"))
10+
api(libs.bundles.adventure)
11+
}
12+
13+
honeyPublish {
14+
artifactId = "honey-adventure"
15+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package dev.shiza.honey.adventure.message.compiler;
2+
3+
import dev.shiza.honey.adventure.placeholder.ParsableValue;
4+
import dev.shiza.honey.message.compiler.MessageCompiler;
5+
import dev.shiza.honey.placeholder.sanitizer.PlaceholderSanitizer.SanitizedPlaceholder;
6+
import java.util.List;
7+
import net.kyori.adventure.text.Component;
8+
import net.kyori.adventure.text.minimessage.MiniMessage;
9+
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
10+
import org.intellij.lang.annotations.Subst;
11+
12+
import static net.kyori.adventure.text.minimessage.tag.resolver.Placeholder.component;
13+
import static net.kyori.adventure.text.minimessage.tag.resolver.Placeholder.parsed;
14+
import static net.kyori.adventure.text.minimessage.tag.resolver.Placeholder.unparsed;
15+
16+
/**
17+
* This class provides a mechanism to compile messages using the Adventure API MiniMessage format.
18+
* It translates sanitized content and placeholders into formatted Adventure {@link Component}s.
19+
*/
20+
final class AdventureMessageCompiler implements MessageCompiler<Component> {
21+
22+
private final MiniMessage miniMessage;
23+
24+
AdventureMessageCompiler(final MiniMessage miniMessage) {
25+
this.miniMessage = miniMessage;
26+
}
27+
28+
/**
29+
* Compiles the sanitized content and placeholders into an Adventure {@link Component}.
30+
*
31+
* @param sanitizedContent The sanitized content string to be compiled.
32+
* @param placeholders The list of sanitized placeholders to be included in the content.
33+
* @return The compiled message as an Adventure {@link Component}.
34+
*/
35+
@Override
36+
public Component compile(
37+
final String sanitizedContent, final List<SanitizedPlaceholder> placeholders) {
38+
final TagResolver[] placeholderTags = getPlaceholderTags(placeholders);
39+
return miniMessage.deserialize(sanitizedContent, placeholderTags);
40+
}
41+
42+
/**
43+
* Converts a list of {@link SanitizedPlaceholder} into an array of {@link TagResolver}.
44+
*
45+
* @param placeholders The list of placeholders to be converted.
46+
* @return An array of {@link TagResolver}.
47+
*/
48+
private TagResolver[] getPlaceholderTags(final List<SanitizedPlaceholder> placeholders) {
49+
return placeholders.stream()
50+
.map(placeholder -> getPlaceholderTags(placeholder.key(), placeholder.evaluatedValue()))
51+
.toArray(TagResolver[]::new);
52+
}
53+
54+
/**
55+
* Creates a {@link TagResolver} based on the key and the evaluated value of the placeholder. The
56+
* method handles different types of evaluated values by converting them into suitable tag
57+
* resolutions.
58+
*
59+
* @param key The key associated with the placeholder.
60+
* @param evaluatedValue The evaluated value of the placeholder.
61+
* @return The appropriate {@link TagResolver} based on the type of evaluated value.
62+
*/
63+
private TagResolver getPlaceholderTags(
64+
final @Subst("default") String key, final Object evaluatedValue) {
65+
if (evaluatedValue instanceof Component component) {
66+
return component(key, component);
67+
}
68+
69+
if (evaluatedValue instanceof ParsableValue parsableValue) {
70+
return parsed(key, parsableValue.value());
71+
}
72+
73+
return unparsed(key, evaluatedValue.toString());
74+
}
75+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package dev.shiza.honey.adventure.message.compiler;
2+
3+
import dev.shiza.honey.message.compiler.MessageCompiler;
4+
import net.kyori.adventure.text.Component;
5+
import net.kyori.adventure.text.minimessage.MiniMessage;
6+
7+
import static net.kyori.adventure.text.minimessage.MiniMessage.miniMessage;
8+
9+
/**
10+
* Factory for creating Adventure message compilers.
11+
*/
12+
public final class AdventureMessageCompilerFactory {
13+
14+
private AdventureMessageCompilerFactory() {}
15+
16+
/**
17+
* Creates a message compiler using a specific MiniMessage.
18+
*
19+
* @param miniMessage the MiniMessage instance to use for the compiler.
20+
* @return a new instance of {@link MessageCompiler<Component>} configured with the given
21+
* MiniMessage.
22+
*/
23+
public static MessageCompiler<Component> create(final MiniMessage miniMessage) {
24+
return new AdventureMessageCompiler(miniMessage);
25+
}
26+
27+
/**
28+
* Creates a default message compiler with default MiniMessage configuration.
29+
*
30+
* @return a new instance of {@link MessageCompiler<Component>} with the default MiniMessage
31+
* configuration.
32+
*/
33+
public static MessageCompiler<Component> create() {
34+
return new AdventureMessageCompiler(miniMessage());
35+
}
36+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package dev.shiza.honey.adventure.message.dispatcher;
2+
3+
import com.google.common.collect.ImmutableList;
4+
import com.spotify.futures.CompletableFutures;
5+
import dev.shiza.honey.message.dispatcher.BatchMessageDispatcher;
6+
import dev.shiza.honey.message.dispatcher.MessagePolyDispatcher;
7+
import dev.shiza.honey.message.dispatcher.MessageRenderer;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.concurrent.CompletableFuture;
11+
import java.util.function.UnaryOperator;
12+
import net.kyori.adventure.audience.Audience;
13+
import net.kyori.adventure.text.Component;
14+
15+
public final class AdventureBatchMessageDispatcher
16+
implements BatchMessageDispatcher<Audience, Component> {
17+
18+
private final List<MessagePolyDispatcher<Audience, Component>> dispatchers;
19+
20+
public AdventureBatchMessageDispatcher(
21+
final List<MessagePolyDispatcher<Audience, Component>> dispatchers) {
22+
this.dispatchers = ImmutableList.copyOf(dispatchers);
23+
}
24+
25+
public AdventureBatchMessageDispatcher() {
26+
this(ImmutableList.of());
27+
}
28+
29+
@Override
30+
public BatchMessageDispatcher<Audience, Component> add(
31+
final MessagePolyDispatcher<Audience, Component> dispatcher) {
32+
return new AdventureBatchMessageDispatcher(
33+
ImmutableList.<MessagePolyDispatcher<Audience, Component>>builder()
34+
.addAll(dispatchers)
35+
.add(dispatcher)
36+
.build());
37+
}
38+
39+
@SafeVarargs
40+
@Override
41+
public final BatchMessageDispatcher<Audience, Component> addAll(
42+
final MessagePolyDispatcher<Audience, Component>... dispatchers) {
43+
return new AdventureBatchMessageDispatcher(
44+
ImmutableList.<MessagePolyDispatcher<Audience, Component>>builder()
45+
.addAll(this.dispatchers)
46+
.addAll(Arrays.asList(dispatchers))
47+
.build());
48+
}
49+
50+
@Override
51+
public MessagePolyDispatcher<Audience, Component> viewer(final Audience audience) {
52+
return new AdventureBatchMessageDispatcher(
53+
dispatchers.stream()
54+
.map(dispatcher -> dispatcher.viewer(audience))
55+
.collect(ImmutableList.toImmutableList()));
56+
}
57+
58+
@Override
59+
public MessagePolyDispatcher<Audience, Component> placeholders(
60+
final UnaryOperator<MessageRenderer<Component>> consumer) {
61+
return new AdventureBatchMessageDispatcher(
62+
dispatchers.stream()
63+
.map(dispatcher -> dispatcher.placeholders(consumer))
64+
.collect(ImmutableList.toImmutableList()));
65+
}
66+
67+
@Override
68+
public void dispatch() {
69+
dispatchers.forEach(MessagePolyDispatcher::dispatch);
70+
}
71+
72+
@Override
73+
public CompletableFuture<Void> dispatchAsync() {
74+
return dispatchers.stream()
75+
.map(MessagePolyDispatcher::dispatchAsync)
76+
.collect(CompletableFutures.joinList())
77+
.thenAccept(__ -> {});
78+
}
79+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package dev.shiza.honey.adventure.message.dispatcher;
2+
3+
import dev.shiza.honey.message.dispatcher.MessageBaseDispatcher;
4+
import dev.shiza.honey.message.dispatcher.MessageDispatcher;
5+
import dev.shiza.honey.message.dispatcher.TitleMessageDispatcher;
6+
import net.kyori.adventure.audience.Audience;
7+
import net.kyori.adventure.text.Component;
8+
9+
/**
10+
* AdventureMessageDispatcher provides static factory methods to create various types of message
11+
* dispatchers for use in an adventure game context. This class cannot be instantiated and provides
12+
* centralized access to functions creating dispatchers for chat messages, action bars, and titles.
13+
*/
14+
public final class AdventureMessageDispatcher {
15+
16+
private AdventureMessageDispatcher() {}
17+
18+
/**
19+
* Creates a MessageDispatcher for sending chat messages.
20+
*
21+
* @return A MessageDispatcher instance configured to send chat messages to an Audience.
22+
*/
23+
public static MessageDispatcher<Audience, Component> createChat() {
24+
return new MessageBaseDispatcher<>(Audience.empty(), Audience::sendMessage);
25+
}
26+
27+
/**
28+
* Creates a MessageDispatcher for sending action bar messages.
29+
*
30+
* @return A MessageDispatcher instance configured to send action bar messages to an Audience.
31+
*/
32+
public static MessageDispatcher<Audience, Component> createActionBar() {
33+
return new MessageBaseDispatcher<>(Audience.empty(), Audience::sendActionBar);
34+
}
35+
36+
/**
37+
* Creates a TitleMessageDispatcher for sending title messages.
38+
*
39+
* @return A TitleMessageDispatcher instance configured to send title messages to an Audience.
40+
*/
41+
public static TitleMessageDispatcher<Audience, Component> createTitle() {
42+
return new AdventureTitleMessageDispatcher();
43+
}
44+
}

0 commit comments

Comments
 (0)