Skip to content

Commit 3295242

Browse files
authored
Improve error handling for unsupported schematic files (#2453)
* Improve error handling for unsupported schematic files * Fix review notes
1 parent 6a5a500 commit 3295242

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* WorldEdit, a Minecraft world manipulation toolkit
3+
* Copyright (C) sk89q <http://www.sk89q.com>
4+
* Copyright (C) WorldEdit team and contributors
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
package com.sk89q.worldedit.extent.clipboard.io;
21+
22+
import com.sk89q.worldedit.util.formatting.text.Component;
23+
24+
/**
25+
* Raised when a known exception occurs during schematic load.
26+
*/
27+
public final class SchematicLoadException extends RuntimeException {
28+
29+
private final Component message;
30+
31+
public SchematicLoadException(Component message) {
32+
this.message = message;
33+
}
34+
35+
/**
36+
* Get the message of this exception as a rich text component.
37+
*
38+
* @return The rich message
39+
*/
40+
public Component getRichMessage() {
41+
return this.message;
42+
}
43+
}

worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/SpongeSchematicReader.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import com.sk89q.worldedit.regions.CuboidRegion;
4646
import com.sk89q.worldedit.regions.Region;
4747
import com.sk89q.worldedit.util.Location;
48+
import com.sk89q.worldedit.util.formatting.text.TextComponent;
49+
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
4850
import com.sk89q.worldedit.world.DataFixer;
4951
import com.sk89q.worldedit.world.biome.BiomeType;
5052
import com.sk89q.worldedit.world.biome.BiomeTypes;
@@ -124,7 +126,7 @@ public Clipboard read() throws IOException {
124126
BlockArrayClipboard clip = readVersion1(schematicTag);
125127
return readVersion2(clip, schematicTag);
126128
}
127-
throw new IOException("This schematic version is currently not supported");
129+
throw new SchematicLoadException(TranslatableComponent.of("worldedit.schematic.load.unsupported-version", TextComponent.of(schematicVersion)));
128130
}
129131

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

159+
// Be lenient about the specific nesting level of the Schematic tag
160+
// Also allows checking the version from newer versions of the specification
161+
if (schematic.size() == 1 && schematic.containsKey("Schematic")) {
162+
schematicTag = requireTag(schematic, "Schematic", CompoundTag.class);
163+
schematic = schematicTag.getValue();
164+
}
165+
157166
schematicVersion = requireTag(schematic, "Version", IntTag.class).getValue();
158167
return schematicTag;
159168
}

worldedit-core/src/main/java/com/sk89q/worldedit/internal/command/exception/WorldEditExceptionConverter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.sk89q.worldedit.command.tool.InvalidToolBindException;
3535
import com.sk89q.worldedit.extension.input.DisallowedUsageException;
3636
import com.sk89q.worldedit.extension.input.NoMatchException;
37+
import com.sk89q.worldedit.extent.clipboard.io.SchematicLoadException;
3738
import com.sk89q.worldedit.internal.expression.ExpressionException;
3839
import com.sk89q.worldedit.regions.RegionOperationException;
3940
import com.sk89q.worldedit.util.formatting.text.Component;
@@ -183,6 +184,11 @@ public void convert(FileSelectionAbortedException e) throws CommandException {
183184
throw newCommandException(TranslatableComponent.of("worldedit.error.file-aborted"), e);
184185
}
185186

187+
@ExceptionMatch
188+
public void convert(SchematicLoadException e) throws CommandException {
189+
throw newCommandException(e.getRichMessage(), e);
190+
}
191+
186192
@ExceptionMatch
187193
public void convert(WorldEditException e) throws CommandException {
188194
throw newCommandException(e.getRichMessage(), e);

worldedit-core/src/main/resources/lang/strings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
"worldedit.schematic.load.does-not-exist": "Schematic {0} does not exist!",
107107
"worldedit.schematic.load.loading": "(Please wait... loading schematic.)",
108108
"worldedit.schematic.load.still-loading": "(Please wait... still loading schematic.)",
109+
"worldedit.schematic.load.unsupported-version": "This schematic version is currently not supported. Version: {0}.",
109110
"worldedit.schematic.save.already-exists": "That schematic already exists. Use the -f flag to overwrite it.",
110111
"worldedit.schematic.save.failed-directory": "Could not create folder for schematics!",
111112
"worldedit.schematic.save.saving": "(Please wait... saving schematic.)",

0 commit comments

Comments
 (0)