From 74a5c482148a1a0fad36c915617742aa90e3bd0c Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Sat, 8 Aug 2020 12:16:48 +0100 Subject: [PATCH] Finish up core and bukkit json --- .../plotsquared/bukkit/BukkitPlatform.java | 12 ---- .../bukkit/listener/PlayerEvents.java | 60 +++++++++++-------- .../bukkit/schematic/StateWrapper.java | 5 +- .../com/plotsquared/core/PlotPlatform.java | 7 --- .../plotsquared/core/command/FlagCommand.java | 2 +- .../com/plotsquared/core/command/Help.java | 3 +- .../core/util/helpmenu/HelpMenu.java | 4 +- .../core/util/helpmenu/HelpObject.java | 19 +++--- 8 files changed, 53 insertions(+), 59 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java index 0c74939a3..d05fb7bf7 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java @@ -685,18 +685,6 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass; Bukkit.getScheduler().cancelTasks(this); } - @Override public void log(@Nonnull String message) { - try { - message = Captions.color(message); - if (!Settings.Chat.CONSOLE_COLOR) { - message = ChatColor.stripColor(message); - } - this.getServer().getConsoleSender().sendMessage(message); - } catch (final Throwable ignored) { - System.out.println(ConsoleColors.fromString(message)); - } - } - @Override public void shutdown() { this.getServer().getPluginManager().disablePlugin(this); } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java index d3ab888a0..bfa29c4cb 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PlayerEvents.java @@ -32,14 +32,14 @@ import com.plotsquared.bukkit.player.BukkitPlayer; import com.plotsquared.bukkit.util.BukkitUtil; import com.plotsquared.bukkit.util.UpdateUtility; import com.plotsquared.core.PlotSquared; -import com.plotsquared.core.configuration.caption.Caption; -import com.plotsquared.core.permissions.Permission; import com.plotsquared.core.configuration.Settings; +import com.plotsquared.core.configuration.caption.Caption; import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.database.DBFunc; import com.plotsquared.core.listener.PlayerBlockEventType; import com.plotsquared.core.listener.PlotListener; import com.plotsquared.core.location.Location; +import com.plotsquared.core.permissions.Permission; import com.plotsquared.core.player.MetaDataAccess; import com.plotsquared.core.player.PlayerMetaDataKeys; import com.plotsquared.core.player.PlotPlayer; @@ -118,6 +118,7 @@ import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.world.block.BlockType; import io.papermc.lib.PaperLib; +import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.minimessage.Template; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -707,17 +708,16 @@ import java.util.regex.Pattern; this.eventDispatcher.doJoinTask(pp); }, TaskTime.seconds(1L)); - if (pp.hasPermission(Permission.PERMISSION_ADMIN_UPDATE_NOTIFICATION.toString()) - && Settings.Enabled_Components.UPDATE_NOTIFICATIONS - && PremiumVerification.isPremium() - && UpdateUtility.hasUpdate) { - Caption upperBoundary = TranslatableCaption.of("update.update_boundary"); + if (pp.hasPermission(Permission.PERMISSION_ADMIN_UPDATE_NOTIFICATION.toString()) && Settings.Enabled_Components.UPDATE_NOTIFICATIONS + && PremiumVerification.isPremium() && UpdateUtility.hasUpdate) { + Caption boundary = TranslatableCaption.of("update.update_boundary"); Caption updateNotification = TranslatableCaption.of("update.update_notification"); Template internalVersion = Template.of("p2version", String.valueOf(UpdateUtility.internalVersion.versionString())); Template spigotVersion = Template.of("spigotversion", UpdateUtility.spigotVersion); Template downloadUrl = Template.of("downloadurl", "https://www.spigotmc.org/resources/77506/updates"); - Caption lowerBoundary = TranslatableCaption.of("update.update_boundary"); - pp.sendMessage(upperBoundary, updateNotification, internalVersion, spigotVersion, downloadUrl, lowerBoundary); + pp.sendMessage(boundary); + pp.sendMessage(updateNotification, internalVersion, spigotVersion, downloadUrl); + pp.sendMessage(boundary); } } @@ -1024,40 +1024,48 @@ import java.util.regex.Pattern; return; } event.setCancelled(true); - Caption msg = TranslatableCaption.of("chat.plot_chat_format"); - Template msgTemplate = Template.of("msg", event.getMessage()); - Template plotTemplate = Template.of("plot_id", String.valueOf(plot.getId())); - Template senderTemplate = Template.of("sender", event.getPlayer().getDisplayName()); - plotPlayer.sendMessage(msg, msgTemplate, plotTemplate, senderTemplate); Set recipients = event.getRecipients(); recipients.clear(); - Set spies = new HashSet<>(); + Set> spies = new HashSet<>(); + Set> plotRecipients = new HashSet<>(); for (final PlotPlayer pp : PlotSquared.platform().getPlayerManager().getPlayers()) { if (pp.getAttribute("chatspy")) { - spies.add(((BukkitPlayer) pp).player); + spies.add(pp); } else { Plot current = pp.getCurrentPlot(); if (current != null && current.getBasePlot(false).equals(plot)) { - recipients.add(((BukkitPlayer) pp).player); + plotRecipients.add(pp); } } } - String partial = ChatColor.translateAlternateColorCodes('&', - format.replace("%plot_id%", id.getX() + ";" + id.getY()).replace("%sender%", sender)); + String message = event.getMessage(); + String sender = event.getPlayer().getDisplayName(); + PlotId id = plot.getId(); + Caption msg = TranslatableCaption.of("chat.plot_chat_format"); + Template msgTemplate; + Template plotTemplate = Template.of("plot_id", id.toString()); + Template senderTemplate = Template.of("sender", sender); + // If we do/don't want colour, we need to be careful about how to go about it, as players could attempt either 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")) { - message = Captions.color(message); + msgTemplate = Template + .of("msg", BukkitUtil.LEGACY_COMPONENT_SERIALIZER.deserialize(ChatColor.translateAlternateColorCodes('&', message))); + } else { + msgTemplate = Template.of("msg", BukkitUtil.MINI_MESSAGE.deserialize( + ChatColor.stripColor(BukkitUtil.LEGACY_COMPONENT_SERIALIZER.serialize(TextComponent.builder(message).build())))); } - String full = partial.replace("%msg%", message); - for (Player receiver : recipients) { - receiver.sendMessage(full); + for (PlotPlayer receiver : plotRecipients) { + receiver.sendMessage(msg, msgTemplate, plotTemplate, senderTemplate); } if (!spies.isEmpty()) { Caption spymsg = TranslatableCaption.of("chat.plot_chat_spy_format"); Template plotidTemplate = Template.of("plot_id", id.getX() + ";" + id.getY()); Template spysenderTemplate = Template.of("sender", sender); - Template spymessageTemplate = Template.of("msg", message); - for (Player player : spies) { - plotPlayer.sendMessage(spymsg, plotidTemplate, spysenderTemplate, spymessageTemplate); + Template spymessageTemplate = Template.of("msg", TextComponent.builder(message).build()); + for (PlotPlayer player : spies) { + player.sendMessage(spymsg, plotidTemplate, spysenderTemplate, spymessageTemplate); } } // TODO: Re-implement diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/schematic/StateWrapper.java b/Bukkit/src/main/java/com/plotsquared/bukkit/schematic/StateWrapper.java index 123e61310..93b9a26b2 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/schematic/StateWrapper.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/schematic/StateWrapper.java @@ -35,6 +35,7 @@ import com.sk89q.jnbt.Tag; import com.sk89q.worldedit.blocks.BaseItemStack; import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.world.item.ItemType; +import org.bukkit.ChatColor; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.Container; @@ -161,9 +162,7 @@ public class StateWrapper { .replace("\"underlined\":true,", "&n").replace("\"italic\":true,", "&o") .replace("[{\"text\":\"", "&0").replace("{\"text\":\"", "&0").replace("\"},", "") .replace("\"}]", "").replace("\"}", ""); - for (Entry entry : Captions.replacements.entrySet()) { - str = str.replace(entry.getKey(), entry.getValue()); - } + str = ChatColor.translateAlternateColorCodes('&', str); return str; } diff --git a/Core/src/main/java/com/plotsquared/core/PlotPlatform.java b/Core/src/main/java/com/plotsquared/core/PlotPlatform.java index 4e0f41170..fbe2d34ff 100644 --- a/Core/src/main/java/com/plotsquared/core/PlotPlatform.java +++ b/Core/src/main/java/com/plotsquared/core/PlotPlatform.java @@ -60,13 +60,6 @@ import java.util.Map; */ public interface PlotPlatform

extends LocaleHolder { - /** - * Logs a message to console. - * - * @param message the message to log - */ - void log(String message); - /** * Gets the directory which contains PlotSquared files. The directory may not exist. * diff --git a/Core/src/main/java/com/plotsquared/core/command/FlagCommand.java b/Core/src/main/java/com/plotsquared/core/command/FlagCommand.java index cfd8bc634..97ea59856 100644 --- a/Core/src/main/java/com/plotsquared/core/command/FlagCommand.java +++ b/Core/src/main/java/com/plotsquared/core/command/FlagCommand.java @@ -217,7 +217,7 @@ public final class FlagCommand extends Command { .contains(args[0].toLowerCase(Locale.ENGLISH))) { new HelpMenu(player).setCategory(CommandCategory.SETTINGS) .setCommands(this.getCommands()).generateMaxPages() - .generatePage(0, getParent().toString()).render(); + .generatePage(0, getParent().toString(), player).render(); return CompletableFuture.completedFuture(true); } return super.execute(player, args, confirm, whenDone); diff --git a/Core/src/main/java/com/plotsquared/core/command/Help.java b/Core/src/main/java/com/plotsquared/core/command/Help.java index 8c654ad60..f417e51d6 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Help.java +++ b/Core/src/main/java/com/plotsquared/core/command/Help.java @@ -120,7 +120,8 @@ public class Help extends Command { player.sendMessage(StaticCaption.of(MINI_MESSAGE.serialize(builder.asComponent()))); return true; } - new HelpMenu(player).setCategory(catEnum).getCommands().generateMaxPages().generatePage(page - 1, getParent().toString()).render(); + new HelpMenu(player).setCategory(catEnum).getCommands().generateMaxPages().generatePage(page - 1, getParent().toString(), player) + .render(); return true; }); } diff --git a/Core/src/main/java/com/plotsquared/core/util/helpmenu/HelpMenu.java b/Core/src/main/java/com/plotsquared/core/util/helpmenu/HelpMenu.java index 11defc7f0..567fb0ab7 100644 --- a/Core/src/main/java/com/plotsquared/core/util/helpmenu/HelpMenu.java +++ b/Core/src/main/java/com/plotsquared/core/util/helpmenu/HelpMenu.java @@ -67,7 +67,7 @@ public class HelpMenu { return this; } - public HelpMenu generatePage(int currentPage, String label) { + public HelpMenu generatePage(int currentPage, String label, PlotPlayer audience) { if (currentPage > this.maxPage) { currentPage = this.maxPage; } @@ -77,7 +77,7 @@ public class HelpMenu { this.page = new HelpPage(this.commandCategory, currentPage, this.maxPage); int max = Math.min((currentPage * PER_PAGE) + (PER_PAGE - 1), this.commands.size()); for (int i = currentPage * PER_PAGE; i < max; i++) { - this.page.addHelpItem(new HelpObject(this.commands.get(i), label)); + this.page.addHelpItem(new HelpObject(this.commands.get(i), label, audience)); } return this; } diff --git a/Core/src/main/java/com/plotsquared/core/util/helpmenu/HelpObject.java b/Core/src/main/java/com/plotsquared/core/util/helpmenu/HelpObject.java index 19913c4ae..b5d7dd73d 100644 --- a/Core/src/main/java/com/plotsquared/core/util/helpmenu/HelpObject.java +++ b/Core/src/main/java/com/plotsquared/core/util/helpmenu/HelpObject.java @@ -27,19 +27,24 @@ package com.plotsquared.core.util.helpmenu; import com.plotsquared.core.command.Argument; import com.plotsquared.core.command.Command; +import com.plotsquared.core.configuration.caption.TranslatableCaption; +import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.util.StringMan; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.Template; public class HelpObject { + static final MiniMessage MINI_MESSAGE = MiniMessage.builder().build(); + private final String _rendered; - public HelpObject(final Command command, final String label) { - _rendered = StringMan.replaceAll(Captions.HELP_ITEM.getTranslated(), "%usage%", - command.getUsage().replaceAll("\\{label\\}", label), "[%alias%]", - !command.getAliases().isEmpty() ? - "(" + StringMan.join(command.getAliases(), "|") + ")" : - "", "%desc%", command.getDescription(), "%arguments%", - buildArgumentList(command.getRequiredArguments()), "{label}", label); + public HelpObject(final Command command, final String label, final PlotPlayer audience) { + _rendered = MINI_MESSAGE.serialize(MINI_MESSAGE.parse(TranslatableCaption.of("help.help_item").getComponent(audience), + Template.of("usage", command.getUsage().replaceAll("\\{label\\}", label)), + Template.of("alias", command.getAliases().isEmpty() ? StringMan.join(command.getAliases(), "|") : ""), + Template.of("desc", command.getDescription()), Template.of("arguments", buildArgumentList(command.getRequiredArguments())), + Template.of("label", label))); } @Override public String toString() {