Skip to content
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

Improve error handling for unsupported schematic files #2453

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.sk89q.worldedit.extent.clipboard.io;

import com.sk89q.worldedit.util.formatting.text.Component;

/**
* Raised when a known exception occurs during schematic load.
*/
public final class SchematicLoadException extends RuntimeException {

private final Component message;

public SchematicLoadException(Component message) {
this.message = message;
}

/**
* Get the message of this exception as a rich text component.
*
* @return The rich message
*/
public Component getRichMessage() {
return this.message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import com.sk89q.worldedit.world.DataFixer;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.biome.BiomeTypes;
Expand Down Expand Up @@ -124,7 +126,7 @@ public Clipboard read() throws IOException {
BlockArrayClipboard clip = readVersion1(schematicTag);
return readVersion2(clip, schematicTag);
}
throw new IOException("This schematic version is currently not supported");
throw new SchematicLoadException(TranslatableComponent.of("worldedit.schematic.load.unsupported-version", TextComponent.of(schematicVersion)));
}

@Override
Expand Down Expand Up @@ -154,6 +156,13 @@ private CompoundTag getBaseTag() throws IOException {
// Check
Map<String, Tag> schematic = schematicTag.getValue();

// Be lenient about the specific nesting level of the Schematic tag
// Also allows checking the version from newer versions of the specification
if (schematic.size() == 1 && schematic.containsKey("Schematic")) {
schematicTag = requireTag(schematic, "Schematic", CompoundTag.class);
schematic = schematicTag.getValue();
}

schematicVersion = requireTag(schematic, "Version", IntTag.class).getValue();
return schematicTag;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.sk89q.worldedit.command.tool.InvalidToolBindException;
import com.sk89q.worldedit.extension.input.DisallowedUsageException;
import com.sk89q.worldedit.extension.input.NoMatchException;
import com.sk89q.worldedit.extent.clipboard.io.SchematicLoadException;
import com.sk89q.worldedit.internal.expression.ExpressionException;
import com.sk89q.worldedit.regions.RegionOperationException;
import com.sk89q.worldedit.util.formatting.text.Component;
Expand Down Expand Up @@ -183,6 +184,11 @@ public void convert(FileSelectionAbortedException e) throws CommandException {
throw newCommandException(TranslatableComponent.of("worldedit.error.file-aborted"), e);
}

@ExceptionMatch
public void convert(SchematicLoadException e) throws CommandException {
throw newCommandException(e.getRichMessage(), e);
}

@ExceptionMatch
public void convert(WorldEditException e) throws CommandException {
throw newCommandException(e.getRichMessage(), e);
Expand Down
1 change: 1 addition & 0 deletions worldedit-core/src/main/resources/lang/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"worldedit.schematic.load.does-not-exist": "Schematic {0} does not exist!",
"worldedit.schematic.load.loading": "(Please wait... loading schematic.)",
"worldedit.schematic.load.still-loading": "(Please wait... still loading schematic.)",
"worldedit.schematic.load.unsupported-version": "This schematic version is currently not supported. Version: {0}.",
"worldedit.schematic.save.already-exists": "That schematic already exists. Use the -f flag to overwrite it.",
"worldedit.schematic.save.failed-directory": "Could not create folder for schematics!",
"worldedit.schematic.save.saving": "(Please wait... saving schematic.)",
Expand Down
Loading