From e1b4b1d164d522543181f394111d962ff1c68836 Mon Sep 17 00:00:00 2001 From: OmerBenGera Date: Fri, 25 Feb 2022 19:03:27 +0200 Subject: [PATCH] Added a check for invalid rows contents for island chests from database --- .../serialization/IslandsDeserializer.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/bgsoftware/superiorskyblock/database/serialization/IslandsDeserializer.java b/src/main/java/com/bgsoftware/superiorskyblock/database/serialization/IslandsDeserializer.java index cc3244010..7e29b0ae1 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/database/serialization/IslandsDeserializer.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/database/serialization/IslandsDeserializer.java @@ -638,6 +638,27 @@ public static void deserializeIslandChest(DatabaseBridge databaseBridge, Databas return; } + int contentsLength = contents.get().length; + ItemStack[] chestContents; + + if(contentsLength % 9 != 0) { + int amountOfRows = Math.min(1, Math.max(6, (contentsLength / 9) + 1)); + chestContents = new ItemStack[amountOfRows * 9]; + int amountOfContentsToCopy = Math.min(contentsLength, chestContents.length); + System.arraycopy(contents.get(), 0, chestContents, 0, amountOfContentsToCopy); + } + else if(contentsLength > 54) { + chestContents = new ItemStack[54]; + System.arraycopy(contents.get(), 0, chestContents, 0, 54); + } + else if(contentsLength < 9) { + chestContents = new ItemStack[9]; + System.arraycopy(contents.get(), 0, chestContents, 0, contentsLength); + } + else { + chestContents = contents.get(); + } + CachedIslandInfo cachedIslandInfo = databaseCache.computeIfAbsentInfo(uuid.get(), CachedIslandInfo::new); if (index.get() >= cachedIslandInfo.islandChests.size()) { @@ -645,9 +666,9 @@ public static void deserializeIslandChest(DatabaseBridge databaseBridge, Databas cachedIslandInfo.islandChests.add(new ItemStack[plugin.getSettings().getIslandChests().getDefaultSize() * 9]); } - cachedIslandInfo.islandChests.add(contents.get()); + cachedIslandInfo.islandChests.add(chestContents); } else { - cachedIslandInfo.islandChests.set(index.get(), contents.get()); + cachedIslandInfo.islandChests.set(index.get(), chestContents); } }); }