Skip to content

Commit

Permalink
Fix backpack IDs not incrementing (Slimefun#4081)
Browse files Browse the repository at this point in the history
  • Loading branch information
WalshyDev committed Jan 7, 2024
1 parent 4ab6388 commit 158c6ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 158c6ee

Please sign in to comment.