BukkitAPI implements MinecraftAPI, AuthorService, NotifyTarget { public BukkitPlayer getAuthorById(final UUID u) { BukkitPlayer bPlayer; bPlayer = playerCache.getIfPresent(u); if (bPlayer == null) { Player onlinePlayer = safeBukkitAPICall(() -> Bukkit.getPlayer(u)); if (onlinePlayer != null) { playerCache.asMap().putIfAbsent(u, new BukkitPlayer(u, this)); } } return playerCache.getIfPresent(u); } @SuppressWarnings("WeakerAccess") protected  BukkitAPI(PwnFilterPlugin p); BukkitPlayer getAuthorById(final UUID u); @Override CommandSender getSenderById(UUID uuid); Console getConsole(); @Override synchronized void reset(); Player getPlayerFromID(UUID id); @Nullable @Override String getPlayerName(final UUID u); @Nullable @Override String getPlayerWorldName(final UUID uuid); @Override Boolean playerIdHasPermission(final UUID u, final String s); @Override boolean burn(final UUID uuid, final int duration, final String messageString); @Override void sendMessage(final UUID uuid, final String message); @Override void sendMessages(final UUID uuid, final List<String> messages); @Override void executePlayerCommand(final UUID uuid, final String command); @Override boolean withdrawMoney(final UUID uuid, final Double amount, final String messageString); @Override void kick(final UUID uuid, final String messageString); @Override void kill(final UUID uuid, final String messageString); @Override void sendConsoleMessage(final String message); @Override void sendConsoleMessage(TextComponent message); @Override void sendConsoleMessages(final List<String> messageList); @Override void sendBroadcast(final String message); @Override void sendBroadCast(TextComponent component); @Override void sendBroadcast(final List<String> messageList); @Override void executeCommand(final String command); @Override boolean globalMute(); @Override void setMutStatus(boolean status); @Override void notifyWithPerm(final String permissionString, final String sendString); @SuppressWarnings("WeakerAccess") static BukkitAudiences bukkitAudiences(); AudienceProvider audiences();  }
@Test public void getAuthorByIdReturnsNullForNoMatch() { server.setPrimaryThread(Thread.currentThread()); server.setScheduler(new WorkingScheduler()); server.setPlayer(null); assertNull(api.getAuthorById(UUID.randomUUID())); }
@Test public void testBasicCacheLoad() { server.setPlayer(testPlayer); server.clearPrimaryThread(); server.setScheduler(new WorkingScheduler()); BukkitPlayer player = api.getAuthorById(testPlayer.getUniqueId()); assertFalse(server.isPrimaryThread()); assertEquals(player.getId(), testPlayer.getUniqueId()); }
@Test public void testAsyncCacheLoad() throws Throwable { server.setPlayer(testPlayer); server.setScheduler(new WorkingScheduler()); final Waiter waiter = new Waiter(); new Thread(() -> { BukkitPlayer player = api.getAuthorById(testPlayer.getUniqueId()); waiter.assertEquals(player.getId(), testPlayer.getUniqueId()); waiter.resume(); }).start(); waiter.await(1000); assertEquals(testPlayer.getUniqueId(), api.getAuthorById(testPlayer.getUniqueId()).getId()); }
@Test(timeout = 100) public void testCacheLoadDoesNotBlock() throws Throwable { final BlockingScheduler scheduler = new BlockingScheduler(); server.setScheduler(scheduler); server.setPlayer(testPlayer); final Thread mainThread = Thread.currentThread(); server.setPrimaryThread(mainThread); Waiter waiter = new Waiter(); final AtomicReference<BukkitPlayer> asyncPlayer = new AtomicReference<>(); scheduler.setWaiter(waiter); new Thread(() -> { asyncPlayer.set(api.getAuthorById(testPlayer.getUniqueId())); waiter.assertEquals(asyncPlayer.get().getId(), testPlayer.getUniqueId()); waiter.resume(); }).start(); waiter.await(); BukkitPlayer myPlayer = api.getAuthorById(testPlayer.getUniqueId()); scheduler.releaseTask(); waiter.await(); assertEquals(asyncPlayer.get(), myPlayer); }
@Test(timeout = 100) public void testPermissionLoadDoesNotBlock() throws Throwable { final BlockingScheduler scheduler = new BlockingScheduler(); server.setScheduler(scheduler); server.setPlayer(testPlayer); final Thread mainThread = Thread.currentThread(); server.setPrimaryThread(mainThread); Waiter waiter = new Waiter(); BukkitPlayer bukkitPlayer; bukkitPlayer = api.getAuthorById(testPlayer.getUniqueId()); scheduler.setWaiter(waiter); new Thread(() -> { waiter.assertFalse(bukkitPlayer.hasPermission("test.permission")); waiter.resume(); }).start(); waiter.await(); BukkitPlayer myPlayer = api.getAuthorById(testPlayer.getUniqueId()); assertFalse(myPlayer.hasPermission("test.permission")); scheduler.releaseTask(); waiter.await(); assertFalse(myPlayer.hasPermission("test.permission")); }