Fix issue that may be caused by trying to get a block in unloaded chunk

This commit is contained in:
SeanOMik 2021-09-20 22:41:45 -04:00
parent 0183ab0109
commit 9935ac68da
No known key found for this signature in database
GPG Key ID: CA09E5BE1F32728A
1 changed files with 24 additions and 22 deletions

View File

@ -91,35 +91,37 @@ public class HopperTask extends BukkitRunnable {
public void run() { public void run() {
for (Map.Entry<UUID, List<ESSystem>> systemEntry : Reference.ES_SYSTEMS.entrySet()) { for (Map.Entry<UUID, List<ESSystem>> systemEntry : Reference.ES_SYSTEMS.entrySet()) {
for (ESSystem system : systemEntry.getValue()) { for (ESSystem system : systemEntry.getValue()) {
Block systemBlock = system.getLocation().getBlock(); if (system.getLocation().isWorldLoaded()) {
Block systemBlock = system.getLocation().getBlock();
// Get all relative hoppers touching the system. // Get all relative hoppers touching the system.
Map<BlockFace, Block> hoppers = getRelativeHoppers(systemBlock); Map<BlockFace, Block> hoppers = getRelativeHoppers(systemBlock);
for (Map.Entry<BlockFace, Block> entry : hoppers.entrySet()) { for (Map.Entry<BlockFace, Block> entry : hoppers.entrySet()) {
Bukkit.getScheduler().runTask(EnergeticStorage.getPlugin(), () -> { Bukkit.getScheduler().runTask(EnergeticStorage.getPlugin(), () -> {
try { try {
org.bukkit.block.Hopper hopper = (org.bukkit.block.Hopper) entry.getValue().getState(); org.bukkit.block.Hopper hopper = (org.bukkit.block.Hopper) entry.getValue().getState();
org.bukkit.material.Hopper hopperType = (org.bukkit.material.Hopper) hopper.getData(); org.bukkit.material.Hopper hopperType = (org.bukkit.material.Hopper) hopper.getData();
// Check if the hopper is facing towards the system // Check if the hopper is facing towards the system
if (isHopperInputting(hopperType, entry.getKey())) { if (isHopperInputting(hopperType, entry.getKey())) {
// Find the first non-null item in the hopper inventory and add it // Find the first non-null item in the hopper inventory and add it
ItemStack firstItem = getFirstItemStack(hopper.getInventory()); ItemStack firstItem = getFirstItemStack(hopper.getInventory());
if (firstItem != null) { if (firstItem != null) {
ItemStack clonedItem = firstItem.clone(); ItemStack clonedItem = firstItem.clone();
clonedItem.setAmount(1); clonedItem.setAmount(1);
firstItem.setAmount(firstItem.getAmount() - 1); firstItem.setAmount(firstItem.getAmount() - 1);
system.addItem(clonedItem); system.addItem(clonedItem);
}
} }
} catch (ClassCastException exception) {
// Ignore exception. These exceptions are only thrown in rare occasions
// that the hopper is destroyed during during this task.
} }
} catch (ClassCastException exception) { });
// Ignore exception. These exceptions are only thrown in rare occasions }
// that the hopper is destroyed during during this task.
}
});
} }
} }
} }