package net.Chipperfluff.chipi.mixin;

import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(DataTracker.class)
public abstract class DataTrackerMixin {

    @Shadow
    protected abstract <T> DataTracker.Entry<T> getEntry(TrackedData<T> data);

    @Inject(method = "get", at = @At("HEAD"), cancellable = true)
    private <T> void chipi$nullSafeGet(TrackedData<T> data, CallbackInfoReturnable<T> cir) {
        DataTracker.Entry<T> entry = this.getEntry(data);

        if (entry == null) {
            cir.setReturnValue(null);
        }
    }
}
