- Joined
- Mar 15, 2017
- Messages
- 96
This is a simple ItemIndexer.
Demo:
vJASS:
library ItemIndex /* v1.2.1
****************************************************************************************************
* _____________________
* * ItemIndex *
* * By: BloodForBlood *
* ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
* */requires/*
*
* */ optional Table /*
* - https://www.hiveworkshop.com/threads/snippet-new-table.188084/
* */ optional ExtensionMethods /*
* - https://www.hiveworkshop.com/pastebin/bacb4501906dae6e0545fa5da8b57a4615770/
*
* What is ItemIndex:
* It assigns 1-8191 index to items.An item get indexed if
* you retrieved its index.In other words, it's not automatic.
*
* Features:
* 1.Auto Item DeIndexing
* 2.Index Recycling
*
****************************************************************************************************
* Configurations: */
globals
private constant boolean USE_USER_DATA = true // Enables retrieving item's index
// through "GetItemUserData"
/***************************************************************************************************
*
* API:
* function GetItemIndex takes item i returns integer
* - Returns the index of the item
* i: The item you want to get the index
*
* function GetIndexItem takes integer i returns item
* - Returns the item by using the index
* i: The item index
*
* function LockItem takes item i returns nothing
* - Locks an item from being indexed or deindexed
* i: The item you want to lock
*
* function UnlockItem takes item i, boolean autoAction returns nothing
* - Unlocks an item, the item can now be indexed or deindexed
* i: The item you want to unlock
* autoAction: If set to true, the item will be automatically indexed,
* or deindexed if the item doesn't exist
*
* Special Thanks to:
* Bribe - for "New Table"
*
***************************************************************************************************/
private integer Index = 0
private integer array RecycleIndex
private integer RecycleArray = 0
private item array Item
private trigger DEATH_TRIG
endglobals
private struct Data
static if LIBRARY_Table then
static HashTable htb = HashTable.create
else
static hashtable ht = InitHashtable()
endif
endstruct
function GetIndexItem takes integer i returns item
return Item[i]
endfunction
private function Widget2Item takes widget w returns item
static if LIBRARY_Table then
return Data.htb[GetHandleId(w)].item[StringHash("ItemIndex_Widget2Item")]
else
return LoadItemHandle(Data.ht, GetHandleId(w), StringHash("ItemIndex_Widget2Item"))
endif
return null
endfunction
private function IndexItem takes item it returns nothing
local item i = it
local boolean b = false
local integer id = GetHandleId(i)
static if LIBRARY_Table then
set b = Data.htb[id].boolean[StringHash("ItemIndex_Indexed")]
else
set b = LoadBoolean(Data.ht, id, StringHash("ItemIndex_Indexed"))
endif
if (i != null) then
if (not b) then
if (RecycleArray < 1) then
set Index = Index + 1
static if LIBRARY_Table then
set Data.htb[id][StringHash("ItemIndex_Index")] = Index
set Data.htb[id].boolean[StringHash("ItemIndex_Indexed")] = true
set Data.htb[id].item[StringHash("ItemIndex_Widget2Item")] = i
call TriggerRegisterDeathEvent(DEATH_TRIG, i)
else
call SaveInteger(Data.ht, id, StringHash("ItemIndex_Index"), Index)
call SaveBoolean(Data.ht, id, StringHash("ItemIndex_Indexed"), true)
call SaveItemHandle(Data.ht, id, StringHash("ItemIndex_Widget2Item"), i)
call TriggerRegisterDeathEvent(DEATH_TRIG, i)
endif
set Item[Index] = i
else
static if LIBRARY_Table then
set Data.htb[id][StringHash("ItemIndex_Index")] = RecycleIndex[RecycleArray]
set Data.htb[id].boolean[StringHash("ItemIndex_Indexed")] = true
set Data.htb[id].item[StringHash("ItemIndex_Widget2Item")] = i
call TriggerRegisterDeathEvent(DEATH_TRIG, i)
else
call SaveInteger(Data.ht, id, StringHash("ItemIndex_Index"), RecycleIndex[RecycleArray])
call SaveBoolean(Data.ht, id, StringHash("ItemIndex_Indexed"), true)
call SaveItemHandle(Data.ht, id, StringHash("ItemIndex_Widget2Item"), i)
call TriggerRegisterDeathEvent(DEATH_TRIG, i)
endif
set Item[Index] = i
set RecycleArray = RecycleArray - 1
endif
endif
endif
set i = null
endfunction
function GetItemIndex takes item it returns integer
local integer i = 0
local integer id = GetHandleId(it)
local boolean b
static if LIBRARY_Table then
set b = Data.htb[id].boolean[StringHash("ItemIndex_Indexed")]
if (not b) then
static if LIBRARY_Table then
if not Data.htb[GetHandleId(it)].boolean[StringHash("ItemIndex_Locked")] then
call IndexItem(it)
endif
else
if not LoadBoolean(Data.ht, GetHandleId(it), StringHash("ItemIndex_Locked")) then
call IndexItem(it)
endif
endif
endif
set i = Data.htb[id][StringHash("ItemIndex_Index")]
else
set b = LoadBoolean(Data.ht, id, StringHash("ItemIndex_Indexed"))
if (not b) then
static if LIBRARY_Table then
if not Data.htb[GetHandleId(it)].boolean[StringHash("ItemIndex_Locked")] then
call IndexItem(it)
endif
else
if not LoadBoolean(Data.ht, GetHandleId(it), StringHash("ItemIndex_Locked")) then
call IndexItem(it)
endif
endif
endif
set i = LoadInteger(Data.ht, id, StringHash("ItemIndex_Index"))
endif
return i
endfunction
private function DeIndexItem takes item it returns nothing
local item i = it
local integer dex = 0
local boolean b = false
local integer id = GetHandleId(i)
static if LIBRARY_Table then
set dex = Data.htb[id][StringHash("ItemIndex_Index")]
else
set dex = LoadInteger(Data.ht, id, StringHash("ItemIndex_Index"))
endif
static if LIBRARY_Table then
set b = Data.htb[id].boolean[StringHash("ItemIndex_Indexed")]
else
set b = LoadBoolean(Data.ht, id, StringHash("ItemIndex_Indexed"))
endif
if (i != null) then
if (b and GetItemIndex(i) > 0) then
set RecycleArray = RecycleArray + 1
set RecycleIndex[RecycleArray] = dex
static if LIBRARY_Table then
set Data.htb[id][StringHash("ItemIndex_Index")] = 0
set Data.htb[id].boolean[StringHash("ItemIndex_Indexed")] = false
set Data.htb[id].boolean[StringHash("ItemIndex_Locked")] = true
else
call SaveInteger(Data.ht, id, StringHash("ItemIndex_Index"), 0)
call SaveBoolean(Data.ht, id, StringHash("ItemIndex_Indexed"), false)
call SaveBoolean(Data.ht, id, StringHash("ItemIndex_Locked"), true)
endif
endif
endif
set i = null
endfunction
function LockItem takes item i returns nothing
static if LIBRARY_Table then
set Data.htb[GetHandleId(i)].boolean[StringHash("ItemIndex_Locked")] = true
else
call SaveBoolean(Data.ht, GetHandleId(i), StringHash("ItemIndex_Locked"), true)
endif
endfunction
function UnlockItem takes item i, boolean autoAction returns nothing
static if LIBRARY_Table then
set Data.htb[GetHandleId(i)].boolean[StringHash("ItemIndex_Locked")] = false
else
call SaveBoolean(Data.ht, GetHandleId(i), StringHash("ItemIndex_Locked"), false)
endif
if (autoAction) then
static if LIBRARY_ExtensionMethods then
if (not IsItemAlive(i)) then
call DeIndexItem(i)
else
call IndexItem(i)
endif
else
if (GetItemTypeId(i) == 0 and GetWidgetLife(i) <= 0.405) then
call DeIndexItem(i)
else
call IndexItem(i)
endif
endif
endif
endfunction
private function Leave takes nothing returns boolean
local item i = Widget2Item(GetTriggerWidget())
static if LIBRARY_Table then
if not Data.htb[GetHandleId(i)].boolean[StringHash("ItemIndex_Locked")] then
call DeIndexItem(i)
endif
else
if not LoadBoolean(Data.ht, GetHandleId(i), StringHash("ItemIndex_Locked")) then
call DeIndexItem(i)
endif
endif
set i = null
return false
endfunction
private function GetItemUserDataHook takes item i returns nothing
if USE_USER_DATA then
call SetItemUserData(i, GetItemIndex(i))
endif
endfunction
hook GetItemUserData GetItemUserDataHook
private module m
private static method onInit takes nothing returns nothing
set DEATH_TRIG = CreateTrigger()
call TriggerAddCondition(DEATH_TRIG, Condition(function Leave))
endmethod
endmodule
private struct s
implement m
endstruct
endlibrary
Demo:
vJASS:
scope Test initializer onInit
private function onInit takes nothing returns nothing
local item i = CreateItem('afac', GetStartLocationX(0), GetStartLocationY(0))
call DisplayTimedTextToPlayer(Player(0), 0, 0, 3, "I used 'GetItemIndex' to retrieve " + GetItemName(i) + "'s index. Index: " + I2S(GetItemIndex(i)))
set i = CreateItem('spsh', GetStartLocationX(0), GetStartLocationY(0))
call DisplayTimedTextToPlayer(Player(0), 0, 0, 3, "I used 'GetItemUserData' to retrieve " + GetItemName(i) + "'s index. Index: " + I2S(GetItemUserData(i)))
set i = null
endfunction
endscope
Last edited: