private PipeTask addTaskToQueue(final PipeTask task, final long delay) { Validate.notNull(task.getOwner(), "Plugin cannot be null"); Validate.notNull(task, "Task cannot be null"); if (!task.getOwner().isEnabled()) { throw new IllegalPluginAccessException("Plugin attempted to register task while disabled"); } task.setNextRun(currentTick + delay); queuedTasks.add(task); return task; }
public void doTick(int currentTick) { this.currentTick = currentTick; Iterator<PipeTask> iterator = queuedTasks.iterator(); while (iterator.hasNext()) { PipeTask task = iterator.next(); long period = task.getPeriod(); if (period == -2) { iterator.remove(); continue; } if (task.getNextRun() <= currentTick) { runningTasks.put(task.getTaskId(), task); if (period == -1) { iterator.remove(); } else { task.setNextRun(currentTick + task.getPeriod()); } } } for (PipeTask runningTask : runningTasks.values()) { if (runningTask.isSync()) { try { runningTask.run(); } catch (final Throwable throwable) { runningTask .getOwner() .getLogger() .log( Level.WARNING, String.format( "Task #%s for %s generated an exception", runningTask.getTaskId(), runningTask.getOwner().getDescription().getFullName()), throwable); } } else { executor.execute(runningTask); } } runningTasks.clear(); }