Use adventure for titles

This commit is contained in:
Alexander Söderberg 2020-07-05 17:57:58 +02:00
parent f287cc34e7
commit 42d648e338
No known key found for this signature in database
GPG Key ID: C0207FF7EA146678
6 changed files with 66 additions and 30 deletions

View File

@ -44,10 +44,12 @@ import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes; import com.sk89q.worldedit.world.item.ItemTypes;
import io.papermc.lib.PaperLib; import io.papermc.lib.PaperLib;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template; import net.kyori.adventure.text.minimessage.Template;
import net.kyori.adventure.title.Title;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.WeatherType; import org.bukkit.WeatherType;
@ -59,6 +61,8 @@ import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.RegisteredListener; import org.bukkit.plugin.RegisteredListener;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Arrays; import java.util.Arrays;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -144,11 +148,6 @@ public class BukkitPlayer extends PlotPlayer<Player> {
return true; return true;
} }
@Override
public void sendTitle(String title, String subtitle, int fadeIn, int stay, int fadeOut) {
player.sendTitle(title, subtitle, fadeIn, stay, fadeOut);
}
private void callEvent(@NotNull final Event event) { private void callEvent(@NotNull final Event event) {
final RegisteredListener[] listeners = event.getHandlers().getRegisteredListeners(); final RegisteredListener[] listeners = event.getHandlers().getRegisteredListeners();
for (final RegisteredListener listener : listeners) { for (final RegisteredListener listener : listeners) {
@ -234,6 +233,16 @@ public class BukkitPlayer extends PlotPlayer<Player> {
return this.player.isPermissionSet(permission); return this.player.isPermissionSet(permission);
} }
@Override
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);
final Audience audience = BUKKIT_AUDIENCES.player(this.player);
audience.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, @Override public void sendMessage(@NotNull final Caption caption,
@NotNull final Template... replacements) { @NotNull final Template... replacements) {
// TODO: Inject the prefix here // TODO: Inject the prefix here

View File

@ -78,7 +78,7 @@ public final class CaptionLoader {
JsonObject.class); JsonObject.class);
Map<TranslatableCaption, String> captions = new HashMap<>(); Map<TranslatableCaption, String> captions = new HashMap<>();
for (Map.Entry<String, JsonElement> entry : object.entrySet()) { for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
TranslatableCaption key = TranslatableCaption.keyed(entry.getKey()); TranslatableCaption key = TranslatableCaption.of(entry.getKey());
captions.put(key, entry.getValue().getAsString()); captions.put(key, entry.getValue().getAsString());
} }
return new LocalizedCaptionMap(locale, captions); return new LocalizedCaptionMap(locale, captions);

View File

@ -48,7 +48,7 @@ public final class TranslatableCaption implements KeyedCaption {
* @param key Caption key * @param key Caption key
* @return Caption instance * @return Caption instance
*/ */
@NotNull public static TranslatableCaption keyed(@NotNull final String key) { @NotNull public static TranslatableCaption of(@NotNull final String key) {
return new TranslatableCaption(key.toLowerCase(Locale.ENGLISH)); return new TranslatableCaption(key.toLowerCase(Locale.ENGLISH));
} }

View File

@ -29,6 +29,7 @@ import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.collection.ByteArrayUtilities; import com.plotsquared.core.collection.ByteArrayUtilities;
import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.events.PlotFlagRemoveEvent; import com.plotsquared.core.events.PlotFlagRemoveEvent;
import com.plotsquared.core.events.Result; import com.plotsquared.core.events.Result;
import com.plotsquared.core.location.Location; import com.plotsquared.core.location.Location;
@ -65,6 +66,7 @@ import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.gamemode.GameModes; import com.sk89q.worldedit.world.gamemode.GameModes;
import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes; import com.sk89q.worldedit.world.item.ItemTypes;
import net.kyori.adventure.text.minimessage.Template;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -259,20 +261,16 @@ public class PlotListener {
TaskManager.runTaskLaterAsync(() -> { TaskManager.runTaskLaterAsync(() -> {
Plot lastPlot = player.getMeta(PlotPlayer.META_LAST_PLOT); Plot lastPlot = player.getMeta(PlotPlayer.META_LAST_PLOT);
if ((lastPlot != null) && plot.getId().equals(lastPlot.getId())) { if ((lastPlot != null) && plot.getId().equals(lastPlot.getId())) {
Map<String, String> replacements = new HashMap<>(); player.sendTitle(
replacements.put("%x%", String.valueOf(lastPlot.getId().x)); TranslatableCaption.of("titles.title_entered_plot"),
replacements.put("%z%", lastPlot.getId().y + ""); TranslatableCaption.of("titles.title_entered_plot_sub"),
replacements.put("%world%", plot.getArea().toString()); Template.of("x", Integer.toString(lastPlot.getId().getX())),
replacements.put("%greeting%", greeting); Template.of("z", Integer.toString(lastPlot.getId().getY())),
replacements.put("%alias", plot.toString()); Template.of("world", plot.getArea().toString()),
replacements.put("%s", MainUtil.getName(plot.getOwner())); Template.of("greeting", greeting),
String main = StringMan Template.of("alias", plot.toString()),
.replaceFromMap(Captions.TITLE_ENTERED_PLOT.getTranslated(), Template.of("owner", MainUtil.getName(plot.getOwner()))
replacements); );
String sub = StringMan
.replaceFromMap(Captions.TITLE_ENTERED_PLOT_SUB.getTranslated(),
replacements);
player.sendTitle(main, sub);
} }
}, 20); }, 20);
} }

View File

@ -29,6 +29,7 @@ import com.google.common.base.Preconditions;
import com.plotsquared.core.PlotSquared; import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.command.CommandCaller; import com.plotsquared.core.command.CommandCaller;
import com.plotsquared.core.command.RequiredType; import com.plotsquared.core.command.RequiredType;
import com.plotsquared.core.configuration.Caption;
import com.plotsquared.core.configuration.CaptionUtility; import com.plotsquared.core.configuration.CaptionUtility;
import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
@ -54,6 +55,7 @@ import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.world.gamemode.GameMode; import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.item.ItemType; import com.sk89q.worldedit.world.item.ItemType;
import lombok.NonNull; import lombok.NonNull;
import net.kyori.adventure.text.minimessage.Template;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -79,7 +81,8 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
public static final String META_LOCATION = "location"; public static final String META_LOCATION = "location";
// Used to track debug mode // Used to track debug mode
private static final Set<PlotPlayer<?>> debugModeEnabled = Collections.synchronizedSet(new HashSet<>()); private static final Set<PlotPlayer<?>> debugModeEnabled =
Collections.synchronizedSet(new HashSet<>());
private static final Map<Class, PlotPlayerConverter> converters = new HashMap<>(); private static final Map<Class, PlotPlayerConverter> converters = new HashMap<>();
private Map<String, byte[]> metaMap = new HashMap<>(); private Map<String, byte[]> metaMap = new HashMap<>();
@ -408,12 +411,31 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
return result; return result;
} }
public void sendTitle(String title, String subtitle) { /**
sendTitle(title, subtitle, 10, 50, 10); * 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 replacements Variable replacements
*/
public void sendTitle(@NotNull final Caption title, @NotNull final Caption subtitle,
@NotNull final Template ... replacements) {
sendTitle(title, subtitle, 10, 50, 10, replacements);
} }
public abstract void sendTitle(String title, String subtitle, int fadeIn, int stay, /**
int fadeOut); * Send a title to the player
*
* @param title Title text
* @param subtitle Subtitle text
* @param fadeIn time in ticks for titles to fade in. Defaults to 10
* @param stay time in ticks for titles to stay. Defaults to 70
* @param fadeOut time in ticks for titles to fade out. Defaults to 20
* @param replacements Variable replacements
*/
public abstract void sendTitle(@NotNull Caption title, @NotNull Caption subtitle, int fadeIn,
int stay, int fadeOut, @NotNull final Template ... replacements);
/** /**
* Teleport this player to a location. * Teleport this player to a location.
@ -774,7 +796,9 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
* The amount of money this Player has. * The amount of money this Player has.
*/ */
public double getMoney() { public double getMoney() {
return EconHandler.getEconHandler() == null ? 0 : EconHandler.getEconHandler().getMoney(this); return EconHandler.getEconHandler() == null ?
0 :
EconHandler.getEconHandler().getMoney(this);
} }
public void withdraw(double amount) { public void withdraw(double amount) {

View File

@ -26,12 +26,14 @@
package com.plotsquared.core.plot.comment; package com.plotsquared.core.plot.comment;
import com.google.common.annotations.Beta; import com.google.common.annotations.Beta;
import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.StaticCaption;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.util.task.RunnableVal; import com.plotsquared.core.util.task.RunnableVal;
import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.util.task.TaskManager;
import net.kyori.adventure.text.minimessage.Template;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
@ -66,8 +68,11 @@ import java.util.concurrent.atomic.AtomicInteger;
total = count.get(); total = count.get();
} }
if ((size.decrementAndGet() == 0) && (total > 0)) { if ((size.decrementAndGet() == 0) && (total > 0)) {
player.sendTitle("", Captions.INBOX_NOTIFICATION.getTranslated() player.sendTitle(
.replaceAll("%s", "" + total)); StaticCaption.of(""),
TranslatableCaption.of("comment.inbox_notification"),
Template.of("amount", Integer.toString(total))
);
} }
} }
}); });