11package de .dafuqs .spectrum .blocks .pastel_network .network ;
22
3+ import com .mojang .serialization .*;
34import de .dafuqs .spectrum .blocks .pastel_network .nodes .*;
45import de .dafuqs .spectrum .blocks .pastel_network .payloads .*;
56import de .dafuqs .spectrum .helpers .*;
2122@ SuppressWarnings ("UnstableApiUsage" )
2223public class PastelTransmissionLogic {
2324
24- private enum TransferMode {
25+ public enum TransferMode {
2526 PUSH ,
2627 PULL ,
2728 PUSH_PULL
@@ -44,7 +45,11 @@ public void invalidateCache() {
4445 this .pathCache = new HashMap <>();
4546 }
4647
47- public @ Nullable GraphPath <BlockPos , DefaultEdge > getPath (Graph <BlockPos , DefaultEdge > graph , PastelNodeBlockEntity source , PastelNodeBlockEntity destination ) {
48+ public @ Nullable GraphPath <BlockPos , DefaultEdge > getPath (PastelNodeBlockEntity source , PastelNodeBlockEntity destination ) {
49+ return this .getPath (this .network .getGraph (), source , destination );
50+ }
51+
52+ protected @ Nullable GraphPath <BlockPos , DefaultEdge > getPath (Graph <BlockPos , DefaultEdge > graph , PastelNodeBlockEntity source , PastelNodeBlockEntity destination ) {
4853 if (this .dijkstra == null ) {
4954 this .dijkstra = new DijkstraShortestPath <>(graph );
5055 }
@@ -84,102 +89,19 @@ private void transferBetween(PastelNodeType sourceType, PastelNodeType destinati
8489 continue ;
8590 }
8691
87- IItemHandler sourceStorage = sourceNode .getConnectedStorage ();
88- if (sourceStorage != null ) {
89- tryTransferToType (sourceNode , sourceStorage , destinationType , transferMode );
90- }
91- }
92- }
93-
94- private void tryTransferToType (PastelNodeBlockEntity sourceNode , IItemHandler sourceStorage , PastelNodeType type , TransferMode transferMode ) {
95- for (PastelNodeBlockEntity destinationNode : this .network .getLoadedNodes (type , PastelNetwork .NodePriority .GENERIC )) {
96- if (!destinationNode .canTransfer ()) {
97- continue ;
98- }
99-
100- IItemHandler destinationStorage = destinationNode .getConnectedStorage ();
101- if (destinationStorage != null ) {
102- boolean success = transferBetween (sourceNode , sourceStorage , destinationNode , destinationStorage , transferMode );
103- if (success && transferMode != TransferMode .PULL ) {
104- return ;
105- }
92+ for (PastelPayloadType payloadType : sourceNode .getSupportedPayloads ()) {
93+ payloadType .tryTransferToType (this , sourceNode , destinationType , transferMode );
10694 }
10795 }
10896 }
10997
110- private boolean transferBetween (PastelNodeBlockEntity sourceNode , IItemHandler sourceStorage , PastelNodeBlockEntity destinationNode , IItemHandler destinationStorage , TransferMode transferMode ) {
111- // check how much room is in the target inventory
112- long totalAvailableStorage = -destinationNode .getItemCountUnderway ();
113- for (int d = 0 ; d < destinationStorage .getSlots (); d ++) {
114- ItemStack stack = destinationStorage .getStackInSlot (d );
115-
116- if (stack .isEmpty ()) {
117- totalAvailableStorage += destinationStorage .getSlotLimit (d );
118- } else {
119- totalAvailableStorage += Math .min (destinationStorage .getSlotLimit (d ), stack .getMaxStackSize ()) - stack .getCount ();
120- }
121- }
122-
123- if (totalAvailableStorage <= 0 )
124- return false ;
125-
126- Predicate <ItemStack > filter = sourceNode .getTransferFilterTo (destinationNode );
127- Map <ItemStack , Long > proposals = new HashMap <>();
128- for (int s = 0 ; s < sourceStorage .getSlots (); s ++) {
129- ItemStack stack = sourceStorage .extractItem (s , DEFAULT_MAX_TRANSFER_AMOUNT , true );
130-
131- if (stack .isEmpty ())
132- continue ;
133- if (!filter .test (stack ))
134- continue ;
135-
136- proposals .put (stack , proposals .getOrDefault (stack , 0L ) + stack .getCount ());
137- }
138-
139- for (ItemStack stack : proposals .keySet ()) {
140- long proposedAmount = Math .min (Math .min (proposals .get (stack ), sourceNode .getMaxTransferredAmount ()), totalAvailableStorage );
141- if (proposedAmount == 0 )
142- continue ;
143-
144- ItemStack proposedStack = stack .copyWithCount ((int ) proposedAmount );
145- int simulatedAmount = (int ) (proposedAmount - ItemHandlerHelper .insertItemStacked (destinationStorage , proposedStack , true ).getCount ());
146- Tuple <Integer , List <ItemStack >> matchingStacks = InventoryHelper .getStackCountInInventory (proposedStack , sourceStorage , simulatedAmount );
147-
148- if (matchingStacks .getA () == 0 )
149- continue ;
150-
151- Optional <PastelTransmission > transmission = createTransmissionOnValidPath (sourceNode , destinationNode , new PastelPayload .ItemPastelPayload (proposedStack .copyWithCount (simulatedAmount )), sourceNode .getTransferTime ());
152- if (transmission .isPresent ()) {
153- int extracted = 0 ;
154- for (int i = 0 ; i < sourceStorage .getSlots (); i ++) {
155- if (ItemStack .isSameItemSameComponents (proposedStack , sourceStorage .getStackInSlot (i )))
156- extracted += sourceStorage .extractItem (i , simulatedAmount - extracted , false ).getCount ();
157- if (extracted == simulatedAmount )
158- break ;
159- }
160-
161- PastelTransmission trans = transmission .get ();
162- int travelTime = trans .getTransmissionDuration ();
163- this .network .addTransmission (trans , travelTime );
164- PastelTransmissionPayload .sendPastelTransmissionParticle (this .network , trans .getTransmissionDuration (), transmission .get ());
165-
166- destinationNode .markTransferred (transferMode != TransferMode .PUSH );
167- sourceNode .markTransferred (transferMode != TransferMode .PULL );
168-
169- destinationNode .addItemCountUnderway (simulatedAmount );
170- return true ;
171- }
172- }
173- return false ;
98+ public Set <PastelNodeBlockEntity > getLoadedNodes (PastelNodeType type ) {
99+ return this .network .getLoadedNodes (type , PastelNetwork .NodePriority .GENERIC );
174100 }
175101
176- public Optional <PastelTransmission > createTransmissionOnValidPath (PastelNodeBlockEntity source , PastelNodeBlockEntity destination , PastelPayload payload , int vertexTime ) {
177- GraphPath <BlockPos , DefaultEdge > graphPath = getPath (this .network .getGraph (), source , destination );
178- if (graphPath != null ) {
179- PastelNodeStatusUpdatePayload .sendPastelNodeStatusUpdate (List .of (source ), true );
180- return Optional .of (new PastelTransmission (graphPath .getVertexList (), payload , vertexTime ));
181- }
182- return Optional .empty ();
102+ public void addTransmission (PastelTransmission transmission ) {
103+ network .addTransmission (transmission , transmission .getTransmissionDuration ());
104+ PastelTransmissionPayload .sendPastelTransmissionParticle (network , transmission .getTransmissionDuration (), transmission );
183105 }
184106
185107}
0 commit comments