diff --git a/core/src/main/java/tc/oc/pgm/action/ActionParser.java b/core/src/main/java/tc/oc/pgm/action/ActionParser.java index e1e270a078..c35e569a85 100644 --- a/core/src/main/java/tc/oc/pgm/action/ActionParser.java +++ b/core/src/main/java/tc/oc/pgm/action/ActionParser.java @@ -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; @@ -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") diff --git a/core/src/main/java/tc/oc/pgm/action/actions/ChatMessageAction.java b/core/src/main/java/tc/oc/pgm/action/actions/ChatMessageAction.java deleted file mode 100644 index 5fd7eaf28a..0000000000 --- a/core/src/main/java/tc/oc/pgm/action/actions/ChatMessageAction.java +++ /dev/null @@ -1,19 +0,0 @@ -package tc.oc.pgm.action.actions; - -import net.kyori.adventure.text.Component; -import tc.oc.pgm.util.Audience; - -public class ChatMessageAction extends AbstractAction { - - private final Component text; - - public ChatMessageAction(Component text) { - super(Audience.class); - this.text = text; - } - - @Override - public void trigger(Audience audience) { - audience.sendMessage(text); - } -} diff --git a/core/src/main/java/tc/oc/pgm/action/actions/MessageAction.java b/core/src/main/java/tc/oc/pgm/action/actions/MessageAction.java new file mode 100644 index 0000000000..a26e1b8a5a --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/action/actions/MessageAction.java @@ -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 { + + 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); + } +} diff --git a/util/src/main/java/tc/oc/pgm/util/xml/XMLUtils.java b/util/src/main/java/tc/oc/pgm/util/xml/XMLUtils.java index f4b9d4a493..d5a31b361f 100644 --- a/util/src/main/java/tc/oc/pgm/util/xml/XMLUtils.java +++ b/util/src/main/java/tc/oc/pgm/util/xml/XMLUtils.java @@ -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; @@ -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); + } }