Skip to content

Commit

Permalink
Replace custom codec by Codec.dispatchedMap
Browse files Browse the repository at this point in the history
  • Loading branch information
malte0811 committed Nov 2, 2024
1 parent 3eaf6c7 commit 0144b87
Showing 1 changed file with 10 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.NbtOps;
Expand Down Expand Up @@ -59,43 +57,16 @@ public static <V> Codec<List<V>> directDispatchMap(
Function<String, Codec<V>> valueCodec, Function<V, String> getKey
)
{
return new Codec<>()
{
@Override
public <T> DataResult<Pair<List<V>, T>> decode(DynamicOps<T> ops, T input)
{
return ops.getMapValues(input).flatMap(s -> {
DataResult<List<V>> result = DataResult.success(new ArrayList<>());
for(var entry : s.toList())
result = result.flatMap(
current -> ops.getStringValue(entry.getFirst()).flatMap(
key -> valueCodec.apply(key).decode(ops, entry.getSecond()).map(
value -> {
var newMap = new ArrayList<>(current);
newMap.add(value.getFirst());
return newMap;
}
)
)
);
return result.map(m -> new Pair<>(m, ops.empty()));
});
}

@Override
public <T> DataResult<T> encode(List<V> input, DynamicOps<T> ops, T prefix)
{
DataResult<T> result = DataResult.success(prefix);
for(var entry : input)
result = result.flatMap(oldT -> {
var type = getKey.apply(entry);
var maybeValue = valueCodec.apply(type).encodeStart(ops, entry);
var key = ops.createString(type);
return maybeValue.flatMap(valueT -> ops.mergeToMap(oldT, key, valueT));
});
return result;
}
};
return Codec.dispatchedMap(Codec.STRING, valueCodec::apply)
.xmap(
m -> List.copyOf(m.values()),
s -> {
Map<String, V> map = new HashMap<>();
for(var v : s)
map.put(getKey.apply(v), v);
return map;
}
);
}

public static <C> Codec<Set<C>> setOf(Codec<C> codec)
Expand Down

0 comments on commit 0144b87

Please sign in to comment.