globals//declare global variables
integer array clipboard//the clipboard, where copied items are stored
integer temp_int//temp_int is used to pass integers to filters. won't ruin MUI since there are no waits
endglobals
//the filters for GroupEnumUnitsSelected()
function Enum_Selected_Filter_Copy takes nothing returns boolean
return UnitInventorySize(GetEnumUnit()) >= temp_int and UnitItemInSlot(GetEnumUnit(),temp_int) != null
endfunction
function Enum_Selected_Filter_Paste takes nothing returns boolean
return UnitInventorySize(GetEnumUnit()) >= temp_int and not UnitItemInSlot(GetEnumUnit(),temp_int) != null
endfunction
//end filters, following is actions
function Trig_Items_Actions takes nothing returns nothing
local string s = GetEventPlayerChatString()//set as variable to improve speed
local player p = GetTriggerPlayer()//" "
local group g = CreateGroup()//" "
local boolexpr e//need to set boolexpr to a variable so it can be destroyed later, preventing leaks
//do following if player wants to copy
if SubString(s,0,9) == "-copyitem" then
set temp_int = S2I(SubString(s,10,11))//set temp_int to slot player wants to copy from
set e = Condition(function Enum_Selected_Filter_Copy)//set filter as the copy filter
call GroupEnumUnitsSelected(g,p,e)//get units selected, checking if valid using filters
if FirstOfGroup(g) != null then//first of group is the unit to copy from
set clipboard[GetPlayerId(p)] = GetItemTypeId(UnitItemInSlot(FirstOfGroup(g), temp_int))
//save itemid of item to player's clipboard
else
call DisplayTextToPlayer(p,0.,0.,"No item to copy in that slot.")//no item to copy, tell player
endif
//do following if player wants to paste
elseif SubString(s,0,10) == "-pasteitem" then
set temp_int = S2I(SubString(s,11,12))//set temp_int to slot player wants to paste in
set e = Condition(function Enum_Selected_Filter_Paste)//set filter as the paste filter
call GroupEnumUnitsSelected(g,p,e)//get units selected, checking if valid using filters
if FirstOfGroup(g) != null then//first of group is the unit to paste to
call UnitAddItemToSlotById(FirstOfGroup(g),clipboard[GetPlayerId(p)],temp_int)
//add item from clipboard to inventory
else
call DisplayTextToPlayer(p,0.,0.,"Could not paste. Please check if that slot is empty, or if you even have a slot "+I2S(temp_int)+".")
//paste slot unavailable, tell player
endif
endif
//cleaning leaks
call DestroyBoolExpr(e)//destroy the boolexpr, boolexprs leak
set g = null//must set all handles to null
set p = null
set e = null
endfunction
//===========================================================================
function InitTrig_Items takes nothing returns nothing
local integer i = 0
set gg_trg_Items = CreateTrigger( )//initialize the trigger
loop//used a loop for events
exitwhen i == bj_MAX_PLAYERS//exits the loop when i is equal to max players
call TriggerRegisterPlayerChatEvent( gg_trg_Items, Player(i), "-copyitem", false )
//'false' indicates it doesn't have to be an exact match to trigger the trigger
call TriggerRegisterPlayerChatEvent( gg_trg_Items, Player(i), "-pasteitem", false )
set i = i+1//setting i = i+1, moving the loop on to the next player
endloop
call TriggerAddAction( gg_trg_Items, function Trig_Items_Actions )//must add actions
endfunction