From 00d2f393696dcb84d203eb09b740695e228cd811 Mon Sep 17 00:00:00 2001 From: StarWishsama Date: Sat, 27 Jan 2024 13:38:11 +0800 Subject: [PATCH] chore: merge follow up change --- pom.xml | 11 +- .../SlimefunItemInteractListener.java | 39 +- .../implementation/setup/PostSetup.java | 1 + .../storage/backend/legacy/LegacyStorage.java | 4 +- .../TestSlimefunRegistryFinalizedEvent.java | 42 -- .../TestSlimefunItemInteractListener.java | 222 ---------- .../storage/backend/TestLegacyBackend.java | 383 ------------------ 7 files changed, 28 insertions(+), 674 deletions(-) delete mode 100644 src/test/java/io/github/thebusybiscuit/slimefun4/api/events/TestSlimefunRegistryFinalizedEvent.java delete mode 100644 src/test/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TestSlimefunItemInteractListener.java delete mode 100644 src/test/java/io/github/thebusybiscuit/slimefun4/storage/backend/TestLegacyBackend.java diff --git a/pom.xml b/pom.xml index b820113817..1f363c3a17 100644 --- a/pom.xml +++ b/pom.xml @@ -281,7 +281,7 @@ com.github.starwishsama.dough dough-api - 20dcab228c + f266e39d48 compile @@ -445,8 +445,15 @@ com.mojang authlib - 1.5.25 + 6.0.52 provided + + + + * + * + + diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/SlimefunItemInteractListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/SlimefunItemInteractListener.java index 19adb0c397..b13182aa9b 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/SlimefunItemInteractListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/SlimefunItemInteractListener.java @@ -54,13 +54,6 @@ public void onRightClick(PlayerInteractEvent e) { return; } - // Fixes #4087 - Prevents players from interacting with a block that is about to be deleted - // We especially don't want to open inventories as that can cause duplication - if (e.getClickedBlock() != null && Slimefun.getTickerTask().isDeletedSoon(e.getClickedBlock().getLocation())) { - e.setCancelled(true); - return; - } - // Fire our custom Event PlayerRightClickEvent event = new PlayerRightClickEvent(e); Bukkit.getPluginManager().callEvent(event); @@ -122,7 +115,7 @@ private boolean rightClickBlock(PlayerRightClickEvent event) { } boolean interactable = - sfItem.callItemHandler(BlockUseHandler.class, handler -> handler.onRightClick(event)); + sfItem.callItemHandler(BlockUseHandler.class, handler -> handler.onRightClick(event)); if (!interactable) { Player p = event.getPlayer(); @@ -152,22 +145,22 @@ private void openInventory(Player p, SlimefunItem item, Block clickedBlock, Play openMenu(blockData.getBlockMenu(), clickedBlock, p); } else { Slimefun.getDatabaseManager() - .getBlockDataController() - .loadBlockDataAsync(blockData, new IAsyncReadCallback<>() { - @Override - public boolean runOnMainThread() { - return true; + .getBlockDataController() + .loadBlockDataAsync(blockData, new IAsyncReadCallback<>() { + @Override + public boolean runOnMainThread() { + return true; + } + + @Override + public void onResult(SlimefunBlockData result) { + if (!p.isOnline()) { + return; } - @Override - public void onResult(SlimefunBlockData result) { - if (!p.isOnline()) { - return; - } - - openMenu(result.getBlockMenu(), clickedBlock, p); - } - }); + openMenu(result.getBlockMenu(), clickedBlock, p); + } + }); } } } catch (Exception | LinkageError x) { @@ -184,4 +177,4 @@ private void openMenu(BlockMenu menu, Block b, Player p) { } } } -} +} \ No newline at end of file diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/PostSetup.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/PostSetup.java index 65fd1d0c60..a0bd90bac8 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/PostSetup.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/PostSetup.java @@ -1,5 +1,6 @@ package io.github.thebusybiscuit.slimefun4.implementation.setup; +import io.github.thebusybiscuit.slimefun4.api.events.SlimefunItemRegistryFinalizedEvent; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/storage/backend/legacy/LegacyStorage.java b/src/main/java/io/github/thebusybiscuit/slimefun4/storage/backend/legacy/LegacyStorage.java index d7981a5466..1dbe31f9c5 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/storage/backend/legacy/LegacyStorage.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/storage/backend/legacy/LegacyStorage.java @@ -51,9 +51,9 @@ public PlayerData loadPlayerData(@Nonnull UUID uuid) { items.put(i, playerFile.getItem("backpacks." + key + ".contents." + i)); } - PlayerBackpack backpack = PlayerBackpack.load(uuid, id, size, items); + //PlayerBackpack backpack = PlayerBackpack.load(uuid, id, size, items); - backpacks.put(id, backpack); + // backpacks.put(id, backpack); } catch (Exception x) { Slimefun.logger().log(Level.WARNING, x, () -> "Could not load Backpack \"" + key + "\" for Player \"" + uuid + '"'); } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/api/events/TestSlimefunRegistryFinalizedEvent.java b/src/test/java/io/github/thebusybiscuit/slimefun4/api/events/TestSlimefunRegistryFinalizedEvent.java deleted file mode 100644 index bef63828c9..0000000000 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/api/events/TestSlimefunRegistryFinalizedEvent.java +++ /dev/null @@ -1,42 +0,0 @@ -package io.github.thebusybiscuit.slimefun4.api.events; - -import org.junit.jupiter.api.Assertions; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import be.seeseemelk.mockbukkit.MockBukkit; -import be.seeseemelk.mockbukkit.ServerMock; -import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; -import io.github.thebusybiscuit.slimefun4.implementation.setup.PostSetup; - -class TestSlimefunRegistryFinalizedEvent { - - private static ServerMock server; - private static Slimefun plugin; - - @BeforeAll - public static void load() { - server = MockBukkit.mock(); - plugin = MockBukkit.load(Slimefun.class); - } - - @AfterAll - public static void unload() { - MockBukkit.unmock(); - } - - @Test - @DisplayName("Test that SlimefunRegistryFinalizedEvent is fired") - void testEventIsFired() { - // Make sure post setup does not throw - Assertions.assertDoesNotThrow(() -> PostSetup.loadItems()); - - // Make sure post setup sent the event - server.getPluginManager().assertEventFired(SlimefunItemRegistryFinalizedEvent.class, ignored -> true); - - server.getPluginManager().clearEvents(); - } -} diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TestSlimefunItemInteractListener.java b/src/test/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TestSlimefunItemInteractListener.java deleted file mode 100644 index cc33e3750a..0000000000 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/TestSlimefunItemInteractListener.java +++ /dev/null @@ -1,222 +0,0 @@ -package io.github.thebusybiscuit.slimefun4.implementation.listeners; - -import org.bukkit.World; -import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.entity.Player; -import org.bukkit.event.Event.Result; -import org.bukkit.event.block.Action; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.EquipmentSlot; -import org.bukkit.inventory.ItemStack; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import io.github.bakedlibs.dough.common.ChatColors; -import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent; -import io.github.thebusybiscuit.slimefun4.api.events.SlimefunBlockBreakEvent; -import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; -import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; -import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; -import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; -import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; -import io.github.thebusybiscuit.slimefun4.implementation.items.electric.EnergyConnector; -import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.ElectricFurnace; -import io.github.thebusybiscuit.slimefun4.implementation.items.magical.staves.WindStaff; -import io.github.thebusybiscuit.slimefun4.test.TestUtilities; -import me.mrCookieSlime.Slimefun.api.BlockStorage; -import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset; -import be.seeseemelk.mockbukkit.MockBukkit; -import be.seeseemelk.mockbukkit.ServerMock; -import be.seeseemelk.mockbukkit.entity.PlayerMock; - -class TestSlimefunItemInteractListener { - - private static ServerMock server; - private static Slimefun plugin; - // Block with inventory - private static SlimefunItem electricFurnace; - // Interactable block - private static SlimefunItem energyConnector; - // Interactable item - private static SlimefunItem windStaff; - - @BeforeAll - public static void load() { - server = MockBukkit.mock(); - plugin = MockBukkit.load(Slimefun.class); - - // Register block listener (for place + break) and our interact listener for inventory handling - new BlockListener(plugin); - new SlimefunItemInteractListener(plugin); - - // Enable tickers so the electric furnace can be registered - Slimefun.getCfg().setValue("URID.enable-tickers", true); - - ItemGroup testGroup = TestUtilities.getItemGroup(plugin, "test"); - - electricFurnace = new ElectricFurnace(testGroup, SlimefunItems.ELECTRIC_FURNACE, RecipeType.NULL, new ItemStack[]{}) - .setCapacity(100) - .setEnergyConsumption(10) - .setProcessingSpeed(1); - electricFurnace.register(plugin); - - energyConnector = new EnergyConnector(testGroup, SlimefunItems.ENERGY_CONNECTOR, RecipeType.NULL, new ItemStack[9], null); - energyConnector.register(plugin); - - windStaff = new WindStaff(testGroup, SlimefunItems.STAFF_WIND, RecipeType.NULL, new ItemStack[9]); - windStaff.register(plugin); - } - - @AfterAll - public static void unload() { - MockBukkit.unmock(); - } - - @AfterEach - public void afterEach() { - server.getPluginManager().clearEvents(); - } - - // Test for dupe bug - issue #4087 - @Test - void testCannotOpenInvOfBrokenBlock() { - // Place down an electric furnace - Player player = server.addPlayer(); - ItemStack itemStack = electricFurnace.getItem(); - player.getInventory().setItemInMainHand(itemStack); - - // Create a world and place the block - World world = TestUtilities.createWorld(server); - Block block = TestUtilities.placeSlimefunBlock(server, itemStack, world, player); - - // Right click on the block - PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent( - player, Action.RIGHT_CLICK_BLOCK, itemStack, block, BlockFace.UP, EquipmentSlot.HAND - ); - - server.getPluginManager().callEvent(playerInteractEvent); - server.getPluginManager().assertEventFired(PlayerInteractEvent.class, e -> { - // We cancel the event on inventory open - Assertions.assertSame(e.useInteractedBlock(), Result.DENY); - return true; - }); - - // Assert our right click event fired and the block usage was not denied - server.getPluginManager().assertEventFired(PlayerRightClickEvent.class, e -> { - Assertions.assertNotSame(e.useBlock(), Result.DENY); - return true; - }); - - // Assert we do have an inventory which would be opened - // TODO: Create an event for open inventory so this isn't guess work - Assertions.assertTrue(BlockMenuPreset.isInventory(electricFurnace.getId())); - Assertions.assertTrue(BlockStorage.getStorage(block.getWorld()).hasInventory(block.getLocation())); - - // Assert player has the inventory open - Assertions.assertEquals(1, BlockStorage.getInventory(block).toInventory().getViewers().size()); - - // Break the block - BlockBreakEvent blockBreakEvent = new BlockBreakEvent(block, player); - server.getPluginManager().callEvent(blockBreakEvent); - server.getPluginManager().assertEventFired(SlimefunBlockBreakEvent.class, e -> { - Assertions.assertEquals(electricFurnace.getId(), e.getSlimefunItem().getId()); - return true; - }); - - // Assert the block is queued for removal - Assertions.assertTrue(Slimefun.getTickerTask().isDeletedSoon(block.getLocation())); - - // Assert that the inventory was closed - Assertions.assertEquals(0, BlockStorage.getInventory(block).toInventory().getViewers().size()); - - // Clear event queue since we'll be running duplicate events - server.getPluginManager().clearEvents(); - - // Right click on the block again now that it's broken - PlayerInteractEvent secondPlayerInteractEvent = new PlayerInteractEvent( - player, Action.RIGHT_CLICK_BLOCK, itemStack, block, BlockFace.UP, EquipmentSlot.HAND - ); - - server.getPluginManager().callEvent(secondPlayerInteractEvent); - server.getPluginManager().assertEventFired(PlayerInteractEvent.class, e -> { - // We cancelled the event due to the block being removed - Assertions.assertSame(e.useInteractedBlock(), Result.DENY); - return true; - }); - - // Assert our right click event was not fired due to the block being broken - Assertions.assertThrows( - AssertionError.class, - () -> server.getPluginManager().assertEventFired(PlayerRightClickEvent.class, e -> true) - ); - } - - @Test - void testRightClickItem() { - Player player = server.addPlayer(); - ItemStack itemStack = windStaff.getItem(); - player.getInventory().setItemInMainHand(itemStack); - - // Assert player is at full food level (wind staff reduces food level on usage) - Assertions.assertEquals(20, player.getFoodLevel()); - - // Right click the air - PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent( - player, Action.RIGHT_CLICK_AIR, itemStack, null, BlockFace.UP, EquipmentSlot.HAND - ); - - server.getPluginManager().callEvent(playerInteractEvent); - server.getPluginManager().assertEventFired(PlayerInteractEvent.class, e -> { - // Assert our interaction was not cancelled - Assertions.assertNotSame(e.useItemInHand(), Result.DENY); - return true; - }); - - // Assert our right click event fired and the item usage was not denied - server.getPluginManager().assertEventFired(PlayerRightClickEvent.class, e -> { - Assertions.assertNotSame(e.useItem(), Result.DENY); - return true; - }); - - // Assert our food level is now 18 - Assertions.assertEquals(18, player.getFoodLevel()); - } - - @Test - void testRightClickInteractableBlock() { - // Place down an energy connector - PlayerMock player = server.addPlayer(); - ItemStack itemStack = energyConnector.getItem(); - player.getInventory().setItemInMainHand(itemStack); - - // Create a world and place the block - World world = TestUtilities.createWorld(server); - Block block = TestUtilities.placeSlimefunBlock(server, itemStack, world, player); - - // Right click on the block - PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent( - player, Action.RIGHT_CLICK_BLOCK, itemStack, block, BlockFace.UP, EquipmentSlot.HAND - ); - - server.getPluginManager().callEvent(playerInteractEvent); - server.getPluginManager().assertEventFired(PlayerInteractEvent.class, e -> { - // Allow interaction of the block - Assertions.assertSame(e.useInteractedBlock(), Result.ALLOW); - return true; - }); - - // Assert our right click event fired and the block usage was not denied - server.getPluginManager().assertEventFired(PlayerRightClickEvent.class, e -> { - Assertions.assertNotSame(e.useBlock(), Result.DENY); - return true; - }); - - // Assert the message our energy connector sends - Assertions.assertEquals(ChatColors.color("&7Connected: " + "&4\u2718"), player.nextMessage()); - } -} diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/storage/backend/TestLegacyBackend.java b/src/test/java/io/github/thebusybiscuit/slimefun4/storage/backend/TestLegacyBackend.java deleted file mode 100644 index c8e1916f54..0000000000 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/storage/backend/TestLegacyBackend.java +++ /dev/null @@ -1,383 +0,0 @@ -package io.github.thebusybiscuit.slimefun4.storage.backend; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.util.UUID; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Location; -import org.bukkit.NamespacedKey; -import org.bukkit.OfflinePlayer; -import org.bukkit.World; -import org.bukkit.WorldCreator; -import org.bukkit.World.Environment; -import org.bukkit.configuration.serialization.ConfigurationSerialization; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import be.seeseemelk.mockbukkit.MockBukkit; -import be.seeseemelk.mockbukkit.ServerMock; -import io.github.thebusybiscuit.slimefun4.api.gps.Waypoint; -import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; -import io.github.thebusybiscuit.slimefun4.api.researches.Research; -import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; -import io.github.thebusybiscuit.slimefun4.storage.backend.legacy.LegacyStorage; -import io.github.thebusybiscuit.slimefun4.storage.data.PlayerData; -import io.github.thebusybiscuit.slimefun4.test.TestUtilities; -import io.github.thebusybiscuit.slimefun4.utils.FileUtils; - -class TestLegacyBackend { - - private static ServerMock server; - private static Slimefun plugin; - - @BeforeAll - public static void load() { - server = MockBukkit.mock(); - plugin = MockBukkit.load(Slimefun.class); - - File playerFolder = new File("data-storage/Slimefun/Players"); - playerFolder.mkdirs(); - File waypointFolder = new File("data-storage/Slimefun/waypoints"); - waypointFolder.mkdirs(); - - // Not too sure why this is needed, we don't use it elsewhere, it should just use the ItemStack serialization - // My guess is MockBukkit isn't loading the ConfigurationSerialization class therefore the static block - // within the class isn't being fired (where ItemStack and other classes are registered) - ConfigurationSerialization.registerClass(ItemStack.class); - ConfigurationSerialization.registerClass(ItemMeta.class); - - setupResearches(); - } - - @AfterAll - public static void unload() throws IOException { - MockBukkit.unmock(); - FileUtils.deleteDirectory(new File("data-storage")); - } - - // Test simple loading and saving of player data - @Test - void testLoadingResearches() throws IOException { - // Create a player file which we can load - UUID uuid = UUID.randomUUID(); - File playerFile = new File("data-storage/Slimefun/Players/" + uuid + ".yml"); - Files.writeString(playerFile.toPath(), """ - researches: - '0': true - '1': true - '2': true - '3': true - '4': true - '5': true - '6': true - '7': true - '8': true - '9': true - """); - - // Load the player data - LegacyStorage storage = new LegacyStorage(); - PlayerData data = storage.loadPlayerData(uuid); - - // Check if the data is correct - Assertions.assertEquals(10, data.getResearches().size()); - for (int i = 0; i < 10; i++) { - Assertions.assertTrue(data.getResearches().contains(Slimefun.getRegistry().getResearches().get(i))); - } - } - - // There's some issues with deserializing items in tests, I spent quite a while debugging this - // and didn't really get anywhere. So commenting this out for now. - /* - @Test - void testLoadingBackpacks() throws IOException { - // Create a player file which we can load - UUID uuid = UUID.randomUUID(); - File playerFile = new File("data-storage/Slimefun/Players/" + uuid + ".yml"); - Files.writeString(playerFile.toPath(), """ - backpacks: - '0': - size: 9 - contents: - '0': - ==: org.bukkit.inventory.ItemStack - v: 1 - type: IRON_BLOCK - meta: - ==: org.bukkit.inventory.meta.ItemMeta - enchants: {} - damage: 0 - persistentDataContainer: - slimefun:slimefun_item: TEST - displayName: ยง6Test block - itemFlags: !!set {} - unbreakable: false - repairCost: 0 - """); - - // Load the player data - LegacyStorage storage = new LegacyStorage(); - PlayerData data = storage.loadPlayerData(uuid); - - // Check if the data is correct - Assertions.assertEquals(1, data.getBackpacks().size()); - Assertions.assertEquals(9, data.getBackpacks().get(0).getSize()); - - // Validate item deserialization - System.out.println( - Arrays.stream(data.getBackpack(0).getInventory().getContents()) - .map((item) -> item == null ? "null" : item.getType().name()) - .collect(Collectors.joining(", ")) - ); - ItemStack stack = data.getBackpack(0).getInventory().getItem(0); - Assertions.assertNotNull(stack); - Assertions.assertEquals("IRON_BLOCK", stack.getType().name()); - Assertions.assertEquals(1, stack.getAmount()); - Assertions.assertEquals(ChatColor.GREEN + "Test block", stack.getItemMeta().getDisplayName()); - } - */ - - @Test - void testLoadingWaypoints() throws IOException { - // Create mock world - server.createWorld(WorldCreator.name("world").environment(Environment.NORMAL)); - - // Create a player file which we can load - UUID uuid = UUID.randomUUID(); - File waypointFile = new File("data-storage/Slimefun/waypoints/" + uuid + ".yml"); - Files.writeString(waypointFile.toPath(), """ - TEST: - x: -173.0 - y: 75.0 - z: -11.0 - pitch: 0.0 - yaw: 178.0 - world: world - name: test - """); - - // Load the player data - LegacyStorage storage = new LegacyStorage(); - PlayerData data = storage.loadPlayerData(uuid); - - // Check if the data is correct - Assertions.assertEquals(1, data.getWaypoints().size()); - - // Validate waypoint deserialization - Waypoint waypoint = data.getWaypoints().iterator().next(); - - Assertions.assertEquals("test", waypoint.getName()); - Assertions.assertEquals(-173.0, waypoint.getLocation().getX()); - Assertions.assertEquals(75.0, waypoint.getLocation().getY()); - Assertions.assertEquals(-11.0, waypoint.getLocation().getZ()); - Assertions.assertEquals(178.0, waypoint.getLocation().getYaw()); - Assertions.assertEquals(0.0, waypoint.getLocation().getPitch()); - Assertions.assertEquals("world", waypoint.getLocation().getWorld().getName()); - } - - @Test - void testSavingResearches() throws InterruptedException { - // Create a player file which we can load - UUID uuid = UUID.randomUUID(); - File playerFile = new File("data-storage/Slimefun/Players/" + uuid + ".yml"); - - OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); - - PlayerProfile profile = TestUtilities.awaitProfile(player); - - for (Research research : Slimefun.getRegistry().getResearches()) { - profile.setResearched(research, true); - } - - // Save the player data - LegacyStorage storage = new LegacyStorage(); - storage.savePlayerData(uuid, profile.getPlayerData()); - - // Assert the file exists and data is correct - Assertions.assertTrue(playerFile.exists()); - PlayerData assertion = storage.loadPlayerData(uuid); - Assertions.assertEquals(10, assertion.getResearches().size()); - for (int i = 0; i < 10; i++) { - Assertions.assertTrue(assertion.getResearches().contains(Slimefun.getRegistry().getResearches().get(i))); - } - } - - // There's some issues with deserializing items in tests, I spent quite a while debugging this - // and didn't really get anywhere. So commenting this out for now. - /* - @Test - void testSavingBackpacks() throws InterruptedException { - // Create a player file which we can load - UUID uuid = UUID.randomUUID(); - File playerFile = new File("data-storage/Slimefun/Players/" + uuid + ".yml"); - - OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); - - PlayerProfile profile = TestUtilities.awaitProfile(player); - - PlayerBackpack backpack = profile.createBackpack(9); - backpack.getInventory().addItem(SlimefunItems.AIR_RUNE); - - // Save the player data - LegacyStorage storage = new LegacyStorage(); - storage.savePlayerData(uuid, profile.getPlayerData()); - - // Assert the file exists and data is correct - Assertions.assertTrue(playerFile.exists()); - PlayerData assertion = storage.loadPlayerData(uuid); - Assertions.assertEquals(1, assertion.getBackpacks().size()); - } - */ - - @Test - void testSavingWaypoints() throws InterruptedException { - // Create mock world - World world = server.createWorld(WorldCreator.name("world").environment(Environment.NORMAL)); - - // Create a player file which we can load - UUID uuid = UUID.randomUUID(); - File playerFile = new File("data-storage/Slimefun/Players/" + uuid + ".yml"); - - OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); - PlayerProfile profile = TestUtilities.awaitProfile(player); - - profile.addWaypoint(new Waypoint( - player.getUniqueId(), - "test", - new Location(world, 1, 2, 3, 4, 5), - ChatColor.GREEN + "Test waypoint") - ); - - // Save the player data - LegacyStorage storage = new LegacyStorage(); - storage.savePlayerData(uuid, profile.getPlayerData()); - - // Assert the file exists and data is correct - Assertions.assertTrue(playerFile.exists()); - PlayerData assertion = storage.loadPlayerData(uuid); - Assertions.assertEquals(1, assertion.getWaypoints().size()); - - // Validate waypoint deserialization - Waypoint waypoint = assertion.getWaypoints().iterator().next(); - - Assertions.assertEquals(ChatColor.GREEN + "Test waypoint", waypoint.getName()); - Assertions.assertEquals(1, waypoint.getLocation().getX()); - Assertions.assertEquals(2, waypoint.getLocation().getY()); - Assertions.assertEquals(3, waypoint.getLocation().getZ()); - Assertions.assertEquals(4, waypoint.getLocation().getYaw()); - Assertions.assertEquals(5, waypoint.getLocation().getPitch()); - Assertions.assertEquals("world", waypoint.getLocation().getWorld().getName()); - } - - // Test realistic situations - @Test - void testResearchChanges() throws InterruptedException { - UUID uuid = UUID.randomUUID(); - File playerFile = new File("data-storage/Slimefun/Players/" + uuid + ".yml"); - - OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); - PlayerProfile profile = TestUtilities.awaitProfile(player); - - // Unlock all researches - for (Research research : Slimefun.getRegistry().getResearches()) { - profile.setResearched(research, true); - } - - // Save the player data - LegacyStorage storage = new LegacyStorage(); - storage.savePlayerData(uuid, profile.getPlayerData()); - - // Assert the file exists and data is correct - Assertions.assertTrue(playerFile.exists()); - PlayerData assertion = storage.loadPlayerData(uuid); - Assertions.assertEquals(10, assertion.getResearches().size()); - for (int i = 0; i < 10; i++) { - Assertions.assertTrue(assertion.getResearches().contains(Slimefun.getRegistry().getResearches().get(i))); - } - - // Now let's change the data and save it again - profile.setResearched(Slimefun.getRegistry().getResearches().get(3), false); - - // Save the player data - storage.savePlayerData(uuid, profile.getPlayerData()); - - // Assert the file exists and data is correct - Assertions.assertTrue(playerFile.exists()); - System.out.println("update assertion"); - assertion = storage.loadPlayerData(uuid); - Assertions.assertEquals(9, assertion.getResearches().size()); - for (int i = 0; i < 10; i++) { - if (i != 3) { - Assertions.assertTrue(assertion.getResearches().contains(Slimefun.getRegistry().getResearches().get(i))); - } - } - } - - // Test realistic situations - when we fix the serialization issue - // @Test - // void testBackpackChanges() throws InterruptedException {} - - @Test - void testWaypointChanges() throws InterruptedException { - // Create mock world - World world = server.createWorld(WorldCreator.name("world").environment(Environment.NORMAL)); - - // Create a player file which we can load - UUID uuid = UUID.randomUUID(); - File playerFile = new File("data-storage/Slimefun/Players/" + uuid + ".yml"); - - OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); - PlayerProfile profile = TestUtilities.awaitProfile(player); - - profile.addWaypoint(new Waypoint( - player.getUniqueId(), - "test", - new Location(world, 1, 2, 3, 4, 5), - ChatColor.GREEN + "Test waypoint" - )); - - Waypoint test2 = new Waypoint( - player.getUniqueId(), - "test2", - new Location(world, 10, 20, 30, 40, 50), - ChatColor.GREEN + "Test 2 waypoint" - ); - profile.addWaypoint(test2); - - // Save the player data - LegacyStorage storage = new LegacyStorage(); - storage.savePlayerData(uuid, profile.getPlayerData()); - - // Assert the file exists and data is correct - Assertions.assertTrue(playerFile.exists()); - PlayerData assertion = storage.loadPlayerData(uuid); - Assertions.assertEquals(2, assertion.getWaypoints().size()); - - // Remove one - profile.removeWaypoint(test2); - - // Save the player data - storage.savePlayerData(uuid, profile.getPlayerData()); - - // Assert the file exists and data is correct - Assertions.assertTrue(playerFile.exists()); - assertion = storage.loadPlayerData(uuid); - Assertions.assertEquals(1, assertion.getWaypoints().size()); - } - - // Utils - private static void setupResearches() { - for (int i = 0; i < 10; i++) { - NamespacedKey key = new NamespacedKey(plugin, "test_" + i); - Research research = new Research(key, i, "Test " + i, 100); - research.register(); - } - } -}