Repository: Slimefun/Slimefun4 (Java)

Issue:
Backpacks don't increment number
### ❗ Checklist

- [X] I am using the official english version of Slimefun and did not modify the jar.
- [X] I am using an up to date "DEV" (not "RC") version of Slimefun.
- [X] I am aware that issues related to Slimefun addons need to be reported on their bug trackers and not here.
- [X] I searched for similar open issues and could not find an existing bug report on this.

### 📍 Description

Crafting new backpacks does not increment its number, and it just gives another copy of an existing backpack. The number only increments when the server is restarted.

### 📑 Reproduction Steps

Craft several Small Backpacks in an Enhanced Crafting Table

### 💡 Expected Behavior

For the backpack number to increment

### 📷 Screenshots / Videos

https://github.com/Slimefun/Slimefun4/assets/101147426/391ec1e1-c56a-4bbf-9c69-a4104a90538f

(Sorry about the quality)


### 📜 Server Log

No error messages

### 📂 `/error-reports/` folder

No errors generated

### 💻 Server Software

Paper

### 🎮 Minecraft Version

1.20.x

### ⭐ Slimefun version

Dev 1122

### 🧭 Other plugins

_No response_


Discussion:
Sounds like something was missed with the new storage api



Patch:
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();


Test patch:
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 {