Skip to content

Commit

Permalink
Fix SaveKey codec accessing domain properly
Browse files Browse the repository at this point in the history
Signed-off-by: HellFirePvP <[email protected]>
  • Loading branch information
HellFirePvP committed Dec 12, 2024
1 parent 484d8eb commit be5b4e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,12 @@ void clear() {

public static class SaveKey<T extends IWorldRelatedData<T>> {

public static final Codec<SaveKey<?>> CODEC = Codec.pair(ResourceLocation.CODEC, Codec.STRING).flatXmap(pair -> {
WorldCacheDomain domain = WorldCacheManager.findDomain(pair.getFirst());
if (domain == null) return DataResult.error(() -> "Unknown domain: " + pair.getFirst());
SaveKey<?> key = domain.getKey(pair.getSecond());
if (key == null) return DataResult.error(() -> "Unknown saveKey: " + pair.getSecond());
return DataResult.success(key);
}, key -> DataResult.success(new Pair<>(key.getDomainName(), key.getIdentifier())));
public static final Codec<SaveKey<?>> CODEC = RecordCodecBuilder.create(inst -> inst.group(
ResourceLocation.CODEC.fieldOf("domain").forGetter(SaveKey::getDomainName),
Codec.STRING.fieldOf("identifier").forGetter(SaveKey::getIdentifier)
).apply(inst, (domainKey, id) -> WorldCacheManager.findDomain(domainKey)
.map(domain -> domain.getKey(id))
.orElseThrow(() -> new IllegalArgumentException("Unknown domain key: " + domainKey + " / " + id))));

private final ResourceLocation domainName;
private final String identifier;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package hellfirepvp.observerlib.common.data;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.Level;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
* This class is part of the ObserverLib Mod
Expand Down Expand Up @@ -55,19 +58,17 @@ public static WorldCacheDomain createDomain(String modid) {
return domain;
}

@Nullable
public static WorldCacheDomain findDomain(String modid) {
public static Optional<WorldCacheDomain> findDomain(String modid) {
return findDomain(ResourceLocation.fromNamespaceAndPath(modid, DEFAULT_DOMAIN_NAME));
}

@Nullable
public static WorldCacheDomain findDomain(ResourceLocation domainKey) {
public static Optional<WorldCacheDomain> findDomain(ResourceLocation domainKey) {
for (ResourceLocation key : domains.keySet()) {
if (key.equals(domainKey)) {
return domains.get(key);
return Optional.of(domains.get(key));
}
}
return null;
return Optional.empty();
}

public void doSave(Level world) {
Expand Down

0 comments on commit be5b4e6

Please sign in to comment.