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

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

Source:BukkitSchedulerMock.java Github

copy

Full Screen

...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)...

Full Screen

Full Screen

wrapTask

Using AI Code Generation

copy

Full Screen

1BukkitSchedulerMock scheduler = server.getScheduler();2scheduler.wrapTask(() -> {3}).scheduleAsyncDelayedTask(plugin, 5 * 20);4BukkitSchedulerMock scheduler = server.getScheduler();5scheduler.wrapTask(() -> {6}).scheduleSyncDelayedTask(plugin, 5 * 20);7BukkitSchedulerMock scheduler = server.getScheduler();8scheduler.wrapTask(() -> {9}).scheduleSyncRepeatingTask(plugin, 5 * 20, 5 * 20);10BukkitSchedulerMock scheduler = server.getScheduler();11scheduler.wrapTask(() -> {12}).scheduleAsyncRepeatingTask(plugin, 5 * 20, 5 * 20);13World world = server.getWorld("world");14Player player = server.getPlayer("TestPlayer");15Player player = server.getPlayer(UUID.fromString("0f8fad5e-7f51-4f6c-8c33-2bb81234e0e2"));16Player player = server.getPlayerExact("TestPlayer");17Entity entity = server.getEntity(UUID.fromString("0f8fad5e-7f51-4f6c-

Full Screen

Full Screen

wrapTask

Using AI Code Generation

copy

Full Screen

1MockBukkit mockBukkit = MockBukkit.mock();2Server server = mockBukkit.getServer();3MockPlugin plugin = MockBukkit.loadWith(JavaPlugin.class, server, "test-plugin");4BukkitSchedulerMock scheduler = (BukkitSchedulerMock) server.getScheduler();5BukkitRunnable runnable = new BukkitRunnable() {6 public void run() {7 System.out.println("Hello world!");8 }9};10int taskId = scheduler.scheduleSyncRepeatingTask(plugin, runnable, 20L, 20L);11MockTask task = scheduler.wrapTask(taskId);12task.cancel();13mockBukkit.unload();

Full Screen

Full Screen

wrapTask

Using AI Code Generation

copy

Full Screen

1public void testWrapTask() {2 BukkitSchedulerMock scheduler = new BukkitSchedulerMock();3 Task task = scheduler.runTaskTimer(null, () -> System.out.println("Hello world!"), 2, 2);4 WrappedTask wrappedTask = scheduler.wrapTask(task);5 wrappedTask.runFor(5);6 int timesExecuted = wrappedTask.getTimesExecuted();7 long totalTime = wrappedTask.getTotalTime();8 double averageTime = wrappedTask.getAverageTime();9}10public void tearDown() {

Full Screen

Full Screen

wrapTask

Using AI Code Generation

copy

Full Screen

1public void testTaskWrap() {2 BukkitSchedulerMock scheduler = server.getScheduler();3 scheduler.wrapTask(plugin, () -> plugin.getLogger().info("Wrapped task"), 10);4 scheduler.performOneTick();5 verify(plugin, times(1)).getLogger();6}

Full Screen

Full Screen

wrapTask

Using AI Code Generation

copy

Full Screen

1{2 public final MockBukkitRule mockBukkit = new MockBukkitRule();3 public void test()4 {5 BukkitSchedulerMock schedulerMock = mockBukkit.getScheduler();6 schedulerMock.wrapTask(new Runnable()7 {

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