From adb1eb832b9905c8a20d8504f6b035fa3669f8dc Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Mon, 28 Apr 2025 22:47:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=91=E5=A4=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/translations/en.yml | 2 +- .../bukkit/block/BukkitBlockManager.java | 27 ++-------------- .../furniture/BukkitFurnitureManager.java | 28 +++-------------- .../bukkit/item/BukkitItemManager.java | 20 ++---------- .../item/recipe/AbstractRecipeManager.java | 9 +----- .../core/pack/AbstractPackManager.java | 31 ++++++++++++------- .../craftengine/core/pack/CachedConfig.java | 8 ++++- .../LocalizedResourceConfigException.java | 4 +-- .../craftengine/core/util/CharacterUtils.java | 2 +- 9 files changed, 40 insertions(+), 91 deletions(-) diff --git a/bukkit/loader/src/main/resources/translations/en.yml b/bukkit/loader/src/main/resources/translations/en.yml index 11c6c2e63..e62b421e4 100644 --- a/bukkit/loader/src/main/resources/translations/en.yml +++ b/bukkit/loader/src/main/resources/translations/en.yml @@ -79,7 +79,7 @@ warning.config.image.lack_char: "Issue found in file - The image warning.config.image.codepoint_in_use: "Issue found in file - The image '' is using a character[()] in font that has been used by another image ''." warning.config.image.invalid_codepoint_grid: "Issue found in file - Image '' has an invalid 'chars' codepoint grind." warning.config.image.file_not_exist: "Issue found in file - PNG file '' not found for image ''." -warning.config.image.invalid_hex: "Issue found in file - The image '' is using an unicode character '' that is not a valid hexadecimal (radix 16) value." +warning.config.image.invalid_hex: "Issue found in file - The image '' is using a unicode character '' that is not a valid hexadecimal (radix 16) value." warning.config.recipe.duplicated: "Issue found in file - Duplicated recipe ''." warning.config.recipe.lack_type: "Issue found in file - The recipe '' is missing the required argument 'type'." warning.config.recipe.invalid_type: "Issue found in file - The recipe '' is using an invalid recipe type ''." diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitBlockManager.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitBlockManager.java index 676698505..f7e0369a3 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitBlockManager.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BukkitBlockManager.java @@ -335,24 +335,10 @@ public class BukkitBlockManager extends AbstractBlockManager { throw new LocalizedResourceConfigException("warning.config.block.duplicated", path, id); } // read block settings - BlockSettings settings; - try { - settings = BlockSettings.fromMap(MiscUtils.castToMap(section.getOrDefault("settings", Map.of()), false)); - } catch (LocalizedResourceConfigException e) { - e.setPath(path); - e.setId(id); - throw e; - } + BlockSettings settings = BlockSettings.fromMap(MiscUtils.castToMap(section.getOrDefault("settings", Map.of()), false)); // read loot table - LootTable lootTable; - try { - lootTable = LootTable.fromMap(MiscUtils.castToMap(section.getOrDefault("loot", Map.of()), false)); - } catch (LocalizedResourceConfigException e) { - e.setPath(path); - e.setId(id); - throw e; - } + LootTable lootTable = LootTable.fromMap(MiscUtils.castToMap(section.getOrDefault("loot", Map.of()), false)); // read states Map> properties; @@ -440,9 +426,7 @@ public class BukkitBlockManager extends AbstractBlockManager { Map behaviors = MiscUtils.castToMap(section.getOrDefault("behavior", Map.of()), false); - CustomBlock block; - try { - block = BukkitCustomBlock.builder(id) + CustomBlock block = BukkitCustomBlock.builder(id) .appearances(appearances) .variantMapper(variants) .lootTable(lootTable) @@ -450,11 +434,6 @@ public class BukkitBlockManager extends AbstractBlockManager { .settings(settings) .behavior(behaviors) .build(); - } catch (LocalizedResourceConfigException e) { - e.setPath(path); - e.setId(id); - throw e; - } // bind appearance and real state for (ImmutableBlockState state : block.variantProvider().states()) { diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureManager.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureManager.java index 09f9f50bf..fea03cbee 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureManager.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureManager.java @@ -157,14 +157,8 @@ public class BukkitFurnitureManager extends AbstractFurnitureManager { List> hitboxConfigs = (List>) placementArguments.getOrDefault("hitboxes", List.of()); List hitboxes = new ArrayList<>(); for (Map config : hitboxConfigs) { - try { - HitBox hitBox = HitBoxTypes.fromMap(config); - hitboxes.add(hitBox); - } catch (LocalizedResourceConfigException e) { - e.setPath(path); - e.setId(id); - throw e; - } + HitBox hitBox = HitBoxTypes.fromMap(config); + hitboxes.add(hitBox); } if (hitboxes.isEmpty() && externalModel.isEmpty()) { hitboxes.add(InteractionHitBox.DEFAULT); @@ -198,24 +192,10 @@ public class BukkitFurnitureManager extends AbstractFurnitureManager { } // get furniture settings - FurnitureSettings settings; - try { - settings = FurnitureSettings.fromMap(settingsMap); - } catch (LocalizedResourceConfigException e) { - e.setPath(path); - e.setId(id); - throw e; - } + FurnitureSettings settings = FurnitureSettings.fromMap(settingsMap); // get loot table - LootTable lootTable; - try { - lootTable = lootMap == null ? null : LootTable.fromMap(lootMap); - } catch (LocalizedResourceConfigException e) { - e.setPath(path); - e.setId(id); - throw e; - } + LootTable lootTable = lootMap == null ? null : LootTable.fromMap(lootMap); CustomFurniture furniture = new CustomFurniture(id, settings, placements, lootTable); byId.put(id, furniture); } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/BukkitItemManager.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/BukkitItemManager.java index a2a182468..795f9d10f 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/BukkitItemManager.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/BukkitItemManager.java @@ -339,15 +339,7 @@ public class BukkitItemManager extends AbstractItemManager { } } - ItemSettings itemSettings; - try { - itemSettings = ItemSettings.fromMap(MiscUtils.castToMap(section.get("settings"), true)); - } catch (LocalizedResourceConfigException e) { - e.setPath(path); - e.setId(id); - throw e; - } - + ItemSettings itemSettings = ItemSettings.fromMap(MiscUtils.castToMap(section.get("settings"), true)); if (isVanillaItem) { itemSettings.canPlaceRelatedVanillaBlock(true); } @@ -391,15 +383,7 @@ public class BukkitItemManager extends AbstractItemManager { return; } - ItemModel model; - try { - model = ItemModels.fromMap(modelSection); - } catch (LocalizedResourceConfigException e) { - e.setPath(path); - e.setId(id); - throw e; - } - + ItemModel model = ItemModels.fromMap(modelSection); boolean hasModel = false; if (customModelData != 0) { hasModel= true; diff --git a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/AbstractRecipeManager.java b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/AbstractRecipeManager.java index f12d49c16..e8b3190ef 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/item/recipe/AbstractRecipeManager.java +++ b/core/src/main/java/net/momirealms/craftengine/core/item/recipe/AbstractRecipeManager.java @@ -159,14 +159,7 @@ public abstract class AbstractRecipeManager implements RecipeManager { if (AbstractRecipeManager.this.byId.containsKey(id)) { throw new LocalizedResourceConfigException("warning.config.recipe.duplicated", path, id); } - Recipe recipe; - try { - recipe = RecipeTypes.fromMap(id, section); - } catch (LocalizedResourceConfigException e) { - e.setPath(path); - e.setId(id); - throw e; - } + Recipe recipe = RecipeTypes.fromMap(id, section); try { markAsCustomRecipe(id); registerInternalRecipe(id, recipe); diff --git a/core/src/main/java/net/momirealms/craftengine/core/pack/AbstractPackManager.java b/core/src/main/java/net/momirealms/craftengine/core/pack/AbstractPackManager.java index ddd89785d..566ee6b75 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/pack/AbstractPackManager.java +++ b/core/src/main/java/net/momirealms/craftengine/core/pack/AbstractPackManager.java @@ -450,20 +450,27 @@ public abstract class AbstractPackManager implements PackManager { String key = configEntry.getKey(); try { Key id = Key.withDefaultNamespace(key, cached.pack().namespace()); - if (parser.isTemplate()) { - this.plugin.templateManager().addTemplate(cached.pack(), cached.filePath(), id, configEntry.getValue()); - } else if (predicate.test(parser)) { - if (configEntry.getValue() instanceof Map configSection0) { - Map configSection1 = castToMap(configSection0, false); - if ((boolean) configSection1.getOrDefault("enable", true)) { - parser.parseSection(cached.pack(), cached.filePath(), id, plugin.templateManager().applyTemplates(configSection1)); + try { + if (parser.isTemplate()) { + this.plugin.templateManager().addTemplate(cached.pack(), cached.filePath(), id, configEntry.getValue()); + } else if (predicate.test(parser)) { + if (configEntry.getValue() instanceof Map configSection0) { + Map configSection1 = castToMap(configSection0, false); + if ((boolean) configSection1.getOrDefault("enable", true)) { + parser.parseSection(cached.pack(), cached.filePath(), id, plugin.templateManager().applyTemplates(configSection1)); + } + } else { + TranslationManager.instance().log("warning.config.not_a_section", cached.filePath().toString(), parser.sectionId()[0], configEntry.getKey(), configEntry.getValue().getClass().getSimpleName()); } - } else { - TranslationManager.instance().log("warning.config.not_a_section", cached.filePath().toString(), parser.sectionId()[0], configEntry.getKey(), configEntry.getValue().getClass().getSimpleName()); } + } catch (LocalizedException e) { + if (e instanceof LocalizedResourceConfigException exception) { + exception.setPath(cached.filePath()); + exception.setId(cached.prefix() + "." + key); + } + TranslationManager.instance().log(e.node(), e.arguments()); + this.plugin.debug(e::node); } - } catch (LocalizedException e) { - TranslationManager.instance().log(e.node(), e.arguments()); } catch (Exception e) { this.plugin.logger().warn("Unexpected error loading file " + cached.filePath() + " - '" + parser.sectionId()[0] + "." + key + "'. Please find the cause according to the stacktrace or seek developer help.", e); } @@ -482,7 +489,7 @@ public abstract class AbstractPackManager implements PackManager { String configType = hashIndex != -1 ? key.substring(0, hashIndex) : key; Optional.ofNullable(this.sectionParsers.get(configType)) .ifPresent(parser -> this.cachedConfigs.computeIfAbsent(parser, k -> new ArrayList<>()) - .add(new CachedConfig(castToMap(typeSections0, false), path, pack))); + .add(new CachedConfig(key, castToMap(typeSections0, false), path, pack))); } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/pack/CachedConfig.java b/core/src/main/java/net/momirealms/craftengine/core/pack/CachedConfig.java index 808c649f0..e98faf8f8 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/pack/CachedConfig.java +++ b/core/src/main/java/net/momirealms/craftengine/core/pack/CachedConfig.java @@ -6,12 +6,14 @@ import java.util.Map; public class CachedConfig { private final Pack pack; private final Path filePath; + private final String prefix; private final Map config; - public CachedConfig(Map config, Path filePath, Pack pack) { + public CachedConfig(String prefix, Map config, Path filePath, Pack pack) { this.config = config; this.filePath = filePath; this.pack = pack; + this.prefix = prefix; } public Map config() { @@ -25,4 +27,8 @@ public class CachedConfig { public Pack pack() { return pack; } + + public String prefix() { + return prefix; + } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/locale/LocalizedResourceConfigException.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/locale/LocalizedResourceConfigException.java index 04f239989..994e4c785 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/locale/LocalizedResourceConfigException.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/locale/LocalizedResourceConfigException.java @@ -37,7 +37,7 @@ public class LocalizedResourceConfigException extends LocalizedException { super.setArgument(0, path.toString()); } - public void setId(Key id) { - super.setArgument(1, id.toString()); + public void setId(String id) { + super.setArgument(1, id); } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/util/CharacterUtils.java b/core/src/main/java/net/momirealms/craftengine/core/util/CharacterUtils.java index 5d00bb55e..6a07d4f57 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/util/CharacterUtils.java +++ b/core/src/main/java/net/momirealms/craftengine/core/util/CharacterUtils.java @@ -23,7 +23,7 @@ public class CharacterUtils { chars[i] = (char) codePoint; } } catch (NumberFormatException e) { - throw new LocalizedResourceConfigException(""); + throw new LocalizedResourceConfigException("warning.config.image.invalid_hex", e, hex); } } return chars;