9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-27 02:49:15 +00:00

改进模型读取

This commit is contained in:
XiaoMoMi
2025-05-18 18:00:15 +08:00
parent eba3188c82
commit 11ab46bc8e
5 changed files with 114 additions and 25 deletions

View File

@@ -145,14 +145,18 @@ warning.config.item.duplicate: "<yellow>Issue found in file <arg:0> - Duplicated
warning.config.item.settings.unknown: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is using an unknown setting type '<arg:2>'.</yellow>"
warning.config.item.missing_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>"
warning.config.item.invalid_custom_model_data: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is using a negative custom model data '<arg:2>' which is invalid.</yellow>"
warning.config.item.bad_custom_model_data: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is using a custom model data '<arg:2>' that is too large. It's recommended to use a value lower than 16,777,216.</yellow>"
warning.config.item.custom_model_data_conflict: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is using a custom model data '<arg:2>' that has been occupied by item '<arg:3>'.</yellow>"
warning.config.item.missing_model_id: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the required 'custom-model-data' or 'item-model' argument.</yellow>"
warning.config.item.missing_model: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the required 'model' section for 1.21.4+ resource pack support.</yellow>"
warning.config.item.behavior.missing_type: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the required 'type' argument for its item behavior.</yellow>"
warning.config.item.behavior.invalid_type: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is using an invalid item behavior type '<arg:2>'.</yellow>"
warning.config.item.behavior.block.missing_block: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the required 'block' argument for 'block_item' behavior.</yellow>"
warning.config.item.behavior.furniture.missing_furniture: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the required 'furniture' argument for 'furniture_item' behavior.</yellow>"
warning.config.item.behavior.liquid_collision.missing_block: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the required 'block' argument for 'liquid_collision_block_item' behavior.</yellow>"
warning.config.item.legacy_model.missing_path: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the require 'path' argument for legacy-model.</yellow>"
warning.config.item.legacy_model.cannot_convert: "<yellow>Issue found in file <arg:0> - Cannot convert 1.21.4+ items to legacy format for item '<arg:1>'. Please manually create 'legacy-model' section for this item.</yellow>"
warning.config.item.model.invalid_type: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is using an invalid model type '<arg:2>'.</yellow>"
warning.config.item.model.tint.missing_type: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is missing the required 'type' argument for tint.</yellow>"
warning.config.item.model.tint.invalid_type: "<yellow>Issue found in file <arg:0> - The item '<arg:1>' is using an invalid tint type '<arg:2>'.</yellow>"

View File

@@ -143,6 +143,7 @@ warning.config.item.duplicate: "<yellow>在文件 <arg:0> 发现问题 - 重复
warning.config.item.settings.unknown: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 使用了未知的设置类型 '<arg:2>'</yellow>"
warning.config.item.missing_material: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 缺少必需的 'material' 参数</yellow>"
warning.config.item.invalid_material: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 使用了无效的材料类型 '<arg:2>'</yellow>"
warning.config.item.invalid_custom_model_data: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 使用了无效的负数模型值 '<arg:2>'.</yellow>"
warning.config.item.bad_custom_model_data: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 使用的自定义模型数据 '<arg:2>' 数值过大 建议使用小于 16,777,216 的值</yellow>"
warning.config.item.custom_model_data_conflict: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 使用的自定义模型数据 '<arg:2>' 已被物品 '<arg:3>' 占用</yellow>"
warning.config.item.missing_model_id: "<yellow>在文件 <arg:0> 发现问题 - 物品 '<arg:1>' 缺少必需的 'custom-model-data' 或 'item-model' 参数</yellow>"

View File

@@ -56,7 +56,6 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
private final DebugStickListener debugStickListener;
private final ArmorEventListener armorEventListener;
public BukkitItemManager(BukkitCraftEngine plugin) {
super(plugin);
instance = this;
@@ -131,7 +130,7 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
@Override
public Optional<BuildableItem<ItemStack>> getVanillaItem(Key key) {
Material material = Registry.MATERIAL.get(Objects.requireNonNull(NamespacedKey.fromString(key.toString())));
Material material = Registry.MATERIAL.get(KeyUtils.toNamespacedKey(key));
if (material == null) {
return Optional.empty();
}

View File

@@ -17,6 +17,7 @@ import net.momirealms.craftengine.core.plugin.config.Config;
import net.momirealms.craftengine.core.plugin.config.ConfigParser;
import net.momirealms.craftengine.core.plugin.event.EventFunctions;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.plugin.locale.TranslationManager;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.WritableRegistry;
@@ -270,7 +271,7 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
@Override
public void parseSection(Pack pack, Path path, Key id, Map<String, Object> section) {
if (customItems.containsKey(id)) {
if (AbstractItemManager.this.customItems.containsKey(id)) {
throw new LocalizedResourceConfigException("warning.config.item.duplicate");
}
@@ -282,7 +283,12 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
boolean isVanillaItem = isVanillaItem(id);
Key material = Key.from(isVanillaItem ? id.value() : ResourceConfigUtils.requireNonEmptyStringOrThrow(section.get("material"), "warning.config.item.missing_material"));
int customModelData = ResourceConfigUtils.getAsInt(section.getOrDefault("custom-model-data", 0), "custom-model-data");
// TODO give warnings if negative
if (customModelData < 0) {
throw new LocalizedResourceConfigException("warning.config.item.invalid_custom_model_data", String.valueOf(customModelData));
}
if (customModelData > 16_777_216) {
throw new LocalizedResourceConfigException("warning.config.item.bad_custom_model_data", String.valueOf(customModelData));
}
Key itemModelKey = null;
@@ -306,7 +312,7 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
}
}
if (hasItemModelSection) {
if (hasItemModelSection && VersionHelper.isOrAbove1_21_2()) {
itemModelKey = Key.from(section.get("item-model").toString());
itemBuilder.dataModifier(new ItemModelModifier<>(itemModelKey));
}
@@ -338,28 +344,67 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
// model part, can be null
// but if it exists, either custom model data or item model should be configured
Map<String, Object> modelSection = MiscUtils.castToMap(section.get("model"), true);
if (modelSection == null) {
Map<String, Object> legacyModelSection = MiscUtils.castToMap(section.get("legacy-model"), true);
if (modelSection == null && legacyModelSection == null) {
return;
}
if (customModelData <= 0 && itemModelKey == null) {
// 如果设置了model但是没有模型值
if (customModelData == 0 && itemModelKey == null) {
throw new LocalizedResourceConfigException("warning.config.item.missing_model_id");
}
ItemModel model = ItemModels.fromMap(modelSection);
for (ModelGeneration generation : model.modelsToGenerate()) {
prepareModelGeneration(generation);
// 1.21.4+必须要配置model区域
if (Config.packMaxVersion() >= 21.4f && modelSection == null) {
throw new LocalizedResourceConfigException("warning.config.item.missing_model");
}
// 新版格式
ItemModel modernModel = null;
// 旧版格式
TreeSet<LegacyOverridesModel> legacyOverridesModels = null;
if (Config.packMinVersion() < 21.4f) {
legacyOverridesModels = new TreeSet<>();
if (section.containsKey("legacy-model")) {
if (Config.packMaxVersion() >= 21.4f) {
modernModel = ItemModels.fromMap(modelSection);
for (ModelGeneration generation : modernModel.modelsToGenerate()) {
prepareModelGeneration(generation);
}
// 如果最低支持版本低于1.21.4则需要准备旧版overrides模型
if (Config.packMinVersion() < 21.4f) {
// 如果有旧版格式,就用旧版
if (legacyModelSection != null) {
LegacyItemModel legacyItemModel = LegacyItemModel.fromMap(legacyModelSection);
for (ModelGeneration generation : legacyItemModel.modelsToGenerate()) {
prepareModelGeneration(generation);
}
legacyOverridesModels = new TreeSet<>(legacyItemModel.overrides());
} else {
// 否则把新版格式并转换为旧版
legacyOverridesModels = new TreeSet<>();
processModelRecursively(modernModel, new LinkedHashMap<>(), legacyOverridesModels, material, customModelData);
if (legacyOverridesModels.isEmpty()) {
TranslationManager.instance().log("warning.config.item.legacy_model.cannot_convert", path.toString(), id.asString());
}
}
}
}
// 最高支持版本不超过1.21.4所以新版model格式为非必需
else {
// 如果有旧版格式,就用旧版
if (legacyModelSection != null) {
LegacyItemModel legacyItemModel = LegacyItemModel.fromMap(legacyModelSection);
for (ModelGeneration generation : legacyItemModel.modelsToGenerate()) {
prepareModelGeneration(generation);
}
legacyOverridesModels = new TreeSet<>(legacyItemModel.overrides());
} else {
// 否则读新版格式并转换为旧版
ItemModel model = ItemModels.fromMap(modelSection);
for (ModelGeneration generation : model.modelsToGenerate()) {
prepareModelGeneration(generation);
}
legacyOverridesModels = new TreeSet<>();
processModelRecursively(model, new LinkedHashMap<>(), legacyOverridesModels, material, customModelData);
if (legacyOverridesModels.isEmpty()) {
// TODO give warnings
plugin.debug(() -> "Can't convert " + id + "'s model to legacy format.");
TranslationManager.instance().log("warning.config.item.legacy_model.cannot_convert", path.toString(), id.asString());
}
}
}
@@ -372,16 +417,13 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
if (conflict.containsKey(customModelData)) {
throw new LocalizedResourceConfigException("warning.config.item.custom_model_data_conflict", String.valueOf(customModelData), conflict.get(customModelData).toString());
}
if (customModelData > 16_777_216) {
throw new LocalizedResourceConfigException("warning.config.item.bad_custom_model_data", String.valueOf(customModelData));
}
conflict.put(customModelData, id);
// Parse models
if (Config.packMaxVersion() >= 21.4f) {
if (Config.packMaxVersion() >= 21.4f && modernModel != null) {
TreeMap<Integer, ItemModel> map = modernOverrides.computeIfAbsent(material, k -> new TreeMap<>());
map.put(customModelData, model);
map.put(customModelData, modernModel);
}
if (Config.packMinVersion() < 21.4f) {
if (Config.packMinVersion() < 21.4f && legacyOverridesModels != null && !legacyOverridesModels.isEmpty()) {
TreeSet<LegacyOverridesModel> lom = legacyOverrides.computeIfAbsent(material, k -> new TreeSet<>());
lom.addAll(legacyOverridesModels);
}
@@ -389,10 +431,10 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
// use item model
if (itemModelKey != null) {
if (Config.packMaxVersion() >= 21.4f) {
modernItemModels1_21_4.put(itemModelKey, model);
if (Config.packMaxVersion() >= 21.4f && modernModel != null) {
modernItemModels1_21_4.put(itemModelKey, modernModel);
}
if (Config.packMaxVersion() >= 21.2f && Config.packMinVersion() < 21.4f) {
if (Config.packMaxVersion() >= 21.2f && Config.packMinVersion() < 21.4f && legacyOverridesModels != null && !legacyOverridesModels.isEmpty()) {
modernItemModels1_21_2.put(itemModelKey, legacyOverridesModels);
}
}

View File

@@ -0,0 +1,43 @@
package net.momirealms.craftengine.core.pack.model;
import net.momirealms.craftengine.core.pack.model.generation.ModelGeneration;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.List;
import java.util.Map;
public class LegacyItemModel {
private final List<ModelGeneration> modelsToGenerate;
private final String path;
private final List<LegacyOverridesModel> overrides;
public LegacyItemModel(String path, List<LegacyOverridesModel> overrides, List<ModelGeneration> modelsToGenerate) {
this.modelsToGenerate = modelsToGenerate;
this.path = path;
this.overrides = overrides;
}
public List<ModelGeneration> modelsToGenerate() {
return modelsToGenerate;
}
public List<LegacyOverridesModel> overrides() {
return overrides;
}
public String path() {
return path;
}
public static LegacyItemModel fromMap(Map<String, Object> legacyModel) {
String legacyModelPath = ResourceConfigUtils.requireNonEmptyStringOrThrow(legacyModel.get("path"), "warning.config.item.legacy_model.missing_path");
Map<String, Object> generation = MiscUtils.castToMap(legacyModel.get("generation"), true);
ModelGeneration modelGeneration = null;
if (generation != null) {
modelGeneration = ModelGeneration.of(Key.of(legacyModelPath), generation);
}
LegacyOverridesModel legacyOverridesModel = new LegacyOverridesModel();
}
}