From 9935ac68da22d4efc2cbb5799ba809c44c219b5f Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Mon, 20 Sep 2021 22:41:45 -0400 Subject: [PATCH] Fix issue that may be caused by trying to get a block in unloaded chunk --- .../energeticstorage/tasks/HopperTask.java | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/seanomik/energeticstorage/tasks/HopperTask.java b/src/main/java/net/seanomik/energeticstorage/tasks/HopperTask.java index 9e778ab..40e9ee4 100644 --- a/src/main/java/net/seanomik/energeticstorage/tasks/HopperTask.java +++ b/src/main/java/net/seanomik/energeticstorage/tasks/HopperTask.java @@ -91,35 +91,37 @@ public class HopperTask extends BukkitRunnable { public void run() { for (Map.Entry> systemEntry : Reference.ES_SYSTEMS.entrySet()) { 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. - Map hoppers = getRelativeHoppers(systemBlock); - for (Map.Entry entry : hoppers.entrySet()) { - Bukkit.getScheduler().runTask(EnergeticStorage.getPlugin(), () -> { + // Get all relative hoppers touching the system. + Map hoppers = getRelativeHoppers(systemBlock); + for (Map.Entry entry : hoppers.entrySet()) { + Bukkit.getScheduler().runTask(EnergeticStorage.getPlugin(), () -> { - try { - org.bukkit.block.Hopper hopper = (org.bukkit.block.Hopper) entry.getValue().getState(); - org.bukkit.material.Hopper hopperType = (org.bukkit.material.Hopper) hopper.getData(); - // Check if the hopper is facing towards the system - if (isHopperInputting(hopperType, entry.getKey())) { + try { + org.bukkit.block.Hopper hopper = (org.bukkit.block.Hopper) entry.getValue().getState(); + org.bukkit.material.Hopper hopperType = (org.bukkit.material.Hopper) hopper.getData(); + // Check if the hopper is facing towards the system + if (isHopperInputting(hopperType, entry.getKey())) { - // Find the first non-null item in the hopper inventory and add it - ItemStack firstItem = getFirstItemStack(hopper.getInventory()); - if (firstItem != null) { - ItemStack clonedItem = firstItem.clone(); - clonedItem.setAmount(1); + // Find the first non-null item in the hopper inventory and add it + ItemStack firstItem = getFirstItemStack(hopper.getInventory()); + if (firstItem != null) { + ItemStack clonedItem = firstItem.clone(); + 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. - } - }); + }); + } } } }