Skip to content

Commit 6cf8ca1

Browse files
committed
make pastel transfers extensible
1 parent cf09832 commit 6cf8ca1

36 files changed

Lines changed: 251 additions & 140 deletions

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +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.network.*;
78
import de.dafuqs.spectrum.capabilities.*;
89
import de.dafuqs.spectrum.compat.*;
910
import de.dafuqs.spectrum.config.*;
@@ -121,6 +122,7 @@ public SpectrumCommon(ModContainer container, IEventBus modBus) {
121122
SpectrumItemGroups.register(modBus);
122123
logInfo("Registering Block Entities...");
123124
SpectrumBlockEntities.register(modBus);
125+
PastelPayload.register(modBus);
124126
modBus.addListener(SpectrumBlockEntities::addBlockEntityTypeBlocks);
125127
SpectrumPastelUpgradeSignatures.register(modBus);
126128

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package de.dafuqs.spectrum.api.energy;
2+
3+
import com.mojang.serialization.*;
4+
import com.mojang.serialization.codecs.*;
5+
import de.dafuqs.spectrum.api.energy.color.*;
6+
import io.netty.buffer.*;
7+
import net.minecraft.network.codec.*;
8+
9+
public record InkAmount(InkColor color, long amount) {
10+
11+
public static final Codec<InkAmount> CODEC = RecordCodecBuilder.create(i -> i.group(
12+
InkColor.CODEC.fieldOf("color").forGetter(InkAmount::color),
13+
Codec.LONG.fieldOf("amount").forGetter(InkAmount::amount)
14+
).apply(i, InkAmount::new));
15+
16+
public static final StreamCodec<ByteBuf, InkAmount> PACKET_CODEC = StreamCodec.composite(
17+
InkColor.PACKET_CODEC, InkAmount::color,
18+
ByteBufCodecs.VAR_LONG, InkAmount::amount,
19+
InkAmount::new
20+
);
21+
22+
}

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

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ static boolean tryDrainEnergy(@NotNull Container inventory, InkColor color, long
114114
return false;
115115
}
116116

117-
static boolean tryDrainEnergy(@NotNull Player player, @NotNull InkCost inkCost) {
118-
return tryDrainEnergy(player, inkCost.color(), inkCost.cost());
117+
static boolean tryDrainEnergy(@NotNull Player player, @NotNull InkAmount inkAmount) {
118+
return tryDrainEnergy(player, inkAmount.color(), inkAmount.amount());
119119
}
120120

121-
static boolean tryDrainEnergy(@NotNull Player player, @NotNull InkCost inkCost, float costModifier) {
122-
return tryDrainEnergy(player, inkCost.color(), Support.getIntFromDecimalWithChance(inkCost.cost() * costModifier, player.getRandom()));
121+
static boolean tryDrainEnergy(@NotNull Player player, @NotNull InkAmount inkAmount, float costModifier) {
122+
return tryDrainEnergy(player, inkAmount.color(), Support.getIntFromDecimalWithChance(inkAmount.amount() * costModifier, player.getRandom()));
123123
}
124124

125125
/**
@@ -176,8 +176,8 @@ static boolean tryDrainEnergy(@NotNull Player player, @NotNull InkColor color, l
176176
return false;
177177
}
178178

179-
static boolean hasAvailableInk(Player player, InkCost inkCost) {
180-
return hasAvailableInk(player, inkCost.color(), inkCost.cost());
179+
static boolean hasAvailableInk(Player player, InkAmount inkAmount) {
180+
return hasAvailableInk(player, inkAmount.color(), inkAmount.amount());
181181
}
182182

183183
static boolean hasAvailableInk(Player player, InkColor color, long amount) {
@@ -258,10 +258,10 @@ static long getAvailableInk(@NotNull Player player, InkColor color) {
258258
return available;
259259
}
260260

261-
default boolean payForUse(Player player, ItemStack stack, @NotNull InkCost inkCost, @Nullable Ingredient itemCost) {
261+
default boolean payForUse(Player player, ItemStack stack, @NotNull InkAmount inkAmount, @Nullable Ingredient itemCost) {
262262
boolean paid = player.isCreative(); // free for creative players
263263
if (!paid) { // try pay with ink
264-
paid = InkPowered.tryDrainEnergy(player, inkCost, getInkCostMod(player.level().registryAccess(), stack));
264+
paid = InkPowered.tryDrainEnergy(player, inkAmount, getInkCostMod(player.level().registryAccess(), stack));
265265
}
266266
if (!paid && itemCost != null && player.getInventory().contains(itemCost)) { // try pay with item
267267
int efficiencyLevel = SpectrumEnchantmentHelper.getLevel(player.level().registryAccess(), Enchantments.EFFICIENCY, stack);

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,25 @@ public class InkPoweredStatusEffectInstance {
2323

2424
public static final Codec<InkPoweredStatusEffectInstance> CODEC = RecordCodecBuilder.create(i -> i.group(
2525
MobEffectInstance.CODEC.fieldOf("effect").forGetter(c -> c.statusEffectInstance),
26-
InkCost.CODEC.fieldOf("ink_cost").forGetter(c -> c.cost),
26+
InkAmount.CODEC.fieldOf("ink_cost").forGetter(c -> c.cost),
2727
Codec.INT.optionalFieldOf("custom_color", -1).forGetter(c -> c.customColor),
2828
Codec.BOOL.optionalFieldOf("unidentifiable", false).forGetter(c -> c.unidentifiable)
2929
).apply(i, InkPoweredStatusEffectInstance::new));
3030

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

3939
private final MobEffectInstance statusEffectInstance;
40-
private final InkCost cost;
40+
private final InkAmount cost;
4141
private final int customColor; // -1: use effect default
4242
private final boolean unidentifiable;
4343

44-
public InkPoweredStatusEffectInstance(MobEffectInstance statusEffectInstance, InkCost cost, int customColor, boolean unidentifiable) {
44+
public InkPoweredStatusEffectInstance(MobEffectInstance statusEffectInstance, InkAmount cost, int customColor, boolean unidentifiable) {
4545
this.statusEffectInstance = statusEffectInstance;
4646
this.cost = cost;
4747
this.customColor = customColor;
@@ -52,7 +52,7 @@ public MobEffectInstance getStatusEffectInstance() {
5252
return statusEffectInstance;
5353
}
5454

55-
public InkCost getInkCost() {
55+
public InkAmount getInkCost() {
5656
return cost;
5757
}
5858

@@ -78,7 +78,7 @@ public static void buildTooltip(List<Component> tooltip, List<InkPoweredStatusEf
7878
continue;
7979
}
8080

81-
InkCost cost = entry.getInkCost();
81+
InkAmount cost = entry.getInkCost();
8282

8383
MutableComponent mutableText = Component.translatable(effect.getDescriptionId());
8484
if (effect.getAmplifier() > 0) {
@@ -88,7 +88,7 @@ public static void buildTooltip(List<Component> tooltip, List<InkPoweredStatusEf
8888
mutableText = Component.translatable("potion.withDuration", mutableText, MobEffectUtil.formatDuration(effect, 1.0F, tickRate));
8989
}
9090
mutableText.withStyle(effect.getEffect().value().getCategory().getTooltipFormatting());
91-
mutableText.append(Component.translatable("spectrum.tooltip.ink_cost", Support.getShortenedNumberString(cost.cost()), cost.color().getColoredInkName()).withStyle(ChatFormatting.GRAY));
91+
mutableText.append(Component.translatable("spectrum.tooltip.ink_cost", Support.getShortenedNumberString(cost.amount()), cost.color().getColoredInkName()).withStyle(ChatFormatting.GRAY));
9292
tooltip.add(mutableText);
9393

9494
effect.getEffect().value().createModifiers(effect.getAmplifier(), (attribute, modifier) ->

src/main/java/de/dafuqs/spectrum/api/item/InkPoweredPotionFillable.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public interface InkPoweredPotionFillable {
1313
int maxEffectCount();
1414
int maxEffectAmplifier();
1515

16-
// used for calculating the items cost to apply a certain effect
16+
// used for calculating the items amount to apply a certain effect
1717
// calculated once and then stored in the items nbt for quick lookup and nicer modifiability
1818
// via commands or special loot (so ones found in dungeon chests can be cheaper!)
1919
default long adjustFinalCostFor(@NotNull InkPoweredStatusEffectInstance instance) {
20-
return (long) Math.pow(instance.getInkCost().cost(), 1 + instance.getStatusEffectInstance().getAmplifier());
20+
return (long) Math.pow(instance.getInkCost().amount(), 1 + instance.getStatusEffectInstance().getAmplifier());
2121
}
2222

2323
// saving
@@ -36,8 +36,8 @@ default void addOrUpgradeEffects(ItemStack potionFillableStack, List<InkPoweredS
3636
break;
3737
}
3838

39-
// calculate the final cost of this effect and add it
40-
InkCost adjustedCost = new InkCost(newEffect.getInkCost().color(), adjustFinalCostFor(newEffect));
39+
// calculate the final amount of this effect and add it
40+
InkAmount adjustedCost = new InkAmount(newEffect.getInkCost().color(), adjustFinalCostFor(newEffect));
4141
InkPoweredStatusEffectInstance modifiedInstance = new InkPoweredStatusEffectInstance(statusEffectInstance, adjustedCost, newEffect.getColor(), newEffect.isUnidentifiable());
4242
existingEffects.add(modifiedInstance);
4343
}

src/main/java/de/dafuqs/spectrum/blocks/enchanter/EnchanterBlockEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ public static int getRequiredExperienceToEnchantCenterItem(@NotNull EnchanterBlo
386386
requiredExperience += currentRequired;
387387
valid = true;
388388
} else {
389-
requiredExperience += 50; // conflicting enchantments (like more enchantments in a book where not all can be applied cost extra
389+
requiredExperience += 50; // conflicting enchantments (like more enchantments in a book where not all can be applied amount extra
390390
}
391391
}
392392
if (valid) { // and applicable enchantment found
@@ -564,7 +564,7 @@ public void craftEnchantmentUpgradeRecipe(Level world, @NotNull EnchantmentUpgra
564564
SpectrumAdvancementCriteria.ENCHANTER_UPGRADING.trigger(serverPlayerEntity, builder.toImmutable(), xpCost);
565565
}
566566

567-
// update the item cost if chain upgrading
567+
// update the item amount if chain upgrading
568568
if (recipeMatches(this, level)) {
569569
craftingTimeTotal = upgrade.getItemScaling().apply(targetLevel);
570570
} else {
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package de.dafuqs.spectrum.blocks.pastel_network.network;
2+
3+
import com.mojang.blaze3d.vertex.*;
4+
import com.mojang.serialization.*;
5+
import com.mojang.serialization.codecs.*;
6+
import de.dafuqs.spectrum.*;
7+
import de.dafuqs.spectrum.blocks.pastel_network.nodes.*;
8+
import de.dafuqs.spectrum.helpers.*;
9+
import de.dafuqs.spectrum.particle.client.*;
10+
import de.dafuqs.spectrum.registries.*;
11+
import net.minecraft.client.renderer.*;
12+
import net.minecraft.client.renderer.texture.*;
13+
import net.minecraft.core.*;
14+
import net.minecraft.network.*;
15+
import net.minecraft.network.codec.*;
16+
import net.minecraft.resources.*;
17+
import net.minecraft.world.item.*;
18+
import net.minecraft.world.level.*;
19+
import net.neoforged.bus.api.*;
20+
import net.neoforged.neoforge.items.*;
21+
import net.neoforged.neoforge.registries.*;
22+
import org.jetbrains.annotations.*;
23+
24+
import java.util.function.*;
25+
26+
public interface PastelPayload {
27+
28+
Codec<PastelPayload> CODEC = SpectrumRegistries.PASTEL_PAYLOAD_TYPE.byNameCodec().dispatch(PastelPayload::codec, codec -> codec);
29+
StreamCodec<RegistryFriendlyByteBuf, PastelPayload> STREAM_CODEC = ByteBufCodecs.fromCodecWithRegistries(CODEC);
30+
31+
DeferredRegister<MapCodec<? extends PastelPayload>> REGISTRAR = DeferredRegister.create(SpectrumRegistryKeys.PASTEL_PAYLOAD_TYPE, SpectrumCommon.MOD_ID);
32+
33+
static void register(IEventBus modBus) {
34+
REGISTRAR.register("item", () -> ItemPastelPayload.CODEC);
35+
36+
REGISTRAR.register(modBus);
37+
}
38+
39+
MapCodec<? extends PastelPayload> codec();
40+
StreamCodec<RegistryFriendlyByteBuf, ? extends PastelPayload> streamCodec();
41+
42+
/**
43+
*
44+
* @param level the level
45+
* @param destination the position of the node to arrive at
46+
* @param destinationNode the pastel node. Can be null if it was broken while the transmission was underway
47+
*/
48+
void arriveAtDestination(Level level, BlockPos destination, PastelNodeBlockEntity destinationNode);
49+
50+
void render(PastelTransmissionParticle pastelTransmissionParticle, Level level, PoseStack poseStack, MultiBufferSource vertexConsumers, int light);
51+
52+
// void spawnTravelParticles(Level level); // TODO
53+
54+
class ItemPastelPayload implements PastelPayload {
55+
56+
public static final MapCodec<ItemPastelPayload> CODEC = RecordCodecBuilder.mapCodec(i -> i.group(
57+
ItemStack.CODEC.fieldOf("stack").forGetter(ItemPastelPayload::getItemStack)
58+
).apply(i, ItemPastelPayload::new));
59+
60+
public static final StreamCodec<RegistryFriendlyByteBuf, ItemPastelPayload> STREAM_CODEC = StreamCodec.composite(
61+
ItemStack.STREAM_CODEC, ItemPastelPayload::getItemStack,
62+
ItemPastelPayload::new
63+
);
64+
65+
private final ItemStack itemStack;
66+
67+
public ItemPastelPayload(ItemStack itemStack) {
68+
this.itemStack = itemStack;
69+
}
70+
71+
public ItemStack getItemStack() {
72+
return itemStack;
73+
}
74+
75+
public void arriveAtDestination(Level level, BlockPos destination, @Nullable PastelNodeBlockEntity destinationNode) {
76+
int inserted = 0;
77+
int count = itemStack.getCount();
78+
if (destinationNode != null) {
79+
IItemHandler destinationStorage = destinationNode.getConnectedStorage();
80+
if (destinationStorage != null) {
81+
inserted = count;
82+
inserted -= ItemHandlerHelper.insertItemStacked(destinationStorage, itemStack.copyWithCount(count), false).getCount();
83+
destinationNode.addItemCountUnderway(-count);
84+
}
85+
}
86+
87+
int amount = itemStack.getCount();
88+
if (inserted != amount) {
89+
long diff = amount - inserted;
90+
InWorldInteractionHelper.scatter(level, destination.getX() + 0.5, destination.getY() + 0.5, destination.getZ() + 0.5, itemStack, diff);
91+
if (destinationNode != null) {
92+
destinationNode.addItemCountUnderway(-diff);
93+
}
94+
}
95+
}
96+
97+
public void render(PastelTransmissionParticle particle, Level level, PoseStack poseStack, final MultiBufferSource vertexConsumers, int light) {
98+
particle.itemRenderer.renderStatic(this.itemStack, ItemDisplayContext.GROUND, light, OverlayTexture.NO_OVERLAY, poseStack, vertexConsumers, level, 0);
99+
}
100+
101+
public MapCodec<ItemPastelPayload> codec() {
102+
return CODEC;
103+
}
104+
105+
public StreamCodec<RegistryFriendlyByteBuf, ItemPastelPayload> streamCodec() {
106+
return STREAM_CODEC;
107+
}
108+
}
109+
110+
/*class FluidPastelPayload implements PastelPayload {
111+
private FluidStack fluidStack;
112+
public FluidPastelPayload(FluidStack fluidStack) {
113+
this.fluidStack = fluidStack;
114+
}
115+
}
116+
117+
class InkPastelPayload implements PastelPayload {
118+
private InkAmount inkAmount;
119+
public InkPastelPayload(InkAmount inkAmount) {
120+
this.inkAmount = inkAmount;
121+
}
122+
}*/
123+
124+
}

0 commit comments

Comments
 (0)