diff --git a/src/lib/plugins/entities.js b/src/lib/plugins/entities.js index 4fa85e3..2206d5a 100644 --- a/src/lib/plugins/entities.js +++ b/src/lib/plugins/entities.js @@ -265,9 +265,12 @@ module.exports.entity=function(entity,serv){ } // Add it to a stack already in the player's inventory if possible - for(var item in entity.inventory.items()){ + for(var itemKey in entity.inventory.slots){ + var item = entity.inventory.slots[itemKey] + if(item == undefined) continue; if(item.type == collectEntity.itemId){ - item.stackSize += 1 + item.count += 1 + entity.inventory.updateSlot(itemKey, item) collectEntity._writeOthersNearby('collect', { collectedEntityId: collectEntity.id, collectorEntityId: entity.id diff --git a/src/lib/plugins/inventory.js b/src/lib/plugins/inventory.js index a8dad72..d3d95be 100644 --- a/src/lib/plugins/inventory.js +++ b/src/lib/plugins/inventory.js @@ -2,7 +2,7 @@ var Version = require("../version") var Windows = require("prismarine-windows")(Version).windows var ItemStack = require("prismarine-item")(Version) -module.exports.player=function(player) +module.exports.player = function(player) { player.heldItemSlot = 0 player.heldItem = new ItemStack(256, 1) @@ -10,8 +10,8 @@ module.exports.player=function(player) player._client.on("held_item_slot", ({slotId} = {}) => { player.heldItemSlot = slotId; - player.heldItem = player.inventory.slots[36 + player.heldItemSlot] + player._writeOthersNearby("entity_equipment",{ entityId: player.id, slot: 0, @@ -19,6 +19,102 @@ module.exports.player=function(player) }); }); + player._client.on("window_click", function(clickInfo){ + // Do other stuff the inventory doesn't do, eg spawn the dropped item. + // I've left in stuff that inventory handles, because the cancelling hooks + // might go here (?) + switch(clickInfo.mode){ + case 0: + if(clickInfo.mouseButton == 0){ + // Left mouse click + // Inventory deals with this + }else{ + // Right mouse click + // Inventory deals with this + } + break; + + case 1: + if(clickInfo.mouseButton == 0){ + // Shift + Left click + // Inventory deals with this + }else{ + // Shift + right click + // Inventory deals with this + } + break; + + case 2: + // button 0 -> 8, indication hotbar switching items + // (Nothing to do with held_item_slot) + // DANGER! crashes because windows.js hasn't implemented it yet. + break + + case 3: + // Middle click + // DANGER! crashes because windows.js hasn't implemented it yet. + break; + + case 4: + if(clickInfo.slot == -999){ + // Click with nothing outside window. Do nothing. + }else{ + // I'd love to implement this, but dropped entities are not finished. + if(clickInfo.mouseButton == 0){ + // Drop one item at slot + // Inventory handles removing one + }else{ + // Drop full stack at slot + // Inventory handles removing the whole stack + } + } + break; + + // Inventory does not support dragging yet, so not implementing yet. + case 5: + if(clickInfo.slot == -999){ + switch(clickInfo.mouseButton){ + case 0: + // Start left mouse drag + break; + + case 4: + // Start right mouse drag + break; + + case 2: + // End left mouse drag + break; + + case 6: + // End right mouse drag + break; + } + }else{ + switch(clickInfo.mouseButton){ + case 1: + // Add slot for left mouse drag + break; + + case 5: + // Add slot for right mouse drag + break; + } + } + break; + + // Inventory does not support double click yet, so not implementing yet. + case 6: + // Double click + break; + } + + // Let the inventory know of the click. + // It's important to let it know of the click later, because it destroys + // information we need about the inventory. + player.inventory.acceptClick(clickInfo) + }) + player._client.on("set_creative_slot", ({slot,item} ={}) => { if(item.blockId == -1){ player.inventory.updateSlot(slot, undefined) @@ -28,12 +124,6 @@ module.exports.player=function(player) var newItem = new ItemStack(item.blockId, item.itemCount, item.metadata) player.inventory.updateSlot(slot, newItem) - if (slot==36) - player._writeOthersNearby("entity_equipment",{ - entityId:player.id, - slot:0, - item:item - }); if (slot==5) player._writeOthersNearby("entity_equipment",{ entityId:player.id, @@ -64,6 +154,14 @@ module.exports.player=function(player) player.inventory.on("windowUpdate", function(){ var items = player.inventory.slots + // Update held item + player._writeOthersNearby("entity_equipment",{ + entityId: player.id, + slot: 0, + item: ItemStack.toNotch(player.heldItem) + }); + + // Update slots in inventory for(var itemIndex in items){ var item = items[itemIndex] player._client.write("set_slot", {