summary refs log blame commit diff stats
path: root/src/main/java/ganarchy/chewstuff/ChewComponent.java
blob: aefa5397c3fa59a96e2146fe02b7b333f8e370f7 (plain) (tree)
1
2
3
4
5
6
7
8
9







                                                          
                                         












                                                                               
                                                              

                                                           
                                                   


































































                                                                              
package ganarchy.chewstuff;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.onyxstudios.cca.api.v3.component.ComponentV3;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtOps;
import net.minecraft.registry.Registries;

import java.util.*;

/**
 * Component for chews.
 */
public class ChewComponent implements ComponentV3 {
    /**
     * Codec for encoding/decoding this component.
     */
    public static final Codec<ChewComponent> CODEC = RecordCodecBuilder.create(
        inst -> inst.group(
            Codec.unboundedMap(
                Registries.STATUS_EFFECT.getCodec(), Codec.INT
            ).fieldOf("effects").forGetter(i -> i.effects),
            Codec.list(
                Registries.STATUS_EFFECT.getCodec()
            ).fieldOf("applied").forGetter(i -> new ArrayList<>(i.applied)),
            Codec.BOOL.fieldOf("sendUpdate").forGetter(i -> i.sendUpdate)
        ).apply(inst, ChewComponent::new)
    );
    /**
     * The effects we're currently consuming.
     */
    public Map<StatusEffect, Integer> effects;
    /**
     * The effects which have been applied and should be skipped.
     */
    public Set<StatusEffect> applied;
    /**
     * Whether to send updated effects to the client.
     */
    public boolean sendUpdate;

    /**
     * Creates a new ChewInfo with no effects.
     */
    public ChewComponent() {
        this.effects = new HashMap<>();
        this.applied = new HashSet<>();
    }

    /**
     * Creates a new ChewInfo with the specified effects.
     *
     * @param effects The bound effects.
     * @param applied The applied effects.
     */
    public ChewComponent(
        Map<StatusEffect, Integer> effects,
        List<StatusEffect> applied,
        boolean sendUpdate
    ) {
        if (effects instanceof HashMap) {
            this.effects = effects;
        } else {
            // ideally we'd just make the HashMap directly but DFU doesn't let
            // you set the Map class.
            // actually ideally we'd use IdentityHashMap but w/e.
            this.effects = new HashMap<>(effects);
        }
        this.applied = new HashSet<>(applied);
        this.sendUpdate = sendUpdate;
    }

    @Override
    public void readFromNbt(NbtCompound tag) {
        var info = ChewComponent.CODEC.parse(
            NbtOps.INSTANCE,
            tag
        ).result().orElseGet(ChewComponent::new);

        this.effects = info.effects;
        this.applied = info.applied;
    }

    @Override
    public void writeToNbt(NbtCompound tag) {
        tag.copyFrom((NbtCompound) ChewComponent.CODEC.encodeStart(
            NbtOps.INSTANCE,
            this
        ).result().orElseThrow());
    }
}