diff --git a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java index fc44dca..fc9d3ba 100644 --- a/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java +++ b/src/main/java/com/songoda/epichoppers/hopper/levels/modules/ModuleSuction.java @@ -19,7 +19,9 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; +import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; public class ModuleSuction implements Module { @@ -50,6 +52,8 @@ public class ModuleSuction implements Module { } catch (ReflectiveOperationException e) { e.printStackTrace(); } + + Bukkit.getScheduler().scheduleSyncRepeatingTask(EpicHoppers.getInstance(), blacklist::clear, 0L, 100L); } @@ -61,12 +65,15 @@ public class ModuleSuction implements Module { public void run(Hopper hopper, Inventory hopperInventory) { double radius = amount + .5; - hopper.getLocation().getWorld().getNearbyEntities(hopper.getLocation().add(0.5, 0.5, 0.5), radius, radius, radius).stream() + Set itemsToSuck = hopper.getLocation().getWorld().getNearbyEntities(hopper.getLocation().add(0.5, 0.5, 0.5), radius, radius, radius) + .stream() .filter(entity -> entity.getType() == EntityType.DROPPED_ITEM - && entity.getTicksLived() >= ((Item)entity).getPickupDelay() - && entity.getLocation().getBlock().getType() != Material.HOPPER).forEach(entity -> { + && entity.getTicksLived() >= ((Item) entity).getPickupDelay() + && entity.getLocation().getBlock().getType() != Material.HOPPER) + .map(entity -> (Item) entity) + .collect(Collectors.toSet()); - Item item = (Item) entity; + for (Item item : itemsToSuck) { ItemStack itemStack = setMax(item.getItemStack().clone(), 0, true); if (itemStack.getType().name().contains("SHULKER_BOX")) @@ -78,7 +85,7 @@ public class ModuleSuction implements Module { } if (wildStacker) - itemStack.setAmount(WildStackerAPI.getItemAmount((Item) entity)); + itemStack.setAmount(WildStackerAPI.getItemAmount(item)); if (ultimateStacker && item.hasMetadata("US_AMT")) itemStack.setAmount(item.getMetadata("US_AMT").get(0).asInt()); @@ -93,14 +100,14 @@ public class ModuleSuction implements Module { float zz = (float) (0 + (Math.random() * .1)); if (EpicHoppers.getInstance().isServerVersionAtLeast(ServerVersion.V1_9)) - entity.getLocation().getWorld().spawnParticle(Particle.FLAME, entity.getLocation(), 5, xx, yy, zz, 0); + item.getLocation().getWorld().spawnParticle(Particle.FLAME, item.getLocation(), 5, xx, yy, zz, 0); + + for (ItemStack is : hopperInventory.addItem(itemStack).values()) + item.getWorld().dropItemNaturally(item.getLocation(), is); - for (ItemStack is : hopperInventory.addItem(itemStack).values()) { - entity.getWorld().dropItemNaturally(entity.getLocation(), is); - } HopTask.updateAdjacentComparators(hopper.getLocation()); - entity.remove(); - }); + item.remove(); + } } public static boolean isBlacklisted(UUID uuid) { @@ -129,13 +136,13 @@ public class ModuleSuction implements Module { } private boolean canMove(Inventory inventory, ItemStack item) { - if (inventory.firstEmpty() != -1) return true; + if (inventory.firstEmpty() != -1) return true; - for (ItemStack stack : inventory.getContents()) { - if (stack.isSimilar(item) && (stack.getAmount() + item.getAmount()) < stack.getMaxStackSize()) { - return true; - } + for (ItemStack stack : inventory.getContents()) { + if (stack.isSimilar(item) && (stack.getAmount() + item.getAmount()) < stack.getMaxStackSize()) { + return true; } + } return false; } diff --git a/src/main/java/com/songoda/epichoppers/listeners/EntityListeners.java b/src/main/java/com/songoda/epichoppers/listeners/EntityListeners.java index 76ad521..91d81d6 100644 --- a/src/main/java/com/songoda/epichoppers/listeners/EntityListeners.java +++ b/src/main/java/com/songoda/epichoppers/listeners/EntityListeners.java @@ -8,6 +8,7 @@ import org.bukkit.Material; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDeathEvent; @@ -56,7 +57,7 @@ public class EntityListeners implements Listener { event.getDrops().clear(); } - @EventHandler + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onPlayerPickup(PlayerPickupItemEvent event) { if (ModuleSuction.isBlacklisted(event.getItem().getUniqueId())) event.setCancelled(true); diff --git a/src/main/java/com/songoda/epichoppers/tasks/HopTask.java b/src/main/java/com/songoda/epichoppers/tasks/HopTask.java index 49c3fea..b41413d 100644 --- a/src/main/java/com/songoda/epichoppers/tasks/HopTask.java +++ b/src/main/java/com/songoda/epichoppers/tasks/HopTask.java @@ -86,8 +86,8 @@ public class HopTask extends BukkitRunnable { continue; } - // If hopper block is powered continue. - if (block.isBlockPowered() || block.isBlockIndirectlyPowered()) { + // If hopper block is powered, update its redstone state and continue. + if (block.getBlockPower() > 0) { hopper.tryTick(this.hopTicks, false); continue; } @@ -175,8 +175,11 @@ public class HopTask extends BukkitRunnable { itemToMove.setAmount(amountToMove); // Add item to container and break on success. - if (this.addItem(hopper, aboveInvHolder, hopperState, block.getType(), item, itemToMove, amountToMove)) + if (this.addItem(hopper, aboveInvHolder, hopperState, block.getType(), item, itemToMove, amountToMove)) { + updateAdjacentComparators(block.getLocation()); + updateAdjacentComparators(above.getLocation()); break; + } } } @@ -424,7 +427,7 @@ public class HopTask extends BukkitRunnable { // Cast to state. BlockState state = endPoint.getBlock().getState(); - //Remove if not a container. + // Remove if not a container. if (!(state instanceof InventoryHolder)) { hopper.getFilter().setEndPoint(null); return null; @@ -471,12 +474,9 @@ public class HopTask extends BukkitRunnable { } private boolean canMove(Inventory inventory, ItemStack item) { - if (inventory.firstEmpty() != -1) return true; - for (ItemStack stack : inventory.getContents()) { - if (stack.isSimilar(item) && (stack.getAmount() + item.getAmount()) - 1 < stack.getMaxStackSize()) { + for (ItemStack stack : inventory.getContents()) + if (stack == null || (stack.getType() == item.getType() && stack.isSimilar(item) && (stack.getAmount() + item.getAmount()) - 1 < stack.getMaxStackSize())) return true; - } - } return false; }