chore: fix minimessage messages, fix circular method reference

This commit is contained in:
Pierre Maurice Schwang 2022-05-12 22:35:49 +02:00
parent 3afd087663
commit b7743b06f0
18 changed files with 115 additions and 58 deletions

View File

@ -288,8 +288,8 @@ public class BlockEventListener implements Listener {
pp.sendMessage(
TranslatableCaption.of("height.height_limit"),
TagResolver.builder()
.tag("minHeight", Tag.inserting(Component.text(area.getMinBuildHeight())))
.tag("maxHeight", Tag.inserting(Component.text(area.getMaxBuildHeight())))
.tag("minheight", Tag.inserting(Component.text(area.getMinBuildHeight())))
.tag("maxheight", Tag.inserting(Component.text(area.getMaxBuildHeight())))
.build()
);
}
@ -388,8 +388,8 @@ public class BlockEventListener implements Listener {
plotPlayer.sendMessage(
TranslatableCaption.of("height.height_limit"),
TagResolver.builder()
.tag("minHeight", Tag.inserting(Component.text(area.getMinBuildHeight())))
.tag("maxHeight", Tag.inserting(Component.text(area.getMaxBuildHeight())))
.tag("minheight", Tag.inserting(Component.text(area.getMinBuildHeight())))
.tag("maxheight", Tag.inserting(Component.text(area.getMaxBuildHeight())))
.build()
);
}
@ -1284,8 +1284,8 @@ public class BlockEventListener implements Listener {
pp.sendMessage(
TranslatableCaption.of("height.height_limit"),
TagResolver.builder()
.tag("minHeight", Tag.inserting(Component.text(area.getMinBuildHeight())))
.tag("maxHeight", Tag.inserting(Component.text(area.getMaxBuildHeight())))
.tag("minheight", Tag.inserting(Component.text(area.getMinBuildHeight())))
.tag("maxheight", Tag.inserting(Component.text(area.getMaxBuildHeight())))
.build()
);
event.setCancelled(true);

View File

@ -84,8 +84,10 @@ import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.world.block.BlockType;
import io.papermc.lib.PaperLib;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.FluidCollisionMode;
@ -741,18 +743,13 @@ public class PlayerEventListener extends PlotListener implements Listener {
builder.tag("world", Tag.inserting(Component.text(worldName)));
builder.tag("plot_id", Tag.inserting(Component.text(id.toString())));
builder.tag("sender", Tag.inserting(Component.text(sender)));
// If we do/don't want colour, we need to be careful about how to go about it, as players could attempt either <gold></gold> or &6 etc.
// In both cases, we want to use a Component Template to ensure that the player cannot use any placeholders in their message on purpose
// or accidentally, as component templates are done at the end. We also need to deserialize from legacy color codes to a Component if
// allowing colour.
if (plotPlayer.hasPermission("plots.chat.color")) {
builder.tag(
"msg",
Tag.inserting(BukkitUtil.LEGACY_COMPONENT_SERIALIZER.deserialize(ChatColor.translateAlternateColorCodes(
'&',
message
)))
);
builder.tag("msg", Tag.inserting(MiniMessage.miniMessage().deserialize(
message,
TagResolver.resolver(StandardTags.color(), StandardTags.gradient(),
StandardTags.rainbow(), StandardTags.decorations()
)
)));
} else {
builder.tag("msg", Tag.inserting(Component.text(message)));
}

View File

@ -25,9 +25,11 @@
*/
package com.plotsquared.core.command;
import com.plotsquared.core.configuration.caption.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.StringMan;
import com.sk89q.worldedit.command.util.SuggestionHelper;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.biome.BiomeTypes;
@ -56,22 +58,13 @@ public class Biome extends SetCommand {
} catch (final Exception ignore) {
}
if (biome == null) {
Component separator = MINI_MESSAGE.deserialize(
TranslatableCaption.of("blocklist.block_list_separator").getComponent(player)
);
TextComponent.Builder biomes = Component.text();
final Collection<BiomeType> biomeTypes = BiomeType.REGISTRY.values();
int counter = 0;
for (final BiomeType biomeType : biomeTypes) {
if (counter++ > 0) {
biomes.append(separator);
}
biomes.append(Component.text(biomeType.toString()));
}
String separator = TranslatableCaption.of("blocklist.block_list_separator").getComponent(player);
player.sendMessage(TranslatableCaption.of("biome.need_biome"));
player.sendMessage(
TranslatableCaption.of("commandconfig.subcommand_set_options_header"),
TagResolver.resolver("values", Tag.inserting(biomes))
StaticCaption.of(
TranslatableCaption.of("commandconfig.subcommand_set_options_header_only").getComponent(player)
+ StringMan.join(BiomeType.REGISTRY.values(), separator)
)
);
return false;
}

View File

@ -39,7 +39,7 @@ import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.plot.PlotCluster;
import com.plotsquared.core.plot.PlotId;
import com.plotsquared.core.util.ComponentMan;
import com.plotsquared.core.util.ComponentHelper;
import com.plotsquared.core.util.Permissions;
import com.plotsquared.core.util.TabCompletions;
import com.plotsquared.core.util.query.PlotQuery;
@ -79,7 +79,7 @@ public class Cluster extends SubCommand {
// return arguments
player.sendMessage(
TranslatableCaption.of("cluster.cluster_available_args"),
TagResolver.resolver("list", Tag.inserting(ComponentMan.join(AVAILABLE_ARGS, SEPARATOR)))
TagResolver.resolver("list", Tag.inserting(ComponentHelper.join(AVAILABLE_ARGS, SEPARATOR)))
);
return false;
}
@ -917,7 +917,7 @@ public class Cluster extends SubCommand {
}
player.sendMessage(
TranslatableCaption.of("cluster.cluster_available_args"),
TagResolver.resolver("list", Tag.inserting(ComponentMan.join(AVAILABLE_ARGS, SEPARATOR)))
TagResolver.resolver("list", Tag.inserting(ComponentHelper.join(AVAILABLE_ARGS, SEPARATOR)))
);
return false;
}

View File

@ -29,6 +29,7 @@ import com.plotsquared.core.configuration.caption.Caption;
import com.plotsquared.core.configuration.caption.LocaleHolder;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.player.PlotPlayer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -100,7 +101,7 @@ public enum CommandCategory implements Caption {
}
@Override
public @NonNull ComponentLike toComponent(@NonNull final LocaleHolder localeHolder) {
public @NonNull Component toComponent(@NonNull final LocaleHolder localeHolder) {
return MiniMessage.miniMessage().deserialize(getComponent(localeHolder));
}

View File

@ -41,6 +41,8 @@ import com.plotsquared.core.uuid.UUIDMapping;
import com.sk89q.worldedit.world.entity.EntityType;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -125,10 +127,11 @@ public class Debug extends SubCommand {
player.sendMessage(TranslatableCaption.of("debug.entity_categories"));
EntityCategory.REGISTRY.forEach(category -> {
final StringBuilder builder =
new StringBuilder("§7- §6").append(category.getId()).append("§7: §6");
new StringBuilder("<gray>-</gray> <gold>").append(category.getId()).append("</gold><gray>: <gold>");
for (final EntityType entityType : category.getAll()) {
builder.append(entityType.getId()).append(" ");
}
builder.append("</gold>");
player.sendMessage(StaticCaption.of("<prefix>" + builder));
});
EntityType.REGISTRY.values().stream().sorted(Comparator.comparing(EntityType::getId))
@ -148,7 +151,8 @@ public class Debug extends SubCommand {
.getCaptionMap(TranslatableCaption.DEFAULT_NAMESPACE)
.getCaptions();
TextComponent.Builder information = Component.text();
Component header = MINI_MESSAGE.deserialize(TranslatableCaption.of("debug.debug_header").getComponent(player) + "\n");
Component header = TranslatableCaption.of("debug.debug_header").toComponent(player)
.append(Component.newline());
String line = TranslatableCaption.of("debug.debug_line").getComponent(player) + "\n";
String section = TranslatableCaption.of("debug.debug_section").getComponent(player) + "\n";
information.append(header);

View File

@ -54,6 +54,7 @@ import com.plotsquared.core.util.task.RunnableVal2;
import com.plotsquared.core.util.task.RunnableVal3;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -607,23 +608,22 @@ public final class FlagCommand extends Command {
return;
}
final Map<String, ArrayList<String>> flags = new HashMap<>();
final Map<Component, ArrayList<String>> flags = new HashMap<>();
for (PlotFlag<?, ?> plotFlag : GlobalFlagContainer.getInstance().getRecognizedPlotFlags()) {
if (plotFlag instanceof InternalFlag) {
continue;
}
final String category = MINI_MESSAGE.escapeTags(plotFlag.getFlagCategory().getComponent(player));
final Collection<String> flagList =
flags.computeIfAbsent(category, k -> new ArrayList<>());
final Component category = plotFlag.getFlagCategory().toComponent(player);
final Collection<String> flagList = flags.computeIfAbsent(category, k -> new ArrayList<>());
flagList.add(plotFlag.getName());
}
for (final Map.Entry<String, ArrayList<String>> entry : flags.entrySet()) {
for (final Map.Entry<Component, ArrayList<String>> entry : flags.entrySet()) {
Collections.sort(entry.getValue());
Component category =
MINI_MESSAGE.deserialize(
TranslatableCaption.of("flag.flag_list_categories").getComponent(player),
TagResolver.resolver("category", Tag.inserting(Component.text(entry.getKey())))
TagResolver.resolver("category", Tag.inserting(entry.getKey().style(Style.empty())))
);
TextComponent.Builder builder = Component.text().append(category);
final Iterator<String> flagIterator = entry.getValue().iterator();

View File

@ -271,7 +271,7 @@ public class Inbox extends SubCommand {
if (!comments.isEmpty()) {
player.sendMessage(
TranslatableCaption.of("comment.comment_removed_success"),
TagResolver.resolver("value", Tag.inserting(Component.text(String.valueOf(comments))))
TagResolver.resolver("value", Tag.inserting(Component.text("*")))
);
plot.getPlotCommentContainer().removeComments(comments);
}

View File

@ -25,6 +25,7 @@
*/
package com.plotsquared.core.configuration.caption;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -46,7 +47,8 @@ public interface Caption {
*
* @param localeHolder Locale holder
* @return ComponentLike
* @since TODO
*/
@NonNull ComponentLike toComponent(@NonNull LocaleHolder localeHolder);
@NonNull Component toComponent(@NonNull LocaleHolder localeHolder);
}

View File

@ -27,23 +27,49 @@ package com.plotsquared.core.configuration.caption;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
/**
* A holder for a caption.
* Useful when constructing messages in multiple steps with {@link TagResolver}s.
*/
public class CaptionHolder {
private Caption caption = StaticCaption.of("");
private TagResolver[] tagResolvers = new TagResolver[0];
/**
* Set the {@link Caption} to send.
*
* @param caption The new caption.
*/
public void set(Caption caption) {
this.caption = caption;
}
/**
* Get the {@link Caption} to send.
*
* @return The caption to send.
*/
public Caption get() {
return this.caption;
}
/**
* Get the {@link TagResolver}s to use when resolving tags in the {@link Caption}.
*
* @return The tag resolvers to use.
* @since TODO
*/
public TagResolver[] getTagResolvers() {
return this.tagResolvers;
}
/**
* Set the {@link TagResolver}s to use when resolving tags in the {@link Caption}.
*
* @param tagResolvers The tag resolvers to use.
* @since TODO
*/
public void setTagResolvers(TagResolver... tagResolvers) {
this.tagResolvers = tagResolvers;
}

View File

@ -26,6 +26,7 @@
package com.plotsquared.core.configuration.caption;
import com.google.common.base.Preconditions;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -54,7 +55,7 @@ public final class StaticCaption implements Caption {
}
@Override
public @NonNull ComponentLike toComponent(@NonNull final LocaleHolder localeHolder) {
public @NonNull Component toComponent(@NonNull final LocaleHolder localeHolder) {
return MiniMessage.miniMessage().deserialize(this.value);
}

View File

@ -27,8 +27,11 @@ package com.plotsquared.core.configuration.caption;
import com.google.common.base.Objects;
import com.plotsquared.core.PlotSquared;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.Locale;
@ -99,9 +102,15 @@ public final class TranslatableCaption implements NamespacedCaption {
}
@Override
public @NonNull ComponentLike toComponent(@NonNull final LocaleHolder localeHolder) {
public @NonNull Component toComponent(@NonNull final LocaleHolder localeHolder) {
if (getKey().equals("core.prefix")) {
return MiniMessage.miniMessage().deserialize(getComponent(localeHolder));
}
return MiniMessage.miniMessage().deserialize(getComponent(localeHolder), TagResolver.resolver(
"prefix",
Tag.inserting(TranslatableCaption.of("core.prefix").toComponent(localeHolder))
));
}
@Override
public @NonNull String getKey() {

View File

@ -65,6 +65,7 @@ import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.item.ItemType;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.title.Title;

View File

@ -91,6 +91,12 @@ public abstract class PlotFlag<T, F extends PlotFlag<T, F>> {
return flagName.toString();
}
/**
* Gets the flag name as a Kyori {@link Component}
*
* @see #getFlagName(Class)
* @since TODO
*/
public static <T, F extends PlotFlag<T, F>> Component getFlagNameComponent(Class<F> flagClass) {
return Component.text(getFlagName(flagClass));
}

View File

@ -31,16 +31,33 @@ import net.kyori.adventure.text.TextComponent;
import java.util.Collection;
public class ComponentMan {
/**
* A utility class for modifying components.
*
* @since TODO
*/
public class ComponentHelper {
/**
* Joins multiple {@link Component}s into one final {@link ComponentLike}
*
* @param components The components to join
* @param delimiter The delimiter to use between the components
* @return The joined components
* @since TODO
*/
public static ComponentLike join(Collection<? extends ComponentLike> components, Component delimiter) {
return join(components.toArray(ComponentLike[]::new), delimiter);
}
public static Component join(Component[] components, Component delimiter) {
return join(components, delimiter);
}
/**
* Joins multiple {@link ComponentLike}s into one final {@link ComponentLike}
*
* @param components The components to join
* @param delimiter The delimiter to use between the components
* @return The joined components
* @since TODO
*/
public static Component join(ComponentLike[] components, Component delimiter) {
TextComponent.Builder builder = Component.text();
for (int i = 0, j = components.length; i < j; i++) {

View File

@ -29,7 +29,7 @@ import com.plotsquared.core.command.CommandCategory;
import com.plotsquared.core.configuration.caption.StaticCaption;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.util.ComponentMan;
import com.plotsquared.core.util.ComponentHelper;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.Tag;
@ -63,7 +63,7 @@ public class HelpPage {
TranslatableCaption.of("help.help_page_header").getComponent(player),
pageHeaderResolver
)))
.tag("help_objects", Tag.inserting(ComponentMan.join(this.helpObjects, Component.text("\n"))))
.tag("help_objects", Tag.inserting(ComponentHelper.join(this.helpObjects, Component.text("\n"))))
.tag("footer", Tag.inserting(TranslatableCaption.of("help.help_footer").toComponent(player)))
.build();
player.sendMessage(

View File

@ -59,7 +59,7 @@
"worldedit.worldedit_bypass": "<prefix><gray><italic>To bypass your restrictions use </gray><dark_aqua><command></dark_aqua></italic>",
"worldedit.worldedit_bypassed": "<prefix><gray>Currently bypassing WorldEdit restriction.</gray>",
"gamemode.gamemode_was_bypassed": "<prefix><gold>You bypassed the gamemode (</gold><gray><gamemode></gray><gold>) <gold>set for </gold><gray><plot>.</gray>",
"height.height_limit": "<prefix><gold>This plot area has building height limits: Min height: </gold><gray><minHeight></gray><gold>, Max height: </gold><gray><maxHeight></gray>",
"height.height_limit": "<prefix><gold>This plot area has building height limits: Min height: </gold><gray><minheight></gray><gold>, Max height: </gold><gray><maxheight></gray>",
"notification.notify_enter": "<prefix><gray><player> entered your plot (</gray><gold><area>;<plot></gold><gray>).</gray>",
"notification.notify_leave": "<prefix><gray><player> left your plot (</gray><gold><area>;<plot></gold><gray>).</gray>",
"swap.swap_overlap": "<prefix><red>The proposed areas are not allowed to overlap.</red>",
@ -510,7 +510,7 @@
"flag.flag_info_name": "<gray>Name: <gold><flag></gold></gray>",
"flag.flag_info_category": "<gray>Category: </gray><gold><value></gold>",
"flag.flag_info_description": "<gray>Description: </gray>",
"flag.flag_info_example": "<gray>Example: <click:suggest_command:<command> <flag> <value>><gold><command> <flag> <value></click></gold></gray>",
"flag.flag_info_example": "<gray>Example: <click:suggest_command:'<command> <flag> <value>'><gold><command> <flag> <value></gold></click></gray>",
"flag.flag_info_default_value": "<gray>Default Value: <value></gray>",
"flags.flag_category_string": "<gray>String Flags</gray>",
"flags.flag_category_integers": "<gray>Integer Flags</gray>",

View File

@ -14,7 +14,7 @@ snakeyaml = "1.30" # Version set by Bukkit
# Adventure & MiniMessage
adventure-api = "4.10.1"
adventure-text-minimessage = "4.10.1"
adventure-platform-bukkit = "4.0.1"
adventure-platform-bukkit = "4.1.0"
# Plugins
worldedit = "7.2.10"