Skip to content

Commit

Permalink
Add extensive support for using dispatchers in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
rchomczyk committed Nov 23, 2024
1 parent 49e7509 commit 7fbaaea
Show file tree
Hide file tree
Showing 30 changed files with 778 additions and 54 deletions.
3 changes: 3 additions & 0 deletions .idea/gradle.xml

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

1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/honey-repositories.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ plugins {

repositories {
mavenCentral()
maven("https://storehouse.okaeri.eu/repository/maven-public/")
}
7 changes: 6 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
guava = "33.3.1-jre"
futures = "0.3.6"
adventure = "4.17.0"
okaeri-configs = "5.0.5"

[libraries]
guava = { module = "com.google.guava:guava", version.ref = "guava" }
futures = { module = "com.spotify:completable-futures", version.ref = "futures" }
adventure-api = { module = "net.kyori:adventure-api", version.ref = "adventure" }
adventure-minimessage = { module = "net.kyori:adventure-text-minimessage", version.ref = "adventure" }
okaeri-configs = { module = "eu.okaeri:okaeri-configs-yaml-snakeyaml", version.ref = "okaeri-configs" }
okaeri-configs-serdes-commons = { module = "eu.okaeri:okaeri-configs-serdes-commons", version.ref = "okaeri-configs" }
okaeri-configs-yaml-bukkit = { module = "eu.okaeri:okaeri-configs-yaml-bukkit", version.ref = "okaeri-configs" }

[bundles]
adventure = ["adventure-api", "adventure-minimessage"]
adventure = ["adventure-api", "adventure-minimessage"]
okaeri-configs = ["okaeri-configs", "okaeri-configs-serdes-commons"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package dev.shiza.honey.adventure.message.dispatcher;

import com.google.common.collect.ImmutableList;
import com.spotify.futures.CompletableFutures;
import dev.shiza.honey.message.dispatcher.BatchMessageDispatcher;
import dev.shiza.honey.message.dispatcher.MessagePolyDispatcher;
import dev.shiza.honey.message.dispatcher.MessageRenderer;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.UnaryOperator;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;

public final class AdventureBatchMessageDispatcher
implements BatchMessageDispatcher<Audience, Component> {

private final List<MessagePolyDispatcher<Audience, Component>> dispatchers;

public AdventureBatchMessageDispatcher(
final List<MessagePolyDispatcher<Audience, Component>> dispatchers) {
this.dispatchers = ImmutableList.copyOf(dispatchers);
}

public AdventureBatchMessageDispatcher() {
this(ImmutableList.of());
}

@Override
public BatchMessageDispatcher<Audience, Component> add(
final MessagePolyDispatcher<Audience, Component> dispatcher) {
return new AdventureBatchMessageDispatcher(
ImmutableList.<MessagePolyDispatcher<Audience, Component>>builder()
.addAll(dispatchers)
.add(dispatcher)
.build());
}

@SafeVarargs
@Override
public final BatchMessageDispatcher<Audience, Component> addAll(
final MessagePolyDispatcher<Audience, Component>... dispatchers) {
return new AdventureBatchMessageDispatcher(
ImmutableList.<MessagePolyDispatcher<Audience, Component>>builder()
.addAll(this.dispatchers)
.addAll(Arrays.asList(dispatchers))
.build());
}

@Override
public MessagePolyDispatcher<Audience, Component> viewer(final Audience audience) {
return new AdventureBatchMessageDispatcher(
dispatchers.stream()
.map(dispatcher -> dispatcher.viewer(audience))
.collect(ImmutableList.toImmutableList()));
}

@Override
public MessagePolyDispatcher<Audience, Component> placeholders(
final UnaryOperator<MessageRenderer<Component>> consumer) {
return new AdventureBatchMessageDispatcher(
dispatchers.stream()
.map(dispatcher -> dispatcher.placeholders(consumer))
.collect(ImmutableList.toImmutableList()));
}

@Override
public void dispatch() {
dispatchers.forEach(MessagePolyDispatcher::dispatch);
}

@Override
public CompletableFuture<Void> dispatchAsync() {
return dispatchers.stream()
.map(MessagePolyDispatcher::dispatchAsync)
.collect(CompletableFutures.joinList())
.thenAccept(__ -> {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dev.shiza.honey.message.dispatcher.MessageRenderer;
import dev.shiza.honey.message.dispatcher.TitleMessageDispatcher;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.UnaryOperator;
import net.kyori.adventure.audience.Audience;
Expand Down Expand Up @@ -116,9 +115,9 @@ public TitleMessageDispatcher<Audience, Component> subtitle(
public TitleMessageDispatcher<Audience, Component> placeholders(
final UnaryOperator<MessageRenderer<Component>> consumer) {
return new AdventureTitleMessageDispatcher(
times.placeholders(consumer),
title.placeholders(consumer),
subtitle.placeholders(consumer),
(MessageDispatcher<Audience, Component>) times.placeholders(consumer),
(MessageDispatcher<Audience, Component>) title.placeholders(consumer),
(MessageDispatcher<Audience, Component>) subtitle.placeholders(consumer),
viewer);
}

Expand Down Expand Up @@ -148,11 +147,12 @@ public void dispatch() {
* indicating when all dispatches are complete
*/
@Override
public CompletableFuture<List<Void>> dispatchAsync() {
public CompletableFuture<Void> dispatchAsync() {
return CompletableFutures.allAsList(
ImmutableList.of(
times.viewer(viewer).dispatchAsync(),
title.viewer(viewer).dispatchAsync(),
subtitle.viewer(viewer).dispatchAsync()));
ImmutableList.of(
times.viewer(viewer).dispatchAsync(),
title.viewer(viewer).dispatchAsync(),
subtitle.viewer(viewer).dispatchAsync()))
.thenAccept(__ -> {});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.shiza.honey.message.dispatcher;

public interface BatchMessageDispatcher<VIEWER, RESULT>
extends MessagePolyDispatcher<VIEWER, RESULT> {

BatchMessageDispatcher<VIEWER, RESULT> add(
final MessagePolyDispatcher<VIEWER, RESULT> dispatcher);

BatchMessageDispatcher<VIEWER, RESULT> addAll(
final MessagePolyDispatcher<VIEWER, RESULT>... dispatchers);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.shiza.honey.message.dispatcher;

import dev.shiza.honey.message.formatter.MessageFormatter;
import java.util.concurrent.CompletableFuture;
import java.util.function.UnaryOperator;

/**
Expand All @@ -11,14 +10,9 @@
* @param <VIEWER> the type of the viewer to whom the message will be sent
* @param <RESULT> the type of the result or content of the message
*/
public interface MessageDispatcher<VIEWER, RESULT> {
public interface MessageDispatcher<VIEWER, RESULT> extends MessagePolyDispatcher<VIEWER, RESULT> {

/**
* Sets the viewer of the message.
*
* @param viewer the viewer of the message
* @return the current instance of {@code MessageDispatcher} for method chaining
*/
@Override
MessageDispatcher<VIEWER, RESULT> viewer(final VIEWER viewer);

/**
Expand All @@ -39,15 +33,7 @@ MessageDispatcher<VIEWER, RESULT> template(
*/
MessageDispatcher<VIEWER, RESULT> template(final RESULT message);

MessageDispatcher<VIEWER, RESULT> placeholders(final UnaryOperator<MessageRenderer<RESULT>> consumer);

/** Dispatches the message synchronously. */
void dispatch();

/**
* Dispatches the message asynchronously.
*
* @return a {@link CompletableFuture} that is completed when the dispatch is done
*/
CompletableFuture<Void> dispatchAsync();
@Override
MessageDispatcher<VIEWER, RESULT> placeholders(
final UnaryOperator<MessageRenderer<RESULT>> consumer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.shiza.honey.message.dispatcher;

import java.util.concurrent.CompletableFuture;
import java.util.function.UnaryOperator;

public interface MessagePolyDispatcher<VIEWER, RESULT> {

MessagePolyDispatcher<VIEWER, RESULT> viewer(final VIEWER viewer);

MessagePolyDispatcher<VIEWER, RESULT> placeholders(
final UnaryOperator<MessageRenderer<RESULT>> consumer);

void dispatch();

CompletableFuture<Void> dispatchAsync();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dev.shiza.honey.message.dispatcher;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.UnaryOperator;

/**
Expand All @@ -11,7 +9,11 @@
* @param <VIEWER> The type of the viewer for whom the messages are intended.
* @param <RESULT> The result type for operations that are performed by the MessageDispatcher.
*/
public interface TitleMessageDispatcher<VIEWER, RESULT> {
public interface TitleMessageDispatcher<VIEWER, RESULT>
extends MessagePolyDispatcher<VIEWER, RESULT> {

@Override
TitleMessageDispatcher<VIEWER, RESULT> viewer(final VIEWER viewer);

/**
* Configures the display times of the title message.
Expand Down Expand Up @@ -41,25 +43,7 @@ TitleMessageDispatcher<VIEWER, RESULT> title(
TitleMessageDispatcher<VIEWER, RESULT> subtitle(
final UnaryOperator<MessageDispatcher<VIEWER, RESULT>> consumer);

@Override
TitleMessageDispatcher<VIEWER, RESULT> placeholders(
final UnaryOperator<MessageRenderer<RESULT>> consumer);

/**
* Sets the viewer of the title message.
*
* @param viewer The viewer who will receive the message.
* @return An instance of TitleMessageDispatcher for method chaining.
*/
TitleMessageDispatcher<VIEWER, RESULT> viewer(final VIEWER viewer);

/** Dispatches the title and subtitle to the viewer synchronously. */
void dispatch();

/**
* Dispatches the title and subtitle to the viewer asynchronously.
*
* @return A CompletableFuture that completes with a list of results (normally empty) once the
* message dispatch operation is complete.
*/
CompletableFuture<List<Void>> dispatchAsync();
}
14 changes: 14 additions & 0 deletions honey-configs/honey-configs-common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
`honey-java`
`honey-publish`
`honey-repositories`
}

dependencies {
compileOnly(project(":honey-common"))
compileOnly(libs.bundles.adventure)
}

honeyPublish {
artifactId = "honey-configs-common"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package dev.shiza.honey.adventure.message.dispatcher;

import com.google.common.collect.ImmutableList;
import dev.shiza.honey.message.dispatcher.BatchMessageConfigurer;
import dev.shiza.honey.message.dispatcher.BatchMessageDispatcher;
import dev.shiza.honey.message.dispatcher.MessagePolyConfigurer;
import dev.shiza.honey.message.dispatcher.MessagePolyDispatcher;
import dev.shiza.honey.message.formatter.MessageFormatter;
import java.util.List;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;

public final class AdventureBatchMessageConfigurer
implements BatchMessageConfigurer<Audience, Component> {

private final List<MessagePolyConfigurer<Audience, Component>> configurers;

AdventureBatchMessageConfigurer(final List<MessagePolyConfigurer<Audience, Component>> configurers) {
this.configurers = ImmutableList.copyOf(configurers);
}

AdventureBatchMessageConfigurer() {
this(ImmutableList.of());
}

@Override
public AdventureBatchMessageConfigurer add(final MessagePolyConfigurer<Audience, Component> configurer) {
return new AdventureBatchMessageConfigurer(
ImmutableList.<MessagePolyConfigurer<Audience, Component>>builder()
.addAll(configurers)
.add(configurer)
.build());
}

@Override
public BatchMessageDispatcher<Audience, Component> dispatcher(
final MessageFormatter<Component> formatter) {
return new AdventureBatchMessageDispatcher()
.addAll(
configurers.stream()
.map(configurer -> configurer.dispatcher(formatter))
.toArray(MessagePolyDispatcher[]::new));
}

List<MessagePolyConfigurer<Audience, Component>> configurers() {
return configurers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package dev.shiza.honey.adventure.message.dispatcher;

import dev.shiza.honey.message.dispatcher.MessageBaseDispatcher;
import dev.shiza.honey.message.dispatcher.MessageConfigurationException;
import dev.shiza.honey.message.dispatcher.MessageConfigurer;
import dev.shiza.honey.message.dispatcher.MessageDispatcher;
import dev.shiza.honey.message.formatter.MessageFormatter;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;

public final class AdventureMessageConfigurer
implements MessageConfigurer<Audience, String, Component> {

private final AdventureMessageDelivery delivery;
private final String template;

AdventureMessageConfigurer(final AdventureMessageDelivery delivery, final String template) {
this.delivery = delivery;
this.template = template;
}

AdventureMessageConfigurer(final AdventureMessageDelivery delivery) {
this(delivery, null);
}

@Override
public MessageConfigurer<Audience, String, Component> delivery(
final AdventureMessageDelivery delivery) {
return new AdventureMessageConfigurer(delivery, template);
}

@Override
public AdventureMessageConfigurer template(final String template) {
return new AdventureMessageConfigurer(delivery, template);
}

@Override
public MessageDispatcher<Audience, Component> dispatcher(
final MessageFormatter<Component> formatter) {
if (template == null) {
throw new MessageConfigurationException(
"Could not get a message dispatcher without a template.");
}

return new MessageBaseDispatcher<>(Audience.empty(), delivery.deliver())
.template(formatter, template);
}

AdventureMessageDelivery delivery() {
return delivery;
}

String template() {
return template;
}
}
Loading

0 comments on commit 7fbaaea

Please sign in to comment.