-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement message action replacements
Signed-off-by: Pablo Herrera <[email protected]>
- Loading branch information
1 parent
81f4a16
commit 976aeca
Showing
2 changed files
with
83 additions
and
9 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
50 changes: 43 additions & 7 deletions
50
core/src/main/java/tc/oc/pgm/action/actions/MessageAction.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 |
---|---|---|
@@ -1,28 +1,64 @@ | ||
package tc.oc.pgm.action.actions; | ||
|
||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.regex.Pattern; | ||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.ComponentLike; | ||
import net.kyori.adventure.text.TextReplacementConfig; | ||
import net.kyori.adventure.title.Title; | ||
import org.jetbrains.annotations.Nullable; | ||
import tc.oc.pgm.util.Audience; | ||
|
||
public class MessageAction extends AbstractAction<Audience> { | ||
public class MessageAction<T extends Audience> extends AbstractAction<T> { | ||
private static final Pattern REPLACEMENT_PATTERN = Pattern.compile("\\{(.+?)}"); | ||
|
||
private final Component text; | ||
private final Component actionbar; | ||
private final Title title; | ||
private final Map<String, Replacement<T>> replacements; | ||
|
||
public MessageAction( | ||
@Nullable Component text, @Nullable Component actionbar, @Nullable Title title) { | ||
super(Audience.class); | ||
Class<T> scope, | ||
@Nullable Component text, | ||
@Nullable Component actionbar, | ||
@Nullable Title title, | ||
@Nullable Map<String, Replacement<T>> replacements) { | ||
super(scope); | ||
this.text = text; | ||
this.actionbar = actionbar; | ||
this.title = title; | ||
this.replacements = replacements; | ||
} | ||
|
||
@Override | ||
public void trigger(Audience audience) { | ||
if (text != null) audience.sendMessage(text); | ||
if (title != null) audience.showTitle(title); | ||
if (actionbar != null) audience.sendActionBar(actionbar); | ||
public void trigger(T scope) { | ||
if (text != null) scope.sendMessage(replace(text, scope)); | ||
if (title != null) scope.showTitle(replace(title, scope)); | ||
if (actionbar != null) scope.sendActionBar(replace(actionbar, scope)); | ||
} | ||
|
||
private Component replace(Component component, T scope) { | ||
if (component == null || replacements == null) { | ||
return component; | ||
} | ||
|
||
return component.replaceText( | ||
TextReplacementConfig.builder() | ||
.match(REPLACEMENT_PATTERN) | ||
.replacement( | ||
(match, original) -> { | ||
Replacement<T> r = replacements.get(match.group(1)); | ||
return r != null ? r.apply(scope) : original; | ||
}) | ||
.build()); | ||
} | ||
|
||
private Title replace(Title title, T scope) { | ||
if (replacements == null) return title; | ||
return Title.title( | ||
replace(title.title(), scope), replace(title.subtitle(), scope), title.times()); | ||
} | ||
|
||
public interface Replacement<T extends Audience> extends Function<T, ComponentLike> {} | ||
} |