@Override public boolean isPrimaryThread() { return Bukkit.isPrimaryThread(); } };
/** * Determine if we are executing the packet event in an asynchronous thread. * <p> * If so, you must synchronize all calls to the Bukkit API. * <p> * Generally, most server packets are executed on the main thread, whereas client packets * are all executed asynchronously. * @return TRUE if we are, FALSE otherwise. */ public boolean isAsync() { return !Bukkit.isPrimaryThread(); }
@Override protected final boolean isPrimaryThread() { return Bukkit.isPrimaryThread(); }
@Override public boolean isMainThread() { return Bukkit.isPrimaryThread(); }
public void checkBlockingAction(String event) { if (!Bukkit.isPrimaryThread()) { return; } String message = "Plugin {0} is performing a blocking I/O operation ({1}) on the main thread. " + "This could affect the server performance, because the thread pauses until it gets the response. " + "Such operations should be performed asynchronous from the main thread. " + "Keep in mind to keep the code thread-safe. "; logCurrentStack(message, event); }
@Override public void run() { try { // Prevent infinite loops if (!Bukkit.isPrimaryThread()) throw new IllegalStateException("Scheduled task was not executed on the main thread!"); sendServerPacket(receiver, packet, copy, filters); } catch (Exception e) { reporter.reportMinimal(library, "sendServerPacket-run()", e); } } });
public static void sendTo(Plugin plugin, String path, CommandSender sender, String... args) { TabooLib.debug(plugin, "TLocaleLoader.sendTo: " + plugin + ", path: " + path + ", sender: " + sender + ", args: " + Arrays.asList(args)); if (Bukkit.isPrimaryThread()) { Optional.ofNullable(map.get(plugin.getName())).ifPresent(localeInstance -> localeInstance.sendTo(path, sender, args)); } else { synchronized (TLocaleLoader.class) { Optional.ofNullable(map.get(plugin.getName())).ifPresent(localeInstance -> localeInstance.sendTo(path, sender, args)); } } }
private void registerFrequentPlayerTask() { if (Bukkit.isPrimaryThread()) { registerFrequentPlayerTaskPrimaryThread(); } else { registerFrequentPlayerTaskAsynchronous(); } }
@Override public void requestPermissionUpdate(final RegisteredPermission registeredPermission) { if (Bukkit.isPrimaryThread()) { requestPermissionUpdatePrimaryThread(registeredPermission); } else { requestPermissionUpdateAsynchronous(registeredPermission); } }
private void requestLazyPermissionsUpdateNonEmpty(final RegisteredPermission... registeredPermissions) { if (Bukkit.isPrimaryThread()) { requestLazyPermissionUpdatePrimaryThread(registeredPermissions); } else { requestLazyPermissionUpdateAsynchronous(registeredPermissions); } }
public void checkThreadSafety(String eventName) { if (Bukkit.isPrimaryThread()) { return; } logCurrentStack("Plugin {0} triggered an synchronous event {1} from an asynchronous Thread. ", eventName); plugin.getLogger().info(THREAD_SAFETY_NOTICE); plugin.getLogger().info("Use runTask* (no Async*), scheduleSync* or callSyncMethod to run on the main thread."); }
public void exempt(final CheckType checkType, final ExemptionContext context) { final PlayerCheckTypeTreeNode node = getNode(checkType); if (node == null) { throw new IllegalArgumentException("Invalid check type."); } if (Bukkit.isPrimaryThread()) { exemptPrimaryThread(node, context); } else { exemptAsynchronous(node, context); } }
public void unexempt(final CheckType checkType, final ExemptionContext context) { final PlayerCheckTypeTreeNode node = getNode(checkType); if (node == null) { throw new IllegalArgumentException("Invalid check type."); } if (Bukkit.isPrimaryThread()) { unexemptPrimaryThread(node, context); } else { unexemptAsynchronous(node, context); } }
public void unexemptAll(final CheckType checkType, final ExemptionContext context) { final PlayerCheckTypeTreeNode node = getNode(checkType); if (node == null) { throw new IllegalArgumentException("Invalid check type."); } if (Bukkit.isPrimaryThread()) { unexemptAllPrimaryThread(node, context); } else { unexemptAllAsynchronous(node, context); } }
public boolean isExempted(final CheckType checkType, final ExemptionContext context) { final PlayerCheckTypeTreeNode node = getNode(checkType); if (node == null) { throw new IllegalArgumentException("Invalid check type."); } if (Bukkit.isPrimaryThread()) { return isExemptedPrimaryThread(node, context); } else { return isExemptedAsynchronous(node, context); } }
private void save(final String message, final File file, final LogType type) { // if the thread isn't primary then it's unsafe to access the file. // schedule it to be done on next tick if (Bukkit.isPrimaryThread()) { sync(message, file, type); } else { new BukkitRunnable() { @Override public void run() { sync(message, file, type); } }.runTask(BetonQuest.getInstance()); } }
@Override public long getStatisticForNormalCategory(UUID player, NormalAchievements category) { validateNotNull(player, "Player"); validateNotNull(category, "Category"); // Underlying structures do not support concurrent write operations and are only modified by the main server // thread. Do not use cache if player is offline. if (Bukkit.isPrimaryThread() && isPlayerOnline(player)) { return cacheManager.getAndIncrementStatisticAmount(category, player, 0); } else { return databaseManager.getNormalAchievementAmount(player, category); } }
@Override public boolean hasPlayerReceivedAchievement(UUID player, String achievementName) { validateNotNull(player, "Player"); validateNotEmpty(achievementName, "Achievement Name"); // Underlying structures do not support concurrent operations and are only used by the main server thread. Not // thread-safe to modify or read them asynchronously. Do not use cached data if player is offline. if (Bukkit.isPrimaryThread() && isPlayerOnline(player)) { return cacheManager.hasPlayerAchievement(player, achievementName); } else { return databaseManager.hasPlayerAchievement(player, achievementName); } }
@Override public long getStatisticForMultipleCategory(UUID player, MultipleAchievements category, String subcategory) { validateNotNull(player, "Player"); validateNotNull(category, "Category"); validateNotEmpty(subcategory, "Sub-category"); // Underlying structures do not support concurrent write operations and are only modified by the main server // thread. Do not use cache if player is offline. if (Bukkit.isPrimaryThread() && isPlayerOnline(player)) { return cacheManager.getAndIncrementStatisticAmount(category, subcategory, player, 0); } else { return databaseManager.getMultipleAchievementAmount(player, category, subcategory); } }
public Hologram createHologram(Plugin plugin, Location source) { Validator.notNull(plugin, "plugin"); Validator.notNull(source, "source"); Validator.notNull(source.getWorld(), "source's world"); Validator.isTrue(Bukkit.isPrimaryThread(), "Async hologram creation"); PluginHologram hologram = new PluginHologram(source, plugin); PluginHologramManager.addHologram(hologram); return hologram; }