Skip to content

Commit

Permalink
the end is not the end is not the end is not the end is not the end i…
Browse files Browse the repository at this point in the history
…s not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not the end is not
  • Loading branch information
Azzyypaaras committed Jan 13, 2025
1 parent 2393a0e commit cbde9ed
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.client.util.math.*;
import net.minecraft.util.math.*;
import net.minecraft.world.*;
import org.jetbrains.annotations.*;
import org.jgrapht.*;
import org.jgrapht.graph.*;
import org.joml.*;
Expand Down Expand Up @@ -52,58 +53,62 @@ public Optional<? extends PastelNetwork> getNetwork(UUID uuid) {
}

@Override
public void connectNodes(PastelNodeBlockEntity node, PastelNodeBlockEntity parent) {
PastelNetwork parentNetwork, otherNetwork;
public void connectNodes(PastelNodeBlockEntity node, PastelNodeBlockEntity parent, @NotNull UUID id) {
PastelNetwork mainNetwork, yieldingNetwork;

if (parent.getParentNetwork() != null) {
parentNetwork = parent.getParentNetwork();
otherNetwork = node.getParentNetwork();
mainNetwork = parent.getParentNetwork();
yieldingNetwork = node.getParentNetwork();

if (otherNetwork == null) {
parentNetwork.addNodeAndConnect(node, parent);
node.setParentNetwork(parentNetwork);
if (yieldingNetwork == null) {
mainNetwork.addNodeAndConnect(node, parent);
node.setParentNetwork(mainNetwork);
return;
}
}
else if (node.getParentNetwork() != null) {
parentNetwork = node.getParentNetwork();
otherNetwork = parent.getParentNetwork();
mainNetwork = node.getParentNetwork();
yieldingNetwork = parent.getParentNetwork();

if (otherNetwork == null) {
parentNetwork.addNodeAndConnect(parent, node);
parent.setParentNetwork(parentNetwork);
if (yieldingNetwork == null) {
mainNetwork.addNodeAndConnect(parent, node);
parent.setParentNetwork(mainNetwork);
return;
}
}
else {
parentNetwork = createNetwork(node.getWorld(), null);
parentNetwork.addNode(parent);
parent.setParentNetwork(parentNetwork);
parentNetwork.addNodeAndConnect(node, parent);
node.setParentNetwork(parentNetwork);
mainNetwork = createNetwork(node.getWorld(), id);
mainNetwork.addNode(parent);
parent.setParentNetwork(mainNetwork);
mainNetwork.addNodeAndConnect(node, parent);
node.setParentNetwork(mainNetwork);
return;
}

if (parentNetwork == otherNetwork) {
if (mainNetwork == yieldingNetwork) {
return;
}

parentNetwork.incorporate(otherNetwork, node, parent);
this.networks.remove(otherNetwork);
mainNetwork.incorporate(yieldingNetwork, node, parent);
this.networks.remove(yieldingNetwork);
}

@Override
public void removeNode(PastelNodeBlockEntity node, NodeRemovalReason reason) {
PastelNetwork network = node.getParentNetwork();
if (network != null) {
network.removeNode(node, reason);
if (network.loadedNodes.isEmpty()) {
if (network.graph.vertexSet().isEmpty()) {
this.networks.remove(network);
}
else {
checkForNetworkSplit(network);
}
}
}

private PastelNetwork createNetwork(World world, UUID uuid) {
@Override
public PastelNetwork createNetwork(World world, UUID uuid) {
PastelNetwork network = new PastelNetwork(world, uuid);
this.networks.add(network);
return network;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ public void updateNodePriority(PastelNodeBlockEntity node, Priority oldPriority)
}

protected boolean removeNode(PastelNodeBlockEntity node, NodeRemovalReason reason) {
boolean hadNode = this.loadedNodes.get(node.getNodeType()).remove(node);
if (!hadNode) {
if (!graph.containsVertex(node.getPos())) {
return false;
}

// delete the now removed node from this networks graph - IF IT WASN'T UNLOADED
if (reason != NodeRemovalReason.UNLOADED)
graph.removeVertex(node.getPos());


this.loadedNodes.get(node.getNodeType()).remove(node);
removePriorityNode(node, node.getPriority());

return true;
Expand Down Expand Up @@ -284,16 +284,18 @@ public NbtCompound toNbt() {
graphStorage.putLong("Vertex" + i, vertex.asLong());

// Save the edges
int currentVertex = i;
var edgeIndexes = graph.edgesOf(vertex)
.stream()
.map((edge) -> {
var target = graph.getEdgeTarget(edge);
var source = graph.getEdgeSource(edge);
return target.equals(vertex) ? source : target;
})
.map(graph::getEdgeTarget)
.mapToInt(vertices::indexOf)
.filter(v -> v != currentVertex)
.boxed()
.collect(Collectors.toList());

if (edgeIndexes.isEmpty())
continue;

edgeIndexes.add(0, vertices.indexOf(vertex));

graphStorage.putIntArray("EdgeIndexes" + i, edgeIndexes);
Expand Down Expand Up @@ -325,11 +327,13 @@ public void fromNbt(NbtCompound compound) {
}

for (int i = 0; i < size; i++) {
if (!graphStorage.contains("EdgeIndexes" + i))
continue;
var edgeIndexes = graphStorage.getIntArray("EdgeIndexes" + i);
var source = vertices.get(edgeIndexes[0]);
for (int targetIndex = 1; targetIndex < edgeIndexes.length; targetIndex++) {
var target = vertices.get(targetIndex);
if (!graph.containsEdge(source, target) && !source.equals(target))
var target = vertices.get(edgeIndexes[targetIndex]);
if (!graph.containsEdge(source, target))
graph.addEdge(source, target);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package de.dafuqs.spectrum.blocks.pastel_network.network;

import de.dafuqs.spectrum.blocks.pastel_network.nodes.*;
import net.minecraft.util.math.*;
import net.minecraft.world.*;
import org.jetbrains.annotations.*;
import org.jgrapht.alg.connectivity.*;
import org.jgrapht.graph.*;

import java.util.*;

Expand All @@ -11,13 +15,18 @@ public interface PastelNetworkManager {

PastelNetwork joinOrCreateNetwork(PastelNodeBlockEntity node, @Nullable UUID uuid);

void connectNodes(PastelNodeBlockEntity node, PastelNodeBlockEntity parent);
void connectNodes(PastelNodeBlockEntity node, PastelNodeBlockEntity parent, @NotNull UUID id);

default void connectNodes(PastelNodeBlockEntity node, PastelNodeBlockEntity parent) {
connectNodes(node, parent, UUID.randomUUID());
}

//void removeEmptyNetwork(PastelNetwork network);

void removeNode(PastelNodeBlockEntity node, NodeRemovalReason reason);



PastelNetwork createNetwork(World world, UUID uuid);

Optional<? extends PastelNetwork> getNetwork(UUID uuid);

default boolean tryAddEdge(PastelNodeBlockEntity node, PastelNodeBlockEntity otherNode) {
Expand Down Expand Up @@ -49,6 +58,41 @@ default boolean tryRemoveEdge(PastelNodeBlockEntity node, PastelNodeBlockEntity
return false;

node.getParentNetwork().removeEdge(node, otherNode);
checkForNetworkSplit(node.getParentNetwork());
return true;
}

default void checkForNetworkSplit(PastelNetwork network) {
ConnectivityInspector<BlockPos, DefaultEdge> connectivityInspector = new ConnectivityInspector<>(network.getGraph());
List<Set<BlockPos>> connectedSets = connectivityInspector.connectedSets();
if (connectedSets.size() != 1) {
for (int i = 1; i < connectedSets.size(); i++) {
Set<BlockPos> disconnectedNodes = connectedSets.get(i);
Map<BlockPos, List<BlockPos>> transitiveEdges = new HashMap<>();
PastelNetwork newNetwork = createNetwork(network.world, ((PastelNodeBlockEntity) (network.world.getBlockEntity(disconnectedNodes.iterator().next()))).getInitialID());
for (BlockPos disconnectedNode : disconnectedNodes) {
for (DefaultEdge switchedEdge : network.getGraph().edgesOf(disconnectedNode)) {
var edgeList = transitiveEdges.computeIfAbsent(disconnectedNode, p -> new ArrayList<>());
var target = network.graph.getEdgeTarget(switchedEdge);
if (!target.equals(disconnectedNode) && !edgeList.contains(target) && disconnectedNodes.contains(target))
edgeList.add(target);
}
}
for (BlockPos disconnectedNode : disconnectedNodes) {
var switchedNode = network.getWorld().getBlockEntity(disconnectedNode);
if (switchedNode instanceof PastelNodeBlockEntity pastelNode) {
network.removeNode(pastelNode, NodeRemovalReason.DISCONNECT);
newNetwork.addNode(pastelNode);
pastelNode.setParentNetwork(newNetwork);
}
}
for (BlockPos node : transitiveEdges.keySet()) {
for (BlockPos target : transitiveEdges.get(node)) {
if (!newNetwork.graph.containsEdge(node, target))
newNetwork.graph.addEdge(node, target);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public static ServerPastelNetworkManager fromNbt(NbtCompound nbt) {
return manager;
}

private ServerPastelNetwork createNetwork(World world, @Nullable UUID uuid) {
@Override
public ServerPastelNetwork createNetwork(World world, @Nullable UUID uuid) {
ServerPastelNetwork network = new ServerPastelNetwork(world, uuid);
this.networks.add(network);
return network;
Expand Down Expand Up @@ -99,7 +100,7 @@ public PastelNetwork joinOrCreateNetwork(PastelNodeBlockEntity node, @Nullable U
}

@Override
public void connectNodes(PastelNodeBlockEntity node, PastelNodeBlockEntity parent) {
public void connectNodes(PastelNodeBlockEntity node, PastelNodeBlockEntity parent, @NotNull UUID id) {
PastelNetwork mainNetwork, yieldingNetwork;

if (parent.getParentNetwork() != null) {
Expand All @@ -123,7 +124,7 @@ else if (node.getParentNetwork() != null) {
}
}
else {
mainNetwork = createNetwork(node.getWorld(), null);
mainNetwork = createNetwork(node.getWorld(), id);
mainNetwork.addNode(parent);
parent.setParentNetwork(mainNetwork);
mainNetwork.addNodeAndConnect(node, parent);
Expand All @@ -139,15 +140,6 @@ else if (node.getParentNetwork() != null) {
this.networks.remove(yieldingNetwork);
}

@Override
public boolean tryRemoveEdge(PastelNodeBlockEntity node, PastelNodeBlockEntity otherNode) {
if (PastelNetworkManager.super.tryRemoveEdge(node, otherNode)) {
checkForNetworkSplit((ServerPastelNetwork) node.getParentNetwork());
return true;
}
return false;
}

@Override
public void removeNode(PastelNodeBlockEntity node, NodeRemovalReason reason) {
ServerPastelNetwork network = (ServerPastelNetwork) node.getParentNetwork();
Expand All @@ -166,54 +158,4 @@ public void removeNode(PastelNodeBlockEntity node, NodeRemovalReason reason) {
}
}

private void checkForNetworkSplit(ServerPastelNetwork network) {
ConnectivityInspector<BlockPos, DefaultEdge> connectivityInspector = new ConnectivityInspector<>(network.getGraph());
List<Set<BlockPos>> connectedSets = connectivityInspector.connectedSets();
if (connectedSets.size() != 1) {
for (int i = 1; i < connectedSets.size(); i++) {
Set<BlockPos> disconnectedNodes = connectedSets.get(i);
PastelNetwork newNetwork = createNetwork(network.world, null);
for (BlockPos disconnectedNode : disconnectedNodes) {
var switchedNode = network.getWorld().getBlockEntity(disconnectedNode);
if (switchedNode instanceof PastelNodeBlockEntity pastelNode) {
network.removeNode(pastelNode, NodeRemovalReason.DISCONNECT);
newNetwork.addNode(pastelNode);
pastelNode.setParentNetwork(newNetwork);
}
}
}
}
}

private void checkNetworkMergesForNewNode(ServerPastelNetwork network, PastelNodeBlockEntity newNode) {
int biggestNetworkNodeCount = network.getNodeCount();

ServerPastelNetwork biggestNetwork = network;
List<ServerPastelNetwork> smallerNetworks = new ArrayList<>();

for (ServerPastelNetwork currentNetwork : this.networks) {
if (currentNetwork == network) {
continue;
}
if (currentNetwork.canConnect(newNode)) {
if (currentNetwork.getNodeCount() > biggestNetworkNodeCount) {
smallerNetworks.add(biggestNetwork);
biggestNetwork = currentNetwork;
} else {
smallerNetworks.add(currentNetwork);
}
break;
}
}

if (smallerNetworks.isEmpty()) {
return;
}

for (ServerPastelNetwork smallerNetwork : smallerNetworks) {
//biggestNetwork.incorporate(smallerNetwork);
this.networks.remove(smallerNetwork);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class PastelNodeBlockEntity extends BlockEntity implements FilterConfigur

@Nullable
protected PastelNetwork parentNetwork;
@NotNull
protected UUID initialID = UUID.randomUUID();
protected Optional<UUID> parentID = Optional.empty();
protected Optional<PastelUpgradeSignature> outerRing, innerRing, redstoneRing;
protected long lastTransferTick = 0;
Expand Down Expand Up @@ -352,6 +354,9 @@ public void readNbt(NbtCompound nbt) {
if (nbt.contains("creationStamp")) {
this.creationStamp = nbt.getLong("creationStamp");
}
if (nbt.contains("InitialID")) {
this.initialID = nbt.getUuid("InitialID");
}
if (nbt.contains("LastTransferTick", NbtElement.LONG_TYPE)) {
this.lastTransferTick = nbt.getLong("LastTransferTick");
}
Expand Down Expand Up @@ -382,6 +387,7 @@ protected void writeNbt(NbtCompound nbt) {
if (creationStamp != -1) {
nbt.putLong("creationStamp", creationStamp);
}
nbt.putUuid("InitialID", this.initialID);
nbt.putBoolean("Triggered", this.triggered);
nbt.putBoolean("Waiting", this.waiting);
nbt.putLong("LastTransferTick", this.lastTransferTick);
Expand Down Expand Up @@ -411,9 +417,13 @@ public NbtCompound toInitialChunkDataNbt() {
@Override
public void markRemoved() {
super.markRemoved();
Pastel.getInstance(world.isClient).removeNode(this, NodeRemovalReason.UNLOADED);
Pastel.getInstance(world.isClient).removeNode(this, world.getBlockState(pos).getBlock() instanceof PastelNodeBlock ? NodeRemovalReason.UNLOADED : NodeRemovalReason.BROKEN);
}


public @NotNull UUID getInitialID() {
return initialID;
}

public void onBroken() {
Pastel.getInstance(world.isClient).removeNode(this, NodeRemovalReason.BROKEN);
}
Expand Down Expand Up @@ -749,7 +759,7 @@ public boolean handleImpression(Optional<UUID> stamper, Optional<PlayerEntity> u
return false;
}

manager.connectNodes(this, sourceNode);
manager.connectNodes(this, sourceNode, initialID);

if (this.parentNetwork != null) {
user.filter(u -> u instanceof ServerPlayerEntity).ifPresent(p -> {
Expand All @@ -766,6 +776,11 @@ public void clearImpression() {
Pastel.getInstance(world.isClient()).removeNode(this, NodeRemovalReason.DISCONNECT);
parentNetwork = null;
parentID = Optional.empty();
initialID = UUID.randomUUID();
if (!this.world.isClient()) {
updateInClientWorld();
markDirty();
}
}
}

Expand Down

0 comments on commit cbde9ed

Please sign in to comment.