fixed pipes sharing filter and extraction modules not behaving correctly

closes #185
This commit is contained in:
Ell 2023-09-26 10:51:43 +02:00
parent f34c89f914
commit 7d54b5d936
6 changed files with 30 additions and 17 deletions

View file

@ -71,14 +71,14 @@ public class ItemFilter extends ItemStackHandler {
} else if (id == 1 && this.canPopulateFromInventories) {
var changed = false;
// populate filter from inventories
var filters = this.pipe.getFilters();
var filters = this.pipe.getFilters(null);
for (var direction : Direction.values()) {
var handler = this.pipe.getItemHandler(direction);
if (handler == null)
continue;
for (var i = 0; i < handler.getSlots(); i++) {
var stack = handler.getStackInSlot(i);
if (stack.isEmpty() || this.isFiltered(stack))
if (stack.isEmpty() || this.isFiltered(stack, null))
continue;
var copy = stack.copy();
copy.setCount(1);
@ -97,14 +97,14 @@ public class ItemFilter extends ItemStackHandler {
}
}
public boolean isAllowed(ItemStack stack) {
return this.isFiltered(stack) == this.isWhitelist;
public boolean isAllowed(ItemStack stack, Direction direction) {
return this.isFiltered(stack, direction) == this.isWhitelist;
}
private boolean isFiltered(ItemStack stack) {
private boolean isFiltered(ItemStack stack, Direction direction) {
var types = ItemFilter.getEqualityTypes(this.pipe);
// also check if any filter increase modules have the item we need
for (ItemStackHandler handler : this.pipe.getFilters()) {
for (ItemStackHandler handler : this.pipe.getFilters(direction)) {
for (var i = 0; i < handler.getSlots(); i++) {
var filter = handler.getStackInSlot(i);
if (filter.isEmpty())
@ -162,5 +162,7 @@ public class ItemFilter extends ItemStackHandler {
default void onFilterPopulated() {
}
}
}

View file

@ -382,10 +382,15 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC
.filter(m -> m != null && m >= 0).findFirst().orElse(index);
}
public List<ItemFilter> getFilters() {
return this.streamModules()
.map(p -> p.getRight().getItemFilter(p.getLeft(), this))
.filter(Objects::nonNull).collect(Collectors.toList());
public List<ItemFilter> getFilters(Direction direction) {
return this.streamModules().map(p -> {
if (direction != null) {
var dir = p.getRight().getDirectionSelector(p.getLeft(), this);
if (dir != null && !dir.has(direction))
return null;
}
return p.getRight().getItemFilter(p.getLeft(), this);
}).filter(Objects::nonNull).collect(Collectors.toList());
}
@Override

View file

@ -45,7 +45,7 @@ public class ExtractionModuleItem extends ModuleItem {
var stack = handler.extractItem(j, this.maxExtraction, true);
if (stack.isEmpty())
continue;
if (!filter.isAllowed(stack))
if (!filter.isAllowed(stack, dir))
continue;
var remain = network.routeItem(tile.getBlockPos(), tile.getBlockPos().relative(dir), stack, this.preventOversending);
if (remain.getCount() != stack.getCount()) {
@ -90,4 +90,5 @@ public class ExtractionModuleItem extends ModuleItem {
public DirectionSelector getDirectionSelector(ItemStack module, PipeBlockEntity tile) {
return new DirectionSelector(module, tile);
}
}

View file

@ -27,7 +27,7 @@ public class FilterModuleItem extends ModuleItem {
@Override
public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction, IItemHandler destination) {
return !this.getDirectionSelector(module, tile).has(direction) || this.getItemFilter(module, tile).isAllowed(stack);
return !this.getDirectionSelector(module, tile).has(direction) || this.getItemFilter(module, tile).isAllowed(stack, direction);
}
@Override
@ -56,4 +56,5 @@ public class FilterModuleItem extends ModuleItem {
public DirectionSelector getDirectionSelector(ItemStack module, PipeBlockEntity tile) {
return new DirectionSelector(module, tile);
}
}

View file

@ -20,7 +20,7 @@ public class FilterModifierModuleContainer extends AbstractPipeContainer<FilterM
public List<ResourceLocation> getTags() {
Set<ResourceLocation> unsortedTags = new HashSet<>();
for (var filter : this.tile.getFilters()) {
for (var filter : this.tile.getFilters(null)) {
for (var i = 0; i < filter.getSlots(); i++) {
var stack = filter.getStackInSlot(i);
stack.getTags().forEach(t -> unsortedTags.add(t.location()));
@ -33,4 +33,5 @@ public class FilterModifierModuleContainer extends AbstractPipeContainer<FilterM
protected void addSlots() {
}
}

View file

@ -15,6 +15,8 @@ import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.IItemHandler;
import java.util.Arrays;
public class RetrievalModuleItem extends ModuleItem {
private final int maxExtraction;
@ -38,9 +40,9 @@ public class RetrievalModuleItem extends ModuleItem {
var network = PipeNetwork.get(tile.getLevel());
var equalityTypes = ItemFilter.getEqualityTypes(tile);
// loop through filters to see which items to pull
for (var subFilter : tile.getFilters()) {
for (var f = 0; f < subFilter.getSlots(); f++) {
var filtered = subFilter.getStackInSlot(f);
Arrays.stream(directions).flatMap(d -> tile.getFilters(d).stream()).distinct().forEach(f -> {
for (var i = 0; i < f.getSlots(); i++) {
var filtered = f.getStackInSlot(i);
if (filtered.isEmpty())
continue;
var copy = filtered.copy();
@ -54,7 +56,7 @@ public class RetrievalModuleItem extends ModuleItem {
if (network.requestItem(tile.getBlockPos(), dest.getLeft(), remain, equalityTypes).isEmpty())
break;
}
}
});
}
@Override
@ -94,4 +96,5 @@ public class RetrievalModuleItem extends ModuleItem {
public DirectionSelector getDirectionSelector(ItemStack module, PipeBlockEntity tile) {
return new DirectionSelector(module, tile);
}
}