-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_clone.lua
More file actions
65 lines (59 loc) · 2.74 KB
/
Copy pathmap_clone.lua
File metadata and controls
65 lines (59 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-- Enable cloning the map with a craft table.
--
-- Vanilla Minecraft implements map cloning as a shapeless recipe: exactly ONE
-- filled map plus 1..8 empty maps (any arrangement, stacks allowed; the grid
-- itself bounds the count - a 2x2 grid fits 1, a 3x3 grid fits 8). The result
-- is one filled map per empty map plus the original, all sharing the source
-- map's data, so every copy stays in sync as the world is explored.
--
---@param Player cPlayer
---@param Grid cCraftingGrid
---@param Recipe cCraftingRecipe
function MapCloningOnCraftingNoRecipe(Player, Grid, Recipe)
local Width = Grid:GetWidth()
local Height = Grid:GetHeight()
local MapPos = nil -- grid position of the single filled map
local EmptyCount = 0 -- total number of empty maps (sum of stack counts)
-- Scan the grid: exactly one filled map, at least one empty map and nothing
-- else. Unlike the old code this does not require adjacency, matching the
-- vanilla shapeless recipe (place the empty maps anywhere around/next to it).
for x = 0, Width - 1 do
for y = 0, Height - 1 do
local Item = Grid:GetItem(x, y)
local ItemType = Item.m_ItemType
if ItemType == E_ITEM_EMPTY_MAP then
EmptyCount = EmptyCount + Item.m_ItemCount
elseif ItemType == E_ITEM_MAP then
if (MapPos ~= nil) or (Item.m_ItemCount ~= 1) then
-- More than one filled map (or a stack of them): not cloning.
return false
end
MapPos = { x = x, y = y }
elseif not Item:IsEmpty() then
-- Anything else means this is not the cloning recipe.
return false
end
end
end
-- Need exactly one filled map and at least one empty map.
if (MapPos == nil) or (EmptyCount < 1) then
return false
end
-- Fill in the recipe. Ingredients are the map and every empty map; the full
-- slot stack is listed so ConsumeIngredients consumes all of it (CopyOne
-- would leave stacked empty maps behind).
Recipe:SetIngredient(MapPos.x, MapPos.y, Grid:GetItem(MapPos.x, MapPos.y):CopyOne())
for x = 0, Width - 1 do
for y = 0, Height - 1 do
local Item = Grid:GetItem(x, y)
if Item.m_ItemType == E_ITEM_EMPTY_MAP then
Recipe:SetIngredient(x, y, Item)
end
end
end
-- Result: one copy per empty map plus the original, all sharing the map data.
Recipe:SetResult(Grid:GetItem(MapPos.x, MapPos.y):CopyOne():AddCount(EmptyCount))
-- Return true so Cuberite applies the recipe we just filled (see OnCraftingNoRecipe
-- docs: returning false/nil means "no recipe will be used").
return true
end