Search in sources :

Example 1 with GlowScheduler

use of net.glowstone.scheduler.GlowScheduler in project Glowstone by GlowstoneMC.

the class GlowPlayerTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    PowerMockito.mockStatic(Bukkit.class);
    super.setUp();
    when(Bukkit.getServer()).thenReturn(server);
    when(Bukkit.getItemFactory()).thenReturn(itemFactory);
    opsListFile = File.createTempFile("test-ops-list", "");
    opsList = new UuidListFile(opsListFile);
    when(server.getSessionRegistry()).thenReturn(sessionRegistry);
    when(server.getPluginManager()).thenReturn(pluginManager);
    when(server.getMaterialValueManager()).thenReturn(materialValueManager);
    scheduler = new GlowScheduler(server, worldScheduler);
    when(session.getServer()).thenReturn(server);
    when(server.getScheduler()).thenReturn(scheduler);
    when(server.getOpsList()).thenReturn(opsList);
    when(server.getPlayerStatisticIoService()).thenReturn(statisticIoService);
    when(world.getSpawnLocation()).thenReturn(location);
    when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(block);
    when(world.getBlockAt(any(Location.class))).thenReturn(block);
    when(world.getChunkManager()).thenReturn(chunkManager);
    when(world.newChunkLock(anyString())).thenReturn(chunkLock);
    when(block.getLocation()).thenReturn(location);
    when(block.getType()).thenReturn(Material.AIR);
    when(block.getRelative(any(BlockFace.class))).thenReturn(block);
    when(block.getMaterialValues()).thenCallRealMethod();
    when(block.getWorld()).thenReturn(world);
    fishingRodItem = new ItemStack(Material.FISHING_ROD);
    entity = entityCreator.apply(location);
    entity.setItemInHand(fishingRodItem);
    entity.setDigging(null);
    entity.setLevel(1);
    when(session.getPlayer()).thenReturn(entity);
    when(session.getChannel()).thenReturn(null);
    when(world.getRawPlayers()).thenReturn(Collections.singletonList(entity));
    EventFactory.setInstance(eventFactory);
    when(eventFactory.callEvent(any(Event.class))).thenAnswer(returnsFirstArg());
}
Also used : UuidListFile(net.glowstone.util.bans.UuidListFile) BlockFace(org.bukkit.block.BlockFace) PlayerStatisticIncrementEvent(org.bukkit.event.player.PlayerStatisticIncrementEvent) Event(org.bukkit.event.Event) PlayerExpChangeEvent(org.bukkit.event.player.PlayerExpChangeEvent) PlayerLevelChangeEvent(org.bukkit.event.player.PlayerLevelChangeEvent) ItemStack(org.bukkit.inventory.ItemStack) GlowScheduler(net.glowstone.scheduler.GlowScheduler) Location(org.bukkit.Location) Before(org.junit.Before)

Example 2 with GlowScheduler

use of net.glowstone.scheduler.GlowScheduler in project Glowstone by GlowstoneMC.

the class EventFactory method callEvent.

/**
 * Calls an event through the plugin manager.
 *
 * @param event The event to throw.
 * @param <T> The type of the event.
 * @return the called event
 */
public <T extends Event> T callEvent(T event) {
    if (event.getHandlers().getRegisteredListeners().length == 0) {
        return event;
    }
    Server server = ServerProvider.getServer();
    if (event.isAsynchronous()) {
        server.getPluginManager().callEvent(event);
        return event;
    } else {
        FutureTask<T> task = new FutureTask<>(() -> server.getPluginManager().callEvent(event), event);
        BukkitScheduler scheduler = server.getScheduler();
        ((GlowScheduler) scheduler).scheduleInTickExecution(task);
        try {
            return task.get();
        } catch (InterruptedException e) {
            ConsoleMessages.Warn.Event.INTERRUPTED.log(e, event.getClass().getSimpleName());
            return event;
        } catch (CancellationException e) {
            ConsoleMessages.Warn.Event.SHUTDOWN.log(event.getClass().getSimpleName());
            return event;
        } catch (ExecutionException e) {
            // No checked exceptions declared for callEvent
            throw new RuntimeException(e);
        }
    }
}
Also used : Server(org.bukkit.Server) FutureTask(java.util.concurrent.FutureTask) CancellationException(java.util.concurrent.CancellationException) BukkitScheduler(org.bukkit.scheduler.BukkitScheduler) ExecutionException(java.util.concurrent.ExecutionException) GlowScheduler(net.glowstone.scheduler.GlowScheduler)

Aggregations

GlowScheduler (net.glowstone.scheduler.GlowScheduler)2 CancellationException (java.util.concurrent.CancellationException)1 ExecutionException (java.util.concurrent.ExecutionException)1 FutureTask (java.util.concurrent.FutureTask)1 UuidListFile (net.glowstone.util.bans.UuidListFile)1 Location (org.bukkit.Location)1 Server (org.bukkit.Server)1 BlockFace (org.bukkit.block.BlockFace)1 Event (org.bukkit.event.Event)1 PlayerExpChangeEvent (org.bukkit.event.player.PlayerExpChangeEvent)1 PlayerLevelChangeEvent (org.bukkit.event.player.PlayerLevelChangeEvent)1 PlayerStatisticIncrementEvent (org.bukkit.event.player.PlayerStatisticIncrementEvent)1 ItemStack (org.bukkit.inventory.ItemStack)1 BukkitScheduler (org.bukkit.scheduler.BukkitScheduler)1 Before (org.junit.Before)1