Skip to content

Commit

Permalink
added support for minecartchests (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
plexigras authored and rom1504 committed Oct 8, 2016
1 parent 0acba54 commit 5429f90
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
6 changes: 3 additions & 3 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
- [bot.moveVehicle(left,forward)](#botmovevehicleleftforward)
- [bot.setQuickBarSlot(slot)](#botsetquickbarslotslot)
- [bot.craft(recipe, count, craftingTable, [callback])](#botcraftrecipe-count-craftingtable-callback)
- [bot.openChest(chestBlock)](#botopenchestchestblock)
- [bot.openChest(chestBlock or minecartchestEntity)](#botopenchestchestblock-or-minecartchestentity)
- [bot.openFurnace(furnaceBlock)](#botopenfurnacefurnaceblock)
- [bot.openDispenser(dispenserBlock)](#botopendispenserdispenserblock)
- [bot.openEnchantmentTable(enchantmentTableBlock)](#botopenenchantmenttableenchantmenttableblock)
Expand Down Expand Up @@ -319,7 +319,7 @@ See [prismarine-recipe](https://github.com/PrismarineJS/prismarine-recipe)
### mineflayer.Chest

Represents a single session of opening and closing a chest.
See `bot.openChest(chestBlock)`.
See `bot.openChest(chestBlock or minecartchestEntity)`.

#### chest.window

Expand Down Expand Up @@ -1252,7 +1252,7 @@ All the direction are relative to where the bot is looking at
* `callback` - (optional) Called when the crafting is complete and your
inventory is updated.

#### bot.openChest(chestBlock)
#### bot.openChest(chestBlock or minecartchestEntity)

Returns a `Chest` instance which represents the chest you are opening.

Expand Down
35 changes: 27 additions & 8 deletions examples/chest.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ bot.on('chat', function(username, message) {
case /^enchant$/.test(message):
watchEnchantmentTable();
break;
case /^chestminecart$/.test(message):
watchChest(true);
break;
case /^invsee \w+( \d)?$/.test(message):
// invsee Herobrine [or]
// invsee Herobrine 1
Expand All @@ -75,15 +78,31 @@ function sayItems(items) {
}
}

function watchChest() {
var chestBlock = bot.findBlock({
matching: [54, 130, 146]
});
if(!chestBlock) {
bot.chat("no chest found");
return;
function watchChest(minecart) {
var chestToOpen;
if (minecart) {
chestToOpen = Object.keys(bot.entities)
.map(function (id) {
return bot.entities[id];
}).find(function (e) {
return e.entityType === 10 &&
e.objectData.intField === 1 &&
bot.entity.position.distanceTo(e.position) < 3;
});
if(!chestToOpen) {
bot.chat("no chest minecart found");
return;
}
} else {
chestToOpen = bot.findBlock({
matching: [54, 130, 146]
});
if(!chestToOpen) {
bot.chat("no chest found");
return;
}
}
var chest = bot.openChest(chestBlock);
var chest = bot.openChest(chestToOpen);
chest.on('open', function() {
sayItems(chest.items());
});
Expand Down
15 changes: 12 additions & 3 deletions lib/plugins/chest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@ module.exports = inject;

function inject(bot) {

function openChest(chestBlock) {
assert.ok(chestBlock.type === 54 || chestBlock.type === 130 || chestBlock.type === 146);
var chest = bot.openBlock(chestBlock, Chest);
function openChest(chestToOpen) {
var chest;
if (chestToOpen.constructor.name === 'Block') {
assert.ok(chestToOpen.type === 54 || chestToOpen.type === 130 || chestToOpen.type === 146);
chest = bot.openBlock(chestToOpen, Chest);
} else if (chestToOpen.constructor.name === 'Entity') {
assert.strictEqual(chestToOpen.entityType, 10);
assert.strictEqual(chestToOpen.objectData.intField, 1);
chest = bot.openEntity(chestToOpen, Chest);
} else {
assert.ok(false, 'chestToOpen is neither block nor entity');
}
chest.withdraw = withdraw;
chest.deposit = deposit;
return chest;
Expand Down

0 comments on commit 5429f90

Please sign in to comment.