Best MockBukkit code snippet using be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock.performTicks
Source:BukkitSchedulerMockTest.java
...30 {31 assertEquals(0, scheduler.getCurrentTick());32 scheduler.performOneTick();33 assertEquals(1, scheduler.getCurrentTick());34 scheduler.performTicks(2L);35 assertEquals(3, scheduler.getCurrentTick());36 }37 @Test38 void runTask()39 {40 AtomicBoolean executed = new AtomicBoolean(false);41 Runnable task = () -> executed.set(true);42 scheduler.runTask(null, task);43 assertFalse(executed.get());44 scheduler.performOneTick();45 assertTrue(executed.get());46 }47 @Test48 void runTaskLater()49 {50 AtomicBoolean executed = new AtomicBoolean(false);51 Runnable callback = () -> executed.set(true);52 BukkitTask task = scheduler.runTaskLater(null, callback, 20L);53 assertNotNull(task);54 assertFalse(executed.get());55 scheduler.performTicks(10L);56 assertFalse(executed.get());57 scheduler.performTicks(20L);58 assertTrue(executed.get());59 }60 @Test61 void runTaskTimer()62 {63 AtomicInteger count = new AtomicInteger(0);64 Runnable callback = () -> count.incrementAndGet();65 BukkitTask task = scheduler.runTaskTimer(null, callback, 10L, 2L);66 assertNotNull(task);67 scheduler.performTicks(9L);68 assertEquals(0, count.get());69 scheduler.performOneTick();70 assertEquals(1, count.get());71 scheduler.performOneTick();72 assertEquals(1, count.get());73 scheduler.performOneTick();74 assertEquals(2, count.get());75 task.cancel();76 scheduler.performOneTick();77 assertEquals(2, count.get());78 }79 private BukkitTask testTask; /* This is needed because a lambda can't reach writable closures */80 @Test81 void runTaskTimer_SelfCancelling()82 {83 AtomicInteger count = new AtomicInteger(0);84 testTask = scheduler.runTaskTimer(null, () ->85 {86 if (count.incrementAndGet() == 2)87 testTask.cancel();88 }, 1, 1);89 assertEquals(0, count.get());90 scheduler.performOneTick();91 assertEquals(1, count.get());92 scheduler.performOneTick();93 assertEquals(2, count.get());94 scheduler.performOneTick();95 assertEquals(2, count.get());96 }97 @Test98 void runTaskTimer_ZeroDelay_DoesntExecuteTaskImmediately()99 {100 AtomicInteger count = new AtomicInteger(0);101 Runnable callback = () -> count.incrementAndGet();102 scheduler.runTaskTimer(null, callback, 0, 2L);103 assertEquals(0, count.get());104 scheduler.performTicks(1L);105 assertEquals(1, count.get());106 }107 @Test108 void runTaskAsynchronously_TaskExecutedOnSeperateThread() throws InterruptedException, BrokenBarrierException, TimeoutException109 {110 final Thread mainThread = Thread.currentThread();111 CyclicBarrier barrier = new CyclicBarrier(2);112 scheduler.runTaskAsynchronously(null, () ->113 {114 assertNotEquals(mainThread, Thread.currentThread());115 try116 {117 barrier.await(3L, TimeUnit.SECONDS);118 }119 catch (InterruptedException | BrokenBarrierException | TimeoutException e)120 {121 throw new RuntimeException(e);122 }123 });124 barrier.await(3L, TimeUnit.SECONDS);125 }126 @Test127 void runTaskTimerAsynchronously_TaskExecutedOnSeperateThread() throws InterruptedException, BrokenBarrierException, TimeoutException128 {129 final Thread mainThread = Thread.currentThread();130 CyclicBarrier barrier = new CyclicBarrier(2);131 AtomicInteger count = new AtomicInteger();132 testTask = scheduler.runTaskTimerAsynchronously(null, () ->133 {134 assertNotEquals(mainThread, Thread.currentThread());135 try136 {137 if (count.incrementAndGet() == 2)138 testTask.cancel();139 barrier.await(3L, TimeUnit.SECONDS);140 }141 catch (InterruptedException | BrokenBarrierException | TimeoutException e)142 {143 testTask.cancel();144 throw new RuntimeException(e);145 }146 }, 2L, 1L);147 assertEquals(0, count.get());148 assertTrue(scheduler.isQueued(testTask.getTaskId()));149 scheduler.performTicks(1L);150 assertTrue(scheduler.isQueued(testTask.getTaskId()));151 assertEquals(0, count.get());152 scheduler.performTicks(1L);153 barrier.await(3L, TimeUnit.SECONDS);154 assertTrue(scheduler.isQueued(testTask.getTaskId()));155 assertEquals(1, count.get());156 scheduler.performTicks(1L);157 barrier.await(3L, TimeUnit.SECONDS);158 assertFalse(scheduler.isQueued(testTask.getTaskId()));159 assertEquals(2, count.get());160 scheduler.performTicks(1L);161 assertFalse(scheduler.isQueued(testTask.getTaskId()));162 assertEquals(2, count.get());163 }164 @Test165 public void cancellingAsyncTaskDecreasesNumberOfQueuedAsyncTasks()166 {167 assertEquals(0, scheduler.getNumberOfQueuedAsyncTasks());168 BukkitTask task = scheduler.runTaskLaterAsynchronously(null, () -> {}, 1);169 assertEquals(1, scheduler.getNumberOfQueuedAsyncTasks());170 task.cancel();171 assertEquals(0, scheduler.getNumberOfQueuedAsyncTasks());172 }173 @Test174 public void cancellingAllTaskByPlugin()175 {176 MockBukkit.mock();177 MockBukkit.load(TestPlugin.class);178 Plugin plugin = MockBukkit.getMock().getPluginManager().getPlugin("MockBukkitTestPlugin");179 BukkitSchedulerMock scheduler1 = MockBukkit.getMock().getScheduler();180 assertEquals(0, scheduler1.getNumberOfQueuedAsyncTasks());181 scheduler1.runTaskLaterAsynchronously(plugin, () -> {}, 5);182 scheduler1.runTaskLaterAsynchronously(plugin, () -> {}, 10);183 BukkitTask task = scheduler1.runTaskLaterAsynchronously(null, () -> {}, 5);184 assertEquals(3, scheduler1.getNumberOfQueuedAsyncTasks());185 scheduler1.cancelTasks(plugin);186 assertEquals(1, scheduler1.getNumberOfQueuedAsyncTasks());187 scheduler1.cancelTask(task.getTaskId());188 assertEquals(0, scheduler1.getNumberOfQueuedAsyncTasks());189 MockBukkit.unmock();190 }191 @Test192 public void longScheduledRunningTask_Throws_RunTimeException()193 {194 assertEquals(0, scheduler.getNumberOfQueuedAsyncTasks());195 scheduler.runTaskAsynchronously(null, () ->196 {197 while (true)198 {199 try200 {201 Thread.sleep(10L);202 }203 catch (InterruptedException e)204 {205 throw new RuntimeException(e);206 }207 }208 });209 scheduler.runTaskLaterAsynchronously(null, () ->210 {211 while (true)212 {213 try214 {215 Thread.sleep(10L);216 }217 catch (InterruptedException e)218 {219 throw new RuntimeException(e);220 }221 }222 }, 2);223 assertEquals(1, scheduler.getActiveRunningCount());224 scheduler.performOneTick();225 assertEquals(1, scheduler.getActiveRunningCount());226 scheduler.performOneTick();227 assertEquals(2, scheduler.getActiveRunningCount());228 scheduler.performOneTick();229 assertEquals(2, scheduler.getActiveRunningCount());230 scheduler.setShutdownTimeout(300);231 assertThrows(RuntimeException.class, ()->232 {233 scheduler.shutdown();234 });235 }236 @Test237 public void longRunningTask_Throws_RunTimeException()238 {239 assertEquals(0, scheduler.getNumberOfQueuedAsyncTasks());240 final AtomicBoolean alive = new AtomicBoolean(true);241 testTask = scheduler.runTaskAsynchronously(null, () ->242 {243 while (alive.get())244 {245 if (testTask.isCancelled())246 {247 alive.set(false);248 }249 try250 {251 Thread.sleep(10L);252 }253 catch (InterruptedException e)254 {255 alive.set(false);256 String message = "Interrupted";257 throw new RuntimeException(message, e);258 }259 }260 });261 assertTrue(alive.get());262 assertEquals(1, scheduler.getActiveRunningCount());263 scheduler.performTicks(10);264 scheduler.setShutdownTimeout(10);265 assertThrows(RuntimeException.class,266 () ->267 {268 scheduler.shutdown();269 });270 }271}...
Source:EnchantingTableUtilTest.java
...89 };90 assertDoesNotThrow(() -> EnchantingTable.updateButtons(plugin, player, offers));91 assertThat("Offer data is sent next tick", offerData, is(anEmptyMap()));92 BukkitSchedulerMock scheduler = server.getScheduler();93 assertDoesNotThrow(() -> scheduler.performTicks(1));94 assertThat("Offer data is sent", offerData, is(not(anEmptyMap())));95 // Ensure enchantment level requirements are sent correctly.96 assertThat(97 "First offer cost must be set",98 offerData.entrySet(),99 hasItem(Map.entry(Property.ENCHANT_BUTTON1, offers[0].getCost())));100 assertThat(101 "Second offer cost must be set",102 offerData.entrySet(),103 hasItem(Map.entry(Property.ENCHANT_BUTTON2, offers[1].getCost())));104 assertThat(105 "Third offer cost must be set",106 offerData.entrySet(),107 hasItem(Map.entry(Property.ENCHANT_BUTTON3, offers[2].getCost())));108 // Ensure enchantment levels are sent correctly.109 assertThat(110 "First offer level must be set",111 offerData.entrySet(),112 hasItem(Map.entry(Property.ENCHANT_LEVEL1, offers[0].getEnchantmentLevel())));113 assertThat(114 "Second offer level must be set",115 offerData.entrySet(),116 hasItem(Map.entry(Property.ENCHANT_LEVEL2, offers[1].getEnchantmentLevel())));117 assertThat(118 "Third offer level must be set",119 offerData.entrySet(),120 hasItem(Map.entry(Property.ENCHANT_LEVEL3, offers[2].getEnchantmentLevel())));121 // Exact IDs not verified - can't really test this without NMS.122 assertThat(123 "Offer contains enchantment IDs",124 offerData.keySet(),125 hasItems(Property.ENCHANT_ID1, Property.ENCHANT_ID2, Property.ENCHANT_ID3));126 }127 @DisplayName("Fetching Enchantment ID should not cause errors.")128 @Test129 void testInvalidGetEnchantmentId() {130 var server = MockBukkit.getMock();131 var plugin = MockBukkit.createMockPlugin("SampleText");132 var player = new PlayerMock(server, "sampletext") {133 @Override134 public boolean setWindowProperty(@NotNull InventoryView.Property prop, int value) {135 return true;136 }137 };138 String enchantName = "enchant_table_util";139 var unregisteredEnchantment = new EnchantmentMock(NamespacedKey.minecraft(enchantName + 1), enchantName + 1);140 var registeredReflectableEnchant = new CraftEnchantMock(NamespacedKey.minecraft(enchantName + 2), 1, value -> value, value -> value);141 EnchantmentHelper.putEnchant(registeredReflectableEnchant);142 var registeredUnlistedNonReflectableEnchant = new EnchantmentMock(NamespacedKey.minecraft(enchantName + 3), enchantName + 3);143 EnchantmentOffer[] offers = new EnchantmentOffer[] {144 new EnchantmentOffer(unregisteredEnchantment, 5, 1),145 new EnchantmentOffer(registeredReflectableEnchant, 20, 2),146 new EnchantmentOffer(registeredUnlistedNonReflectableEnchant, 30, 3)147 };148 // Schedule button update.149 EnchantingTable.updateButtons(plugin, player, offers);150 BukkitSchedulerMock scheduler = server.getScheduler();151 assertDoesNotThrow(() -> scheduler.performTicks(1));152 }153}...
Source:SpigotTests.java
...68 private PlayerMock preparePlayer() {69 PlayerMock tester = server.addPlayer("Tester");70 Wait.mainThread(500);71 BukkitSchedulerMock scheduler = (BukkitSchedulerMock) Bukkit.getScheduler();72 scheduler.performTicks(10);73 return tester;74 }75 @Test76 public void player_test_common_stuff() {77 PlayerMock playerMock = preparePlayer();78 playerMock.addAttachment(plugin, "ntrpg.class.warrior", true);79 // select primary class80 playerMock.performCommand("char choose class warrior");81 Wait.mainThread(500);82 ISpigotCharacter character = characterService.getCharacter(playerMock.getUniqueId());83 Assertions.assertTrue(character.getClasses().containsKey("warrior"));84 // add level85 ClassDefinition warriorClass = classService.getClassDefinitionByName("warrior");86 double levelTreshold = warriorClass.getLevelProgression().getLevelMargins()[0] + 1D;...
performTicks
Using AI Code Generation
1import static org.junit.Assert.assertEquals;2import static org.junit.Assert.assertTrue;3import static org.junit.Assert.fail;4import org.bukkit.Bukkit;5import org.bukkit.scheduler.BukkitRunnable;6import org.junit.Before;7import org.junit.Test;8import be.seeseemelk.mockbukkit.MockBukkit;9import be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock;10public class TestScheduler {11public void setUp()12{13MockBukkit.mock();14}15public void testScheduler()16{17BukkitSchedulerMock scheduler = (BukkitSchedulerMock) Bukkit.getScheduler();18scheduler.performTicks(20);19assertEquals(20, scheduler.getCurrentTick());20}21public void testScheduler2()22{23BukkitSchedulerMock scheduler = (BukkitSchedulerMock) Bukkit.getScheduler();24scheduler.performTicks(20);25assertEquals(20, scheduler.getCurrentTick());26new BukkitRunnable() {27public void run()28{29fail();30}31}.runTaskLater(Bukkit.getPluginManager().getPlugin("MockBukkit"), 20);32scheduler.performTicks(20);33assertTrue(true);34}35}36}
performTicks
Using AI Code Generation
1import be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock;2import org.bukkit.plugin.Plugin;3public class Main extends JavaPlugin {4 private BukkitSchedulerMock scheduler;5 private Plugin plugin;6 public void onEnable() {7 scheduler = new BukkitSchedulerMock();8 plugin = this;9 scheduler.runTaskTimer(plugin, () -> {10 System.out.println("Hello World!");11 }, 1, 1);12 scheduler.performTicks(10);13 }14}15import be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock;16import org.bukkit.plugin.Plugin;17public class Main extends JavaPlugin {18 private BukkitSchedulerMock scheduler;19 private Plugin plugin;20 public void onEnable() {21 scheduler = new BukkitSchedulerMock();22 plugin = this;23 scheduler.runTaskTimer(plugin, () -> {24 System.out.println("Hello World!");25 }, 1, 1);26 scheduler.performTicks(10);27 }28}29import be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock;30import org.bukkit.plugin.Plugin;31public class Main extends JavaPlugin {32 private BukkitSchedulerMock scheduler;33 private Plugin plugin;34 public void onEnable() {35 scheduler = new BukkitSchedulerMock();36 plugin = this;37 scheduler.runTaskTimer(plugin, () -> {38 System.out.println("Hello World!");39 }, 1, 1);40 scheduler.performTicks(10);41 }42}43import be.seeseemelk.mockbukkit.scheduler.BukkitSchedulerMock;44import org.bukkit.plugin.Plugin;45public class Main extends JavaPlugin {46 private BukkitSchedulerMock scheduler;47 private Plugin plugin;48 public void onEnable() {49 scheduler = new BukkitSchedulerMock();50 plugin = this;51 scheduler.runTaskTimer(plugin, () -> {52 System.out.println("Hello World!");53 }, 1, 1);54 scheduler.performTicks(10);55 }56}
performTicks
Using AI Code Generation
1BukkitSchedulerMock scheduler = new BukkitSchedulerMock();2BukkitSchedulerMock scheduler = new BukkitSchedulerMock();3BukkitSchedulerMock scheduler = new BukkitSchedulerMock();4BukkitSchedulerMock scheduler = new BukkitSchedulerMock();5BukkitSchedulerMock scheduler = new BukkitSchedulerMock();6BukkitSchedulerMock scheduler = new BukkitSchedulerMock();7BukkitSchedulerMock scheduler = new BukkitSchedulerMock();8BukkitSchedulerMock scheduler = new BukkitSchedulerMock();9BukkitSchedulerMock scheduler = new BukkitSchedulerMock();
performTicks
Using AI Code Generation
1public class 2 {2 private static final Logger logger = Logger.getLogger("Minecraft");3 private static BukkitSchedulerMock scheduler;4 private static ServerMock server;5 private static PluginManagerMock pluginManager;6 public static void main(String[] args) {7 server = MockBukkit.mock();8 scheduler = server.getScheduler();9 pluginManager = server.getPluginManager();10 pluginManager.registerEvents(new Listener() {11 public void onPlayerJoin(PlayerJoinEvent event) {12 logger.info("Player joined!");13 }14 }, MockBukkit.loadWith(JavaPlugin.getProvidingPlugin(getClass())));15 PlayerMock player = server.addPlayer();16 player.join();17 scheduler.performTicks(1);18 server.unloadWorld(server.getWorlds().get(0));19 server.shutdown();20 }21}22public class 3 {23 private static final Logger logger = Logger.getLogger("Minecraft");24 private static BukkitSchedulerMock scheduler;25 private static ServerMock server;26 private static PluginManagerMock pluginManager;27 public static void main(String[] args) {28 server = MockBukkit.mock();29 scheduler = server.getScheduler();30 pluginManager = server.getPluginManager();31 pluginManager.registerEvents(new Listener() {32 public void onPlayerJoin(PlayerJoinEvent event) {33 logger.info("Player joined!");34 }35 }, MockBukkit.loadWith(JavaPlugin.getProvidingPlugin(getClass())));36 PlayerMock player = server.addPlayer();37 player.join();38 scheduler.performOneTick();39 server.unloadWorld(server.getWorlds().get(0));40 server.shutdown();41 }42}43public class 4 {44 private static final Logger logger = Logger.getLogger("Minecraft");45 private static BukkitSchedulerMock scheduler;46 private static ServerMock server;47 private static PluginManagerMock pluginManager;48 public static void main(String[] args) {49 server = MockBukkit.mock();50 scheduler = server.getScheduler();51 pluginManager = server.getPluginManager();52 pluginManager.registerEvents(new Listener() {53 public void onPlayerJoin(PlayerJoinEvent event) {
performTicks
Using AI Code Generation
1public class 2 {2 public static void main(String[] args) {3 ServerMock server = new ServerMock();4 WorldMock world = new WorldMock();5 world.setName("world");6 server.addWorld(world);7 Bukkit.setServer(server);8 BukkitSchedulerMock scheduler = (BukkitSchedulerMock) Bukkit.getScheduler();9 scheduler.performTicks(2);10 }11}12public class 3 {13 public static void main(String[] args) {14 ServerMock server = new ServerMock();15 WorldMock world = new WorldMock();16 world.setName("world");17 server.addWorld(world);18 Bukkit.setServer(server);19 BukkitSchedulerMock scheduler = (BukkitSchedulerMock) Bukkit.getScheduler();20 scheduler.performTicks(2);21 scheduler.performTicks(2);22 }23}
performTicks
Using AI Code Generation
1@ExtendWith(MockBukkitExtension.class)2{3 private Plugin plugin;4 private Server server;5 private BukkitScheduler scheduler;6 private BukkitSchedulerMock schedulerMock;7 private BukkitSchedulerMock schedulerMock2;8 public void setUp()9 {10 when(plugin.getServer()).thenReturn(server);11 when(server.getScheduler()).thenReturn(scheduler);12 when(schedulerMock.getScheduler()).thenReturn(schedulerMock2);13 }14 public void test()15 {16 schedulerMock.runTaskTimer(plugin, () -> System.out.println("Test"), 0, 1);17 schedulerMock.performTicks(20);18 }19}20@ExtendWith(MockBukkitExtension.class)21{22 private Plugin plugin;23 private Server server;24 private BukkitScheduler scheduler;25 private BukkitSchedulerMock schedulerMock;26 private BukkitSchedulerMock schedulerMock2;27 public void setUp()28 {29 when(plugin.getServer()).thenReturn(server);30 when(server.getScheduler()).thenReturn(scheduler);31 when(schedulerMock.getScheduler()).thenReturn(schedulerMock2);32 }33 public void test()34 {35 schedulerMock.runTaskTimer(plugin, () -> System.out.println("Test"), 0, 1);36 schedulerMock.performTicks(20);37 }38}39@ExtendWith(MockBukkitExtension.class)40{41 private Plugin plugin;42 private Server server;43 private BukkitScheduler scheduler;44 private BukkitSchedulerMock schedulerMock;45 private BukkitSchedulerMock schedulerMock2;46 public void setUp()47 {48 when(plugin.getServer()).thenReturn(server);49 when(server.getScheduler()).thenReturn(scheduler);50 when(scheduler
performTicks
Using AI Code Generation
1private World world;2private World world2;3private Server server;4private BukkitSchedulerMock scheduler;5private final int TICKS_PER_SECOND = 20;6private final int TICKS_PER_MINUTE = 20 * 60;7private final int TICKS_PER_HOUR = 20 * 60 * 60;8private final int TICKS_PER_DAY = 20 * 60 * 60 * 24;9public void setUp() throws Exception10{11 server = MockBukkit.mock();12 world = server.getWorld("world");13 world2 = server.getWorld("world2");14 scheduler = (BukkitSchedulerMock) server.getScheduler();15}16public void tearDown() throws Exception17{18 MockBukkit.unmock();19}20public void test()21{22 scheduler.performTicks(10);23 assertEquals(10, world.getTime());24}25private World world;26private World world2;27private Server server;28private BukkitSchedulerMock scheduler;29private final int TICKS_PER_SECOND = 20;30private final int TICKS_PER_MINUTE = 20 * 60;31private final int TICKS_PER_HOUR = 20 * 60 * 60;32private final int TICKS_PER_DAY = 20 * 60 * 60 * 24;33public void setUp() throws Exception34{35 server = MockBukkit.mock();36 world = server.getWorld("world");37 world2 = server.getWorld("world2");38 scheduler = (BukkitSchedulerMock) server.getScheduler();39}40public void tearDown() throws Exception41{42 MockBukkit.unmock();43}44public void test()45{46 scheduler.performTicks(10);47 assertEquals(10, world.getTime());48}49private World world;50private World world2;51private Server server;52private BukkitSchedulerMock scheduler;53private final int TICKS_PER_SECOND = 20;54private final int TICKS_PER_MINUTE = 20 * 60;
performTicks
Using AI Code Generation
1package be.seeseemelk.mockbukkit.scheduler;2import be.seeseemelk.mockbukkit.MockBukkit;3import be.seeseemelk.mockbukkit.ServerMock;4{5 public static void main(String[] args)6 {7 ServerMock server = MockBukkit.mock();8 BukkitSchedulerMock scheduler = new BukkitSchedulerMock(server);9 scheduler.performTicks(1);10 }11}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!