Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 3 additions & 28 deletions src/main/java/net/tfminecraft/rpcharacters/pvp/ArmourDonning.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<UUID, Donning> donning = new HashMap<>();
private static final Set<UUID> completing = new HashSet<>();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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())));
}
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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());
}
}
38 changes: 38 additions & 0 deletions src/main/java/net/tfminecraft/rpcharacters/pvp/PvpActionBar.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/pvp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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 {
Expand All @@ -142,7 +145,11 @@ class KnockoutRestrictionsTest {
var notices = (java.util.Map<UUID, Long>) 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 <E extends Event & Cancellable> void check(E event, Player player, Consumer<E> handler) {
Expand Down