Skip to content

Commit

Permalink
pastel networks are now more generic than your music taste
Browse files Browse the repository at this point in the history
  • Loading branch information
DaFuqs committed Jan 24, 2025
1 parent 59d853a commit f51b79b
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
public enum NodeRemovalReason {
UNLOADED(false),
BROKEN(true),
DISCONNECT(true),
DISABLED(false),
MOVED(true);
DISCONNECT(true);

public final boolean destructive;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import java.util.*;
import java.util.stream.*;

public class PastelNetwork {
public class PastelNetwork<W extends World> {

protected Graph<BlockPos, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class);
protected final World world;
protected final W world;
protected final UUID uuid;

public enum NodePriority {
Expand All @@ -24,12 +24,12 @@ public enum NodePriority {
HIGH
}

public PastelNetwork(World world, @Nullable UUID uuid) {
public PastelNetwork(W world, @Nullable UUID uuid) {
this.world = world;
this.uuid = uuid == null ? UUID.randomUUID() : uuid;
}

public World getWorld() {
public W getWorld() {
return this.world;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

import java.util.*;

public interface PastelNetworkManager {
public interface PastelNetworkManager<W extends World, N extends PastelNetwork<W>> {

PastelNetwork createNetwork(World world, UUID uuid);
N createNetwork(W world, UUID uuid);

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.fabricmc.fabric.api.transfer.v1.transaction.*;
import net.minecraft.nbt.*;
import net.minecraft.network.*;
import net.minecraft.server.world.*;
import net.minecraft.util.math.*;
import net.minecraft.world.*;
import org.jetbrains.annotations.*;
Expand All @@ -31,8 +32,8 @@ public PastelTransmission(List<BlockPos> nodePositions, ItemVariant variant, lon
public void setNetwork(@NotNull ServerPastelNetwork network) {
this.network = network;
}

public @Nullable PastelNetwork getNetwork() {
public @Nullable PastelNetwork<ServerWorld> getNetwork() {
return this.network;
}

Expand Down Expand Up @@ -66,31 +67,30 @@ public void trigger() {
}

private void arriveAtDestination() {
if (nodePositions.size() == 0) {
if (nodePositions.isEmpty()) {
return;
}

BlockPos destinationPos = nodePositions.get(nodePositions.size() - 1);
PastelNodeBlockEntity destinationNode = this.network.getNodeAt(destinationPos);
@NotNull BlockPos destinationPos = nodePositions.get(nodePositions.size() - 1);
@Nullable PastelNodeBlockEntity destinationNode = this.network.getNodeAt(destinationPos);
World world = this.network.getWorld();
if (!world.isClient) {
int inserted = 0;
if (destinationNode != null) {
Storage<ItemVariant> destinationStorage = destinationNode.getConnectedStorage();
if (destinationStorage != null) {
try (Transaction transaction = Transaction.openOuter()) {
if (destinationStorage.supportsInsertion()) {
inserted = (int) destinationStorage.insert(variant, amount, transaction);
destinationNode.addItemCountUnderway(-inserted);
transaction.commit();
}
}
}
}
if (inserted != amount) {
InWorldInteractionHelper.scatter(world, destinationPos.getX() + 0.5, destinationPos.getY() + 0.5, destinationPos.getZ() + 0.5, variant, amount - inserted);
}
}

int inserted = 0;
if (destinationNode != null) {
Storage<ItemVariant> destinationStorage = destinationNode.getConnectedStorage();
if (destinationStorage != null) {
try (Transaction transaction = Transaction.openOuter()) {
if (destinationStorage.supportsInsertion()) {
inserted = (int) destinationStorage.insert(variant, amount, transaction);
destinationNode.addItemCountUnderway(-inserted);
transaction.commit();
}
}
}
}
if (inserted != amount) {
InWorldInteractionHelper.scatter(world, destinationPos.getX() + 0.5, destinationPos.getY() + 0.5, destinationPos.getZ() + 0.5, variant, amount - inserted);
}
}

public NbtCompound toNbt() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import net.minecraft.block.entity.*;
import net.minecraft.nbt.*;
import net.minecraft.registry.*;
import net.minecraft.server.world.*;
import net.minecraft.util.*;
import net.minecraft.util.math.*;
import net.minecraft.world.*;
import org.jetbrains.annotations.*;
import org.jgrapht.alg.connectivity.*;
import org.jgrapht.graph.*;
Expand All @@ -19,7 +19,7 @@
import java.util.concurrent.*;
import java.util.stream.*;

public class ServerPastelNetwork extends PastelNetwork {
public class ServerPastelNetwork extends PastelNetwork<ServerWorld> {

protected final Map<PastelNodeType, Set<PastelNodeBlockEntity>> loadedNodes = new ConcurrentHashMap<>();
protected final Set<PastelNodeBlockEntity> priorityNodes = new HashSet<>();
Expand All @@ -30,8 +30,8 @@ public class ServerPastelNetwork extends PastelNetwork {

protected final SchedulerMap<PastelTransmission> transmissions = new SchedulerMap<>();
protected final PastelTransmissionLogic transmissionLogic;

public ServerPastelNetwork(World world, @Nullable UUID uuid) {
public ServerPastelNetwork(ServerWorld world, @Nullable UUID uuid) {
super(world, uuid);
for (PastelNodeType type : PastelNodeType.values()) {
this.loadedNodes.put(type, new HashSet<>());
Expand Down Expand Up @@ -81,8 +81,8 @@ public String toString() {
return builder.toString();
}

public PastelNodeBlockEntity getNodeAt(BlockPos blockPos) {
if (!this.getWorld().isChunkLoaded(blockPos)) {
public @Nullable PastelNodeBlockEntity getNodeAt(BlockPos blockPos) {
if (!this.graph.vertexSet().contains(blockPos) || !this.getWorld().isChunkLoaded(blockPos)) {
return null; // hmmmmm
}

Expand Down Expand Up @@ -131,37 +131,6 @@ public Map<PastelNodeType, Set<PastelNodeBlockEntity>> getLoadedNodes() {
return this.loadedNodes;
}

public int getNodeCount() {
int nodes = 0;
for (Set<PastelNodeBlockEntity> nodeList : this.loadedNodes.values()) {
nodes += nodeList.size();
}
return nodes;
}

public List<PastelNodeBlockEntity> getAllNodes() {
List<PastelNodeBlockEntity> nodes = new ArrayList<>();
for (Map.Entry<PastelNodeType, Set<PastelNodeBlockEntity>> nodeList : this.loadedNodes.entrySet()) {
nodes.addAll(this.loadedNodes.get(nodeList.getKey()));
}
return nodes;
}

public boolean canConnect(PastelNodeBlockEntity newNode) {
if (newNode.getWorld() != this.getWorld()) {
return false;
}

for (Set<PastelNodeBlockEntity> nodeList : this.loadedNodes.values()) {
for (PastelNodeBlockEntity currentNode : nodeList) {
if (currentNode.canConnect(newNode)) {
return true;
}
}
}
return false;
}

public void addNode(PastelNodeBlockEntity node) {
//If this node already has a vertex, then all we are doing it is loading it
if (graph.containsVertex(node.getPos())) {
Expand Down Expand Up @@ -209,7 +178,7 @@ void checkForNetworkSplit() {
for (BlockPos disconnectedNode : disconnectedNodes) {
var switchedNode = getWorld().getBlockEntity(disconnectedNode);
if (switchedNode instanceof PastelNodeBlockEntity pastelNode) {
removeNode(pastelNode, NodeRemovalReason.DISCONNECT);
ServerPastelNetworkManager.get(world).removeNode(pastelNode, NodeRemovalReason.DISCONNECT);
newNetwork.addNode(pastelNode);
pastelNode.setNetworkUUID(newNetwork.getUUID());
}
Expand Down Expand Up @@ -248,7 +217,11 @@ public void incorporate(ServerPastelNetwork networkToIncorporate, PastelNodeBloc
this.transmissionLogic.invalidateCache();
}

public boolean removeNode(PastelNodeBlockEntity node, NodeRemovalReason reason) {
// Call ServerPastelNetworkManager.removeNode() where possible!
// that one does cleanup of networks with no entries
// Else we might get dangling empty networks
@Deprecated()
protected boolean removeNode(PastelNodeBlockEntity node, NodeRemovalReason reason) {
if (!graph.containsVertex(node.getPos())) {
return false;
}
Expand Down Expand Up @@ -394,7 +367,7 @@ public NbtCompound toNbt() {

public static ServerPastelNetwork fromNbt(NbtCompound nbt) {
UUID uuid = nbt.getUuid("UUID");
World world = SpectrumCommon.minecraftServer.getWorld(RegistryKey.of(RegistryKeys.WORLD, Identifier.tryParse(nbt.getString("World"))));
ServerWorld world = SpectrumCommon.minecraftServer.getWorld(RegistryKey.of(RegistryKeys.WORLD, Identifier.tryParse(nbt.getString("World"))));
ServerPastelNetwork network = new ServerPastelNetwork(world, uuid);
network.graph = graphFromNbt(nbt.getCompound("Graph"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

// Persisted together with the overworld
// resetting the overworld will also reset all networks
public class ServerPastelNetworkManager extends PersistentState implements PastelNetworkManager {
public class ServerPastelNetworkManager extends PersistentState implements PastelNetworkManager<ServerWorld, ServerPastelNetwork> {

private static final String PERSISTENT_STATE_ID = "spectrum_pastel_network_manager";

Expand All @@ -21,6 +21,7 @@ public ServerPastelNetworkManager() {
super();
}


@Override
public boolean isDirty() {
return true;
Expand All @@ -29,7 +30,14 @@ public boolean isDirty() {
public static ServerPastelNetworkManager get(ServerWorld world) {
return world.getPersistentStateManager().getOrCreate(ServerPastelNetworkManager::fromNbt, ServerPastelNetworkManager::new, PERSISTENT_STATE_ID);
}


@Override
public ServerPastelNetwork createNetwork(ServerWorld world, UUID uuid) {
ServerPastelNetwork network = new ServerPastelNetwork(world, uuid);
this.networks.add(network);
return network;
}

@Override
public Optional<ServerPastelNetwork> getNetwork(UUID uuid) {
return networks.stream().filter(n -> n.uuid.equals(uuid)).findFirst();
Expand All @@ -53,13 +61,6 @@ public static ServerPastelNetworkManager fromNbt(NbtCompound nbt) {
return manager;
}

@Override
public ServerPastelNetwork createNetwork(World world, UUID uuid) {
ServerPastelNetwork network = new ServerPastelNetwork(world, uuid);
this.networks.add(network);
return network;
}

public void tick() {
// using a for here instead of foreach
// to prevent ConcurrentModificationExceptions
Expand All @@ -70,7 +71,7 @@ public void tick() {
}

@Contract("_, null -> new")
public PastelNetwork joinOrCreateNetwork(PastelNodeBlockEntity node, @Nullable UUID uuid) {
public PastelNetwork<ServerWorld> joinOrCreateNetwork(PastelNodeBlockEntity node, @Nullable UUID uuid) {
if (uuid != null) {
//noinspection ForLoopReplaceableByForEach
for (int i = 0; i < this.networks.size(); i++) {
Expand All @@ -82,7 +83,7 @@ public PastelNetwork joinOrCreateNetwork(PastelNodeBlockEntity node, @Nullable U
}
}

ServerPastelNetwork network = createNetwork(node.getWorld(), uuid);
ServerPastelNetwork network = createNetwork((ServerWorld) node.getWorld(), uuid);
network.addNode(node);
return network;
}
Expand Down Expand Up @@ -112,7 +113,7 @@ public void connectNodes(PastelNodeBlockEntity node, PastelNodeBlockEntity paren
}
}
else {
ServerPastelNetwork newNetwork = createNetwork(node.getWorld(), node.getNodeId());
ServerPastelNetwork newNetwork = createNetwork((ServerWorld) node.getWorld(), node.getNodeId());
newNetwork.addNode(parent);
parent.setNetworkUUID(newNetwork.getUUID());
newNetwork.addNodeAndConnect(node, parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public class PastelNodeBlockEntity extends BlockEntity implements FilterConfigur
protected UUID nodeId = UUID.randomUUID();
protected Optional<UUID> networkUUID = Optional.empty();
protected Optional<PastelUpgradeSignature> outerRing, innerRing, redstoneRing;
protected long lastTransferTick = 0;
protected long lastTransferTick = 0; // TODO: move to ServerPastelNetwork?
protected final long cachedRedstonePowerTick = 0;
protected boolean cachedUnpowered = true;
protected PastelNetwork.NodePriority priority = PastelNetwork.NodePriority.GENERIC;
protected long itemCountUnderway = 0;
protected long itemCountUnderway = 0; // TODO: move to ServerPastelNetwork?

// upgrade impl stuff
protected boolean lit, triggerTransfer, triggered, waiting, lamp, sensor, updated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ public static void sendPastelTransmissionParticle(ServerPastelNetwork network, i
buf.writeUuid(network.getUUID());
buf.writeInt(travelTime);
PastelTransmission.writeToBuf(buf, transmission);

for (ServerPlayerEntity player : PlayerLookup.tracking((ServerWorld) network.getWorld(), transmission.getStartPos())) {
for (ServerPlayerEntity player : PlayerLookup.tracking(network.getWorld(), transmission.getStartPos())) {
ServerPlayNetworking.send(player, SpectrumS2CPackets.PASTEL_TRANSMISSION, buf);
}
}
Expand Down Expand Up @@ -496,7 +496,7 @@ public static void syncPastelNetworkEdges(ServerPastelNetwork serverPastelNetwor
buf.writeUuid(serverPastelNetwork.getUUID());
buf.writeNbt(serverPastelNetwork.graphToNbt());

for (ServerPlayerEntity player : PlayerLookup.tracking((ServerWorld) serverPastelNetwork.getWorld(), pos)) {
for (ServerPlayerEntity player : PlayerLookup.tracking(serverPastelNetwork.getWorld(), pos)) {
ServerPlayNetworking.send(player, SpectrumS2CPackets.PASTEL_NETWORK_EDGE_SYNC, buf);
}
}
Expand Down

0 comments on commit f51b79b

Please sign in to comment.