public static MonitorRecord getTaskTimingsByPlugin(Plugin plg) { MonitorRecord monitorRecord = getMonitorRecord("Scheduler", 0L, 0L, 0L); if (plg == null) { return monitorRecord; } for (BukkitTask pendingTask : Bukkit.getScheduler().getPendingTasks()) { if (pendingTask.isSync() && pendingTask.getOwner().equals(plg)) { try { FieldAccessor<Runnable> field = Reflection.getField(pendingTask.getClass(), "task", Runnable.class); Runnable runnable = field.get(pendingTask); if (runnable instanceof SchedulerTaskInjector) { SchedulerTaskInjector schedulerTaskInjector = (SchedulerTaskInjector) runnable; monitorRecord = monitorRecord.merge(getMonitorRecord("Scheduler", schedulerTaskInjector.getTotalTime(), schedulerTaskInjector.getTotalCount(), schedulerTaskInjector.getMaxExecuteTime())); } } catch (Exception e) { e.printStackTrace(); } } } return monitorRecord; }
/** * * @author jiongjionger,Vlvxingze */ // 替换原本的Runnable为带性能统计的版本 public static void inject(Plugin plg) { if (plg != null) { for (BukkitTask pendingTask : Bukkit.getScheduler().getPendingTasks()) { if (pendingTask.isSync() && pendingTask.getOwner().equals(plg)) { try { FieldAccessor<Runnable> field = Reflection.getField(pendingTask.getClass(), "task", Runnable.class); field.set(pendingTask, new SchedulerTaskInjector(plg, field.get(pendingTask))); } catch (Exception e) { e.printStackTrace(); } } } } }
public static void uninject(Plugin plg) { if (plg != null) { for (BukkitTask pendingTask : Bukkit.getScheduler().getPendingTasks()) { if (pendingTask.isSync() && pendingTask.getOwner().equals(plg)) { try { FieldAccessor<Runnable> field = Reflection.getField(pendingTask.getClass(), "task", Runnable.class); Runnable runnable = field.get(pendingTask); if (runnable instanceof SchedulerTaskInjector) { field.set(pendingTask, ((SchedulerTaskInjector) runnable).getRunnable()); } } catch (Exception e) { e.printStackTrace(); } } } } }
public void remove(Player player) { BukkitTask bukkitTask = this.destroyTasks.get(player.getUniqueId()); if (bukkitTask != null) bukkitTask.cancel(); LivingEntity[] livingEntities = this.balloons.get(player.getUniqueId()); if (livingEntities == null) return; this.balloons.remove(player.getUniqueId()); for (LivingEntity livingEntity : livingEntities) livingEntity.remove(); }
@Test public void runTaskTimer() { AtomicInteger count = new AtomicInteger(0); Runnable callback = () -> { count.incrementAndGet(); }; BukkitTask task = scheduler.runTaskTimer(null, callback, 10L, 2L); assertNotNull(task); scheduler.performTicks(9L); assertEquals(0, count.get()); scheduler.performOneTick(); assertEquals(1, count.get()); scheduler.performOneTick(); assertEquals(1, count.get()); scheduler.performOneTick(); assertEquals(2, count.get()); task.cancel(); scheduler.performOneTick(); assertEquals(2, count.get()); }
public static MonitorRecord getTaskTimingsByPlugin(Plugin plg) { MonitorRecord monitorRecord = getMonitorRecord("Scheduler", 0L, 0L, 0L); if (plg == null) { return monitorRecord; } for (BukkitTask pendingTask : Bukkit.getScheduler().getPendingTasks()) { if (pendingTask.isSync() && pendingTask.getOwner().equals(plg)) { try { FieldAccessor<Runnable> field = Reflection.getField(pendingTask.getClass(), "task", Runnable.class); Runnable runnable = field.get(pendingTask); if (runnable instanceof SchedulerTaskInjector) { SchedulerTaskInjector schedulerTaskInjector = (SchedulerTaskInjector) runnable; monitorRecord = monitorRecord .merge(getMonitorRecord("Scheduler", schedulerTaskInjector.getTotalTime(), schedulerTaskInjector.getTotalCount(), schedulerTaskInjector.getMaxExecuteTime())); } } catch (Exception e) { e.printStackTrace(); } } } return monitorRecord; }
private BukkitTask start() { if (checkTask != null) throw new IllegalStateException("Task is already running"); return new BukkitRunnable() { @Override public void run() { String latestVersion = fetchLatestVersion(); UpdateNotifier.this.latestVersion = latestVersion.equals("Error") ? UpdateNotifier.this.latestVersion == null ? currentVersion : UpdateNotifier.this.latestVersion : latestVersion; if (!isUpToDate()) new BukkitRunnable() { @Override public void run() { notifyConsole(); if (plugin.getSettings().getBoolean( "MiscellaneousOptions.UpdateChecker.NotifyAdmins")) notifyAdmins(); } }.runTask(plugin); } }.runTaskTimerAsynchronously(plugin, 0, CHECK_INTERVAL); }
public void createTask() { final Game game = this.game; BukkitTask task = new BukkitRunnable() { @Override public void run() { for (Player player : game.getTeamPlayers()) { if (player.getInventory().contains(getItemMaterial())) { Player target = findTargetPlayer(player); if (target != null) { player.setCompassTarget(target.getLocation()); continue; } } player.setCompassTarget(game.getPlayerTeam(player).getSpawnLocation()); } } }.runTaskTimer(BedwarsRel.getInstance(), 20L, 20L); this.game.addRunningTask(task); }
public void gmOfflineNewTimer(String a) { BukkitTask b = gA.getServer().getScheduler().runTaskLater(gA, new Runnable() { @Override public void run() { if (gA.mC.cGa) { Team c = gA.getServer().getScoreboardManager().getMainScoreboard().getEntryTeam(a); gmBroadcastMsg(gA.mC.cPb.replace("{0}", c.getPrefix() + a + "\u00A7r")); c.removeEntry(a); if (c.getSize() == 0) { gHa.remove(c.getName()); c.unregister(); } } else { gmBroadcastMsg(gA.mC.cPb.replace("{0}", a)); } gHb.remove(a); gHc.remove(a); gmStatusCheck(); } }, ((60 * gA.mC.cPa) * 20) + (gA.mA.i() == 7 ? 0 : gJ[0])); gHc.put(a, b); }
public BukkitTask runTaskTimer(Plugin plugin, Runnable runnable, long delay, long period) { validate(plugin, runnable); if (delay < 0l) { delay = 0; } if (period == 0l) { period = 1l; } else if (period < -1l) { period = -1l; } return handle(new CraftTask(plugin, runnable, nextId(), period), delay); }
public BukkitTask runTaskTimerAsynchronously(Plugin plugin, Runnable runnable, long delay, long period) { validate(plugin, runnable); if (delay < 0l) { delay = 0; } if (period == 0l) { period = 1l; } else if (period < -1l) { period = -1l; } return handle(new CraftAsyncTask(runners, plugin, runnable, nextId(), period), delay); }
public static <ENTITY extends Entity> BukkitTask onNearbyOf(JavaPlugin plugin, Entity entity, double offsetX, double offsetY, double offsetZ, Class<ENTITY> filter, Consumer<ENTITY> callback) { return plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, () -> { entity.getNearbyEntities(offsetX, offsetY, offsetZ).forEach(found -> { if (filter == null || filter.isAssignableFrom(found.getClass())) plugin.getServer().getScheduler().runTask(plugin, () -> callback.accept((ENTITY) found)); }); }, 2L, 2L); }
@Override public BukkitTask startTask(Task.Parameters schedule, Runnable runnable) { final Duration delay = schedule.delay(); final Duration interval = schedule.interval(); return bukkit.runTaskTimer(plugin, runnable, delay == null ? 0 : TimeUtils.toTicks(delay), interval == null ? -1 : TimeUtils.toTicks(interval)); }
@Override public BukkitTask runTaskLater(Plugin plugin, Runnable task, long delay) throws IllegalArgumentException { ScheduledTask scheduledTask = new ScheduledTask(id++, plugin, true, currentTick + delay, task); tasks.add(scheduledTask); return scheduledTask; }
@Override public BukkitTask runTaskTimer(Plugin plugin, Runnable task, long delay, long period) throws IllegalArgumentException { RepeatingTask repeatingTask = new RepeatingTask(id++, plugin, true, currentTick + delay, period, task); tasks.add(repeatingTask); return repeatingTask; }
@Override public BukkitTask runTaskLaterAsynchronously(Plugin plugin, Runnable task, long delay) throws IllegalArgumentException { // TODO Auto-generated method stub throw new UnimplementedOperationException(); }
@Override public BukkitTask runTaskLaterAsynchronously(Plugin plugin, BukkitRunnable task, long delay) throws IllegalArgumentException { // TODO Auto-generated method stub throw new UnimplementedOperationException(); }
@Override public BukkitTask runTaskTimerAsynchronously(Plugin plugin, Runnable task, long delay, long period) throws IllegalArgumentException { // TODO Auto-generated method stub throw new UnimplementedOperationException(); }
@Override public BukkitTask runTaskTimerAsynchronously(Plugin plugin, BukkitRunnable task, long delay, long period) throws IllegalArgumentException { // TODO Auto-generated method stub throw new UnimplementedOperationException(); }
@Test public void runTaskLater() { AtomicBoolean executed = new AtomicBoolean(false); Runnable callback = () -> { executed.set(true); }; BukkitTask task = scheduler.runTaskLater(null, callback, 20L); assertNotNull(task); assertFalse(executed.get()); scheduler.performTicks(10L); assertFalse(executed.get()); scheduler.performTicks(20L); assertTrue(executed.get()); }
/** * Start the task at hand. */ public void start() { while (!values.isEmpty()) { List<T> threadList = new ArrayList<>(); for (int i = 0; i < Math.min(perThread, values.size()); i++) threadList.add(values.remove(0)); BukkitTask[] task = new BukkitTask[1]; task[0] = Bukkit.getScheduler().runTaskTimerAsynchronously(Core.getInstance(), () -> { for (int i = 0; i < Math.min(perTick, threadList.size()); i++) getHandler().accept(threadList.remove(0)); // Only continue if this thread's task is done. if (!threadList.isEmpty()) return; // Cancel this task tasks.remove(task[0]); task[0].cancel(); }, 0L, 1L); tasks.add(task[0]); } checkComplete = Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> { if (!tasks.isEmpty()) return; checkComplete.cancel(); // Don't call again. getOnFinish().run(); }, 0L, 1L); }
/** * Cancel the current reboot, if any. */ public static void cancelReboot() { if (!isRebootScheduled()) return; rebootTasks.forEach(BukkitTask::cancel); // Cancel all tasks. Core.announce(ChatColor.AQUA + "Reboot cancelled."); }
/** * Toggle the entities AI until it should expire. * Does not conflict with other AI toggles. * @param done */ public void toggleAI(Function<LivingEntity, Boolean> done) { LivingEntity le = getLivingEntity(); BukkitTask[] task = new BukkitTask[1]; le.setAI(true); task[0] = Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> { boolean isDone = done.apply(le); le.setAI(!isDone); if (isDone) task[0].cancel(); }, 10L, 20L); }
@Override public void execute() { if (this.target == null) return; Entity e = getEntity(); Location goal = this.target.clone(); goal.setWorld(e.getWorld()); BukkitTask[] task = new BukkitTask[1]; final double walkSpeed = e.getLocation().distance(goal) / (double) getEvent().getTickDelay(); task[0] = Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> stepLocation(e, goal, walkSpeed), 0L, 1L); // Move the entity closer every tick. Bukkit.getScheduler().runTaskLater(Core.getInstance(), task[0]::cancel, getEvent().getTickDelay() - 1); }
public void onEnable() { Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> Bukkit.getOnlinePlayers().stream().filter(p -> !Utils.getRank(p).isAtLeast(EnumRank.MEDIA)).filter(AFK::isAFK).forEach(p -> { BukkitTask kickTask = Bukkit.getScheduler().runTaskLater(Core.getInstance(), () -> Callbacks.cancel(p, Callbacks.ListenerType.CHAT), 35 * 20L); int numA = Utils.nextInt(20); int numB = Utils.nextInt(20); String op = Utils.randChance(2) ? "+" : "-"; int answer = op.equals("+") ? numA + numB : numA - numB; String problem = ChatColor.YELLOW.toString() + numA + " " + op + " " + numB + " = ?"; p.sendTitle(new Title(ChatColor.RED + "AFK CHECK", problem, 20, 100, 20)); p.sendMessage(ChatColor.RED.toString() + ChatColor.BOLD + "AFK CHECK: " + problem); Runnable fail = () -> { if (!p.isOnline()) return; p.kickPlayer(ChatColor.RED + "You were kicked for idling more than " + Configs.getMainConfig().getAfkLimit() + " minutes."); Core.alertStaff(p.getName() + " was kicked for AFKing."); kickTask.cancel(); }; Callbacks.listenForNumber(p, n -> { kickTask.cancel(); if (n == answer) { p.sendMessage(ChatColor.GREEN + "Correct."); markActive(p); } else { p.kickPlayer(ChatColor.RED + "Incorrect answer."); } }, fail); }), 0L, 30 * 20L); }
public static void inject(Plugin plg) { if (plg != null) { for (BukkitTask pendingTask : Bukkit.getScheduler().getPendingTasks()) { if (pendingTask.isSync() && pendingTask.getOwner().equals(plg)) { try { FieldAccessor<Runnable> field = Reflection.getField(pendingTask.getClass(), "task", Runnable.class); field.set(pendingTask, new SchedulerTaskInjector(plg, field.get(pendingTask))); } catch (Exception e) { e.printStackTrace(); } } } } }
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) public void onPlayerQuit(PlayerQuitEvent event) { if (!this.useTaskInstead) return; BukkitTask task = wallBorderTask.remove(event.getPlayer().getUniqueId()); if (task != null) { task.cancel(); } }
@Override public BukkitTask runTask(Plugin plugin, Runnable runnable) throws IllegalArgumentException { TaskedRunnable wrapped = new TaskedRunnable(runnable); BukkitTask bukkitTask = delegate.runTask(plugin, wrapped); wrapped.setTaskID(bukkitTask.getTaskId()); return bukkitTask; }
@Override public BukkitTask runTask(Plugin plugin, BukkitRunnable bukkitRunnable) throws IllegalArgumentException { TaskedRunnable wrapped = new TaskedRunnable(bukkitRunnable); BukkitTask bukkitTask = delegate.runTask(plugin, wrapped); wrapped.setTaskID(bukkitTask.getTaskId()); return bukkitTask; }
@Override public BukkitTask runTaskAsynchronously(Plugin plugin, Runnable runnable) throws IllegalArgumentException { TaskedRunnable wrapped = new TaskedRunnable(runnable); BukkitTask bukkitTask = delegate.runTaskAsynchronously(plugin, wrapped); wrapped.setTaskID(bukkitTask.getTaskId()); return bukkitTask; }
@Override public BukkitTask runTaskAsynchronously(Plugin plugin, BukkitRunnable bukkitRunnable) throws IllegalArgumentException { TaskedRunnable wrapped = new TaskedRunnable(bukkitRunnable); BukkitTask bukkitTask = delegate.runTaskAsynchronously(plugin, wrapped); wrapped.setTaskID(bukkitTask.getTaskId()); return bukkitTask; }
@Override public BukkitTask runTaskLater(Plugin plugin, Runnable runnable, long l) throws IllegalArgumentException { TaskedRunnable wrapped = new TaskedRunnable(runnable); BukkitTask bukkitTask = delegate.runTaskLater(plugin, wrapped, l); wrapped.setTaskID(bukkitTask.getTaskId()); return bukkitTask; }
@Override public BukkitTask runTaskLater(Plugin plugin, BukkitRunnable bukkitRunnable, long l) throws IllegalArgumentException { TaskedRunnable wrapped = new TaskedRunnable(bukkitRunnable); BukkitTask bukkitTask = delegate.runTaskLater(plugin, wrapped, l); wrapped.setTaskID(bukkitTask.getTaskId()); return bukkitTask; }
@Override public BukkitTask runTaskLaterAsynchronously(Plugin plugin, Runnable runnable, long l) throws IllegalArgumentException { TaskedRunnable wrapped = new TaskedRunnable(runnable); BukkitTask bukkitTask = delegate.runTaskLaterAsynchronously(plugin, wrapped, l); wrapped.setTaskID(bukkitTask.getTaskId()); return bukkitTask; }
@Override public BukkitTask runTaskLaterAsynchronously(Plugin plugin, BukkitRunnable bukkitRunnable, long l) throws IllegalArgumentException { TaskedRunnable wrapped = new TaskedRunnable(bukkitRunnable); BukkitTask bukkitTask = delegate.runTaskLaterAsynchronously(plugin, wrapped, l); wrapped.setTaskID(bukkitTask.getTaskId()); return bukkitTask; }
@Override public BukkitTask runTaskTimer(Plugin plugin, Runnable runnable, long l, long l1) throws IllegalArgumentException { TaskedRunnable wrapped = new TaskedRunnable(runnable); BukkitTask bukkitTask = delegate.runTaskTimer(plugin, wrapped, l, l1); wrapped.setTaskID(bukkitTask.getTaskId()); return bukkitTask; }
@Override public BukkitTask runTaskTimer(Plugin plugin, BukkitRunnable bukkitRunnable, long l, long l1) throws IllegalArgumentException { TaskedRunnable wrapped = new TaskedRunnable(bukkitRunnable); BukkitTask bukkitTask = delegate.runTaskTimer(plugin, wrapped, l, l1); wrapped.setTaskID(bukkitTask.getTaskId()); return bukkitTask; }
@Override public BukkitTask runTaskTimerAsynchronously(Plugin plugin, Runnable runnable, long l, long l1) throws IllegalArgumentException { TaskedRunnable wrapped = new TaskedRunnable(runnable); BukkitTask bukkitTask = delegate.runTaskTimerAsynchronously(plugin, wrapped, l, l1); wrapped.setTaskID(bukkitTask.getTaskId()); return bukkitTask; }