From 158c6eea211cb608d9ae5b4f2d7ea7ee45c81548 Mon Sep 17 00:00:00 2001 From: Daniel Walsh Date: Sun, 7 Jan 2024 09:19:51 +0000 Subject: [PATCH] Fix backpack IDs not incrementing (#4081) --- .../slimefun4/api/player/PlayerProfile.java | 8 +++++--- .../api/profiles/TestPlayerBackpacks.java | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerProfile.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerProfile.java index c51888a1d0..96290f4849 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerProfile.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/player/PlayerProfile.java @@ -91,7 +91,10 @@ protected PlayerProfile(@Nonnull OfflinePlayer p, PlayerData data) { * Only intended for internal usage. * * @return The {@link Config} associated with this {@link PlayerProfile} + * + * @deprecated Look at {@link PlayerProfile#getPlayerData()} instead for reading data. */ + @Deprecated public @Nonnull Config getConfig() { return configFile; } @@ -245,10 +248,9 @@ public final void markDirty() { } public @Nonnull PlayerBackpack createBackpack(int size) { - IntStream stream = IntStream.iterate(0, i -> i + 1).filter(i -> !configFile.contains("backpacks." + i + ".size")); - int id = stream.findFirst().getAsInt(); + int nextId = this.data.getBackpacks().size(); // Size is not 0 indexed so next ID can just be the current size - PlayerBackpack backpack = PlayerBackpack.newBackpack(this.ownerId, id, size); + PlayerBackpack backpack = PlayerBackpack.newBackpack(this.ownerId, nextId, size); this.data.addBackpack(backpack); markDirty(); diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/api/profiles/TestPlayerBackpacks.java b/src/test/java/io/github/thebusybiscuit/slimefun4/api/profiles/TestPlayerBackpacks.java index 07f69761e0..90d475d479 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/api/profiles/TestPlayerBackpacks.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/api/profiles/TestPlayerBackpacks.java @@ -47,6 +47,21 @@ void testCreateBackpack() throws InterruptedException { Assertions.assertEquals(18, backpack.getInventory().getSize()); } + @Test + @DisplayName("Test creating a new backpack will increment the id") + void testCreateBackpackIncrementsId() throws InterruptedException { + Player player = server.addPlayer(); + PlayerProfile profile = TestUtilities.awaitProfile(player); + + PlayerBackpack backpackOne = profile.createBackpack(18); + PlayerBackpack backpackTwo = profile.createBackpack(18); + PlayerBackpack backpackThree = profile.createBackpack(18); + + Assertions.assertEquals(0, backpackOne.getId()); + Assertions.assertEquals(1, backpackTwo.getId()); + Assertions.assertEquals(2, backpackThree.getId()); + } + @Test @DisplayName("Test upgrading the backpack size") void testChangeSize() throws InterruptedException {