aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/isxander/yacl3/config/v3/ChildConfigEntryImpl.java
blob: 5b608de2b62db265e51f09c62e4ec1a73ed6d6f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package dev.isxander.yacl3.config.v3;

import com.mojang.serialization.DataResult;
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.RecordBuilder;
import dev.isxander.yacl3.impl.utils.YACLConstants;
import org.jetbrains.annotations.ApiStatus;

import java.util.Optional;

@ApiStatus.Experimental
public class ChildConfigEntryImpl<T extends CodecConfig<T>> extends AbstractReadonlyConfigEntry<T> {
    private final T config;
    private final MapCodec<T> mapCodec;

    public ChildConfigEntryImpl(String fieldName, T config) {
        super(fieldName);
        this.config = config;
        this.mapCodec = config.fieldOf(this.fieldName());
    }

    @Override
    protected T innerGet() {
        return config;
    }

    @Override
    public <R> RecordBuilder<R> encode(DynamicOps<R> ops, RecordBuilder<R> recordBuilder) {
        return mapCodec.encode(config, ops, recordBuilder);
    }

    @Override
    public <R> boolean decode(R encoded, DynamicOps<R> ops) {
        DataResult<T> result = mapCodec.decoder().parse(ops, encoded);

        //? if >1.20.4 {
        Optional<DataResult.Error<T>> error = result.error();
         //?} else {
        /*Optional<DataResult.PartialResult<T>> error = result.error();
        *///?}
        if (error.isPresent()) {
            YACLConstants.LOGGER.error("Failed to decode entry {}: {}", this.fieldName(), error.get().message());
            return false;
        }

        return true;
    }
}