Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use prismarine-world #1145

Merged
merged 9 commits into from
Aug 9, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
- [Bot](#bot)
- [mineflayer.createBot(options)](#mineflayercreatebotoptions)
- [Properties](#properties)
- [bot.world](#botworld)
- [bot.entity](#botentity)
- [bot.entities](#botentities)
- [bot.username](#botusername)
Expand All @@ -110,13 +111,13 @@
- [bot.settings.colorsEnabled](#botsettingscolorsenabled)
- [bot.settings.viewDistance](#botsettingsviewdistance)
- [bot.settings.difficulty](#botsettingsdifficulty)
- [bot.settings.showCape](#botsettingsshowcape)
- [bot.settings.showJacket](#botsettingsshowjacket)
- [bot.settings.showLeftSleeve](#botsettingsleftsleeve)
- [bot.settings.showRightSleeve](#botsettingsshowrightsleeve)
- [bot.settings.showLeftPants](#botsettingsshowleftpants)
- [bot.settings.showRightPants](#botsettingsshowrightpants)
- [bot.settings.showHat](#botsettingsshowhat)
- [bot.settings.skinParts.showCape](#botsettingsskinpartsshowcape)
- [bot.settings.skinParts.showJacket](#botsettingsskinpartsshowjacket)
- [bot.settings.skinParts.showLeftSleeve](#botsettingsskinpartsshowleftsleeve)
- [bot.settings.skinParts.showRightSleeve](#botsettingsskinpartsshowrightsleeve)
- [bot.settings.skinParts.showLeftPants](#botsettingsskinpartsshowleftpants)
- [bot.settings.skinParts.showRightPants](#botsettingsskinpartsshowrightpants)
- [bot.settings.skinParts.showHat](#botsettingsskinpartsshowhat)
- [bot.experience.level](#botexperiencelevel)
- [bot.experience.points](#botexperiencepoints)
- [bot.experience.progress](#botexperienceprogress)
Expand Down Expand Up @@ -181,7 +182,7 @@
- ["hardcodedSoundEffectHeard" (soundId, soundCategory, position, volume, pitch)](#hardcodedsoundeffectheard-soundid-soundcategory-position-volume-pitch)
- ["noteHeard" (block, instrument, pitch)](#noteheard-block-instrument-pitch)
- ["pistonMove" (block, isPulling, direction)](#pistonmove-block-ispulling-direction)
- ["chestLidMove" (block, isOpen)](#chestlidmove-block-isopen)
- ["chestLidMove" (block, isOpen, block2)](#chestlidmove-block-isopen-block2)
- ["blockBreakProgressObserved" (block, destroyStage)](#blockbreakprogressobserved-block-destroystage)
- ["blockBreakProgressEnd" (block)](#blockbreakprogressend-block)
- ["diggingCompleted" (block)](#diggingcompleted-block)
Expand All @@ -206,6 +207,7 @@
- ["bossBarUpdated" (bossBar)](#bossbarupdated-bossbar)
- [Functions](#functions)
- [bot.blockAt(point, extraInfos=true)](#botblockatpoint-extrainfostrue)
- [bot.waitForChunksToLoad(cb)](#botwaitforchunkstoloadcb)
- [bot.blockInSight(maxSteps, vectorLength)](#botblockinsightmaxsteps-vectorlength)
- [bot.canSeeBlock(block)](#botcanseeblockblock)
- [bot.findBlocks(options)](#botfindblocksoptions)
Expand Down Expand Up @@ -746,6 +748,10 @@ Create and return an instance of the class bot.

### Properties

#### bot.world

A sync representation of the world. Check the doc at http://github.com/PrismarineJS/prismarine-world

#### bot.entity

Your own entity. See `Entity`.
Expand Down Expand Up @@ -1249,6 +1255,10 @@ Fires when new boss bar is updated.
Returns the block at `point` or `null` if that point is not loaded. If `extraInfos` set to true, also returns informations about signs, paintings and block entities (slower).
See `Block`.

#### bot.waitForChunksToLoad(cb)

The cb gets called when many chunks have loaded.

#### bot.blockInSight(maxSteps, vectorLength)

Returns the block at which bot is looking at or `null`
Expand Down
1 change: 0 additions & 1 deletion docs/unstable_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [unstable API : bot._](#unstable-api--bot_)
- [bot._chunkColumn(x, z)](#bot_chunkcolumnx-z)
- [bot._client](#bot_client)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->
Expand Down
1 change: 0 additions & 1 deletion docs/unstable_api_ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
Чтобы посмотреть актуальную информацию, Вы можете посмотреть оригинальную документацию [нестабильного API](unstable_api.md).

- [нестабильное API : bot._](#unstable-api--bot_)
- [bot._chunkColumn(x, z)](#bot_chunkcolumnx-z)
- [bot._client](#bot_client)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->
Expand Down
5 changes: 5 additions & 0 deletions examples/bee.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ function loop (n) {
bot.on('chat', (username, message) => {
if (username === bot.username) return
switch (message) {
case 'loaded':
bot.waitForChunksToLoad(() => {
bot.chat('Ready!')
})
break
case 'fly':
bot.creative.startFlying()
loop(10)
Expand Down
7 changes: 7 additions & 0 deletions examples/blockfinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ bot.on('chat', (username, message) => {

const mcData = require('minecraft-data')(bot.version)

if (message === 'loaded') {
console.log(bot.entity.position)
bot.waitForChunksToLoad(() => {
bot.chat('Ready!')
})
}

if (message.startsWith('find')) {
const name = message.split(' ')[1]
if (mcData.blocksByName[name] === undefined) {
Expand Down
5 changes: 5 additions & 0 deletions examples/digger.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const bot = mineflayer.createBot({
bot.on('chat', (username, message) => {
if (username === bot.username) return
switch (message) {
case 'loaded':
bot.waitForChunksToLoad(() => {
bot.chat('Ready!')
})
break
case 'list':
sayItems()
break
Expand Down
5 changes: 5 additions & 0 deletions examples/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ bot.on('chat', (username, message) => {
if (username === bot.username) return
const command = message.split(' ')
switch (true) {
case message === 'loaded':
bot.waitForChunksToLoad(() => {
bot.chat('Ready!')
})
break
case /^list$/.test(message):
sayItems()
break
Expand Down
82 changes: 40 additions & 42 deletions lib/plugins/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ function inject (bot, { version }) {
const Block = require('prismarine-block')(version)
const Chunk = require('prismarine-chunk')(version)
const ChatMessage = require('prismarine-chat')(version)
let columns = {}
const World = require('prismarine-world')(version)
bot.world = new World().sync
const signs = {}
const paintingsByPos = {}
const paintingsById = {}
Expand Down Expand Up @@ -74,21 +75,22 @@ function inject (bot, { version }) {

function delColumn (chunkX, chunkY) {
const columnCorner = new Vec3(chunkX * 16, 0, chunkY * 16)
const key = columnKeyXZ(columnCorner.x, columnCorner.z)
delete columns[key]
bot.world.unloadColumn(chunkX, chunkY)
rom1504 marked this conversation as resolved.
Show resolved Hide resolved
bot.emit('chunkColumnUnload', columnCorner)
}

function addColumn (args) {
const columnCorner = new Vec3(args.x * 16, 0, args.z * 16)
const key = columnKeyXZ(columnCorner.x, columnCorner.z)
if (!args.bitMap) {
// stop storing the chunk column
delColumn(args.x, args.z)
return
}
let column = columns[key]
if (!column) columns[key] = column = new Chunk()
let column = bot.world.getColumn(args.x, args.z)
if (!column) {
column = new Chunk()
bot.world.setColumn(args.x, args.z, column)
}

try {
column.load(args.data, args.bitMap, args.skyLightSent, args.groundUp)
Expand All @@ -103,6 +105,21 @@ function inject (bot, { version }) {
bot.emit('chunkColumnLoad', columnCorner)
}

function waitForChunksToLoad (cb) {
// check 5x5 chunks around us
for (let x = -2; x <= 2; x++) {
for (let y = -2; y <= 2; y++) {
if (bot.blockAt(bot.entity.position.plus(new Vec3(x, 0, y).scaled(16))) == null) {
rom1504 marked this conversation as resolved.
Show resolved Hide resolved
// keep wait
return setTimeout(() => {
waitForChunksToLoad(cb)
}, 100)
}
}
}
cb()
}

function getMatchingFunction (matching) {
if (typeof (matching) !== 'function') {
if (!Array.isArray(matching)) {
Expand Down Expand Up @@ -150,7 +167,7 @@ function inject (bot, { version }) {
let startedLayer = 0
let next = start
while (next) {
const column = columns[columnKeyXZ(next.x * 16, next.z * 16)]
const column = bot.world.getColumn(next.x, next.y)
if (next.y >= 0 && next.y < 16 && column && !visitedSections.has(next.toString())) {
const section = column.sections[next.y]
if (isBlockInSection(section, matcher)) {
Expand Down Expand Up @@ -189,22 +206,13 @@ function inject (bot, { version }) {
return bot.blockAt(blocks[0])
}

function posInChunk (pos) {
pos = pos.floored()
pos.x &= 15
pos.z &= 15
return pos
}

function blockAt (absolutePoint, extraInfos = true) {
const loc = new Location(absolutePoint)
const key = columnKeyXZ(loc.chunkCorner.x, loc.chunkCorner.z)

const column = columns[key]
// null column means chunk not loaded
if (!column) return null
const block = bot.world.getBlock(absolutePoint)
// null block means chunk not loaded
if (!block) return null

const block = column.getBlock(posInChunk(absolutePoint))
block.position = loc.floored
if (extraInfos) {
block.signText = signs[loc.floored]
Expand Down Expand Up @@ -250,11 +258,6 @@ function inject (bot, { version }) {
return false
}

function chunkColumn (x, z) {
const key = columnKeyXZ(x, z)
return columns[key]
}

function emitBlockUpdate (oldBlock, newBlock) {
bot.emit('blockUpdate', oldBlock, newBlock)
const position = oldBlock ? oldBlock.position
Expand All @@ -269,14 +272,14 @@ function inject (bot, { version }) {
function updateBlockState (point, stateId) {
const oldBlock = blockAt(point)
const loc = new Location(point)
const key = columnKeyXZ(loc.chunkCorner.x, loc.chunkCorner.z)
const column = columns[key]
// sometimes minecraft server sends us block updates before it sends
// us the column that the block is in. ignore this.
if (!column) return
column.setBlockStateId(posInChunk(point), stateId)
bot.world.setBlockStateId(point, stateId)

const newBlock = blockAt(point)
// sometimes minecraft server sends us block updates before it sends
// us the column that the block is in. ignore this.
if (newBlock === null) {
return
}
if (oldBlock.type !== newBlock.type) {
delete blockEntities[loc.floored]
delete signs[loc.floored]
Expand All @@ -289,10 +292,11 @@ function inject (bot, { version }) {
}

bot._client.on('update_light', (packet) => {
const key = columnKeyXZ(packet.chunkX, packet.chunkZ)
let column = columns[key]

if (!column) columns[key] = column = new Chunk()
let column = bot.world.getColumn(packet.chunkX, packet.chunkZ)
if (!column) {
column = new Chunk()
bot.world.setColumn(packet.chunkX, packet.chunkZ, column)
}

column.loadLight(packet.data, packet.skyLightMask, packet.blockLightMask, packet.emptySkyLightMask, packet.emptyBlockLightMask)
})
Expand Down Expand Up @@ -448,21 +452,15 @@ function inject (bot, { version }) {
bot._client.on('respawn', (packet) => {
if (dimension === packet.dimension) return
dimension = packet.dimension
columns = {}
bot._columns = columns
bot.world = new World().sync
})

bot.findBlock = findBlock
bot.canSeeBlock = canSeeBlock
bot.blockAt = blockAt
bot._chunkColumn = chunkColumn
bot._updateBlockState = updateBlockState
bot._columns = columns
bot._blockEntities = blockEntities
}

function columnKeyXZ (x, z) {
return `${x},${z}`
bot.waitForChunksToLoad = waitForChunksToLoad
}

function onesInShort (n) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"prismarine-physics": "^1.0.2",
"prismarine-recipe": "^1.1.0",
"prismarine-windows": "^1.5.0",
"prismarine-world": "^3.1.0",
"protodef": "^1.8.0",
"typed-emitter": "^1.2.0",
"vec3": "^0.1.5"
Expand Down
Loading