From 92d5e12bfe5d7f2b2c6cfb13ac6fe76341938bab Mon Sep 17 00:00:00 2001 From: Uhutown Date: Sun, 15 Sep 2024 13:24:00 +0200 Subject: [PATCH] feat: added system to select pathType when more is possible --- .../signals/guis/ContainerSignalBox.java | 20 ++++--- .../signals/guis/GuiSignalBox.java | 34 +++++++++-- .../troblecodings/signals/guis/SidePanel.java | 7 +-- .../signals/signalbox/PathwayData.java | 2 +- .../signals/signalbox/SignalBoxGrid.java | 56 ++++++++++++------- .../signals/signalbox/SignalBoxNode.java | 9 +-- .../signals/signalbox/SignalBoxPathway.java | 2 +- .../signals/signalbox/SignalBoxUtil.java | 13 ++++- .../PathwayRequesterTileEntity.java | 14 +++-- 9 files changed, 108 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/troblecodings/signals/guis/ContainerSignalBox.java b/src/main/java/com/troblecodings/signals/guis/ContainerSignalBox.java index 4afb19c71..8e6bf7016 100644 --- a/src/main/java/com/troblecodings/signals/guis/ContainerSignalBox.java +++ b/src/main/java/com/troblecodings/signals/guis/ContainerSignalBox.java @@ -24,6 +24,7 @@ import com.troblecodings.signals.core.TrainNumber; import com.troblecodings.signals.enums.EnumGuiMode; import com.troblecodings.signals.enums.LinkType; +import com.troblecodings.signals.enums.PathType; import com.troblecodings.signals.enums.PathwayRequestResult; import com.troblecodings.signals.enums.SignalBoxNetwork; import com.troblecodings.signals.handler.SignalBoxHandler; @@ -48,7 +49,7 @@ public class ContainerSignalBox extends ContainerBase implements UIClientSync, I protected final Map> greenSignals = new HashMap<>(); protected final Map> possibleSubsidiaries = new HashMap<>(); protected final Map> enabledSubsidiaryTypes = new HashMap<>(); - protected final List> nextPathways = new ArrayList<>(); + protected final Map, PathType> nextPathways = new HashMap<>(); protected final Map> validInConnections = new HashMap<>(); protected SignalBoxGrid grid; private final Map propertiesForType = new HashMap<>(); @@ -87,11 +88,12 @@ public void sendAllDataToRemote() { buffer.putBlockPos(pos); buffer.putByte((byte) type.ordinal()); }); - final List> nextPathways = grid.getNextPathways(); + final Map, PathType> nextPathways = grid.getNextPathways(); buffer.putByte((byte) nextPathways.size()); - nextPathways.forEach(entry -> { + nextPathways.forEach((entry, pathType) -> { entry.getKey().writeNetwork(buffer); entry.getValue().writeNetwork(buffer); + buffer.putEnumValue(pathType); }); final Map> validInConnections = new HashMap<>(); positions.entrySet().stream().filter(entry -> entry.getValue().equals(LinkType.SIGNALBOX)) @@ -155,7 +157,8 @@ public void deserializeClient(final ReadBuffer buffer) { for (int i = 0; i < nextPathwaySize; i++) { final Point start = Point.of(buffer); final Point end = Point.of(buffer); - nextPathways.add(Maps.immutableEntry(start, end)); + final PathType type = buffer.getEnumValue(PathType.class); + nextPathways.put(Maps.immutableEntry(start, end), type); } final int validInConnectionsSize = buffer.getByteToUnsignedInt(); for (int i = 0; i < validInConnectionsSize; i++) { @@ -211,7 +214,8 @@ public void deserializeClient(final ReadBuffer buffer) { final PathwayRequestResult result = buffer.getEnumValue(PathwayRequestResult.class); final Point start = Point.of(buffer); final Point end = Point.of(buffer); - nextPathways.add(Maps.immutableEntry(start, end)); + final PathType type = buffer.getEnumValue(PathType.class); + nextPathways.put(Maps.immutableEntry(start, end), type); infoUpdates.accept(I18Wrapper.format("error." + result.getName()) + " - " + I18Wrapper.format("info.pathwaysaver")); break; @@ -336,16 +340,18 @@ public void deserializeServer(final ReadBuffer buffer) { case REQUEST_PW: { final Point start = Point.of(buffer); final Point end = Point.of(buffer); - final PathwayRequestResult request = grid.requestWay(start, end); + final PathType type = buffer.getEnumValue(PathType.class); + final PathwayRequestResult request = grid.requestWay(start, end, type); if (!request.isPass()) { final SignalBoxNode endNode = grid.getNode(end); if (request.canBeAddedToSaver() && !endNode.containsOutConnection() - && grid.addNextPathway(start, end)) { + && grid.addNextPathway(start, end, type)) { final WriteBuffer sucess = new WriteBuffer(); sucess.putEnumValue(SignalBoxNetwork.ADDED_TO_SAVER); sucess.putEnumValue(request); start.writeNetwork(sucess); end.writeNetwork(sucess); + sucess.putEnumValue(type); OpenSignalsMain.network.sendTo(info.player, sucess); break; } diff --git a/src/main/java/com/troblecodings/signals/guis/GuiSignalBox.java b/src/main/java/com/troblecodings/signals/guis/GuiSignalBox.java index c1e86ffaa..1f8ab060d 100644 --- a/src/main/java/com/troblecodings/signals/guis/GuiSignalBox.java +++ b/src/main/java/com/troblecodings/signals/guis/GuiSignalBox.java @@ -48,6 +48,8 @@ import com.troblecodings.signals.enums.EnumGuiMode; import com.troblecodings.signals.enums.EnumPathUsage; import com.troblecodings.signals.enums.LinkType; +import com.troblecodings.signals.enums.PathType; +import com.troblecodings.signals.enums.PathwayRequestResult; import com.troblecodings.signals.enums.ShowTypes; import com.troblecodings.signals.enums.SignalBoxNetwork; import com.troblecodings.signals.enums.SignalBoxPage; @@ -294,7 +296,7 @@ private void tileNormal(final UIEntity tile, final UISignalBoxTile currentTile) return; } if (currentTile.isValidEnd()) { - sendPWRequest(currentTile.getNode()); + checkForMultiplePathTypes(lastTile.getNode(), currentTile.getNode()); this.resetTileSelection(); return; } @@ -303,6 +305,29 @@ private void tileNormal(final UIEntity tile, final UISignalBoxTile currentTile) tile.add(new UIClickable(e -> openNodeShortcuts(currentTile.getNode(), e), 1)); } + private void checkForMultiplePathTypes(final SignalBoxNode start, final SignalBoxNode end) { + final List possibleTypes = start.getPossibleTypes(end); + if (possibleTypes.isEmpty()) { + infoUpdate(I18Wrapper + .format("error." + PathwayRequestResult.NO_EQUAL_PATH_TYPE.getName())); + } else if (possibleTypes.size() == 1) { + sendPWRequest(lastTile.getPoint(), end.getPoint(), possibleTypes.get(0)); + } else if (possibleTypes.size() > 1) { + push(GuiElements.createScreen(entity -> { + entity.add(GuiElements.createButton(I18Wrapper.format("btn.return"), e -> pop())); + entity.add(GuiElements.createSpacerV(10)); + entity.add(GuiElements.createLabel(I18Wrapper.format("gui.signalbox.choosetypes"), + 0xffffff)); + entity.add(GuiElements.createSpacerV(10)); + possibleTypes + .forEach(type -> entity.add(GuiElements.createButton(type.name(), e -> { + sendPWRequest(start.getPoint(), end.getPoint(), type); + pop(); + }))); + })); + } + } + private void resetSelection(final UIEntity entity) { final UIEntity parent = entity.getParent(); parent.findRecursive(UIClickable.class).forEach(click -> click.setVisible(true)); @@ -681,13 +706,14 @@ private void disableBottomEntity() { bottomEntity.getParent().update(); } - private void sendPWRequest(final SignalBoxNode currentNode) { + private void sendPWRequest(final Point start, final Point end, final PathType type) { if (!allPacketsRecived) return; final WriteBuffer buffer = new WriteBuffer(); buffer.putEnumValue(SignalBoxNetwork.REQUEST_PW); - lastTile.getPoint().writeNetwork(buffer); - currentNode.getPoint().writeNetwork(buffer); + start.writeNetwork(buffer); + end.writeNetwork(buffer); + buffer.putEnumValue(type); OpenSignalsMain.network.sendTo(info.player, buffer); } diff --git a/src/main/java/com/troblecodings/signals/guis/SidePanel.java b/src/main/java/com/troblecodings/signals/guis/SidePanel.java index b423a7b5f..8b48f017a 100644 --- a/src/main/java/com/troblecodings/signals/guis/SidePanel.java +++ b/src/main/java/com/troblecodings/signals/guis/SidePanel.java @@ -865,14 +865,13 @@ private void addSavedPathsToUI() { final UIScrollBox scrollbox = new UIScrollBox(UIBox.VBOX, 2); list.add(scrollbox); - gui.container.nextPathways.forEach(entry -> { + gui.container.nextPathways.forEach((entry, pathType) -> { final UIEntity layout = new UIEntity(); layout.setHeight(20); layout.setInheritWidth(true); layout.add(new UIBox(UIBox.HBOX, 2)); - final UIEntity button = GuiElements - .createButton("Start: " + entry.getKey().toShortString() + ", End: " - + entry.getValue().toShortString()); + final UIEntity button = GuiElements.createButton(entry.getKey().toShortString() + + " to " + entry.getValue().toShortString() + " as " + pathType.name()); layout.add(button); layout.add( GuiElements.createButton(I18Wrapper.format("info.usage.show"), 40, _u -> { diff --git a/src/main/java/com/troblecodings/signals/signalbox/PathwayData.java b/src/main/java/com/troblecodings/signals/signalbox/PathwayData.java index 949bff560..6ea062b2b 100644 --- a/src/main/java/com/troblecodings/signals/signalbox/PathwayData.java +++ b/src/main/java/com/troblecodings/signals/signalbox/PathwayData.java @@ -575,7 +575,7 @@ private PathwayData requestInterSignalBoxPathway(final SignalBoxGrid grid) { return; } final PathwayRequestResult endRequeset = SignalBoxUtil.requestPathway(endGrid, - otherStartPoint.get(), otherEndPoint.get()); + otherStartPoint.get(), otherEndPoint.get(), PathType.NORMAL); if (endRequeset.isPass()) { returnResult.set(endRequeset.getPathwayData()); } diff --git a/src/main/java/com/troblecodings/signals/signalbox/SignalBoxGrid.java b/src/main/java/com/troblecodings/signals/signalbox/SignalBoxGrid.java index c2fb72f59..b0925d3fc 100644 --- a/src/main/java/com/troblecodings/signals/signalbox/SignalBoxGrid.java +++ b/src/main/java/com/troblecodings/signals/signalbox/SignalBoxGrid.java @@ -25,6 +25,7 @@ import com.troblecodings.signals.core.SubsidiaryState; import com.troblecodings.signals.core.TrainNumber; import com.troblecodings.signals.enums.EnumPathUsage; +import com.troblecodings.signals.enums.PathType; import com.troblecodings.signals.enums.PathwayRequestResult; import com.troblecodings.signals.enums.SignalBoxNetwork; import com.troblecodings.signals.handler.SignalBoxHandler; @@ -49,10 +50,11 @@ public class SignalBoxGrid implements INetworkSavable { private static final String NEXT_PATHWAYS = "nextPathways"; private static final String START_POINT = "startPoint"; private static final String END_POINT = "endPoint"; + private static final String PATH_TYPE = "pathType"; protected final Map startsToPath = new HashMap<>(); protected final Map endsToPath = new HashMap<>(); - protected final List> nextPathways = new ArrayList<>(); + protected final Map, PathType> nextPathways = new HashMap<>(); protected final Map modeGrid = new HashMap<>(); protected final SignalBoxFactory factory; protected SignalBoxTileEntity tile; @@ -150,8 +152,8 @@ protected void updateToNet(final SignalBoxPathway pathway) { OpenSignalsMain.network.sendTo(tile.get(0).getPlayer(), buffer); } - public PathwayRequestResult requestWay(final Point p1, final Point p2) { - final PathwayRequestResult result = SignalBoxUtil.requestPathway(this, p1, p2); + public PathwayRequestResult requestWay(final Point p1, final Point p2, final PathType type) { + final PathwayRequestResult result = SignalBoxUtil.requestPathway(this, p1, p2, type); if (!result.isPass()) return result; final PathwayData data = result.getPathwayData(); @@ -276,47 +278,52 @@ private void tryReset(final List pathways, final BlockPos pos) tryNextPathways(); } - private final List> toAdd = new ArrayList<>(); + private final Map, PathType> toAdd = new HashMap<>(); private boolean executingForEach = false; private void tryNextPathways() { executingForEach = true; - nextPathways.removeIf(entry -> { - final PathwayRequestResult request = requestWay(entry.getKey(), entry.getValue()); + final Map, PathType> toRemove = new HashMap<>(); + nextPathways.forEach((entry, type) -> { + final PathwayRequestResult request = requestWay(entry.getKey(), entry.getValue(), type); if (request == PathwayRequestResult.PASS) { - if (tile == null || !tile.isBlocked()) - return true; + if (tile == null || !tile.isBlocked()) { + toRemove.put(entry, type); + return; + } final WriteBuffer buffer = new WriteBuffer(); buffer.putEnumValue(SignalBoxNetwork.REMOVE_SAVEDPW); entry.getKey().writeNetwork(buffer); entry.getValue().writeNetwork(buffer); OpenSignalsMain.network.sendTo(tile.get(0).getPlayer(), buffer); - return true; + toRemove.put(entry, type); + return; } - return false; }); executingForEach = false; - toAdd.forEach(nextPathways::add); + toRemove.keySet().forEach(nextPathways::remove); + toRemove.clear(); + toAdd.forEach(nextPathways::put); toAdd.clear(); if (startsToPath.isEmpty()) nextPathways.clear(); } - public List> getNextPathways() { - return ImmutableList.copyOf(nextPathways); + public Map, PathType> getNextPathways() { + return ImmutableMap.copyOf(nextPathways); } - public boolean addNextPathway(final Point start, final Point end) { + public boolean addNextPathway(final Point start, final Point end, final PathType type) { final Map.Entry entry = Maps.immutableEntry(start, end); - if (!nextPathways.contains(entry)) { + if (!nextPathways.containsKey(entry)) { final SignalBoxPathway pw = startsToPath.get(start); if (pw != null && pw.isInterSignalBoxPathway()) { return false; } if (executingForEach) { - toAdd.add(entry); + toAdd.put(entry, type); } else { - nextPathways.add(entry); + nextPathways.put(entry, type); } return true; } @@ -369,14 +376,15 @@ public void writePathways(final NBTWrapper tag) { pathway.write(path); return path; })::iterator); - tag.putList(NEXT_PATHWAYS, nextPathways.stream().map(entry -> { + tag.putList(NEXT_PATHWAYS, nextPathways.entrySet().stream().map(entry -> { final NBTWrapper wrapper = new NBTWrapper(); final NBTWrapper start = new NBTWrapper(); - entry.getKey().write(start); + entry.getKey().getKey().write(start); final NBTWrapper end = new NBTWrapper(); - entry.getValue().write(end); + entry.getKey().getValue().write(end); wrapper.putWrapper(START_POINT, start); wrapper.putWrapper(END_POINT, end); + wrapper.putString(PATH_TYPE, entry.getValue().name()); return wrapper; })::iterator); } @@ -426,7 +434,13 @@ public void readPathways(final NBTWrapper tag) { start.read(comp.getWrapper(START_POINT)); final Point end = new Point(); end.read(comp.getWrapper(END_POINT)); - nextPathways.add(Maps.immutableEntry(start, end)); + if (comp.contains(PATH_TYPE)) { + nextPathways.put(Maps.immutableEntry(start, end), + PathType.valueOf(comp.getString(PATH_TYPE))); + } else { + nextPathways.put(Maps.immutableEntry(start, end), + SignalBoxUtil.getPathTypeFrom(modeGrid.get(start), modeGrid.get(end))); + } }); } diff --git a/src/main/java/com/troblecodings/signals/signalbox/SignalBoxNode.java b/src/main/java/com/troblecodings/signals/signalbox/SignalBoxNode.java index 2a0e5abf1..1895553c9 100644 --- a/src/main/java/com/troblecodings/signals/signalbox/SignalBoxNode.java +++ b/src/main/java/com/troblecodings/signals/signalbox/SignalBoxNode.java @@ -219,9 +219,10 @@ public Optional getOption(final Optional mode) { return mode.flatMap(this::getOption); } - public PathType getPathType(final SignalBoxNode other) { + public List getPossibleTypes(final SignalBoxNode other) { + final List possibleTypes = new ArrayList<>(); if (other == null || other.getPoint().equals(this.getPoint())) - return PathType.NONE; + return possibleTypes; final Set thisMode = this.possibleModes.keySet().stream() .map(mode -> mode.mode).collect(Collectors.toSet()); @@ -233,9 +234,9 @@ public PathType getPathType(final SignalBoxNode other) { final boolean otherContains = Arrays.stream(type.getModes()) .anyMatch(otherMode::contains); if (thisContains && otherContains) - return type; + possibleTypes.add(type); } - return PathType.NONE; + return possibleTypes; } public PathwayRequestResult canMakePath(final Path path, final PathType type) { diff --git a/src/main/java/com/troblecodings/signals/signalbox/SignalBoxPathway.java b/src/main/java/com/troblecodings/signals/signalbox/SignalBoxPathway.java index ff1448220..92f54e4cb 100644 --- a/src/main/java/com/troblecodings/signals/signalbox/SignalBoxPathway.java +++ b/src/main/java/com/troblecodings/signals/signalbox/SignalBoxPathway.java @@ -635,7 +635,7 @@ protected boolean loadTileAndExecute(final BlockPos tilePos, public void checkReRequest() { if (isAutoPathway) { - grid.requestWay(originalFirstPoint, getLastPoint()); + grid.requestWay(originalFirstPoint, getLastPoint(), getPathType()); } } diff --git a/src/main/java/com/troblecodings/signals/signalbox/SignalBoxUtil.java b/src/main/java/com/troblecodings/signals/signalbox/SignalBoxUtil.java index 01a97b178..5e3df6967 100644 --- a/src/main/java/com/troblecodings/signals/signalbox/SignalBoxUtil.java +++ b/src/main/java/com/troblecodings/signals/signalbox/SignalBoxUtil.java @@ -67,13 +67,11 @@ public Point getPoint() { } public static PathwayRequestResult requestPathway(final SignalBoxGrid grid, final Point p1, - final Point p2) { + final Point p2, final PathType pathType) { final Map modeGrid = grid.modeGrid; if (!modeGrid.containsKey(p1) || !modeGrid.containsKey(p2)) return PathwayRequestResult.NOT_IN_GRID; - final SignalBoxNode lastNode = modeGrid.get(p2); final SignalBoxNode firstNode = modeGrid.get(p1); - final PathType pathType = firstNode.getPathType(lastNode); if (pathType.equals(PathType.NONE)) return PathwayRequestResult.NO_EQUAL_PATH_TYPE; @@ -209,4 +207,13 @@ public static int getDefaultCosts(final ModeSet mode) { } } + public static PathType getPathTypeFrom(final SignalBoxNode start, final SignalBoxNode end) { + final List possilbeTypes = start.getPossibleTypes(end); + if (!possilbeTypes.isEmpty()) { + return possilbeTypes.get(0); + } else { + return PathType.NONE; + } + } + } \ No newline at end of file diff --git a/src/main/java/com/troblecodings/signals/tileentitys/PathwayRequesterTileEntity.java b/src/main/java/com/troblecodings/signals/tileentitys/PathwayRequesterTileEntity.java index 9456d46ed..9cca73afd 100644 --- a/src/main/java/com/troblecodings/signals/tileentitys/PathwayRequesterTileEntity.java +++ b/src/main/java/com/troblecodings/signals/tileentitys/PathwayRequesterTileEntity.java @@ -7,10 +7,12 @@ import com.troblecodings.linkableapi.ILinkableTile; import com.troblecodings.signals.OpenSignalsMain; import com.troblecodings.signals.blocks.SignalBox; +import com.troblecodings.signals.enums.PathType; import com.troblecodings.signals.enums.PathwayRequestResult; import com.troblecodings.signals.signalbox.Point; import com.troblecodings.signals.signalbox.SignalBoxGrid; import com.troblecodings.signals.signalbox.SignalBoxTileEntity; +import com.troblecodings.signals.signalbox.SignalBoxUtil; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; @@ -62,10 +64,14 @@ public void saveWrapper(final NBTWrapper wrapper) { public void requestPathway() { loadChunkAndGetTile(SignalBoxTileEntity.class, world, linkedSignalBox, (tile, _u) -> { final SignalBoxGrid grid = tile.getSignalBoxGrid(); - final PathwayRequestResult result = grid.requestWay(pathway.getKey(), - pathway.getValue()); - if (!result.isPass() && result.canBeAddedToSaver() && addPWToSaver) { - grid.addNextPathway(pathway.getKey(), pathway.getValue()); + final PathType type = SignalBoxUtil.getPathTypeFrom(grid.getNode(pathway.getKey()), + grid.getNode(pathway.getValue())); + if (!type.equals(PathType.NONE)) { + final PathwayRequestResult result = grid.requestWay(pathway.getKey(), + pathway.getValue(), type); + if (!result.isPass() && result.canBeAddedToSaver() && addPWToSaver) { + grid.addNextPathway(pathway.getKey(), pathway.getValue(), type); + } } });