diff --git a/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/NodeRemovalReason.java b/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/NodeRemovalReason.java index 704049e028..839d8e2244 100644 --- a/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/NodeRemovalReason.java +++ b/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/NodeRemovalReason.java @@ -3,9 +3,7 @@ public enum NodeRemovalReason { UNLOADED(false), BROKEN(true), - DISCONNECT(true), - DISABLED(false), - MOVED(true); + DISCONNECT(true); public final boolean destructive; diff --git a/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelNetwork.java b/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelNetwork.java index a70d51fec6..f9b1069b24 100644 --- a/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelNetwork.java +++ b/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelNetwork.java @@ -12,10 +12,10 @@ import java.util.*; import java.util.stream.*; -public class PastelNetwork { +public class PastelNetwork { protected Graph graph = new SimpleGraph<>(DefaultEdge.class); - protected final World world; + protected final W world; protected final UUID uuid; public enum NodePriority { @@ -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; } diff --git a/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelNetworkManager.java b/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelNetworkManager.java index 0bba8272ab..f73f934f43 100644 --- a/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelNetworkManager.java +++ b/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelNetworkManager.java @@ -4,10 +4,10 @@ import java.util.*; -public interface PastelNetworkManager { +public interface PastelNetworkManager> { - PastelNetwork createNetwork(World world, UUID uuid); + N createNetwork(W world, UUID uuid); - Optional getNetwork(UUID uuid); + Optional getNetwork(UUID uuid); } diff --git a/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelTransmission.java b/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelTransmission.java index b8faa2abc0..1f2529c8fa 100644 --- a/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelTransmission.java +++ b/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelTransmission.java @@ -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.*; @@ -31,8 +32,8 @@ public PastelTransmission(List nodePositions, ItemVariant variant, lon public void setNetwork(@NotNull ServerPastelNetwork network) { this.network = network; } - - public @Nullable PastelNetwork getNetwork() { + + public @Nullable PastelNetwork getNetwork() { return this.network; } @@ -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 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 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() { diff --git a/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/ServerPastelNetwork.java b/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/ServerPastelNetwork.java index 7092fd54ed..55f9cd7623 100644 --- a/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/ServerPastelNetwork.java +++ b/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/ServerPastelNetwork.java @@ -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.*; @@ -19,7 +19,7 @@ import java.util.concurrent.*; import java.util.stream.*; -public class ServerPastelNetwork extends PastelNetwork { +public class ServerPastelNetwork extends PastelNetwork { protected final Map> loadedNodes = new ConcurrentHashMap<>(); protected final Set priorityNodes = new HashSet<>(); @@ -30,8 +30,8 @@ public class ServerPastelNetwork extends PastelNetwork { protected final SchedulerMap 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<>()); @@ -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 } @@ -131,37 +131,6 @@ public Map> getLoadedNodes() { return this.loadedNodes; } - public int getNodeCount() { - int nodes = 0; - for (Set nodeList : this.loadedNodes.values()) { - nodes += nodeList.size(); - } - return nodes; - } - - public List getAllNodes() { - List nodes = new ArrayList<>(); - for (Map.Entry> 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 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())) { @@ -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()); } @@ -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; } @@ -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")); diff --git a/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/ServerPastelNetworkManager.java b/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/ServerPastelNetworkManager.java index cf139b310d..2e4f22d4bb 100644 --- a/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/ServerPastelNetworkManager.java +++ b/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/ServerPastelNetworkManager.java @@ -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 { private static final String PERSISTENT_STATE_ID = "spectrum_pastel_network_manager"; @@ -21,6 +21,7 @@ public ServerPastelNetworkManager() { super(); } + @Override public boolean isDirty() { return true; @@ -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 getNetwork(UUID uuid) { return networks.stream().filter(n -> n.uuid.equals(uuid)).findFirst(); @@ -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 @@ -70,7 +71,7 @@ public void tick() { } @Contract("_, null -> new") - public PastelNetwork joinOrCreateNetwork(PastelNodeBlockEntity node, @Nullable UUID uuid) { + public PastelNetwork joinOrCreateNetwork(PastelNodeBlockEntity node, @Nullable UUID uuid) { if (uuid != null) { //noinspection ForLoopReplaceableByForEach for (int i = 0; i < this.networks.size(); i++) { @@ -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; } @@ -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); diff --git a/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/nodes/PastelNodeBlockEntity.java b/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/nodes/PastelNodeBlockEntity.java index ee87c137fd..b2ea3f6e40 100644 --- a/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/nodes/PastelNodeBlockEntity.java +++ b/src/main/java/de/dafuqs/spectrum/blocks/pastel_network/nodes/PastelNodeBlockEntity.java @@ -57,11 +57,11 @@ public class PastelNodeBlockEntity extends BlockEntity implements FilterConfigur protected UUID nodeId = UUID.randomUUID(); protected Optional networkUUID = Optional.empty(); protected Optional 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; diff --git a/src/main/java/de/dafuqs/spectrum/networking/SpectrumS2CPacketSender.java b/src/main/java/de/dafuqs/spectrum/networking/SpectrumS2CPacketSender.java index 7eda12af6f..d81960765a 100644 --- a/src/main/java/de/dafuqs/spectrum/networking/SpectrumS2CPacketSender.java +++ b/src/main/java/de/dafuqs/spectrum/networking/SpectrumS2CPacketSender.java @@ -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); } } @@ -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); } }