Expose legacy serializer for placeholders

This commit is contained in:
Hannes Greule 2020-12-28 11:21:35 +01:00
parent ffc31f565b
commit 019da4d2f4
3 changed files with 49 additions and 21 deletions

View File

@ -119,6 +119,8 @@ import com.plotsquared.core.uuid.offline.OfflineModeUUIDService;
import com.sk89q.worldedit.WorldEdit;
import io.papermc.lib.PaperLib;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
@ -1107,4 +1109,10 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl
}
}
@Nonnull
@Override
public String toLegacyPlatformString(Component component) {
return LegacyComponentSerializer.legacyAmpersand().serialize(component);
}
}

View File

@ -48,6 +48,7 @@ import com.plotsquared.core.util.SetupUtils;
import com.plotsquared.core.util.WorldUtil;
import com.plotsquared.core.util.placeholders.PlaceholderRegistry;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -307,4 +308,6 @@ public interface PlotPlatform<P> extends LocaleHolder {
return injector().getInstance(PlaceholderRegistry.class);
}
@Nonnull String toLegacyPlatformString(Component component);
}

View File

@ -29,7 +29,9 @@ import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import com.google.inject.Singleton;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.LocaleHolder;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot;
@ -37,6 +39,8 @@ import com.plotsquared.core.plot.flag.GlobalFlagContainer;
import com.plotsquared.core.plot.flag.PlotFlag;
import com.plotsquared.core.util.EventDispatcher;
import com.plotsquared.core.util.PlayerManager;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -80,74 +84,75 @@ public final class PlaceholderRegistry {
this.createPlaceholder("has_plot", player -> player.getPlotCount() > 0 ? "true" : "false");
this.createPlaceholder("allowed_plot_count", (player) -> {
if (player.getAllowedPlots() >= Integer.MAX_VALUE) { // Beautifies cases with '*' permission
return String.valueOf(TranslatableCaption.of("info.infinite"));
return legacyComponent(TranslatableCaption.of("info.infinite"), player);
}
return Integer.toString(player.getAllowedPlots());
});
this.createPlaceholder("plot_count", player -> Integer.toString(player.getPlotCount()));
this.createPlaceholder("currentplot_alias", (player, plot) -> {
if (plot.getAlias() == null) {
return String.valueOf(TranslatableCaption.of("info.none"));
if (plot.getAlias().isEmpty()) {
return legacyComponent(TranslatableCaption.of("info.none"), player);
}
return plot.getAlias();
});
this.createPlaceholder("currentplot_owner", (player, plot) -> {
final UUID plotOwner = plot.getOwnerAbs();
if (plotOwner == null) {
return String.valueOf(TranslatableCaption.of("generic.generic_unowned"));
return legacyComponent(TranslatableCaption.of("generic.generic_unowned"), player);
}
try {
return PlayerManager.getName(plotOwner, false);
} catch (final Exception ignored) {
}
return String.valueOf(TranslatableCaption.of("info.unknown"));
return legacyComponent(TranslatableCaption.of("info.unknown"), player);
});
this.createPlaceholder("currentplot_members", (player, plot) -> {
if (plot.getMembers() == null && plot.getTrusted() == null) {
return String.valueOf(TranslatableCaption.of("info.none"));
if (plot.getMembers().isEmpty() && plot.getTrusted().isEmpty()) {
return legacyComponent(TranslatableCaption.of("info.none"), player);
}
return String.valueOf(plot.getMembers().size() + plot.getTrusted().size());
});
this.createPlaceholder("currentplot_members_added", (player, plot) -> {
if (plot.getMembers() == null) {
return String.valueOf(TranslatableCaption.of("info.none"));
if (plot.getMembers() .isEmpty()) {
return legacyComponent(TranslatableCaption.of("info.none"), player);
}
return String.valueOf(plot.getMembers().size());
});
this.createPlaceholder("currentplot_members_trusted", (player, plot) -> {
if (plot.getTrusted() == null) {
return String.valueOf(TranslatableCaption.of("info.none"));
if (plot.getTrusted().isEmpty()) {
return legacyComponent(TranslatableCaption.of("info.none"), player);
}
return String.valueOf(plot.getTrusted().size());
});
this.createPlaceholder("currentplot_members_denied", (player, plot) -> {
if (plot.getDenied() == null) {
return String.valueOf(TranslatableCaption.of("info.none"));
if (plot.getDenied().isEmpty()) {
return legacyComponent(TranslatableCaption.of("info.none"), player);
}
return String.valueOf(plot.getDenied().size());
});
this.createPlaceholder("currentplot_members_trusted_list", (player, plot) -> {
if (plot.getTrusted() == null) {
return String.valueOf(TranslatableCaption.of("info.none"));
if (plot.getTrusted().isEmpty()) {
return legacyComponent(TranslatableCaption.of("info.none"), player);
}
return String.valueOf(PlayerManager.getPlayerList(plot.getTrusted(), player));
return PlotSquared.platform().toLegacyPlatformString(
PlayerManager.getPlayerList(plot.getTrusted(), player));
});
this.createPlaceholder("currentplot_members_added_list", (player, plot) -> {
if (plot.getMembers() == null) {
return String.valueOf(TranslatableCaption.of("info.none"));
if (plot.getMembers().isEmpty()) {
return legacyComponent(TranslatableCaption.of("info.none"), player);
}
return String.valueOf(PlayerManager.getPlayerList(plot.getMembers(), player));
});
this.createPlaceholder("currentplot_members_denied_list", (player, plot) -> {
if (plot.getDenied() == null) {
return String.valueOf(TranslatableCaption.of("info.none"));
if (plot.getDenied().isEmpty()) {
return legacyComponent(TranslatableCaption.of("info.none"), player);
}
return String.valueOf(PlayerManager.getPlayerList(plot.getDenied(), player));
});
this.createPlaceholder("currentplot_creationdate", (player, plot) -> {
if (plot.getTimestamp() == 0) {
return String.valueOf(TranslatableCaption.of("info.unknown"));
return legacyComponent(TranslatableCaption.of("info.unknown"), player);
}
long creationDate = plot.getTimestamp();
SimpleDateFormat sdf = new SimpleDateFormat(Settings.Timeformat.DATE_FORMAT);
@ -258,6 +263,18 @@ public final class PlaceholderRegistry {
return Collections.unmodifiableCollection(this.placeholders.values());
}
/**
* Converts a {@link Component} into a legacy-formatted string.
*
* @param caption the caption key.
* @param localeHolder the locale holder to get the component for
* @return a legacy-formatted string.
*/
private static String legacyComponent(TranslatableCaption caption, LocaleHolder localeHolder) {
Component component = MiniMessage.get().parse(caption.getComponent(localeHolder));
return PlotSquared.platform().toLegacyPlatformString(component);
}
/**
* Event called when a new {@link Placeholder} has been added