Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { Vec3 } = require('vec3')
const mcData = require('minecraft-data')

const sponge = require('./lib/spongeSchematic')
const spongeV3 = require('./lib/spongeV3Schematic')
const mcedit = require('./lib/mceditSchematic')

class Schematic {
Expand Down Expand Up @@ -130,10 +131,13 @@ class Schematic {

static async read (buffer, version = null) {
const schem = nbt.simplify(await parseNbt(buffer))
try {
return sponge.read(schem, version)
} catch {
return mcedit.read(schem, version)
const formats = [sponge, spongeV3, mcedit]
for (const format of formats) {
try {
return format.read(schem, version)
} catch (e) {

}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good if it throws an error if it can't read the format. Right now, it could return undefined if the format is unrecognized. This is wrong for the type definition and also bad if people are wondering why they get undefined instead of a schematic or error.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I don't like is that all errors, even those unrelated to the format, are discarded here in favor of a generic error message that doesn't say anything about what triggered the error. There should also really be a parameter passed here to specify the schematic type, that can fallback to trying different types.

}

Expand Down
65 changes: 65 additions & 0 deletions lib/spongeV3Schematic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const { Vec3 } = require('vec3')
const versions = require('minecraft-data').versions.pc
const { parseBlockName, getStateId } = require('./states')

function findVersion (dataVersion) {
for (const v of versions) {
if (v.dataVersion === dataVersion) {
return v.minecraftVersion
}
}
return versions[0].minecraftVersion // default to latest
}

function parsePalette (mcData, palette) {
const out = []
for (const [str, id] of Object.entries(palette)) {
const { name, properties } = parseBlockName(str)
out[id] = getStateId(mcData, name, properties)
}
return out
}

function byteArrayToVarintArray (byteArray) {
const varintArray = []
let i = 0
while (i < byteArray.length) {
let value = 0
let varintLength = 0
while (true) {
value |= (byteArray[i] & 127) << (varintLength++ * 7)
if (varintLength > 5) throw new Error('VarInt too big (probably corrupted data)')
if ((byteArray[i++] & 128) !== 128) break
}
varintArray.push(value)
}
return varintArray
}

function read (nbt, version) {
const { Schematic } = require('../')
if (!version) {
version = findVersion(nbt.Schematic.DataVersion)
}
let mcData = require('minecraft-data')(version)
if (!mcData) {
version = '1.20.6'
console.log(`Warning, ${version} is not supported, using 1.20.6 as fallback`)
mcData = require('minecraft-data')(version)
}
const palette = parsePalette(mcData, nbt.Schematic.Blocks.Palette)
const size = new Vec3(nbt.Schematic.Width, nbt.Schematic.Height, nbt.Schematic.Length)
let offset = new Vec3(0, 0, 0)

if (nbt.Schematic.Offset) {
offset = new Vec3(
nbt.Schematic.Offset[0],
nbt.Schematic.Offset[1],
nbt.Schematic.Offset[2])
}
const blocks = byteArrayToVarintArray(nbt.Schematic.Blocks.Data)
const schem = new Schematic(version, size, offset, palette, blocks)
return schem
}

module.exports = { read }
1 change: 0 additions & 1 deletion test/cycle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ describe('cycle test', () => {
const schemFile = path.join(__dirname, 'schematics', schemName)
test(`${schemName} read / write`, async () => {
const schematic = await Schematic.read(await fs.readFile(schemFile))

const tmpFile = path.join(__dirname, schemName + '_test.schem')

await fs.writeFile(tmpFile, await schematic.write())
Expand Down
Binary file added test/schematics/spongeV3.schem
Binary file not shown.