@@ -3,7 +3,7 @@ const assert = require('assert')
33module . exports = inject
44
55function inject ( bot ) {
6- const allowedWindowTypes = [ 'minecraft:cartography' ]
6+ const allowedWindowTypes = [ 'minecraft:cartography' , 'minecraft:generic' ]
77
88 function matchWindowType ( window ) {
99 for ( const type of allowedWindowTypes ) {
@@ -18,14 +18,18 @@ function inject (bot) {
1818 throw new Error ( 'This is not a cartographyTable-like window' )
1919 }
2020
21+ const mapSlot = 0
22+ const modifierSlot = 1
23+ const outputSlot = 2
24+
2125 cartographyTable . takeMap = takeMap
2226 cartographyTable . takeModifier = takeModifier
2327 cartographyTable . takeOutput = takeOutput
2428 cartographyTable . putMap = putMap
2529 cartographyTable . putModifier = putModifier
26- cartographyTable . mapItem = function ( ) { return this . slots [ 0 ] }
27- cartographyTable . modifierItem = function ( ) { return this . slots [ 1 ] }
28- cartographyTable . outputItem = function ( ) { return this . slots [ 2 ] }
30+ cartographyTable . mapItem = function ( ) { return this . slots [ mapSlot ] }
31+ cartographyTable . modifierItem = function ( ) { return this . slots [ modifierSlot ] }
32+ cartographyTable . outputItem = function ( ) { return this . slots [ outputSlot ] }
2933
3034 return cartographyTable
3135
@@ -47,26 +51,31 @@ function inject (bot) {
4751 return takeSomething ( cartographyTable . outputItem ( ) )
4852 }
4953
50- async function putSomething ( destSlot , itemType , metadata , count ) {
51- const options = {
52- window : cartographyTable ,
53- itemType,
54- metadata,
55- count,
56- sourceStart : cartographyTable . inventoryStart ,
57- sourceEnd : cartographyTable . inventoryEnd ,
58- destStart : destSlot ,
59- destEnd : destSlot + 1
54+ async function putMap ( itemType , metadata , count ) {
55+ const sourceSlot = findItemInInventory ( itemType , metadata )
56+ if ( sourceSlot === null ) {
57+ throw new Error ( `Cannot find item ${ itemType } in inventory` )
6058 }
61- await bot . transfer ( options )
59+ await bot . moveSlotItem ( sourceSlot , mapSlot )
6260 }
6361
64- async function putMap ( itemType , metadata , count ) {
65- await putSomething ( 0 , itemType , metadata , count )
62+ async function putModifier ( itemType , metadata , count ) {
63+ const sourceSlot = findItemInInventory ( itemType , metadata )
64+ if ( sourceSlot === null ) {
65+ throw new Error ( `Cannot find item ${ itemType } in inventory` )
66+ }
67+ await bot . moveSlotItem ( sourceSlot , modifierSlot )
6668 }
6769
68- async function putModifier ( itemType , metadata , count ) {
69- await putSomething ( 1 , itemType , metadata , count )
70+ function findItemInInventory ( itemType , metadata ) {
71+ const inventory = bot . inventory
72+ for ( let i = inventory . inventoryStart ; i < inventory . inventoryEnd ; i ++ ) {
73+ const item = inventory . slots [ i ]
74+ if ( item && item . type === itemType && ( metadata === null || item . metadata === metadata ) ) {
75+ return i
76+ }
77+ }
78+ return null
7079 }
7180 }
7281
0 commit comments