private ItemStack doPipeIteration(Location<World> location, ItemStack itemStack, Direction fromDirection, Set<Location<World>> traversed) { if(traversed.contains(location)) return itemStack; traversed.add(location); if(itemStack.getQuantity() == 0) return itemStack; PipePart pipePart = getPipePart(location); if(pipePart == null) return itemStack; for (Location<World> location1 : pipePart.findPotentialOutputs(location, itemStack, fromDirection)) { if (pipePart.validateOutput(location, location1, itemStack)) { if (pipePart instanceof OutputPipePart) { Optional<Inventory> inventory = LocationUtil.getInventoryForLocation(location1); if (inventory.isPresent()) { InventoryTransactionResult result = inventory.get().offer(itemStack); if (!result.getRejectedItems().isEmpty()) { for (ItemStackSnapshot snapshot : result.getRejectedItems()) { itemStack = snapshot.createStack(); } } else { itemStack.setQuantity(0); } } } else itemStack = doPipeIteration(location1, itemStack, BlockUtil.getFacing(location1, location), traversed); } } return itemStack; }
private void performPipeAction(Location<World> location) { Direction direction = location.get(Keys.DIRECTION).get(); Location<World> inventorySource = location.getRelative(direction); //Let's try and find a source of items! LocationUtil.getInventoryForLocation(inventorySource).ifPresent(inv -> { while (inv.peek(1).isPresent()) { Optional<ItemStack> itemStackOptional = inv.poll(1); itemStackOptional.ifPresent(itemStack -> { Set<Location<World>> traversed = new HashSet<>(); try { itemStack = doPipeIteration(location, itemStack, direction, traversed); } catch (StackOverflowError e) { CraftBookPlugin.spongeInst().getLogger() .error("Pipe overflow. Please report this issue to the developers with images of setup.", e); } if (itemStack.getQuantity() > 0) { for (ItemStackSnapshot snapshot : inv.offer(itemStack).getRejectedItems()) { Item item = (Item) location.getExtent().createEntity(EntityTypes.ITEM, location.getPosition()); item.offer(Keys.REPRESENTED_ITEM, snapshot); location.getExtent().spawnEntity(item); } } }); if (stackPerPull.getValue()) { break; } } }); }