-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use more flexible implementation for message dispatchers
- Loading branch information
Showing
21 changed files
with
254 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
honey-common/src/dev/shiza/honey/adventure/AdventureHoneyBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletions
8
honey-common/src/dev/shiza/honey/adventure/ParsableValue.java
This file was deleted.
Oops, something went wrong.
5 changes: 3 additions & 2 deletions
5
...y/adventure/AdventureMessageCompiler.java → ...ge/compiler/AdventureMessageCompiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ture/AdventureMessageCompilerFactory.java → ...iler/AdventureMessageCompilerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...y-common/src/dev/shiza/honey/adventure/message/dispatcher/AdventureMessageDispatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...mon/src/dev/shiza/honey/adventure/message/dispatcher/AdventureMessageDispatcherBatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...-common/src/dev/shiza/honey/adventure/message/dispatcher/AdventureMessageDispatchers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
10 changes: 0 additions & 10 deletions
10
honey-common/src/dev/shiza/honey/adventure/message/dispatcher/MessageDispatcher.java
This file was deleted.
Oops, something went wrong.
31 changes: 0 additions & 31 deletions
31
honey-common/src/dev/shiza/honey/adventure/message/dispatcher/MessageDispatcherImpl.java
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
honey-common/src/dev/shiza/honey/adventure/message/dispatcher/MessageDispatchers.java
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
honey-common/src/dev/shiza/honey/adventure/placeholder/ParsableValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../shiza/honey/message/MessageCompiler.java → ...ney/message/compiler/MessageCompiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
honey-common/src/dev/shiza/honey/message/dispatcher/MessageBaseDispatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
honey-common/src/dev/shiza/honey/message/dispatcher/MessageDispatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.