Skip to content

Commit

Permalink
fix: datapacks tools actions
Browse files Browse the repository at this point in the history
  • Loading branch information
majestyotbr committed Aug 25, 2024
1 parent 65c58b0 commit a351f6a
Show file tree
Hide file tree
Showing 22 changed files with 200 additions and 82 deletions.
8 changes: 8 additions & 0 deletions data-canary/scripts/actions/tools/crowbar.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local crowbar = Action()

function crowbar.onUse(player, item, fromPosition, target, toPosition, isHotkey)
return ActionsLib.useCrowbar(player, item, fromPosition, target, toPosition, isHotkey)
end

crowbar:id(3304)
crowbar:register()
8 changes: 8 additions & 0 deletions data-canary/scripts/actions/tools/machete.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local machete = Action()

function machete.onUse(player, item, fromPosition, target, toPosition, isHotkey)
return ActionsLib.useMachete(player, item, fromPosition, target, toPosition, isHotkey)
end

machete:id(3308)
machete:register()
8 changes: 8 additions & 0 deletions data-canary/scripts/actions/tools/pick.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local pick = Action()

function pick.onUse(player, item, fromPosition, target, toPosition, isHotkey)
return ActionsLib.usePick(player, item, fromPosition, target, toPosition, isHotkey)
end

pick:id(3456)
pick:register()
8 changes: 8 additions & 0 deletions data-canary/scripts/actions/tools/rope.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local rope = Action()

function rope.onUse(player, item, fromPosition, target, toPosition, isHotkey)
return ActionsLib.useRope(player, item, fromPosition, target, toPosition, isHotkey)
end

rope:id(3003, 646)
rope:register()
8 changes: 8 additions & 0 deletions data-canary/scripts/actions/tools/scythe.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local scythe = Action()

function scythe.onUse(player, item, fromPosition, target, toPosition, isHotkey)
return ActionsLib.useScythe(player, item, fromPosition, target, toPosition, isHotkey)
end

scythe:id(3453)
scythe:register()
8 changes: 8 additions & 0 deletions data-canary/scripts/actions/tools/shovel.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local shovel = Action()

function shovel.onUse(player, item, itemEx, fromPosition, target, toPosition, isHotkey)
return ActionsLib.useShovel(player, item, itemEx, fromPosition, target, toPosition, isHotkey)
end

shovel:id(3457, 5710)
shovel:register()
8 changes: 8 additions & 0 deletions data-canary/scripts/actions/tools/spoon.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local spoon = Action()

function spoon.onUse(player, item, fromPosition, target, toPosition, isHotkey)
return ActionsLib.useSpoon(player, item, fromPosition, target, toPosition, isHotkey)
end

spoon:id(3468)
spoon:register()
23 changes: 23 additions & 0 deletions data-canary/scripts/actions/tools/toolgear.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local toolGear = Action()

function toolGear.onUse(player, item, fromPosition, target, toPosition, isHotkey)
if math.random(100) > 5 then
return ActionsLib.useRope(player, item, fromPosition, target, toPosition, isHotkey)
or ActionsLib.useShovel(player, item, fromPosition, target, toPosition, isHotkey)
or ActionsLib.usePick(player, item, fromPosition, target, toPosition, isHotkey)
or ActionsLib.useMachete(player, item, fromPosition, target, toPosition, isHotkey)
or ActionsLib.useCrowbar(player, item, fromPosition, target, toPosition, isHotkey)
or ActionsLib.useSpoon(player, item, fromPosition, target, toPosition, isHotkey)
or ActionsLib.useScythe(player, item, fromPosition, target, toPosition, isHotkey)
or ActionsLib.useKitchenKnife(player, item, fromPosition, target, toPosition, isHotkey)
else
player:say("Oh no! Your tool is jammed and can't be used for a minute.", TALKTYPE_MONSTER_SAY)
player:addAchievementProgress("Bad Timing", 10)
item:transform(item.itemid + 1)
item:decay()
end
return true
end

toolGear:id(9594, 9596, 9598)
toolGear:register()
169 changes: 111 additions & 58 deletions data-canary/scripts/lib/register_actions.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,63 @@
local holeId = { 294, 369, 370, 385, 394, 411, 412, 413, 432, 433, 435, 8709, 594, 595, 615, 609, 610, 615, 1156, 482, 483, 868, 874, 4824, 7768, 433, 432, 413, 7767, 411, 370, 369, 7737, 7755, 7768, 7767, 7515, 7516, 7517, 7518, 7519, 7520, 7521, 7522, 7762, 8144, 8690, 8709, 12203, 12961, 17239, 19220, 23364 } -- usable rope holes, for rope spots see global.lua
local wildGrowth = { 2130, 2130, 2982, 2524, 2030, 2029, 10182 } -- wild growth destroyable by machete
local jungleGrass = { [3696] = 3695, [3702] = 3701, [17153] = 17151 } -- grass destroyable by machete
local groundIds = { 354, 355 } -- pick usable ground
local sandIds = { 231 } -- desert sand
local fruits = { 2673, 2674, 2675, 2676, 2677, 2678, 2679, 2680, 2681, 2682, 2684, 2685, 5097, 8839, 8840, 8841 } -- fruits to make decorated cake with knife
local wildGrowth = { 3635, 30224 } -- wild growth destroyable by machete
local jungleGrass = { -- grass destroyable by machete
[3696] = 3695,
[3702] = 3701,
[17153] = 17151,
}
local holeId = { -- usable rope holes, for rope spots see global.lua
294,
369,
370,
385,
394,
411,
412,
413,
432,
433,
435,
482,
483,
594,
595,
609,
610,
615,
868,
874,
1156,
4824,
7515,
7516,
7517,
7518,
7519,
7520,
7521,
7522,
7737,
7755,
7762,
7767,
7768,
8144,
8690,
8709,
12203,
12961,
17239,
19220,
23364,
}
local groundIds = { 354, 355 } -- pick usable grounds
local holes = { 593, 606, 608, 867, 21341 } -- holes opened by shovel
local sandIds = { 231, 9059 } -- desert sand
local fruits = { 3584, 3585, 3586, 3587, 3588, 3589, 3590, 3591, 3592, 3593, 3595, 3596, 5096, 8011, 8012, 8013 } -- fruits to make decorated cake with knife
local ropeSpots = { 386, 421, 12935, 12936, 14238, 17238, 21501, 21965, 21966, 21967, 21968, 23363 }

function destroyItem(player, target, toPosition)
ActionsLib = {}

ActionsLib.destroyItem = function(player, target, toPosition)
if type(target) ~= "userdata" or not target:isItem() then
return false
end
Expand Down Expand Up @@ -40,15 +91,13 @@ function destroyItem(player, target, toPosition)
end
end
end

target:remove(1)
end

toPosition:sendMagicEffect(CONST_ME_POFF)
return true
end

function onUseMachete(player, item, fromPosition, target, toPosition, isHotkey)
ActionsLib.useMachete = function(player, item, fromPosition, target, toPosition, isHotkey)
local targetId = target.itemid
if not targetId then
return true
Expand All @@ -67,10 +116,10 @@ function onUseMachete(player, item, fromPosition, target, toPosition, isHotkey)
player:addAchievementProgress("Nothing Can Stop Me", 100)
return true
end
return destroyItem(player, target, toPosition)
return false
end

function onUsePick(player, item, fromPosition, target, toPosition, isHotkey)
ActionsLib.usePick = function(player, item, fromPosition, target, toPosition, isHotkey)
if target.itemid == 10310 then -- shiny stone refining
local chance = math.random(1, 100)
if chance == 1 then
Expand All @@ -82,7 +131,6 @@ function onUsePick(player, item, fromPosition, target, toPosition, isHotkey)
else
player:addItem(3028) -- 49% chance of getting small diamond
end

player:addAchievementProgress("Petrologist", 100)
target:getPosition():sendMagicEffect(CONST_ME_BLOCKHIT)
target:remove(1)
Expand All @@ -99,7 +147,7 @@ function onUsePick(player, item, fromPosition, target, toPosition, isHotkey)
return false
end

if table.contains(groundIds, ground.itemid) and ground.actionid == actionIds.pickHole then
if table.contains(groundIds, ground.itemid) and (ground:hasAttribute(ITEM_ATTRIBUTE_UNIQUEID) or ground:hasAttribute(ITEM_ATTRIBUTE_ACTIONID)) then
ground:transform(394)
ground:decay()
toPosition:sendMagicEffect(CONST_ME_POFF)
Expand All @@ -119,55 +167,41 @@ function onUsePick(player, item, fromPosition, target, toPosition, isHotkey)
return false
end

function onUseRope(player, item, fromPosition, target, toPosition, isHotkey)
ActionsLib.useRope = function(player, item, fromPosition, target, toPosition, isHotkey)
local tile = Tile(toPosition)
if not tile then
return false
end

local ground = tile:getGround()
if ground and table.contains(ropeSpots, ground:getId()) or tile:getItemById(12935) then
tile = Tile(toPosition:moveUpstairs())
if not tile then
return false
end

if tile:hasFlag(TILESTATE_PROTECTIONZONE) and player:isPzLocked() then
if table.contains(ropeSpots, tile:getGround():getId()) or tile:getItemById(12935) then
if Tile(toPosition:moveUpstairs()):hasFlag(TILESTATE_PROTECTIONZONE) and player:isPzLocked() then
player:sendCancelMessage(RETURNVALUE_PLAYERISPZLOCKED)
return true
end

player:teleportTo(toPosition, false)
return true
end

if table.contains(holeId, target.itemid) then
elseif table.contains(holeId, target.itemid) then
toPosition.z = toPosition.z + 1
tile = Tile(toPosition)
if not tile then
return false
end

local thing = tile:getTopVisibleThing()
if not thing then
return true
end

if thing:isPlayer() then
if Tile(toPosition:moveUpstairs()):queryAdd(thing) ~= RETURNVALUE_NOERROR then
return false
if tile then
local thing = tile:getTopVisibleThing()
if thing:isPlayer() then
if Tile(toPosition:moveUpstairs()):hasFlag(TILESTATE_PROTECTIONZONE) and thing:isPzLocked() then
return false
end
return thing:teleportTo(toPosition, false)
end
if thing:isItem() and thing:getType():isMovable() then
return thing:moveTo(toPosition:moveUpstairs())
end

return thing:teleportTo(toPosition, false)
elseif thing:isItem() and thing:getType():isMovable() then
return thing:moveTo(toPosition:moveUpstairs())
end
player:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
return true
end
return false
end

function onUseShovel(player, item, fromPosition, target, toPosition, isHotkey)
ActionsLib.useShovel = function(player, item, fromPosition, target, toPosition, isHotkey)
local tile = Tile(toPosition)
if not tile then
return false
Expand All @@ -182,7 +216,13 @@ function onUseShovel(player, item, fromPosition, target, toPosition, isHotkey)
if table.contains(holes, groundId) then
ground:transform(groundId + 1)
ground:decay()

toPosition:moveDownstairs()
toPosition.y = toPosition.y - 1
if Tile(toPosition):hasFlag(TILESTATE_PROTECTIONZONE) and player:isPzLocked() then
player:sendCancelMessage(RETURNVALUE_PLAYERISPZLOCKED)
return true
end
player:teleportTo(toPosition, false)
toPosition.z = toPosition.z + 1
tile:relocateTo(toPosition)
player:addAchievementProgress("The Undertaker", 500)
Expand All @@ -209,24 +249,23 @@ function onUseShovel(player, item, fromPosition, target, toPosition, isHotkey)
end
elseif table.contains(sandIds, groundId) then
local randomValue = math.random(1, 100)
if target.actionid == actionIds.sandHole and randomValue <= 20 then
if target.actionid == 100 and randomValue <= 20 then
ground:transform(615)
ground:decay()
elseif randomValue == 1 then
Game.createItem(3042, 1, toPosition)
Game.createItem(3042, 1, toPosition) -- Scarab Coin
player:addAchievementProgress("Gold Digger", 100)
elseif randomValue > 95 then
Game.createMonster("Scarab", toPosition)
end

toPosition:sendMagicEffect(CONST_ME_POFF)
else
return false
end
return true
end

function onUseScythe(player, item, fromPosition, target, toPosition, isHotkey)
ActionsLib.useScythe = function(player, item, fromPosition, target, toPosition, isHotkey)
if not table.contains({ 3453, 9596 }, item.itemid) then
return false
end
Expand All @@ -238,6 +277,27 @@ function onUseScythe(player, item, fromPosition, target, toPosition, isHotkey)
player:addAchievementProgress("Happy Farmer", 200)
return true
end
return false
end

ActionsLib.useCrowbar = function(player, item, fromPosition, target, toPosition, isHotkey)
if not table.contains({ 3304, 9598 }, item.itemid) then
return false
end
return false
end

ActionsLib.useSpoon = function(player, item, fromPosition, target, toPosition, isHotkey)
if not table.contains({ 3468, 3470 }, item.itemid) then
return false
end
return false
end

ActionsLib.useSickle = function(player, item, fromPosition, target, toPosition, isHotkey)
if not table.contains({ 3293, 3306 }, item.itemid) then
return false
end

if target.itemid == 5464 then -- burning sugar cane
target:transform(5463)
Expand All @@ -246,17 +306,10 @@ function onUseScythe(player, item, fromPosition, target, toPosition, isHotkey)
player:addAchievementProgress("Natural Sweetener", 50)
return true
end
return destroyItem(player, target, toPosition)
end

function onUseCrowbar(player, item, fromPosition, target, toPosition, isHotkey)
if not table.contains({ 3304, 9598 }, item.itemid) then
return false
end
return destroyItem(player, target, toPosition)
return false
end

function onUseKitchenKnife(player, item, fromPosition, target, toPosition, isHotkey)
ActionsLib.useKitchenKnife = function(player, item, fromPosition, target, toPosition, isHotkey)
if not table.contains({ 3469, 9594, 9598 }, item.itemid) then
return false
end
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit a351f6a

Please sign in to comment.