From 078b776f60dbd605c4bd46533beb86850c2f0d0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sun, 5 Jul 2020 18:14:29 +0200 Subject: [PATCH] Implement console messages --- .../com/plotsquared/bukkit/BukkitMain.java | 15 ++++-------- .../bukkit/player/BukkitPlayer.java | 7 ++---- .../plotsquared/bukkit/util/BukkitUtil.java | 3 +++ .../java/com/plotsquared/core/IPlotMain.java | 15 ++++++------ .../core/player/ConsolePlayer.java | 23 ++++++++++++++++--- 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index d6c38bec4..a21348302 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -41,7 +41,6 @@ import com.plotsquared.bukkit.placeholder.Placeholders; import com.plotsquared.bukkit.player.BukkitPlayerManager; import com.plotsquared.bukkit.queue.BukkitLocalQueue; import com.plotsquared.bukkit.schematic.BukkitSchematicHandler; -import com.plotsquared.bukkit.util.BukkitChatManager; import com.plotsquared.bukkit.util.BukkitChunkManager; import com.plotsquared.bukkit.util.BukkitEconHandler; import com.plotsquared.bukkit.util.BukkitInventoryUtil; @@ -81,7 +80,6 @@ import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.plot.PlotAreaTerrainType; import com.plotsquared.core.plot.PlotAreaType; import com.plotsquared.core.plot.PlotId; -import com.plotsquared.core.plot.message.PlainChatManager; import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.plot.world.SinglePlotArea; import com.plotsquared.core.plot.world.SinglePlotAreaManager; @@ -112,6 +110,7 @@ import com.sk89q.worldedit.extension.platform.Actor; import io.papermc.lib.PaperLib; import lombok.Getter; import lombok.NonNull; +import net.kyori.adventure.audience.Audience; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -1101,14 +1100,6 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain< return name.substring(name.lastIndexOf('.') + 1); } - @Override public ChatManager initChatManager() { - if (Settings.Chat.INTERACTIVE) { - return new BukkitChatManager(); - } else { - return new PlainChatManager(); - } - } - @Override public GeneratorWrapper wrapPlotGenerator(@Nullable final String world, @NonNull final IndependentPlotGenerator generator) { return new BukkitPlotGenerator(world, generator); @@ -1136,4 +1127,8 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain< return this.playerManager; } + @Override @NotNull public Audience getConsoleAudience() { + return BukkitUtil.BUKKIT_AUDIENCES.audience(Bukkit.getConsoleSender()); + } + } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java index f8e100226..a5f3129f4 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java @@ -27,7 +27,6 @@ package com.plotsquared.bukkit.player; import com.google.common.base.Charsets; import com.google.common.base.Objects; -import com.plotsquared.bukkit.BukkitMain; import com.plotsquared.bukkit.util.BukkitUtil; import com.plotsquared.core.PlotSquared; import com.plotsquared.core.configuration.Caption; @@ -46,7 +45,6 @@ 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; @@ -78,7 +76,6 @@ import static com.sk89q.worldedit.world.gamemode.GameModes.SURVIVAL; public class BukkitPlayer extends PlotPlayer { private static final MiniMessage MINI_MESSAGE = MiniMessage.builder().build(); - private static final BukkitAudiences BUKKIT_AUDIENCES = BukkitAudiences.create(BukkitMain.getPlugin(BukkitMain.class)); private static boolean CHECK_EFFECTIVE = true; public final Player player; @@ -240,7 +237,7 @@ public class BukkitPlayer extends PlotPlayer { 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); + final Audience audience = BukkitUtil.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))); } @@ -259,7 +256,7 @@ public class BukkitPlayer extends PlotPlayer { if (!Objects.equal(component, this.getMeta("lastMessage")) || System.currentTimeMillis() - this.getMeta("lastMessageTime") > 5000) { setMeta("lastMessage", component); setMeta("lastMessageTime", System.currentTimeMillis()); - BUKKIT_AUDIENCES.player(player).sendMessage(component); + BukkitUtil.BUKKIT_AUDIENCES.player(player).sendMessage(component); } } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java index 5acdde298..eef64f0bd 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java @@ -52,6 +52,7 @@ import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockTypes; import io.papermc.lib.PaperLib; import lombok.NonNull; +import net.kyori.adventure.platform.bukkit.BukkitAudiences; import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.Material; @@ -110,6 +111,8 @@ import java.util.stream.Stream; @SuppressWarnings({"unused", "WeakerAccess"}) public class BukkitUtil extends WorldUtil { + public static final BukkitAudiences BUKKIT_AUDIENCES = BukkitAudiences.create(BukkitMain.getPlugin(BukkitMain.class)); + private static String lastString = null; private static World lastWorld = null; diff --git a/Core/src/main/java/com/plotsquared/core/IPlotMain.java b/Core/src/main/java/com/plotsquared/core/IPlotMain.java index 921a0adcd..568a2745b 100644 --- a/Core/src/main/java/com/plotsquared/core/IPlotMain.java +++ b/Core/src/main/java/com/plotsquared/core/IPlotMain.java @@ -43,6 +43,7 @@ import com.plotsquared.core.util.WorldUtil; import com.plotsquared.core.util.logger.ILogger; import com.plotsquared.core.util.task.TaskManager; import com.sk89q.worldedit.extension.platform.Actor; +import net.kyori.adventure.audience.Audience; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -133,13 +134,6 @@ public interface IPlotMain

extends ILogger { */ SchematicHandler initSchematicHandler(); - /** - * Starts the {@link ChatManager}. - * - * @return the ChatManager - */ - ChatManager initChatManager(); - /** * The task manager will run and manage Minecraft tasks. * @@ -293,4 +287,11 @@ public interface IPlotMain

extends ILogger { */ @NotNull PlayerManager, ? extends P> getPlayerManager(); + /** + * Get the platform specific console {@link Audience} + * + * @return Console audience + */ + @NotNull Audience getConsoleAudience(); + } diff --git a/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java b/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java index 7e265541a..d918b3c88 100644 --- a/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java +++ b/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java @@ -27,6 +27,8 @@ package com.plotsquared.core.player; import com.plotsquared.core.PlotSquared; import com.plotsquared.core.command.RequiredType; +import com.plotsquared.core.configuration.Caption; +import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.database.DBFunc; import com.plotsquared.core.events.TeleportCause; import com.plotsquared.core.location.Location; @@ -37,12 +39,17 @@ import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.world.gamemode.GameMode; import com.sk89q.worldedit.world.gamemode.GameModes; import com.sk89q.worldedit.world.item.ItemType; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.Template; import org.jetbrains.annotations.NotNull; +import java.util.Arrays; +import java.util.List; import java.util.UUID; public class ConsolePlayer extends PlotPlayer { + private static final MiniMessage MINI_MESSAGE = MiniMessage.builder().build(); private static ConsolePlayer instance; private ConsolePlayer() { @@ -80,7 +87,8 @@ public class ConsolePlayer extends PlotPlayer { } @Override - public void sendTitle(String title, String subtitle, int fadeIn, int stay, int fadeOut) { + public void sendTitle(@NotNull final Caption title, @NotNull final Caption subtitle, + final int fadeIn, final int stay, final int fadeOut, @NotNull final Template... replacements) { } @NotNull @Override public Location getLocation() { @@ -107,8 +115,17 @@ public class ConsolePlayer extends PlotPlayer { return true; } - @Override public void sendMessage(String message) { - PlotSquared.log(message); + @Override public void sendMessage(@NotNull final Caption caption, @NotNull final Template... replacements) { + final String message = caption.getComponent(this); + if (message.isEmpty()) { + return; + } + // Create the template list, and add the prefix as a replacement + final List