From db37077af73fa83cfaa9744e69d1034422221a51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Thu, 9 Jul 2020 12:58:28 +0200 Subject: [PATCH] Make signs able to use captions --- .../plotsquared/bukkit/util/BukkitUtil.java | 48 +++++++------------ .../configuration/caption/LocaleHolder.java | 11 +++++ .../com/plotsquared/core/util/WorldUtil.java | 12 ++++- 3 files changed, 39 insertions(+), 32 deletions(-) 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 eef64f0bd..5edf72bb0 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java @@ -29,12 +29,13 @@ import com.plotsquared.bukkit.BukkitMain; import com.plotsquared.bukkit.player.BukkitPlayer; import com.plotsquared.bukkit.player.BukkitPlayerManager; import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.configuration.Caption; import com.plotsquared.core.configuration.Captions; +import com.plotsquared.core.configuration.caption.LocaleHolder; import com.plotsquared.core.location.Location; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; import com.plotsquared.core.util.BlockUtil; -import com.plotsquared.core.util.MainUtil; import com.plotsquared.core.util.MathMan; import com.plotsquared.core.util.PlayerManager; import com.plotsquared.core.util.StringComparison; @@ -53,6 +54,9 @@ import com.sk89q.worldedit.world.block.BlockTypes; import io.papermc.lib.PaperLib; import lombok.NonNull; import net.kyori.adventure.platform.bukkit.BukkitAudiences; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.Template; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.Material; @@ -112,6 +116,8 @@ import java.util.stream.Stream; public class BukkitUtil extends WorldUtil { public static final BukkitAudiences BUKKIT_AUDIENCES = BukkitAudiences.create(BukkitMain.getPlugin(BukkitMain.class)); + public static final LegacyComponentSerializer LEGACY_COMPONENT_SERIALIZER = LegacyComponentSerializer.legacy(); + public static final MiniMessage MINI_MESSAGE = MiniMessage.builder().build(); private static String lastString = null; private static World lastWorld = null; @@ -235,17 +241,6 @@ public class BukkitUtil extends WorldUtil { return BukkitPlayer.wrap(player).getPlots(world); } - /** - * Send a message to a player. The message supports color codes. - * - * @param player the recipient of the message - * @param string the message - * @see MainUtil#sendMessage(PlotPlayer, String) - */ - public static void sendMessage(Player player, String string) { - MainUtil.sendMessage(BukkitUtil.getPlayer(player), string); - } - /** * Gets the player plot count. * @@ -260,16 +255,6 @@ public class BukkitUtil extends WorldUtil { return BukkitUtil.getPlayer(player).getPlotCount(world); } - /** - * Send a message to a player. - * - * @param player the recipient of the message - * @param caption the message - */ - public static void sendMessage(Player player, Captions caption) { - MainUtil.sendMessage(BukkitUtil.getPlayer(player), caption); - } - public static BukkitPlayer getPlayer(@NonNull final Player player) { if (player == lastPlayer) { return lastPlotPlayer; @@ -445,20 +430,20 @@ public class BukkitUtil extends WorldUtil { } @Override @SuppressWarnings("deprecation") - public void setSign(@NonNull final String worldName, final int x, final int y, final int z, - @NonNull final String[] lines) { - ensureLoaded(worldName, x, z, chunk -> { - final World world = getWorld(worldName); - final Block block = world.getBlockAt(x, y, z); + public void setSign(@NotNull final Location location, @NotNull final Caption[] lines, + @NotNull final Template ... replacements) { + ensureLoaded(location.getWorld(), location.getX(), location.getZ(), chunk -> { + final World world = getWorld(location.getWorld()); + final Block block = world.getBlockAt(location.getX(), location.getY(), location.getZ()); // block.setType(Material.AIR); final Material type = block.getType(); if (type != Material.LEGACY_SIGN && type != Material.LEGACY_WALL_SIGN) { BlockFace facing = BlockFace.EAST; - if (world.getBlockAt(x, y, z + 1).getType().isSolid()) { + if (world.getBlockAt(location.getX(), location.getY(), location.getZ() + 1).getType().isSolid()) { facing = BlockFace.NORTH; - } else if (world.getBlockAt(x + 1, y, z).getType().isSolid()) { + } else if (world.getBlockAt(location.getX() + 1, location.getY(), location.getZ()).getType().isSolid()) { facing = BlockFace.WEST; - } else if (world.getBlockAt(x, y, z - 1).getType().isSolid()) { + } else if (world.getBlockAt(location.getX(), location.getY(), location.getZ() - 1).getType().isSolid()) { facing = BlockFace.SOUTH; } if (PlotSquared.get().IMP.getServerVersion()[1] == 13) { @@ -478,7 +463,8 @@ public class BukkitUtil extends WorldUtil { if (blockstate instanceof Sign) { final Sign sign = (Sign) blockstate; for (int i = 0; i < lines.length; i++) { - sign.setLine(i, lines[i]); + sign.setLine(i, LEGACY_COMPONENT_SERIALIZER + .serialize(MINI_MESSAGE.parse(lines[i].getComponent(LocaleHolder.console())))); } sign.update(true); } diff --git a/Core/src/main/java/com/plotsquared/core/configuration/caption/LocaleHolder.java b/Core/src/main/java/com/plotsquared/core/configuration/caption/LocaleHolder.java index 29748531e..e539a95e4 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/caption/LocaleHolder.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/caption/LocaleHolder.java @@ -25,6 +25,7 @@ */ package com.plotsquared.core.configuration.caption; +import com.plotsquared.core.player.ConsolePlayer; import org.jetbrains.annotations.NotNull; import java.util.Locale; @@ -34,6 +35,16 @@ import java.util.Locale; */ public interface LocaleHolder { + /** + * Get the console locale holder + * + * @return Console locale holder + * @see ConsolePlayer#getConsole() for direct access to the {@link ConsolePlayer} + */ + @NotNull static LocaleHolder console() { + return ConsolePlayer.getConsole(); + } + /** * Get the locale used by the holder * diff --git a/Core/src/main/java/com/plotsquared/core/util/WorldUtil.java b/Core/src/main/java/com/plotsquared/core/util/WorldUtil.java index 4d733d5eb..fca482394 100644 --- a/Core/src/main/java/com/plotsquared/core/util/WorldUtil.java +++ b/Core/src/main/java/com/plotsquared/core/util/WorldUtil.java @@ -26,6 +26,7 @@ package com.plotsquared.core.util; import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.configuration.Caption; import com.plotsquared.core.location.Location; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; @@ -41,6 +42,7 @@ import com.sk89q.worldedit.world.biome.BiomeType; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.entity.EntityType; +import net.kyori.adventure.text.minimessage.Template; import org.jetbrains.annotations.NotNull; import java.io.ByteArrayOutputStream; @@ -107,7 +109,15 @@ public abstract class WorldUtil { */ @Deprecated public abstract int getHighestBlockSynchronous(String world, int x, int z); - public abstract void setSign(String world, int x, int y, int z, String[] lines); + /** + * Set the block at the specified location to a sign, with given text + * + * @param location Block location + * @param lines Sign text + * @param replacements Text replacements + */ + public abstract void setSign(@NotNull Location location, @NotNull Caption[] lines, + @NotNull Template ... replacements); public abstract void setBiomes(String world, CuboidRegion region, BiomeType biome);