Skip to content

Commit

Permalink
Support for the Litematica format
Browse files Browse the repository at this point in the history
  • Loading branch information
SandroHc committed Dec 15, 2023
1 parent cb91e30 commit 8e3b2ae
Show file tree
Hide file tree
Showing 27 changed files with 85,872 additions and 316 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ Java parser for the .schem/.schematic/.litematic Minecraft formats. 🗺

## Supported formats

| Format | Extension | Links |
|-----------------------------------|------------|--------------------------------------------------------------------------|
| [Sponge Schematic Format][sponge] | .schem | Spec: [v1][sponge-spec-v1][v2][sponge-spec-v2][v3][sponge-spec-v3] |
| [Schematica][schematica] | .schematic | [Spec][schematica-spec] |
| Format | Extension | Links |
|----------------------------|------------|--------------------------------------------------------------------------|
| [Sponge Schematic][sponge] | .schem | Spec: [v1][sponge-spec-v1][v2][sponge-spec-v2][v3][sponge-spec-v3] |
| [Litematica][litematica] | .litematic | [Spec][litematica-spec][Discussion][litematica-discussion] |
| [Schematica][schematica] | .schematic | [Spec][schematica-spec] |

[sponge]: https://github.com/SpongePowered/Schematic-Specification
[sponge-spec-v1]: https://github.com/SpongePowered/Schematic-Specification/blob/master/versions/schematic-1.md
[sponge-spec-v2]: https://github.com/SpongePowered/Schematic-Specification/blob/master/versions/schematic-2.md
[sponge-spec-v3]: https://github.com/SpongePowered/Schematic-Specification/blob/master/versions/schematic-3.md
[litematica]: https://github.com/maruohon/litematica
[litematica-spec]: https://github.com/maruohon/litematica/blob/pre-rewrite/fabric/1.20.x/src/main/java/fi/dy/masa/litematica/schematic/LitematicaSchematic.java
[litematica-discussion]: https://github.com/maruohon/litematica/issues/53#issuecomment-520279558
[schematica]: https://curseforge.com/minecraft/mc-mods/schematica
[schematica-spec]: https://minecraft.fandom.com/wiki/Schematic_file_format

Expand Down
57 changes: 38 additions & 19 deletions src/main/java/net/sandrohc/schematic4j/SchematicFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@

import net.sandrohc.schematic4j.exception.NoParserFoundException;
import net.sandrohc.schematic4j.nbt.tag.CompoundTag;
import net.sandrohc.schematic4j.parser.LitematicaParser;
import net.sandrohc.schematic4j.parser.Parser;
import net.sandrohc.schematic4j.parser.SchematicaParser;
import net.sandrohc.schematic4j.parser.SpongeSchematicParser;
import net.sandrohc.schematic4j.parser.SpongeParser;

public enum SchematicFormat {
SPONGE_V1 ("schem", SpongeSchematicParser::new),
SPONGE_V2 ("schem", SpongeSchematicParser::new),
SPONGE_V3 ("schem", SpongeSchematicParser::new),
SPONGE_V1 ("schem", SpongeParser::new),
SPONGE_V2 ("schem", SpongeParser::new),
SPONGE_V3 ("schem", SpongeParser::new),
LITEMATICA ("litematic"),
SCHEMATICA ("schematic", SchematicaParser::new),
WORLD_EDITOR ("schematic"),
Expand Down Expand Up @@ -68,6 +69,7 @@ public Parser createParser() throws NoParserFoundException {

final Candidates<SchematicFormat> candidates = new Candidates<>();
guessSpongeFormat(candidates, nbt);
guessLitematicaFormat(candidates, nbt);
guessSchematicaFormat(candidates, nbt);

final SchematicFormat guess = candidates.best().orElse(SchematicFormat.UNKNOWN);
Expand All @@ -76,8 +78,8 @@ public Parser createParser() throws NoParserFoundException {
}

private static void guessSpongeFormat(Candidates<SchematicFormat> candidates, @NonNull CompoundTag rootTag) {
if (rootTag.containsKey(SpongeSchematicParser.NBT_VERSION)) {
final int version = rootTag.getInt(SpongeSchematicParser.NBT_VERSION);
if (rootTag.containsKey(SpongeParser.NBT_VERSION)) {
final int version = rootTag.getInt(SpongeParser.NBT_VERSION);
switch (version) {
case 1:
candidates.increment(SchematicFormat.SPONGE_V1, 5);
Expand All @@ -91,63 +93,80 @@ private static void guessSpongeFormat(Candidates<SchematicFormat> candidates, @N
candidates.exclude(SchematicFormat.SPONGE_V2);
candidates.exclude(SchematicFormat.SPONGE_V3);
}
if (rootTag.containsKey(SpongeSchematicParser.NBT_DATA_VERSION)) {
if (rootTag.containsKey(SpongeParser.NBT_DATA_VERSION)) {
candidates.increment(SchematicFormat.SPONGE_V1, 1);
candidates.increment(SchematicFormat.SPONGE_V2, 1);
candidates.increment(SchematicFormat.SPONGE_V3, 1);
} else {
candidates.exclude(SchematicFormat.SPONGE_V2);
candidates.exclude(SchematicFormat.SPONGE_V3);
}
if (rootTag.containsKey(SpongeSchematicParser.NBT_WIDTH)) {
if (rootTag.containsKey(SpongeParser.NBT_WIDTH)) {
candidates.increment(SchematicFormat.SPONGE_V1, 1);
candidates.increment(SchematicFormat.SPONGE_V2, 1);
candidates.increment(SchematicFormat.SPONGE_V3, 1);
}
if (rootTag.containsKey(SpongeSchematicParser.NBT_HEIGHT)) {
if (rootTag.containsKey(SpongeParser.NBT_HEIGHT)) {
candidates.increment(SchematicFormat.SPONGE_V1, 1);
candidates.increment(SchematicFormat.SPONGE_V2, 1);
candidates.increment(SchematicFormat.SPONGE_V3, 1);
}
if (rootTag.containsKey(SpongeSchematicParser.NBT_LENGTH)) {
if (rootTag.containsKey(SpongeParser.NBT_LENGTH)) {
candidates.increment(SchematicFormat.SPONGE_V1, 1);
candidates.increment(SchematicFormat.SPONGE_V2, 1);
candidates.increment(SchematicFormat.SPONGE_V3, 1);
}
if (rootTag.containsKey(SpongeSchematicParser.NBT_PALETTE)) {
if (rootTag.containsKey(SpongeParser.NBT_PALETTE)) {
candidates.increment(SchematicFormat.SPONGE_V1, 1);
candidates.increment(SchematicFormat.SPONGE_V2, 1);
}
if (rootTag.containsKey(SpongeSchematicParser.NBT_PALETTE_MAX)) {
if (rootTag.containsKey(SpongeParser.NBT_PALETTE_MAX)) {
candidates.increment(SchematicFormat.SPONGE_V1, 1);
candidates.increment(SchematicFormat.SPONGE_V2, 1);
}
if (rootTag.containsKey(SpongeSchematicParser.NBT_BLOCK_DATA)) {
if (rootTag.containsKey(SpongeParser.NBT_BLOCK_DATA)) {
candidates.increment(SchematicFormat.SPONGE_V1, 1);
candidates.increment(SchematicFormat.SPONGE_V2, 1);
}
if (rootTag.containsKey(SpongeSchematicParser.NBT_BIOME_DATA)) {
if (rootTag.containsKey(SpongeParser.NBT_BIOME_DATA)) {
candidates.increment(SchematicFormat.SPONGE_V2, 1);
}
if (rootTag.containsKey(SpongeSchematicParser.NBT_TILE_ENTITIES)) {
if (rootTag.containsKey(SpongeParser.NBT_TILE_ENTITIES)) {
candidates.increment(SchematicFormat.SPONGE_V1, 1);
}
if (rootTag.containsKey(SpongeSchematicParser.NBT_BLOCK_ENTITIES)) {
if (rootTag.containsKey(SpongeParser.NBT_BLOCK_ENTITIES)) {
candidates.increment(SchematicFormat.SPONGE_V2, 1);
}
if (rootTag.containsKey(SpongeSchematicParser.NBT_V3_BLOCKS)) {
if (rootTag.containsKey(SpongeParser.NBT_V3_BLOCKS)) {
candidates.increment(SchematicFormat.SPONGE_V3, 1);
}
if (rootTag.containsKey(SpongeSchematicParser.NBT_V3_BIOMES)) {
if (rootTag.containsKey(SpongeParser.NBT_V3_BIOMES)) {
candidates.increment(SchematicFormat.SPONGE_V3, 1);
}
if (rootTag.containsKey(SpongeSchematicParser.NBT_METADATA)) {
if (rootTag.containsKey(SpongeParser.NBT_METADATA)) {
candidates.increment(SchematicFormat.SPONGE_V1, 1);
candidates.increment(SchematicFormat.SPONGE_V2, 1);
candidates.increment(SchematicFormat.SPONGE_V3, 1);
}
}

private static void guessLitematicaFormat(Candidates<SchematicFormat> candidates, @NonNull CompoundTag nbt) {
if (nbt.containsKey(LitematicaParser.NBT_MINECRAFT_DATA_VERSION)) {
candidates.increment(SchematicFormat.LITEMATICA, 1);
}
if (nbt.containsKey(LitematicaParser.NBT_VERSION)) {
candidates.increment(SchematicFormat.LITEMATICA, 1);
}
if (nbt.containsKey(LitematicaParser.NBT_METADATA)) {
candidates.increment(SchematicFormat.LITEMATICA, 1);
}
if (nbt.containsKey(LitematicaParser.NBT_REGIONS)) {
candidates.increment(SchematicFormat.LITEMATICA, 2);
} else {
candidates.exclude(SchematicFormat.LITEMATICA);
}
}

private static void guessSchematicaFormat(Candidates<SchematicFormat> candidates, @NonNull CompoundTag nbt) {
if (nbt.containsKey(SchematicaParser.NBT_MAPPING_SCHEMATICA)) {
candidates.increment(SchematicFormat.SCHEMATICA, 10);
Expand Down
Loading

0 comments on commit 8e3b2ae

Please sign in to comment.