This commit is contained in:
1 parent
cc6c1a7f44
commit
c80c088945
10 files changed
+1048
-2
No files matched your search
@@ -60,8 +60,8 @@ class FantasyStickLifecycleNativeTest {
|
||||
assertTrue(fixture.order.indexOf("CharacterChatNames") >= 0);
|
||||
assertTrue(fixture.order.indexOf("CharacterChatNames") < recipe);
|
||||
assertNotNull(fixture.packetListener);
|
||||
assertEquals(6, fixture.timers.size(),
|
||||
"Presentation, ability/bond cleanup, provenance, reward retry and session particles");
|
||||
assertEquals(7, fixture.timers.size(),
|
||||
"Presentation, ability/bond/rally cleanup, provenance, reward retry and session particles");
|
||||
assertTrue(fixture.order.indexOf("PartyCombat") >= 0);
|
||||
assertTrue(fixture.order.indexOf("PartyCombat") < recipe);
|
||||
assertTrue(fixture.order.indexOf("CombatExperience") >= 0);
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package games.dmg.fantasystick;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import games.dmg.fantasystick.ability.AbilityExecution;
|
||||
import games.dmg.fantasystick.ability.AbilityReport;
|
||||
import games.dmg.fantasystick.ability.OutgoingDamageBonus;
|
||||
import games.dmg.fantasystick.party.PartyRoster;
|
||||
import games.dmg.fantasystick.session.ControlInput;
|
||||
import games.dmg.fantasystick.session.EquipmentSessions;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class OutgoingDamageBonusNativeTest {
|
||||
@BeforeAll static void bootstrap() throws Exception { NativeRuntime.bootstrap(); }
|
||||
|
||||
@Test void strongestDeterministicGrantAppliesOnceAndReportsOnlyAcceptedAllyUse() throws Exception {
|
||||
var sessions = mock(EquipmentSessions.class); var parties = mock(PartyRoster.class);
|
||||
var recipient = mock(Player.class); var enemy = mock(LivingEntity.class);
|
||||
var recipientId = UUID.randomUUID(); when(recipient.getUniqueId()).thenReturn(recipientId);
|
||||
when(enemy.getUniqueId()).thenReturn(UUID.randomUUID());
|
||||
var aided = new EquipmentSessions.Active(recipientId, UUID.randomUUID(), UUID.randomUUID());
|
||||
when(sessions.active(recipientId)).thenReturn(java.util.Optional.of(aided)); when(sessions.current(aided)).thenReturn(true);
|
||||
var sourcePlayer = mock(Player.class); when(sourcePlayer.getUniqueId()).thenReturn(UUID.randomUUID());
|
||||
var party = new PartyRoster.Party(UUID.randomUUID(), sourcePlayer.getUniqueId(), List.of(sourcePlayer.getUniqueId(), recipientId));
|
||||
when(parties.partyOf(sourcePlayer.getUniqueId())).thenReturn(java.util.Optional.of(party));
|
||||
when(parties.partyOf(recipientId)).thenReturn(java.util.Optional.of(party));
|
||||
var supports = new ArrayList<OutgoingDamageBonus.Support>();
|
||||
var bonuses = new OutgoingDamageBonus(sessions, parties, supports::add);
|
||||
var first = execution(sourcePlayer, UUID.randomUUID()); var strongest = execution(sourcePlayer, UUID.randomUUID());
|
||||
var tiedLater = execution(sourcePlayer, UUID.randomUUID());
|
||||
var firstGrant = bonuses.grant(first, recipient, new BigDecimal("20"), () -> true);
|
||||
var strongestGrant = bonuses.grant(strongest, recipient, new BigDecimal("25"), () -> true);
|
||||
var tiedGrant = bonuses.grant(tiedLater, recipient, new BigDecimal("25"), () -> true);
|
||||
|
||||
var hit = hit(recipient, enemy, 10); bonuses.increase(hit); assertEquals(12.5, hit.getDamage(), 1.0e-9);
|
||||
bonuses.accepted(hit);
|
||||
assertEquals(1, supports.size()); assertEquals(0, new BigDecimal("2.50").compareTo(supports.getFirst().amount()));
|
||||
assertEquals(strongest.input().session().character(), supports.getFirst().supportingCharacter());
|
||||
verify(strongest).report(eq(AbilityReport.Type.BOOST_USED), eq(enemy),
|
||||
argThat(amount -> amount.compareTo(new BigDecimal("2.50")) == 0));
|
||||
verify(first, never()).report(any(), any(), any()); verify(tiedLater, never()).report(any(), any(), any());
|
||||
|
||||
var cancelled = hit(recipient, enemy, 10); bonuses.increase(cancelled); cancelled.setCancelled(true); bonuses.accepted(cancelled);
|
||||
assertEquals(1, supports.size());
|
||||
firstGrant.close(); strongestGrant.close(); tiedGrant.close(); bonuses.close();
|
||||
}
|
||||
|
||||
@Test void invalidOrSelfUseAndMarkedGuardianRedirectionCreateNoSupportOrRepeatedBonus() throws Exception {
|
||||
var sessions = mock(EquipmentSessions.class); var parties = mock(PartyRoster.class);
|
||||
var player = mock(Player.class); var enemy = mock(LivingEntity.class); var id = UUID.randomUUID();
|
||||
when(player.getUniqueId()).thenReturn(id); when(enemy.getUniqueId()).thenReturn(UUID.randomUUID());
|
||||
var active = new EquipmentSessions.Active(id, UUID.randomUUID(), UUID.randomUUID());
|
||||
when(sessions.active(id)).thenReturn(java.util.Optional.of(active)); when(sessions.current(active)).thenReturn(true);
|
||||
var supports = new ArrayList<OutgoingDamageBonus.Support>(); var bonuses = new OutgoingDamageBonus(sessions, parties, supports::add);
|
||||
var valid = new java.util.concurrent.atomic.AtomicBoolean(true);
|
||||
var self = execution(player, active.character()); bonuses.grant(self, player, new BigDecimal("20"), valid::get);
|
||||
var hit = hit(player, enemy, 10); bonuses.increase(hit); bonuses.accepted(hit);
|
||||
assertEquals(12, hit.getDamage(), 1.0e-9); assertTrue(supports.isEmpty());
|
||||
when(sessions.current(active)).thenReturn(false); valid.set(false);
|
||||
var stale = hit(player, enemy, 10); bonuses.increase(stale); assertEquals(10, stale.getDamage(), 1.0e-9);
|
||||
bonuses.close();
|
||||
}
|
||||
|
||||
private static AbilityExecution execution(Player source, UUID character) {
|
||||
var execution = mock(AbilityExecution.class); var input = mock(ControlInput.class);
|
||||
var active = new EquipmentSessions.Active(source.getUniqueId(), UUID.randomUUID(), character);
|
||||
when(input.player()).thenReturn(source); when(input.session()).thenReturn(active);
|
||||
when(execution.input()).thenReturn(input); when(execution.stillCurrent()).thenReturn(true); return execution;
|
||||
}
|
||||
private static EntityDamageByEntityEvent hit(Player attacker, LivingEntity target, double amount) {
|
||||
var source = org.bukkit.damage.DamageSource.builder(org.bukkit.damage.DamageType.PLAYER_ATTACK)
|
||||
.withCausingEntity(attacker).withDirectEntity(attacker).build();
|
||||
return new EntityDamageByEntityEvent(attacker, target, EntityDamageEvent.DamageCause.ENTITY_ATTACK, source, amount);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
package games.dmg.fantasystick;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import games.dmg.fantasystick.ability.AbilityExecution;
|
||||
import games.dmg.fantasystick.ability.OutgoingDamageBonus;
|
||||
import games.dmg.fantasystick.ability.RallyingCry;
|
||||
import games.dmg.fantasystick.party.PartyRoster;
|
||||
import games.dmg.fantasystick.persistence.CharacterRecord;
|
||||
import games.dmg.fantasystick.persistence.CharacterStorage;
|
||||
import games.dmg.fantasystick.persistence.YamlCharacterRepository;
|
||||
import games.dmg.fantasystick.progression.Stat;
|
||||
import games.dmg.fantasystick.session.ControlInput;
|
||||
import games.dmg.fantasystick.session.EquipmentSessions;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import net.minecraft.network.chat.ClickEvent;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.entity.EntityDeathEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
class RallyingCryNativeTest {
|
||||
@TempDir Path data;
|
||||
@BeforeAll static void bootstrap() throws Exception { NativeRuntime.bootstrap(); }
|
||||
|
||||
@Test void bookAcquisitionEquippedUseBuffsCasterAndNearbyPartyAllyAndRewardsActualUse() throws Exception {
|
||||
try (var fixture = new FantasyStickLifecycleNativeTest.Fixture(data)) {
|
||||
fixture.plugin.setEnabled(true); var caster = new FantasyStickLifecycleNativeTest.Actor(data);
|
||||
var ally = new FantasyStickLifecycleNativeTest.Actor(data); prepareCaster(data, caster, false);
|
||||
fixture.players.put(caster.core.id, caster.player); fixture.players.put(ally.core.id, ally.player);
|
||||
fixture.activate(caster); fixture.activate(ally); party(fixture, caster, ally); nearby(caster, List.of(ally.player));
|
||||
var casterEffects = effects(caster.player); var allyEffects = effects(ally.player);
|
||||
|
||||
caster.core.inventory.getInventory().setSelectedSlot(0); PartyBookNativeTest.open(fixture, caster);
|
||||
String text = PartyBookNativeTest.text(caster).replaceAll("\\s+", "");
|
||||
assertTrue(text.contains("RallyingCry")); assertTrue(text.contains("CHR16")); assertTrue(text.contains("CON12"));
|
||||
assertTrue(text.contains("Cost:1")); assertTrue(text.contains("20%")); assertTrue(text.contains("6seconds"));
|
||||
PartyBookNativeTest.click(fixture, caster, button(caster, "[Learn]", "rallying_cry")); drain(fixture, 2);
|
||||
PartyBookNativeTest.click(fixture, caster, button(caster, "[3]", "rallying_cry")); drain(fixture, 2);
|
||||
use(fixture, caster); drain(fixture, 2);
|
||||
assertEquals(0, casterEffects.get(PotionEffectType.SPEED).getAmplifier());
|
||||
assertEquals(0, allyEffects.get(PotionEffectType.SPEED).getAmplifier());
|
||||
assertNotNull(storage(fixture).find(caster.core.record.id()).get().orElseThrow().cooldowns().get("rallying_cry"));
|
||||
|
||||
var enemy = mock(LivingEntity.class); when(enemy.getUniqueId()).thenReturn(UUID.randomUUID());
|
||||
when(enemy.getType()).thenReturn(org.bukkit.entity.EntityType.ZOMBIE);
|
||||
when(enemy.getLocation()).thenReturn(new Location(caster.world, 4, 64, 0)); when(caster.world.getUID()).thenReturn(UUID.randomUUID());
|
||||
var source = org.bukkit.damage.DamageSource.builder(org.bukkit.damage.DamageType.PLAYER_ATTACK)
|
||||
.withCausingEntity(ally.player).withDirectEntity(ally.player).build();
|
||||
var hit = new EntityDamageByEntityEvent(ally.player, enemy, EntityDamageEvent.DamageCause.ENTITY_ATTACK, source, 10);
|
||||
fixture.dispatcher.accept(hit); assertEquals(12, hit.getDamage(), 1.0e-9);
|
||||
fixture.dispatcher.accept(new EntityDeathEvent(enemy, source, new ArrayList<>(), 0)); drain(fixture, 4);
|
||||
assertEquals(BigInteger.valueOf(5), storage(fixture).find(caster.core.record.id()).get().orElseThrow().experience());
|
||||
assertEquals(BigInteger.valueOf(5), storage(fixture).find(ally.core.record.id()).get().orElseThrow().experience());
|
||||
fixture.delayed.getLast().run();
|
||||
assertFalse(casterEffects.containsKey(PotionEffectType.SPEED)); assertFalse(allyEffects.containsKey(PotionEffectType.SPEED));
|
||||
var expired = new EntityDamageByEntityEvent(ally.player, enemy, EntityDamageEvent.DamageCause.ENTITY_ATTACK, source, 10);
|
||||
fixture.dispatcher.accept(expired); assertEquals(10, expired.getDamage(), 1.0e-9);
|
||||
var deadline = storage(fixture).find(caster.core.record.id()).get().orElseThrow().cooldowns().get("rallying_cry");
|
||||
caster.core.inventory.getInventory().setSelectedSlot(0); PartyBookNativeTest.open(fixture, caster);
|
||||
var unlearn = button(caster, "[Unlearn]", "rallying_cry");
|
||||
PartyBookNativeTest.click(fixture, caster, unlearn); PartyBookNativeTest.click(fixture, caster, unlearn); drain(fixture, 2);
|
||||
assertEquals(deadline, storage(fixture).find(caster.core.record.id()).get().orElseThrow().cooldowns().get("rallying_cry"));
|
||||
PartyBookNativeTest.click(fixture, caster, button(caster, "[Learn]", "rallying_cry")); drain(fixture, 2);
|
||||
PartyBookNativeTest.click(fixture, caster, button(caster, "[3]", "rallying_cry")); drain(fixture, 2);
|
||||
use(fixture, caster); assertTrue(caster.messages.stream().anyMatch(message -> message.contains("ready in")));
|
||||
}
|
||||
}
|
||||
|
||||
@Test void dynamicSphereMembershipSessionAndSourceValidityRecheckOwnedBenefits() throws Exception {
|
||||
var sessions = mock(EquipmentSessions.class); var parties = mock(PartyRoster.class);
|
||||
var bonus = mock(OutgoingDamageBonus.class); var sourceGrant = mock(AutoCloseable.class); var allyGrant = mock(AutoCloseable.class);
|
||||
when(bonus.grant(any(), any(), any(), any())).thenReturn(sourceGrant, allyGrant);
|
||||
var source = mock(Player.class); var ally = mock(Player.class); var outsider = mock(Player.class);
|
||||
var world = mock(org.bukkit.World.class); var sourceId = UUID.randomUUID(); var allyId = UUID.randomUUID(); var outsiderId = UUID.randomUUID();
|
||||
when(source.getUniqueId()).thenReturn(sourceId); when(ally.getUniqueId()).thenReturn(allyId); when(outsider.getUniqueId()).thenReturn(outsiderId);
|
||||
when(source.getLocation()).thenReturn(new Location(world, 0, 64, 0)); when(ally.getLocation()).thenReturn(new Location(world, 7, 64, 0));
|
||||
when(outsider.getLocation()).thenReturn(new Location(world, 2, 64, 0));
|
||||
when(source.getNearbyEntities(8, 8, 8)).thenReturn(List.of(ally, outsider));
|
||||
var sourceEffects = effects(source); var allyEffects = effects(ally); effects(outsider);
|
||||
var sourceActive = new EquipmentSessions.Active(sourceId, UUID.randomUUID(), UUID.randomUUID());
|
||||
var allyActive = new EquipmentSessions.Active(allyId, UUID.randomUUID(), UUID.randomUUID());
|
||||
var outsiderActive = new EquipmentSessions.Active(outsiderId, UUID.randomUUID(), UUID.randomUUID());
|
||||
when(sessions.active(sourceId)).thenReturn(java.util.Optional.of(sourceActive)); when(sessions.current(sourceActive)).thenReturn(true);
|
||||
when(sessions.active(allyId)).thenReturn(java.util.Optional.of(allyActive)); when(sessions.current(allyActive)).thenReturn(true);
|
||||
when(sessions.active(outsiderId)).thenReturn(java.util.Optional.of(outsiderActive)); when(sessions.current(outsiderActive)).thenReturn(true);
|
||||
var party = new PartyRoster.Party(UUID.randomUUID(), sourceId, List.of(sourceId, allyId));
|
||||
when(parties.partyOf(sourceId)).thenReturn(java.util.Optional.of(party)); when(parties.partyOf(allyId)).thenReturn(java.util.Optional.of(party));
|
||||
var execution = mock(AbilityExecution.class); var input = mock(ControlInput.class);
|
||||
when(input.player()).thenReturn(source); when(input.session()).thenReturn(sourceActive);
|
||||
when(execution.input()).thenReturn(input); when(execution.stillCurrent()).thenReturn(true);
|
||||
var rally = new RallyingCry(RallyingCry.Settings.defaults(), sessions, parties, bonus); when(execution.definition()).thenReturn(rally.definition());
|
||||
rally.activate(execution);
|
||||
assertTrue(sourceEffects.containsKey(PotionEffectType.SPEED)); assertTrue(allyEffects.containsKey(PotionEffectType.SPEED));
|
||||
verify(bonus, times(2)).grant(eq(execution), any(), eq(new BigDecimal("20")), any());
|
||||
|
||||
when(ally.getLocation()).thenReturn(new Location(world, 9, 64, 0)); rally.sweep();
|
||||
assertFalse(allyEffects.containsKey(PotionEffectType.SPEED)); verify(allyGrant).close();
|
||||
when(ally.getLocation()).thenReturn(new Location(world, 7, 64, 0)); rally.sweep();
|
||||
assertTrue(allyEffects.containsKey(PotionEffectType.SPEED), "A valid party ally re-entering the active cry is rechecked");
|
||||
when(parties.partyOf(allyId)).thenReturn(java.util.Optional.empty()); rally.sweep();
|
||||
assertFalse(allyEffects.containsKey(PotionEffectType.SPEED));
|
||||
when(parties.partyOf(allyId)).thenReturn(java.util.Optional.of(party)); rally.sweep();
|
||||
assertTrue(allyEffects.containsKey(PotionEffectType.SPEED));
|
||||
when(sessions.current(allyActive)).thenReturn(false); rally.sweep();
|
||||
assertFalse(allyEffects.containsKey(PotionEffectType.SPEED));
|
||||
when(sessions.current(sourceActive)).thenReturn(false); rally.sweep(); verify(execution).finish(); verify(sourceGrant).close();
|
||||
}
|
||||
|
||||
@Test void duplicateRalliesDoNotStackSpeedOrDamageAndPreserveStrongerUnrelatedSpeed() throws Exception {
|
||||
var sessions = mock(EquipmentSessions.class); var parties = mock(PartyRoster.class);
|
||||
var supports = new ArrayList<OutgoingDamageBonus.Support>(); var bonus = new OutgoingDamageBonus(sessions, parties, supports::add);
|
||||
var sourceA = mock(Player.class); var sourceB = mock(Player.class); var ally = mock(Player.class); var enemy = mock(LivingEntity.class);
|
||||
var world = mock(org.bukkit.World.class); UUID a = UUID.randomUUID(), b = UUID.randomUUID(), c = UUID.randomUUID();
|
||||
when(sourceA.getUniqueId()).thenReturn(a); when(sourceB.getUniqueId()).thenReturn(b); when(ally.getUniqueId()).thenReturn(c);
|
||||
when(enemy.getUniqueId()).thenReturn(UUID.randomUUID());
|
||||
for (var player : List.of(sourceA, sourceB, ally)) when(player.getLocation()).thenReturn(new Location(world, 0, 64, 0));
|
||||
when(sourceA.getNearbyEntities(8, 8, 8)).thenReturn(List.of(sourceB, ally));
|
||||
when(sourceB.getNearbyEntities(8, 8, 8)).thenReturn(List.of(sourceA, ally));
|
||||
effects(sourceA); effects(sourceB); var allyEffects = effects(ally);
|
||||
allyEffects.put(PotionEffectType.SPEED, new PotionEffect(PotionEffectType.SPEED, 200, 1));
|
||||
var aa = new EquipmentSessions.Active(a, UUID.randomUUID(), UUID.randomUUID());
|
||||
var ba = new EquipmentSessions.Active(b, UUID.randomUUID(), UUID.randomUUID());
|
||||
var ca = new EquipmentSessions.Active(c, UUID.randomUUID(), UUID.randomUUID());
|
||||
when(sessions.active(a)).thenReturn(java.util.Optional.of(aa)); when(sessions.active(b)).thenReturn(java.util.Optional.of(ba));
|
||||
when(sessions.active(c)).thenReturn(java.util.Optional.of(ca)); when(sessions.current(any())).thenReturn(true);
|
||||
var party = new PartyRoster.Party(UUID.randomUUID(), a, List.of(a, b, c));
|
||||
when(parties.partyOf(a)).thenReturn(java.util.Optional.of(party)); when(parties.partyOf(b)).thenReturn(java.util.Optional.of(party));
|
||||
when(parties.partyOf(c)).thenReturn(java.util.Optional.of(party));
|
||||
var rally = new RallyingCry(RallyingCry.Settings.defaults(), sessions, parties, bonus);
|
||||
var first = execution(sourceA, aa, rally); var second = execution(sourceB, ba, rally); rally.activate(first); rally.activate(second);
|
||||
var hit = hit(ally, enemy, 10); bonus.increase(hit); assertEquals(12, hit.getDamage(), 1.0e-9);
|
||||
assertEquals(1, allyEffects.get(PotionEffectType.SPEED).getAmplifier(), "Stronger unrelated Speed is untouched");
|
||||
rally.close(); bonus.close(); assertEquals(1, allyEffects.get(PotionEffectType.SPEED).getAmplifier());
|
||||
}
|
||||
|
||||
@Test void configuredDefinitionAdminRestartAndFutureCastCoverEverySetting() throws Exception {
|
||||
java.nio.file.Files.writeString(data.resolve("config.yml"),
|
||||
"rallying-cry-cooldown-seconds: 2\nrallying-cry-radius-blocks: 10\nrallying-cry-duration-seconds: 8\n"
|
||||
+ "rallying-cry-speed-amplifier: 1\nrallying-cry-damage-bonus-percent: 30\n");
|
||||
try (var fixture = new FantasyStickLifecycleNativeTest.Fixture(data)) {
|
||||
fixture.plugin.setEnabled(true); var registry = (games.dmg.fantasystick.ability.AbilityRegistry)
|
||||
FantasyStickLifecycleNativeTest.Fixture.field(fixture.plugin, "abilityRegistry");
|
||||
var definition = registry.find("rallying_cry").orElseThrow(); assertEquals(java.time.Duration.ofSeconds(2), definition.cooldown());
|
||||
assertEquals(0, new BigDecimal("10").compareTo(definition.parameters().get("radius")));
|
||||
assertEquals(0, new BigDecimal("8").compareTo(definition.parameters().get("duration-seconds")));
|
||||
assertEquals(new BigDecimal("1"), definition.parameters().get("speed-amplifier"));
|
||||
assertEquals(new BigDecimal("30"), definition.parameters().get("damage-bonus-percent"));
|
||||
var sender = new EquipmentAdminCommandNativeTest.Sender(); sender.allowed = false;
|
||||
assertTrue(fixture.command.tabComplete(sender.port, "fantasystick", new String[]{"config", "rallying-cry-"}).isEmpty());
|
||||
fixture.command.execute(sender.port, "fantasystick", new String[]{"config", "rallying-cry-radius-blocks", "12"});
|
||||
assertTrue(sender.messages.getLast().contains("Permission denied")); sender.allowed = true;
|
||||
assertEquals(List.of("rallying-cry-cooldown-seconds", "rallying-cry-damage-bonus-percent",
|
||||
"rallying-cry-duration-seconds", "rallying-cry-radius-blocks", "rallying-cry-speed-amplifier"),
|
||||
fixture.command.tabComplete(sender.port, "fantasystick", new String[]{"config", "rallying-cry-"}));
|
||||
assertEquals(List.of("10", "8"), fixture.command.tabComplete(sender.port, "fantasystick",
|
||||
new String[]{"config", "rallying-cry-radius-blocks", ""}));
|
||||
fixture.command.execute(sender.port, "fantasystick", new String[]{"config", "rallying-cry-radius-blocks"});
|
||||
assertTrue(sender.messages.getLast().contains("=10"));
|
||||
for (String[] update : List.of(new String[]{"rallying-cry-cooldown-seconds", "3"},
|
||||
new String[]{"rallying-cry-radius-blocks", "12"}, new String[]{"rallying-cry-duration-seconds", "9"},
|
||||
new String[]{"rallying-cry-speed-amplifier", "2"}, new String[]{"rallying-cry-damage-bonus-percent", "40"})) {
|
||||
fixture.command.execute(sender.port, "fantasystick", new String[]{"config", update[0], update[1]});
|
||||
var response = fixture.tasks.poll(5, TimeUnit.SECONDS); assertNotNull(response); response.run();
|
||||
}
|
||||
var configured = registry.find("rallying_cry").orElseThrow();
|
||||
assertEquals(java.time.Duration.ofSeconds(3), configured.cooldown());
|
||||
assertEquals(0, new BigDecimal("12").compareTo(configured.parameters().get("radius")));
|
||||
assertEquals(0, new BigDecimal("9").compareTo(configured.parameters().get("duration-seconds")));
|
||||
assertEquals(new BigDecimal("2"), configured.parameters().get("speed-amplifier"));
|
||||
assertEquals(new BigDecimal("40"), configured.parameters().get("damage-bonus-percent"));
|
||||
|
||||
var caster = new FantasyStickLifecycleNativeTest.Actor(data); var ally = new FantasyStickLifecycleNativeTest.Actor(data);
|
||||
prepareCaster(data, caster, true); fixture.players.put(caster.core.id, caster.player); fixture.players.put(ally.core.id, ally.player);
|
||||
fixture.activate(caster); fixture.activate(ally); party(fixture, caster, ally); nearby(caster, List.of(ally.player));
|
||||
var casterEffects = effects(caster.player); var allyEffects = effects(ally.player); use(fixture, caster); drain(fixture, 2);
|
||||
assertEquals(2, casterEffects.get(PotionEffectType.SPEED).getAmplifier());
|
||||
assertEquals(2, allyEffects.get(PotionEffectType.SPEED).getAmplifier()); assertEquals(180L, fixture.delayedTicks.getLast());
|
||||
var futureTarget = mock(LivingEntity.class); when(futureTarget.getUniqueId()).thenReturn(UUID.randomUUID());
|
||||
var future = hit(ally.player, futureTarget, 10); fixture.dispatcher.accept(future);
|
||||
assertEquals(14, future.getDamage(), 1.0e-9);
|
||||
fixture.command.execute(sender.port, "fantasystick", new String[]{"config", "rallying-cry-duration-seconds", "0"});
|
||||
assertTrue(sender.messages.getLast().contains("invalid"));
|
||||
fixture.command.execute(sender.port, "fantasystick", new String[]{"config", "rallying-cry-radius-blocks", "12", "extra"});
|
||||
assertTrue(sender.messages.getLast().contains("Usage"));
|
||||
fixture.plugin.setEnabled(false); fixture.plugin.setEnabled(true);
|
||||
var reloaded = (games.dmg.fantasystick.ability.AbilityRegistry)
|
||||
FantasyStickLifecycleNativeTest.Fixture.field(fixture.plugin, "abilityRegistry");
|
||||
var saved = reloaded.find("rallying_cry").orElseThrow(); assertEquals(java.time.Duration.ofSeconds(3), saved.cooldown());
|
||||
assertEquals(new BigDecimal("12"), saved.parameters().get("radius")); assertEquals(new BigDecimal("40"), saved.parameters().get("damage-bonus-percent"));
|
||||
assertEquals(0, new BigDecimal("9").compareTo(saved.parameters().get("duration-seconds")));
|
||||
assertEquals(new BigDecimal("2"), saved.parameters().get("speed-amplifier"));
|
||||
}
|
||||
for (String invalid : List.of("rallying-cry-cooldown-seconds: -1\n", "rallying-cry-radius-blocks: 0\n",
|
||||
"rallying-cry-duration-seconds: 0\n", "rallying-cry-speed-amplifier: -1\n",
|
||||
"rallying-cry-damage-bonus-percent: 101\n")) {
|
||||
var directory = data.resolve(Integer.toHexString(invalid.hashCode())); java.nio.file.Files.createDirectories(directory);
|
||||
java.nio.file.Files.writeString(directory.resolve("config.yml"), invalid);
|
||||
try (var fixture = new FantasyStickLifecycleNativeTest.Fixture(directory)) {
|
||||
assertThrows(IllegalArgumentException.class, () -> fixture.plugin.setEnabled(true)); assertTrue(fixture.order.isEmpty());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static AbilityExecution execution(Player player, EquipmentSessions.Active active, RallyingCry rally) {
|
||||
var execution = mock(AbilityExecution.class); var input = mock(ControlInput.class);
|
||||
when(input.player()).thenReturn(player); when(input.session()).thenReturn(active);
|
||||
when(execution.input()).thenReturn(input); when(execution.stillCurrent()).thenReturn(true); when(execution.definition()).thenReturn(rally.definition());
|
||||
return execution;
|
||||
}
|
||||
private static Map<org.bukkit.potion.PotionEffectType, PotionEffect> effects(Player player) {
|
||||
var effects = new HashMap<org.bukkit.potion.PotionEffectType, PotionEffect>();
|
||||
doAnswer(call -> effects.get(call.getArgument(0))).when(player).getPotionEffect(any());
|
||||
doAnswer(call -> { PotionEffect effect = call.getArgument(0); effects.put(effect.getType(), effect); return true; })
|
||||
.when(player).addPotionEffect(any(), eq(true));
|
||||
doAnswer(call -> { effects.remove(call.getArgument(0)); return null; }).when(player).removePotionEffect(any()); return effects;
|
||||
}
|
||||
private static void nearby(FantasyStickLifecycleNativeTest.Actor source, List<? extends org.bukkit.entity.Entity> entities) {
|
||||
doReturn(new Location(source.world, 0, 64, 0)).when(source.player).getLocation();
|
||||
doReturn(new ArrayList<>(entities)).when(source.player).getNearbyEntities(anyDouble(), anyDouble(), anyDouble());
|
||||
for (var entity : entities) if (entity instanceof Player player) doReturn(new Location(source.world, 4, 64, 0)).when(player).getLocation();
|
||||
}
|
||||
private static EntityDamageByEntityEvent hit(Player attacker, LivingEntity target, double amount) {
|
||||
var source = org.bukkit.damage.DamageSource.builder(org.bukkit.damage.DamageType.PLAYER_ATTACK)
|
||||
.withCausingEntity(attacker).withDirectEntity(attacker).build();
|
||||
return new EntityDamageByEntityEvent(attacker, target, EntityDamageEvent.DamageCause.ENTITY_ATTACK, source, amount);
|
||||
}
|
||||
private static void use(FantasyStickLifecycleNativeTest.Fixture fixture, FantasyStickLifecycleNativeTest.Actor actor) {
|
||||
actor.core.inventory.getInventory().setSelectedSlot(2);
|
||||
fixture.dispatcher.accept(new PlayerInteractEvent(actor.player, org.bukkit.event.block.Action.RIGHT_CLICK_AIR,
|
||||
actor.core.inventory.getItem(2), null, org.bukkit.block.BlockFace.SELF, EquipmentSlot.HAND));
|
||||
}
|
||||
private static PartyRoster.Party party(FantasyStickLifecycleNativeTest.Fixture fixture,
|
||||
FantasyStickLifecycleNativeTest.Actor source,
|
||||
FantasyStickLifecycleNativeTest.Actor target) {
|
||||
var parties = PartyLifecycleNativeTest.parties(fixture); var group = parties.create(source.core.id).orElseThrow();
|
||||
assertTrue(parties.accept(target.core.id, parties.invite(source.core.id, group.id(), target.core.id).orElseThrow().id())); return group;
|
||||
}
|
||||
private static void drain(FantasyStickLifecycleNativeTest.Fixture fixture, int count) throws Exception {
|
||||
for (int index = 0; index < count; index++) PartyBookNativeTest.drain(fixture);
|
||||
}
|
||||
private static CharacterStorage storage(FantasyStickLifecycleNativeTest.Fixture fixture) {
|
||||
return (CharacterStorage) FantasyStickLifecycleNativeTest.Fixture.field(fixture.plugin, "characters");
|
||||
}
|
||||
private static ClickEvent.Custom button(FantasyStickLifecycleNativeTest.Actor actor, String label, String ability) {
|
||||
var book = org.bukkit.craftbukkit.inventory.CraftItemStack.asNMSCopy(actor.books.getLast())
|
||||
.get(net.minecraft.core.component.DataComponents.WRITTEN_BOOK_CONTENT); assertNotNull(book);
|
||||
for (var page : book.pages()) { var found = button(page.raw(), label, ability); if (found != null) return found; }
|
||||
throw new AssertionError("Missing " + label + " for " + ability);
|
||||
}
|
||||
private static ClickEvent.Custom button(net.minecraft.network.chat.Component component, String label, String ability) {
|
||||
if (component.getString().equals(label) && component.getStyle().getClickEvent() instanceof ClickEvent.Custom custom
|
||||
&& custom.payload().orElse(null) instanceof net.minecraft.nbt.StringTag payload
|
||||
&& payload.value().contains(":" + ability)) return custom;
|
||||
for (var child : component.getSiblings()) { var found = button(child, label, ability); if (found != null) return found; } return null;
|
||||
}
|
||||
private static void prepareCaster(Path directory, FantasyStickLifecycleNativeTest.Actor actor, boolean learned) throws Exception {
|
||||
java.nio.file.Files.createDirectories(directory); var repository = new YamlCharacterRepository(directory);
|
||||
var old = repository.load(actor.core.record.id()).orElseThrow(); var stats = new EnumMap<>(old.stats());
|
||||
stats.put(Stat.CHR, BigInteger.valueOf(16)); stats.put(Stat.CON, BigInteger.valueOf(12));
|
||||
repository.replace(new CharacterRecord(old.id(), old.name(), old.revision().add(BigInteger.ONE), stats,
|
||||
old.level(), old.experience(), BigInteger.valueOf(learned ? 39 : 40), old.health(),
|
||||
learned ? Set.of("rallying_cry") : old.learned(), learned ? List.of("rallying_cry", "", "", "", "", "", "") : old.loadout(),
|
||||
old.cooldowns(), old.restrictedKills(), false, null, null), old.revision());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user