Repository navigation
Add Support for Reading SpongeV3 schematics #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DanielSchmerber
wants to merge
7
commits into
PrismarineJS:master
Choose a base branch
from
DanielSchmerber:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fd28451
Add Support for reading the spongeV3 schematic Format
DanielSchmerber ca5657b
Fixed Offset when parsing
DanielSchmerber 0cfde9a
Improved formatting & Removed unnecessary imports/function/const
DanielSchmerber f8ba133
Removed todo
DanielSchmerber d67baf6
Fixed Style in index.js
DanielSchmerber b4b620f
Update index.js
DanielSchmerber b5a0ef6
Update index.js
DanielSchmerber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DanielSchmerber
There was a problem hiding this comment.
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.