Scriptname PlayerBookShelfTriggerSCRIPT extends ObjectReference  
{This script has been overhauled to fix several issues listed in USLEEP 3.0.1 - Bug #19515. This overhaul was contributed by Adria.}

import debug
import utility

ObjectReference Property ShelfContainer auto hidden

Bool Property AlreadyLoaded = FALSE Auto Hidden


EVENT OnCellLoad()
	if AlreadyLoaded == FALSE
		ShelfContainer = GetLinkedRef()
		AlreadyLoaded = TRUE
	endif
endEVENT


auto STATE WaitForBooks

	EVENT OnBeginState()
		;Trace(self + " BOOKTRIGGER - Waiting For Books!")
	endEVENT

	EVENT OnTriggerEnter(ObjectReference TriggerRef)
		;Trace("DARYL - " + self + " Reference " + TriggerRef + " has ENTERED")

	endEvent

	EVENT OnTriggerLeave(ObjectReference TriggerRef)
		; Reference has left this trigger
		if GetParentCell().IsAttached()
			;Trace("DARYL - " + self + " Reference " + TriggerRef + " has EXITED")
			if (TriggerRef == Game.GetPlayerGrabbedRef())
				; Check to see if the player is holding it
				;Trace("DARYL - " + self + " Player has a hold of this reference, do nothing")
			else
				; Player isnt' grabbing this reference, check to see if it's in the container.
				;Trace("BookShelfFix Trigger - Trying to pick up " + TriggerRef)
				;if Shelfcontainer.GetItemCount(TriggerRef.GetBaseObject()) >= 1
					;USLEEP 3.0.1 - Bug #19515
					(Shelfcontainer as PlayerBookShelfContainerScript).PickUp(TriggerRef)
					;(Shelfcontainer as PlayerBookShelfContainerScript).CurrentBookAmount = (Shelfcontainer as PlayerBookShelfContainerScript).CurrentBookAmount - 1
					;(ShelfContainer as PlayerBookShelfContainerScript).RemoveBooks((TriggerRef.GetBaseObject()), 1)
				;else
				; There is no book based on this form in the container
					;Trace("DARYL - " + self + " Container doesn't contain this book")
				;endif
			endif
		endif
	endEvent


endSTATE


STATE IgnoreBooks
	Event OnBeginState()
		;Trace(self + " BOOKTRIGGER - Ignoring Books!")
	endEVENT
endSTATE


int Property testint  Auto