From 497be64855b031e3573d2a2aec67842ecdb3b1f0 Mon Sep 17 00:00:00 2001 From: KnightMiner Date: Thu, 3 Feb 2022 23:24:44 -0500 Subject: [PATCH] Okay, lets actually use has resource, and only error if we still get an exception despite has returning true --- .../mantle/command/DumpTagCommand.java | 21 ++++++++++++------- .../resources/assets/mantle/lang/en_us.json | 1 + 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/slimeknights/mantle/command/DumpTagCommand.java b/src/main/java/slimeknights/mantle/command/DumpTagCommand.java index 6ac925c8..5ff8d2b0 100644 --- a/src/main/java/slimeknights/mantle/command/DumpTagCommand.java +++ b/src/main/java/slimeknights/mantle/command/DumpTagCommand.java @@ -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; @@ -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 @@ -63,15 +66,19 @@ private static int run(CommandContext 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 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 diff --git a/src/main/resources/assets/mantle/lang/en_us.json b/src/main/resources/assets/mantle/lang/en_us.json index 8914e243..22d42b56 100644 --- a/src/main/resources/assets/mantle/lang/en_us.json +++ b/src/main/resources/assets/mantle/lang/en_us.json @@ -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'",