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

Merge pull request #484 from jhqwqmc/fix/keep_components

修复锻造配方后处理器如果保留custom_data组件会写入错误的自定义物品ID的问题
This commit is contained in:
XiaoMoMi
2025-12-04 05:49:00 +08:00
committed by GitHub
3 changed files with 43 additions and 5 deletions

View File

@@ -162,6 +162,7 @@ warning.config.recipe.smithing_transform.post_processor.missing_type: "<yellow>I
warning.config.recipe.smithing_transform.post_processor.invalid_type: "<yellow>Issue found in file <arg:0> - The smithing transform recipe '<arg:1>' is using an invalid post processor type '<arg:2>'.</yellow>"
warning.config.recipe.smithing_transform.post_processor.keep_component.missing_components: "<yellow>Issue found in file <arg:0> - The smithing transform recipe '<arg:1>' is missing the required argument 'components' for post-processors 'keep_components'.</yellow>"
warning.config.recipe.smithing_transform.post_processor.keep_component.missing_tags: "<yellow>Issue found in file <arg:0> - The smithing transform recipe '<arg:1>' is missing the required argument 'tags' for post-processors 'keep_tags'.</yellow>"
warning.config.recipe.smithing_transform.post_processor.keep_custom_data.missing_paths: "<yellow>Issue found in file <arg:0> - The smithing transform recipe '<arg:1>' is missing the required argument 'paths' for post-processors 'keep_custom_data'.</yellow>"
warning.config.recipe.smithing_transform.missing_base: "<yellow>Issue found in file <arg:0> - The smithing transform recipe '<arg:1>' is missing the required 'base' argument.</yellow>"
warning.config.recipe.smithing_trim.missing_base: "<yellow>Issue found in file <arg:0> - The smithing trim recipe '<arg:1>' is missing the required 'base' argument.</yellow>"
warning.config.recipe.smithing_trim.missing_template_type: "<yellow>Issue found in file <arg:0> - The smithing trim recipe '<arg:1>' is missing the required 'template-type' argument.</yellow>"

View File

@@ -159,6 +159,7 @@ warning.config.recipe.smithing_transform.post_processor.missing_type: "<yellow>
warning.config.recipe.smithing_transform.post_processor.invalid_type: "<yellow>在文件 <arg:0> 发现问题 - 锻造升级配方 '<arg:1>' 使用了无效的后处理器类型 '<arg:2>'</yellow>"
warning.config.recipe.smithing_transform.post_processor.keep_component.missing_components: "<yellow>在文件 <arg:0> 发现问题 - 锻造升级配方 '<arg:1>' 的 'keep_components' 后处理器缺少必需的 'components' 参数</yellow>"
warning.config.recipe.smithing_transform.post_processor.keep_component.missing_tags: "<yellow>在文件 <arg:0> 发现问题 - 锻造升级配方 '<arg:1>' 的 'keep_tags' 后处理器缺少必需的 'tags' 参数</yellow>"
warning.config.recipe.smithing_transform.post_processor.keep_custom_data.missing_paths: "<yellow>在文件 <arg:0> 发现问题 - 锻造升级配方 '<arg:1>' 的 'keep_custom_data' 后处理器缺少必需的 'paths' 参数</yellow>"
warning.config.recipe.smithing_transform.missing_base: "<yellow>在文件 <arg:0> 发现问题 - 锻造升级配方 '<arg:1>' 缺少必需的 'base' 参数</yellow>"
warning.config.recipe.smithing_trim.missing_base: "<yellow>在文件 <arg:0> 发现问题 - 锻造纹饰配方 '<arg:1>' 缺少必需的 'base' 参数</yellow>"
warning.config.recipe.smithing_trim.missing_template_type: "<yellow>在文件 <arg:0> 发现问题 - 锻造纹饰配方 '<arg:1>' 缺少必需的 'template-type' 参数</yellow>"

View File

@@ -19,10 +19,7 @@ import net.momirealms.craftengine.core.util.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -232,10 +229,12 @@ public class CustomSmithingTransformRecipe<T> extends AbstractedFixedResultRecip
public static final Key KEEP_COMPONENTS = Key.of("craftengine:keep_components");
public static final Key KEEP_TAGS = Key.of("craftengine:keep_tags");
public static final Key MERGE_ENCHANTMENTS = Key.of("craftengine:merge_enchantments");
public static final Key KEEP_CUSTOM_DATA = Key.of("craftengine:keep_custom_data");
static {
if (VersionHelper.isOrAbove1_20_5()) {
register(KEEP_COMPONENTS, KeepComponents.FACTORY);
register(KEEP_CUSTOM_DATA, KeepCustomData.FACTORY);
} else {
register(KEEP_TAGS, KeepTags.FACTORY);
}
@@ -315,6 +314,42 @@ public class CustomSmithingTransformRecipe<T> extends AbstractedFixedResultRecip
}
}
public static class KeepCustomData implements ItemDataProcessor {
public static final Factory FACTORY = new Factory();
private final List<String[]> paths;
public KeepCustomData(List<String[]> data) {
this.paths = data;
}
@Override
public void accept(Item<?> item1, Item<?> item2, Item<?> item3) {
for (String[] path : this.paths) {
Object dataObj = item1.getJavaTag((Object[]) path);
if (dataObj != null) {
item3.setTag(dataObj, (Object[]) path);
}
}
}
@Override
public Key type() {
return ItemDataProcessors.KEEP_CUSTOM_DATA;
}
public static class Factory implements ProcessorFactory {
@Override
public ItemDataProcessor create(Map<String, Object> arguments) {
List<String> paths = MiscUtils.getAsStringList(ResourceConfigUtils.requireNonNullOrThrow(
arguments.get("paths"),
"warning.config.recipe.smithing_transform.post_processor.keep_custom_data.missing_paths")
);
return new KeepCustomData(paths.stream().map(it -> it.split("\\.")).toList());
}
}
}
public static class KeepComponents implements ItemDataProcessor {
public static final Factory FACTORY = new Factory();
private final List<Key> components;
@@ -339,6 +374,7 @@ public class CustomSmithingTransformRecipe<T> extends AbstractedFixedResultRecip
}
public static class Factory implements ProcessorFactory {
private static final Key CUSTOM_DATA = Key.of("minecraft", "custom_data");
@Override
public ItemDataProcessor create(Map<String, Object> arguments) {
@@ -347,7 +383,7 @@ public class CustomSmithingTransformRecipe<T> extends AbstractedFixedResultRecip
throw new LocalizedResourceConfigException("warning.config.recipe.smithing_transform.post_processor.keep_component.missing_components");
}
List<String> components = MiscUtils.getAsStringList(componentsObj);
return new KeepComponents(components.stream().map(Key::of).toList());
return new KeepComponents(components.stream().map(Key::of).filter(it -> !CUSTOM_DATA.equals(it)).toList());
}
}
}