package com.r3944realms.leashedplayer.mixin.both;

import com.r3944realms.leashedplayer.modInterface.PlayerLeashable;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.Leashable;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import javax.annotation.Nullable;

@Mixin(Player.class)
public abstract class MixinPlayer extends LivingEntity implements PlayerLeashable {

    @Unique
    @Nullable
    private Leashable.LeashData Pl$LeashData;//Data

    protected MixinPlayer(EntityType<? extends LivingEntity> pEntityType, Level pLevel) {
        super(pEntityType, pLevel);
    }



    @org.jetbrains.annotations.Nullable
    @Override
    public LeashData getLeashData() {
        return Pl$LeashData;
    }
    @Override
    public void setLeashData(@org.jetbrains.annotations.Nullable Leashable.LeashData pLeashData) {
        this.Pl$LeashData = pLeashData;
    }

    @Inject(
            method = {"addAdditionalSaveData"}, at = {@At("RETURN")}
    )//数据保存
    private void addSaveData(ValueOutput valueOutput, CallbackInfo ci) {
        writeLeashData(valueOutput, Pl$LeashData);
    }
    @Inject(
            method = {"readAdditionalSaveData"}, at = {@At("RETURN")}
    )//数据读取
    private void readSaveData(ValueInput valueInput, CallbackInfo ci) {
        this.readLeashData(valueInput);

    }
}
