22
33import de .dafuqs .spectrum .*;
44import de .dafuqs .spectrum .api .energy .*;
5- import de .dafuqs .spectrum .api .energy .color .*;
65import de .dafuqs .spectrum .blocks .pastel_network .network .*;
76import de .dafuqs .spectrum .blocks .pastel_network .nodes .*;
87import net .minecraft .core .*;
98import net .minecraft .world .level .block .entity .*;
109import net .minecraft .world .level .block .state .*;
11- import net .neoforged .neoforge .capabilities .*;
12- import net .neoforged .neoforge .fluids .*;
13- import net .neoforged .neoforge .fluids .capability .*;
1410import org .jetbrains .annotations .*;
15- import org .jgrapht .*;
16- import org .jgrapht .graph .*;
1711
1812import java .util .*;
19- import java .util .function .*;
2013
2114public class InkPastelPayloadType extends PastelPayloadType {
2215
@@ -38,68 +31,25 @@ public InkPastelPayloadType() {
3831 }
3932
4033 @ Override
41- public void tick (PastelTransmissionLogic logic ) {
42- for (PastelNodeBlockEntity sourceNode : logic .getLoadedNodes (PastelNodeType .SENDER )) {
43- for (Supplier <? extends PastelPayloadType > payloadType : sourceNode .getSupportedPayloads ()) {
44- if (payloadType .get () != this ) {
45- continue ;
46- }
47- payloadType .get ().tryTransferToType (logic , sourceNode , PastelNodeType .SENDER , PastelTransmissionLogic .TransferMode .PUSH );
48- }
49- }
50- }
51-
52- @ Override
53- public void tryTransferToType (PastelTransmissionLogic logic , PastelNodeBlockEntity sourceNode , PastelNodeType type , PastelTransmissionLogic .TransferMode transferMode ) {
54- @ Nullable InkStorageBlockEntity <?> sourceHandler = getConnectedInkStorage (sourceNode );
55- if (sourceHandler == null ) {
56- return ;
57- }
34+ public void tick (PastelTransmissionLogic logic , PastelNetwork .NodePriority priority ) {
35+ Set <PastelNodeBlockEntity > nodes = logic .getLoadedNodes (PastelNodeType .SENDER );
36+ if (nodes .isEmpty ()) return ;
5837
59- for (PastelNodeBlockEntity destinationNode : logic .getLoadedNodes (type )) {
60- if (!destinationNode .canTransfer ()) {
61- continue ;
62- }
63-
64- @ Nullable InkStorageBlockEntity <?> destinationHandler = getConnectedInkStorage (destinationNode );
65- if (destinationHandler != null ) {
66- boolean success = transferBetween (logic , sourceNode , sourceHandler , destinationNode , destinationHandler , transferMode );
67- if (success && transferMode != PastelTransmissionLogic .TransferMode .PULL ) {
68- return ;
69- }
38+ List <InkStorageBlockEntity <?>> blockEntities = new ArrayList <>(nodes .size ());
39+ List <InkStorage > inkStorages = new ArrayList <>(nodes .size ());
40+ for (PastelNodeBlockEntity node : nodes ) {
41+ InkStorageBlockEntity <?> storage = getConnectedInkStorage (node );
42+ if (storage != null ) {
43+ blockEntities .add (storage );
44+ inkStorages .add (storage .getEnergyStorage ());
7045 }
7146 }
72- }
73-
74- private boolean transferBetween (PastelTransmissionLogic logic , PastelNodeBlockEntity sourceNode , InkStorageBlockEntity <?> sourceHandler , PastelNodeBlockEntity destinationNode , InkStorageBlockEntity <?> destinationHandler , PastelTransmissionLogic .TransferMode transferMode ) {
75- InkStorage sourceStorage = sourceHandler .getEnergyStorage ();
76- InkStorage destinationStorage = destinationHandler .getEnergyStorage ();
77- for (Map .Entry <InkColor , Long > sourceEntry : sourceStorage .getEnergy ().entrySet ()) {
78- GraphPath <BlockPos , DefaultEdge > graphPath = logic .getPath (sourceNode , destinationNode );
79- if (graphPath == null ) {
80- continue ;
81- }
82-
83- InkColor sourceColor = sourceEntry .getKey ();
84-
85- long roomAtDestination = destinationStorage .getRoom (sourceColor );
86- List <InkAmount > transmittedAmounts = new ArrayList <>();
87- if (roomAtDestination > 0 ) {
88- long transmittedAmount = Math .min (Math .min (sourceEntry .getValue (), roomAtDestination ), sourceNode .getMaxTransferredAmount () * 20L );
89- if (transmittedAmount > 0 ) {
90- sourceStorage .addEnergy (sourceEntry .getKey (), -transmittedAmount );
91- transmittedAmounts .add (new InkAmount (sourceColor , transmittedAmount ));
92- return true ;
93- }
94- }
95-
96- if (!transmittedAmounts .isEmpty ()) {
97- PastelTransmission transmission = new PastelTransmission (graphPath .getVertexList (), new InkPastelPayload (transmittedAmounts ), sourceNode .getTransferTime ());
98- logic .addTransmission (sourceNode , destinationNode , transferMode , transmission );
99- sourceHandler .setInkDirty ();
100- }
47+ if (blockEntities .isEmpty ()) return ;
48+
49+ InkStorage .equalizeInk (inkStorages );
50+ for (InkStorageBlockEntity <?> s : blockEntities ) {
51+ s .setInkDirty ();
10152 }
102- return false ;
10353 }
10454
10555}
0 commit comments