374 lines
32 KiB
Java
374 lines
32 KiB
Java
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.FrostNova;
|
|
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.Entity;
|
|
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.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 FrostNovaNativeTest {
|
|
@TempDir Path data;
|
|
@BeforeAll static void bootstrap() throws Exception { NativeRuntime.bootstrap(); }
|
|
|
|
@Test void bookAcquisitionEquippedBurstDamagesSlowsEligibleTargetsOnceAndRetainsCooldown() throws Exception {
|
|
try (var fixture = new FantasyStickLifecycleNativeTest.Fixture(data)) {
|
|
fixture.plugin.setEnabled(true);
|
|
assertTrue(java.util.Arrays.stream(EntityDamageEvent.getHandlerList().getRegisteredListeners())
|
|
.anyMatch(listener -> listener.getListener() instanceof FrostNova));
|
|
var caster = new FantasyStickLifecycleNativeTest.Actor(data);
|
|
var ally = new FantasyStickLifecycleNativeTest.Actor(data); var opponent = 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.players.put(opponent.core.id, opponent.player);
|
|
fixture.activate(caster); fixture.activate(ally); fixture.activate(opponent); party(fixture, caster, ally);
|
|
var enemy = mock(LivingEntity.class); when(enemy.getUniqueId()).thenReturn(UUID.randomUUID());
|
|
when(enemy.getLocation()).thenReturn(new Location(caster.world, 3, 64, 0)); when(enemy.getType()).thenReturn(org.bukkit.entity.EntityType.ZOMBIE);
|
|
when(enemy.getName()).thenReturn("Zombie");
|
|
var vetoed = mock(LivingEntity.class); when(vetoed.getUniqueId()).thenReturn(UUID.randomUUID());
|
|
when(vetoed.getLocation()).thenReturn(new Location(caster.world, 2.5, 64, 0));
|
|
doReturn(true).when(caster.player).hasLineOfSight(enemy); doReturn(true).when(caster.player).hasLineOfSight(vetoed);
|
|
locate(caster, ally.player, 2, true); locate(caster, opponent.player, 4, true);
|
|
var ordinary = mock(Player.class); when(ordinary.getUniqueId()).thenReturn(UUID.randomUUID()); locate(caster, ordinary, 2, true);
|
|
doReturn(List.of(enemy, enemy, vetoed, ally.player, ordinary, opponent.player)).when(caster.player)
|
|
.getNearbyEntities(anyDouble(), anyDouble(), anyDouble());
|
|
var enemyEffects = effects(enemy); var vetoedEffects = effects(vetoed);
|
|
var opponentEffects = effects(opponent.player); effects(ally.player); effects(ordinary);
|
|
var damage = new HashMap<UUID, Double>(); damage(fixture, enemy, damage); damage(fixture, vetoed, damage); damage(fixture, opponent.player, damage);
|
|
class Veto implements org.bukkit.event.Listener {
|
|
@org.bukkit.event.EventHandler(priority = org.bukkit.event.EventPriority.LOWEST)
|
|
public void cancel(EntityDamageEvent event) { if (event.getEntity() == vetoed) event.setCancelled(true); }
|
|
}
|
|
fixture.dispatcher.register(new Veto(), fixture.plugin);
|
|
|
|
caster.core.inventory.getInventory().setSelectedSlot(0); PartyBookNativeTest.open(fixture, caster);
|
|
String text = PartyBookNativeTest.text(caster).replaceAll("\\s+", "");
|
|
assertTrue(text.contains("FrostNova")); assertTrue(text.contains("INT16")); assertTrue(text.contains("DEX10"));
|
|
assertFalse(text.contains("Cost:")); assertTrue(text.contains("5")); assertTrue(text.contains("SlownessII"));
|
|
PartyBookNativeTest.click(fixture, caster, button(caster, "[Learn]", "frost_nova")); drain(fixture, 2);
|
|
var viewer = new FantasyStickLifecycleNativeTest.Actor(data);
|
|
viewer.core.inventory.setItemInOffHand(null); viewer.core.location.setWorld(caster.world);
|
|
fixture.players.put(viewer.core.id, viewer.player);
|
|
fixture.particlesCommand.execute(caster.player, "fantasyparticles", new String[]{"all", "off"}); PartyBookNativeTest.drain(fixture);
|
|
caster.particleCalls.clear(); viewer.particleCalls.clear();
|
|
use(fixture, caster); drain(fixture, 2);
|
|
assertTrue(caster.particleCalls.isEmpty(), "The opted-out caster receives no Frost Nova particles");
|
|
assertTrue(viewer.particleCalls.stream().anyMatch(call -> call[0] == org.bukkit.Particle.SNOWFLAKE
|
|
&& call[2].equals(32)), "An enabled nearby viewer retains the Frost Nova burst");
|
|
assertEquals(8, damage.get(enemy.getUniqueId()), 1.0e-9); assertEquals(8, damage.get(opponent.core.id), 1.0e-9);
|
|
assertEquals(2, damage.size(), "Duplicate, cancelled, party and ordinary targets cannot add accepted hits");
|
|
assertEquals(1, caster.messages.stream().filter("Frost Nova did 8 damage to Zombie."::equals).count());
|
|
assertEquals(1, caster.messages.stream().filter(("Frost Nova did 8 damage to "
|
|
+ opponent.core.record.name() + ".")::equals).count());
|
|
assertFalse(vetoedEffects.containsKey(PotionEffectType.SLOWNESS));
|
|
assertEquals(1, enemyEffects.get(PotionEffectType.SLOWNESS).getAmplifier());
|
|
assertEquals(1, opponentEffects.get(PotionEffectType.SLOWNESS).getAmplifier());
|
|
var stored = storage(fixture).find(caster.core.record.id()).get().orElseThrow();
|
|
assertEquals(BigInteger.valueOf(39), stored.unspentPoints()); assertNotNull(stored.cooldowns().get("frost_nova"));
|
|
assertEquals(60L, fixture.delayedTicks.getLast()); fixture.delayed.getLast().run();
|
|
assertFalse(enemyEffects.containsKey(PotionEffectType.SLOWNESS)); assertFalse(opponentEffects.containsKey(PotionEffectType.SLOWNESS));
|
|
}
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
@Test void areaDamageMeasuresTrainingDummyWithoutApplyingOwnedSlowness() throws Exception {
|
|
try (var fixture = new FantasyStickLifecycleNativeTest.Fixture(data)) {
|
|
fixture.plugin.setEnabled(true);
|
|
var caster = new FantasyStickLifecycleNativeTest.Actor(data); prepareCaster(data, caster, true);
|
|
fixture.players.put(caster.core.id, caster.player); fixture.activate(caster);
|
|
UUID worldId = UUID.randomUUID(); when(caster.world.getUID()).thenReturn(worldId);
|
|
var directory = (games.dmg.fantasystick.training.TrainingDummyDirectory)
|
|
FantasyStickLifecycleNativeTest.Fixture.field(fixture.plugin, "trainingDummies");
|
|
var saved = directory.place("Nova Target", worldId, 3, 64, 0, 0, 0).get();
|
|
int chunkX = ((int) Math.floor(saved.x())) >> 4, chunkZ = ((int) Math.floor(saved.z())) >> 4;
|
|
var chunk = mock(org.bukkit.Chunk.class); when(chunk.getWorld()).thenReturn(caster.world);
|
|
when(chunk.getX()).thenReturn(chunkX); when(chunk.getZ()).thenReturn(chunkZ);
|
|
when(chunk.getEntities()).thenReturn(new Entity[0]); when(caster.world.isChunkLoaded(chunkX, chunkZ)).thenReturn(true);
|
|
var dummy = TrainingDummyNativeTest.zombie(new Location(caster.world, 3, 64, 0)); when(dummy.getName()).thenReturn("Nova Target");
|
|
doAnswer(call -> { ((java.util.function.Consumer<org.bukkit.entity.Zombie>) call.getArgument(2)).accept(dummy); return dummy; })
|
|
.when(caster.world).spawn(any(Location.class), eq(org.bukkit.entity.Zombie.class), any(java.util.function.Consumer.class));
|
|
var controller = (games.dmg.fantasystick.training.TrainingDummyController)
|
|
FantasyStickLifecycleNativeTest.Fixture.field(fixture.plugin, "trainingDummyController");
|
|
controller.reconcile(chunk); assertTrue(controller.tagged(dummy)); assertTrue(controller.target(dummy));
|
|
doReturn(new Location(caster.world, 0, 64, 0)).when(caster.player).getLocation();
|
|
doReturn(new Location(caster.world, 0, 65, 0)).when(caster.player).getEyeLocation();
|
|
doReturn(List.of(dummy)).when(caster.player).getNearbyEntities(anyDouble(), anyDouble(), anyDouble());
|
|
doReturn(true).when(caster.player).hasLineOfSight(dummy);
|
|
var events = new ArrayList<EntityDamageEvent>();
|
|
doAnswer(call -> { var source = (org.bukkit.damage.DamageSource) call.getArgument(1);
|
|
var event = new EntityDamageEvent(dummy, EntityDamageEvent.DamageCause.FREEZE, source, (Double) call.getArgument(0));
|
|
events.add(event); fixture.dispatcher.accept(event); return null; })
|
|
.when(dummy).damage(anyDouble(), any(org.bukkit.damage.DamageSource.class));
|
|
use(fixture, caster); drain(fixture, 2);
|
|
assertEquals(1, events.size()); assertTrue(events.getFirst().isCancelled());
|
|
assertEquals(1, caster.messages.stream().filter("Frost Nova did 8 damage to Nova Target."::equals).count(), caster.messages::toString);
|
|
verify(dummy, never()).addPotionEffect(any(PotionEffect.class));
|
|
}
|
|
}
|
|
|
|
@Test void spherePathOrderingCandidateAndTargetBoundsAreDeterministic() {
|
|
var sessions = mock(EquipmentSessions.class); var parties = mock(PartyRoster.class); var source = mock(Player.class);
|
|
var world = mock(org.bukkit.World.class); when(source.getUniqueId()).thenReturn(UUID.randomUUID());
|
|
when(source.getEyeLocation()).thenReturn(new Location(world, 0, 64, 0)); when(source.getLocation()).thenReturn(new Location(world, 0, 64, 0));
|
|
var active = new EquipmentSessions.Active(source.getUniqueId(), UUID.randomUUID(), UUID.randomUUID());
|
|
when(sessions.active(source.getUniqueId())).thenReturn(java.util.Optional.of(active)); when(sessions.current(active)).thenReturn(true);
|
|
var candidates = new ArrayList<Entity>(); var hitOrder = new ArrayList<UUID>();
|
|
for (int index = 0; index < 270; index++) {
|
|
var target = mock(LivingEntity.class); var id = new UUID(0, 300 - index); when(target.getUniqueId()).thenReturn(id);
|
|
double distance = index < 70 ? 1 + index / 100.0 : 4;
|
|
when(target.getLocation()).thenReturn(new Location(world, distance, 64, 0)); when(source.hasLineOfSight(target)).thenReturn(index != 1);
|
|
doAnswer(call -> { hitOrder.add(id); return null; }).when(target).damage(anyDouble(), any(org.bukkit.damage.DamageSource.class));
|
|
effects(target); candidates.add(target);
|
|
}
|
|
when(source.getNearbyEntities(5, 5, 5)).thenReturn(candidates);
|
|
var execution = execution(source, active); var nova = new FrostNova(FrostNova.Settings.defaults(), sessions, parties);
|
|
when(execution.definition()).thenReturn(nova.definition()); nova.activate(execution);
|
|
assertEquals(64, hitOrder.size()); assertFalse(hitOrder.contains(new UUID(0, 299)), "Obstructed target excluded");
|
|
var sorted = new ArrayList<>(hitOrder); sorted.sort(UUID::compareTo);
|
|
assertNotEquals(sorted, hitOrder, "Distance precedes UUID while UUID breaks equal-distance ties");
|
|
verify((LivingEntity) candidates.getLast(), never()).damage(anyDouble(), any(org.bukkit.damage.DamageSource.class));
|
|
verify(execution, atMost(64)).report(any(), any(), any());
|
|
}
|
|
|
|
@Test void exactSphereBoundaryIsIncludedWhileOutsideAndObstructedTargetsAreExcluded() {
|
|
var sessions = mock(EquipmentSessions.class); var parties = mock(PartyRoster.class); var source = mock(Player.class);
|
|
var world = mock(org.bukkit.World.class); when(source.getUniqueId()).thenReturn(UUID.randomUUID());
|
|
when(source.getEyeLocation()).thenReturn(new Location(world, 0, 64, 0));
|
|
var active = new EquipmentSessions.Active(source.getUniqueId(), UUID.randomUUID(), UUID.randomUUID());
|
|
when(sessions.active(source.getUniqueId())).thenReturn(java.util.Optional.of(active)); when(sessions.current(active)).thenReturn(true);
|
|
var boundary = mock(LivingEntity.class); var outside = mock(LivingEntity.class); var blocked = mock(LivingEntity.class);
|
|
for (var target : List.of(boundary, outside, blocked)) when(target.getUniqueId()).thenReturn(UUID.randomUUID());
|
|
when(boundary.getLocation()).thenReturn(new Location(world, 5, 64, 0));
|
|
when(outside.getLocation()).thenReturn(new Location(world, 5.01, 64, 0));
|
|
when(blocked.getLocation()).thenReturn(new Location(world, 4, 64, 0));
|
|
when(source.hasLineOfSight(boundary)).thenReturn(true); when(source.hasLineOfSight(outside)).thenReturn(true);
|
|
when(source.hasLineOfSight(blocked)).thenReturn(true); when(source.getNearbyEntities(5, 5, 5)).thenReturn(List.of(boundary, outside, blocked));
|
|
when(world.rayTraceBlocks(any(Location.class), any(org.bukkit.util.Vector.class), eq(4.0),
|
|
eq(org.bukkit.FluidCollisionMode.NEVER), eq(true))).thenReturn(mock(org.bukkit.util.RayTraceResult.class));
|
|
effects(boundary); effects(outside); effects(blocked);
|
|
var execution = execution(source, active); var nova = new FrostNova(FrostNova.Settings.defaults(), sessions, parties);
|
|
when(execution.definition()).thenReturn(nova.definition()); nova.activate(execution);
|
|
verify(boundary).damage(anyDouble(), any(org.bukkit.damage.DamageSource.class));
|
|
verify(outside, never()).damage(anyDouble(), any(org.bukkit.damage.DamageSource.class));
|
|
verify(blocked, never()).damage(anyDouble(), any(org.bukkit.damage.DamageSource.class));
|
|
}
|
|
|
|
@Test void cancelledDamageAndPartyOrModeRaceApplyNoSlownessOrCredit() throws Exception {
|
|
var sessions = mock(EquipmentSessions.class); var parties = mock(PartyRoster.class); var source = mock(Player.class);
|
|
var target = mock(Player.class); var world = mock(org.bukkit.World.class); UUID sourceId = UUID.randomUUID(), targetId = UUID.randomUUID();
|
|
when(source.getUniqueId()).thenReturn(sourceId); when(target.getUniqueId()).thenReturn(targetId);
|
|
when(source.getEyeLocation()).thenReturn(new Location(world, 0, 64, 0)); when(source.getLocation()).thenReturn(new Location(world, 0, 64, 0));
|
|
when(target.getLocation()).thenReturn(new Location(world, 3, 64, 0)); when(source.hasLineOfSight(target)).thenReturn(true);
|
|
when(source.getNearbyEntities(5, 5, 5)).thenReturn(List.of(target));
|
|
var sourceActive = new EquipmentSessions.Active(sourceId, UUID.randomUUID(), UUID.randomUUID());
|
|
var targetActive = new EquipmentSessions.Active(targetId, UUID.randomUUID(), UUID.randomUUID());
|
|
when(sessions.active(sourceId)).thenReturn(java.util.Optional.of(sourceActive)); when(sessions.current(sourceActive)).thenReturn(true);
|
|
when(sessions.active(targetId)).thenReturn(java.util.Optional.of(targetActive)); when(sessions.current(targetActive)).thenReturn(true);
|
|
var targetEffects = effects(target); var execution = execution(source, sourceActive);
|
|
var nova = new FrostNova(FrostNova.Settings.defaults(), sessions, parties); when(execution.definition()).thenReturn(nova.definition());
|
|
doAnswer(call -> { when(parties.partyOf(sourceId)).thenReturn(java.util.Optional.of(
|
|
new PartyRoster.Party(UUID.randomUUID(), sourceId, List.of(sourceId, targetId)))); return null; })
|
|
.when(target).damage(anyDouble(), any(org.bukkit.damage.DamageSource.class));
|
|
nova.activate(execution); assertTrue(targetEffects.isEmpty()); verify(execution, never()).report(any(), any(), any());
|
|
|
|
when(parties.partyOf(sourceId)).thenReturn(java.util.Optional.empty()); when(sessions.current(targetActive)).thenReturn(false);
|
|
nova.activate(execution); verify(target, times(1)).damage(anyDouble(), any(org.bukkit.damage.DamageSource.class));
|
|
}
|
|
|
|
@Test void ownedSlownessRestoresWeakerPriorStateButPreservesNewerStrongerState() throws Exception {
|
|
var sessions = mock(EquipmentSessions.class); var parties = mock(PartyRoster.class); var source = mock(Player.class);
|
|
var target = mock(LivingEntity.class); var world = mock(org.bukkit.World.class);
|
|
when(source.getUniqueId()).thenReturn(UUID.randomUUID()); when(target.getUniqueId()).thenReturn(UUID.randomUUID());
|
|
when(source.getEyeLocation()).thenReturn(new Location(world, 0, 64, 0)); when(target.getLocation()).thenReturn(new Location(world, 2, 64, 0));
|
|
when(source.hasLineOfSight(target)).thenReturn(true); when(source.getNearbyEntities(5, 5, 5)).thenReturn(List.of(target));
|
|
var active = new EquipmentSessions.Active(source.getUniqueId(), UUID.randomUUID(), UUID.randomUUID());
|
|
when(sessions.active(source.getUniqueId())).thenReturn(java.util.Optional.of(active)); when(sessions.current(active)).thenReturn(true);
|
|
var targetEffects = effects(target); targetEffects.put(PotionEffectType.SLOWNESS,
|
|
new PotionEffect(PotionEffectType.SLOWNESS, 200, 0));
|
|
var execution = execution(source, active); var nova = new FrostNova(FrostNova.Settings.defaults(), sessions, parties);
|
|
when(execution.definition()).thenReturn(nova.definition());
|
|
doAnswer(call -> { var damageSource = (org.bukkit.damage.DamageSource) call.getArgument(1);
|
|
var event = new EntityDamageEvent(target, EntityDamageEvent.DamageCause.FREEZE, damageSource, (Double) call.getArgument(0));
|
|
nova.acceptedDamage(event); return null; }).when(target).damage(anyDouble(), any(org.bukkit.damage.DamageSource.class));
|
|
nova.activate(execution); assertEquals(1, targetEffects.get(PotionEffectType.SLOWNESS).getAmplifier());
|
|
var resource = org.mockito.ArgumentCaptor.forClass(AutoCloseable.class); verify(execution).own(resource.capture());
|
|
resource.getValue().close(); assertEquals(0, targetEffects.get(PotionEffectType.SLOWNESS).getAmplifier());
|
|
|
|
var execution2 = execution(source, active); when(execution2.definition()).thenReturn(nova.definition());
|
|
nova.activate(execution2); targetEffects.put(PotionEffectType.SLOWNESS, new PotionEffect(PotionEffectType.SLOWNESS, 400, 3));
|
|
var resource2 = org.mockito.ArgumentCaptor.forClass(AutoCloseable.class); verify(execution2).own(resource2.capture());
|
|
resource2.getValue().close(); assertEquals(3, targetEffects.get(PotionEffectType.SLOWNESS).getAmplifier());
|
|
}
|
|
|
|
@Test void configuredDefinitionAdminRestartAndFutureCastCoverEverySetting() throws Exception {
|
|
java.nio.file.Files.writeString(data.resolve("config.yml"),
|
|
"frost-nova-base-damage: 7\nfrost-nova-cooldown-seconds: 2\nfrost-nova-radius-blocks: 8\n"
|
|
+ "frost-nova-slowness-amplifier: 2\nfrost-nova-slowness-duration-seconds: 5\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("frost_nova").orElseThrow(); assertEquals(java.time.Duration.ofSeconds(2), definition.cooldown());
|
|
assertEquals(new BigDecimal("7"), definition.parameters().get("base-damage")); assertEquals(new BigDecimal("8"), definition.parameters().get("radius"));
|
|
assertEquals(new BigDecimal("2"), definition.parameters().get("slowness-amplifier"));
|
|
assertEquals(0, new BigDecimal("5").compareTo(definition.parameters().get("slowness-duration-seconds")));
|
|
var sender = new EquipmentAdminCommandNativeTest.Sender(); sender.allowed = false;
|
|
assertTrue(fixture.command.tabComplete(sender.port, "fantasystick", new String[]{"config", "frost-nova-"}).isEmpty());
|
|
fixture.command.execute(sender.port, "fantasystick", new String[]{"config", "frost-nova-radius-blocks", "10"});
|
|
assertTrue(sender.messages.getLast().contains("Permission denied")); sender.allowed = true;
|
|
assertEquals(List.of("frost-nova-base-damage", "frost-nova-cooldown-seconds", "frost-nova-radius-blocks",
|
|
"frost-nova-slowness-amplifier", "frost-nova-slowness-duration-seconds"),
|
|
fixture.command.tabComplete(sender.port, "fantasystick", new String[]{"config", "frost-nova-"}));
|
|
assertEquals(List.of("5", "8"), fixture.command.tabComplete(sender.port, "fantasystick",
|
|
new String[]{"config", "frost-nova-radius-blocks", ""}));
|
|
fixture.command.execute(sender.port, "fantasystick", new String[]{"config", "frost-nova-radius-blocks"});
|
|
assertTrue(sender.messages.getLast().contains("=8"));
|
|
for (String[] update : List.of(new String[]{"frost-nova-base-damage", "9"},
|
|
new String[]{"frost-nova-cooldown-seconds", "3"}, new String[]{"frost-nova-radius-blocks", "10"},
|
|
new String[]{"frost-nova-slowness-amplifier", "3"}, new String[]{"frost-nova-slowness-duration-seconds", "6"})) {
|
|
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("frost_nova").orElseThrow(); assertEquals(java.time.Duration.ofSeconds(3), configured.cooldown());
|
|
assertEquals(new BigDecimal("9"), configured.parameters().get("base-damage"));
|
|
assertEquals(0, new BigDecimal("10").compareTo(configured.parameters().get("radius")));
|
|
assertEquals(new BigDecimal("3"), configured.parameters().get("slowness-amplifier"));
|
|
assertEquals(0, new BigDecimal("6").compareTo(configured.parameters().get("slowness-duration-seconds")));
|
|
|
|
var caster = new FantasyStickLifecycleNativeTest.Actor(data); prepareCaster(data, caster, true);
|
|
fixture.players.put(caster.core.id, caster.player); fixture.activate(caster);
|
|
var enemy = mock(LivingEntity.class); when(enemy.getUniqueId()).thenReturn(UUID.randomUUID());
|
|
when(enemy.getLocation()).thenReturn(new Location(caster.world, 9, 64, 0)); doReturn(true).when(caster.player).hasLineOfSight(enemy);
|
|
doReturn(new Location(caster.world, 0, 64, 0)).when(caster.player).getEyeLocation();
|
|
doReturn(List.of(enemy)).when(caster.player).getNearbyEntities(anyDouble(), anyDouble(), anyDouble());
|
|
var enemyEffects = effects(enemy); var applied = new HashMap<UUID, Double>(); damage(fixture, enemy, applied);
|
|
use(fixture, caster); drain(fixture, 2); assertEquals(12, applied.get(enemy.getUniqueId()), 1.0e-9);
|
|
assertEquals(3, enemyEffects.get(PotionEffectType.SLOWNESS).getAmplifier()); assertEquals(120L, fixture.delayedTicks.getLast());
|
|
fixture.command.execute(sender.port, "fantasystick", new String[]{"config", "frost-nova-radius-blocks", "0"});
|
|
assertTrue(sender.messages.getLast().contains("invalid"));
|
|
fixture.command.execute(sender.port, "fantasystick", new String[]{"config", "frost-nova-radius-blocks", "10", "extra"});
|
|
assertTrue(sender.messages.getLast().contains("Usage"));
|
|
fixture.plugin.setEnabled(false); fixture.plugin.setEnabled(true);
|
|
var saved = ((games.dmg.fantasystick.ability.AbilityRegistry) FantasyStickLifecycleNativeTest.Fixture.field(fixture.plugin, "abilityRegistry"))
|
|
.find("frost_nova").orElseThrow(); assertEquals(new BigDecimal("9"), saved.parameters().get("base-damage"));
|
|
assertEquals(0, new BigDecimal("10").compareTo(saved.parameters().get("radius")));
|
|
assertEquals(new BigDecimal("3"), saved.parameters().get("slowness-amplifier"));
|
|
assertEquals(java.time.Duration.ofSeconds(3), saved.cooldown());
|
|
assertEquals(0, new BigDecimal("6").compareTo(saved.parameters().get("slowness-duration-seconds")));
|
|
}
|
|
for (String invalid : List.of("frost-nova-base-damage: -1\n", "frost-nova-cooldown-seconds: -1\n",
|
|
"frost-nova-radius-blocks: 0\n", "frost-nova-slowness-amplifier: -1\n", "frost-nova-slowness-duration-seconds: 0\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());
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test void arbitraryPrecisionFormulaUsesFloorModifier() {
|
|
assertEquals(BigInteger.valueOf(8), FrostNova.damage(BigInteger.valueOf(16)));
|
|
assertEquals(BigInteger.valueOf(4), FrostNova.damage(BigInteger.valueOf(9)));
|
|
var huge = BigInteger.TEN.pow(500); assertEquals(huge.subtract(BigInteger.TEN).divide(BigInteger.TWO).add(BigInteger.valueOf(5)), FrostNova.damage(huge));
|
|
}
|
|
|
|
private static AbilityExecution execution(Player source, EquipmentSessions.Active active) {
|
|
var execution = mock(AbilityExecution.class); var input = mock(ControlInput.class);
|
|
when(input.player()).thenReturn(source); when(input.session()).thenReturn(active);
|
|
when(execution.input()).thenReturn(input); when(execution.stillCurrent()).thenReturn(true);
|
|
doAnswer(call -> { ((LivingEntity) call.getArgument(0)).damage((Double) call.getArgument(1), (org.bukkit.damage.DamageSource) call.getArgument(2)); return null; })
|
|
.when(execution).damage(any(LivingEntity.class), anyDouble(), any(org.bukkit.damage.DamageSource.class));
|
|
var record = new games.dmg.fantasystick.crafting.CharacterFactory().create(games.dmg.fantasystick.progression.ProgressionRules.defaults());
|
|
var stats = new EnumMap<>(record.stats()); stats.put(Stat.INT, BigInteger.valueOf(16));
|
|
when(execution.character()).thenReturn(new CharacterRecord(active.character(), record.name(), record.revision(), stats,
|
|
record.level(), record.experience(), record.unspentPoints(), record.health(), Set.of("frost_nova"),
|
|
List.of("frost_nova", "", "", "", "", "", ""), record.cooldowns(), record.restrictedKills(), false, null, null));
|
|
return execution;
|
|
}
|
|
private static Map<org.bukkit.potion.PotionEffectType, PotionEffect> effects(LivingEntity target) {
|
|
var effects = new HashMap<org.bukkit.potion.PotionEffectType, PotionEffect>();
|
|
doAnswer(call -> effects.get(call.getArgument(0))).when(target).getPotionEffect(any());
|
|
doAnswer(call -> { PotionEffect effect = call.getArgument(0); effects.put(effect.getType(), effect); return true; })
|
|
.when(target).addPotionEffect(any());
|
|
doAnswer(call -> { effects.remove(call.getArgument(0)); return null; }).when(target).removePotionEffect(any()); return effects;
|
|
}
|
|
private static void damage(FantasyStickLifecycleNativeTest.Fixture fixture, LivingEntity target, Map<UUID, Double> damage) {
|
|
doAnswer(call -> { double amount = call.getArgument(0); org.bukkit.damage.DamageSource source = call.getArgument(1);
|
|
var event = new EntityDamageByEntityEvent(source.getCausingEntity(), target, EntityDamageEvent.DamageCause.FREEZE, source, amount);
|
|
fixture.dispatcher.accept(event); if (!event.isCancelled()) damage.merge(target.getUniqueId(), event.getFinalDamage(), Double::sum); return null;
|
|
}).when(target).damage(anyDouble(), any(org.bukkit.damage.DamageSource.class));
|
|
}
|
|
private static void locate(FantasyStickLifecycleNativeTest.Actor caster, Player target, double distance, boolean visible) {
|
|
doReturn(new Location(caster.world, 0, 64, 0)).when(caster.player).getEyeLocation();
|
|
doReturn(new Location(caster.world, 0, 64, 0)).when(caster.player).getLocation();
|
|
doReturn(new Location(caster.world, distance, 64, 0)).when(target).getLocation(); doReturn(visible).when(caster.player).hasLineOfSight(target);
|
|
}
|
|
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 void 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()));
|
|
}
|
|
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.INT, BigInteger.valueOf(16)); stats.put(Stat.DEX, BigInteger.TEN);
|
|
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("frost_nova") : old.learned(), learned ? List.of("frost_nova", "", "", "", "", "", "") : old.loadout(),
|
|
old.cooldowns(), old.restrictedKills(), false, null, null), old.revision());
|
|
}
|
|
}
|