Skip to content

Commit

Permalink
Automatically migrate pre-InkColor registry InkStorages (#619)
Browse files Browse the repository at this point in the history
Signed-off-by: unilock <unilock@fennet.rentals>
  • Loading branch information
unilock authored Dec 2, 2024
1 parent c453b36 commit 31fe916
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/main/java/de/dafuqs/spectrum/api/energy/InkStorage.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.dafuqs.spectrum.api.energy;

import de.dafuqs.spectrum.api.energy.color.*;
import de.dafuqs.spectrum.registries.*;
import net.minecraft.nbt.*;
import net.minecraft.text.*;
import net.minecraft.util.*;
Expand Down Expand Up @@ -165,9 +164,11 @@ default float getTotalFillPercent() {
Map<InkColor, Long> energy = new HashMap<>();
if (compound != null) {
for (String key : compound.getKeys()) {
InkColor inkColor = SpectrumRegistries.INK_COLORS.get(new Identifier(key));
long amount = compound.getLong(key);
energy.put(inkColor, amount);
Optional<InkColor> color = InkColor.ofIdString(key);
if (color.isPresent()) {
long amount = compound.getLong(key);
energy.put(color.get(), amount);
}
}
}
return energy;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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.tag.*;
Expand Down Expand Up @@ -46,7 +47,12 @@ public static Optional<InkColor> ofId(Identifier id) {
}

public static Optional<InkColor> ofIdString(String idString) {
return SpectrumRegistries.INK_COLORS.getOrEmpty(new Identifier(idString));
try {
Identifier id = new Identifier(idString);
return SpectrumRegistries.INK_COLORS.getOrEmpty(id).or(() -> SpectrumRegistries.INK_COLORS.getOrEmpty(SpectrumCommon.locate(idString)));
} catch (InvalidIdentifierException ignored) {
return Optional.empty();
}
}

public DyeColor getDyeColor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public boolean isFull() {

public static IndividualCappedInkStorage fromNbt(@NotNull NbtCompound compound) {
long maxEnergyPerColor = compound.getLong("MaxEnergyPerColor");
Map<InkColor, Long> colors = InkStorage.readEnergy(compound.getCompound("Energy"));
Map<InkColor, Long> colors = InkStorage.readEnergy(compound.contains("Energy") ? compound.getCompound("Energy") : compound);
return new IndividualCappedInkStorage(maxEnergyPerColor, colors);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public NbtCompound toNbt() {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public NbtCompound toNbt() {

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

Expand Down

0 comments on commit 31fe916

Please sign in to comment.