Skip to content

Commit 1514a1d

Browse files
authored
Fix false value handling in fromProperties for post flatenning versions (#98)
* Fix false value handling in fromProperties for post flatenning versions * fix: always use values! * fix: rerun test * normalize to string in pc test * fix
1 parent 05aed2e commit 1514a1d

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

index.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ function provider (registry, { Biome, version }) {
209209
static fromProperties (typeId, properties, biomeId) {
210210
const block = typeof typeId === 'string' ? registry.blocksByName[typeId] : registry.blocks[typeId]
211211

212+
if (!block) throw new Error('No matching block id found for ' + typeId + ' with properties ' + JSON.stringify(properties)) // This should not happen
213+
212214
if (version.type === 'pc') {
213215
if (block.states) {
214216
let data = 0
@@ -355,12 +357,12 @@ function provider (registry, { Biome, version }) {
355357
}
356358

357359
function parseValue (value, state) {
358-
if (state.type === 'enum') {
359-
return state.values.indexOf(value)
360+
if (state.type === 'enum' || state.values) {
361+
return state.values.indexOf(String(value))
360362
}
361363
if (state.type === 'bool') {
362-
if (value === true) return 0
363-
if (value === false) return 1
364+
if (value === true || value === 'true') return 0
365+
if (value === false || value === 'false') return 1
364366
}
365367
if (state.type === 'int') {
366368
return value
@@ -382,7 +384,7 @@ function provider (registry, { Biome, version }) {
382384
}
383385

384386
function propValue (state, value) {
385-
if (state.type === 'enum') return state.values[value]
387+
if (state.type === 'enum' || state.values) return state.values[value]
386388
if (state.type === 'bool') return !value
387389
return value
388390
}

test/basic.test.js

-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ describe('Dig time', () => {
130130
it('using iron_shovel', () => {
131131
const tool = registry.itemsByName[toolName]
132132
const block = Block.fromStateId(registry.blocksByName[blockName].defaultState)
133-
console.log('Block', block)
134133
const time = block.digTime(tool.id, false, false, false, [], {})
135134
expect(time).toBe(750)
136135
})
@@ -174,7 +173,6 @@ describe('fromString', () => {
174173
for (const [version, str] of Object.entries(versions)) {
175174
const Block = require('prismarine-block')(version)
176175
const block = Block.fromString(str, 0)
177-
console.log(block)
178176
expect(block.getProperties().lit).toBeTruthy()
179177
}
180178
})

test/fromProperties.test.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const assert = require('assert')
44
const testedVersions = require('..').testedVersions
55

66
describe('Block From Properties', () => {
7+
// PC (Java)
78
it('spruce half slab: waterlogged, upper (pc_1.16.4)', () => {
89
const registry = require('prismarine-registry')('1.16.4')
910
const Block = require('prismarine-block')(registry)
@@ -14,6 +15,18 @@ describe('Block From Properties', () => {
1415
expect(block.stateId).toBe(8310)
1516
expect(block.getProperties()).toMatchObject(properties)
1617
})
18+
it('Boolean properties are string (1.18.2, ...)', () => {
19+
const registry = require('prismarine-registry')('1.18.2')
20+
const Block = require('prismarine-block')(registry)
21+
const signId = registry.blocksByName.oak_sign.id
22+
const sourceProperties = { waterlogged: 'false', rotation: '8' }
23+
24+
const block = Block.fromProperties(signId, sourceProperties, 0)
25+
expect(block.stateId).toBe(3455)
26+
expect(block.getProperties()).toMatchObject({ waterlogged: false, rotation: 8 })
27+
})
28+
29+
// Bedrock
1730
it('spruce half slab: waterlogged, upper (bedrock_1.17.10)', () => {
1831
const registry = require('prismarine-registry')('bedrock_1.17.10')
1932
const Block = require('prismarine-block')(registry)
@@ -42,10 +55,16 @@ describe('versions should return block state and properties', () => {
4255
// make sure that .fromProperties works
4356
{
4457
const blockData = registry.blocksByName.light_weighted_pressure_plate
45-
const properties = { pc: { power: 2 }, bedrock: { redstone_signal: 2 } }[e]
58+
const properties = { pc: { power: '2' }, bedrock: { redstone_signal: 2 } }[e]
4659
const block = Block.fromProperties(blockData.name, properties, 0)
4760
assert(block.stateId >= blockData.minStateId && block.stateId <= blockData.maxStateId)
48-
expect(block.getProperties()).toMatchObject(properties)
61+
const propertiesNormalized = block.getProperties()
62+
if (e === 'pc') {
63+
for (const key in propertiesNormalized) {
64+
propertiesNormalized[key] = propertiesNormalized[key].toString()
65+
}
66+
}
67+
expect(propertiesNormalized).toMatchObject(properties)
4968
}
5069

5170
// Make sure type IDs work

0 commit comments

Comments
 (0)