Skip to content

Commit

Permalink
Use more flexible implementation for message dispatchers
Browse files Browse the repository at this point in the history
  • Loading branch information
rchomczyk committed Nov 16, 2024
1 parent 77a0691 commit 0615946
Show file tree
Hide file tree
Showing 21 changed files with 254 additions and 89 deletions.
2 changes: 1 addition & 1 deletion honey-common/src/dev/shiza/honey/Honey.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import dev.shiza.honey.conversion.ImplicitConversion;
import dev.shiza.honey.message.Message;
import dev.shiza.honey.message.MessageCompiler;
import dev.shiza.honey.message.compiler.MessageCompiler;
import dev.shiza.honey.placeholder.evaluator.PlaceholderContext;
import dev.shiza.honey.placeholder.evaluator.PlaceholderEvaluator;
import dev.shiza.honey.placeholder.resolver.PlaceholderResolver;
Expand Down
2 changes: 1 addition & 1 deletion honey-common/src/dev/shiza/honey/HoneyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import dev.shiza.honey.conversion.ImplicitConversion;
import dev.shiza.honey.message.Message;
import dev.shiza.honey.message.MessageCompiler;
import dev.shiza.honey.message.compiler.MessageCompiler;
import dev.shiza.honey.placeholder.evaluator.EvaluatedPlaceholder;
import dev.shiza.honey.placeholder.evaluator.PlaceholderContext;
import dev.shiza.honey.placeholder.evaluator.PlaceholderEvaluator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package dev.shiza.honey.adventure;

import dev.shiza.honey.adventure.message.compiler.AdventureMessageCompilerFactory;
import dev.shiza.honey.builder.HoneyBaseBuilder;
import dev.shiza.honey.builder.HoneyBuilder;
import dev.shiza.honey.message.MessageCompiler;
import dev.shiza.honey.message.compiler.MessageCompiler;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;

final class AdventureHoneyBuilder extends HoneyBaseBuilder<Component, AdventureHoney> {
public final class AdventureHoneyBuilder extends HoneyBaseBuilder<Component, AdventureHoney> {

private AdventureHoneyBuilder(final MessageCompiler<Component> messageCompiler) {
super(messageCompiler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import dev.shiza.honey.HoneyImpl;
import dev.shiza.honey.conversion.ImplicitConversion;
import dev.shiza.honey.message.MessageCompiler;
import dev.shiza.honey.message.compiler.MessageCompiler;
import dev.shiza.honey.placeholder.evaluator.PlaceholderContext;
import dev.shiza.honey.placeholder.evaluator.PlaceholderEvaluator;
import dev.shiza.honey.placeholder.resolver.PlaceholderResolver;
Expand Down
8 changes: 0 additions & 8 deletions honey-common/src/dev/shiza/honey/adventure/ParsableValue.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package dev.shiza.honey.adventure;
package dev.shiza.honey.adventure.message.compiler;

import static net.kyori.adventure.text.minimessage.tag.resolver.Placeholder.component;
import static net.kyori.adventure.text.minimessage.tag.resolver.Placeholder.parsed;
import static net.kyori.adventure.text.minimessage.tag.resolver.Placeholder.unparsed;

import dev.shiza.honey.message.MessageCompiler;
import dev.shiza.honey.adventure.placeholder.ParsableValue;
import dev.shiza.honey.message.compiler.MessageCompiler;
import dev.shiza.honey.placeholder.sanitizer.SanitizedPlaceholder;
import java.util.List;
import net.kyori.adventure.text.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.shiza.honey.adventure;
package dev.shiza.honey.adventure.message.compiler;

import static net.kyori.adventure.text.minimessage.MiniMessage.miniMessage;

import dev.shiza.honey.message.MessageCompiler;
import dev.shiza.honey.message.compiler.MessageCompiler;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.shiza.honey.adventure.message.dispatcher;

import dev.shiza.honey.adventure.AdventureHoney;
import dev.shiza.honey.message.Message;
import dev.shiza.honey.message.dispatcher.MessageBaseDispatcher;
import java.util.function.BiConsumer;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;

public final class AdventureMessageDispatcher extends MessageBaseDispatcher<Audience, Component> {

AdventureMessageDispatcher(
final AdventureHoney honey,
final Message message,
final BiConsumer<Audience, Component> deliver) {
super(honey, message, deliver);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dev.shiza.honey.adventure.message.dispatcher;

import dev.shiza.honey.message.dispatcher.MessageDispatcherBatch;
import java.util.ArrayList;
import java.util.List;
import net.kyori.adventure.audience.Audience;

public final class AdventureMessageDispatcherBatch
extends MessageDispatcherBatch<Audience, AdventureMessageDispatcher> {

private AdventureMessageDispatcherBatch(final List<AdventureMessageDispatcher> dispatchers) {
super(dispatchers);
}

public static AdventureMessageDispatcherBatch newBatch() {
return new AdventureMessageDispatcherBatch(new ArrayList<>());
}

@Override
public AdventureMessageDispatcherBatch append(final AdventureMessageDispatcher dispatcher) {
super.append(dispatcher);
return this;
}

@Override
public AdventureMessageDispatcherBatch recipient(final Audience recipient) {
super.recipient(recipient);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package dev.shiza.honey.adventure.message.dispatcher;

import dev.shiza.honey.adventure.AdventureHoney;
import dev.shiza.honey.message.Message;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.title.Title;
import net.kyori.adventure.title.Title.Times;
import net.kyori.adventure.title.TitlePart;

public final class AdventureMessageDispatchers {

private final AdventureHoney honey;

public AdventureMessageDispatchers(final AdventureHoney honey) {
this.honey = honey;
}

public AdventureMessageDispatcher chat(final Message message) {
return new AdventureMessageDispatcher(honey, message, Audience::sendMessage);
}

public AdventureMessageDispatcher actionBar(final Message message) {
return new AdventureMessageDispatcher(honey, message, Audience::sendActionBar);
}

public AdventureMessageDispatcherBatch title(
final Message titleMessage, final Message subtitleMessage, final Times times) {
return AdventureMessageDispatcherBatch.newBatch()
.append(times(times))
.append(title(titleMessage))
.append(subtitle(subtitleMessage));
}

public AdventureMessageDispatcherBatch title(
final Message titleMessage, final Message subtitleMessage) {
return title(titleMessage, subtitleMessage, Title.DEFAULT_TIMES);
}

public AdventureMessageDispatcher title(final Message message) {
return new AdventureMessageDispatcher(
honey,
message,
(audience, component) -> audience.sendTitlePart(TitlePart.TITLE, component));
}

public AdventureMessageDispatcher subtitle(final Message message) {
return new AdventureMessageDispatcher(
honey,
message,
(audience, component) -> audience.sendTitlePart(TitlePart.SUBTITLE, component));
}

private AdventureMessageDispatcher times(final Times times) {
return new AdventureMessageDispatcher(
honey,
Message.blank(),
(audience, component) -> audience.sendTitlePart(TitlePart.TIMES, times));
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.shiza.honey.adventure.placeholder;

/**
* Represents a value that should be parsed by {@link
* net.kyori.adventure.text.minimessage.MiniMessage}.
*
* @param value the value to be parsed
*/
public record ParsableValue(String value) {

public static ParsableValue of(final String value) {
return new ParsableValue(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import dev.shiza.honey.Honey;
import dev.shiza.honey.conversion.ImplicitConversion;
import dev.shiza.honey.message.MessageCompiler;
import dev.shiza.honey.message.compiler.MessageCompiler;
import dev.shiza.honey.placeholder.evaluator.PlaceholderContext;
import dev.shiza.honey.placeholder.evaluator.PlaceholderEvaluator;
import dev.shiza.honey.placeholder.resolver.PlaceholderResolver;
Expand Down
6 changes: 6 additions & 0 deletions honey-common/src/dev/shiza/honey/message/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@

public record Message(String content, PlaceholderContext context) {

private static Message BLANK = new Message("", PlaceholderContext.create());

public static Message of(final String content) {
return new Message(content, PlaceholderContext.create());
}

public static Message blank() {
return BLANK;
}

public Message variable(final String name, final Object value) {
context.withValue(name, value);
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.shiza.honey.message;
package dev.shiza.honey.message.compiler;

import dev.shiza.honey.placeholder.sanitizer.SanitizedPlaceholder;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package dev.shiza.honey.message.dispatcher;

import dev.shiza.honey.Honey;
import dev.shiza.honey.message.Message;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import org.jetbrains.annotations.ApiStatus.Internal;

@Internal
public abstract class MessageBaseDispatcher<T, R> implements MessageDispatcher<T> {

private final Honey<R> honey;
private final Message message;
private final BiConsumer<T, R> deliver;
private T recipient;

protected MessageBaseDispatcher(
final Honey<R> honey, final Message message, final BiConsumer<T, R> deliver) {
this.honey = honey;
this.message = message;
this.deliver = deliver;
}

@Override
public MessageDispatcher<T> recipient(final T recipient) {
this.recipient = recipient;
return this;
}

@Override
public void dispatch() {
if (!message.context().getPromisedValues().isEmpty()) {
throw new MessageDispatchingException(
"Cannot dispatch a message with promised values synchronously");
}

deliver.accept(recipient, honey.compile(message));
}

@Override
public CompletableFuture<Void> dispatchAsync() {
return honey
.compileAsync(message)
.thenAccept(result -> deliver.accept(recipient, result))
.exceptionally(
cause -> {
throw new MessageDispatchingException(
"Could not dispatch message, because of unexpected exception.", cause);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.shiza.honey.message.dispatcher;

import java.util.concurrent.CompletableFuture;

public interface MessageDispatcher<T> {

MessageDispatcher<T> recipient(final T recipient);

void dispatch();

CompletableFuture<Void> dispatchAsync();
}
Loading

0 comments on commit 0615946

Please sign in to comment.