diff --git a/src/main/java/net/tfminecraft/magic/blood/BloodListener.java b/src/main/java/net/tfminecraft/magic/blood/BloodListener.java index 13fa2e5..d9aa7e9 100644 --- a/src/main/java/net/tfminecraft/magic/blood/BloodListener.java +++ b/src/main/java/net/tfminecraft/magic/blood/BloodListener.java @@ -376,12 +376,27 @@ public void onAnimalHit(EntityDamageByEntityEvent event) { AnimalRiteService.onHit(event); } + /** LOWEST: before Cooking's death handler deletes the animal's keepers. */ + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public void onAnimalDying(EntityDeathEvent event) { + Player killer = event.getEntity().getKiller(); + if (killer != null) { + WhisperService.noteKeepers(killer, event.getEntity()); + } + } + + /** MONITOR: the noted keepers are dropped however the death ended. */ + @EventHandler(priority = EventPriority.MONITOR) + public void onAnimalDead(EntityDeathEvent event) { + WhisperService.forgetKeepers(event.getEntity()); + } + @EventHandler(ignoreCancelled = true) public void onAnimalDeath(EntityDeathEvent event) { AnimalRiteService.onDeath(event); Player killer = event.getEntity().getKiller(); if (killer != null) { - WhisperService.add(killer, WhisperService.animalKillWeight(killer, event.getEntity())); + WhisperService.animalKill(killer, event.getEntity()); } } diff --git a/src/main/java/net/tfminecraft/magic/blood/BloodRecord.java b/src/main/java/net/tfminecraft/magic/blood/BloodRecord.java index 60326d9..6fb57f9 100644 --- a/src/main/java/net/tfminecraft/magic/blood/BloodRecord.java +++ b/src/main/java/net/tfminecraft/magic/blood/BloodRecord.java @@ -46,6 +46,10 @@ public final class BloodRecord { private double resonance; /** Blood in the altar's pool once the path is complete; null until then (a full pool). */ private Double pool; + /** The UTC day (days since the epoch) ownAnimalWhisper counts for. */ + private long ownAnimalDay; + /** Whisper gained on ownAnimalDay from killing the character's own animals (the daily cap). */ + private double ownAnimalWhisper; /** * One character sacrificed at the altar. rank: the victim's closeness rank when taken (1 = closest), @@ -102,6 +106,9 @@ public void normalize() { if (whisper < 0 || Double.isNaN(whisper)) { whisper = 0; } + if (ownAnimalWhisper < 0 || Double.isNaN(ownAnimalWhisper)) { + ownAnimalWhisper = 0; + } whisperLine = Math.max(0, whisperLine); huntPoints = Math.max(0, huntPoints); finalsDone = Math.max(0, finalsDone); @@ -146,6 +153,29 @@ public double getWhisper() { return whisper; } + /** Whisper own animals gave on this UTC day; 0 for any other day. */ + public double getOwnAnimalWhisper(long day) { + return day == ownAnimalDay ? ownAnimalWhisper : 0; + } + + /** Counts whisper from own animals toward this UTC day's cap, starting afresh on a new day. */ + public void addOwnAnimalWhisper(long day, double amount) { + if (day != ownAnimalDay) { + ownAnimalDay = day; + ownAnimalWhisper = 0; + } + ownAnimalWhisper += amount; + } + + /** Drops an earlier day's own-animal count, so the record can empty out; true when one was dropped. */ + public boolean forgetOwnAnimalWhisperBefore(long day) { + if (ownAnimalWhisper <= 0 || ownAnimalDay >= day) { + return false; + } + ownAnimalWhisper = 0; + return true; + } + public void setWhisper(double whisper) { this.whisper = Math.max(0, whisper); } @@ -298,6 +328,6 @@ public boolean isEmpty() { && species.isEmpty() && altarId == null && huntPoints == 0 && finalsDone == 0 && sacrifices.isEmpty() && finalTargets.isEmpty() && !pendingBacklash && !pendingNaming && artefacts.isEmpty() && charges.isEmpty() && pending.isEmpty() && resonance <= 0 - && pool == null; + && pool == null && ownAnimalWhisper <= 0; } } diff --git a/src/main/java/net/tfminecraft/magic/blood/WhisperConfig.java b/src/main/java/net/tfminecraft/magic/blood/WhisperConfig.java index 89806ec..5467e1f 100644 --- a/src/main/java/net/tfminecraft/magic/blood/WhisperConfig.java +++ b/src/main/java/net/tfminecraft/magic/blood/WhisperConfig.java @@ -60,8 +60,9 @@ public boolean coloured() { private static double decayPerHour = 1; private static Set fastTraits = Set.of(); private static double fastMultiplier = 3; - private static double animalKill = 2; - private static double aloneOrNightMultiplier = 2; + private static double animalOthers = 4; + private static double animalOwn = 1; + private static double animalOwnDailyCap = 2; private static double characterKill = 15; private static double robbery = 4; private static double corruption = 1; @@ -84,8 +85,9 @@ static void reset() { decayPerHour = 1; fastTraits = Set.of(); fastMultiplier = 3; - animalKill = 2; - aloneOrNightMultiplier = 2; + animalOthers = 4; + animalOwn = 1; + animalOwnDailyCap = 2; characterKill = 15; robbery = 4; corruption = 1; @@ -111,8 +113,16 @@ static void load(ConfigurationSection whispers, ConfigurationSection voices, Log } fastTraits = Set.copyOf(traits); fastMultiplier = Math.max(1, whispers.getDouble("fast_multiplier", 3)); - animalKill = Math.max(0, whispers.getDouble("sources.animal_kill", 2)); - aloneOrNightMultiplier = Math.max(1, whispers.getDouble("sources.alone_or_night_multiplier", 2)); + ConfigurationSection kills = whispers.getConfigurationSection("sources.animal_kills"); + if (kills != null) { + animalOthers = Math.max(0, kills.getDouble("others", 4)); + animalOwn = Math.max(0, kills.getDouble("own_fast_trait", 1)); + animalOwnDailyCap = Math.max(0, kills.getDouble("own_daily_cap", 2)); + } else if (whispers.isSet("sources.animal_kill")) { + // The older single value: it now prices other keepers' animals only, and own ones give nothing. + animalOthers = Math.max(0, whispers.getDouble("sources.animal_kill")); + animalOwn = 0; + } characterKill = Math.max(0, whispers.getDouble("sources.character_kill", 15)); robbery = Math.max(0, whispers.getDouble("sources.robbery", 4)); corruption = Math.max(0, whispers.getDouble("sources.corruption", 1)); @@ -215,12 +225,19 @@ public static double fastMultiplier() { return fastMultiplier; } - public static double animalKill() { - return animalKill; + /** Killing an animal another player keeps (before the fast-trait multiplier). */ + public static double animalOthers() { + return animalOthers; + } + + /** Killing an animal you keep yourself; fast-trait characters only, never multiplied. */ + public static double animalOwn() { + return animalOwn; } - public static double aloneOrNightMultiplier() { - return aloneOrNightMultiplier; + /** Most whisper own animals give a character per UTC day; 0 = no cap. */ + public static double animalOwnDailyCap() { + return animalOwnDailyCap; } public static double characterKill() { diff --git a/src/main/java/net/tfminecraft/magic/blood/WhisperService.java b/src/main/java/net/tfminecraft/magic/blood/WhisperService.java index ed3dcea..defce72 100644 --- a/src/main/java/net/tfminecraft/magic/blood/WhisperService.java +++ b/src/main/java/net/tfminecraft/magic/blood/WhisperService.java @@ -7,12 +7,14 @@ import java.util.Random; import java.util.UUID; import java.util.function.LongSupplier; +import java.util.function.Predicate; import java.util.function.ToDoubleFunction; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.entity.Tameable; import org.bukkit.inventory.ItemStack; import net.tfminecraft.magic.Magic; @@ -31,13 +33,28 @@ public final class WhisperService { private static final long MINUTE = 60_000L; + private static final long DAY = 86_400_000L; private static final long FIRST_VOICE_DELAY = MINUTE; private static final Map NEXT_VOICE = new HashMap<>(); + /** Keepers of animals being killed, read before Cooking's death handler deletes them (by animal). */ + private static final Map> KEEPERS_AT_DEATH = new HashMap<>(); static LongSupplier clock = System::currentTimeMillis; static Random random = new Random(); /** The player's equilibrium, NaN without a resonance session. */ static ToDoubleFunction equilibrium = WhisperService::sessionEquilibrium; + /** Whether a character has a fast trait (the evil fast route). */ + static Predicate fastTraitOf = WhisperService::fastTrait; + + /** Who keeps a killed animal, seen from its killer. */ + enum Keeper { + /** Nobody keeps it. */ + WILD, + /** The killer's account is one of its keepers. */ + OWN, + /** Other players keep it, the killer doesn't. */ + OTHERS + } private WhisperService() {} @@ -54,6 +71,10 @@ public static void add(Player player, double amount) { * before the voices start. Returns true when this woke them. */ public static boolean addTo(String character, String owner, double amount, Player online) { + return addTo(character, owner, amount, online, true); + } + + private static boolean addTo(String character, String owner, double amount, Player online, boolean multiply) { if (!BloodConfig.isEnabled() || amount <= 0) { return false; } @@ -61,7 +82,7 @@ public static boolean addTo(String character, String owner, double amount, Playe if (record.getStage() != BloodStage.NONE) { return false; } - double gain = amount * (fastTrait(character) ? WhisperConfig.fastMultiplier() : 1); + double gain = amount * (multiply && fastTraitOf.test(character) ? WhisperConfig.fastMultiplier() : 1); record.setWhisper(record.getWhisper() + gain); boolean woke = record.getWhisper() >= WhisperConfig.threshold(); if (woke) { @@ -89,14 +110,81 @@ static boolean fastTrait(String character) { } } - /** Whisper weight for killing a listed animal with a blade, more when alone or at night. */ - public static double animalKillWeight(Player killer, Entity animal) { - if (!WhisperConfig.isAnimal(animal.getType()) || !blade(killer.getInventory().getItemInMainHand())) { - return 0; + /** + * Killing a listed animal with a blade. Another player's animal feeds anyone's meter (fast traits + * multiply it as usual); the killer's own only feeds a fast-trait character's, unmultiplied and up + * to a daily cap; a wild one never does. + */ + public static void animalKill(Player killer, Entity animal) { + if (!BloodConfig.isEnabled() || !WhisperConfig.isAnimal(animal.getType()) + || !blade(killer.getInventory().getItemInMainHand())) { + return; + } + switch (keeper(killer, animal)) { + case OTHERS -> add(killer, WhisperConfig.animalOthers()); + case OWN -> ownAnimal(killer); + default -> { } } - long time = animal.getWorld().getTime(); - boolean night = time >= 13_000 && time < 23_000; - return WhisperConfig.animalKill() * (night || alone(killer) ? WhisperConfig.aloneOrNightMultiplier() : 1); + } + + /** + * Remembers the keepers of a listed animal killed with a blade, as it dies. Cooking deletes them in + * its own death handler, which runs before Magic's at the same priority, so this runs at LOWEST. + */ + public static void noteKeepers(Player killer, Entity animal) { + if (BloodConfig.isEnabled() && WhisperConfig.isAnimal(animal.getType()) + && blade(killer.getInventory().getItemInMainHand())) { + KEEPERS_AT_DEATH.put(animal.getUniqueId(), HusbandryOwners.of(animal.getUniqueId())); + } + } + + /** Drops the keepers noted for a death once it is over. */ + public static void forgetKeepers(Entity animal) { + KEEPERS_AT_DEATH.remove(animal.getUniqueId()); + } + + /** + * Cooking's husbandry keepers (as noted at the death, else read now), and a vanilla tamer, + * decide whose animal it was. + */ + static Keeper keeper(Player killer, Entity animal) { + boolean kept = false; + List owners = KEEPERS_AT_DEATH.get(animal.getUniqueId()); + for (HusbandryOwners.Owner owner : owners != null ? owners : HusbandryOwners.of(animal.getUniqueId())) { + if (owner.account().equals(killer.getUniqueId())) { + return Keeper.OWN; + } + kept = true; + } + if (animal instanceof Tameable tameable && tameable.getOwnerUniqueId() != null) { + if (tameable.getOwnerUniqueId().equals(killer.getUniqueId())) { + return Keeper.OWN; + } + kept = true; + } + return kept ? Keeper.OTHERS : Keeper.WILD; + } + + /** An own animal: fast-trait characters only, never multiplied, no more than the daily cap. */ + static void ownAnimal(Player killer) { + String character = AltarService.currentCharacter(killer); + double amount = WhisperConfig.animalOwn(); + if (character == null || amount <= 0 || !fastTraitOf.test(character)) { + return; + } + String owner = killer.getUniqueId().toString(); + BloodRecord record = BloodRecords.getOrCreate(character, owner); + if (record.getStage() != BloodStage.NONE) { + return; + } + long day = Math.floorDiv(clock.getAsLong(), DAY); + double cap = WhisperConfig.animalOwnDailyCap(); + double gain = cap > 0 ? Math.min(amount, cap - record.getOwnAnimalWhisper(day)) : amount; + if (gain <= 0) { + return; + } + record.addOwnAnimalWhisper(day, gain); + addTo(character, owner, gain, killer, false); } static boolean blade(ItemStack item) { @@ -145,8 +233,16 @@ private static void tenMinutes(Player player, String character) { addTo(character, player.getUniqueId().toString(), WhisperConfig.corruption(), player); } BloodRecord record = BloodRecords.get(character); - if (record != null && record.getStage() == BloodStage.NONE && record.getWhisper() > 0) { + if (record == null) { + return; + } + // The daily cap's count is kept until its day is over, even with the meter drained to 0. + boolean changed = record.forgetOwnAnimalWhisperBefore(Math.floorDiv(clock.getAsLong(), DAY)); + if (record.getStage() == BloodStage.NONE && record.getWhisper() > 0) { record.setWhisper(record.getWhisper() - WhisperConfig.decayPerHour() / 6.0); + changed = true; + } + if (changed) { BloodRecords.save(record); } } @@ -344,6 +440,7 @@ static Long nextVoice(UUID player) { public static void clear() { NEXT_VOICE.clear(); + KEEPERS_AT_DEATH.clear(); } private static double sessionEquilibrium(Player player) { diff --git a/src/main/resources/wildmagic/bloodmagic/voices.yml b/src/main/resources/wildmagic/bloodmagic/voices.yml index cac6910..9aabbd4 100644 --- a/src/main/resources/wildmagic/bloodmagic/voices.yml +++ b/src/main/resources/wildmagic/bloodmagic/voices.yml @@ -9,10 +9,15 @@ whispers: fast_traits: [occultist] fast_multiplier: 3.0 sources: - # Killing a listed animal with a blade. - animal_kill: 2.0 - # Animal kills count this many times more at night or with nobody near. - alone_or_night_multiplier: 2.0 + # Killing a listed animal with a blade. Keepers come from Cooking's husbandry records and a vanilla + # tamer; a wild animal (no keeper) never counts. + animal_kills: + # An animal other players keep and the killer doesn't. Counts for anyone; fast traits multiply it. + others: 4.0 + # An animal the killer's account keeps. Fast-trait characters only, never multiplied. + own_fast_trait: 1.0 + # Most whisper own animals give a character per UTC day (0 = no cap). + own_daily_cap: 2.0 # A character you killed died for good (permanent kills only; knockouts never count). character_kill: 15.0 # An evil RP session started by robbing, pickpocketing, lockpicking or looting a locked grave diff --git a/src/test/java/net/tfminecraft/magic/blood/BloodDomainTest.java b/src/test/java/net/tfminecraft/magic/blood/BloodDomainTest.java index d316cfe..b639fb2 100644 --- a/src/test/java/net/tfminecraft/magic/blood/BloodDomainTest.java +++ b/src/test/java/net/tfminecraft/magic/blood/BloodDomainTest.java @@ -42,6 +42,9 @@ void recordNormalizesBrokenFieldsAndKnowsWhenItIsEmpty() throws Exception { var line = BloodRecord.class.getDeclaredField("whisperLine"); line.setAccessible(true); line.set(record, -3); + var ownAnimal = BloodRecord.class.getDeclaredField("ownAnimalWhisper"); + ownAnimal.setAccessible(true); + ownAnimal.set(record, Double.NaN); record.setAltarId(" "); record.normalize(); assertEquals(BloodStage.NONE, record.getStage()); @@ -49,6 +52,22 @@ void recordNormalizesBrokenFieldsAndKnowsWhenItIsEmpty() throws Exception { assertTrue(record.getSpecies().isEmpty()); assertEquals(0, record.getWhisper()); assertEquals(0, record.getWhisperLine()); + assertEquals(0, record.getOwnAnimalWhisper(0)); + ownAnimal.set(record, -1.0); + record.normalize(); + assertEquals(0, record.getOwnAnimalWhisper(0)); + record.addOwnAnimalWhisper(0, 1); + record.addOwnAnimalWhisper(0, 0.5); + assertEquals(1.5, record.getOwnAnimalWhisper(0)); + assertEquals(0, record.getOwnAnimalWhisper(1)); + record.addOwnAnimalWhisper(1, 1); + assertEquals(1, record.getOwnAnimalWhisper(1)); + assertEquals(0, record.getOwnAnimalWhisper(0)); + assertFalse(record.isEmpty(), "a used daily cap keeps the record"); + assertFalse(record.forgetOwnAnimalWhisperBefore(1)); + assertTrue(record.forgetOwnAnimalWhisperBefore(2)); + assertFalse(record.forgetOwnAnimalWhisperBefore(3)); + assertEquals(0, record.getOwnAnimalWhisper(1)); assertNull(record.getAltarId()); assertTrue(record.isEmpty()); whisper.set(record, -1.0); diff --git a/src/test/java/net/tfminecraft/magic/blood/BloodListenerTest.java b/src/test/java/net/tfminecraft/magic/blood/BloodListenerTest.java index 1cfe358..9deaaef 100644 --- a/src/test/java/net/tfminecraft/magic/blood/BloodListenerTest.java +++ b/src/test/java/net/tfminecraft/magic/blood/BloodListenerTest.java @@ -212,6 +212,9 @@ void killsFeedTheKillersMeterAndSessionsResetVoiceTimers() { BloodFixture.loadBundled(fx.logger); BloodConfig.setEnabled(true); var cow = fx.world.spawn(killer.getLocation(), org.bukkit.entity.Cow.class); + var tamer = fx.server.addPlayer(); + var wolf = fx.world.spawn(killer.getLocation(), org.bukkit.entity.Wolf.class); + wolf.setOwner(tamer); killer.getInventory().setItemInMainHand(new ItemStack(Material.IRON_SWORD)); listener.onAnimalDeath(new org.bukkit.event.entity.EntityDeathEvent(cow, org.bukkit.damage.DamageSource.builder(org.bukkit.damage.DamageType.GENERIC).build(), new ArrayList<>())); @@ -219,7 +222,29 @@ void killsFeedTheKillersMeterAndSessionsResetVoiceTimers() { fx.world.setTime(1000); listener.onAnimalDeath(new org.bukkit.event.entity.EntityDeathEvent(cow, org.bukkit.damage.DamageSource.builder(org.bukkit.damage.DamageType.GENERIC).build(), new ArrayList<>())); - assertEquals(19, BloodRecords.get(killerId).getWhisper()); + assertEquals(15, BloodRecords.get(killerId).getWhisper(), "a wild cow gives nothing"); + wolf.setKiller(killer); + listener.onAnimalDeath(new org.bukkit.event.entity.EntityDeathEvent(wolf, + org.bukkit.damage.DamageSource.builder(org.bukkit.damage.DamageType.GENERIC).build(), new ArrayList<>())); + assertEquals(19, BloodRecords.get(killerId).getWhisper(), "another player's wolf counts"); + + FakeHusbandry.use(); + try { + var claimed = fx.world.spawn(killer.getLocation(), org.bukkit.entity.Cow.class); + FakeHusbandry.OWNERS.put(claimed.getUniqueId(), java.util.List.of(new FakeHusbandry.Owner(UUID.randomUUID(), null))); + var death = new org.bukkit.event.entity.EntityDeathEvent(claimed, + org.bukkit.damage.DamageSource.builder(org.bukkit.damage.DamageType.GENERIC).build(), new ArrayList<>()); + listener.onAnimalDying(death); + claimed.setKiller(killer); + listener.onAnimalDying(death); + FakeHusbandry.OWNERS.clear(); + listener.onAnimalDeath(death); + listener.onAnimalDead(death); + assertEquals(23, BloodRecords.get(killerId).getWhisper(), "a claimed cow counts though Cooking deleted its keepers"); + assertEquals(WhisperService.Keeper.WILD, WhisperService.keeper(killer, claimed)); + } finally { + FakeHusbandry.restore(); + } WhisperService.voiceSoon(killer); listener.onQuit(new org.bukkit.event.player.PlayerQuitEvent(killer, "bye")); diff --git a/src/test/java/net/tfminecraft/magic/blood/BloodStoreTest.java b/src/test/java/net/tfminecraft/magic/blood/BloodStoreTest.java index ef91e0c..51639ab 100644 --- a/src/test/java/net/tfminecraft/magic/blood/BloodStoreTest.java +++ b/src/test/java/net/tfminecraft/magic/blood/BloodStoreTest.java @@ -45,6 +45,13 @@ void recordsRoundTripAndEmptyRecordsLeaveDisk() throws Exception { assertTrue(store.save(BloodRecord.create("c1", "o1"))); assertFalse(new File(characters(), "c1.json").exists()); assertTrue(store.save(BloodRecord.create("never", "o1"))); + + BloodRecord capped = BloodRecord.create("c2", "o1"); + capped.addOwnAnimalWhisper(7, 2); + assertTrue(store.save(capped)); + BloodRecord back = store.loadRecords().get(0); + assertEquals(0, back.getWhisper()); + assertEquals(2, back.getOwnAnimalWhisper(7), "the day's cap survives a drained meter and a restart"); } @Test diff --git a/src/test/java/net/tfminecraft/magic/blood/WhisperConfigTest.java b/src/test/java/net/tfminecraft/magic/blood/WhisperConfigTest.java index 2888e77..128c1e9 100644 --- a/src/test/java/net/tfminecraft/magic/blood/WhisperConfigTest.java +++ b/src/test/java/net/tfminecraft/magic/blood/WhisperConfigTest.java @@ -31,8 +31,9 @@ void bundledVoicesAndSourcesLoad() { assertFalse(WhisperConfig.fastTrait(Set.of("thief"))); assertFalse(WhisperConfig.fastTrait(java.util.Collections.singleton(null))); assertEquals(3, WhisperConfig.fastMultiplier()); - assertEquals(2, WhisperConfig.animalKill()); - assertEquals(2, WhisperConfig.aloneOrNightMultiplier()); + assertEquals(4, WhisperConfig.animalOthers()); + assertEquals(1, WhisperConfig.animalOwn()); + assertEquals(2, WhisperConfig.animalOwnDailyCap()); assertEquals(15, WhisperConfig.characterKill()); assertEquals(1, WhisperConfig.corruption()); assertEquals(-40, WhisperConfig.corruptionBelow()); @@ -82,8 +83,9 @@ void defaultsAndBadEntriesAreSafe() throws Exception { assertEquals(0, WhisperConfig.decayPerHour()); assertEquals(1, WhisperConfig.fastMultiplier()); assertTrue(WhisperConfig.isAnimal(EntityType.COW)); - assertEquals(0, WhisperConfig.animalKill()); - assertEquals(1, WhisperConfig.aloneOrNightMultiplier()); + assertEquals(0, WhisperConfig.animalOthers()); + assertEquals(0, WhisperConfig.animalOwn()); + assertEquals(2, WhisperConfig.animalOwnDailyCap()); assertEquals(0, WhisperConfig.characterKill()); assertEquals(0, WhisperConfig.corruption()); assertEquals(1, WhisperConfig.minInterval()); @@ -96,5 +98,31 @@ void defaultsAndBadEntriesAreSafe() throws Exception { Files.writeString(temp.resolve("c.yml"), "voices:\n interval_minutes: [3]\n"); BloodConfig.load(temp.resolve("c.yml").toFile(), log); assertEquals(10, WhisperConfig.minInterval()); + assertEquals(4, WhisperConfig.animalOthers()); + assertEquals(1, WhisperConfig.animalOwn()); + + Files.writeString(temp.resolve("d.yml"), String.join("\n", + "whispers:", + " sources:", + " animal_kill: 9", + " animal_kills:", + " others: -1", + " own_fast_trait: -1", + " own_daily_cap: -1", + "")); + BloodConfig.load(temp.resolve("d.yml").toFile(), log); + assertEquals(0, WhisperConfig.animalOthers()); + assertEquals(0, WhisperConfig.animalOwn()); + assertEquals(0, WhisperConfig.animalOwnDailyCap()); + + Files.writeString(temp.resolve("e.yml"), "whispers:\n sources:\n animal_kill: 3\n"); + BloodConfig.load(temp.resolve("e.yml").toFile(), log); + assertEquals(3, WhisperConfig.animalOthers()); + assertEquals(0, WhisperConfig.animalOwn()); + + Files.writeString(temp.resolve("f.yml"), "whispers:\n threshold: 5\n"); + BloodConfig.load(temp.resolve("f.yml").toFile(), log); + assertEquals(4, WhisperConfig.animalOthers()); + assertEquals(1, WhisperConfig.animalOwn()); } } diff --git a/src/test/java/net/tfminecraft/magic/blood/WhisperServiceTest.java b/src/test/java/net/tfminecraft/magic/blood/WhisperServiceTest.java index 95c4390..56909a0 100644 --- a/src/test/java/net/tfminecraft/magic/blood/WhisperServiceTest.java +++ b/src/test/java/net/tfminecraft/magic/blood/WhisperServiceTest.java @@ -8,6 +8,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.time.Instant; +import java.util.Arrays; import java.util.Optional; import java.util.Random; import java.util.Set; @@ -25,6 +26,8 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Cow; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Wolf; import org.bukkit.entity.Zombie; import org.bukkit.inventory.ItemStack; import org.junit.jupiter.api.AfterEach; @@ -52,6 +55,8 @@ void cleanup() { WhisperService.clear(); WhisperService.clock = System::currentTimeMillis; WhisperService.random = new Random(); + WhisperService.fastTraitOf = WhisperService::fastTrait; + FakeHusbandry.restore(); fx.close(); } @@ -96,23 +101,152 @@ void fastTraitCharactersFillFaster() { assertFalse(WhisperService.fastTrait("not-a-uuid")); } + static void keep(Entity animal, UUID... accounts) { + FakeHusbandry.OWNERS.put(animal.getUniqueId(), + Arrays.stream(accounts).map(a -> new FakeHusbandry.Owner(a, null)).toList()); + } + + @Test + void animalKillsCountByWhoKeepsTheAnimal() { + FakeHusbandry.use(); + PlayerMock player = fx.player("mage"); + String id = BloodFixture.id("mage"); + UUID stranger = UUID.randomUUID(); + Cow cow = fx.world.spawn(player.getLocation(), Cow.class); + Cow wild = fx.world.spawn(player.getLocation(), Cow.class); + Zombie zombie = fx.world.spawn(player.getLocation(), Zombie.class); + keep(cow, stranger); + keep(zombie, stranger); + + WhisperService.animalKill(player, cow); + assertNull(BloodRecords.get(id), "no blade, no whisper"); + player.getInventory().setItemInMainHand(new ItemStack(Material.IRON_SWORD)); + WhisperService.animalKill(player, zombie); + BloodConfig.setEnabled(false); + WhisperService.animalKill(player, cow); + BloodConfig.setEnabled(true); + WhisperService.animalKill(player, wild); + assertNull(BloodRecords.get(id), "unlisted, off or wild: nothing"); + + WhisperService.animalKill(player, cow); + assertEquals(4, BloodRecords.get(id).getWhisper(), "another player's animal counts for anyone"); + keep(cow, stranger, player.getUniqueId()); + WhisperService.animalKill(player, cow); + assertEquals(4, BloodRecords.get(id).getWhisper(), "own animals need the fast trait"); + + WhisperService.fastTraitOf = character -> true; + for (int i = 0; i < 3; i++) { + WhisperService.animalKill(player, cow); + } + assertEquals(6, BloodRecords.get(id).getWhisper(), "own animals: 1 each, 2 a day, never tripled"); + now.addAndGet(86_400_000L); + WhisperService.animalKill(player, cow); + assertEquals(7, BloodRecords.get(id).getWhisper(), "a new day, a new cap"); + keep(cow, stranger); + WhisperService.animalKill(player, cow); + assertEquals(19, BloodRecords.get(id).getWhisper(), "another player's animal is tripled"); + } + @Test - void animalKillsWeighBladesNightAndSolitude() { + void keepersNotedAtDeathOutliveCookingsDelete() { + FakeHusbandry.use(); PlayerMock player = fx.player("mage"); + String id = BloodFixture.id("mage"); + player.getInventory().setItemInMainHand(new ItemStack(Material.IRON_SWORD)); Cow cow = fx.world.spawn(player.getLocation(), Cow.class); Zombie zombie = fx.world.spawn(player.getLocation(), Zombie.class); - fx.world.setTime(1000); - assertEquals(0, WhisperService.animalKillWeight(player, cow)); + Cow shot = fx.world.spawn(player.getLocation(), Cow.class); + keep(cow, UUID.randomUUID()); + keep(zombie, UUID.randomUUID()); + keep(shot, UUID.randomUUID()); + + WhisperService.noteKeepers(player, cow); + WhisperService.noteKeepers(player, zombie); + BloodConfig.setEnabled(false); + WhisperService.noteKeepers(player, cow); + BloodConfig.setEnabled(true); + player.getInventory().setItemInMainHand(new ItemStack(Material.BOW)); + WhisperService.noteKeepers(player, shot); player.getInventory().setItemInMainHand(new ItemStack(Material.IRON_SWORD)); - assertEquals(0, WhisperService.animalKillWeight(player, zombie)); - assertEquals(4, WhisperService.animalKillWeight(player, cow)); + FakeHusbandry.OWNERS.clear(); + assertEquals(WhisperService.Keeper.WILD, WhisperService.keeper(player, shot), "no blade: not noted"); + assertEquals(WhisperService.Keeper.WILD, WhisperService.keeper(player, zombie), "unlisted: not noted"); + WhisperService.animalKill(player, cow); + assertEquals(4, BloodRecords.get(id).getWhisper(), "noted keepers count after Cooking deleted them"); + WhisperService.forgetKeepers(cow); + assertEquals(WhisperService.Keeper.WILD, WhisperService.keeper(player, cow)); + WhisperService.noteKeepers(player, cow); + WhisperService.clear(); + assertEquals(WhisperService.Keeper.WILD, WhisperService.keeper(player, cow)); + } + + @Test + void tamersKeepTheirAnimalsToo() { + FakeHusbandry.use(); + PlayerMock player = fx.player("mage"); + PlayerMock other = fx.server.addPlayer(); + Wolf wolf = fx.world.spawn(player.getLocation(), Wolf.class); + assertEquals(WhisperService.Keeper.WILD, WhisperService.keeper(player, wolf)); + wolf.setOwner(other); + assertEquals(WhisperService.Keeper.OTHERS, WhisperService.keeper(player, wolf)); + wolf.setOwner(player); + assertEquals(WhisperService.Keeper.OWN, WhisperService.keeper(player, wolf)); + keep(wolf, UUID.randomUUID()); + assertEquals(WhisperService.Keeper.OWN, WhisperService.keeper(player, wolf)); + } + + @Test + void ownAnimalsNeedACharacterAnOpenMeterAndFollowTheCapSetting() throws Exception { + PlayerMock bare = fx.server.addPlayer(); + WhisperService.fastTraitOf = character -> true; + WhisperService.ownAnimal(bare); + + PlayerMock player = fx.player("mage"); + String id = BloodFixture.id("mage"); + fx.record("mage", BloodStage.WHISPERED); + WhisperService.ownAnimal(player); + assertEquals(0, BloodRecords.get(id).getOwnAnimalWhisper(0), "awake: the meter no longer fills"); + + WhisperService.fastTraitOf = character -> false; + PlayerMock plain = fx.player("plain"); + WhisperService.ownAnimal(plain); + assertNull(BloodRecords.get(BloodFixture.id("plain")), "no fast trait, no record"); + WhisperService.fastTraitOf = character -> true; + + PlayerMock second = fx.player("second"); + String secondId = BloodFixture.id("second"); + Files.writeString(temp.resolve("v.yml"), String.join("\n", + "whispers:", + " sources:", + " animal_kills:", + " own_fast_trait: 1.5", + " own_daily_cap: 0", + "")); + BloodConfig.load(temp.resolve("v.yml").toFile(), fx.logger); + BloodConfig.setEnabled(true); + for (int i = 0; i < 4; i++) { + WhisperService.ownAnimal(second); + } + assertEquals(6, BloodRecords.get(secondId).getWhisper(), "no cap"); + + Files.writeString(temp.resolve("w.yml"), String.join("\n", + "whispers:", + " sources:", + " animal_kills:", + " own_fast_trait: 0", + "")); + BloodConfig.load(temp.resolve("w.yml").toFile(), fx.logger); + BloodConfig.setEnabled(true); + WhisperService.ownAnimal(second); + assertEquals(6, BloodRecords.get(secondId).getWhisper()); + } + + @Test + void bladesAndSolitude() { + PlayerMock player = fx.player("mage"); PlayerMock witness = fx.server.addPlayer(); witness.teleport(player.getLocation()); - assertEquals(2, WhisperService.animalKillWeight(player, cow)); - fx.world.setTime(15000); - assertEquals(4, WhisperService.animalKillWeight(player, cow)); - fx.world.setTime(23500); - assertEquals(2, WhisperService.animalKillWeight(player, cow)); + assertFalse(WhisperService.alone(player)); witness.teleport(new Location(fx.world, 500, 11, 500)); assertTrue(WhisperService.alone(player)); @@ -218,6 +352,24 @@ void giftDropsWhatDoesNotFit() { assertTrue(fx.world.getEntities().size() >= before); } + @Test + void theDaysCapOutlivesADrainedMeterUntilTheDayIsOver() { + PlayerMock player = fx.player("mage"); + String id = BloodFixture.id("mage"); + BloodRecord record = BloodRecords.getOrCreate(id, player.getUniqueId().toString()); + record.addOwnAnimalWhisper(0, 2); + record.setWhisper(0.1); + BloodRecords.save(record); + WhisperService.tick(minute(10)); + assertEquals(0, record.getWhisper()); + assertSame(record, BloodRecords.get(id), "kept while today's cap is in use"); + WhisperService.tick(minute(20)); + assertSame(record, BloodRecords.get(id)); + now.addAndGet(86_400_000L); + WhisperService.tick(minute(30)); + assertNull(BloodRecords.get(id), "a new day: the old count goes and the empty record with it"); + } + @Test void minuteTicksDrainCorruptAndSpeakWhenAloneAndDue() { PlayerMock player = fx.player("mage");