|
| 1 | +package gregtech.api.pipenet; |
| 2 | + |
| 3 | +import gregtech.api.pipenet.tile.IPipeTile; |
| 4 | +import gregtech.api.util.GTLog; |
| 5 | +import gregtech.common.pipelike.fluidpipe.net.FluidNetWalker; |
| 6 | +import gregtech.common.pipelike.itempipe.net.ItemNetWalker; |
| 7 | +import net.minecraft.tileentity.TileEntity; |
| 8 | +import net.minecraft.util.EnumFacing; |
| 9 | +import net.minecraft.util.math.BlockPos; |
| 10 | +import net.minecraft.world.World; |
| 11 | + |
| 12 | +import javax.annotation.Nullable; |
| 13 | +import java.util.*; |
| 14 | + |
| 15 | +/** |
| 16 | + * This is a helper class to get information about a pipe net |
| 17 | + * <p>The walker is written that it will always find the shortest path to any destination |
| 18 | + * <p>On the way it can collect information about the pipes and it's neighbours |
| 19 | + * <p>After creating a walker simply call {@link #traversePipeNet()} to start walking, then you can just collect the data |
| 20 | + * <p><b>Do not walk a walker more than once</b> |
| 21 | + * <p>For example implementations look at {@link ItemNetWalker} and {@link FluidNetWalker} |
| 22 | + */ |
| 23 | +public abstract class PipeNetWalker { |
| 24 | + |
| 25 | + private final PipeNet<?> net; |
| 26 | + private final World world; |
| 27 | + private final Set<IPipeTile<?, ?>> walked = new HashSet<>(); |
| 28 | + private final List<EnumFacing> pipes = new ArrayList<>(); |
| 29 | + private List<PipeNetWalker> walkers; |
| 30 | + private BlockPos currentPos; |
| 31 | + private int walkedBlocks; |
| 32 | + private boolean invalid; |
| 33 | + |
| 34 | + protected PipeNetWalker(PipeNet<?> net, World world, BlockPos sourcePipe, int walkedBlocks) { |
| 35 | + this.world = Objects.requireNonNull(world); |
| 36 | + this.net = Objects.requireNonNull(net); |
| 37 | + this.walkedBlocks = walkedBlocks; |
| 38 | + this.currentPos = Objects.requireNonNull(sourcePipe); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates a sub walker |
| 43 | + * Will be called when a pipe has multiple valid pipes |
| 44 | + * |
| 45 | + * @param net pipe net |
| 46 | + * @param world world |
| 47 | + * @param nextPos next pos to check |
| 48 | + * @param walkedBlocks distance from source in blocks |
| 49 | + * @return new sub walker |
| 50 | + */ |
| 51 | + protected abstract PipeNetWalker createSubWalker(PipeNet<?> net, World world, BlockPos nextPos, int walkedBlocks); |
| 52 | + |
| 53 | + /** |
| 54 | + * You can increase walking stats here. for example |
| 55 | + * |
| 56 | + * @param pipeTile current checking pipe |
| 57 | + * @param pos current pipe pos |
| 58 | + */ |
| 59 | + protected abstract void checkPipe(IPipeTile<?, ?> pipeTile, BlockPos pos); |
| 60 | + |
| 61 | + /** |
| 62 | + * Checks the neighbour of the current pos |
| 63 | + * neighbourTile is NEVER an instance of {@link IPipeTile} |
| 64 | + * |
| 65 | + * @param pipePos current pos |
| 66 | + * @param faceToNeighbour face to neighbour |
| 67 | + * @param neighbourTile neighbour tile |
| 68 | + */ |
| 69 | + protected abstract void checkNeighbour(IPipeTile<?, ?> pipeTile, BlockPos pipePos, EnumFacing faceToNeighbour, @Nullable TileEntity neighbourTile); |
| 70 | + |
| 71 | + /** |
| 72 | + * If the pipe is valid to perform a walk on |
| 73 | + * |
| 74 | + * @param currentPipe current pipe |
| 75 | + * @param neighbourPipe neighbour pipe to check |
| 76 | + * @param pipePos current pos (tile.getPipePos() != pipePos) |
| 77 | + * @param faceToNeighbour face to pipeTile |
| 78 | + * @return if the pipe is valid |
| 79 | + */ |
| 80 | + protected abstract boolean isValidPipe(IPipeTile<?, ?> currentPipe, IPipeTile<?, ?> neighbourPipe, BlockPos pipePos, EnumFacing faceToNeighbour); |
| 81 | + |
| 82 | + public void traversePipeNet() { |
| 83 | + traversePipeNet(Integer.MAX_VALUE); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Starts walking the pipe net and gathers information. |
| 88 | + * |
| 89 | + * @param maxWalks max walks to prevent possible stack overflow |
| 90 | + * @throws IllegalStateException if the walker already walked |
| 91 | + */ |
| 92 | + public void traversePipeNet(int maxWalks) { |
| 93 | + if (invalid) |
| 94 | + throw new IllegalStateException("This walker already walked. Create a new one if you want to walk again"); |
| 95 | + int i = 0; |
| 96 | + while (!walk() && i++ < maxWalks) ; |
| 97 | + walked.forEach(IPipeTile::resetWalk); |
| 98 | + invalid = true; |
| 99 | + } |
| 100 | + |
| 101 | + private boolean walk() { |
| 102 | + if (walkers == null) |
| 103 | + checkPos(currentPos); |
| 104 | + |
| 105 | + if (pipes.size() == 0) |
| 106 | + return true; |
| 107 | + if (pipes.size() == 1) { |
| 108 | + currentPos = currentPos.offset(pipes.get(0)); |
| 109 | + walkedBlocks++; |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + if (walkers == null) { |
| 114 | + walkers = new ArrayList<>(); |
| 115 | + for (EnumFacing side : pipes) { |
| 116 | + walkers.add(Objects.requireNonNull(createSubWalker(net, world, currentPos.offset(side), walkedBlocks + 1), "Walker can't be null")); |
| 117 | + } |
| 118 | + } else { |
| 119 | + Iterator<PipeNetWalker> iterator = walkers.iterator(); |
| 120 | + while (iterator.hasNext()) { |
| 121 | + PipeNetWalker walker = iterator.next(); |
| 122 | + if (walker.walk()) { |
| 123 | + walked.addAll(walker.walked); |
| 124 | + iterator.remove(); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return walkers.size() == 0; |
| 130 | + } |
| 131 | + |
| 132 | + private void checkPos(BlockPos pos) { |
| 133 | + pipes.clear(); |
| 134 | + TileEntity thisPipe = world.getTileEntity(pos); |
| 135 | + IPipeTile<?, ?> pipeTile = (IPipeTile<?, ?>) thisPipe; |
| 136 | + if (pipeTile == null) { |
| 137 | + if (walkedBlocks == 1) { |
| 138 | + // if it is the first block, it wasn't already checked |
| 139 | + GTLog.logger.warn("First PipeTile is null during walk"); |
| 140 | + return; |
| 141 | + } else |
| 142 | + throw new IllegalStateException("PipeTile was not null last walk, but now is"); |
| 143 | + } |
| 144 | + checkPipe(pipeTile, pos); |
| 145 | + pipeTile.markWalked(); |
| 146 | + walked.add(pipeTile); |
| 147 | + |
| 148 | + // check for surrounding pipes and item handlers |
| 149 | + for (EnumFacing accessSide : EnumFacing.VALUES) { |
| 150 | + //skip sides reported as blocked by pipe network |
| 151 | + if (!pipeTile.isConnectionOpenAny(accessSide)) |
| 152 | + continue; |
| 153 | + |
| 154 | + TileEntity tile = world.getTileEntity(pos.offset(accessSide)); |
| 155 | + if (tile instanceof IPipeTile) { |
| 156 | + IPipeTile<?, ?> otherPipe = (IPipeTile<?, ?>) tile; |
| 157 | + if (otherPipe.isWalked()) |
| 158 | + continue; |
| 159 | + if (isValidPipe(pipeTile, otherPipe, pos, accessSide)) { |
| 160 | + pipes.add(accessSide); |
| 161 | + continue; |
| 162 | + } |
| 163 | + } |
| 164 | + checkNeighbour(pipeTile, pos, accessSide, tile); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + public PipeNet<?> getNet() { |
| 169 | + return net; |
| 170 | + } |
| 171 | + |
| 172 | + public World getWorld() { |
| 173 | + return world; |
| 174 | + } |
| 175 | + |
| 176 | + public BlockPos getCurrentPos() { |
| 177 | + return currentPos; |
| 178 | + } |
| 179 | + |
| 180 | + public int getWalkedBlocks() { |
| 181 | + return walkedBlocks; |
| 182 | + } |
| 183 | +} |
0 commit comments