Joriku
Working in the mines, need something?
Hi,
I am lost here..
no idea what I am doing wrong as of now, but the player gets access before the items are turned in.
Anyone got a clue why?
I am lost here..
no idea what I am doing wrong as of now, but the player gets access before the items are turned in.
Anyone got a clue why?
LUA:
local actions = {}
-- Configuration
local doorsConfig = {
[9000] = {doorID = 6907, requiredItems = {9020}, useCount = 20, teleportPosition = Position(30998, 31642, 7), storageID = 10000, levelRequired = 50},
[9001] = {doorID = 6907, requiredItems = {4033}, useCount = 20, teleportPosition = Position(31020, 31592, 7), storageID = 10001, levelRequired = 60},
[9002] = {doorID = 6907, requiredItems = {29347}, useCount = 20, teleportPosition = Position(30923, 31636, 7), storageID = 10002, levelRequired = 70},
[9003] = {doorID = 6907, requiredItems = {7425}, useCount = 20, teleportPosition = Position(31016, 31590, 7), storageID = 10003, levelRequired = 80},
[9004] = {doorID = 6907, requiredItems = {9692}, useCount = 20, teleportPosition = Position(31045, 31821, 7), storageID = 10004, levelRequired = 90},
[9005] = {doorID = 6898, requiredItems = {9017, 9383, 8820, 11701, 3206}, useCount = 20, teleportPosition = Position(30927, 31689, 7), storageID = 10005, levelRequired = 100, requireAllItems = true},
}
-- true for debugging mode
local deBug = false
function getItemCount(player, requiredItems, requireAll)
local itemCount = 0
for _, itemId in ipairs(requiredItems) do
local count = player:getItemCount(itemId)
if requireAll then
itemCount = math.min(itemCount == 0 and count or itemCount, count)
else
itemCount = itemCount + count
end
end
return itemCount
end
function removeRequiredItems(player, requiredItems, count, requireAll)
for _, itemId in ipairs(requiredItems) do
local removeCount = requireAll and count or math.min(player:getItemCount(itemId), count)
player:removeItem(itemId, removeCount)
count = count - removeCount
if count <= 0 then break end
end
end
local function onUse(player, item, fromPosition, target, toPosition, isHotkey, actionID)
if deBug then
player:sendTextMessage(18, "Attempting to use the door.")
player:sendTextMessage(18, "Item ID: " .. item.itemid .. ", Action ID: " .. actionID)
end
local doorConfig = doorsConfig[actionID]
if doorConfig then
if deBug then
player:sendTextMessage(18, "Configuration found for action ID: " .. actionID)
player:sendTextMessage(18, "Door ID in config: " .. doorConfig.doorID .. ", Required Items: " .. table.concat(doorConfig.requiredItems, ", "))
end
if item.itemid == doorConfig.doorID then
if deBug then
player:sendTextMessage(18, "The door ID matches configuration.")
end
-- Check the player's level
if player:getLevel() < doorConfig.levelRequired then
player:sendTextMessage(18, "You need to be at least level " .. doorConfig.levelRequired .. " to use this door.")
return false
end
-- Check if the door is already unlocked
if player:getStorageValue(doorConfig.storageID) == 1 then
player:sendTextMessage(18, "The door is already unlocked for you.")
player:teleportTo(doorConfig.teleportPosition)
doorConfig.teleportPosition:sendMagicEffect(CONST_ME_TELEPORT)
return true
end
-- Get the current use count
local currentUseCount = player:getStorageValue(doorConfig.storageID)
if currentUseCount < 0 then
currentUseCount = 0
end
-- Calculate the number of items the player has
local itemCount = getItemCount(player, doorConfig.requiredItems, doorConfig.requireAllItems)
if itemCount > 0 then
-- Ensure only required amount of items is used
local remainingItemsRequired = doorConfig.useCount - currentUseCount
local itemsToUse = math.min(itemCount, remainingItemsRequired)
-- Remove the used items from the player's inventory
removeRequiredItems(player, doorConfig.requiredItems, itemsToUse, doorConfig.requireAllItems)
-- Update the use count
currentUseCount = currentUseCount + itemsToUse
player:setStorageValue(doorConfig.storageID, currentUseCount)
-- If the new use count meets or exceeds the required use count
if currentUseCount >= doorConfig.useCount then
-- Set the storage value to mark the door as unlocked
player:setStorageValue(doorConfig.storageID, 1)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have used the required items and the door is now permanently unlocked.")
player:teleportTo(doorConfig.teleportPosition)
doorConfig.teleportPosition:sendMagicEffect(CONST_ME_TELEPORT)
return true
else
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have used some items. Use the door " .. (doorConfig.useCount - currentUseCount) .. " more times to permanently unlock it.")
return false
end
else
player:sendTextMessage(18, "You need more items to use this door.")
return false
end
else
player:sendTextMessage(18, "The door ID does not match any configuration. Expected: " .. doorConfig.doorID .. ", Got: " .. item.itemid)
end
else
player:sendTextMessage(18, "No configuration found for action ID: " .. actionID)
end
return false
end
for actionID, config in pairs(doorsConfig) do
actions[actionID] = Action()
actions[actionID].onUse = function(player, item, fromPosition, target, toPosition, isHotkey)
return onUse(player, item, fromPosition, target, toPosition, isHotkey, actionID)
end
actions[actionID]:aid(actionID)
actions[actionID]:register()
end