Skip to content

Commit

Permalink
perf: performance optimization that causes game lag when clearing leaves
Browse files Browse the repository at this point in the history
  • Loading branch information
Lete114 committed Oct 2, 2023
1 parent ad79809 commit 0d880d8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -25,7 +25,7 @@ To download, you need to go to the GitHub repository [releases](https://github.c

(Drains axe endurance, supports axes with enchanted endurance, stops cutting trees when axe endurance is reduced to 1 point)

![](logo/tree.gif)
![](logo/animation.gif)

## Debugger

Expand Down
Binary file modified logo/animation.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed logo/tree.gif
Binary file not shown.
6 changes: 3 additions & 3 deletions src/behavior_pack/manifest.json
Expand Up @@ -4,7 +4,7 @@
"description": "pack.description",
"name": "pack.name",
"uuid": "1c874d6e-707e-484e-9027-0efff2b048a6",
"version": [1, 0, 0],
"version": [1, 0, 1],
"min_engine_version": [1, 20, 0]
},
"modules": [
Expand All @@ -13,14 +13,14 @@
"language": "javascript",
"type": "script",
"uuid": "9717780a-c69f-4679-bbe6-dbf6dfcdd7de",
"version": [1, 0, 0],
"version": [1, 0, 1],
"entry": "scripts/main.js"
}
],
"dependencies": [
{
"module_name": "@minecraft/server",
"version": "1.3.0-beta"
"version": "1.4.0-beta"
}
]
}
32 changes: 17 additions & 15 deletions src/behavior_pack/scripts/main.ts
Expand Up @@ -91,9 +91,11 @@ async function treeCut(location: Vector3, dimension: Dimension, logLocations: Ve
})
}

function getLeavesLocations(dimension: Dimension, logLocations: Vector3[]) {
// eslint-disable-next-line max-statements
async function clearLeaves(dimension: Dimension, logLocations: Vector3[]) {
const visited = new Set()
const leavesLocations: Vector3[] = []
const batchSize = 27 // Size of each batch
let counter = 0

for (const logLocation of logLocations) {
const locations = getRadiusRange(logLocation)
Expand All @@ -119,20 +121,21 @@ function getLeavesLocations(dimension: Dimension, logLocations: Vector3[]) {
// eslint-disable-next-line max-depth
if (isIncludesLog) continue

leavesLocations.push(location)
locations.push(...getRadiusRange(block.location))
}
}
}

return leavesLocations.sort((a, b) => a.y - b.y)
}
// eslint-disable-next-line max-depth
if (counter === batchSize) {
// Add a short delay to allow the event loop to execute the toggle
await new Promise<void>((resolve) => system.runTimeout(resolve))
counter = 0
}

// eslint-disable-next-line max-statements
async function clearLeaves(dimension: Dimension, leavesLocations: Vector3[]) {
for (const location of leavesLocations) {
const command = `setblock ${Object.values(location).join(' ')} air destroy`
dimension.runCommand(command)
counter++

const command = `setblock ${Object.values(location).join(' ')} air destroy`
dimension.runCommandAsync(command)
}
}
}
}

Expand All @@ -154,8 +157,7 @@ world.afterEvents.blockBreak.subscribe(async (e) => {

await treeCut(block.location, dimension, logLocations)

const leavesLocations = getLeavesLocations(dimension, logLocations)
await clearLeaves(dimension, leavesLocations)
clearLeaves(dimension, logLocations)
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const err = error as any
Expand Down

0 comments on commit 0d880d8

Please sign in to comment.