Skip to content

Commit

Permalink
feat: Add optimistic update for block placement
Browse files Browse the repository at this point in the history
The code changes in `place_block.js` introduce an optimistic update feature for block placement. This feature sets the default state of the block being placed based on the held item. It retrieves the default state from the `minecraft-data` module and updates the block state at the destination accordingly. This optimistic update improves the responsiveness of block placement.

Note: This suggested commit message follows the convention of using a prefix to indicate the type of change (`feat` for a new feature).
  • Loading branch information
zardoy committed Jun 6, 2024
1 parent 315cdfc commit 1995a47
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/plugins/place_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ function inject (bot) {
let oldBlock = bot.blockAt(dest)
await bot._genericPlace(referenceBlock, faceVector, options)

// optimistic update
const heldItem = bot.heldItem
const mcData = require('minecraft-data')(bot.version)
const defaultState = mcData.blocksByName[heldItem?.name]?.defaultState
if (defaultState) {
bot.world.setBlockStateId(dest, defaultState)
}

let newBlock = bot.blockAt(dest)
if (oldBlock.type === newBlock.type) {
[oldBlock, newBlock] = await onceWithCleanup(bot, `blockUpdate:${dest}`, {
Expand Down

0 comments on commit 1995a47

Please sign in to comment.