33//
44
55import CoreGraphics
6+ import Foundation
67
78public enum MapArtGenerator {
89 private static let tileSize = 128
910
10- /// Generates map art from an image and places it in a shulker box chest at the specified location.
11+ /// Generates map art from an image, packs the maps into a shulker box,
12+ /// and inserts it into the specified player's inventory.
13+ ///
1114 /// - Parameters:
12- /// - database: The level database to store map data and chest
13- /// - image: The source image to convert to map art
14- /// - x: X coordinate for the chest placement
15- /// - y: Y coordinate for the chest placement
16- /// - z: Z coordinate for the chest placement
17- /// - shulkerBoxName: Optional name for the shulker box containing maps
18- /// - Throws: If image processing, map data generation, or placement fails
19- public static func generateAndPlace(
15+ /// - database: The level database used to store generated map data
16+ /// - image: The source image to convert into map art
17+ /// - playerKey: The database key identifying the target player
18+ /// - shulkerBoxName: Optional custom name for the shulker box containing the maps
19+ ///
20+ /// - Throws:
21+ /// - If map data generation or storage fails
22+ /// - If the player's inventory has no available slot
23+ /// - If the player's NBT data is invalid or cannot be parsed
24+ public static func generateAndGiveToPlayer(
2025 database: LevelKeyValueStore ,
2126 image: CGImage ,
22- x: Int32 ,
23- y: Int32 ,
24- z: Int32 ,
27+ playerKey: Data ,
2528 shulkerBoxName: String ? = nil
2629 ) throws {
2730 let ( mapItems, mapDataDict) = try buildMapItemsAndData ( image: image, database: database)
@@ -32,7 +35,7 @@ public enum MapArtGenerator {
3235 try database. putData ( entryData, forKey: lvdbKey. data)
3336 }
3437 let shulkerBox = try ShulkerNestingPacker . pack ( items: mapItems, rootName: shulkerBoxName)
35- try ChestEntityGenerator . place ( database : database , x : x , y : y , z : z , items : [ shulkerBox ] )
38+ try ItemInjector . giveItemToPlayer ( item : shulkerBox , playerKey : playerKey , in : database )
3639 } catch {
3740 // Rollback map data on failure
3841 for lvdbKey in mapDataDict. keys {
@@ -122,6 +125,7 @@ public enum MapArtGenerator {
122125 /// - Throws: If unable to allocate the requested number of IDs
123126 private static func allocateMapIDs( in database: LevelKeyValueStore , count: Int ) throws -> [ Int64 ] {
124127 let iter = try database. makeIterator ( )
128+ iter. moveToFirst ( )
125129 defer {
126130 iter. close ( )
127131 }
@@ -131,7 +135,7 @@ public enum MapArtGenerator {
131135 while ids. count < count {
132136 let keyData = LvDBKey . map ( currentID) . data
133137 iter. move ( to: keyData)
134- if iter. currentKey != keyData {
138+ if ! ( iter. isValid && iter . currentKey == keyData) {
135139 ids. append ( currentID)
136140 }
137141 currentID += 1
0 commit comments