package ru.dbotthepony.mc.otm.mixin.compat.ad_astra;

import earth.terrarium.ad_astra.common.entity.system.EntityOxygenSystem;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
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.CallbackInfo;
import ru.dbotthepony.mc.otm.capability.MatteryCapability;
import ru.dbotthepony.mc.otm.config.ServerCompatConfig;

@Mixin(EntityOxygenSystem.class)
public class EntityOxygenSystemMixin {
	@Inject(
		method = "oxygenTick(Lnet/minecraft/world/entity/LivingEntity;Lnet/minecraft/server/level/ServerLevel;)V",
		at = @At("HEAD"),
		cancellable = true,
		remap = false
	)
	private static void oxygenTick(LivingEntity entity, ServerLevel level, CallbackInfo hook) {
		if (entity instanceof Player && ServerCompatConfig.AdAstra.INSTANCE.getANDROIDS_DO_NOT_NEED_OXYGEN()) {
			entity.getCapability(MatteryCapability.MATTERY_PLAYER).ifPresent(it -> {
				if (it.isAndroid()) {
					hook.cancel();
				}
			});
		}
	}
}
