package asm.mixins;

import dev.crucifixbeta.mod.modules.impl.render.CapeChanger;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.util.SkinTextures;
import net.minecraft.util.Identifier;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Environment(EnvType.CLIENT)
@Mixin({AbstractClientPlayerEntity.class})
public class MixinAbstractClientPlayerEntity {
   @Inject(
      method = {"getSkinTextures"},
      at = {@At("RETURN")},
      cancellable = true
   )
   private void onGetSkinTextures(CallbackInfoReturnable<SkinTextures> cir) {
      if (CapeChanger.INSTANCE != null && !CapeChanger.INSTANCE.isOff()) {
         AbstractClientPlayerEntity var2 = (AbstractClientPlayerEntity)(Object)this;
         if (CapeChanger.INSTANCE.shouldReplaceCape(var2)) {
            SkinTextures var3 = (SkinTextures)cir.getReturnValue();
            if (var3 != null) {
               Identifier var4 = CapeChanger.getCustomCape();
               cir.setReturnValue(new SkinTextures(var3.texture(), var3.textureUrl(), var4, var3.elytraTexture(), var3.model(), var3.secure()));
            }
         }
      }
   }
}
