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;
}
}
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")));
}
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")));
}
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);
}
}
}
Aggregations