Skip to content

Commit

Permalink
Start the ink storage component refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Electro593 committed Jan 16, 2025
1 parent b370dee commit 38a1afb
Show file tree
Hide file tree
Showing 34 changed files with 104 additions and 129 deletions.
18 changes: 1 addition & 17 deletions src/main/java/de/dafuqs/spectrum/api/energy/InkCost.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,7 @@

import java.util.*;

public class InkCost {

private final InkColor color;
private final long cost;

public InkCost(InkColor color, long cost) {
this.color = color;
this.cost = cost;
}

public InkColor getColor() {
return color;
}

public long getCost() {
return cost;
}
public record InkCost(InkColor color, long cost) {

public void writeNbt(NbtCompound nbt) {
nbt.putString("InkColor", color.getID().toString());
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/dafuqs/spectrum/api/energy/InkPowered.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ static boolean tryDrainEnergy(@NotNull Inventory inventory, InkColor color, long
}

static boolean tryDrainEnergy(@NotNull PlayerEntity player, @NotNull InkCost inkCost) {
return tryDrainEnergy(player, inkCost.getColor(), inkCost.getCost());
return tryDrainEnergy(player, inkCost.color(), inkCost.cost());
}

static boolean tryDrainEnergy(@NotNull PlayerEntity player, @NotNull InkCost inkCost, float costModifier) {
return tryDrainEnergy(player, inkCost.getColor(), Support.getIntFromDecimalWithChance(inkCost.getCost() * costModifier, player.getRandom()));
return tryDrainEnergy(player, inkCost.color(), Support.getIntFromDecimalWithChance(inkCost.cost() * costModifier, player.getRandom()));
}

/**
Expand Down Expand Up @@ -207,7 +207,7 @@ static long getAvailableInk(@NotNull PlayerEntity player, InkColor color) {
}

static boolean hasAvailableInk(PlayerEntity player, InkCost inkCost) {
return hasAvailableInk(player, inkCost.getColor(), inkCost.getCost());
return hasAvailableInk(player, inkCost.color(), inkCost.cost());
}

static boolean hasAvailableInk(PlayerEntity player, InkColor color, long amount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static void buildTooltip(List<Text> tooltip, List<InkPoweredStatusEffectI
mutableText = Text.translatable("potion.withDuration", mutableText, StatusEffectUtil.getDurationText(effect, 1.0F, tickRate));
}
mutableText.formatted(effect.getEffectType().value().getCategory().getFormatting());
mutableText.append(Text.translatable("spectrum.tooltip.ink_cost", Support.getShortenedNumberString(cost.getCost()), cost.getColor().getColoredInkName()).formatted(Formatting.GRAY));
mutableText.append(Text.translatable("spectrum.tooltip.ink_cost", Support.getShortenedNumberString(cost.cost()), cost.color().getColoredInkName()).formatted(Formatting.GRAY));
if (entry.isIncurable()) {
mutableText.append(Text.translatable("item.spectrum.potion.tooltip.incurable"));
}
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/de/dafuqs/spectrum/api/energy/color/InkColor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.mojang.serialization.DataResult;
import de.dafuqs.spectrum.helpers.*;
import de.dafuqs.spectrum.registries.*;
import io.netty.buffer.*;
import net.minecraft.network.codec.*;
import net.minecraft.registry.tag.*;
import net.minecraft.text.*;
import net.minecraft.util.*;
Expand All @@ -13,10 +15,16 @@

public class InkColor {

public static Codec<InkColor> CODEC = Identifier.CODEC.comapFlatMap(
public static final Codec<InkColor> CODEC = Identifier.CODEC.comapFlatMap(
id -> ofId(id).map(DataResult::success).orElse(DataResult.error(() -> "Not a valid ink color: " + id)),
InkColor::getID);
InkColor::getID
);

public static final PacketCodec<ByteBuf, InkColor> PACKET_CODEC = Identifier.PACKET_CODEC.xmap(
id -> ofId(id).orElseThrow(),
InkColor::getID
);

protected static final Map<DyeColor, InkColor> DYE_TO_COLOR = new HashMap<>();

protected final DyeColor dyeColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static InkColor getRandomMixedColor(InkColor color1, InkColor color2, Ran

if (!possibleOutcomes.isEmpty()) {
Collections.shuffle(possibleOutcomes);
return possibleOutcomes.get(0);
return possibleOutcomes.getFirst();
}
return color1;
} else if (color1Elemental) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package de.dafuqs.spectrum.api.energy.color;

import de.dafuqs.spectrum.*;
import de.dafuqs.spectrum.helpers.*;
import de.dafuqs.spectrum.registries.*;
import net.minecraft.registry.*;
import net.minecraft.registry.entry.*;
import net.minecraft.util.*;
import org.joml.*;

import java.util.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import de.dafuqs.spectrum.api.energy.*;
import de.dafuqs.spectrum.api.energy.color.*;
import net.minecraft.nbt.*;
import net.minecraft.text.*;
import org.jetbrains.annotations.*;

import java.util.*;

Expand All @@ -20,10 +18,6 @@ public CreativeInkStorage() {
super();
}

public static CreativeInkStorage fromNbt(@NotNull NbtCompound compound) {
return new CreativeInkStorage();
}

@Override
public boolean accepts(InkColor color) {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package de.dafuqs.spectrum.api.energy.storage;

import de.dafuqs.spectrum.api.energy.*;
import de.dafuqs.spectrum.api.energy.color.*;
import net.fabricmc.api.*;
import net.minecraft.nbt.*;
import net.minecraft.text.*;
import org.jetbrains.annotations.*;

import java.util.*;

Expand Down Expand Up @@ -122,12 +119,6 @@ public long getEnergy(InkColor color) {
return maxAmount;
}

public static TotalCappedElementalMixingInkStorage fromNbt(@NotNull NbtCompound compound) {
long maxEnergyTotal = compound.getLong("MaxEnergyTotal");
Map<InkColor, Long> energy = InkStorage.readEnergy(compound.getCompound("Energy"));
return new TotalCappedElementalMixingInkStorage(maxEnergyTotal, energy);
}

@Override
public void fillCompletely() {
this.storedEnergy.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface InkPoweredPotionFillable {
// calculated once and then stored in the items nbt for quick lookup and nicer modifiability
// via commands or special loot (so ones found in dungeon chests can be cheaper!)
default long adjustFinalCostFor(@NotNull InkPoweredStatusEffectInstance instance) {
return (long) Math.pow(instance.getInkCost().getCost(), 1 + instance.getStatusEffectInstance().getAmplifier());
return (long) Math.pow(instance.getInkCost().cost(), 1 + instance.getStatusEffectInstance().getAmplifier());
}

// saving
Expand All @@ -36,7 +36,7 @@ default void addOrUpgradeEffects(ItemStack potionFillableStack, List<InkPoweredS
}

// calculate the final cost of this effect and add it
InkCost adjustedCost = new InkCost(newEffect.getInkCost().getColor(), adjustFinalCostFor(newEffect));
InkCost adjustedCost = new InkCost(newEffect.getInkCost().color(), adjustFinalCostFor(newEffect));
InkPoweredStatusEffectInstance modifiedInstance = new InkPoweredStatusEffectInstance(statusEffectInstance, adjustedCost, newEffect.getColor(), newEffect.isUnidentifiable(), newEffect.isIncurable());
existingEffects.add(modifiedInstance);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package de.dafuqs.spectrum.component_type;

import com.mojang.serialization.*;
import com.mojang.serialization.codecs.*;
import de.dafuqs.spectrum.api.energy.*;
import de.dafuqs.spectrum.api.energy.color.*;
import io.netty.buffer.*;
import net.minecraft.network.codec.*;
import net.minecraft.util.dynamic.*;

import java.util.*;

public record InkStorageComponent(long maxEnergyTotal, long maxPerColor, Map<InkColor, Long> storedEnergy) {

public static final Codec<InkStorageComponent> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.LONG.fieldOf("max_energy_total").forGetter(c -> c.maxEnergyTotal),
Codec.LONG.fieldOf("max_per_color").forGetter(c -> c.maxPerColor),
Codecs.strictUnboundedMap(InkColor.CODEC, Codec.LONG).fieldOf("stored_energy").forGetter(c -> c.storedEnergy)
).apply(instance, InkStorageComponent::new));

public static final PacketCodec<ByteBuf, InkStorageComponent> PACKET_CODEC = PacketCodec.tuple(
PacketCodecs.VAR_LONG,
c -> c.maxEnergyTotal,
PacketCodecs.VAR_LONG,
c -> c.maxPerColor,
PacketCodecs.map(HashMap::new, InkColor.PACKET_CODEC, PacketCodecs.VAR_LONG),
c -> c.storedEnergy,
InkStorageComponent::new
);

public InkStorageComponent(InkStorage storage) {
this(storage.getMaxTotal(), storage.getMaxPerColor(), storage.getEnergy());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private boolean tryApplyOil(ItemStack oil, ItemStack food, PlayerEntity user) {
return false;

var effect = getEffects(oil).get(0);
if (!InkPowered.tryDrainEnergy(user, effect.getInkCost().getColor(), effect.getInkCost().getCost()))
if (!InkPowered.tryDrainEnergy(user, effect.getInkCost().color(), effect.getInkCost().cost()))
return false;

var foodComponent = food.get(DataComponentTypes.FOOD);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package de.dafuqs.spectrum.items.bundles;

import de.dafuqs.spectrum.registries.SpectrumDataComponentTypes;
import net.minecraft.block.Block;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.BundleContentsComponent;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.StackReference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.item.*;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.entry.*;
import net.minecraft.text.*;
import net.minecraft.util.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,4 @@ public GemstonePowderItem(Settings settings, Identifier cloakAdvancementIdentifi
this.gemstoneColor = gemstoneColor;
}

public GemstoneColor getGemstoneColor() {
return gemstoneColor;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public class StormStoneItem extends CloakedItem implements DamageAwareItem {

public StormStoneItem(Settings settings, Identifier cloakAdvancementIdentifier, Item cloakItem) {
super(settings, cloakAdvancementIdentifier, cloakItem);


}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import de.dafuqs.spectrum.api.energy.storage.*;
import de.dafuqs.spectrum.api.item.*;
import de.dafuqs.spectrum.api.render.*;
import de.dafuqs.spectrum.component_type.*;
import de.dafuqs.spectrum.helpers.*;
import de.dafuqs.spectrum.items.trinkets.*;
import de.dafuqs.spectrum.registries.*;
Expand All @@ -15,9 +16,7 @@
import net.minecraft.entity.player.*;
import net.minecraft.item.*;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.nbt.*;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.entry.*;
import net.minecraft.text.*;
import net.minecraft.util.*;
import org.jetbrains.annotations.*;
Expand All @@ -40,10 +39,9 @@ public Drainability getDrainability() {

@Override
public TotalCappedElementalMixingInkStorage getEnergyStorage(ItemStack itemStack) {
NbtCompound compound = itemStack.getNbt();
if (compound != null && compound.contains("EnergyStore")) {
return TotalCappedElementalMixingInkStorage.fromNbt(compound.getCompound("EnergyStore"));
}
var storage = itemStack.get(SpectrumDataComponentTypes.INK_STORAGE);
if (storage != null)
return new TotalCappedElementalMixingInkStorage(storage.maxEnergyTotal(), storage.storedEnergy());
return new TotalCappedElementalMixingInkStorage(this.maxEnergyTotal, Map.of());
}

Expand All @@ -55,10 +53,7 @@ public ItemStack getDefaultStack() {

@Override
public void setEnergyStorage(ItemStack itemStack, InkStorage storage) {
if (storage instanceof TotalCappedElementalMixingInkStorage artistsPaletteInkStorage) {
NbtCompound compound = itemStack.getOrCreateNbt();
compound.put("EnergyStore", artistsPaletteInkStorage.toNbt());
}
itemStack.set(SpectrumDataComponentTypes.INK_STORAGE, new InkStorageComponent(storage));
}

@Override
Expand Down Expand Up @@ -98,7 +93,7 @@ public ExtendedItemBarProvider.BarSignature getSignature(@Nullable PlayerEntity

var progress = Support.getSensiblePercent(storage.getCurrentTotal(), storage.getMaxTotal(), 14);
if (colors.size() == 1) {
var color = colors.get(0);
var color = colors.getFirst();
return new ExtendedItemBarProvider.BarSignature(1, 13, 14, progress, 1, color.getColorInt() | 0xFF000000, 2, DEFAULT_BACKGROUND_COLOR);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
import de.dafuqs.spectrum.registries.*;
import net.fabricmc.api.*;
import net.minecraft.block.entity.*;
import net.minecraft.client.item.*;
import net.minecraft.entity.player.*;
import net.minecraft.item.*;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.nbt.*;
import net.minecraft.text.*;
import net.minecraft.util.*;
import net.minecraft.world.*;
Expand Down Expand Up @@ -48,10 +46,6 @@ public Drainability getDrainability() {

@Override
public CreativeInkStorage getEnergyStorage(ItemStack itemStack) {
NbtCompound compound = itemStack.getNbt();
if (compound != null && compound.contains("EnergyStore")) {
return CreativeInkStorage.fromNbt(compound.getCompound("EnergyStore"));
}
return new CreativeInkStorage();
}

Expand Down Expand Up @@ -92,7 +86,7 @@ public int getBackgroundColor(@Nullable PlayerEntity player, ItemStack stack, fl
}

if (colors.size() == 1) {
var color = colors.get(0);
var color = colors.getFirst();
return ColorHelper.colorVecToRGB(color.getColorVec());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
import de.dafuqs.spectrum.api.energy.color.*;
import de.dafuqs.spectrum.api.energy.storage.*;
import de.dafuqs.spectrum.api.render.*;
import de.dafuqs.spectrum.component_type.*;
import de.dafuqs.spectrum.helpers.*;
import de.dafuqs.spectrum.registries.*;
import net.fabricmc.api.*;
import net.minecraft.client.*;
import net.minecraft.client.item.*;
import net.minecraft.entity.player.*;
import net.minecraft.item.*;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.nbt.*;
import net.minecraft.text.*;
import net.minecraft.world.*;
import org.jetbrains.annotations.*;

import java.util.*;
Expand All @@ -35,10 +33,9 @@ public Drainability getDrainability() {

@Override
public IndividualCappedInkStorage getEnergyStorage(ItemStack itemStack) {
NbtCompound compound = itemStack.getNbt();
if (compound != null && compound.contains("EnergyStore")) {
return IndividualCappedInkStorage.fromNbt(compound.getCompound("EnergyStore"));
}
var storage = itemStack.get(SpectrumDataComponentTypes.INK_STORAGE);
if (storage != null)
return new IndividualCappedInkStorage(storage.maxEnergyTotal(), storage.storedEnergy());
return new IndividualCappedInkStorage(this.maxEnergy);
}

Expand All @@ -50,10 +47,7 @@ public ItemStack getDefaultStack() {

@Override
public void setEnergyStorage(ItemStack itemStack, InkStorage storage) {
if (storage instanceof IndividualCappedInkStorage individualCappedInkStorage) {
NbtCompound compound = itemStack.getOrCreateNbt();
compound.put("EnergyStore", individualCappedInkStorage.toNbt());
}
itemStack.set(SpectrumDataComponentTypes.INK_STORAGE, new InkStorageComponent(storage));
}

@Override
Expand Down Expand Up @@ -85,7 +79,7 @@ public ExtendedItemBarProvider.BarSignature getSignature(@Nullable PlayerEntity

var progress = Support.getSensiblePercent(storage.getCurrentTotal(), storage.getMaxTotal(), 14);
if (colors.size() == 1) {
var color = colors.get(0);
var color = colors.getFirst();
return new ExtendedItemBarProvider.BarSignature(1, 13, 14, progress, 1, ColorHelper.colorVecToRGB(color.getColorVec()) | 0xFF000000, 2, DEFAULT_BACKGROUND_COLOR);
}

Expand Down
Loading

0 comments on commit 38a1afb

Please sign in to comment.