From 12525271c8bac3c4462c1ec5e8a108a840214ea6 Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 24 Feb 2021 17:29:29 -0800 Subject: [PATCH] Adds support for AdvancedChests. Requires version 14.3 https://github.com/BentoBoxWorld/Level/issues/214 --- src/main/java/world/bentobox/level/Level.java | 37 ++++++++++++++++++- .../calculators/IslandLevelCalculator.java | 20 +++++----- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/main/java/world/bentobox/level/Level.java b/src/main/java/world/bentobox/level/Level.java index 2901406..b4c2422 100644 --- a/src/main/java/world/bentobox/level/Level.java +++ b/src/main/java/world/bentobox/level/Level.java @@ -10,6 +10,7 @@ import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.plugin.Plugin; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; @@ -100,12 +101,44 @@ public class Level extends Addon implements Listener { log("Hooked into WildStackers."); } // Check if AdvancedChests is enabled on the server - advChestEnabled = Bukkit.getPluginManager().getPlugin("AdvancedChests") != null; + Plugin advChest = Bukkit.getPluginManager().getPlugin("AdvancedChests"); + advChestEnabled = advChest != null; if (advChestEnabled) { - log("Hooked into AdvancedChests."); + // Check version + if (compareVersions(advChest.getDescription().getVersion(), "14.2") > 0) { + log("Hooked into AdvancedChests."); + } else { + logError("Could not hook into AdvancedChests " + advChest.getDescription().getVersion() + " - requires version 14.3 or later"); + advChestEnabled = false; + } } } + /** + * Compares versions + * @param version1 + * @param version2 + * @return <0 if version 1 is older than version 2, =0 if the same, >0 if version 1 is newer than version 2 + */ + public static int compareVersions(String version1, String version2) { + int comparisonResult = 0; + + String[] version1Splits = version1.split("\\."); + String[] version2Splits = version2.split("\\."); + int maxLengthOfVersionSplits = Math.max(version1Splits.length, version2Splits.length); + + for (int i = 0; i < maxLengthOfVersionSplits; i++){ + Integer v1 = i < version1Splits.length ? Integer.parseInt(version1Splits[i]) : 0; + Integer v2 = i < version2Splits.length ? Integer.parseInt(version2Splits[i]) : 0; + int compare = v1.compareTo(v2); + if (compare != 0) { + comparisonResult = compare; + break; + } + } + return comparisonResult; + } + @EventHandler public void onBentoBoxReady(BentoBoxReadyEvent e) { // Perform upgrade check diff --git a/src/main/java/world/bentobox/level/calculators/IslandLevelCalculator.java b/src/main/java/world/bentobox/level/calculators/IslandLevelCalculator.java index b3668d6..253b997 100644 --- a/src/main/java/world/bentobox/level/calculators/IslandLevelCalculator.java +++ b/src/main/java/world/bentobox/level/calculators/IslandLevelCalculator.java @@ -22,7 +22,6 @@ import org.bukkit.block.BlockState; import org.bukkit.block.Container; import org.bukkit.block.data.BlockData; import org.bukkit.block.data.type.Slab; -import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; import org.eclipse.jdt.annotation.NonNull; @@ -435,20 +434,21 @@ public class IslandLevelCalculator { if (bs instanceof Container) { if (addon.isAdvChestEnabled()) { AdvancedChest aChest = AdvancedChestsAPI.getChestManager().getAdvancedChest(bs.getLocation()); - aChest.getPages().stream().map(ChestPage::getInventory).forEach(this::countChestItems); - } else { - countChestItems(((Container)bs).getSnapshotInventory()); + if (aChest != null) { + aChest.getPages().stream().map(ChestPage::getItems).forEach(c -> c.forEach(this::countItemStack)); + continue; + } } + // Regular chest + ((Container)bs).getSnapshotInventory().forEach(this::countItemStack); } } } - private void countChestItems(Inventory inv) { - for (ItemStack i : inv) { - if (i != null && i.getType().isBlock()) { - for (int c = 0; c < i.getAmount(); c++) { - checkBlock(i.getType(), false); - } + private void countItemStack(ItemStack i) { + if (i != null && i.getType().isBlock()) { + for (int c = 0; c < i.getAmount(); c++) { + checkBlock(i.getType(), false); } } }