Skip to content

Commit

Permalink
Fix false value handling in fromProperties for post flatenning versions
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Jun 12, 2024
1 parent 52fda55 commit 86dab0e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ function provider (registry, { Biome, version }) {
static fromProperties (typeId, properties, biomeId) {
const block = typeof typeId === 'string' ? registry.blocksByName[typeId] : registry.blocks[typeId]

if (!block) throw new Error('No matching block id found for ' + typeId + ' with properties ' + JSON.stringify(properties)) // This should not happen

if (version.type === 'pc') {
if (block.states) {
let data = 0
Expand Down Expand Up @@ -359,8 +361,8 @@ function provider (registry, { Biome, version }) {
return state.values.indexOf(value)
}
if (state.type === 'bool') {
if (value === true) return 0
if (value === false) return 1
if (value === true || value === 'true') return 0
if (value === false || value === 'false') return 1
}
if (state.type === 'int') {
return value
Expand Down
2 changes: 0 additions & 2 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ describe('Dig time', () => {
it('using iron_shovel', () => {
const tool = registry.itemsByName[toolName]
const block = Block.fromStateId(registry.blocksByName[blockName].defaultState)
console.log('Block', block)
const time = block.digTime(tool.id, false, false, false, [], {})
expect(time).toBe(750)
})
Expand Down Expand Up @@ -174,7 +173,6 @@ describe('fromString', () => {
for (const [version, str] of Object.entries(versions)) {
const Block = require('prismarine-block')(version)
const block = Block.fromString(str, 0)
console.log(block)
expect(block.getProperties().lit).toBeTruthy()
}
})
13 changes: 13 additions & 0 deletions test/fromProperties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const assert = require('assert')
const testedVersions = require('..').testedVersions

describe('Block From Properties', () => {
// PC (Java)
it('spruce half slab: waterlogged, upper (pc_1.16.4)', () => {
const registry = require('prismarine-registry')('1.16.4')
const Block = require('prismarine-block')(registry)
Expand All @@ -14,6 +15,18 @@ describe('Block From Properties', () => {
expect(block.stateId).toBe(8310)
expect(block.getProperties()).toMatchObject(properties)
})
it('Boolean properties are string (1.18.2, ...)', () => {
const registry = require('prismarine-registry')('1.18.2')
const Block = require('prismarine-block')(registry)
const signId = registry.blocksByName.oak_sign.id
const sourceProperties = { waterlogged: 'false', rotation: '8' }

const block = Block.fromProperties(signId, sourceProperties, 0)
expect(block.stateId).toBe(3455)
expect(block.getProperties()).toMatchObject({ waterlogged: false, rotation: 8 })
})

// Bedrock
it('spruce half slab: waterlogged, upper (bedrock_1.17.10)', () => {
const registry = require('prismarine-registry')('bedrock_1.17.10')
const Block = require('prismarine-block')(registry)
Expand Down

0 comments on commit 86dab0e

Please sign in to comment.