Skip to content

Commit 92644a1

Browse files
committed
very wip ink transfer without equalization yet
1 parent 5dca054 commit 92644a1

13 files changed

Lines changed: 204 additions & 26 deletions

src/main/java/de/dafuqs/spectrum/api/energy/InkAmount.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public record InkAmount(InkColor color, long amount) {
1313
Codec.LONG.fieldOf("amount").forGetter(InkAmount::amount)
1414
).apply(i, InkAmount::new));
1515

16-
public static final StreamCodec<ByteBuf, InkAmount> PACKET_CODEC = StreamCodec.composite(
16+
public static final StreamCodec<ByteBuf, InkAmount> STREAM_CODEC = StreamCodec.composite(
1717
InkColor.PACKET_CODEC, InkAmount::color,
1818
ByteBufCodecs.VAR_LONG, InkAmount::amount,
1919
InkAmount::new

src/main/java/de/dafuqs/spectrum/api/energy/InkPoweredStatusEffectInstance.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class InkPoweredStatusEffectInstance {
3030

3131
public static final StreamCodec<RegistryFriendlyByteBuf, InkPoweredStatusEffectInstance> PACKET_CODEC = StreamCodec.composite(
3232
MobEffectInstance.STREAM_CODEC, c -> c.statusEffectInstance,
33-
InkAmount.PACKET_CODEC, c -> c.cost,
33+
InkAmount.STREAM_CODEC, c -> c.cost,
3434
ByteBufCodecs.VAR_INT, c -> c.customColor,
3535
ByteBufCodecs.BOOL, c -> c.unidentifiable,
3636
InkPoweredStatusEffectInstance::new

src/main/java/de/dafuqs/spectrum/blocks/crystallarieum/CrystallarieumBlockEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public void setOwner(Player playerEntity) {
249249
}
250250

251251
/**
252-
* Searches recipes for a valid one using fluidStack and plants the first block of that recipe on top
252+
* Searches recipes for a valid one using inkAmount and plants the first block of that recipe on top
253253
*
254254
* @param itemStack stack that is tried to plant on top, if a valid recipe
255255
*/

src/main/java/de/dafuqs/spectrum/blocks/pastel_network/network/PastelTransmissionLogic.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import de.dafuqs.spectrum.blocks.pastel_network.payloads.*;
66
import de.dafuqs.spectrum.helpers.*;
77
import de.dafuqs.spectrum.networking.s2c_payloads.*;
8+
import de.dafuqs.spectrum.registries.*;
89
import net.minecraft.core.*;
10+
import net.minecraft.resources.*;
911
import net.minecraft.util.*;
1012
import net.minecraft.world.item.*;
1113
import net.neoforged.neoforge.items.*;
@@ -77,21 +79,8 @@ public void invalidateCache() {
7779
}
7880

7981
public void tick(PastelNetwork.NodePriority priority) {
80-
transferBetween(PastelNodeType.SENDER, PastelNodeType.GATHER, TransferMode.PUSH_PULL, priority);
81-
transferBetween(PastelNodeType.PROVIDER, PastelNodeType.GATHER, TransferMode.PULL, priority);
82-
transferBetween(PastelNodeType.STORAGE, PastelNodeType.GATHER, TransferMode.PULL, priority);
83-
transferBetween(PastelNodeType.SENDER, PastelNodeType.STORAGE, TransferMode.PUSH, priority);
84-
}
85-
86-
private void transferBetween(PastelNodeType sourceType, PastelNodeType destinationType, TransferMode transferMode, PastelNetwork.NodePriority priority) {
87-
for (PastelNodeBlockEntity sourceNode : this.network.getLoadedNodes(sourceType, priority)) {
88-
if (!sourceNode.canTransfer()) {
89-
continue;
90-
}
91-
92-
for(Supplier<? extends PastelPayloadType> payloadType : sourceNode.getSupportedPayloads()) {
93-
payloadType.get().tryTransferToType(this, sourceNode, destinationType, transferMode);
94-
}
82+
for(Map.Entry<ResourceKey<PastelPayloadType>, PastelPayloadType> payloadType : SpectrumRegistries.PASTEL_PAYLOAD_TYPE.entrySet()) {
83+
payloadType.getValue().tick(this);
9584
}
9685
}
9786

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package de.dafuqs.spectrum.blocks.pastel_network.payloads;
2+
3+
import com.mojang.blaze3d.vertex.*;
4+
import com.mojang.serialization.*;
5+
import com.mojang.serialization.codecs.*;
6+
import de.dafuqs.spectrum.api.energy.*;
7+
import de.dafuqs.spectrum.blocks.pastel_network.nodes.*;
8+
import de.dafuqs.spectrum.items.*;
9+
import de.dafuqs.spectrum.particle.client.*;
10+
import net.minecraft.client.renderer.*;
11+
import net.minecraft.client.renderer.texture.*;
12+
import net.minecraft.core.*;
13+
import net.minecraft.network.*;
14+
import net.minecraft.network.codec.*;
15+
import net.minecraft.world.item.*;
16+
import net.minecraft.world.level.*;
17+
import net.neoforged.neoforge.fluids.*;
18+
import net.neoforged.neoforge.fluids.capability.*;
19+
import org.jetbrains.annotations.*;
20+
21+
import java.util.*;
22+
23+
public record InkPastelPayload(List<InkAmount> inkAmount) implements PastelPayload {
24+
25+
public static final MapCodec<InkPastelPayload> CODEC = RecordCodecBuilder.mapCodec(i -> i.group(
26+
InkAmount.CODEC.listOf().fieldOf("stack").forGetter(InkPastelPayload::inkAmount)
27+
).apply(i, InkPastelPayload::new));
28+
29+
public static final StreamCodec<RegistryFriendlyByteBuf, InkPastelPayload> STREAM_CODEC = StreamCodec.composite(
30+
InkAmount.STREAM_CODEC.apply(ByteBufCodecs.list()), InkPastelPayload::inkAmount,
31+
InkPastelPayload::new
32+
);
33+
34+
public void render(PastelTransmissionParticle particle, Level level, PoseStack poseStack, final MultiBufferSource vertexConsumers, int light) {
35+
particle.itemRenderer.renderStatic(PigmentItem.byColor(this.inkAmount.get(0).color()).getDefaultInstance(), ItemDisplayContext.GROUND, light, OverlayTexture.NO_OVERLAY, poseStack, vertexConsumers, level, 0);
36+
}
37+
38+
public MapCodec<InkPastelPayload> codec() {
39+
return CODEC;
40+
}
41+
42+
public StreamCodec<RegistryFriendlyByteBuf, InkPastelPayload> streamCodec() {
43+
return STREAM_CODEC;
44+
}
45+
46+
@Override
47+
public void arriveAtDestination(Level level, BlockPos destination, @Nullable PastelNodeBlockEntity destinationNode) {
48+
if (destinationNode != null) {
49+
@Nullable InkStorageBlockEntity<?> destinationHandler = InkPastelPayloadType.getConnectedInkStorage(destinationNode);
50+
if (destinationHandler != null) {
51+
InkStorage destinationStorage = destinationHandler.getEnergyStorage();
52+
for(InkAmount ic : inkAmount) {
53+
destinationStorage.addEnergy(ic.color(), ic.amount());
54+
destinationHandler.setInkDirty();
55+
}
56+
}
57+
}
58+
}
59+
60+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import de.dafuqs.spectrum.blocks.pastel_network.nodes.*;
66
import net.minecraft.resources.*;
77

8+
import java.util.function.*;
9+
810
public abstract class PastelPayloadType {
911

1012
protected final ResourceLocation id;
@@ -15,6 +17,25 @@ public PastelPayloadType(ResourceLocation id, MapCodec<? extends PastelPayload>
1517
this.codec = codec;
1618
}
1719

18-
public abstract void tryTransferToType(PastelTransmissionLogic logic, PastelNodeBlockEntity sourceNode, PastelNodeType type, PastelTransmissionLogic.TransferMode transferMode);
20+
public void tick(PastelTransmissionLogic logic) {
21+
transferBetween(logic, PastelNodeType.SENDER, PastelNodeType.GATHER, PastelTransmissionLogic.TransferMode.PUSH_PULL);
22+
transferBetween(logic, PastelNodeType.PROVIDER, PastelNodeType.GATHER, PastelTransmissionLogic.TransferMode.PULL);
23+
transferBetween(logic, PastelNodeType.STORAGE, PastelNodeType.GATHER, PastelTransmissionLogic.TransferMode.PULL);
24+
transferBetween(logic, PastelNodeType.SENDER, PastelNodeType.STORAGE, PastelTransmissionLogic.TransferMode.PUSH);
25+
}
26+
27+
protected void transferBetween(PastelTransmissionLogic logic, PastelNodeType sourceType, PastelNodeType destinationType, PastelTransmissionLogic.TransferMode transferMode) {
28+
for (PastelNodeBlockEntity sourceNode : logic.getLoadedNodes(sourceType)) {
29+
if (!sourceNode.canTransfer()) {
30+
continue;
31+
}
32+
33+
for(Supplier<? extends PastelPayloadType> payloadType : sourceNode.getSupportedPayloads()) {
34+
payloadType.get().tryTransferToType(logic, sourceNode, destinationType, transferMode);
35+
}
36+
}
37+
}
38+
39+
protected abstract void tryTransferToType(PastelTransmissionLogic logic, PastelNodeBlockEntity sourceNode, PastelNodeType type, PastelTransmissionLogic.TransferMode transferMode);
1940

2041
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class SpectrumPastelPayloadTypes {
1111

1212
public static final DeferredHolder<PastelPayloadType, ItemPastelPayloadType> ITEM = REGISTRAR.register("item", ItemPastelPayloadType::new);
1313
public static final DeferredHolder<PastelPayloadType, FluidPastelPayloadType> FLUID = REGISTRAR.register("fluid", FluidPastelPayloadType::new);
14-
//public static final DeferredHolder<PastelPayloadType, ItemPastelPayloadType> INK = REGISTRAR.register("ink", InkPastelPayload.CODEC);
14+
public static final DeferredHolder<PastelPayloadType, InkPastelPayloadType> INK = REGISTRAR.register("ink", InkPastelPayloadType::new);
1515

1616
public static void register(IEventBus modBus) {
1717
REGISTRAR.register(modBus);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class SpectrumPastelPayloads {
1212

1313
public static final DeferredHolder<MapCodec<? extends PastelPayload>, MapCodec<ItemPastelPayload>> ITEM = REGISTRAR.register("item", () -> ItemPastelPayload.CODEC);
1414
public static final DeferredHolder<MapCodec<? extends PastelPayload>, MapCodec<FluidPastelPayload>> FLUID = REGISTRAR.register("fluid", () -> FluidPastelPayload.CODEC);
15-
//DeferredHolder<MapCodec<? extends PastelPayload>, MapCodec<InkPastelPayload>> INK = REGISTRAR.register("ink", () -> InkPastelPayload.CODEC);
15+
public static final DeferredHolder<MapCodec<? extends PastelPayload>, MapCodec<InkPastelPayload>> INK = REGISTRAR.register("ink", () -> InkPastelPayload.CODEC);
1616

1717
public static void register(IEventBus modBus) {
1818
REGISTRAR.register(modBus);

src/main/java/de/dafuqs/spectrum/compat/travelersbackpack/TravelersBackpackCompat.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void register() {
3838
/*
3939
EffectFluidRegistry.registerFluidEffect(new SpectrumEffectFluid("spectrum:sludge", SpectrumFluids.SLUDGE.get().getSource()) {
4040
@Override
41-
public void affectDrinker(FluidStack fluidStack, Level world, Entity entity) {
41+
public void affectDrinker(FluidStack inkAmount, Level world, Entity entity) {
4242
if (entity instanceof LivingEntity livingEntity) {
4343
livingEntity.addEffect(new MobEffectInstance(MobEffects.CONFUSION, 200));
4444
livingEntity.addEffect(new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE, 400, 2));
@@ -49,7 +49,7 @@ public void affectDrinker(FluidStack fluidStack, Level world, Entity entity) {
4949
5050
EffectFluidRegistry.registerFluidEffect(new SpectrumEffectFluid("spectrum:liquid_crystal", SpectrumFluids.LIQUID_CRYSTAL.get().getSource()) {
5151
@Override
52-
public void affectDrinker(FluidStack fluidStack, Level world, Entity entity) {
52+
public void affectDrinker(FluidStack inkAmount, Level world, Entity entity) {
5353
if (entity instanceof Player player) {
5454
player.addEffect(new MobEffectInstance(MobEffects.REGENERATION, 400, 1));
5555
}
@@ -58,7 +58,7 @@ public void affectDrinker(FluidStack fluidStack, Level world, Entity entity) {
5858
5959
EffectFluidRegistry.registerFluidEffect(new SpectrumEffectFluid("spectrum:midnight_solution", SpectrumFluids.MIDNIGHT_SOLUTION.get().getSource()) {
6060
@Override
61-
public void affectDrinker(FluidStack fluidStack, Level world, Entity entity) {
61+
public void affectDrinker(FluidStack inkAmount, Level world, Entity entity) {
6262
if (entity instanceof Player player) {
6363
player.giveExperiencePoints(-20);
6464
@@ -81,7 +81,7 @@ public void affectDrinker(FluidStack fluidStack, Level world, Entity entity) {
8181
8282
EffectFluidRegistry.registerFluidEffect(new SpectrumEffectFluid("spectrum:dragonrot", SpectrumFluids.DRAGONROT.get().getSource()) {
8383
@Override
84-
public void affectDrinker(FluidStack fluidStack, Level world, Entity entity) {
84+
public void affectDrinker(FluidStack inkAmount, Level world, Entity entity) {
8585
if (entity instanceof LivingEntity livingEntity) {
8686
livingEntity.addEffect(new MobEffectInstance(SpectrumStatusEffects.LIFE_DRAIN, 600, 3));
8787
livingEntity.hurt(SpectrumDamageTypes.dragonrot(world), 1000); // 💀

0 commit comments

Comments
 (0)