How to use TaskList method of be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock class

Best MockBukkit code snippet using be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock.TaskList

Source:BukkitSchedulerMock.java Github

copy

Full Screen

...20 private final ThreadPoolExecutor pool = new ThreadPoolExecutor(0, Integer.MAX_VALUE,21 60L, TimeUnit.SECONDS,22 new SynchronousQueue<>());23 private final ExecutorService asyncEventExecutor = Executors.newCachedThreadPool();24 private final TaskList scheduledTasks = new TaskList();25 private final AtomicReference<Exception> asyncException = new AtomicReference<>();26 private long currentTick = 0;27 private int id = 0;28 private long executorTimeout = 60000;29 private static Runnable wrapTask(ScheduledTask task)30 {31 return () ->32 {33 task.setRunning(true);34 task.run();35 task.setRunning(false);36 };37 }38 public void setShutdownTimeout(long timeout)39 {40 this.executorTimeout = timeout;41 }42 /**43 * Shuts the scheduler down. Note that this function will throw exception that where thrown by old asynchronous44 * tasks.45 */46 public void shutdown()47 {48 waitAsyncTasksFinished();49 pool.shutdown();50 if (asyncException.get() != null)51 throw new AsyncTaskException(asyncException.get());52 asyncEventExecutor.shutdownNow();53 }54 public Future<?> executeAsyncEvent(Event event)55 {56 Validate.notNull(event, "Cannot schedule an Event that is null!");57 return asyncEventExecutor.submit(() -> MockBukkit.getMock().getPluginManager().callEvent(event));58 }59 /**60 * Get the current tick of the server.61 *62 * @return The current tick of the server.63 */64 public long getCurrentTick()65 {66 return currentTick;67 }68 /**69 * Perform one tick on the server.70 */71 public void performOneTick()72 {73 currentTick++;74 List<ScheduledTask> oldTasks = scheduledTasks.getCurrentTaskList();75 for (ScheduledTask task : oldTasks)76 {77 if (task.getScheduledTick() == currentTick && !task.isCancelled())78 {79 if (task.isSync())80 {81 wrapTask(task).run();82 }83 else84 {85 pool.submit(wrapTask(task));86 }87 if (task instanceof RepeatingTask && !task.isCancelled())88 {89 ((RepeatingTask) task).updateScheduledTick();90 scheduledTasks.addTask(task);91 }92 }93 }94 }95 /**96 * Perform a number of ticks on the server.97 *98 * @param ticks The number of ticks to executed.99 */100 public void performTicks(long ticks)101 {102 for (long i = 0; i < ticks; i++)103 {104 performOneTick();105 }106 }107 /**108 * Gets the number of async tasks which are awaiting execution.109 *110 * @return The number of async tasks which are pending execution.111 */112 public int getNumberOfQueuedAsyncTasks()113 {114 int queuedAsync = 0;115 for (ScheduledTask task : scheduledTasks.getCurrentTaskList())116 {117 if (task.isSync() || task.isCancelled() || task.isRunning())118 {119 continue;120 }121 queuedAsync++;122 }123 return queuedAsync;124 }125 /**126 * Waits until all asynchronous tasks have finished executing. If you have an asynchronous task that runs127 * indefinitely, this function will never return.128 */129 public void waitAsyncTasksFinished()130 {131 // Make sure all tasks get to execute. (except for repeating asynchronous tasks, they only will fire once)132 while (scheduledTasks.getScheduledTaskCount() > 0)133 {134 performOneTick();135 }136 // Wait for all tasks to finish executing.137 long systemTime = System.currentTimeMillis();138 while (pool.getActiveCount() > 0)139 {140 try141 {142 Thread.sleep(10L);143 }144 catch (InterruptedException e)145 {146 Thread.currentThread().interrupt();147 return;148 }149 if (System.currentTimeMillis() > (systemTime + executorTimeout))150 {151 // If a plugin has left a a runnable going and not cancelled it we could call this bad practice.152 // we should now force interrupt all these runnables forcing them to throw Interrupted Exceptions.153 // if they handle that154 for (ScheduledTask task : scheduledTasks.getCurrentTaskList())155 {156 if (task.isRunning())157 {158 task.cancel();159 cancelTask(task.getTaskId());160 throw new RuntimeException("Forced Cancellation of task owned by "161 + task.getOwner().getName());162 }163 }164 pool.shutdownNow();165 }166 }167 }168 @Override169 public BukkitTask runTask(Plugin plugin, Runnable task)170 {171 return runTaskLater(plugin, task, 1L);172 }173 @Override174 public BukkitTask runTask(Plugin plugin, BukkitRunnable task)175 {176 return runTask(plugin, (Runnable) task);177 }178 @Override179 public BukkitTask runTaskLater(Plugin plugin, Runnable task, long delay)180 {181 delay = Math.max(delay, 1);182 ScheduledTask scheduledTask = new ScheduledTask(id++, plugin, true, currentTick + delay, task);183 scheduledTasks.addTask(scheduledTask);184 return scheduledTask;185 }186 @Override187 public BukkitTask runTaskTimer(Plugin plugin, Runnable task, long delay, long period)188 {189 delay = Math.max(delay, 1);190 RepeatingTask repeatingTask = new RepeatingTask(id++, plugin, true, currentTick + delay, period, task);191 scheduledTasks.addTask(repeatingTask);192 return repeatingTask;193 }194 @Override195 public BukkitTask runTaskTimer(Plugin plugin, BukkitRunnable task, long delay, long period)196 {197 return runTaskTimer(plugin, (Runnable) task, delay, period);198 }199 @Override200 public int scheduleSyncDelayedTask(Plugin plugin, Runnable task, long delay)201 {202 Logger.getLogger(LOGGER_NAME).warning("Consider using runTaskLater instead of scheduleSyncDelayTask");203 return runTaskLater(plugin, task, delay).getTaskId();204 }205 @Override206 public int scheduleSyncDelayedTask(Plugin plugin, BukkitRunnable task, long delay)207 {208 Logger.getLogger(LOGGER_NAME).warning("Consider using runTaskLater instead of scheduleSyncDelayTask");209 return runTaskLater(plugin, (Runnable) task, delay).getTaskId();210 }211 @Override212 public int scheduleSyncDelayedTask(Plugin plugin, Runnable task)213 {214 Logger.getLogger(LOGGER_NAME).warning("Consider using runTask instead of scheduleSyncDelayTask");215 return runTask(plugin, task).getTaskId();216 }217 @Override218 public int scheduleSyncDelayedTask(Plugin plugin, BukkitRunnable task)219 {220 Logger.getLogger(LOGGER_NAME).warning("Consider using runTask instead of scheduleSyncDelayTask");221 return runTask(plugin, (Runnable) task).getTaskId();222 }223 @Override224 public int scheduleSyncRepeatingTask(Plugin plugin, Runnable task, long delay, long period)225 {226 Logger.getLogger(LOGGER_NAME).warning("Consider using runTaskTimer instead of scheduleSyncRepeatingTask");227 return runTaskTimer(plugin, task, delay, period).getTaskId();228 }229 @Override230 public int scheduleSyncRepeatingTask(Plugin plugin, BukkitRunnable task, long delay, long period)231 {232 Logger.getLogger(LOGGER_NAME).warning("Consider using runTaskTimer instead of scheduleSyncRepeatingTask");233 return runTaskTimer(plugin, (Runnable) task, delay, period).getTaskId();234 }235 @Override236 public int scheduleAsyncDelayedTask(Plugin plugin, Runnable task, long delay)237 {238 Logger.getLogger(LOGGER_NAME)239 .warning("Consider using runTaskLaterAsynchronously instead of scheduleAsyncDelayedTask");240 return runTaskLaterAsynchronously(plugin, task, delay).getTaskId();241 }242 @Override243 public int scheduleAsyncDelayedTask(Plugin plugin, Runnable task)244 {245 Logger.getLogger(LOGGER_NAME)246 .warning("Consider using runTaskAsynchronously instead of scheduleAsyncDelayedTask");247 return runTaskAsynchronously(plugin, task).getTaskId();248 }249 @Override250 public int scheduleAsyncRepeatingTask(Plugin plugin, Runnable task, long delay, long period)251 {252 Logger.getLogger(LOGGER_NAME)253 .warning("Consider using runTaskTimerAsynchronously instead of scheduleAsyncRepeatingTask");254 return runTaskTimerAsynchronously(plugin, task, delay, period).getTaskId();255 }256 @Override257 public <T> Future<T> callSyncMethod(Plugin plugin, Callable<T> task)258 {259 // TODO Auto-generated method stub260 throw new UnimplementedOperationException();261 }262 @Override263 public void cancelTask(int taskId)264 {265 scheduledTasks.cancelTask(taskId);266 }267 @Override268 public void cancelTasks(Plugin plugin)269 {270 for (ScheduledTask task : scheduledTasks.getCurrentTaskList())271 {272 if (task.getOwner() != null)273 {274 if (task.getOwner().equals(plugin))275 {276 task.cancel();277 }278 }279 }280 }281 @Override282 public void cancelAllTasks() {283 // TODO Auto-generated method stub284 throw new UnimplementedOperationException();285 }286 @Override287 public boolean isCurrentlyRunning(int taskId)288 {289 // TODO Auto-generated method stub290 throw new UnimplementedOperationException();291 }292 @Override293 public boolean isQueued(int taskId)294 {295 for (ScheduledTask task : scheduledTasks.getCurrentTaskList())296 {297 if (task.getTaskId() == taskId)298 return !task.isCancelled();299 }300 return false;301 }302 @Override303 public List<BukkitWorker> getActiveWorkers()304 {305 // TODO Auto-generated method stub306 throw new UnimplementedOperationException();307 }308 @Override309 public List<BukkitTask> getPendingTasks()310 {311 // TODO Auto-generated method stub312 throw new UnimplementedOperationException();313 }314 @Override315 public BukkitTask runTaskAsynchronously(Plugin plugin, Runnable task)316 {317 ScheduledTask scheduledTask = new ScheduledTask(id++, plugin, false, currentTick, new AsyncRunnable(task));318 pool.execute(wrapTask(scheduledTask));319 return scheduledTask;320 }321 @Override322 public BukkitTask runTaskAsynchronously(Plugin plugin, BukkitRunnable task)323 {324 return runTaskAsynchronously(plugin, (Runnable) task);325 }326 @Override327 public BukkitTask runTaskLater(Plugin plugin, BukkitRunnable task, long delay)328 {329 return runTaskLater(plugin, (Runnable) task, delay);330 }331 @Override332 public BukkitTask runTaskLaterAsynchronously(Plugin plugin, Runnable task, long delay)333 {334 ScheduledTask scheduledTask = new ScheduledTask(id++, plugin, false, currentTick + delay,335 new AsyncRunnable(task));336 scheduledTasks.addTask(scheduledTask);337 return scheduledTask;338 }339 @Override340 public BukkitTask runTaskLaterAsynchronously(Plugin plugin, BukkitRunnable task, long delay)341 {342 return runTaskLaterAsynchronously(plugin, (Runnable) task, delay);343 }344 @Override345 public BukkitTask runTaskTimerAsynchronously(Plugin plugin, Runnable task, long delay, long period)346 {347 RepeatingTask scheduledTask = new RepeatingTask(id++, plugin, false, currentTick + delay, period,348 new AsyncRunnable(task));349 scheduledTasks.addTask(scheduledTask);350 return scheduledTask;351 }352 @Override353 public BukkitTask runTaskTimerAsynchronously(Plugin plugin, BukkitRunnable task, long delay, long period)354 {355 return runTaskTimerAsynchronously(plugin, (Runnable) task, delay, period);356 }357 class AsyncRunnable implements Runnable358 {359 private final Runnable task;360 private AsyncRunnable(Runnable runnable)361 {362 task = runnable;363 }364 @Override365 public void run()366 {367 try368 {369 task.run();370 }371 catch (Exception t)372 {373 asyncException.set(t);374 }375 }376 }377 protected int getActiveRunningCount()378 {379 return pool.getActiveCount();380 }381 private static class TaskList382 {383 private final Map<Integer, ScheduledTask> tasks;384 private TaskList()385 {386 tasks = new ConcurrentHashMap<>();387 }388 /**389 * Add a task but locks the Task list to other writes while adding it.390 *391 * @param task the task to remove.392 * @return true on success.393 */394 private boolean addTask(ScheduledTask task)395 {396 if (task == null)397 {398 return false;399 }400 tasks.put(task.getTaskId(), task);401 return true;402 }403 protected final List<ScheduledTask> getCurrentTaskList()404 {405 List<ScheduledTask> out = new ArrayList<>();406 if (tasks.size() != 0)407 {408 out.addAll(tasks.values());409 }410 return out;411 }412 protected int getScheduledTaskCount()413 {414 int scheduled = 0;415 if (tasks.size() == 0)416 {417 return 0;...

Full Screen

Full Screen

TaskList

Using AI Code Generation

copy

Full Screen

1import static org.junit.jupiter.api.Assertions.*;2import org.bukkit.plugin.Plugin;3import org.junit.jupiter.api.AfterEach;4import org.junit.jupiter.api.BeforeEach;5import org.junit.jupiter.api.Test;6import be.seeseemelk.mockbukkit.MockBukkit;7import be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock;8class BukkitSchedulerMockTest {9 private Plugin plugin;10 private BukkitSchedulerMock scheduler;11 void setUp() {12 plugin = MockBukkit.createMockPlugin();13 scheduler = MockBukkit.getMock().getScheduler();14 }15 void tearDown() {16 MockBukkit.unmock();17 }18 void testTaskList() {19 scheduler.runTask(plugin, () -> {20 System.out.println("Hello world!");21 });22 assertEquals(1, scheduler.getPendingTasks().size());23 }24}25Hi, I am trying to use this method, but I am getting a "Cannot resolve method 'getPendingTasks()'" error. I am using the latest version of MockBukkit, and I have tried using both the Maven and the JAR file. Any ideas?26I have the same problem. I'm using the latest version of MockBukkit (0.16.1) and I'm trying to use the method getPendingTasks() but I'm getting a "Cannot resolve method 'getPendingTasks()'" error. I have tried both the Maven and the JAR file. Any ideas?27I have the same problem. I'm using the latest version of MockBukkit (0.16.1) and I'm trying to use the method getPendingTasks() but I'm getting a "Cannot resolve method 'getPendingTasks()'" error. I have tried both the Maven and the JAR file. Any ideas?28I have the same problem. I'm using the latest version of MockBukkit (0.16.1) and I'm trying to use the method getPendingTasks() but I'm getting a "Cannot resolve method 'getPendingTasks()'" error. I have tried both the Maven and the JAR file. Any ideas?29I have the same problem. I'm using the latest version of MockBukkit (0.16.1) and I'm trying to use the method getPendingTasks() but

Full Screen

Full Screen

TaskList

Using AI Code Generation

copy

Full Screen

1import be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock2import be.seeseemelk.mockbukkit.scheduler.BukkitTaskMock3import be.seeseemelk.mockbukkit.scheduler.SchedulerMock4import org.bukkit.plugin.Plugin5import org.bukkit.scheduler.BukkitTask6import java.util.function.Consumer7public class BukkitSchedulerMock extends SchedulerMock implements BukkitSchedulerMock {8 public BukkitSchedulerMock(@NotNull Plugin plugin) {9 super(plugin);10 }11 public List<BukkitTaskMock> getPendingTasks() {12 return getPendingTasks(it -> true);13 }14 public List<BukkitTaskMock> getPendingTasks(@NotNull Consumer<BukkitTaskMock> filter) {15 return getPendingTasks(it -> {16 filter.accept(it);17 return true;18 });19 }20 public List<BukkitTaskMock> getPendingTasks(@NotNull Predicate<BukkitTaskMock> filter) {21 return getPendingTasks().stream().filter(filter).collect(Collectors.toList());22 }23 public void assertNoPendingTasks() {24 assertNoPendingTasks(it -> true);25 }26 public void assertNoPendingTasks(@NotNull Consumer<BukkitTaskMock> filter) {27 assertNoPendingTasks(it -> {28 filter.accept(it);29 return true;30 });31 }32 public void assertNoPendingTasks(@NotNull Predicate<BukkitTaskMock> filter) {33 List<BukkitTaskMock> tasks = getPendingTasks(filter);34 if (!tasks.isEmpty()) {35 throw new AssertionError("There are pending tasks: " + tasks);36 }37 }38 public List<BukkitTaskMock> getActiveTasks() {39 return getActiveTasks(it -> true);40 }41 public List<BukkitTaskMock> getActiveTasks(@NotNull Consumer<BukkitTaskMock> filter) {42 return getActiveTasks(it -> {43 filter.accept(it);44 return true;45 });46 }47 public List<BukkitTaskMock> getActiveTasks(@NotNull Predicate<BukkitTaskMock> filter) {48 return getActiveTasks().stream().filter(filter).collect(Collectors.toList());49 }50 public void assertNoActiveTasks() {51 assertNoActiveTasks(it -> true);52 }53 public void assertNoActiveTasks(@NotNull Consumer<BukkitTaskMock> filter) {

Full Screen

Full Screen

TaskList

Using AI Code Generation

copy

Full Screen

1public void getTasksForPlugin()2{3 TestPlugin plugin = new TestPlugin();4 BukkitSchedulerMock scheduler = new BukkitSchedulerMock();5 Runnable task = () -> System.out.println("test");6 scheduler.runTaskTimer(plugin, task, 1, 1);7 List<Task> tasks = scheduler.getTasksForPlugin(plugin);8 assertNotNull(tasks);9 assertEquals(1, tasks.size());10}11public void getTasksForPlugin()12{13 TestPlugin plugin = new TestPlugin();14 BukkitSchedulerMock scheduler = new BukkitSchedulerMock();15 Runnable task = () -> System.out.println("test");16 scheduler.runTaskTimer(plugin, task, 1, 1);17 List<Task> tasks = scheduler.getTasksForPlugin(plugin);18 assertNotNull(tasks);19 assertEquals(1, tasks.size());20}

Full Screen

Full Screen

TaskList

Using AI Code Generation

copy

Full Screen

1{2 private BukkitSchedulerMock scheduler;3 private BukkitTaskMock task1;4 private BukkitTaskMock task2;5 private BukkitTaskMock task3;6 public void setUp() throws Exception7 {8 scheduler = new BukkitSchedulerMock();9 task1 = scheduler.runTask(null, () -> {});10 task2 = scheduler.runTaskLater(null, () -> {}, 20);11 task3 = scheduler.runTaskTimer(null, () -> {}, 20, 20);12 }13 public void testTaskList()14 {15 List<BukkitTaskMock> tasks = scheduler.getTasks();16 assertEquals(3, tasks.size());17 assertTrue(tasks.contains(task1));18 assertTrue(tasks.contains(task2));19 assertTrue(tasks.contains(task3));20 }21}

Full Screen

Full Screen

TaskList

Using AI Code Generation

copy

Full Screen

1List<Task> tasks = scheduler.getPendingTasks();2for(Task task : tasks){3 System.out.println(task.getTaskId());4 System.out.println(task.getOwner());5 System.out.println(task.getName());6 System.out.println(task.getDescription());7 System.out.println(task.getState());8 System.out.println(task.getType());9 System.out.println(task.getPeriod());10 System.out.println(task.getDelay());11 System.out.println(task.getNextRun());12 System.out.println(task.getLastRun());13 System.out.println(task.isSync());14}15List<Task> tasks = scheduler.getPendingTasks("owner");16List<Task> tasks = scheduler.getPendingTasks(TaskType.ASYNC_REPEATING_TASK);17List<Task> tasks = scheduler.getPendingTasks(TaskState.WAITING);18List<Task> tasks = scheduler.getPendingTasks(TaskType.ASYNC_REPEATING_TASK, TaskState.WAITING);

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful