Skip to content

Commit

Permalink
Implement title and actionbar for message action
Browse files Browse the repository at this point in the history
Signed-off-by: Pablete1234 <[email protected]>
  • Loading branch information
Pablete1234 committed Oct 8, 2022
1 parent 99127f9 commit cbab949
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 22 deletions.
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
19 changes: 0 additions & 19 deletions core/src/main/java/tc/oc/pgm/action/actions/ChatMessageAction.java

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);
}
}

0 comments on commit cbab949

Please sign in to comment.