From a69b1d895cb8278b3538c341ce68e396cc3990ea Mon Sep 17 00:00:00 2001 From: Glare Date: Sun, 23 May 2021 10:28:03 -0500 Subject: [PATCH] Implemented the ability to change titles to actionbar on plot entry (#3060) * Implemented the ability to change titles to actionbar on plot entry * Fixed typo in action * Updated explanation in Javadoc * Implemented suggestions * Remove excess import * Implemented PR suggestions Co-authored-by: NotMyFault --- .../core/configuration/Settings.java | 5 +++ .../core/listener/PlotListener.java | 11 +++++-- .../plotsquared/core/player/PlotPlayer.java | 32 +++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/configuration/Settings.java b/Core/src/main/java/com/plotsquared/core/configuration/Settings.java index bc0c93a58..2ee5f1ba2 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/Settings.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/Settings.java @@ -58,6 +58,11 @@ public class Settings extends Config { public static int TITLES_STAY = 50; @Comment("Plot titles fading out (duration in ticks)") public static int TITLES_FADE_OUT = 20; + @Comment({"Changes the notification method on plot entry from Title + SubTitle -> ActionBar.", + "The message still sent to the player is pulled from the lang key \"titles.title_entered_plot\".", + "If you would like to still show the owner of the plot, append the contents of \"titles.title_entered_plot_sub\" onto the " + + "former lang key."}) + public static boolean TITLES_AS_ACTIONBAR = false; @Create // This value will be generated automatically public static ConfigBlock AUTO_CLEAR = null; diff --git a/Core/src/main/java/com/plotsquared/core/listener/PlotListener.java b/Core/src/main/java/com/plotsquared/core/listener/PlotListener.java index a382e675a..110c51e20 100644 --- a/Core/src/main/java/com/plotsquared/core/listener/PlotListener.java +++ b/Core/src/main/java/com/plotsquared/core/listener/PlotListener.java @@ -303,8 +303,15 @@ public class PlotListener { Template plotTemplate = Template.of("plot", lastPlot.getId().toString()); Template worldTemplate = Template.of("world", player.getLocation().getWorldName()); Template ownerTemplate = Template.of("owner", owner); - final Consumer userConsumer = user -> player - .sendTitle(header, subHeader, plotTemplate, worldTemplate, ownerTemplate); + + final Consumer userConsumer = user -> { + if (Settings.TITLES_AS_ACTIONBAR) { + player.sendActionBar(header, plotTemplate, worldTemplate, ownerTemplate); + } else { + player.sendTitle(header, subHeader, plotTemplate, worldTemplate, ownerTemplate); + } + }; + UUID uuid = plot.getOwner(); if (uuid == null) { userConsumer.accept("Unknown"); diff --git a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java index fb2c764f6..517d4bef9 100644 --- a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java +++ b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java @@ -841,6 +841,38 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer, .title(titleComponent, subtitleComponent, times)); } + /** + * Method designed to send an ActionBar to a player. + * + * @param caption Caption + * @param replacements Variable replacements + */ + public void sendActionBar( + final @NonNull Caption caption, + final @NonNull Template... replacements + ) { + String message; + try { + message = caption.getComponent(this); + } catch (final CaptionMap.NoSuchCaptionException exception) { + // This sends feedback to the player + message = NON_EXISTENT_CAPTION + ((TranslatableCaption) caption).getKey(); + // And this also prints it to the console + exception.printStackTrace(); + } + if (message.isEmpty()) { + return; + } + // Replace placeholders, etc + message = CaptionUtility.format(this, message) + .replace('\u2010', '%').replace('\u2020', '&').replace('\u2030', '&') + .replace("", TranslatableCaption.of("core.prefix").getComponent(this)); + + + final Component component = MiniMessage.get().parse(message, replacements); + getAudience().sendActionBar(component); + } + @Override public void sendMessage( final @NonNull Caption caption,