diff --git a/src/main/java/xyz/nkomarn/harbor/listener/BedListener.java b/src/main/java/xyz/nkomarn/harbor/listener/BedListener.java index 9c58e42..487f9a9 100644 --- a/src/main/java/xyz/nkomarn/harbor/listener/BedListener.java +++ b/src/main/java/xyz/nkomarn/harbor/listener/BedListener.java @@ -1,6 +1,5 @@ package xyz.nkomarn.harbor.listener; -import me.clip.placeholderapi.PlaceholderAPI; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -9,6 +8,7 @@ import org.bukkit.event.player.PlayerBedEnterEvent; import org.bukkit.event.player.PlayerBedLeaveEvent; import org.jetbrains.annotations.NotNull; import xyz.nkomarn.harbor.Harbor; +import xyz.nkomarn.harbor.util.Messages; import xyz.nkomarn.harbor.util.PlayerManager; import java.util.concurrent.TimeUnit; @@ -16,21 +16,15 @@ import java.util.concurrent.TimeUnit; public class BedListener implements Listener { private final Harbor harbor; + private final Messages messages; private final PlayerManager playerManager; - private final boolean papiPresent = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null; public BedListener(@NotNull Harbor harbor) { this.harbor = harbor; + this.messages = harbor.getMessages(); this.playerManager = harbor.getPlayerManager(); } - public String formatForChat(Player sender, String message) { - String output = message.replace("[player]", sender.getName()) - .replace("[displayname]", sender.getDisplayName()); - if(papiPresent) output = PlaceholderAPI.setPlaceholders(sender, output); - return output; - } - @EventHandler(ignoreCancelled = true) public void onBedEnter(PlayerBedEnterEvent event) { if (event.getBedEnterResult() != PlayerBedEnterEvent.BedEnterResult.OK) { @@ -44,8 +38,9 @@ public class BedListener implements Listener { Bukkit.getScheduler().runTaskLater(harbor, () -> { playerManager.setCooldown(player, System.currentTimeMillis()); - harbor.getMessages().sendWorldChatMessage(event.getBed().getWorld(), - formatForChat(player, harbor.getConfiguration().getString("messages.chat.player-sleeping"))); + harbor.getMessages().sendWorldChatMessage(event.getBed().getWorld(), messages.prepareMessage( + player, harbor.getConfiguration().getString("messages.chat.player-sleeping")) + ); }, 1); } @@ -57,8 +52,9 @@ public class BedListener implements Listener { Bukkit.getScheduler().runTaskLater(harbor, () -> { playerManager.setCooldown(event.getPlayer(), System.currentTimeMillis()); - harbor.getMessages().sendWorldChatMessage(event.getBed().getWorld(), formatForChat(event.getPlayer(), - harbor.getConfiguration().getString("messages.chat.player-left-bed"))); + harbor.getMessages().sendWorldChatMessage(event.getBed().getWorld(), messages.prepareMessage( + event.getPlayer(), harbor.getConfiguration().getString("messages.chat.player-left-bed")) + ); }, 1); } diff --git a/src/main/java/xyz/nkomarn/harbor/util/Messages.java b/src/main/java/xyz/nkomarn/harbor/util/Messages.java index f712bd2..75fb7f9 100644 --- a/src/main/java/xyz/nkomarn/harbor/util/Messages.java +++ b/src/main/java/xyz/nkomarn/harbor/util/Messages.java @@ -1,6 +1,7 @@ package xyz.nkomarn.harbor.util; import com.google.common.base.Enums; +import me.clip.placeholderapi.PlaceholderAPI; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.TextComponent; @@ -14,7 +15,6 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerChangedWorldEvent; -import org.bukkit.event.server.PluginDisableEvent; import org.bukkit.event.world.WorldLoadEvent; import org.jetbrains.annotations.NotNull; import xyz.nkomarn.harbor.Harbor; @@ -28,12 +28,14 @@ public class Messages implements Listener { private final Config config; private final Random random; private final HashMap bossBars; + private final boolean papiPresent; public Messages(@NotNull Harbor harbor) { this.harbor = harbor; this.config = harbor.getConfiguration(); this.random = new Random(); this.bossBars = new HashMap<>(); + this.papiPresent = harbor.getServer().getPluginManager().isPluginEnabled("PlaceholderAPI"); for (World world : Bukkit.getWorlds()) { if (harbor.getChecker().isBlacklisted(world)) { @@ -132,7 +134,7 @@ public class Messages implements Listener { * @return The provided message with placeholders replaced with correct values for the world context. */ @NotNull - private String prepareMessage(final World world, final String message) { + public String prepareMessage(@NotNull World world, @NotNull String message) { Checker checker = harbor.getChecker(); return ChatColor.translateAlternateColorCodes('&', message .replace("[sleeping]", String.valueOf(checker.getSleepingPlayers(world).size())) @@ -141,6 +143,19 @@ public class Messages implements Listener { .replace("[more]", String.valueOf(checker.getNeeded(world)))); } + @NotNull + public String prepareMessage(@NotNull Player player, @NotNull String message) { + String output = ChatColor.translateAlternateColorCodes('&', message + .replace("[player]", player.getName() + .replace("[displayname]", player.getDisplayName()))); + + if (papiPresent) { + output = PlaceholderAPI.setPlaceholders(player, output); + } + + return output; + } + /** * Creates a new bossbar for the given world if one isn't already present. *