local shovels = {
[2556] = { -- Wooden Shovel
dig_time = 5, -- Digging time in seconds
uses = 5, -- Number of uses
find_chance = 0.1, -- Chance to find an item (10%)
possible_items = {{itemid = 2400, count = 3}, {itemid = 2401, count = 2}, {itemid = 2402, count = 1}} -- Possible items with count
},
[2432] = { -- Metal Shovel
dig_time = 3,
uses = 20,
find_chance = 0.15,
possible_items = {{itemid = 2403, count = 1}, {itemid = 2404, count = 1}, {itemid = 2405, count = 2}}
},
[38864] = { -- Golden Shovel
dig_time = 0.5,
uses = math.huge, -- Unlimited usage for one week
find_chance = 0.25,
expiration = os.time() + (7 * 24 * 60 * 60), -- Expires in one week
possible_items = {{itemid = 2406, count = 2}, {itemid = 2407, count = 1}, {itemid = 2408, count = 3}}
}
}
local diggingInProgress = {}
function createHole(position)
local tile = Tile(position)
if tile then
local ground = tile:getGround()
if ground then
local groundId = ground:getId()
if groundId == 9043 then
Game.createItem(103, 1, position) -- Create a hole (ID 103) in the ground
elseif groundId == 9044 then
Game.createItem(104, 1, position) -- Create a hole (ID 104) in the ground
elseif groundId == 9045 then
Game.createItem(105, 1, position) -- Create a hole (ID 105) in the ground
end
end
end
end
function repeatEffect(position, effect, times)
if times > 0 then
position:sendMagicEffect(effect)
addEvent(repeatEffect, 1000, position, effect, times - 1)
end
end
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
-- Check if the player is already digging
if diggingInProgress[player:getGuid()] then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are already digging. Please wait until it's finished.")
return false
end
local shovel = shovels[item:getId()]
if not shovel then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You need to use a shovel to dig here.")
return false
end
if target.actionid ~= 9000 then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You cannot dig here.")
return false
end
local usesLeft = item:getCustomAttribute("uses") or shovel.uses
if usesLeft <= 0 then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your shovel is broken.")
item:remove(1)
return false
else
player:say("Digging... This will take " .. shovel.dig_time .. " seconds. Uses left: " .. usesLeft, TALKTYPE_MONSTER_SAY)
end
local targetPos = toPosition or target:getPosition()
targetPos:sendMagicEffect(CONST_ME_POFF)
player:setMovementBlocked(true)
-- Repeat the effect CONST_ME_HITAREA during the digging time
repeatEffect(targetPos, CONST_ME_HITAREA, shovel.dig_time)
addEvent(function()
targetPos:sendMagicEffect(CONST_ME_HITAREA)
usesLeft = usesLeft - 1
if usesLeft <= 0 then
player:sendTextMessage(MESSAGE_INFO_DESCR, "Your shovel broke.")
item:remove(1)
createHole(target:getPosition()) -- Create a hole in the ground when the shovel breaks
else
item:setCustomAttribute("uses", usesLeft)
if math.random() <= shovel.find_chance then
local randomItem = shovel.possible_items[math.random(#shovel.possible_items)]
player:addItem(randomItem.itemid, randomItem.count)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You found an item while digging!")
else
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You dug but found nothing.")
end
end
player:setMovementBlocked(false)
diggingInProgress[player:getGuid()] = nil
end, shovel.dig_time * 1000)
diggingInProgress[player:getGuid()] = true
return true
end