9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-28 19:39:11 +00:00

尝试一下新的异常方式

This commit is contained in:
XiaoMoMi
2025-04-27 20:58:31 +08:00
parent a49e488165
commit 4d8c2fdb77
7 changed files with 72 additions and 18 deletions

View File

@@ -85,6 +85,8 @@ warning.config.furniture.duplicated: "<yellow>Issue found in file <arg:0> - Dupl
warning.config.furniture.lack_placement: "<yellow>Issue found in file <arg:0> - The furniture '<arg:1>' is missing the required 'placement' argument.</yellow>"
warning.config.furniture.element.lack_item: "<yellow>Issue found in file <arg:0> - The furniture '<arg:1>' is missing the required 'item' argument for one of its elements.</yellow>"
warning.config.furniture.settings.unknown: "<yellow>Issue found in file <arg:0> - The furniture '<arg:1>' is using an unknown setting type '<arg:2>'.</yellow>"
warning.config.furniture.hitbox.invalid_type: "<yellow>Issue found in file <arg:0> - The furniture '<arg:1>' is using an invalid hitbox type '<arg:2>'.</yellow>"
warning.config.furniture.hitbox.custom.invalid_entity: "<yellow>Issue found in file <arg:0> - The furniture '<arg:1>' is using a custom hitbox with invalid entity type '<arg:2>'.</yellow>"
warning.config.item.duplicated: "<yellow>Issue found in file <arg:0> - Duplicated item '<arg:1>'.</yellow>"
warning.config.item.lack_material: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the required 'material' argument.</yellow>"
warning.config.item.invalid_material: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is using an invalid material type '<arg:2>'.</yellow>"

View File

@@ -7,8 +7,7 @@ import net.momirealms.craftengine.core.advancement.AbstractAdvancementManager;
import net.momirealms.craftengine.core.pack.LoadingSequence;
import net.momirealms.craftengine.core.pack.Pack;
import net.momirealms.craftengine.core.plugin.config.ConfigSectionParser;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.plugin.locale.TranslationManager;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.GsonHelper;
import net.momirealms.craftengine.core.util.Key;
@@ -52,7 +51,7 @@ public class BukkitAdvancementManager extends AbstractAdvancementManager {
@Override
public void parseSection(Pack pack, Path path, Key id, Map<String, Object> section) {
if (advancements.containsKey(id)) {
throw new LocalizedException("warning.config.advancement.duplicated", path.toString(), id.toString());
throw new LocalizedResourceConfigException("warning.config.advancement.duplicated", path, id);
}
JsonElement jsonTree = GsonHelper.get().toJsonTree(section);
FastNMS.INSTANCE.registerAdvancement(id.decompose(), jsonTree);

View File

@@ -12,8 +12,7 @@ import net.momirealms.craftengine.core.pack.LoadingSequence;
import net.momirealms.craftengine.core.pack.Pack;
import net.momirealms.craftengine.core.plugin.config.Config;
import net.momirealms.craftengine.core.plugin.config.ConfigSectionParser;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.plugin.locale.TranslationManager;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.sound.SoundData;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
@@ -107,14 +106,14 @@ public class BukkitFurnitureManager extends AbstractFurnitureManager {
@Override
public void parseSection(Pack pack, Path path, Key id, Map<String, Object> section) {
if (byId.containsKey(id)) {
throw new LocalizedException("warning.config.furniture.duplicated", path.toString(), id.toString());
throw new LocalizedResourceConfigException("warning.config.furniture.duplicated", path, id);
}
Map<String, Object> lootMap = MiscUtils.castToMap(section.get("loot"), true);
Map<String, Object> settingsMap = MiscUtils.castToMap(section.get("settings"), true);
Map<String, Object> placementMap = MiscUtils.castToMap(section.get("placement"), true);
if (placementMap == null) {
throw new LocalizedException("warning.config.furniture.lack_placement", path.toString(), id.toString());
throw new LocalizedResourceConfigException("warning.config.furniture.lack_placement", path, id);
}
EnumMap<AnchorType, CustomFurniture.Placement> placements = new EnumMap<>(AnchorType.class);
@@ -130,7 +129,7 @@ public class BukkitFurnitureManager extends AbstractFurnitureManager {
for (Map<String, Object> element : elementConfigs) {
String key = (String) element.get("item");
if (key == null) {
throw new LocalizedException("warning.config.furniture.element.lack_item", path.toString(), id.toString());
throw new LocalizedResourceConfigException("warning.config.furniture.element.lack_item", path, id);
}
ItemDisplayContext transform = ItemDisplayContext.valueOf(element.getOrDefault("transform", "NONE").toString().toUpperCase(Locale.ENGLISH));
Billboard billboard = Billboard.valueOf(element.getOrDefault("billboard", "FIXED").toString().toUpperCase(Locale.ENGLISH));
@@ -157,8 +156,14 @@ public class BukkitFurnitureManager extends AbstractFurnitureManager {
List<Map<String, Object>> hitboxConfigs = (List<Map<String, Object>>) placementArguments.getOrDefault("hitboxes", List.of());
List<HitBox> hitboxes = new ArrayList<>();
for (Map<String, Object> config : hitboxConfigs) {
HitBox hitBox = HitBoxTypes.fromMap(config);
hitboxes.add(hitBox);
try {
HitBox hitBox = HitBoxTypes.fromMap(config);
hitboxes.add(hitBox);
} catch (LocalizedResourceConfigException e) {
e.setPath(path);
e.setId(id);
throw e;
}
}
if (hitboxes.isEmpty() && externalModel.isEmpty()) {
hitboxes.add(InteractionHitBox.DEFAULT);
@@ -195,9 +200,9 @@ public class BukkitFurnitureManager extends AbstractFurnitureManager {
FurnitureSettings settings;
try {
settings = FurnitureSettings.fromMap(settingsMap);
} catch (LocalizedException e) {
e.setArgument(0, path.toString());
e.setArgument(1, id.toString());
} catch (LocalizedResourceConfigException e) {
e.setPath(path);
e.setId(id);
throw e;
}
CustomFurniture furniture = new CustomFurniture(id, settings, placements, lootMap == null ? null : LootTable.fromMap(lootMap));

View File

@@ -4,6 +4,7 @@ import net.momirealms.craftengine.bukkit.entity.data.BaseEntityData;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.core.entity.furniture.*;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
import net.momirealms.craftengine.core.util.VersionHelper;
@@ -82,9 +83,10 @@ public class CustomHitBox extends AbstractHitBox {
public HitBox create(Map<String, Object> arguments) {
Vector3f position = MiscUtils.getVector3f(arguments.getOrDefault("position", "0"));
float scale = MiscUtils.getAsFloat(arguments.getOrDefault("scale", "1"));
EntityType entityType = Registry.ENTITY_TYPE.get(new NamespacedKey("minecraft", (String) arguments.getOrDefault("entity-type", "slime")));
String type = (String) arguments.getOrDefault("entity-type", "slime");
EntityType entityType = Registry.ENTITY_TYPE.get(new NamespacedKey("minecraft", type));
if (entityType == null) {
throw new IllegalArgumentException("EntityType not found: " + arguments.get("entity-type"));
throw new LocalizedResourceConfigException("warning.config.furniture.hitbox.custom.invalid_entity", new IllegalArgumentException("EntityType not found: " + type), type);
}
boolean canBeHitByProjectile = (boolean) arguments.getOrDefault("can-be-hit-by-projectile", false);
boolean blocksBuilding = (boolean) arguments.getOrDefault("blocks-building", true);

View File

@@ -1,6 +1,6 @@
package net.momirealms.craftengine.core.entity.furniture;
import net.momirealms.craftengine.core.plugin.locale.LocalizedException;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
import org.jetbrains.annotations.Nullable;
@@ -39,7 +39,7 @@ public class FurnitureSettings {
if (factory != null) {
factory.createModifier(entry.getValue()).apply(settings);
} else {
throw new LocalizedException("warning.config.furniture.settings.unknown", new RuntimeException("Unknown furniture settings: " + entry.getKey()), "null", "null", entry.getKey());
throw new LocalizedResourceConfigException("warning.config.furniture.settings.unknown", new RuntimeException("Unknown furniture settings: " + entry.getKey()), entry.getKey());
}
}
return settings;
@@ -73,10 +73,12 @@ public class FurnitureSettings {
return this;
}
@FunctionalInterface
public interface Modifier {
void apply(FurnitureSettings settings);
@FunctionalInterface
interface Factory {
FurnitureSettings.Modifier createModifier(Object value);

View File

@@ -1,5 +1,6 @@
package net.momirealms.craftengine.core.entity.furniture;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.Registries;
@@ -26,7 +27,7 @@ public class HitBoxTypes {
Key type = Optional.ofNullable((String) arguments.get("type")).map(Key::of).orElse(HitBoxTypes.INTERACTION);
HitBoxFactory factory = BuiltInRegistries.HITBOX_FACTORY.getValue(type);
if (factory == null) {
throw new IllegalArgumentException("Unknown hitbox type: " + type);
throw new LocalizedResourceConfigException("warning.config.furniture.hitbox.invalid_type", new IllegalArgumentException("Unknown hitbox type: " + type), type.toString());
}
return factory.create(arguments);
}

View File

@@ -0,0 +1,43 @@
package net.momirealms.craftengine.core.plugin.locale;
import net.momirealms.craftengine.core.util.ArrayUtils;
import net.momirealms.craftengine.core.util.Key;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.nio.file.Path;
public class LocalizedResourceConfigException extends LocalizedException {
public LocalizedResourceConfigException(
@NotNull String node,
@Nullable Exception cause,
@Nullable String... arguments
) {
super(node, cause, ArrayUtils.merge(new String[] {"null", "null"}, arguments));
}
public LocalizedResourceConfigException(
@NotNull String node,
@Nullable String... arguments
) {
super(node, (Exception) null, ArrayUtils.merge(new String[] {"null", "null"}, arguments));
}
public LocalizedResourceConfigException(
@NotNull String node,
@NotNull Path path,
@NotNull Key id,
@Nullable String... arguments
) {
super(node, (Exception) null, ArrayUtils.merge(new String[] {path.toString(), id.toString()}, arguments));
}
public void setPath(Path path) {
super.setArgument(0, path.toString());
}
public void setId(Key id) {
super.setArgument(1, id.toString());
}
}