package ru.dbotthepony.mc.otm.mixin;

import net.minecraft.advancements.CriterionTrigger;
import net.minecraft.advancements.CriterionTriggerInstance;
import net.minecraft.advancements.critereon.InventoryChangeTrigger;
import net.minecraft.advancements.critereon.SimpleCriterionTrigger;
import net.minecraft.server.PlayerAdvancements;
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.triggers.MatteryInventoryChangeTrigger;

// i tried to mixin into InventoryChangeTrigger with extends SimpleCriterionTrigger and @Overwrite+@Override
// while also defining SimpleCriterionTrigger methods non final in accesstransfoer
// it didn't work.
@Mixin(SimpleCriterionTrigger.class)
public abstract class SimpleCriterionTriggerMixin implements CriterionTrigger<CriterionTriggerInstance> {
	@Inject(
		method = "removePlayerListener(Lnet/minecraft/server/PlayerAdvancements;Lnet/minecraft/advancements/CriterionTrigger$Listener;)V",
		at = @At("HEAD"),
		cancellable = true
	)
	public void removePlayerListener(PlayerAdvancements p_66254_, CriterionTrigger.Listener p_66255_, CallbackInfo info) {
		if (((Object) this) instanceof InventoryChangeTrigger) {
			MatteryInventoryChangeTrigger.INSTANCE.removePlayerListener(p_66254_, p_66255_);
			info.cancel();
		}
	}

	@Inject(
		method = "addPlayerListener(Lnet/minecraft/server/PlayerAdvancements;Lnet/minecraft/advancements/CriterionTrigger$Listener;)V",
		at = @At("HEAD"),
		cancellable = true
	)
	public void addPlayerListener(PlayerAdvancements p_66254_, CriterionTrigger.Listener p_66255_, CallbackInfo info) {
		if (((Object) this) instanceof InventoryChangeTrigger) {
			MatteryInventoryChangeTrigger.INSTANCE.addPlayerListener(p_66254_, p_66255_);
			info.cancel();
		}
	}

	@Inject(
		method = "removePlayerListeners(Lnet/minecraft/server/PlayerAdvancements;)V",
		at = @At("HEAD"),
		cancellable = true
	)
	public void removePlayerListeners(PlayerAdvancements p_66254_, CallbackInfo info) {
		if (((Object) this) instanceof InventoryChangeTrigger) {
			MatteryInventoryChangeTrigger.INSTANCE.removePlayerListeners(p_66254_);
			info.cancel();
		}
	}
}
