@Override public <T> Future<T> callSyncMethod(Callable<T> task) { if(DynmapPlugin.this.isEnabled()) return getServer().getScheduler().callSyncMethod(DynmapPlugin.this, task); else return null; } @Override
CommandBook.server().getScheduler().callSyncMethod(CommandBook.inst(), new Callable<Boolean>() { @Override public Boolean call() {
/** * Checks whether the player is online by making a call on the server's main thread of execution. * * @param player * @return true if player is online, false otherwise */ private boolean isPlayerOnline(UUID player) { if (Bukkit.isPrimaryThread()) { return Bukkit.getPlayer(player) != null; } // Called asynchronously. To ensure thread safety we must issue a call on the server's main thread of execution. Future<Boolean> onlineCheckFuture = Bukkit.getScheduler().callSyncMethod(advancedAchievements, () -> Bukkit.getPlayer(player) != null); boolean playerOnline = true; try { playerOnline = onlineCheckFuture.get(); } catch (InterruptedException e) { logger.log(Level.SEVERE, "Thread interrupted while checking whether player online:", e); Thread.currentThread().interrupt(); } catch (ExecutionException e) { logger.log(Level.SEVERE, "Unexpected execution exception while checking whether player online:", e); } catch (CancellationException ignored) { // Task can be cancelled when plugin disabled. } return playerOnline; }
/** * Performs the aformentioned removals for a given category. * * @param categoryMap */ private void cleanUpCache(Map<String, CachedStatistic> categoryMap) { for (Entry<String, CachedStatistic> entry : categoryMap.entrySet()) { if (entry.getValue().didPlayerDisconnect() && entry.getValue().isDatabaseConsistent()) { // Player was disconnected at some point in the recent past. Hand over the cleaning to the main server // thread. Bukkit.getScheduler().callSyncMethod(advancedAchievements, () -> { // Check again whether statistic has been written to the database. This is necessary to cover // cases where the player may have reconnected in the meantime. if (entry.getValue().isDatabaseConsistent()) { categoryMap.remove(entry.getKey()); } else { // Get player UUID, which always corresponds to the 36 first characters of the key // regardless of the category type. UUID player = UUID.fromString(entry.getKey().substring(0, 36)); if (Bukkit.getPlayer(player) != null) { entry.getValue().resetDisconnection(); } } return null; }); } } } }
private Map<UUID, WorldData> getWorldData() throws ExecutionException, InterruptedException { //this is not thread-safe and have to run sync Future<Map<UUID, WorldData>> worldFuture = Bukkit.getScheduler() .callSyncMethod(plugin, () -> { List<World> worlds = Bukkit.getWorlds(); Map<UUID, WorldData> worldsData = Maps.newHashMapWithExpectedSize(worlds.size()); for (World world : worlds) { worldsData.put(world.getUID(), WorldData.fromWorld(world)); } return worldsData; }); Map<UUID, WorldData> worldsData = worldFuture.get(); //this can run async because it's thread-safe worldsData.values().parallelStream() .forEach(data -> { Path worldFolder = Bukkit.getWorld(data.getWorldName()).getWorldFolder().toPath(); int worldSize = LagUtils.byteToMega(LagUtils.getFolderSize(plugin.getLogger(), worldFolder)); data.setWorldSize(worldSize); }); return worldsData; }
@Override public <T> T runOnMainThreadAndWait(Callable<T> callable) throws InterruptedException, ExecutionException { if(!CommandHelperPlugin.self.isEnabled()) { throw new CancelCommandException(Implementation.GetServerType().getBranding() + " tried to schedule a task while the plugin was disabled (is the server shutting down?).", Target.UNKNOWN); } return Bukkit.getServer().getScheduler().callSyncMethod(CommandHelperPlugin.self, callable).get(); }
public NanoHTTPD.Response execute(final NanoHTTPD.HTTPSession session) { try { if (async) { return getResponse(session); } // Sync to server thread return Bukkit.getScheduler().callSyncMethod(TotalFreedomMod.plugin(), new Callable<NanoHTTPD.Response>() { @Override public NanoHTTPD.Response call() throws Exception { return getResponse(session); } }).get(); } catch (Exception ex) { FLog.severe(ex); } return null; }
@Override public Item getHandItem(final UserConnection info) { if (HandItemCache.CACHE) { return HandItemCache.getHandItem(info.get(ProtocolInfo.class).getUuid()); } else { try { return Bukkit.getScheduler().callSyncMethod(Bukkit.getPluginManager().getPlugin("ViaVersion"), new Callable<Item>() { @Override public Item call() throws Exception { UUID playerUUID = info.get(ProtocolInfo.class).getUuid(); if (Bukkit.getPlayer(playerUUID) != null) { return HandItemCache.convert(Bukkit.getPlayer(playerUUID).getItemInHand()); } return null; } }).get(10, TimeUnit.SECONDS); } catch (Exception e) { Via.getPlatform().getLogger().severe("Error fetching hand item: " + e.getClass().getName()); if (Via.getManager().isDebug()) e.printStackTrace(); return null; } } } });
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, () -> { Future<Collection<? extends Player>> syncPlayers = Bukkit.getScheduler() .callSyncMethod(plugin, Bukkit::getOnlinePlayers);
this.returnFuture = this.plugin.getServer().getScheduler().callSyncMethod(this.plugin, new getOccupiedChunks(this.plugin.getServer())); final ArrayList<Chunk> chunks = this.returnFuture.get();
private List<PlayerData> getPlayerData(final Map<UUID, WorldData> worldsData) throws InterruptedException, ExecutionException { Future<List<PlayerData>> playerFuture = Bukkit.getScheduler() .callSyncMethod(plugin, () -> { Collection<? extends Player> onlinePlayers = Bukkit.getOnlinePlayers(); List<PlayerData> playerData = Lists.newArrayListWithCapacity(onlinePlayers.size()); for (Player player : onlinePlayers) { UUID worldId = player.getWorld().getUID(); int worldRowId = 0; WorldData worldData = worldsData.get(worldId); if (worldData != null) { worldRowId = worldData.getRowId(); } int lastPing = (int) plugin.getPingManager().getHistory(player.getName()).getLastSample(); String playerName = player.getName(); UUID playerId = player.getUniqueId(); playerData.add(new PlayerData(worldRowId, playerId, playerName, lastPing)); } return playerData; }); return playerFuture.get(); }
@Override public void runOnMainThreadLater(DaemonManager dm, final Runnable r) { if(!CommandHelperPlugin.self.isEnabled()) { throw new CancelCommandException(Implementation.GetServerType().getBranding() + " tried to schedule a task while the plugin was disabled (is the server shutting down?).", Target.UNKNOWN); } Bukkit.getServer().getScheduler().callSyncMethod(CommandHelperPlugin.self, new Callable<Object>() { @Override public Object call() throws Exception { r.run(); return null; } }); }
copy.setFormat(event.getFormat()); Future f = Bukkit.getServer().getScheduler().callSyncMethod(CommandHelperPlugin.self, new Callable() { @Override public Object call() throws Exception {