- Joined
- Oct 12, 2011
- Messages
- 3,449
[Snippet] ItemSwapEvent
Description
DEMO
Description
A simple library which provides 3 trigger events which fires when a unit is ordered to move an item to a different or same inventory slot and when a unit is ordered to swap between items.
Requirements- JNGP
Code
JASS:
library ItemSwapEvent initializer ini /* v1.7
Description
This library provides 3 trigger events which fires when a unit is ordered to
move an item to a different or same inventory slot and when a unit is ordered
to swap between items.
APIs
1. Returns an item current slot index inside a units inventory
function GetItemSlotIndex takes unit whichUnit, item whichItem returns integer
2. To execute a function when inventory event fired
function RegisterAnyUnitInventoryEvent takes boolexpr func, integer whichEvent returns triggercondition
3. To prevent a function from being executed on inventory event
function RemoveAnyUnitInventoryEvent takes triggercondition tc, integer whichEvent returns nothing
Trigger Events
1. Fires when a unit is ordered to move an item to an empty slot
EVENT_UNIT_ITEM_MOVE
2. Fires when a unit is ordered to swap between items
EVENT_UNIT_ITEM_SWAP
3. Fires when a unit is ordered to move an item to a same slot
EVENT_UNIT_ITEM_CANCEL
Event Responses
1. Get swapping/moving unit
function GetEventSwapUnit takes nothing returns unit
2. Get targeted item at the event
function GetEventSwapItemTarget takes nothing returns item
3. Get source item at the event
function GetEventSwapItemSource takes nothing returns item
*/
// Edit them by your own risk
globals
private integer Total = -1
private integer array Slot
private item array Target
private item EventTarget = null
private item EventSource = null
private unit EventUnit = null
private unit array Unit
private trigger array Trigger
private timer Timer = CreateTimer()
constant integer EVENT_UNIT_ITEM_SWAP = 0
constant integer EVENT_UNIT_ITEM_MOVE = 1
constant integer EVENT_UNIT_ITEM_CANCEL = 2
endglobals
function GetItemSlotIndex takes unit whichUnit, item whichItem returns integer
local integer i = 0
local integer ext = UnitInventorySize(whichUnit)
loop
exitwhen i == ext
if UnitItemInSlot(whichUnit, i) == whichItem then
return i
endif
set i = i + 1
endloop
return -1
endfunction
function GetEventSwapUnit takes nothing returns unit
return EventUnit
endfunction
function GetEventSwapItemTarget takes nothing returns item
return EventTarget
endfunction
function GetEventSwapItemSource takes nothing returns item
return EventSource
endfunction
function RegisterAnyUnitInventoryEvent takes boolexpr func, integer whichEvent returns triggercondition
return TriggerAddCondition(Trigger[whichEvent], func)
endfunction
function RemoveAnyUnitInventoryEvent takes triggercondition tc, integer whichEvent returns nothing
call TriggerRemoveCondition(Trigger[whichEvent], tc)
endfunction
private function swapEvent takes nothing returns nothing
local unit bu
local item bt
local item bs
loop
exitwhen Total < 0
set bu = EventUnit
set bt = EventTarget
set bs = EventSource
set EventUnit = Unit[Total]
if Slot[Total] != GetItemSlotIndex(Unit[Total], Target[Total]) then
set EventTarget = UnitItemInSlot(Unit[Total], Slot[Total])
set EventSource = UnitItemInSlot(Unit[Total], GetItemSlotIndex(Unit[Total], Target[Total]))
if GetItemTypeId(EventTarget) == 0 then
call TriggerEvaluate(Trigger[EVENT_UNIT_ITEM_MOVE])
else
call TriggerEvaluate(Trigger[EVENT_UNIT_ITEM_SWAP])
endif
else
set EventTarget = UnitItemInSlot(Unit[Total], GetItemSlotIndex(Unit[Total], Target[Total]))
set EventSource = EventTarget
call TriggerEvaluate(Trigger[EVENT_UNIT_ITEM_CANCEL])
endif
set EventUnit = bu
set EventTarget = bt
set EventSource = bs
set Total = Total - 1
endloop
set bu = null
set bt = null
set bs = null
endfunction
private function onOrder takes nothing returns boolean
if IsItemOwned(GetOrderTargetItem()) then
set Total = Total + 1
set Target[Total] = GetOrderTargetItem()
set Unit[Total] = GetTriggerUnit()
set Slot[Total] = GetItemSlotIndex(Unit[Total], Target[Total])
call TimerStart(Timer, 0, false, function swapEvent)
endif
return false
endfunction
private function ini takes nothing returns nothing
local trigger t = CreateTrigger()
set Trigger[EVENT_UNIT_ITEM_MOVE] = CreateTrigger()
set Trigger[EVENT_UNIT_ITEM_CANCEL] = CreateTrigger()
set Trigger[EVENT_UNIT_ITEM_SWAP] = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER)
call TriggerAddCondition(t, Condition(function onOrder))
endfunction
endlibrary
DEMO
JASS:
// Display text when a unit is swapping items
function a takes nothing returns boolean
call BJDebugMsg(GetUnitName(GetEventSwapUnit()) + " swapped " + GetItemName(GetEventSwapItemTarget()) + " with " + GetItemName(GetEventSwapItemSource()))
return false
endfunction
// Display text when a unit is moving items
function b takes nothing returns boolean
call BJDebugMsg(GetUnitName(GetEventSwapUnit()) + " moved " + GetItemName(GetEventSwapItemSource()) + " to slot-" + I2S(GetItemSlotIndex(GetEventSwapUnit(), GetEventSwapItemSource()) + 1))
return false
endfunction
// Display text when a unit cancelled to move/swap items
function c takes nothing returns boolean
call BJDebugMsg(GetUnitName(GetEventSwapUnit()) + " canceled to move " + GetItemName(GetEventSwapItemSource()))
return false
endfunction
function InitTrig_Demo takes nothing returns nothing
call RegisterAnyUnitInventoryEvent(Filter(function a), EVENT_UNIT_ITEM_SWAP)
call RegisterAnyUnitInventoryEvent(Filter(function b), EVENT_UNIT_ITEM_MOVE)
call RegisterAnyUnitInventoryEvent(Filter(function c), EVENT_UNIT_ITEM_CANCEL)
endfunction
Attachments
Last edited: