Skip to content

Commit

Permalink
Improve error handling for unsupported schematic files
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Dec 24, 2023
1 parent 6a5a500 commit 38b3548
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
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 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,7 @@
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.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 +125,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"));
}

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

// Handle newer versions in a cleaner way
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.",
"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

0 comments on commit 38b3548

Please sign in to comment.