package net.knarcraft.blacksmith.listener;

import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

/**
 * A listener for detecting and handling a Blacksmith being right-clicked
 */
public class NPCClickListener implements Listener {

    @EventHandler
    public void onRightClick(net.citizensnpcs.api.event.NPCRightClickEvent event) {
        //We only care about blacksmiths
        if (!event.getNPC().hasTrait(BlacksmithTrait.class)) {
            return;
        }
        BlacksmithTrait blacksmithTrait = event.getNPC().getTrait(BlacksmithTrait.class);

        //Perform any necessary pre-session work
        Player player = event.getClicker();
        if (!blacksmithTrait.prepareForSession(player)) {
            return;
        }

        if (blacksmithTrait.hasSession()) {
            //Continue the existing session
            blacksmithTrait.continueSession(player);
        } else {
            //Start a new session
            blacksmithTrait.startSession(player);
        }
    }

}
