From fbf6a3517d41258f2cd7f5bd43203dc1dcd68e93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 14 Jul 2020 15:04:31 +0200 Subject: [PATCH] Add back title method that somehow went missing and add a template utility class to make templates less annoying to work with --- .../configuration/caption/KeyedCaption.java | 25 ++++ .../core/configuration/caption/Templates.java | 111 ++++++++++++++++++ .../core/listener/PlotListener.java | 13 +- .../plotsquared/core/player/PlotPlayer.java | 44 +++++-- 4 files changed, 176 insertions(+), 17 deletions(-) create mode 100644 Core/src/main/java/com/plotsquared/core/configuration/caption/Templates.java diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/KeyedCaption.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/KeyedCaption.java index 9a2b78c8e..c83173231 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/caption/KeyedCaption.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/KeyedCaption.java @@ -1,3 +1,28 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package com.plotsquared.core.configuration.caption; import com.plotsquared.core.configuration.Caption; diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/Templates.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/Templates.java new file mode 100644 index 000000000..c67afce78 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/Templates.java @@ -0,0 +1,111 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.plotsquared.core.configuration.caption; + +import com.plotsquared.core.configuration.Caption; +import com.plotsquared.core.plot.PlotArea; +import com.plotsquared.core.util.MainUtil; +import lombok.experimental.UtilityClass; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.Template; +import org.jetbrains.annotations.NotNull; + +import java.util.UUID; + +/** + * Utility class that generates {@link net.kyori.adventure.text.minimessage.Template templates} + */ +@UtilityClass +public class Templates { + + private final MiniMessage MINI_MESSAGE = MiniMessage.builder().build(); + + /** + * Create a {@link net.kyori.adventure.text.minimessage.Template} from a PlotSquared {@link Caption} + * + * @param localeHolder Locale holder + * @param key Template key + * @param caption Caption object + * @param replacements Replacements + * @return Generated template + */ + @NotNull public Template of(@NotNull final LocaleHolder localeHolder, + @NotNull final String key, + @NotNull final Caption caption, + @NotNull final Template... replacements) { + return Template.of(key, MINI_MESSAGE.parse(caption.getComponent(localeHolder), replacements)); + } + + /** + * Create a {@link Template} from a username (using UUID mappings) + * + * @param key Template key + * @param uuid Player UUID + * @return Generated template + */ + @NotNull public Template of(@NotNull final String key, + @NotNull final UUID uuid) { + final String username = MainUtil.getName(uuid); + return Template.of(key, username); + } + + /** + * Create a {@link Template} from a string + * + * @param key Template key + * @param value Template value + * @return Generated template + */ + @NotNull public Template of(@NotNull final String key, + @NotNull final String value) { + return Template.of(key, value); + } + + /** + * Create a {@link Template} from a plot area + * + * @param key Template Key + * @param area Plot area + * @return Generated template + */ + @NotNull public Template of(@NotNull final String key, + @NotNull final PlotArea area) { + return Template.of(key, area.toString()); + } + + /** + * Create a {@link Template} from a number + * + * @param key Template key + * @param number Number + * @return Generated template + */ + @NotNull public Template of(@NotNull final String key, + @NotNull final Number number) { + return Template.of(key, number.toString()); + } + +} 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 a43587744..162129d52 100644 --- a/Core/src/main/java/com/plotsquared/core/listener/PlotListener.java +++ b/Core/src/main/java/com/plotsquared/core/listener/PlotListener.java @@ -29,6 +29,7 @@ import com.plotsquared.core.PlotSquared; import com.plotsquared.core.collection.ByteArrayUtilities; import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Settings; +import com.plotsquared.core.configuration.caption.Templates; import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.events.PlotFlagRemoveEvent; import com.plotsquared.core.events.Result; @@ -264,12 +265,12 @@ public class PlotListener { player.sendTitle( TranslatableCaption.of("titles.title_entered_plot"), TranslatableCaption.of("titles.title_entered_plot_sub"), - Template.of("x", Integer.toString(lastPlot.getId().getX())), - Template.of("z", Integer.toString(lastPlot.getId().getY())), - Template.of("world", plot.getArea().toString()), - Template.of("greeting", greeting), - Template.of("alias", plot.toString()), - Template.of("owner", MainUtil.getName(plot.getOwner())) + Templates.of("x", lastPlot.getId().getX()), + Templates.of("z", lastPlot.getId().getY()), + Templates.of("world", plot.getArea()), + Templates.of("greeting", greeting), + Templates.of("alias", plot.getAlias()), + Templates.of("owner", plot.getOwner()) ); } }, 20); 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 387b5dcb9..6b2707c1f 100644 --- a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java +++ b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java @@ -34,6 +34,7 @@ import com.plotsquared.core.configuration.Caption; import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.caption.LocaleHolder; +import com.plotsquared.core.configuration.caption.Templates; import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.database.DBFunc; import com.plotsquared.core.events.TeleportCause; @@ -688,7 +689,8 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer, TaskManager.runTask(() -> { if (getMeta("teleportOnLogin", true)) { teleport(location); - sendMessage(TranslatableCaption.of("teleport.teleported_to_plot")); + sendMessage( + TranslatableCaption.of("teleport.teleported_to_plot")); } }); } else if (!PlotSquared.get().isMainThread(Thread.currentThread())) { @@ -698,7 +700,8 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer, if (getMeta("teleportOnLogin", true)) { if (plot.isLoaded()) { teleport(location); - sendMessage(TranslatableCaption.of("teleport.teleported_to_plot")); + sendMessage(TranslatableCaption + .of("teleport.teleported_to_plot")); } } })); @@ -735,16 +738,35 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer, * Send a title to the player that fades in, in 10 ticks, stays for 50 ticks and fades * out in 20 ticks * - * @param title Title text - * @param subtitle Subtitle text + * @param title Title text + * @param subtitle Subtitle text * @param replacements Variable replacements */ public void sendTitle(@NotNull final Caption title, @NotNull final Caption subtitle, - final int fadeIn, final int stay, final int fadeOut, @NotNull final Template ... replacements) { + @NotNull final Template... replacements) { + sendTitle(title, subtitle, 10, 50, 20, replacements); + } + + /** + * Send a title to to the player + * + * @param title Title + * @param subtitle Subtitle + * @param fadeIn Fade in time (in ticks) + * @param stay The the title stays for (in ticks) + * @param fadeOut Fade out time (in ticks) + * @param replacements Variable replacements + */ + public void sendTitle(@NotNull final Caption title, @NotNull final Caption subtitle, + final int fadeIn, final int stay, final int fadeOut, + @NotNull final Template... replacements) { final Component titleComponent = MINI_MESSAGE.parse(title.getComponent(this), replacements); - final Component subtitleComponent = MINI_MESSAGE.parse(subtitle.getComponent(this), replacements); - getAudience().showTitle(Title.of(titleComponent, subtitleComponent, Duration.of(fadeIn * 50, - ChronoUnit.MILLIS), Duration.of(stay * 50, ChronoUnit.MILLIS), Duration.of(fadeOut * 50, ChronoUnit.MILLIS))); + final Component subtitleComponent = + MINI_MESSAGE.parse(subtitle.getComponent(this), replacements); + getAudience().showTitle(Title + .of(titleComponent, subtitleComponent, Duration.of(fadeIn * 50, ChronoUnit.MILLIS), + Duration.of(stay * 50, ChronoUnit.MILLIS), + Duration.of(fadeOut * 50, ChronoUnit.MILLIS))); } @Override public void sendMessage(@NotNull final Caption caption, @@ -755,11 +777,11 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer, } // Create the template list, and add the prefix as a replacement final List