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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Beyond a name and appearance, characters have traits, professions, injuries, and
- **Consequences and investigation** — support lethal or nonlethal PvP, graves, and discoverable clues left in the world.
- **Nonlethal knockouts** — GSit holds downed players in a crawl pose for the knockout duration, alongside freeze and blindness. The pose uses GSit's API, bypasses command restrictions, respects other plugins' crawl vetoes, and releases only knockout-created crawls on recovery. GSit is optional for the rest of RPCharacters; without it, knockouts retain freeze and blindness only. While downed, a player can only be finished by another player in lethal mode, a mob, `/kill`, the void or the world border: falls, drowning, fire and other damage with no attacker leave them at half a heart, and the fall distance built up by the freeze is cleared when they get up. Knockouts never apply to players in a started SimpleFactions battle, so the battle records the death and routes the respawn.
- **PvP strikes** — after `/pvp start`, the fight lasts 15 minutes unless the player who started it runs `/pvp end`. When it ends, players who have not died see a title that a new RP interaction is needed. Whoever kills or knocks someone out chooses to spare them or give a strike; the third strike kills the character, though a killer can wound or maim instead of killing. The same player must wait 24 hours by default before striking a character again (`strikes.same-target-cooldown-hours` in `pvp.yml`; 0 disables the wait), so at that default they cannot land all three strikes in a day. Lockpicking, robbing, pickpocketing and looting locked graves start a timed evil RP session, during which any strike kills and a death leaves an unlocked grave.
- **Armour takes time** — putting armour on takes a while (by default 1 second for a helmet, 20 for a chestplate, 12 for leggings and 8 for boots). Pieces go on one at a time and the player is slowed until they're done. Taking or dealing damage, sprinting, dying or logging out stops it. `/pvp start` takes off any armour that someone in range put on in the last 3 minutes, and nobody in the fight can put armour on until it ends unless they die. The times are set under `armour` in `pvp.yml`. Masks, heads and elytras go on at once.
- **Armour takes time** — putting armour on takes a while (by default 1 second for a helmet, 20 for a chestplate, 12 for leggings and 8 for boots). Pieces go on one at a time and the player is slowed until they're done. Taking or dealing damage, sprinting, dying or logging out stops it. `/pvp start` takes off any chestplate, leggings or boots that someone in range put on in the last 3 minutes, and nobody in the fight can put armour on until it ends unless they die. `/pvp start` never takes a helmet off. Once recent armour is off, it notes who is still in a chestplate, leggings and boots: they can take their helmet off and put it back on for the rest of the fight, and anyone else can't put one on. This is decided when the fight is called and doesn't change during it. The times are set under `armour` in `pvp.yml`. Masks, heads and elytras go on at once.

## Beyond the game

Expand Down
21 changes: 15 additions & 6 deletions src/main/java/net/tfminecraft/rpcharacters/pvp/ArmourDonning.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
* looks likely. Pieces are fastened one after another while the player is
* slowed. Taking or dealing damage, sprinting, dying or logging out stops it.
* Masks, heads and elytras go on at once, and creative or spectator players
* are never delayed. {@code /pvp start} takes off anything put on recently.
* are never delayed. {@code /pvp start} takes off any body armour put on
* recently. Helmets are left alone, so they can come off and on for roleplay.
*/
public final class ArmourDonning implements Listener {

Expand Down Expand Up @@ -111,6 +112,13 @@ static boolean isArmourPiece(ItemStack item) {
return armour && !MaskService.isMaskItem(item);
}

/** Wearing a real chestplate, leggings and boots. The helmet doesn't count. */
public static boolean wearsBodyArmour(Player player) {
PlayerInventory inventory = player.getInventory();
return isArmourPiece(inventory.getChestplate()) && isArmourPiece(inventory.getLeggings())
&& isArmourPiece(inventory.getBoots());
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onArmorEquip(ArmorEquipEvent event) {
Player player = event.getPlayer();
Expand Down Expand Up @@ -178,7 +186,7 @@ static void undoSwap(Player player, ArmorType type, int handSlot, ItemStack want
|| !previous.isSimilar(inventory.getItem(handSlot))) {
return;
}
boolean locked = PvpSituations.locksArmour(player.getUniqueId());
boolean locked = PvpSituations.locksArmour(player.getUniqueId(), type);
if (!locked && !delays(player, type, wanted)) {
return;
}
Expand Down Expand Up @@ -227,8 +235,9 @@ public void onQuit(PlayerQuitEvent event) {
}

/**
* For {@code /pvp start}: stop fastening, and take off any piece put on within
* {@code armour.recent-seconds}. Returns whether anything came off.
* For {@code /pvp start}: stop fastening, and take off any body armour put on
* within {@code armour.recent-seconds}. The helmet stays. Returns whether
* anything came off.
*/
public static boolean takeOffRecent(Player player, long nowMs) {
stop(player, true);
Expand All @@ -241,7 +250,7 @@ public static boolean takeOffRecent(Player player, long nowMs) {
List<String> pieces = new ArrayList<>();
for (ArmorType type : ArmorType.values()) {
Long at = times.get(type);
if (at == null || nowMs - at > windowMs) {
if (type == ArmorType.HELMET || at == null || nowMs - at > windowMs) {
continue;
}
times.remove(type);
Expand Down Expand Up @@ -318,7 +327,7 @@ static void tick(long nowMs) {

private static void finish(Player player, Donning state, long nowMs) {
Piece piece = state.current;
if (PvpSituations.locksArmour(player.getUniqueId())) {
if (PvpSituations.locksArmour(player.getUniqueId(), piece.type())) {
RPTexts.send(player, PvpLoader.getArmourLocked());
stop(player, false);
return;
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/net/tfminecraft/rpcharacters/pvp/PvpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ private void startWarning(Player player) {
if (previous != null) {
finish(previous);
}
PvpSituation situation = new PvpSituation(player.getUniqueId(), targets);
takeOffRecentArmour(targets);
PvpSituation situation = new PvpSituation(player.getUniqueId(), targets, armoured(targets));
PvpSituations.track(situation);
tasksBySituation.put(situation, new ArrayList<>());
takeOffRecentArmour(targets);

String warning = PvpLoader.getStartWarning()
.replace("{seconds}", String.valueOf(PvpLoader.getStartWarnSeconds()));
Expand Down Expand Up @@ -229,12 +229,15 @@ private void schedule(PvpSituation situation, BukkitTask task) {
tasks.add(task);
}

/** Everyone caught in an open fight keeps the armour they had when it was called. */
/**
* Everyone caught in an open fight keeps the armour they had when it was called.
* Those in body armour may still put a helmet on.
*/
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onArmorEquip(ArmorEquipEvent event) {
Player player = event.getPlayer();
if (player == null || event.getNewArmorPiece() == null
|| !PvpSituations.locksArmour(player.getUniqueId())) {
|| !PvpSituations.locksArmour(player.getUniqueId(), event.getType())) {
return;
}
event.setCancelled(true);
Expand All @@ -257,6 +260,18 @@ private void takeOffRecentArmour(List<UUID> targets) {
}
}

/** Who is still in a chestplate, leggings and boots once recent armour is off. */
private static List<UUID> armoured(List<UUID> targets) {
List<UUID> armoured = new ArrayList<>();
for (UUID id : targets) {
Player online = Bukkit.getPlayer(id);
if (online != null && ArmourDonning.wearsBodyArmour(online)) {
armoured.add(id);
}
}
return armoured;
}

private void broadcast(List<UUID> targets, String raw) {
for (UUID id : targets) {
Player online = Bukkit.getPlayer(id);
Expand Down
26 changes: 21 additions & 5 deletions src/main/java/net/tfminecraft/rpcharacters/pvp/PvpSituation.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,32 @@ public final class PvpSituation {

private final UUID issuerId;
private final Set<UUID> participants;
private final Set<UUID> armoured;
private final Set<UUID> died = ConcurrentHashMap.newKeySet();
private boolean open = true;
private boolean started;

public PvpSituation(UUID issuerId, Collection<UUID> participants) {
this(issuerId, participants, null);
}

/** {@code armoured} are the participants who wore body armour when it was called. */
public PvpSituation(UUID issuerId, Collection<UUID> participants, Collection<UUID> armoured) {
this.issuerId = issuerId;
this.participants = idsOf(participants);
this.armoured = idsOf(armoured);
}

private static Set<UUID> idsOf(Collection<UUID> source) {
Set<UUID> ids = ConcurrentHashMap.newKeySet();
if (participants != null) {
for (UUID participant : participants) {
if (participant != null) {
ids.add(participant);
if (source != null) {
for (UUID id : source) {
if (id != null) {
ids.add(id);
}
}
}
this.participants = Set.copyOf(ids);
return Set.copyOf(ids);
}

public UUID issuerId() {
Expand All @@ -53,6 +64,11 @@ public boolean includes(UUID playerId) {
return playerId != null && participants.contains(playerId);
}

/** Came into the fight in a chestplate, leggings and boots, so may put a helmet on during it. */
public boolean wasArmoured(UUID playerId) {
return playerId != null && armoured.contains(playerId);
}

public boolean hasDied(UUID playerId) {
return playerId != null && died.contains(playerId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

import net.tfminecraft.tlibs.armour.ArmorType;

/** Open {@code /pvp start} situations. The command layer owns timers and titles. */
public final class PvpSituations {

Expand Down Expand Up @@ -42,13 +44,17 @@ public static void markDeath(UUID playerId) {
}
}

/** From {@code /pvp start} until the fight closes, nobody in it who is still alive may put armour on. */
public static boolean locksArmour(UUID playerId) {
/**
* From {@code /pvp start} until the fight closes, nobody in it who is still alive
* may put armour on. Helmets are free for those who came in wearing body armour.
*/
public static boolean locksArmour(UUID playerId, ArmorType type) {
if (playerId == null) {
return false;
}
for (PvpSituation situation : open) {
if (situation.isOpen() && situation.includes(playerId) && !situation.hasDied(playerId)) {
if (situation.isOpen() && situation.includes(playerId) && !situation.hasDied(playerId)
&& (type != ArmorType.HELMET || !situation.wasArmoured(playerId))) {
return true;
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/resources/pvp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ armour:
boots: 8
# Slowness while fastening (0 = Slowness I). -1 for none.
slowness-amplifier: 1
# /pvp start takes off armour that anyone in range put on within this many
# seconds, and from then until the fight ends nobody in it can put armour on.
# Someone who dies in the fight can gear up again. 0 takes nothing off.
# /pvp start takes off body armour that anyone in range put on within this
# many seconds, and from then until the fight ends nobody in it can put armour
# on. Helmets are never taken off. Whoever is still in a chestplate, leggings
# and boots when the fight is called can take their helmet off and put it back
# on until it ends; anyone else can't put one on. Someone who dies in the fight
# can gear up again.
# 0 takes nothing off.
recent-seconds: 180

messages:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ ArmorEquipEvent equip(ArmorType type, ItemStack piece, EquipMethod method) {
}

@Test void leggingsGoOnAndShutdownStopsFastening() {
assertFalse(PvpSituations.locksArmour(null));
assertFalse(PvpSituations.locksArmour(null, ArmorType.HELMET));
ItemStack leggings = new ItemStack(Material.IRON_LEGGINGS);
player.getInventory().setItem(0, leggings.clone());
equip(ArmorType.LEGGINGS, leggings, EquipMethod.SHIFT_CLICK);
Expand Down Expand Up @@ -360,8 +360,8 @@ ArmorEquipEvent equip(ArmorType type, ItemStack piece, EquipMethod method) {
listener.onArmorChanged(new ArmorEquipEvent(player, EquipMethod.SHIFT_CLICK, ArmorType.HELMET, null, helmet));
listener.onArmorChanged(new ArmorEquipEvent(player, EquipMethod.SHIFT_CLICK, ArmorType.HELMET, helmet, null));
long put = System.currentTimeMillis();
player.getInventory().setHelmet(new ItemStack(Material.PLAYER_HEAD));
listener.onArmorChanged(new ArmorEquipEvent(player, EquipMethod.HOTBAR, ArmorType.HELMET, null, new ItemStack(Material.PLAYER_HEAD)));
player.getInventory().setHelmet(helmet.clone());
listener.onArmorChanged(new ArmorEquipEvent(player, EquipMethod.SHIFT_CLICK, ArmorType.HELMET, null, helmet));
player.getInventory().setChestplate(chestplate.clone());
listener.onArmorChanged(new ArmorEquipEvent(player, EquipMethod.SHIFT_CLICK, ArmorType.CHESTPLATE, null, chestplate));
player.getInventory().setBoots(boots.clone());
Expand All @@ -380,10 +380,43 @@ ArmorEquipEvent equip(ArmorType type, ItemStack piece, EquipMethod method) {
assertTrue(lines.stream().anyMatch(line -> line.contains("stopped fastening your boots")));
assertNull(player.getInventory().getChestplate());
assertNull(player.getInventory().getBoots());
assertEquals(Material.PLAYER_HEAD, player.getInventory().getHelmet().getType(), "Heads aren't armour");
assertEquals(Material.IRON_HELMET, player.getInventory().getHelmet().getType(), "Helmets stay on for roleplay");
assertTrue(player.getInventory().contains(Material.IRON_CHESTPLATE));
assertEquals(1, player.getWorld().getEntities().stream().filter(org.bukkit.entity.Item.class::isInstance).count(), "What doesn't fit is dropped at their feet");
assertFalse(ArmourDonning.takeOffRecent(player, put + 60_000L), "Each piece comes off once");
ItemStack elytra = new ItemStack(Material.ELYTRA); player.getInventory().setChestplate(elytra.clone());
listener.onArmorChanged(new ArmorEquipEvent(player, EquipMethod.HOTBAR, ArmorType.CHESTPLATE, null, elytra));
assertFalse(ArmourDonning.takeOffRecent(player, put + 60_000L));
assertEquals(Material.ELYTRA, player.getInventory().getChestplate().getType(), "Elytras aren't armour");
}

@Test void onlyThoseInBodyArmourMayPutAHelmetOnInAFight() {
ItemStack helmet = new ItemStack(Material.IRON_HELMET), leggings = new ItemStack(Material.IRON_LEGGINGS);
assertFalse(ArmourDonning.wearsBodyArmour(player));
player.getInventory().setChestplate(chestplate.clone()); player.getInventory().setLeggings(leggings.clone());
player.getInventory().setBoots(new ItemStack(Material.ELYTRA));
assertFalse(ArmourDonning.wearsBodyArmour(player), "Boots are needed too");
player.getInventory().setBoots(boots.clone());
assertTrue(ArmourDonning.wearsBodyArmour(player));

PvpSituations.track(new PvpSituation(player.getUniqueId(), List.of(player.getUniqueId()), List.of(player.getUniqueId())));
assertFalse(PvpSituations.locksArmour(player.getUniqueId(), ArmorType.HELMET));
assertTrue(PvpSituations.locksArmour(player.getUniqueId(), ArmorType.CHESTPLATE));
player.getInventory().setItem(0, helmet.clone());
equip(ArmorType.HELMET, helmet, EquipMethod.SHIFT_CLICK);
tickAt(System.currentTimeMillis() + 2_000L);
assertEquals(Material.IRON_HELMET, player.getInventory().getHelmet().getType(), "Armoured fighters can put their helmet back on");

ItemStack hood = new ItemStack(Material.LEATHER_HELMET);
player.getInventory().setItemInOffHand(hood.clone());
player.getInventory().setHelmet(hood.clone()); player.getInventory().setItemInOffHand(helmet.clone());
ArmourDonning.undoSwap(player, ArmorType.HELMET, 40, hood, helmet);
tickAt(System.currentTimeMillis() + 4_000L);
assertEquals(Material.LEATHER_HELMET, player.getInventory().getHelmet().getType(), "A helmet swap is fastened, not refused");
assertFalse(said("can't put armour on"));

lockInFight();
assertTrue(PvpSituations.locksArmour(player.getUniqueId(), ArmorType.HELMET), "Another fight they came into unarmoured still locks it");
}

static EntityDamageByEntityEvent hit(org.bukkit.entity.Entity damager, org.bukkit.entity.Entity victim) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ class PvpCommandTest extends PvpRuntimeFixture {
command("end");var released=armor(victim,true);handler.onArmorEquip(released);assertFalse(released.isCancelled());
}

@Test void helmetsStayFreeForThoseWhoCameInBodyArmour() {
var inventory=victim.getInventory();when(inventory.getChestplate()).thenReturn(new ItemStack(Material.IRON_CHESTPLATE));when(inventory.getLeggings()).thenReturn(new ItemStack(Material.IRON_LEGGINGS));when(inventory.getBoots()).thenReturn(new ItemStack(Material.IRON_BOOTS));
try(var masks=mockStatic(net.tfminecraft.rpcharacters.identity.MaskService.class)){start();}countdown();
var helmet=armor(victim,true);handler.onArmorEquip(helmet);assertFalse(helmet.isCancelled(),"Armoured fighters keep their helmet free");
var chestplate=new ArmorEquipEvent(victim,ArmorEquipEvent.EquipMethod.SHIFT_CLICK,ArmorType.CHESTPLATE,null,new ItemStack(Material.IRON_CHESTPLATE));handler.onArmorEquip(chestplate);assertTrue(chestplate.isCancelled());
var unarmoured=armor(killer,true);handler.onArmorEquip(unarmoured);assertTrue(unarmoured.isCancelled(),"Without body armour a helmet stays off");var removing=armor(killer,false);handler.onArmorEquip(removing);assertFalse(removing.isCancelled());
}

@Test void startTakesOffRecentArmourAndTellsTheOthersAtOnce() {
try(var donning=mockStatic(ArmourDonning.class)){donning.when(()->ArmourDonning.takeOffRecent(eq(victim),anyLong())).thenReturn(true);start();
donning.verify(()->ArmourDonning.takeOffRecent(eq(killer),anyLong()));donning.verify(()->ArmourDonning.takeOffRecent(eq(other),anyLong()),never());assertTrue(text(killer).contains("Victim hadn't finished fastening their armour"));assertFalse(text(victim).contains("hadn't finished fastening their"));assertTrue(allTasks.stream().allMatch(task->task.delay()>0),"Armour comes off when the command runs, before the countdown");}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.tfminecraft.rpcharacters.pvp;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -78,6 +79,15 @@ void anotherOpenFightKeepsThePlayerActive() {
assertFalse(PvpSituations.remainsActiveElsewhere(issuer, first));
}

@Test
void onlyTheArmouredGetAFreeHelmet() {
PvpSituation situation = new PvpSituation(issuer, List.of(issuer, survivor), Arrays.asList(null, survivor));
assertTrue(situation.wasArmoured(survivor));
assertFalse(situation.wasArmoured(issuer));
assertFalse(situation.wasArmoured(null));
assertFalse(situation().wasArmoured(survivor));
}

private PvpSituation situation() {
return new PvpSituation(issuer, List.of(issuer, survivor, dead));
}
Expand Down
Loading
Loading