From d6af23d3a5ad0b4e44846cb5e4adb7cf443a865f Mon Sep 17 00:00:00 2001 From: InnovationGuru Date: Mon, 16 Mar 2026 18:44:40 -0400 Subject: [PATCH] Fix `givemagianitem` GM command Co-authored-by: Xaver-DaRed --- scripts/commands/givemagianitem.lua | 64 ++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/scripts/commands/givemagianitem.lua b/scripts/commands/givemagianitem.lua index f834441a4f6..074826713a8 100644 --- a/scripts/commands/givemagianitem.lua +++ b/scripts/commands/givemagianitem.lua @@ -11,35 +11,61 @@ commandObj.cmdprops = parameters = 'sib' } -commandObj.onTrigger = function(player, target, trialId, isRewardItem) +commandObj.onTrigger = function(player, targetName, trialId, isRewardItem) + -- Early return: No target name. + if not targetName then + player:printToPlayer('You must enter a valid player name.') + return + end + + -- Early return: No trial ID input. + if not trialId then + player:printToPlayer('You must enter a valid Trial ID.') + return + end + + -- Early return: No valid target entity. + local targetEntity = GetPlayerByName(targetName) + if not targetEntity then + player:printToPlayer(string.format('Player named \'%s\' not found!', targetName)) + return + end + + -- Early return: No trial data. + local trialData = xi.magian.trials[trialId] + if not trialData then + player:printToPlayer(string.format('Trial ID \'%u\' was not found.', trialId)) + return + end + local giveRewardItem = isRewardItem and true or false + local itemData = giveRewardItem and trialData.rewardItem or trialData.requiredItem + if not itemData or not itemData.itemId then + player:printToPlayer(string.format('Trial ID \'%u\' does not have a valid %s item.', trialId, giveRewardItem and 'reward' or 'required')) + return + end - if - target == nil or - trialId == nil - then - player:printToPlayer('You must enter a valid player name and Trial ID.') + -- Check target inventory. + if targetEntity:getFreeSlotsCount() == 0 then + player:printToPlayer(string.format('Player \'%s\' does not have free space for that item!', targetName)) return end - local targ = GetPlayerByName(target) - if targ == nil then - player:printToPlayer(string.format('Player named \'%s\' not found!', target)) + -- Check if target already has the item. + if targetEntity:hasItem(itemData.itemId) then + player:printToPlayer(string.format('%s already has item %i.', targetEntity:getName(), itemData.itemId)) return end - -- Attempt to give the target the item - if targ:getFreeSlotsCount() == 0 then - player:printToPlayer(string.format('Player \'%s\' does not have free space for that item!', target)) + -- Handle item. + if giveRewardItem then + xi.magian.giveRewardItem(targetEntity, trialId) else - if giveRewardItem then - xi.magian.giveRewardItem(target, trialId) - else - xi.magian.giveRequiredItem(target, trialId) - end - - player:printToPlayer(string.format('Gave player \'%s\' Item for Trial ID \'%u\' ', target, trialId)) + xi.magian.giveRequiredItem(targetEntity, trialId, true) end + + targetEntity:messageSpecial(zones[targetEntity:getZoneID()].text.ITEM_OBTAINED, itemData.itemId) + player:printToPlayer(string.format('Gave player \'%s\' Item for Trial ID \'%u\' ', targetName, trialId)) end return commandObj