Skip to content

Commit 0900504

Browse files
committed
wörk
1 parent da7c9a0 commit 0900504

12 files changed

Lines changed: 215 additions & 138 deletions

src/main/java/de/dafuqs/spectrum/SpectrumCommon.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import de.dafuqs.spectrum.api.energy.color.*;
55
import de.dafuqs.spectrum.attachment_types.*;
66
import de.dafuqs.spectrum.blocks.pastel_network.*;
7-
import de.dafuqs.spectrum.blocks.pastel_network.payloads.PastelPayload;
7+
import de.dafuqs.spectrum.blocks.pastel_network.payloads.*;
88
import de.dafuqs.spectrum.capabilities.*;
99
import de.dafuqs.spectrum.compat.*;
1010
import de.dafuqs.spectrum.config.*;
@@ -123,6 +123,7 @@ public SpectrumCommon(ModContainer container, IEventBus modBus) {
123123
logInfo("Registering Block Entities...");
124124
SpectrumBlockEntities.register(modBus);
125125
PastelPayload.register(modBus);
126+
PastelPayloadType.register(modBus);
126127
modBus.addListener(SpectrumBlockEntities::addBlockEntityTypeBlocks);
127128
SpectrumPastelUpgradeSignatures.register(modBus);
128129

Lines changed: 14 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.dafuqs.spectrum.blocks.pastel_network.network;
22

3+
import com.mojang.serialization.*;
34
import de.dafuqs.spectrum.blocks.pastel_network.nodes.*;
45
import de.dafuqs.spectrum.blocks.pastel_network.payloads.*;
56
import de.dafuqs.spectrum.helpers.*;
@@ -21,7 +22,7 @@
2122
@SuppressWarnings("UnstableApiUsage")
2223
public 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
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public class PastelNodeBlock extends SpectrumFacingBlock implements EntityBlock,
5252
}};
5353

5454
protected final PastelNodeType pastelNodeType;
55-
protected final Set<MapCodec<PastelPayload>> supportedPayloads;
55+
protected final Set<PastelPayloadType> supportedPayloads;
5656

57-
public PastelNodeBlock(Properties settings, PastelNodeType pastelNodeType, Set<MapCodec<PastelPayload>> supportedPayloads) {
57+
public PastelNodeBlock(Properties settings, PastelNodeType pastelNodeType, Set<PastelPayloadType> supportedPayloads) {
5858
super(settings.lightLevel(s -> s.getValue(LIT) ? 13 : 0));
5959
this.pastelNodeType = pastelNodeType;
6060
this.supportedPayloads = supportedPayloads;

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package de.dafuqs.spectrum.blocks.pastel_network.nodes;
22

33
import com.google.common.base.*;
4+
import com.mojang.serialization.*;
45
import de.dafuqs.spectrum.*;
56
import de.dafuqs.spectrum.api.block.*;
67
import de.dafuqs.spectrum.api.pastel_network.*;
78
import de.dafuqs.spectrum.blocks.pastel_network.*;
89
import de.dafuqs.spectrum.blocks.pastel_network.network.*;
10+
import de.dafuqs.spectrum.blocks.pastel_network.payloads.*;
911
import de.dafuqs.spectrum.helpers.*;
1012
import de.dafuqs.spectrum.inventories.*;
1113
import de.dafuqs.spectrum.networking.s2c_payloads.*;
@@ -60,7 +62,6 @@ public class PastelNodeBlockEntity extends BlockEntity implements FilterConfigur
6062
protected PastelNetwork.NodePriority priority = PastelNetwork.NodePriority.GENERIC;
6163
protected long itemCountUnderway = 0;
6264

63-
6465
// upgrade impl stuff
6566
protected boolean lit, triggerTransfer, triggered, waiting, lamp, sensor, updated;
6667
protected int transferCount = PastelTransmissionLogic.DEFAULT_MAX_TRANSFER_AMOUNT;
@@ -415,6 +416,13 @@ public PastelNodeType getNodeType() {
415416
return PastelNodeType.CONNECTION;
416417
}
417418

419+
public Set<PastelPayloadType> getSupportedPayloads() {
420+
if (this.getBlockState().getBlock() instanceof PastelNodeBlock pastelNodeBlock) {
421+
return pastelNodeBlock.supportedPayloads;
422+
}
423+
return Set.of();
424+
}
425+
418426
public void setNetworkUUID(@Nullable UUID uuid) {
419427
this.networkUUID = Optional.ofNullable(uuid);
420428
if (this.getLevel() != null && !this.getLevel().isClientSide()) {

src/main/java/de/dafuqs/spectrum/blocks/pastel_network/payloads/ItemPastelPayload.java

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import com.mojang.blaze3d.vertex.PoseStack;
44
import com.mojang.serialization.MapCodec;
55
import com.mojang.serialization.codecs.RecordCodecBuilder;
6-
import de.dafuqs.spectrum.blocks.pastel_network.nodes.PastelNodeBlockEntity;
7-
import de.dafuqs.spectrum.helpers.InWorldInteractionHelper;
6+
import de.dafuqs.spectrum.blocks.pastel_network.nodes.*;
7+
import de.dafuqs.spectrum.helpers.*;
88
import de.dafuqs.spectrum.particle.client.PastelTransmissionParticle;
99
import net.minecraft.client.renderer.MultiBufferSource;
1010
import net.minecraft.client.renderer.texture.OverlayTexture;
@@ -18,27 +18,30 @@
1818
import net.neoforged.neoforge.items.ItemHandlerHelper;
1919
import org.jetbrains.annotations.Nullable;
2020

21-
public class ItemPastelPayload implements PastelPayload {
21+
public record ItemPastelPayload(ItemStack itemStack) implements PastelPayload {
2222

23-
public static final MapCodec<de.dafuqs.spectrum.blocks.pastel_network.payloads.ItemPastelPayload> CODEC = RecordCodecBuilder.mapCodec(i -> i.group(
24-
ItemStack.CODEC.fieldOf("stack").forGetter(de.dafuqs.spectrum.blocks.pastel_network.payloads.ItemPastelPayload::getItemStack)
25-
).apply(i, de.dafuqs.spectrum.blocks.pastel_network.payloads.ItemPastelPayload::new));
23+
public static final MapCodec<ItemPastelPayload> CODEC = RecordCodecBuilder.mapCodec(i -> i.group(
24+
ItemStack.CODEC.fieldOf("stack").forGetter(ItemPastelPayload::itemStack)
25+
).apply(i, ItemPastelPayload::new));
2626

27-
public static final StreamCodec<RegistryFriendlyByteBuf, de.dafuqs.spectrum.blocks.pastel_network.payloads.ItemPastelPayload> STREAM_CODEC = StreamCodec.composite(
28-
ItemStack.STREAM_CODEC, de.dafuqs.spectrum.blocks.pastel_network.payloads.ItemPastelPayload::getItemStack,
29-
de.dafuqs.spectrum.blocks.pastel_network.payloads.ItemPastelPayload::new
27+
public static final StreamCodec<RegistryFriendlyByteBuf, ItemPastelPayload> STREAM_CODEC = StreamCodec.composite(
28+
ItemStack.STREAM_CODEC, ItemPastelPayload::itemStack,
29+
ItemPastelPayload::new
3030
);
3131

32-
private final ItemStack itemStack;
32+
public void render(PastelTransmissionParticle particle, Level level, PoseStack poseStack, final MultiBufferSource vertexConsumers, int light) {
33+
particle.itemRenderer.renderStatic(this.itemStack, ItemDisplayContext.GROUND, light, OverlayTexture.NO_OVERLAY, poseStack, vertexConsumers, level, 0);
34+
}
3335

34-
public ItemPastelPayload(ItemStack itemStack) {
35-
this.itemStack = itemStack;
36+
public MapCodec<ItemPastelPayload> codec() {
37+
return CODEC;
3638
}
3739

38-
public ItemStack getItemStack() {
39-
return itemStack;
40+
public StreamCodec<RegistryFriendlyByteBuf, ItemPastelPayload> streamCodec() {
41+
return STREAM_CODEC;
4042
}
4143

44+
@Override
4245
public void arriveAtDestination(Level level, BlockPos destination, @Nullable PastelNodeBlockEntity destinationNode) {
4346
int inserted = 0;
4447
int count = itemStack.getCount();
@@ -61,15 +64,4 @@ public void arriveAtDestination(Level level, BlockPos destination, @Nullable Pas
6164
}
6265
}
6366

64-
public void render(PastelTransmissionParticle particle, Level level, PoseStack poseStack, final MultiBufferSource vertexConsumers, int light) {
65-
particle.itemRenderer.renderStatic(this.itemStack, ItemDisplayContext.GROUND, light, OverlayTexture.NO_OVERLAY, poseStack, vertexConsumers, level, 0);
66-
}
67-
68-
public MapCodec<de.dafuqs.spectrum.blocks.pastel_network.payloads.ItemPastelPayload> codec() {
69-
return CODEC;
70-
}
71-
72-
public StreamCodec<RegistryFriendlyByteBuf, de.dafuqs.spectrum.blocks.pastel_network.payloads.ItemPastelPayload> streamCodec() {
73-
return STREAM_CODEC;
74-
}
7567
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package de.dafuqs.spectrum.blocks.pastel_network.payloads;
2+
3+
import de.dafuqs.spectrum.SpectrumCommon;
4+
import de.dafuqs.spectrum.blocks.pastel_network.network.PastelTransmission;
5+
import de.dafuqs.spectrum.blocks.pastel_network.network.PastelTransmissionLogic;
6+
import de.dafuqs.spectrum.blocks.pastel_network.nodes.PastelNodeBlockEntity;
7+
import de.dafuqs.spectrum.blocks.pastel_network.nodes.PastelNodeType;
8+
import de.dafuqs.spectrum.helpers.InventoryHelper;
9+
import de.dafuqs.spectrum.networking.s2c_payloads.PastelNodeStatusUpdatePayload;
10+
import net.minecraft.core.BlockPos;
11+
import net.minecraft.util.Tuple;
12+
import net.minecraft.world.item.ItemStack;
13+
import net.neoforged.neoforge.items.IItemHandler;
14+
import net.neoforged.neoforge.items.ItemHandlerHelper;
15+
import org.jgrapht.GraphPath;
16+
import org.jgrapht.graph.DefaultEdge;
17+
18+
import java.util.HashMap;
19+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.function.Predicate;
22+
23+
public class ItemPastelPayloadType extends PastelPayloadType {
24+
25+
public ItemPastelPayloadType() {
26+
super(SpectrumCommon.locate("item"), PastelPayload.ITEM.get());
27+
}
28+
29+
@Override
30+
public void tryTransferToType(PastelTransmissionLogic logic, PastelNodeBlockEntity sourceNode, PastelNodeType type, PastelTransmissionLogic.TransferMode transferMode) {
31+
IItemHandler sourceStorage = sourceNode.getConnectedStorage();
32+
if (sourceStorage == null) {
33+
return;
34+
}
35+
36+
for (PastelNodeBlockEntity destinationNode : logic.getLoadedNodes(type)) {
37+
if (!destinationNode.canTransfer()) {
38+
continue;
39+
}
40+
41+
IItemHandler destinationStorage = destinationNode.getConnectedStorage();
42+
if (destinationStorage != null) {
43+
boolean success = transferBetween(logic, sourceNode, sourceStorage, destinationNode, destinationStorage, transferMode);
44+
if (success && transferMode != PastelTransmissionLogic.TransferMode.PULL) {
45+
return;
46+
}
47+
}
48+
}
49+
}
50+
51+
private boolean transferBetween(PastelTransmissionLogic logic, PastelNodeBlockEntity sourceNode, IItemHandler sourceStorage, PastelNodeBlockEntity destinationNode, IItemHandler destinationStorage, PastelTransmissionLogic.TransferMode transferMode) {
52+
// check how much room is in the target inventory
53+
long totalAvailableStorage = -destinationNode.getItemCountUnderway();
54+
for (int d = 0; d < destinationStorage.getSlots(); d++) {
55+
ItemStack stack = destinationStorage.getStackInSlot(d);
56+
57+
if (stack.isEmpty()) {
58+
totalAvailableStorage += destinationStorage.getSlotLimit(d);
59+
} else {
60+
totalAvailableStorage += Math.min(destinationStorage.getSlotLimit(d), stack.getMaxStackSize()) - stack.getCount();
61+
}
62+
}
63+
64+
if (totalAvailableStorage <= 0)
65+
return false;
66+
67+
Predicate<ItemStack> filter = sourceNode.getTransferFilterTo(destinationNode);
68+
Map<ItemStack, Long> proposals = new HashMap<>();
69+
for (int s = 0; s < sourceStorage.getSlots(); s++) {
70+
ItemStack stack = sourceStorage.extractItem(s, PastelTransmissionLogic.DEFAULT_MAX_TRANSFER_AMOUNT, true);
71+
72+
if (stack.isEmpty())
73+
continue;
74+
if (!filter.test(stack))
75+
continue;
76+
77+
proposals.put(stack, proposals.getOrDefault(stack, 0L) + stack.getCount());
78+
}
79+
80+
for (ItemStack stack : proposals.keySet()) {
81+
long proposedAmount = Math.min(Math.min(proposals.get(stack), sourceNode.getMaxTransferredAmount()), totalAvailableStorage);
82+
if (proposedAmount == 0)
83+
continue;
84+
85+
ItemStack proposedStack = stack.copyWithCount((int) proposedAmount);
86+
int simulatedAmount = (int) (proposedAmount - ItemHandlerHelper.insertItemStacked(destinationStorage, proposedStack, true).getCount());
87+
Tuple<Integer, List<ItemStack>> matchingStacks = InventoryHelper.getStackCountInInventory(proposedStack, sourceStorage, simulatedAmount);
88+
89+
if (matchingStacks.getA() == 0)
90+
continue;
91+
92+
PastelPayload payload = new ItemPastelPayload(proposedStack.copyWithCount(simulatedAmount));
93+
int vertexTime = sourceNode.getTransferTime();
94+
GraphPath<BlockPos, DefaultEdge> graphPath = logic.getPath(sourceNode, destinationNode);
95+
if (graphPath == null) {
96+
return false;
97+
}
98+
99+
PastelNodeStatusUpdatePayload.sendPastelNodeStatusUpdate(List.of(sourceNode), true);
100+
PastelTransmission transmission = new PastelTransmission(graphPath.getVertexList(), payload, vertexTime);
101+
int extracted = 0;
102+
for (int i = 0; i < sourceStorage.getSlots(); i++) {
103+
if (ItemStack.isSameItemSameComponents(proposedStack, sourceStorage.getStackInSlot(i)))
104+
extracted += sourceStorage.extractItem(i, simulatedAmount - extracted, false).getCount();
105+
if (extracted == simulatedAmount)
106+
break;
107+
}
108+
109+
logic.addTransmission(transmission);
110+
111+
destinationNode.markTransferred(transferMode != PastelTransmissionLogic.TransferMode.PUSH);
112+
sourceNode.markTransferred(transferMode != PastelTransmissionLogic.TransferMode.PULL);
113+
114+
destinationNode.addItemCountUnderway(simulatedAmount);
115+
return true;
116+
}
117+
return false;
118+
}
119+
120+
}

0 commit comments

Comments
 (0)