Skip to content

Commit

Permalink
Okay, lets actually use has resource, and only error if we still get …
Browse files Browse the repository at this point in the history
…an exception despite has returning true
  • Loading branch information
KnightMiner committed Feb 4, 2022
1 parent 8408b0d commit 497be64
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/main/java/slimeknights/mantle/command/DumpTagCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.Dynamic2CommandExceptionType;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.ResourceLocationArgument;
Expand Down Expand Up @@ -35,6 +36,8 @@
/** Command that dumps a tag into a JSON object */
public class DumpTagCommand {
protected static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static final Dynamic2CommandExceptionType ERROR_READING_TAG = new Dynamic2CommandExceptionType((type, name) -> new TranslatableComponent("command.mantle.dump_tag.read_error", type, name));
private static final Component SUCCESS_LOG = new TranslatableComponent("command.mantle.dump_tag.success_log");

/**
* Registers this sub command with the root command
Expand Down Expand Up @@ -63,15 +66,19 @@ private static int run(CommandContext<CommandSourceStack> context, boolean saveF

ResourceLocation path = new ResourceLocation(name.getNamespace(), "tags/" + type.getTagFolder() + "/" + name.getPath() + ".json");

// if the tag file does not exist, only error if the tag is unknown
List<Resource> resources = Collections.emptyList();
try {
resources = manager.getResources(path);
} catch (IOException ex) {
// if the tag does not exist in the collect, probably an invalid tag name
if (type.getCollection().getTag(name) == null) {
throw ViewTagCommand.TAG_NOT_FOUND.create(type.getName(), name);
if (manager.hasResource(path)) {
try {
resources = manager.getResources(path);
} catch (IOException ex) {
// tag exists and we still could not read it? something went wrong
Mantle.logger.error("Couldn't read {} tag list {} from {}", type.getName(), name, path, ex);
throw ERROR_READING_TAG.create(type.getName(), name);
}
// if its a valid name, avoid erroring, just do the empty tag
// if the tag does not exist in the collect, probably an invalid tag name
} else if (type.getCollection().getTag(name) == null) {
throw ViewTagCommand.TAG_NOT_FOUND.create(type.getName(), name);
}

// simply create a tag builder
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/mantle/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"command.mantle.book_test.not_found": "Unknown book with ID %s",

"command.mantle.dump_tag.success": "Printed combined %s tag '%s' to console log",
"command.mantle.dump_tag.read_error": "Error reading %s tag '%s':",
"command.mantle.dump_tag.success_log": "Dumped combined %s tag '%s' to '%s'",
"command.mantle.dump_all_tags.success": "Dumped all tags to '%s'",
"command.mantle.dump_all_tags.type_success": "Dumped all %s tags to '%s'",
Expand Down

0 comments on commit 497be64

Please sign in to comment.