|
| 1 | +package de.dafuqs.spectrum.blocks.pastel_network.payloads; |
| 2 | + |
| 3 | +import de.dafuqs.spectrum.*; |
| 4 | +import de.dafuqs.spectrum.api.energy.*; |
| 5 | +import de.dafuqs.spectrum.api.energy.color.*; |
| 6 | +import de.dafuqs.spectrum.blocks.pastel_network.network.*; |
| 7 | +import de.dafuqs.spectrum.blocks.pastel_network.nodes.*; |
| 8 | +import net.minecraft.core.*; |
| 9 | +import net.minecraft.world.level.block.entity.*; |
| 10 | +import 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.*; |
| 14 | +import org.jetbrains.annotations.*; |
| 15 | +import org.jgrapht.*; |
| 16 | +import org.jgrapht.graph.*; |
| 17 | + |
| 18 | +import java.util.*; |
| 19 | +import java.util.function.*; |
| 20 | + |
| 21 | +public class InkPastelPayloadType extends PastelPayloadType { |
| 22 | + |
| 23 | + public InkPastelPayloadType() { |
| 24 | + super(SpectrumCommon.locate("ink"), SpectrumPastelPayloads.INK.get()); |
| 25 | + } |
| 26 | + |
| 27 | + public static @Nullable InkStorageBlockEntity<?> getConnectedInkStorage(PastelNodeBlockEntity pastelNodeBlockEntity) { |
| 28 | + BlockState state = pastelNodeBlockEntity.getBlockState(); |
| 29 | + if (!(state.getBlock() instanceof PastelNodeBlock)) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + Direction direction = state.getValue(PastelNodeBlock.FACING); |
| 33 | + BlockEntity be = pastelNodeBlockEntity.getLevel().getBlockEntity(pastelNodeBlockEntity.getBlockPos().relative(direction.getOpposite())); |
| 34 | + if(be instanceof InkStorageBlockEntity<?> inkStorageBlockEntity) { |
| 35 | + return inkStorageBlockEntity; |
| 36 | + } |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + @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 | + } |
| 58 | + |
| 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 | + } |
| 70 | + } |
| 71 | + } |
| 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 | + } |
| 101 | + } |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments