Skip to content

Commit f51b79b

Browse files
committed
pastel networks are now more generic than your music taste
1 parent 59d853a commit f51b79b

File tree

8 files changed

+64
-92
lines changed

8 files changed

+64
-92
lines changed

src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/NodeRemovalReason.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
public enum NodeRemovalReason {
44
UNLOADED(false),
55
BROKEN(true),
6-
DISCONNECT(true),
7-
DISABLED(false),
8-
MOVED(true);
6+
DISCONNECT(true);
97

108
public final boolean destructive;
119

src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelNetwork.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
import java.util.*;
1313
import java.util.stream.*;
1414

15-
public class PastelNetwork {
15+
public class PastelNetwork<W extends World> {
1616

1717
protected Graph<BlockPos, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class);
18-
protected final World world;
18+
protected final W world;
1919
protected final UUID uuid;
2020

2121
public enum NodePriority {
@@ -24,12 +24,12 @@ public enum NodePriority {
2424
HIGH
2525
}
2626

27-
public PastelNetwork(World world, @Nullable UUID uuid) {
27+
public PastelNetwork(W world, @Nullable UUID uuid) {
2828
this.world = world;
2929
this.uuid = uuid == null ? UUID.randomUUID() : uuid;
3030
}
31-
32-
public World getWorld() {
31+
32+
public W getWorld() {
3333
return this.world;
3434
}
3535

src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelNetworkManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
import java.util.*;
66

7-
public interface PastelNetworkManager {
7+
public interface PastelNetworkManager<W extends World, N extends PastelNetwork<W>> {
88

9-
PastelNetwork createNetwork(World world, UUID uuid);
9+
N createNetwork(W world, UUID uuid);
1010

11-
Optional<? extends PastelNetwork> getNetwork(UUID uuid);
11+
Optional<? extends N> getNetwork(UUID uuid);
1212

1313
}

src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelTransmission.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.fabricmc.fabric.api.transfer.v1.transaction.*;
88
import net.minecraft.nbt.*;
99
import net.minecraft.network.*;
10+
import net.minecraft.server.world.*;
1011
import net.minecraft.util.math.*;
1112
import net.minecraft.world.*;
1213
import org.jetbrains.annotations.*;
@@ -31,8 +32,8 @@ public PastelTransmission(List<BlockPos> nodePositions, ItemVariant variant, lon
3132
public void setNetwork(@NotNull ServerPastelNetwork network) {
3233
this.network = network;
3334
}
34-
35-
public @Nullable PastelNetwork getNetwork() {
35+
36+
public @Nullable PastelNetwork<ServerWorld> getNetwork() {
3637
return this.network;
3738
}
3839

@@ -66,31 +67,30 @@ public void trigger() {
6667
}
6768

6869
private void arriveAtDestination() {
69-
if (nodePositions.size() == 0) {
70+
if (nodePositions.isEmpty()) {
7071
return;
7172
}
72-
73-
BlockPos destinationPos = nodePositions.get(nodePositions.size() - 1);
74-
PastelNodeBlockEntity destinationNode = this.network.getNodeAt(destinationPos);
73+
74+
@NotNull BlockPos destinationPos = nodePositions.get(nodePositions.size() - 1);
75+
@Nullable PastelNodeBlockEntity destinationNode = this.network.getNodeAt(destinationPos);
7576
World world = this.network.getWorld();
76-
if (!world.isClient) {
77-
int inserted = 0;
78-
if (destinationNode != null) {
79-
Storage<ItemVariant> destinationStorage = destinationNode.getConnectedStorage();
80-
if (destinationStorage != null) {
81-
try (Transaction transaction = Transaction.openOuter()) {
82-
if (destinationStorage.supportsInsertion()) {
83-
inserted = (int) destinationStorage.insert(variant, amount, transaction);
84-
destinationNode.addItemCountUnderway(-inserted);
85-
transaction.commit();
86-
}
87-
}
88-
}
89-
}
90-
if (inserted != amount) {
91-
InWorldInteractionHelper.scatter(world, destinationPos.getX() + 0.5, destinationPos.getY() + 0.5, destinationPos.getZ() + 0.5, variant, amount - inserted);
92-
}
93-
}
77+
78+
int inserted = 0;
79+
if (destinationNode != null) {
80+
Storage<ItemVariant> destinationStorage = destinationNode.getConnectedStorage();
81+
if (destinationStorage != null) {
82+
try (Transaction transaction = Transaction.openOuter()) {
83+
if (destinationStorage.supportsInsertion()) {
84+
inserted = (int) destinationStorage.insert(variant, amount, transaction);
85+
destinationNode.addItemCountUnderway(-inserted);
86+
transaction.commit();
87+
}
88+
}
89+
}
90+
}
91+
if (inserted != amount) {
92+
InWorldInteractionHelper.scatter(world, destinationPos.getX() + 0.5, destinationPos.getY() + 0.5, destinationPos.getZ() + 0.5, variant, amount - inserted);
93+
}
9494
}
9595

9696
public NbtCompound toNbt() {

src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/ServerPastelNetwork.java

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import net.minecraft.block.entity.*;
99
import net.minecraft.nbt.*;
1010
import net.minecraft.registry.*;
11+
import net.minecraft.server.world.*;
1112
import net.minecraft.util.*;
1213
import net.minecraft.util.math.*;
13-
import net.minecraft.world.*;
1414
import org.jetbrains.annotations.*;
1515
import org.jgrapht.alg.connectivity.*;
1616
import org.jgrapht.graph.*;
@@ -19,7 +19,7 @@
1919
import java.util.concurrent.*;
2020
import java.util.stream.*;
2121

22-
public class ServerPastelNetwork extends PastelNetwork {
22+
public class ServerPastelNetwork extends PastelNetwork<ServerWorld> {
2323

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

3131
protected final SchedulerMap<PastelTransmission> transmissions = new SchedulerMap<>();
3232
protected final PastelTransmissionLogic transmissionLogic;
33-
34-
public ServerPastelNetwork(World world, @Nullable UUID uuid) {
33+
34+
public ServerPastelNetwork(ServerWorld world, @Nullable UUID uuid) {
3535
super(world, uuid);
3636
for (PastelNodeType type : PastelNodeType.values()) {
3737
this.loadedNodes.put(type, new HashSet<>());
@@ -81,8 +81,8 @@ public String toString() {
8181
return builder.toString();
8282
}
8383

84-
public PastelNodeBlockEntity getNodeAt(BlockPos blockPos) {
85-
if (!this.getWorld().isChunkLoaded(blockPos)) {
84+
public @Nullable PastelNodeBlockEntity getNodeAt(BlockPos blockPos) {
85+
if (!this.graph.vertexSet().contains(blockPos) || !this.getWorld().isChunkLoaded(blockPos)) {
8686
return null; // hmmmmm
8787
}
8888

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

134-
public int getNodeCount() {
135-
int nodes = 0;
136-
for (Set<PastelNodeBlockEntity> nodeList : this.loadedNodes.values()) {
137-
nodes += nodeList.size();
138-
}
139-
return nodes;
140-
}
141-
142-
public List<PastelNodeBlockEntity> getAllNodes() {
143-
List<PastelNodeBlockEntity> nodes = new ArrayList<>();
144-
for (Map.Entry<PastelNodeType, Set<PastelNodeBlockEntity>> nodeList : this.loadedNodes.entrySet()) {
145-
nodes.addAll(this.loadedNodes.get(nodeList.getKey()));
146-
}
147-
return nodes;
148-
}
149-
150-
public boolean canConnect(PastelNodeBlockEntity newNode) {
151-
if (newNode.getWorld() != this.getWorld()) {
152-
return false;
153-
}
154-
155-
for (Set<PastelNodeBlockEntity> nodeList : this.loadedNodes.values()) {
156-
for (PastelNodeBlockEntity currentNode : nodeList) {
157-
if (currentNode.canConnect(newNode)) {
158-
return true;
159-
}
160-
}
161-
}
162-
return false;
163-
}
164-
165134
public void addNode(PastelNodeBlockEntity node) {
166135
//If this node already has a vertex, then all we are doing it is loading it
167136
if (graph.containsVertex(node.getPos())) {
@@ -209,7 +178,7 @@ void checkForNetworkSplit() {
209178
for (BlockPos disconnectedNode : disconnectedNodes) {
210179
var switchedNode = getWorld().getBlockEntity(disconnectedNode);
211180
if (switchedNode instanceof PastelNodeBlockEntity pastelNode) {
212-
removeNode(pastelNode, NodeRemovalReason.DISCONNECT);
181+
ServerPastelNetworkManager.get(world).removeNode(pastelNode, NodeRemovalReason.DISCONNECT);
213182
newNetwork.addNode(pastelNode);
214183
pastelNode.setNetworkUUID(newNetwork.getUUID());
215184
}
@@ -248,7 +217,11 @@ public void incorporate(ServerPastelNetwork networkToIncorporate, PastelNodeBloc
248217
this.transmissionLogic.invalidateCache();
249218
}
250219

251-
public boolean removeNode(PastelNodeBlockEntity node, NodeRemovalReason reason) {
220+
// Call ServerPastelNetworkManager.removeNode() where possible!
221+
// that one does cleanup of networks with no entries
222+
// Else we might get dangling empty networks
223+
@Deprecated()
224+
protected boolean removeNode(PastelNodeBlockEntity node, NodeRemovalReason reason) {
252225
if (!graph.containsVertex(node.getPos())) {
253226
return false;
254227
}
@@ -394,7 +367,7 @@ public NbtCompound toNbt() {
394367

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

src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/ServerPastelNetworkManager.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

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

1616
private static final String PERSISTENT_STATE_ID = "spectrum_pastel_network_manager";
1717

@@ -21,6 +21,7 @@ public ServerPastelNetworkManager() {
2121
super();
2222
}
2323

24+
2425
@Override
2526
public boolean isDirty() {
2627
return true;
@@ -29,7 +30,14 @@ public boolean isDirty() {
2930
public static ServerPastelNetworkManager get(ServerWorld world) {
3031
return world.getPersistentStateManager().getOrCreate(ServerPastelNetworkManager::fromNbt, ServerPastelNetworkManager::new, PERSISTENT_STATE_ID);
3132
}
32-
33+
34+
@Override
35+
public ServerPastelNetwork createNetwork(ServerWorld world, UUID uuid) {
36+
ServerPastelNetwork network = new ServerPastelNetwork(world, uuid);
37+
this.networks.add(network);
38+
return network;
39+
}
40+
3341
@Override
3442
public Optional<ServerPastelNetwork> getNetwork(UUID uuid) {
3543
return networks.stream().filter(n -> n.uuid.equals(uuid)).findFirst();
@@ -53,13 +61,6 @@ public static ServerPastelNetworkManager fromNbt(NbtCompound nbt) {
5361
return manager;
5462
}
5563

56-
@Override
57-
public ServerPastelNetwork createNetwork(World world, UUID uuid) {
58-
ServerPastelNetwork network = new ServerPastelNetwork(world, uuid);
59-
this.networks.add(network);
60-
return network;
61-
}
62-
6364
public void tick() {
6465
// using a for here instead of foreach
6566
// to prevent ConcurrentModificationExceptions
@@ -70,7 +71,7 @@ public void tick() {
7071
}
7172

7273
@Contract("_, null -> new")
73-
public PastelNetwork joinOrCreateNetwork(PastelNodeBlockEntity node, @Nullable UUID uuid) {
74+
public PastelNetwork<ServerWorld> joinOrCreateNetwork(PastelNodeBlockEntity node, @Nullable UUID uuid) {
7475
if (uuid != null) {
7576
//noinspection ForLoopReplaceableByForEach
7677
for (int i = 0; i < this.networks.size(); i++) {
@@ -82,7 +83,7 @@ public PastelNetwork joinOrCreateNetwork(PastelNodeBlockEntity node, @Nullable U
8283
}
8384
}
8485

85-
ServerPastelNetwork network = createNetwork(node.getWorld(), uuid);
86+
ServerPastelNetwork network = createNetwork((ServerWorld) node.getWorld(), uuid);
8687
network.addNode(node);
8788
return network;
8889
}
@@ -112,7 +113,7 @@ public void connectNodes(PastelNodeBlockEntity node, PastelNodeBlockEntity paren
112113
}
113114
}
114115
else {
115-
ServerPastelNetwork newNetwork = createNetwork(node.getWorld(), node.getNodeId());
116+
ServerPastelNetwork newNetwork = createNetwork((ServerWorld) node.getWorld(), node.getNodeId());
116117
newNetwork.addNode(parent);
117118
parent.setNetworkUUID(newNetwork.getUUID());
118119
newNetwork.addNodeAndConnect(node, parent);

src/main/java/de/dafuqs/spectrum/blocks/pastel_network/nodes/PastelNodeBlockEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ public class PastelNodeBlockEntity extends BlockEntity implements FilterConfigur
5757
protected UUID nodeId = UUID.randomUUID();
5858
protected Optional<UUID> networkUUID = Optional.empty();
5959
protected Optional<PastelUpgradeSignature> outerRing, innerRing, redstoneRing;
60-
protected long lastTransferTick = 0;
60+
protected long lastTransferTick = 0; // TODO: move to ServerPastelNetwork?
6161
protected final long cachedRedstonePowerTick = 0;
6262
protected boolean cachedUnpowered = true;
6363
protected PastelNetwork.NodePriority priority = PastelNetwork.NodePriority.GENERIC;
64-
protected long itemCountUnderway = 0;
64+
protected long itemCountUnderway = 0; // TODO: move to ServerPastelNetwork?
6565

6666
// upgrade impl stuff
6767
protected boolean lit, triggerTransfer, triggered, waiting, lamp, sensor, updated;

src/main/java/de/dafuqs/spectrum/networking/SpectrumS2CPacketSender.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ public static void sendPastelTransmissionParticle(ServerPastelNetwork network, i
218218
buf.writeUuid(network.getUUID());
219219
buf.writeInt(travelTime);
220220
PastelTransmission.writeToBuf(buf, transmission);
221-
222-
for (ServerPlayerEntity player : PlayerLookup.tracking((ServerWorld) network.getWorld(), transmission.getStartPos())) {
221+
222+
for (ServerPlayerEntity player : PlayerLookup.tracking(network.getWorld(), transmission.getStartPos())) {
223223
ServerPlayNetworking.send(player, SpectrumS2CPackets.PASTEL_TRANSMISSION, buf);
224224
}
225225
}
@@ -496,7 +496,7 @@ public static void syncPastelNetworkEdges(ServerPastelNetwork serverPastelNetwor
496496
buf.writeUuid(serverPastelNetwork.getUUID());
497497
buf.writeNbt(serverPastelNetwork.graphToNbt());
498498

499-
for (ServerPlayerEntity player : PlayerLookup.tracking((ServerWorld) serverPastelNetwork.getWorld(), pos)) {
499+
for (ServerPlayerEntity player : PlayerLookup.tracking(serverPastelNetwork.getWorld(), pos)) {
500500
ServerPlayNetworking.send(player, SpectrumS2CPackets.PASTEL_NETWORK_EDGE_SYNC, buf);
501501
}
502502
}

0 commit comments

Comments
 (0)