package net.mcreator.gts.procedures;

import net.neoforged.neoforge.event.entity.EntityMountEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.bus.api.ICancellableEvent;
import net.neoforged.bus.api.Event;

import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.entity.Entity;
import net.minecraft.tags.TagKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.registries.Registries;

import javax.annotation.Nullable;

@EventBusSubscriber
public class TokiDismountActionProcedure {
	@SubscribeEvent
	public static void onEventTriggered(EntityMountEvent event) {
		execute(event, event.getLevel(), event.getEntityBeingMounted(), event.getEntityMounting(), event.isDismounting());
	}

	public static void execute(LevelAccessor world, Entity entity, Entity sourceentity, boolean DISMOUNTING) {
		execute(null, world, entity, sourceentity, DISMOUNTING);
	}

	private static void execute(@Nullable Event event, LevelAccessor world, Entity entity, Entity sourceentity, boolean DISMOUNTING) {
		if (entity == null || sourceentity == null)
			return;
		if (!world.isClientSide() && DISMOUNTING) {
			if (entity.getType().is(TagKey.create(Registries.ENTITY_TYPE, ResourceLocation.parse("gts:untamedgiantess")))) {
				if (!entity.getPersistentData().getBoolean("ThrowingTarget") && sourceentity.isAlive() && entity.isAlive()) {
					if (event instanceof ICancellableEvent _cancellable) {
						_cancellable.setCanceled(true);
					}
				} else {
					entity.getPersistentData().putBoolean("ThrowingTarget", false);
					sourceentity.getPersistentData().putBoolean("BeingGrabbed", false);
				}
			}
		}
	}
}
