• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[General] Removing selected items on hero inventory slot 6

Status
Not open for further replies.
Level 18
Joined
Nov 21, 2012
Messages
835
hey, here you have:
JASS:
globals  
    hashtable           g_ItemHash=InitHashtable()
    trigger                 g_trg_ItemSwitch=CreateTrigger()
endglobals
//-----------------------------------------------------------------

function IsItemAllowedForSlot5 takes item itm returns boolean
    local integer itemId=GetItemTypeId(itm)
    return itemId=='gemt' // or itemId=='frgd' or ...... list here all item that  can be carried in slot5
endfunction
//-----------------------------------------------------------------
//------------ITEM PICK UP-------------------------------------
//-----------------------------------------------------------------
function Trig_ItemWeapon_Pickup takes nothing returns nothing
    local unit u=GetManipulatingUnit()
    local item itm=GetManipulatedItem()
    local item itm5=UnitItemInSlot(u, 5)
    if itm==itm5 and (not IsItemAllowedForSlot5(itm5)) then
        call UnitRemoveItem(u, itm)
    elseif IsItemAllowedForSlot5(itm) then //move to slot5
        call UnitDropItemSlot(u, itm, 5)
    endif
    set u=null
    set itm=null
    set itm5=null
endfunction

//-----------------------------------------------------------------
//------------ITEM SWITCHED IN INVENTORY---------------------------
//-----------------------------------------------------------------
function Trig_ItemWeaponSwitchInInventory_Cond takes nothing returns boolean
    local integer ord = GetIssuedOrderId()
    return (ord==852002 or ord==852003 or ord==852004 or ord==852005 or ord==852006 or ord==852007)
endfunction
//-------------------------------------------------------------
function Trig_ItemWeaponSwitchInInventory_Child takes nothing returns nothing //runs when any item was moved in inventory
    local timer t = GetExpiredTimer()
    local unit u = LoadUnitHandle(g_ItemHash, GetHandleId(t), 0)//ordered unit
    local item itm5=UnitItemInSlot(u, 5)
    local integer i=0
   
    loop//unit has item5 in diffrent slot?
        exitwhen i>5
        if IsItemAllowedForSlot5(UnitItemInSlot(u, i)) then
            exitwhen true
        endif
        set i=i+1
    endloop
   
    if i<5 then //yes, the item is in wrong slot number "i" 0,1,2,3 or 4
        call DisableTrigger(g_trg_ItemSwitch)
        call IssueTargetOrderById(u, 852007, UnitItemInSlot(u, i)) //reverse player switching
        //call BJDebugMsg("reversed switching")
        call EnableTrigger(g_trg_ItemSwitch)
       
    else //no, unit has not item5-type in inventory
        if itm5!=null and (not IsItemAllowedForSlot5(itm5)) then
            call UnitRemoveItem(u, itm5)
            //call BJDebugMsg("no gem, dropping item from slot 5")
        endif
    endif

    //---
    call FlushChildHashtable(g_ItemHash, GetHandleId(t))
    call DestroyTimer(t)
    set t = null
    set u=null
endfunction
//---------------------------------------------------------------------
function Trig_ItemWeaponSwitchInInventory takes nothing returns nothing
    local timer t=CreateTimer()   
    call SaveUnitHandle(g_ItemHash, GetHandleId(t), 0, GetOrderedUnit())
    call TimerStart(t, 0.00, false, function Trig_ItemWeaponSwitchInInventory_Child)   
    set t=null
endfunction

//===========================================================================
//===========================================================================
function InitTrig_ItemSlotRestriction takes nothing returns nothing
    local trigger t=CreateTrigger() // item picked up
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_PICKUP_ITEM )
    call TriggerAddAction(t, function Trig_ItemWeapon_Pickup)

    call TriggerRegisterAnyUnitEventBJ(g_trg_ItemSwitch, EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER )
    call TriggerAddCondition(g_trg_ItemSwitch, Condition(function Trig_ItemWeaponSwitchInInventory_Cond))
    call TriggerAddAction(g_trg_ItemSwitch, function Trig_ItemWeaponSwitchInInventory)
endfunction
 
Status
Not open for further replies.
Top