package net.mcreator.puzzle_code.procedures;

import net.minecraft.world.item.component.CustomData;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.InteractionHand;
import net.minecraft.core.component.DataComponents;

public class TutorialGadgetTutorialPreviousPageProcedure {
	public static void execute(Entity entity) {
		if (entity == null)
			return;
		ItemStack itemHand = ItemStack.EMPTY;
		itemHand = (entity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).copy();
		if (itemHand.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY).copyTag().getDouble("pageTutorial") > 0) {
			{
				final String _tagName = "pageTutorial";
				final double _tagValue = (itemHand.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY).copyTag().getDouble("pageTutorial") - 1);
				CustomData.update(DataComponents.CUSTOM_DATA, itemHand, tag -> tag.putDouble(_tagName, _tagValue));
			}
			if (entity instanceof LivingEntity _entity) {
				ItemStack _setstack = itemHand.copy();
				_setstack.setCount(1);
				_entity.setItemInHand(InteractionHand.MAIN_HAND, _setstack);
				if (_entity instanceof Player _player)
					_player.getInventory().setChanged();
			}
		}
	}
}
