Skip to content

Commit c21696c

Browse files
committed
Forgot about tunnel rotation kek
1 parent 7ef33d3 commit c21696c

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

src/main/java/dev/compactmods/machines/tunnel/TunnelHelper.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package dev.compactmods.machines.tunnel;
22

3+
import net.minecraft.core.Direction;
4+
35
import javax.annotation.Nonnull;
6+
import java.util.Collection;
7+
import java.util.Optional;
8+
import java.util.Set;
49
import java.util.stream.Stream;
5-
import net.minecraft.core.Direction;
610

711
public class TunnelHelper {
812

@@ -21,4 +25,12 @@ public static Direction getNextDirection(Direction in) {
2125
public static Stream<Direction> getOrderedSides() {
2226
return Stream.of(Direction.UP, Direction.DOWN, Direction.NORTH, Direction.SOUTH, Direction.WEST, Direction.EAST);
2327
}
28+
29+
public static Optional<Direction> getNextDirection(Direction current, Set<Direction> used) {
30+
final var ordered = getOrderedSides().toList();
31+
final var found = ordered.indexOf(current);
32+
final var stream = Stream.generate(() -> ordered).flatMap(Collection::stream);
33+
34+
return stream.skip(found + 1).filter(dir -> !used.contains(dir)).findFirst();
35+
}
2436
}

src/main/java/dev/compactmods/machines/tunnel/TunnelWallBlock.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -144,36 +144,36 @@ public InteractionResult use(BlockState state, Level level, BlockPos pos, Player
144144

145145
try {
146146
final var tunnelData = RoomTunnelData.get(serverLevel.getServer(), new ChunkPos(pos));
147+
final var tunnelGraph = tunnelData.getGraph();
148+
final var existingDirs = tunnelGraph
149+
.getTunnelSides(def)
150+
.collect(Collectors.toSet());
147151

148-
final var existingDirs = tunnelData.getGraph()
149-
.getTunnelSides(def)
150-
.collect(Collectors.toSet());
151-
152-
final Optional<Direction> firstNotFound = TunnelHelper.getOrderedSides()
153-
.filter(side -> !existingDirs.contains(side))
154-
.findFirst();
155-
156-
final var next = firstNotFound.orElse(dir);
157-
158-
if (next == dir) {
152+
if (existingDirs.size() == 6) {
159153
// WARN PLAYER - NO OTHER SIDES REMAIN
160154
player.displayClientMessage(
161155
TranslationUtil.message(Messages.NO_TUNNEL_SIDE).withStyle(ChatFormatting.DARK_RED), true);
162156

163157
return InteractionResult.FAIL;
164158
}
165159

166-
level.setBlockAndUpdate(pos, state.setValue(CONNECTED_SIDE, next));
160+
final var next = TunnelHelper.getNextDirection(dir, existingDirs);
161+
next.ifPresent(newSide -> {
162+
level.setBlockAndUpdate(pos, state.setValue(CONNECTED_SIDE, newSide));
163+
164+
if (def instanceof ITunnelTeardown teardown) {
165+
teardown.teardown(new TunnelPosition(serverLevel, pos, tunnelWallSide), tunnel.getTunnel(), TeardownReason.ROTATED);
166+
}
167+
168+
var newTunn = def.newInstance(pos, newSide);
169+
tunnel.setTunnel(newTunn);
170+
171+
tunnelGraph.rotateTunnel(pos, newSide);
172+
tunnelData.setDirty();
173+
});
167174
} catch (MissingDimensionException e) {
168175
return InteractionResult.FAIL;
169176
}
170-
171-
if (def instanceof ITunnelTeardown teardown) {
172-
teardown.teardown(new TunnelPosition(serverLevel, pos, tunnelWallSide), tunnel.getTunnel(), TeardownReason.ROTATED);
173-
}
174-
175-
var newTunn = def.newInstance(pos, tunnelWallSide);
176-
tunnel.setTunnel(newTunn);
177177
}
178178

179179
return InteractionResult.SUCCESS;

src/main/java/dev/compactmods/machines/tunnel/graph/TunnelConnectionGraph.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,4 +510,20 @@ private void cleanupOrphanedMachines() {
510510

511511
removed.forEach(machines::remove);
512512
}
513+
514+
public void rotateTunnel(BlockPos tunnel, Direction newSide) {
515+
if(!tunnels.containsKey(tunnel))
516+
return;
517+
518+
final var connected = connectedMachine(tunnel);
519+
connected.ifPresent(machine -> {
520+
if(!machines.containsKey(machine))
521+
return;
522+
523+
final var t = tunnels.get(tunnel);
524+
final var m = machines.get(machine);
525+
graph.removeEdge(t, m);
526+
graph.putEdgeValue(t, m, new TunnelMachineEdge(newSide));
527+
});
528+
}
513529
}

0 commit comments

Comments
 (0)