Skip to content

Commit

Permalink
feat: add action bar and improve title support
Browse files Browse the repository at this point in the history
  • Loading branch information
Thatsmusic99 committed May 10, 2023
1 parent 89a6330 commit 14992b4
Showing 1 changed file with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.HoverEventSource;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
Expand Down Expand Up @@ -513,10 +514,11 @@ public static void sendMessage(
@Nullable final TagResolver... placeholders
) {
if (config == null) return;
if (supportsTitles() && sender instanceof Player player) {
if (sender instanceof Player player) {
ConfigSection titles = config.getConfigSection(path + "_title");
ConfigSection subtitles = config.getConfigSection(path + "_subtitle");
if (titles != null || subtitles != null) {
ConfigSection actionBars = config.getConfigSection(path + "_actionbar");
if (titles != null || subtitles != null || actionBars != null) {

// Fade in, stay, out
int[] titleInfo = new int[]{0, 0, 0};
Expand All @@ -532,6 +534,7 @@ public static void sendMessage(
private int current = 0;
@Nullable private Component previousTitle = null;
@Nullable private Component previousSubtitle = null;
@Nullable private Component previousActionBar = null;

@Override
public void run() {
Expand All @@ -542,14 +545,12 @@ public void run() {

String title = null;
String subtitle = null;
String actionbar = null;

if (titles != null) {
title = titles.getString(String.valueOf(current));
}
if (titles != null) title = titles.getPath() + "." + current;
if (subtitles != null) subtitle = subtitles.getPath() + "." + current;
if (actionBars != null) actionbar = actionBars.getPath() + "." + current;

if (subtitles != null) {
subtitle = subtitles.getString(String.valueOf(current));
}

asAudience(player).showTitle(
Title.title(
Expand All @@ -563,6 +564,8 @@ public void run() {
)
);

asAudience(player).sendActionBar(actionbar == null ? (previousActionBar == null ? Component.empty() : previousActionBar) : (previousActionBar = get(actionbar, placeholders)));

current++;
}
};
Expand All @@ -571,14 +574,31 @@ public void run() {
}
}

final var component = Component.text();
var component = Component.text();
if (config.get(path) instanceof List) {
config.getStringList(path).forEach(line -> component.append(get(preProcess.apply(line), placeholders)));
} else component.append(get(path, placeholders));

// TODO - broken
config.getStringList(path).forEach(line -> appendNonEmpty(component, preProcess, line, placeholders));
} else appendNonEmpty(component, preProcess, path, placeholders);

if (component.content().isEmpty() && component.children().size() == 0) return;
asAudience(sender).sendMessage(component);
}

private static void appendNonEmpty(
@NotNull final TextComponent.Builder base,
@NotNull final Function<String, String> preProcess,
@NotNull final String line,
@Nullable final TagResolver... placeholders
) {
if (line.isEmpty()) return;
Component component = get(preProcess.apply(line), placeholders);
base.append(component);

var component2 = Component.text();
component2.append(component);
}

public static void sendMessage(
@NotNull final CommandSender sender,
@NotNull final String path,
Expand Down Expand Up @@ -703,16 +723,6 @@ public static void failable(
);
}


private static boolean supportsTitles() {
try {
Player.class.getDeclaredMethod("sendTitle", String.class, String.class, int.class, int.class, int.class);
return true;
} catch (NoSuchMethodException e) {
return false;
}
}

private void addFormsDefault(String command, String title, String description) {
addDefault("Forms." + command + "-title", title);
addDefault("Forms." + command + "-description", description);
Expand Down

0 comments on commit 14992b4

Please sign in to comment.