- Joined
- Mar 15, 2017
- Messages
- 96
This adds new functions and events on Items.
NOTE:
If there are any moderators reading this thread,
please delete this, I abandoned this thread.
Because only blizzard can make "Item enters region/map"
function..
Here's a demo on how to use it:
NOTE:
If there are any moderators reading this thread,
please delete this, I abandoned this thread.
Because only blizzard can make "Item enters region/map"
function..
vJASS:
library ItemEvent /* v1.1.3
****************************************************************************************************
* _____________________
* * ItemEvent *
* * By: BloodForBlood *
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* */requires/*
*
* */ optional Table /*
* - https://www.hiveworkshop.com/threads/snippet-new-table.188084/
* */ optional TimerUtils /*
* - http://www.wc3c.net/showthread.php?t=101322
*
* What is ItemEvent:
* It adds new functions and events to items.
* The two new events can be used to create
* item indexer or any else.
*
* Features:
* Lightweight
* Leakless(Maybe)
* Bugless(Maybe)
* Fixed runes death still showing small models
*
****************************************************************************************************
* Configurations: */
globals
private constant real REMOVE_DELAY = 1.00 // The delay of item removal
// on death.
/***************************************************************************************************
*
* EVENT:
* function RegisterItemEnterEvent takes code c returns nothing
* - Adds item enters the map event
* c: The function you want to run when the event get executed
*
* function RegisterItemLeavEvent takes code c returns nothing
* - Adds item leaves the map event
* c: The function you want to run when the event get executed
*
* function RegisterItemEnterEventEx takes code c returns nothing
* - Adds item enters the map event including preplaced
* c: The function you want to run when the event get executed
*
* EVENT FUNCTIONS:
* function GetEnteringItem takes nothing returns item
* - Returns the entering item in the map
*
* function GetLeavingItem takes nothing returns item
* - Returns the leaving item from the map
*
* function GetEnteringItemEx takes nothing returns item
* - Used for "RegisterItemEnterEventEx"
*
* Special Thanks to:
* Bribe - for "NewTable"
* Vexorian - for "TimerUtils"
*
***************************************************************************************************/
private item array ITEM
private trigger array TRIG
private trigger DEATH
endglobals
private struct Data
static if LIBRARY_Table then
static HashTable htb = HashTable.create
else
static hashtable ht = InitHashtable()
endif
endstruct
function GetEnteringItem takes nothing returns item
return ITEM[1]
endfunction
function GetLeavingItem takes nothing returns item
return ITEM[2]
endfunction
function GetEnteringItemEx takes nothing returns item
if (ITEM[3] == null) then
return ITEM[1]
endif
return ITEM[3]
endfunction
function RegisterItemEnterEvent takes code c returns nothing
call TriggerAddCondition(TRIG[1], Filter(c))
endfunction
function RegisterItemLeaveEvent takes code c returns nothing
call TriggerAddCondition(TRIG[2], Filter(c))
endfunction
function RegisterItemEnterEventEx takes code c returns nothing
call RegisterItemEnterEvent(c)
call TriggerAddCondition(TRIG[3], Filter(c))
endfunction
private function EnterItem takes item it returns nothing
local boolean b = false
local item i = it
local integer id = GetHandleId(i)
static if LIBRARY_Table then
set b = Data.htb[id].boolean[StringHash("IE_Entered")]
else
set b = LoadBoolean(Data.ht, id, StringHash("IE_Entered"))
endif
if (not b) then
static if LIBRARY_Table then
set Data.htb[id].boolean[StringHash("IE_Entered")] = true
set Data.htb[StringHash("ItemEvent")].item[id] = i
else
call SaveBoolean(Data.ht, id, StringHash("IE_Entered"), true)
call SaveItemHandle(Data.ht, StringHash("ItemEvent"), id, i)
endif
call TriggerRegisterDeathEvent(DEATH, i)
set ITEM[1] = i
call TriggerEvaluate(TRIG[1])
endif
set i = null
endfunction
private function onEnter takes nothing returns nothing
local item i = GetEnumItem()
call EnterItem(i)
set i = null
endfunction
private function IdentifyItem takes nothing returns nothing
local timer t = GetExpiredTimer()
local rect r
local integer i = GetHandleId(t)
static if LIBRARY_Table then
set r = Data.htb[i].rect[StringHash("IE_Rect")]
else
set r = LoadRectHandle(Data.ht, i, StringHash("IE_Rect"))
endif
call EnumItemsInRect(r, null, function onEnter)
static if LIBRARY_TimerUtils then
call ReleaseTimer(t)
else
call DestroyTimer(t)
endif
call RemoveRect(r)
set r = null
set t = null
endfunction
private function onAquire takes nothing returns boolean
local item i = GetManipulatedItem()
call EnterItem(i)
set i = null
return false
endfunction
private function LoadItem takes real x, real y returns nothing
local rect r = Rect(x-64, y-64, x+64, y+64)
local timer t
local integer i
static if LIBRARY_TimerUtils then
set t = NewTimer()
else
set t = CreateTimer()
endif
set i = GetHandleId(t)
static if LIBRARY_Table then
set Data.htb[i].rect[StringHash("IE_Rect")] = r
else
call SaveRectHandle(Data.ht, i, StringHash("IE_Rect"), r)
endif
call TimerStart(t, 0, false, function IdentifyItem)
set r = null
set t = null
endfunction
private function Widget2Item takes widget w returns item
local integer i = GetHandleId(w)
static if LIBRARY_Table then
set ITEM[2] = Data.htb[StringHash("ItemEvent")].item[i]
else
set ITEM[2] = LoadItemHandle(Data.ht, StringHash("ItemEvent"), i)
endif
return ITEM[2]
endfunction
private function removeItem takes nothing returns nothing
local timer t = GetExpiredTimer()
local item i
local integer id = GetHandleId(t)
static if LIBRARY_Table then
set i = Data.htb[id].item[StringHash("IE_DeathItem")]
else
set i = LoadItemHandle(Data.ht, id, StringHash("IE_DeathItem"))
endif
call RemoveItem(i)
set t = null
set i = null
endfunction
private function onDeath takes nothing returns boolean
local item i = Widget2Item(GetTriggerWidget())
local timer t
local integer id
static if LIBRARY_TimerUtils then
set t = NewTimer()
else
set t = CreateTimer()
endif
set id = GetHandleId(t)
static if LIBRARY_Table then
set Data.htb[id].item[StringHash("IE_DeathItem")] = i
else
call SaveItemHandle(Data.ht, id, StringHash("IE_DeathItem"), i)
endif
call TimerStart(t, REMOVE_DELAY, false, function removeItem)
call TriggerEvaluate(TRIG[2])
set i = null
set t = null
return false
endfunction
private function CreateItemHook takes integer itemid, real x, real y returns nothing
call LoadItem(x, y)
endfunction
private function CreateItemLocHook takes integer itemId, location loc returns nothing
call LoadItem(GetLocationX(loc), GetLocationY(loc))
endfunction
private function UnitAddItemByIdSwappedHook takes integer itemId, unit whichHero returns nothing
if UnitInventoryCount(whichHero) == UnitInventorySize(whichHero) then
call LoadItem(GetUnitX(whichHero), GetUnitY(whichHero))
endif
endfunction
private function UnitAddItemByIdHook takes unit whichUnit, integer itemId returns nothing
if UnitInventoryCount(whichUnit) == UnitInventorySize(whichUnit) then
call LoadItem(GetUnitX(whichUnit), GetUnitY(whichUnit))
endif
endfunction
private function UnitAddItemToSlotByIdHook takes unit whichUnit, integer itemId, integer itemSlot returns nothing
if UnitInventoryCount(whichUnit) == UnitInventorySize(whichUnit) then
call LoadItem(GetUnitX(whichUnit), GetUnitY(whichUnit))
endif
endfunction
private function UnitDropItemHook takes unit inUnit, integer inItemID returns nothing
call LoadItem(GetUnitX(inUnit), GetUnitY(inUnit))
endfunction
private function WidgetDropItemHook takes widget inWidget, integer inItemID returns nothing
call LoadItem(GetWidgetX(inWidget), GetWidgetY(inWidget))
endfunction
hook CreateItem CreateItemHook
hook CreateItemLoc CreateItemLocHook
hook UnitAddItemByIdSwapped UnitAddItemByIdSwappedHook
hook UnitAddItemById UnitAddItemByIdHook
hook UnitAddItemToSlotById UnitAddItemToSlotByIdHook
hook UnitDropItem UnitDropItemHook
hook WidgetDropItem WidgetDropItemHook
private function onInventory takes unit uu returns boolean
local integer i = 0
local unit u = uu
local item it = null
local integer id = 0
local boolean b = false
if (UnitInventorySize(u) > 0) then
loop
set it = UnitItemInSlot(u, i)
if (it != null) then
set id = GetHandleId(it)
static if LIBRARY_Table then
set b = Data.htb[id].boolean[StringHash("IE_Entered")]
else
set b = LoadBoolean(Data.ht, id, StringHash("IE_Entered"))
endif
if (not b) then
static if LIBRARY_Table then
set Data.htb[id].boolean[StringHash("IE_Entered")] = true
set Data.htb[StringHash("ItemEvent")].item[id] = it
else
call SaveBoolean(Data.ht, id, StringHash("IE_Entered"), true)
call SaveItemHandle(Data.ht, StringHash("ItemEvent"), id, it)
endif
call TriggerRegisterDeathEvent(DEATH, it)
set ITEM[3] = it
call TriggerEvaluate(TRIG[3])
set ITEM[3] = null
endif
endif
set i = i + 1
exitwhen i > UnitInventorySize(u)
endloop
endif
set u = null
set it = null
return false
endfunction
private function addPreplaced takes nothing returns nothing
local item i = GetEnumItem()
local boolean b = false
local integer id = GetHandleId(i)
static if LIBRARY_Table then
set b = Data.htb[id].boolean[StringHash("IE_Entered")]
else
set b = LoadBoolean(Data.ht, id, StringHash("IE_Entered"))
endif
if (not b) then
static if LIBRARY_Table then
set Data.htb[id].boolean[StringHash("IE_Entered")] = true
set Data.htb[StringHash("ItemEvent")].item[id] = i
else
call SaveBoolean(Data.ht, id, StringHash("IE_Entered"), true)
call SaveItemHandle(Data.ht, StringHash("ItemEvent"), id, i)
endif
call TriggerRegisterDeathEvent(DEATH, i)
set ITEM[3] = i
call TriggerEvaluate(TRIG[3])
set ITEM[3] = null
endif
set i = null
endfunction
private function InitPreplaced takes nothing returns nothing
local rect r = GetWorldBounds()
local group g = CreateGroup()
local unit u
call EnumItemsInRect(r, null, function addPreplaced)
static if LIBRARY_TimerUtils then
call ReleaseTimer(GetExpiredTimer())
else
call DestroyTimer(GetExpiredTimer())
endif
call GroupEnumUnitsInRect(g, r, null)
loop
set u = FirstOfGroup(g)
exitwhen u == null
call GroupRemoveUnit(g, u)
call onInventory(u)
endloop
call RemoveRect(r)
call DestroyGroup(g)
set r = null
set g = null
endfunction
private module m
private static method onInit takes nothing returns nothing
local trigger t = CreateTrigger()
set TRIG[1] = CreateTrigger()
set TRIG[2] = CreateTrigger()
set TRIG[3] = CreateTrigger()
set DEATH = CreateTrigger()
static if LIBRARY_TimerUtils then
call TimerStart(NewTimer(), 0, false, function InitPreplaced)
else
call TimerStart(CreateTimer(), 0, false, function InitPreplaced)
endif
call TriggerAddCondition(DEATH, Condition(function onDeath))
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_PICKUP_ITEM)
call TriggerAddCondition(t, Condition(function onAquire))
set t = null
endmethod
endmodule
private struct s
implement m
endstruct
endlibrary
Here's a demo on how to use it:
vJASS:
scope Demo initializer Init
private function Enter takes nothing returns nothing
local item i = GetEnteringItem() // Getting the entering item
call DisplayTimedTextToPlayer(Player(0), 0, 0, 3, GetItemName(i) + " entered the map")
set i = null
endfunction
private function Leave takes nothing returns nothing
local item i = GetLeavingItem() // Getting the leaving item
call DisplayTimedTextToPlayer(Player(0), 0, 0, 3, GetItemName(i) + " leaved the map.")
set i = null
endfunction
private function Init takes nothing returns nothing
call RegisterItemLeaveEvent(function Leave) // Registers leave event to the function
call RegisterItemEnterEvent(function Enter) // Registers enter event to the function
endfunction
endscope
Last edited: