Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement title and actionbar for message action #1067

Merged
merged 1 commit into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions core/src/main/java/tc/oc/pgm/action/ActionParser.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package tc.oc.pgm.action;

import static net.kyori.adventure.text.Component.empty;

import com.google.common.collect.ImmutableList;
import java.lang.reflect.Method;
import java.util.Map;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.title.Title;
import org.bukkit.inventory.ItemStack;
import org.jdom2.Element;
import org.jetbrains.annotations.Nullable;
import tc.oc.pgm.action.actions.ActionNode;
import tc.oc.pgm.action.actions.ChatMessageAction;
import tc.oc.pgm.action.actions.KillEntitiesAction;
import tc.oc.pgm.action.actions.MessageAction;
import tc.oc.pgm.action.actions.ReplaceItemAction;
import tc.oc.pgm.action.actions.ScopeSwitchAction;
import tc.oc.pgm.action.actions.SetVariableAction;
Expand Down Expand Up @@ -180,8 +184,25 @@ public Kit parseKitTrigger(Element el, Class<?> scope) throws InvalidXMLExceptio
}

@MethodParser("message")
public ChatMessageAction parseChatMessage(Element el, Class<?> scope) throws InvalidXMLException {
return new ChatMessageAction(XMLUtils.parseFormattedText(Node.fromRequiredAttr(el, "text")));
public MessageAction parseChatMessage(Element el, Class<?> scope) throws InvalidXMLException {
Component text = XMLUtils.parseFormattedText(Node.fromChildOrAttr(el, "text"));
Component actionbar = XMLUtils.parseFormattedText(Node.fromChildOrAttr(el, "actionbar"));

Node titleNode = Node.fromChildOrAttr(el, "title");
Node subtitleNode = Node.fromChildOrAttr(el, "subtitle");
Title title = null;
if (titleNode != null || subtitleNode != null)
title =
Title.title(
XMLUtils.parseFormattedText(titleNode, empty()),
XMLUtils.parseFormattedText(subtitleNode, empty()),
XMLUtils.parseTitleTimes(el, Title.DEFAULT_TIMES));

if (text == null && actionbar == null && title == null)
throw new InvalidXMLException(
"Expected at least one of text, title, subtitle or actionbar", el);

return new MessageAction(text, actionbar, title);
}

@MethodParser("set")
Expand Down

This file was deleted.

28 changes: 28 additions & 0 deletions core/src/main/java/tc/oc/pgm/action/actions/MessageAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tc.oc.pgm.action.actions;

import javax.annotation.Nullable;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.title.Title;
import tc.oc.pgm.util.Audience;

public class MessageAction extends AbstractAction<Audience> {

private final Component text;
private final Component actionbar;
private final Title title;

public MessageAction(
@Nullable Component text, @Nullable Component actionbar, @Nullable Title title) {
super(Audience.class);
this.text = text;
this.actionbar = actionbar;
this.title = title;
}

@Override
public void trigger(Audience audience) {
if (text != null) audience.sendMessage(text);
if (title != null) audience.showTitle(title);
if (actionbar != null) audience.sendActionBar(actionbar);
}
}
10 changes: 10 additions & 0 deletions util/src/main/java/tc/oc/pgm/util/xml/XMLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.title.Title;
import org.bukkit.*;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Entity;
Expand Down Expand Up @@ -1118,4 +1119,13 @@ public static LocalDate parseDate(Node node) throws InvalidXMLException {
throw new InvalidXMLException(node, e);
}
}

public static Title.Times parseTitleTimes(Element el, Title.Times def)
throws InvalidXMLException {
Duration fadeIn = XMLUtils.parseDuration(Node.fromAttr(el, "fade-in"), def.fadeIn());
Duration stay = XMLUtils.parseDuration(Node.fromAttr(el, "stay"), def.stay());
Duration fadeOut = XMLUtils.parseDuration(Node.fromAttr(el, "fade-out"), def.fadeOut());

return Title.Times.times(fadeIn, stay, fadeOut);
}
}