schematic4j icon indicating copy to clipboard operation
schematic4j copied to clipboard

Error when loading a schematic

Open LegendsOfXania opened this issue 8 months ago • 10 comments

Hello, I'm having an issue with the schematic4j library: I can't load .schem files (Sponge format v3) in my Kotlin plugin. I'm using PaperMC 1.21.4, and I’ve added this version of schematic4j as a dependency.

Currently, my code looks like this:

val schematic: String = ""
private fun pasteSchematic(schematic: String) {
    val schem = SchematicLoader.load("plugins/FastAsyncWorldEdit/schematics/$schematic")
    logger.info("Loaded ${schem.name()} : ${schem.width()}x${schem.height()}x${schem.length()}")
}

However, the logger outputs: [Typewriter] Loaded null : 0x0x0

I'm sorry to bother you, and I appreciate any help. Looking forward to your response, Xaya

LegendsOfXania avatar May 03 '25 17:05 LegendsOfXania

This version : https://github.com/LegendsOfXania/schematic4j

LegendsOfXania avatar May 03 '25 17:05 LegendsOfXania

I checked that the schematic existed and that it wasn't corrupted.

LegendsOfXania avatar May 04 '25 09:05 LegendsOfXania

I recently have run into this issue as well. Using fabric loader 1.21.4 and world edit to save my schematic. The schematic works in Minecraft (though it is very large, not sure if that plays a part). When trying to use this tool all the methods return there being 0 blocks. Code snippet below

    File schemFile = new File(filepath);
    System.out.println("file");
    System.out.println(filepath);
    try {
        System.out.println("Chemin du fichier: " + schemFile.getAbsolutePath());

        if (!schemFile.exists()) {
            throw new IOException("Le fichier n'existe pas: " + schemFile.getAbsolutePath());
        }

        Schematic schematic = SchematicLoader.load(schemFile);

        controller.showProgressBar();
        int totalBlocks = schematic.width() * schematic.length() * schematic.height();
        System.out.println("Blocks");
        System.out.println(totalBlocks);

I have confirmed the file exists and is not corrupted.

JaCrispySSB avatar Jul 15 '25 19:07 JaCrispySSB

Hello @JaCrispySSB @LegendsOfXania , I don't think SandroHc is maintaining this project anymore since he's stopped responding to/updating it in over 2 years. However, I may be able to help both of you, I have a private fork of this repository which can read litematics and sponge schematics just fine. If you'd like to use this please add me on Discord at ryandev.

RyanLandDev avatar Jul 15 '25 21:07 RyanLandDev

Hey @RyanLandDev! Yes, I do not have the mental capacity/motivation to support the project at the moment.

I have added you as as a collaborator so you can manage it as you see fit.

I can also find a way to give you access to the Maven Central repository, so you can publish new versions without breaking existing projects.

SandroHc avatar Jul 16 '25 00:07 SandroHc

Hey @RyanLandDev! Yes, I do not have the mental capacity/motivation to support the project at the moment.

I have added you as as a collaborator so you can manage it as you see fit.

I can also find a way to give you access to the Maven Central repository, so you can publish new versions without breaking existing projects.

Thank you for your response.

I will probably update the project with fixes/enhancements in the near future. I think that a lot of developers could benefit from this.

And sure, that would be great.

RyanLandDev avatar Jul 16 '25 00:07 RyanLandDev

I'm sorry for being unresponsive. Leaving my projects unmaintained always leaves a bad taste in my mouth. Sometimes life just gets in the way.

SandroHc avatar Jul 16 '25 23:07 SandroHc

I hope everything goes well for you @SandroHc . Also, @RyanLandDev I've sent you a friend request on Discord to chat (xayatv)

LegendsOfXania avatar Jul 17 '25 19:07 LegendsOfXania

Haven't received anything, make sure you've got the correct username - "ryandev." with the dot

RyanLandDev avatar Jul 17 '25 19:07 RyanLandDev

I've think I've found the problem. World Edit typically generates schematics with an extra sub-compound tag called Schematic under the root tag similar to this SNBT:

"": {
	Schematic: {
		Blocks: {
			Palette: {
				"minecraft:stone": 0
				"minecraft:cobblestone": 1
			}
			Data: bytes(...)
			BlockEntities: []
		}
		Version: 3
		Length: 33S
		Metadata: {
			WorldEdit: {
				Origin: ints(8, -60, 8)
				Platforms: {
					"enginehub:fabric": {
						Version: "7.3.16+cbf4bd5"
						Name: Fabric-Official
					}
				}
				EditingPlatform: "enginehub:fabric"
				Version: "7.3.16"
			}
			Date: 1759923072977L
		}
		Height: 1S
		DataVersion: 4440
		Width: 33S
		Offset: ints(-16, -1, -16)
	}
}

However, [SpongeParser}(https://github.com/SandroHc/schematic4j/blob/master/src/main/java/net/sandrohc/schematic4j/parser/SpongeParser.java) ingores this sub-tag alltogether and assumes all children belong to the root resulting with zero / empty data.

As a workaround one can load their schematics similar to this:

            final NamedTag rootTag = NBTUtil.Reader.read().from(...); // path, InputStream, or byte[]
            final CompoundTag nbt = rootTag!=null&& rootTag.getTag() instanceof CompoundTag? (CompoundTag) rootTag.getTag() :null;
            final CompoundTag schematicRoot = nbt.getCompoundTag("Schematic");
            Schematic schematic = SchematicLoader.parse(schematicRoot);

In addition, you may need to use a GZIPInputStream if your schematic is compressed (which is very likely).

ajh123 avatar Oct 08 '25 15:10 ajh123