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.ItemTypes;
import io.papermc.lib.PaperLib;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
import net.kyori.adventure.title.Title;
import org.bukkit.GameMode;
import org.bukkit.Sound;
import org.bukkit.WeatherType;
@ -59,6 +61,8 @@ import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.RegisteredListener;
import org.jetbrains.annotations.NotNull;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Set;
import java.util.UUID;
@ -144,11 +148,6 @@ public class BukkitPlayer extends PlotPlayer<Player> {
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) {
final RegisteredListener[] listeners = event.getHandlers().getRegisteredListeners();
for (final RegisteredListener listener : listeners) {
@ -234,6 +233,16 @@ public class BukkitPlayer extends PlotPlayer<Player> {
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,
@NotNull final Template... replacements) {
// TODO: Inject the prefix here

View File

@ -78,7 +78,7 @@ public final class CaptionLoader {
JsonObject.class);
Map<TranslatableCaption, String> captions = new HashMap<>();
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());
}
return new LocalizedCaptionMap(locale, captions);

View File

@ -48,7 +48,7 @@ public final class TranslatableCaption implements KeyedCaption {
* @param key Caption key
* @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));
}

View File

@ -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.TranslatableCaption;
import com.plotsquared.core.events.PlotFlagRemoveEvent;
import com.plotsquared.core.events.Result;
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.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import net.kyori.adventure.text.minimessage.Template;
import java.util.HashMap;
import java.util.Iterator;
@ -259,20 +261,16 @@ public class PlotListener {
TaskManager.runTaskLaterAsync(() -> {
Plot lastPlot = player.getMeta(PlotPlayer.META_LAST_PLOT);
if ((lastPlot != null) && plot.getId().equals(lastPlot.getId())) {
Map<String, String> replacements = new HashMap<>();
replacements.put("%x%", String.valueOf(lastPlot.getId().x));
replacements.put("%z%", lastPlot.getId().y + "");
replacements.put("%world%", plot.getArea().toString());
replacements.put("%greeting%", greeting);
replacements.put("%alias", plot.toString());
replacements.put("%s", MainUtil.getName(plot.getOwner()));
String main = StringMan
.replaceFromMap(Captions.TITLE_ENTERED_PLOT.getTranslated(),
replacements);
String sub = StringMan
.replaceFromMap(Captions.TITLE_ENTERED_PLOT_SUB.getTranslated(),
replacements);
player.sendTitle(main, sub);
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()))
);
}
}, 20);
}

View File

@ -29,6 +29,7 @@ import com.google.common.base.Preconditions;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.command.CommandCaller;
import com.plotsquared.core.command.RequiredType;
import com.plotsquared.core.configuration.Caption;
import com.plotsquared.core.configuration.CaptionUtility;
import com.plotsquared.core.configuration.Captions;
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.item.ItemType;
import lombok.NonNull;
import net.kyori.adventure.text.minimessage.Template;
import org.jetbrains.annotations.NotNull;
import java.nio.ByteBuffer;
@ -79,7 +81,8 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
public static final String META_LOCATION = "location";
// 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 Map<String, byte[]> metaMap = new HashMap<>();
@ -408,12 +411,31 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
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.
@ -774,7 +796,9 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
* The amount of money this Player has.
*/
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) {

View File

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