Skip to content

Commit

Permalink
feat: added backend system for red and green signals in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Uhutown committed Oct 5, 2023
1 parent c25f6b1 commit c6a6c3e
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 11 deletions.
51 changes: 51 additions & 0 deletions src/main/java/com/troblecodings/signals/core/ModeIdentifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.troblecodings.signals.core;

import java.util.Objects;

import com.troblecodings.core.ReadBuffer;
import com.troblecodings.core.WriteBuffer;
import com.troblecodings.signals.signalbox.ModeSet;
import com.troblecodings.signals.signalbox.Point;

public class ModeIdentifier {

public final Point point;
public final ModeSet mode;

public ModeIdentifier(final Point point, final ModeSet mode) {
this.point = point;
this.mode = mode;
}

public void writeNetwork(final WriteBuffer buffer) {
point.writeNetwork(buffer);
mode.writeNetwork(buffer);
}

public static ModeIdentifier of(final ReadBuffer buffer) {
return new ModeIdentifier(Point.of(buffer), ModeSet.of(buffer));
}

@Override
public int hashCode() {
return Objects.hash(mode, point);
}

@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final ModeIdentifier other = (ModeIdentifier) obj;
return Objects.equals(mode, other.mode) && Objects.equals(point, other.point);
}

@Override
public String toString() {
return "ModeIdentifier [point=" + point + ",mode=" + mode + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public enum SignalBoxNetwork {
SEND_PW_UPDATE, RESET_ALL_PW, SEND_CHANGED_MODES, REQUEST_LINKED_POS, NO_PW_FOUND,
REQUEST_SUBSIDIARY, SEND_ZS2_ENTRY, UPDATE_RS_OUTPUT, OUTPUT_UPDATE, RESET_SUBSIDIARY,
SET_AUTO_POINT, SEND_NAME, SEND_SIGNAL_REPEATER, ADDED_TO_SAVER, REMOVE_SAVEDPW,
SEND_POINT_ENTRY;
SEND_POINT_ENTRY, SET_SIGNALS;

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.troblecodings.guilib.ecs.GuiInfo;
import com.troblecodings.guilib.ecs.interfaces.UIClientSync;
import com.troblecodings.signals.OpenSignalsMain;
import com.troblecodings.signals.core.ModeIdentifier;
import com.troblecodings.signals.core.PosIdentifier;
import com.troblecodings.signals.core.SubsidiaryEntry;
import com.troblecodings.signals.core.SubsidiaryState;
Expand All @@ -37,6 +38,7 @@

public class ContainerSignalBox extends ContainerBase implements UIClientSync {

protected final List<ModeIdentifier> greenSignals = new ArrayList<>();
protected final Map<BlockPos, List<SubsidiaryState>> possibleSubsidiaries = new HashMap<>();
protected final Map<Point, Map<ModeSet, SubsidiaryEntry>> enabledSubsidiaryTypes = new HashMap<>();
protected final List<Map.Entry<Point, Point>> nextPathways = new ArrayList<>();
Expand Down Expand Up @@ -96,6 +98,9 @@ public void sendAllDataToRemote() {
buffer.putByte((byte) list.size());
list.forEach(point -> point.writeNetwork(buffer));
});
final List<ModeIdentifier> greenSignals = SignalBoxHandler.getGreenSignals(identifier);
buffer.putInt(greenSignals.size());
greenSignals.forEach(signal -> signal.writeNetwork(buffer));
OpenSignalsMain.network.sendTo(info.player, buffer);
}

Expand All @@ -115,6 +120,7 @@ public void deserializeClient(final ReadBuffer buffer) {
possibleSubsidiaries.clear();
nextPathways.clear();
validInConnections.clear();
greenSignals.clear();
final int signalSize = buffer.getInt();
for (int i = 0; i < signalSize; i++) {
final BlockPos signalPos = buffer.getBlockPos();
Expand Down Expand Up @@ -149,6 +155,10 @@ public void deserializeClient(final ReadBuffer buffer) {
}
validInConnections.put(boxPos, points);
}
final int greenSignalsSize = buffer.getInt();
for (int i = 0; i < greenSignalsSize; i++) {
greenSignals.add(ModeIdentifier.of(buffer));
}
update();
break;
}
Expand Down Expand Up @@ -185,6 +195,19 @@ public void deserializeClient(final ReadBuffer buffer) {
nextPathways.remove(Maps.immutableEntry(start, end));
break;
}
case SET_SIGNALS: {
final int redSignalSize = buffer.getByteToUnsignedInt();
for (int i = 0; i < redSignalSize; i++) {
greenSignals.remove(ModeIdentifier.of(buffer));
}
final int greenSignalSize = buffer.getByteToUnsignedInt();
for (int i = 0; i < greenSignalSize; i++) {
final ModeIdentifier modeIdentifier = ModeIdentifier.of(buffer);
if (!greenSignals.contains(modeIdentifier))
greenSignals.add(modeIdentifier);
}
break;
}
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.troblecodings.signals.blocks.Signal;
import com.troblecodings.signals.core.LinkedPositions;
import com.troblecodings.signals.core.LinkingUpdates;
import com.troblecodings.signals.core.ModeIdentifier;
import com.troblecodings.signals.core.PosIdentifier;
import com.troblecodings.signals.core.RedstoneUpdatePacket;
import com.troblecodings.signals.core.SubsidiaryState;
Expand Down Expand Up @@ -224,6 +225,18 @@ public static PathwayHolder getPathwayHolder(final PosIdentifier identifier) {
}
}

public static List<ModeIdentifier> getGreenSignals(final PosIdentifier identifier) {
if (identifier.world.isClientSide)
return new ArrayList<>();
PathwayHolder holder;
synchronized (ALL_GRIDS) {
holder = ALL_GRIDS.get(identifier);
}
if (holder == null)
return new ArrayList<>();
return holder.getGreenSignals();
}

public static boolean addNextPathway(final PosIdentifier identifier, final Point start,
final Point end) {
if (identifier.world.isClientSide)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

import java.util.Objects;

import com.troblecodings.signals.core.ModeIdentifier;

import net.minecraft.core.BlockPos;

public class MainSignalIdentifier {

public final Point point;
public final ModeSet mode;
public final ModeIdentifier identifier;
public final BlockPos pos;
public SignalState state = SignalState.RED;

public MainSignalIdentifier(final Point point, final ModeSet mode, final BlockPos pos) {
this.point = point;
this.mode = mode;
this.identifier = new ModeIdentifier(point, mode);
this.pos = pos;
}

@Override
public int hashCode() {
return Objects.hash(mode, point, pos);
return Objects.hash(identifier, pos);
}

@Override
Expand All @@ -30,7 +31,12 @@ public boolean equals(final Object obj) {
if (getClass() != obj.getClass())
return false;
final MainSignalIdentifier other = (MainSignalIdentifier) obj;
return Objects.equals(mode, other.mode) && Objects.equals(point, other.point)
&& Objects.equals(pos, other.pos);
return Objects.equals(identifier, other.identifier) && Objects.equals(pos, other.pos);
}

public static enum SignalState {

RED, GREEN;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.troblecodings.core.WriteBuffer;
import com.troblecodings.signals.OpenSignalsMain;
import com.troblecodings.signals.blocks.CombinedRedstoneInput;
import com.troblecodings.signals.core.ModeIdentifier;
import com.troblecodings.signals.core.RedstoneUpdatePacket;
import com.troblecodings.signals.enums.EnumPathUsage;
import com.troblecodings.signals.enums.SignalBoxNetwork;
Expand All @@ -30,9 +31,9 @@ public class PathwayHolder implements IChunkLoadable {
private static final String START_POINT = "startPoint";
private static final String END_POINT = "endPoint";

private final Map<Point, SignalBoxPathway> startsToPath = new HashMap<>();
private final Map<Point, SignalBoxPathway> endsToPath = new HashMap<>();
private final List<Map.Entry<Point, Point>> nextPathways = new ArrayList<>();
protected final Map<Point, SignalBoxPathway> startsToPath = new HashMap<>();
protected final Map<Point, SignalBoxPathway> endsToPath = new HashMap<>();
protected final List<Map.Entry<Point, Point>> nextPathways = new ArrayList<>();
private Level world;
private final BlockPos tilePos;
private Map<Point, SignalBoxNode> modeGrid = new HashMap<>();
Expand Down Expand Up @@ -105,6 +106,12 @@ public SignalBoxNode getNode(final Point point) {
return modeGrid.get(point);
}

public List<ModeIdentifier> getGreenSignals() {
final List<ModeIdentifier> returnList = new ArrayList<>();
startsToPath.values().forEach(pathway -> returnList.addAll(pathway.getGreenSignals()));
return returnList;
}

public boolean requestWay(final Point p1, final Point p2) {
if (startsToPath.containsKey(p1) || endsToPath.containsKey(p2))
return false;
Expand All @@ -117,6 +124,7 @@ public boolean requestWay(final Point p1, final Point p2) {
updatePrevious(pathway);
updateToNet(pathway);
});
way.setPathwayHolder(this);
way.setPathStatus(EnumPathUsage.SELECTED);
way.updatePathwaySignals();
this.onWayAdd(way);
Expand Down Expand Up @@ -296,6 +304,7 @@ public void read(final NBTWrapper tag) {
updatePrevious(way);
updateToNet(way);
});
pathway.setPathwayHolder(this);
pathway.setWorldAndPos(world, tilePos);
pathway.read(comp);
if (pathway.isEmptyOrBroken()) {
Expand Down
Loading

0 comments on commit c6a6c3e

Please sign in to comment.