Skip to content

Commit 31fe916

Browse files
authored
Automatically migrate pre-InkColor registry InkStorages (#619)
Signed-off-by: unilock <[email protected]>
1 parent c453b36 commit 31fe916

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

Diff for: src/main/java/de/dafuqs/spectrum/api/energy/InkStorage.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package de.dafuqs.spectrum.api.energy;
22

33
import de.dafuqs.spectrum.api.energy.color.*;
4-
import de.dafuqs.spectrum.registries.*;
54
import net.minecraft.nbt.*;
65
import net.minecraft.text.*;
76
import net.minecraft.util.*;
@@ -165,9 +164,11 @@ default float getTotalFillPercent() {
165164
Map<InkColor, Long> energy = new HashMap<>();
166165
if (compound != null) {
167166
for (String key : compound.getKeys()) {
168-
InkColor inkColor = SpectrumRegistries.INK_COLORS.get(new Identifier(key));
169-
long amount = compound.getLong(key);
170-
energy.put(inkColor, amount);
167+
Optional<InkColor> color = InkColor.ofIdString(key);
168+
if (color.isPresent()) {
169+
long amount = compound.getLong(key);
170+
energy.put(color.get(), amount);
171+
}
171172
}
172173
}
173174
return energy;

Diff for: src/main/java/de/dafuqs/spectrum/api/energy/color/InkColor.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.dafuqs.spectrum.api.energy.color;
22

3+
import de.dafuqs.spectrum.*;
34
import de.dafuqs.spectrum.helpers.*;
45
import de.dafuqs.spectrum.registries.*;
56
import net.minecraft.registry.tag.*;
@@ -46,7 +47,12 @@ public static Optional<InkColor> ofId(Identifier id) {
4647
}
4748

4849
public static Optional<InkColor> ofIdString(String idString) {
49-
return SpectrumRegistries.INK_COLORS.getOrEmpty(new Identifier(idString));
50+
try {
51+
Identifier id = new Identifier(idString);
52+
return SpectrumRegistries.INK_COLORS.getOrEmpty(id).or(() -> SpectrumRegistries.INK_COLORS.getOrEmpty(SpectrumCommon.locate(idString)));
53+
} catch (InvalidIdentifierException ignored) {
54+
return Optional.empty();
55+
}
5056
}
5157

5258
public DyeColor getDyeColor() {

Diff for: src/main/java/de/dafuqs/spectrum/api/energy/storage/IndividualCappedInkStorage.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public boolean isFull() {
130130

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

Diff for: src/main/java/de/dafuqs/spectrum/api/energy/storage/TotalCappedElementalMixingInkStorage.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public NbtCompound toNbt() {
132132

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

Diff for: src/main/java/de/dafuqs/spectrum/api/energy/storage/TotalCappedInkStorage.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public NbtCompound toNbt() {
116116

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

0 commit comments

Comments
 (0)