added default "all sides" option which restores the old behavior

This commit is contained in:
Ell 2022-07-01 00:05:11 +02:00
parent 1b061759e1
commit 626d8efe2e
8 changed files with 106 additions and 116 deletions

View file

@ -13,9 +13,13 @@ import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.gui.widget.ExtendedButton; import net.minecraftforge.client.gui.widget.ExtendedButton;
import org.apache.commons.lang3.ArrayUtils;
public class DirectionSelector { public class DirectionSelector {
private static final Direction[] ALL = ArrayUtils.addAll(Direction.values(), (Direction) null);
// null means old behavior, which is all directions
private Direction direction; private Direction direction;
private boolean modified; private boolean modified;
@ -35,8 +39,8 @@ public class DirectionSelector {
@Override @Override
public Component getMessage() { public Component getMessage() {
var pipe = DirectionSelector.this.pipe; var pipe = DirectionSelector.this.pipe;
var dir = DirectionSelector.this.getDirection(); var dir = DirectionSelector.this.direction;
MutableComponent msg = new TranslatableComponent("dir." + PrettyPipes.ID + "." + (dir != null ? dir.getName() : "none")); MutableComponent msg = new TranslatableComponent("dir." + PrettyPipes.ID + "." + (dir != null ? dir.getName() : "all"));
if (dir != null) { if (dir != null) {
var blockName = pipe.getItemHandler(dir) != null ? pipe.getLevel().getBlockState(pipe.getBlockPos().relative(dir)).getBlock().getName() : null; var blockName = pipe.getItemHandler(dir) != null ? pipe.getLevel().getBlockState(pipe.getBlockPos().relative(dir)).getBlock().getName() : null;
if (blockName != null) if (blockName != null)
@ -48,7 +52,10 @@ public class DirectionSelector {
} }
public void onButtonPacket() { public void onButtonPacket() {
var dir = this.getValidDirection(this.getDirection()); var dir = this.direction;
do {
dir = DirectionSelector.ALL[(ArrayUtils.indexOf(DirectionSelector.ALL, dir) + 1) % DirectionSelector.ALL.length];
} while (!this.isDirectionValid(dir));
if (this.direction != dir) { if (this.direction != dir) {
this.direction = dir; this.direction = dir;
this.modified = true; this.modified = true;
@ -61,9 +68,8 @@ public class DirectionSelector {
this.modified = false; this.modified = false;
var tag = new CompoundTag(); var tag = new CompoundTag();
var dir = this.getDirection(); if (this.direction != null)
if (dir != null) tag.putString("direction", this.direction.getName());
tag.putString("direction", dir.getName());
this.stack.getOrCreateTag().put("direction_selector", tag); this.stack.getOrCreateTag().put("direction_selector", tag);
} }
@ -74,34 +80,25 @@ public class DirectionSelector {
} }
} }
public Direction getDirection() { public Direction[] directions() {
// default to the first direction with a container if ours is invalid or unset return this.direction != null ? new Direction[]{this.direction} : Direction.values();
if (this.direction == null || !this.isDirectionValid(this.direction)) }
return this.getValidDirection(this.direction);
return this.direction; public boolean has(Direction dir) {
return this.direction == null || this.direction == dir;
} }
private boolean isDirectionValid(Direction dir) { private boolean isDirectionValid(Direction dir) {
if (dir == null)
return true;
if (this.pipe.getItemHandler(dir) == null) if (this.pipe.getItemHandler(dir) == null)
return false; return false;
return this.pipe.streamModules() return this.pipe.streamModules()
.filter(p -> p.getLeft() != this.stack) .filter(p -> p.getLeft() != this.stack)
.map(p -> p.getRight().getDirectionSelector(p.getLeft(), this.pipe)) .map(p -> p.getRight().getDirectionSelector(p.getLeft(), this.pipe))
// don't use getDirection here because we don't want a stack overflow
.noneMatch(p -> p != null && p.direction == dir); .noneMatch(p -> p != null && p.direction == dir);
} }
private Direction getValidDirection(Direction dir) {
if (dir == null)
dir = Direction.UP;
for (var i = 0; i < 6; i++) {
dir = Direction.from3DDataValue(dir.get3DDataValue() + 1);
if (this.isDirectionValid(dir))
return dir;
}
return null;
}
public interface IDirectionContainer { public interface IDirectionContainer {
DirectionSelector getSelector(); DirectionSelector getSelector();

View file

@ -147,7 +147,7 @@ public class PipeNetwork implements ICapabilitySerializable<CompoundTag>, GraphL
if (!this.world.isLoaded(pipePos)) if (!this.world.isLoaded(pipePos))
continue; continue;
var pipe = this.getPipe(pipePos); var pipe = this.getPipe(pipePos);
var dest = pipe.getAvailableDestination(stack, false, preventOversending); var dest = pipe.getAvailableDestination(Direction.values(), stack, false, preventOversending);
if (dest == null || dest.getLeft().equals(startInventory)) if (dest == null || dest.getLeft().equals(startInventory))
continue; continue;
var sup = (Function<Float, IPipeItem>) speed -> itemSupplier.apply(dest.getRight(), speed); var sup = (Function<Float, IPipeItem>) speed -> itemSupplier.apply(dest.getRight(), speed);

View file

@ -167,7 +167,7 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC
} }
public Pair<BlockPos, ItemStack> getAvailableDestinationOrConnectable(ItemStack stack, boolean force, boolean preventOversending) { public Pair<BlockPos, ItemStack> getAvailableDestinationOrConnectable(ItemStack stack, boolean force, boolean preventOversending) {
var dest = this.getAvailableDestination(stack, force, preventOversending); var dest = this.getAvailableDestination(Direction.values(), stack, force, preventOversending);
if (dest != null) if (dest != null)
return dest; return dest;
// if there's no available destination, try inserting into terminals etc. // if there's no available destination, try inserting into terminals etc.
@ -185,32 +185,26 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC
return null; return null;
} }
public Pair<BlockPos, ItemStack> getAvailableDestination(ItemStack stack, boolean force, boolean preventOversending) { public Pair<BlockPos, ItemStack> getAvailableDestination(Direction[] directions, ItemStack stack, boolean force, boolean preventOversending) {
for (var dir : Direction.values()) {
var dest = this.getAvailableDestination(dir, stack, force, preventOversending);
if (!dest.isEmpty())
return Pair.of(this.worldPosition.relative(dir), dest);
}
return null;
}
public ItemStack getAvailableDestination(Direction direction, ItemStack stack, boolean force, boolean preventOversending) {
if (!this.canWork()) if (!this.canWork())
return ItemStack.EMPTY; return null;
var handler = this.getItemHandler(direction); for (var dir : directions) {
if (handler == null || !force && this.streamModules().anyMatch(m -> !m.getRight().canAcceptItem(m.getLeft(), this, stack, direction, handler))) var handler = this.getItemHandler(dir);
return ItemStack.EMPTY; if (handler == null)
continue;
if (!force && this.streamModules().anyMatch(m -> !m.getRight().canAcceptItem(m.getLeft(), this, stack, dir, handler)))
continue;
var remain = ItemHandlerHelper.insertItem(handler, stack, true); var remain = ItemHandlerHelper.insertItem(handler, stack, true);
// did we insert anything? // did we insert anything?
if (remain.getCount() == stack.getCount()) if (remain.getCount() == stack.getCount())
return ItemStack.EMPTY; continue;
var toInsert = stack.copy(); var toInsert = stack.copy();
toInsert.shrink(remain.getCount()); toInsert.shrink(remain.getCount());
// limit to the max amount that modules allow us to insert // limit to the max amount that modules allow us to insert
var maxAmount = this.streamModules().mapToInt(m -> m.getRight().getMaxInsertionAmount(m.getLeft(), this, stack, handler)).min().orElse(Integer.MAX_VALUE); var maxAmount = this.streamModules().mapToInt(m -> m.getRight().getMaxInsertionAmount(m.getLeft(), this, stack, handler)).min().orElse(Integer.MAX_VALUE);
if (maxAmount < toInsert.getCount()) if (maxAmount < toInsert.getCount())
toInsert.setCount(maxAmount); toInsert.setCount(maxAmount);
var offset = this.worldPosition.relative(direction); var offset = this.worldPosition.relative(dir);
if (preventOversending || maxAmount < Integer.MAX_VALUE) { if (preventOversending || maxAmount < Integer.MAX_VALUE) {
var network = PipeNetwork.get(this.level); var network = PipeNetwork.get(this.level);
// these are the items that are currently in the pipes, going to this inventory // these are the items that are currently in the pipes, going to this inventory
@ -245,7 +239,10 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC
} }
} }
// we return the item that can actually be inserted, NOT the remainder! // we return the item that can actually be inserted, NOT the remainder!
return toInsert; if (!toInsert.isEmpty())
return Pair.of(offset, toInsert);
}
return null;
} }
public int getPriority() { public int getPriority() {

View file

@ -75,7 +75,7 @@ public class CraftingModuleItem extends ModuleItem {
network.startProfile("crafting_ingredients"); network.startProfile("crafting_ingredients");
var request = tile.craftIngredientRequests.peek(); var request = tile.craftIngredientRequests.peek();
var equalityTypes = ItemFilter.getEqualityTypes(tile); var equalityTypes = ItemFilter.getEqualityTypes(tile);
var dest = tile.getAvailableDestination(request.stack, true, true); var dest = tile.getAvailableDestination(Direction.values(), request.stack, true, true);
if (dest != null) { if (dest != null) {
var requestRemain = network.requestExistingItem(request.location, tile.getBlockPos(), dest.getLeft(), request, dest.getRight(), equalityTypes); var requestRemain = network.requestExistingItem(request.location, tile.getBlockPos(), dest.getLeft(), request, dest.getRight(), equalityTypes);
network.resolveNetworkLock(request); network.resolveNetworkLock(request);

View file

@ -35,13 +35,11 @@ public class ExtractionModuleItem extends ModuleItem {
if (!tile.shouldWorkNow(this.speed) || !tile.canWork()) if (!tile.shouldWorkNow(this.speed) || !tile.canWork())
return; return;
var filter = this.getItemFilter(module, tile); var filter = this.getItemFilter(module, tile);
var dir = this.getDirectionSelector(module, tile).getDirection(); var dirSelector = this.getDirectionSelector(module, tile);
if (dir == null) for (var dir : dirSelector.directions()) {
return;
var handler = tile.getItemHandler(dir); var handler = tile.getItemHandler(dir);
if (handler == null) if (handler == null)
return; continue;
var network = PipeNetwork.get(tile.getLevel()); var network = PipeNetwork.get(tile.getLevel());
for (var j = 0; j < handler.getSlots(); j++) { for (var j = 0; j < handler.getSlots(); j++) {
var stack = handler.extractItem(j, this.maxExtraction, true); var stack = handler.extractItem(j, this.maxExtraction, true);
@ -56,15 +54,16 @@ public class ExtractionModuleItem extends ModuleItem {
} }
} }
} }
}
@Override @Override
public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile, Direction direction, IItemHandler handler) { public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile, Direction direction, IItemHandler handler) {
return direction != this.getDirectionSelector(module, tile).getDirection(); return !this.getDirectionSelector(module, tile).has(direction);
} }
@Override @Override
public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction, IItemHandler destination) { public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction, IItemHandler destination) {
return direction != this.getDirectionSelector(module, tile).getDirection(); return !this.getDirectionSelector(module, tile).has(direction);
} }
@Override @Override

View file

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

View file

@ -34,10 +34,7 @@ public class RetrievalModuleItem extends ModuleItem {
public void tick(ItemStack module, PipeBlockEntity tile) { public void tick(ItemStack module, PipeBlockEntity tile) {
if (!tile.shouldWorkNow(this.speed) || !tile.canWork()) if (!tile.shouldWorkNow(this.speed) || !tile.canWork())
return; return;
var dir = this.getDirectionSelector(module, tile).getDirection(); var directions = this.getDirectionSelector(module, tile).directions();
if (dir == null)
return;
var network = PipeNetwork.get(tile.getLevel()); var network = PipeNetwork.get(tile.getLevel());
var equalityTypes = ItemFilter.getEqualityTypes(tile); var equalityTypes = ItemFilter.getEqualityTypes(tile);
// loop through filters to see which items to pull // loop through filters to see which items to pull
@ -48,13 +45,13 @@ public class RetrievalModuleItem extends ModuleItem {
continue; continue;
var copy = filtered.copy(); var copy = filtered.copy();
copy.setCount(this.maxExtraction); copy.setCount(this.maxExtraction);
var dest = tile.getAvailableDestination(dir, copy, true, this.preventOversending); var dest = tile.getAvailableDestination(directions, copy, true, this.preventOversending);
if (dest.isEmpty()) if (dest.getRight().isEmpty())
continue; continue;
var remain = dest.copy(); var remain = dest.getRight().copy();
// are we already waiting for crafting results? If so, don't request those again // are we already waiting for crafting results? If so, don't request those again
remain.shrink(network.getCurrentlyCraftingAmount(tile.getBlockPos(), copy, equalityTypes)); remain.shrink(network.getCurrentlyCraftingAmount(tile.getBlockPos(), copy, equalityTypes));
if (network.requestItem(tile.getBlockPos(), tile.getBlockPos().relative(dir), remain, equalityTypes).isEmpty()) if (network.requestItem(tile.getBlockPos(), dest.getLeft(), remain, equalityTypes).isEmpty())
break; break;
} }
} }
@ -62,12 +59,12 @@ public class RetrievalModuleItem extends ModuleItem {
@Override @Override
public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile, Direction direction, IItemHandler handler) { public boolean canNetworkSee(ItemStack module, PipeBlockEntity tile, Direction direction, IItemHandler handler) {
return direction != this.getDirectionSelector(module, tile).getDirection(); return !this.getDirectionSelector(module, tile).has(direction);
} }
@Override @Override
public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction, IItemHandler destination) { public boolean canAcceptItem(ItemStack module, PipeBlockEntity tile, ItemStack stack, Direction direction, IItemHandler destination) {
return direction != this.getDirectionSelector(module, tile).getDirection(); return !this.getDirectionSelector(module, tile).has(direction);
} }
@Override @Override

View file

@ -97,5 +97,5 @@
"dir.prettypipes.east": "East", "dir.prettypipes.east": "East",
"dir.prettypipes.south": "South", "dir.prettypipes.south": "South",
"dir.prettypipes.west": "West", "dir.prettypipes.west": "West",
"dir.prettypipes.none": "No Side Valid" "dir.prettypipes.all": "All Sides"
} }