Skip to content

Commit

Permalink
Allow two variations of syntax in fromString (#96)
Browse files Browse the repository at this point in the history
* Allow two variations of syntax in fromString

* Added comments to show differences between the variations
  • Loading branch information
Flonja authored Mar 17, 2024
1 parent 38e8e87 commit 52fda55
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,12 @@ function provider (registry, { Biome, version }) {
if (str.startsWith('minecraft:')) str = str.substring(10)
const name = str.split('[', 1)[0]
const propertiesStr = str.slice(name.length + 1, -1).split(',')
if (version.type === 'pc') {
if (!str.includes('["')) {
// Example state: `minecraft:candle[lit=true]` -> candle, {lit: "true"}
return Block.fromProperties(name, Object.fromEntries(propertiesStr.map(property => property.split('='))), biomeId)
} else if (version.type === 'bedrock') {
} else {
// Kept for backwards compatibility
// Example state: `minecraft:candle["lit":true]` -> candle, {lit: 1}
return Block.fromProperties(name, Object.fromEntries(propertiesStr.map(property => {
const [key, value] = property.split(':')
return [key.slice(1, -1), value.startsWith('"') ? value.slice(1, -1) : { true: 1, false: 0 }[value] ?? parseInt(value)]
Expand Down
1 change: 1 addition & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ describe('fromString', () => {
const versions = {
1.18: 'minecraft:candle[lit=true]',
'pe_1.18.0': 'minecraft:candle["lit":true]',
1.19: 'minecraft:candle["lit":true]',
'1.20': 'minecraft:candle[lit=true]'
}
for (const [version, str] of Object.entries(versions)) {
Expand Down

0 comments on commit 52fda55

Please sign in to comment.