107 lines
5.5 KiB
Java
107 lines
5.5 KiB
Java
package games.dmg.spigotignis;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static org.mockito.Mockito.*;
|
|
import java.util.UUID;
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.World;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.Event;
|
|
import org.bukkit.event.block.Action;
|
|
import org.bukkit.event.player.PlayerInteractEvent;
|
|
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
class DaggerUseTest {
|
|
@Test
|
|
void visibleTargetUsesBehindDestinationWithoutUnsafeFallbackAndHonorsItemVetoes() {
|
|
var player = mock(Player.class);
|
|
var target = mock(Player.class);
|
|
var id = UUID.randomUUID();
|
|
var receipt = UUID.randomUUID();
|
|
when(player.getUniqueId()).thenReturn(id);
|
|
when(target.getUniqueId()).thenReturn(UUID.randomUUID());
|
|
when(player.isOnline()).thenReturn(true);
|
|
when(target.isOnline()).thenReturn(true);
|
|
when(player.canSee(target)).thenReturn(true);
|
|
when(player.hasLineOfSight(target)).thenReturn(true);
|
|
var world = mock(World.class);
|
|
when(world.getPlayers()).thenReturn(java.util.List.of(player, target));
|
|
when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true);
|
|
when(player.getWorld()).thenReturn(world);
|
|
when(player.getLocation()).thenReturn(new Location(world, 0, 64, 0));
|
|
when(player.getEyeLocation()).thenReturn(new Location(world, 0, 65.62, 0));
|
|
when(target.getLocation()).thenReturn(new Location(world, 0, 64, 5));
|
|
when(target.getEyeLocation()).thenReturn(new Location(world, 0, 65.62, 5));
|
|
var item = mock(ItemStack.class);
|
|
var items = mock(DaggerItems.class);
|
|
when(items.identify(item)).thenReturn(java.util.Optional.of(new ItemToken(id, receipt, 0)));
|
|
var state = new TradableRewards(Track.DAGGER).issue(IgnisState.empty(), id, receipt, true);
|
|
var checked = new AtomicReference<Location>();
|
|
var allowed = new java.util.concurrent.atomic.AtomicBoolean(false);
|
|
var clock = new AtomicLong();
|
|
var use = new DaggerUse(() -> state, items, new DaggerCooldown(clock::get), (actor, destination) -> {
|
|
checked.set(destination); return allowed.get();
|
|
});
|
|
var event = mock(PlayerInteractEvent.class);
|
|
when(event.getPlayer()).thenReturn(player);
|
|
when(event.getItem()).thenReturn(item);
|
|
when(event.getAction()).thenReturn(Action.RIGHT_CLICK_AIR);
|
|
when(event.useItemInHand()).thenReturn(Event.Result.DENY);
|
|
use.onUse(event);
|
|
assertNull(checked.get());
|
|
when(event.useItemInHand()).thenReturn(Event.Result.DEFAULT);
|
|
use.onUse(event);
|
|
assertEquals(new Location(world, 0, 64, 3.5), checked.get());
|
|
verify(player, never()).teleport(any(Location.class), any(TeleportCause.class));
|
|
allowed.set(true);
|
|
clock.set(2_000_000_000L);
|
|
when(target.isInvisible()).thenReturn(true);
|
|
use.onUse(event);
|
|
verify(player).teleport(new Location(world, 0, 64, 3), TeleportCause.PLUGIN);
|
|
when(target.isInvisible()).thenReturn(false);
|
|
when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(false);
|
|
clock.set(4_000_000_000L);
|
|
clearInvocations(player);
|
|
use.onUse(event);
|
|
verify(player, never()).hasLineOfSight(target);
|
|
}
|
|
|
|
@Test
|
|
void rightClickAirUsesPluginTeleportRetainsItemAndKeepsCooldownOnCancellation() {
|
|
var player = mock(Player.class);
|
|
UUID id = UUID.randomUUID(), receipt = UUID.randomUUID();
|
|
when(player.getUniqueId()).thenReturn(id);
|
|
when(player.isOnline()).thenReturn(true);
|
|
var world = mock(World.class);
|
|
when(player.getWorld()).thenReturn(world);
|
|
when(player.getLocation()).thenReturn(new Location(world, 0, 64, 0));
|
|
when(player.getEyeLocation()).thenReturn(new Location(world, 0, 65.62, 0));
|
|
var item = mock(ItemStack.class);
|
|
var items = mock(DaggerItems.class);
|
|
when(items.identify(item)).thenReturn(java.util.Optional.of(new ItemToken(id, receipt, 0)));
|
|
var state = new AtomicReference<>(new TradableRewards(Track.DAGGER).issue(IgnisState.empty(), id, receipt, true));
|
|
var time = new AtomicLong();
|
|
var use = new DaggerUse(state::get, items, new DaggerCooldown(time::get), (actor, destination) -> true);
|
|
var event = mock(PlayerInteractEvent.class);
|
|
when(event.getPlayer()).thenReturn(player);
|
|
when(event.getItem()).thenReturn(item);
|
|
when(event.getAction()).thenReturn(Action.RIGHT_CLICK_AIR);
|
|
when(event.useItemInHand()).thenReturn(Event.Result.DEFAULT);
|
|
when(event.useInteractedBlock()).thenReturn(Event.Result.DENY); // Default air interaction, not an item veto.
|
|
use.onUse(event); use.onUse(event);
|
|
verify(player, times(1)).teleport(new Location(world, 0, 64, 3), TeleportCause.PLUGIN);
|
|
verify(event, atLeastOnce()).setUseItemInHand(Event.Result.DENY);
|
|
verify(event, atLeastOnce()).setUseInteractedBlock(Event.Result.DENY);
|
|
verify(item, never()).setAmount(anyInt());
|
|
time.set(2_000_000_000L);
|
|
state.set(ProgressAdministration.reset(state.get(), id, java.util.Set.of(Track.DAGGER)));
|
|
use.onUse(event);
|
|
verify(player, times(1)).teleport(any(Location.class), eq(TeleportCause.PLUGIN));
|
|
assertFalse(state.get().players().get(id).track(Track.DAGGER).unlocked());
|
|
}
|
|
}
|