Search in sources :

Example 11 with BukkitScheduler

use of org.bukkit.scheduler.BukkitScheduler in project Minigames by AddstarMC.

the class ResourcePack method validate.

private void validate() {
    BukkitScheduler scheduler = Bukkit.getScheduler();
    try {
        scheduler.runTaskAsynchronously(Minigames.getPlugin(), () -> {
            synchronized (this.local) {
                if (this.local.exists()) {
                    // set the local hash;
                    try (final FileInputStream fis = new FileInputStream(this.local)) {
                        this.hash = this.getSH1Hash(fis);
                    } catch (final IOException e) {
                        e.printStackTrace();
                    }
                    // Validate the remote file hash = local.
                    final File temp;
                    try (final InputStream in = this.url.openStream()) {
                        temp = File.createTempFile(this.name, ext);
                        Files.copy(in, temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
                    } catch (final IOException e) {
                        Minigames.log().warning(e.getMessage());
                        e.printStackTrace();
                        this.valid = false;
                        return;
                    }
                    try (final FileInputStream fis = new FileInputStream(temp)) {
                        final byte[] has = this.getSH1Hash(fis);
                        if (Arrays.equals(has, this.hash)) {
                            Minigames.log().info("Resource Pack: " + this.name + " passed external validation");
                            this.valid = true;
                            return;
                        }
                    } catch (final IOException e) {
                        e.printStackTrace();
                        this.valid = false;
                        return;
                    }
                    // Local did not match hash on remote so copy the remote over the local.
                    try (final FileInputStream fis = new FileInputStream(temp)) {
                        Files.copy(fis, this.local.toPath(), StandardCopyOption.REPLACE_EXISTING);
                    } catch (final IOException e) {
                        e.printStackTrace();
                    }
                    // set the new hash as long as its not null its valid
                    this.setLocalHash();
                } else {
                    this.download(this.local);
                    this.setLocalHash();
                    this.valid = true;
                }
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        this.valid = false;
    }
}
Also used : BukkitScheduler(org.bukkit.scheduler.BukkitScheduler) MalformedURLException(java.net.MalformedURLException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 12 with BukkitScheduler

use of org.bukkit.scheduler.BukkitScheduler in project AuthMeReloaded by AuthMe.

the class PurgeTaskTest method shouldStopTaskAndInformConsoleUser.

@Test
public void shouldStopTaskAndInformConsoleUser() {
    // given
    Set<String> names = newHashSet("name1", "name2");
    PurgeTask task = new PurgeTask(purgeService, permissionsManager, null, names, new OfflinePlayer[0]);
    BukkitTask bukkitTask = mock(BukkitTask.class);
    given(bukkitTask.getTaskId()).willReturn(10049);
    ReflectionTestUtils.setField(BukkitRunnable.class, task, "task", bukkitTask);
    Server server = mock(Server.class);
    BukkitScheduler scheduler = mock(BukkitScheduler.class);
    given(server.getScheduler()).willReturn(scheduler);
    ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
    ConsoleCommandSender consoleSender = mock(ConsoleCommandSender.class);
    given(server.getConsoleSender()).willReturn(consoleSender);
    // Run for the first time -> results in empty names list
    task.run();
    // when
    task.run();
    // then
    verify(scheduler).cancelTask(task.getTaskId());
    verify(consoleSender).sendMessage(argThat(containsString("Database has been purged successfully")));
}
Also used : Server(org.bukkit.Server) BukkitTask(org.bukkit.scheduler.BukkitTask) BukkitScheduler(org.bukkit.scheduler.BukkitScheduler) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) Test(org.junit.Test)

Example 13 with BukkitScheduler

use of org.bukkit.scheduler.BukkitScheduler in project AuthMeReloaded by AuthMe.

the class PurgeTaskTest method shouldStopTaskAndInformSenderUponCompletion.

@Test
public void shouldStopTaskAndInformSenderUponCompletion() {
    // given
    Set<String> names = newHashSet("name1", "name2");
    Player sender = mock(Player.class);
    UUID uuid = UUID.randomUUID();
    given(sender.getUniqueId()).willReturn(uuid);
    PurgeTask task = new PurgeTask(purgeService, permissionsManager, sender, names, new OfflinePlayer[0]);
    BukkitTask bukkitTask = mock(BukkitTask.class);
    given(bukkitTask.getTaskId()).willReturn(10049);
    ReflectionTestUtils.setField(BukkitRunnable.class, task, "task", bukkitTask);
    Server server = mock(Server.class);
    BukkitScheduler scheduler = mock(BukkitScheduler.class);
    given(server.getScheduler()).willReturn(scheduler);
    ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
    given(server.getPlayer(uuid)).willReturn(sender);
    // Run for the first time -> results in empty names list
    task.run();
    // when
    task.run();
    // then
    verify(scheduler).cancelTask(task.getTaskId());
    verify(sender).sendMessage(argThat(containsString("Database has been purged successfully")));
}
Also used : Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) Server(org.bukkit.Server) BukkitTask(org.bukkit.scheduler.BukkitTask) BukkitScheduler(org.bukkit.scheduler.BukkitScheduler) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UUID(java.util.UUID) Test(org.junit.Test)

Example 14 with BukkitScheduler

use of org.bukkit.scheduler.BukkitScheduler 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

BukkitScheduler (org.bukkit.scheduler.BukkitScheduler)14 Server (org.bukkit.Server)4 BukkitTask (org.bukkit.scheduler.BukkitTask)3 Test (org.junit.Test)3 Player (org.bukkit.entity.Player)2 PluginDescriptionFile (org.bukkit.plugin.PluginDescriptionFile)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 IDisableListener (fr.neatmonster.nocheatplus.components.registry.feature.IDisableListener)1 IPostRegisterRunnable (fr.neatmonster.nocheatplus.components.registry.feature.IPostRegisterRunnable)1 ConfigFile (fr.neatmonster.nocheatplus.config.ConfigFile)1 Counters (fr.neatmonster.nocheatplus.stats.Counters)1 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 CancellationException (java.util.concurrent.CancellationException)1