Adds support for AdvancedChests. Requires version 14.3

https://github.com/BentoBoxWorld/Level/issues/214
This commit is contained in:
tastybento 2021-02-24 17:29:29 -08:00
parent 311455e921
commit 12525271c8
2 changed files with 45 additions and 12 deletions

View File

@ -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,11 +101,43 @@ 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) {
// 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) {

View File

@ -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,23 +434,24 @@ 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) {
private void countItemStack(ItemStack i) {
if (i != null && i.getType().isBlock()) {
for (int c = 0; c < i.getAmount(); c++) {
checkBlock(i.getType(), false);
}
}
}
}
/**
* Scan the chunk chests and count the blocks