diff --git a/src/main/java/net/tfminecraft/rpcharacters/pvp/ArmourDonning.java b/src/main/java/net/tfminecraft/rpcharacters/pvp/ArmourDonning.java index af86a08..e71c201 100644 --- a/src/main/java/net/tfminecraft/rpcharacters/pvp/ArmourDonning.java +++ b/src/main/java/net/tfminecraft/rpcharacters/pvp/ArmourDonning.java @@ -33,10 +33,6 @@ import org.bukkit.potion.PotionEffectType; import org.bukkit.scheduler.BukkitTask; -import io.lumine.mythic.lib.api.player.MMOPlayerData; -import io.lumine.mythic.lib.message.actionbar.ActionBarPriority; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.tfminecraft.tlibs.armour.ArmorEquipEvent; import net.tfminecraft.tlibs.armour.ArmorType; @@ -61,7 +57,6 @@ public final class ArmourDonning implements Listener { private static final int CURSOR = -1; private static final int NOT_FOUND = -2; /** Ticks a progress line holds MMOCore's bar off; the ticker refreshes it well before. */ - private static final long ACTION_BAR_TICKS = 40L; private static final Map donning = new HashMap<>(); private static final Set completing = new HashSet<>(); @@ -261,7 +256,7 @@ static void tick(long nowMs) { private static void finish(Player player, Donning state, long nowMs) { Piece piece = state.current; if (equip(player, piece)) { - actionBar(player, PvpLoader.getDonDone().replace("{piece}", pieceName(piece.type()))); + PvpActionBar.show(player, PvpLoader.getDonDone().replace("{piece}", pieceName(piece.type()))); player.playSound(player.getLocation(), Sound.ITEM_ARMOR_EQUIP_GENERIC, 1f, 1f); } next(player, state, nowMs); @@ -338,7 +333,7 @@ static void stop(Player player, boolean tell) { return; } clearSlowness(player); - clearActionBar(player); + PvpActionBar.clear(player); if (tell) { RPTexts.send(player, PvpLoader.getDonInterrupted().replace("{piece}", pieceName(state.current.type()))); } @@ -359,33 +354,13 @@ private static void showProgress(Player player, Donning state, long nowMs) { String bar = "&a" + "|".repeat(filled) + "&8" + "|".repeat(BAR_LENGTH - filled); String queued = state.queue.isEmpty() ? "" : PvpLoader.getDonQueued().replace("{count}", String.valueOf(state.queue.size())); - actionBar(player, PvpLoader.getDonProgress() + PvpActionBar.show(player, PvpLoader.getDonProgress() .replace("{piece}", pieceName(state.current.type())) .replace("{bar}", bar) .replace("{seconds}", String.valueOf((left + 999L) / 1000L)) .replace("{queued}", queued)); } - /** MythicLib redraws MMOCore's bar every tick, so go through its queue when it tracks the player. */ - private static void actionBar(Player player, String raw) { - String text = RPTexts.formatDisplay(raw); - MMOPlayerData data = MMOPlayerData.getOrNull(player.getUniqueId()); - if (data != null) { - data.getActionBar().show(ActionBarPriority.HIGH, ACTION_BAR_TICKS, text); - } else { - player.sendActionBar(LegacyComponentSerializer.legacySection().deserialize(text)); - } - } - - private static void clearActionBar(Player player) { - MMOPlayerData data = MMOPlayerData.getOrNull(player.getUniqueId()); - if (data != null) { - data.getActionBar().reset(ActionBarPriority.HIGH); - } else { - player.sendActionBar(Component.empty()); - } - } - /** Armour doesn't stack, so it goes in the first free storage slot or on the ground. */ private static void giveBack(Player player, ItemStack item) { PlayerInventory inventory = player.getInventory(); diff --git a/src/main/java/net/tfminecraft/rpcharacters/pvp/KnockoutRestrictions.java b/src/main/java/net/tfminecraft/rpcharacters/pvp/KnockoutRestrictions.java index ac43d72..482baa4 100644 --- a/src/main/java/net/tfminecraft/rpcharacters/pvp/KnockoutRestrictions.java +++ b/src/main/java/net/tfminecraft/rpcharacters/pvp/KnockoutRestrictions.java @@ -26,12 +26,12 @@ import io.lumine.mythic.lib.api.event.skill.PlayerCastSkillEvent; import net.tfminecraft.rpcharacters.loaders.PvpLoader; -import net.tfminecraft.rpcharacters.utils.RPTexts; /** * A knocked out player can talk and roleplay, but can't fight back: no hits, shots, skills * or spells, and no eating, drinking, item use, dropping items or building. Everything runs - * first so other plugins never act on the attempt. + * first so other plugins never act on the attempt. The reminder goes on the action bar, so + * clicking while down never floods chat. */ public final class KnockoutRestrictions implements Listener { @@ -131,6 +131,6 @@ private void notice(Player player) { return; } lastNotice.put(player.getUniqueId(), now); - RPTexts.send(player, PvpLoader.getDownedBlocked()); + PvpActionBar.show(player, PvpLoader.getDownedBlocked()); } } diff --git a/src/main/java/net/tfminecraft/rpcharacters/pvp/PvpActionBar.java b/src/main/java/net/tfminecraft/rpcharacters/pvp/PvpActionBar.java new file mode 100644 index 0000000..fc371a0 --- /dev/null +++ b/src/main/java/net/tfminecraft/rpcharacters/pvp/PvpActionBar.java @@ -0,0 +1,38 @@ +package net.tfminecraft.rpcharacters.pvp; + +import org.bukkit.entity.Player; + +import io.lumine.mythic.lib.api.player.MMOPlayerData; +import io.lumine.mythic.lib.message.actionbar.ActionBarPriority; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import net.tfminecraft.rpcharacters.utils.RPTexts; + +/** Action bar text for PvP. MythicLib redraws MMOCore's bar every tick, so go through its queue when it tracks the player. */ +final class PvpActionBar { + + /** How long a line stays up when nothing replaces it. */ + static final long TICKS = 40L; + + private PvpActionBar() { + } + + static void show(Player player, String raw) { + String text = RPTexts.formatDisplay(raw); + MMOPlayerData data = MMOPlayerData.getOrNull(player.getUniqueId()); + if (data != null) { + data.getActionBar().show(ActionBarPriority.HIGH, TICKS, text); + } else { + player.sendActionBar(LegacyComponentSerializer.legacySection().deserialize(text)); + } + } + + static void clear(Player player) { + MMOPlayerData data = MMOPlayerData.getOrNull(player.getUniqueId()); + if (data != null) { + data.getActionBar().reset(ActionBarPriority.HIGH); + } else { + player.sendActionBar(Component.empty()); + } + } +} diff --git a/src/main/resources/pvp.yml b/src/main/resources/pvp.yml index bb88a9d..02dd06f 100644 --- a/src/main/resources/pvp.yml +++ b/src/main/resources/pvp.yml @@ -83,7 +83,7 @@ messages: already-lethal: "&7This character is already in lethal PvP." lethal: "&cThis character is now in lethal PvP. &7It goes back to nonlethal when you log out, switch character or run &f/pvp nonlethal&7." nonlethal: "&aThis character is now in nonlethal PvP." - # Shown (at most once a second) when a knocked out player tries to fight or use an item. + # Shown on the action bar (at most once a second) when a knocked out player tries to fight or use an item. downed-blocked: "&cYou're knocked out. You can still talk, but you can't do that." # Sent to a player struck by a lethal kill. {killer} = the killer's display name. auto-strike-victim: "&c{killer} &cwas in lethal PvP, so their kill gave you a strike." diff --git a/src/test/java/net/tfminecraft/rpcharacters/pvp/KnockoutRestrictionsTest.java b/src/test/java/net/tfminecraft/rpcharacters/pvp/KnockoutRestrictionsTest.java index f79342a..a57a590 100644 --- a/src/test/java/net/tfminecraft/rpcharacters/pvp/KnockoutRestrictionsTest.java +++ b/src/test/java/net/tfminecraft/rpcharacters/pvp/KnockoutRestrictionsTest.java @@ -28,6 +28,8 @@ import org.junit.jupiter.api.Test; import io.lumine.mythic.lib.api.event.skill.PlayerCastSkillEvent; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; class KnockoutRestrictionsTest { @@ -121,17 +123,18 @@ class KnockoutRestrictionsTest { verify(standingClick, never()).setUseItemInHand(any()); } - @Test void theNoticeIsSentAtMostOnceASecondAndResetsOnQuit() { + @Test void theNoticeGoesOnTheActionBarAtMostOnceASecondAndResetsOnQuit() { var first = interact(downed, Action.LEFT_CLICK_AIR); restrictions.onInteract(first); restrictions.onInteract(interact(downed, Action.RIGHT_CLICK_AIR)); - verify(downed, times(1)).sendMessage(contains("knocked out")); + verify(downed, times(1)).sendActionBar(knockedOutText()); + verify(downed, never()).sendMessage(anyString()); var quit = mock(PlayerQuitEvent.class); when(quit.getPlayer()).thenReturn(downed); restrictions.onQuit(quit); restrictions.onInteract(interact(downed, Action.RIGHT_CLICK_AIR)); - verify(downed, times(2)).sendMessage(contains("knocked out")); - verify(standing, never()).sendMessage(anyString()); + verify(downed, times(2)).sendActionBar(knockedOutText()); + verify(standing, never()).sendActionBar(any(Component.class)); } @Test void theNoticeRepeatsOnceTheIntervalHasPassed() throws Exception { @@ -142,7 +145,11 @@ class KnockoutRestrictionsTest { var notices = (java.util.Map) field.get(restrictions); notices.put(downed.getUniqueId(), System.currentTimeMillis() - 1_000L); restrictions.onInteract(interact(downed, Action.RIGHT_CLICK_AIR)); - verify(downed, times(2)).sendMessage(contains("knocked out")); + verify(downed, times(2)).sendActionBar(knockedOutText()); + } + + private static Component knockedOutText() { + return argThat(text -> PlainTextComponentSerializer.plainText().serialize(text).contains("knocked out")); } private void check(E event, Player player, Consumer handler) {