119 lines
5.3 KiB
Java
119 lines
5.3 KiB
Java
package tckubejsbridge.kubejs;
|
|
|
|
import com.google.gson.JsonObject;
|
|
import dev.latvian.mods.kubejs.script.ConsoleJS;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import thaumcraft.api.ThaumcraftApi;
|
|
import thaumcraft.api.aspects.AspectList;
|
|
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
|
|
/**
|
|
* 桥接工具:将 KubeJS 脚本中解析出的配方参数实际注册到 Thaumcraft。
|
|
*
|
|
* 注意:TC 注魔/炼金/奥术配方全部走 {@link ThaumcraftApi#getCraftingRecipes()} 内存列表,
|
|
* 不会进入原版 RecipeManager;因此本类在脚本执行时直接调用 TC API 完成注册,
|
|
* 并生成一个无害的占位 JSON(避免 KubeJS 把空配方交给 RecipeManager 时报错)。
|
|
*/
|
|
public final class TCRecipeBridge {
|
|
|
|
private static final Set<String> REGISTERED_RESEARCH = new HashSet<>();
|
|
|
|
private TCRecipeBridge() {
|
|
}
|
|
|
|
/** 按研究键去重,防止 /reload 重复注册 */
|
|
private static boolean alreadyRegistered(String research) {
|
|
return !REGISTERED_RESEARCH.add(research);
|
|
}
|
|
|
|
/** 清空去重集合(供 KubeJS 事件在 /reload 时调用,允许重新注册) */
|
|
public static void clearRegistered() {
|
|
REGISTERED_RESEARCH.clear();
|
|
}
|
|
|
|
/**
|
|
* 按 research 键从 TC 配方列表中移除已有的注魔配方。
|
|
* 用于脚本"先删原配方,再注册新配方",实现真正替换。
|
|
*
|
|
* @return 是否至少移除了一条
|
|
*/
|
|
public static boolean removeInfusion(String research) {
|
|
boolean removed = ThaumcraftApi.getCraftingRecipes().removeIf(r ->
|
|
r instanceof thaumcraft.api.crafting.InfusionRecipe ir && research.equals(ir.research()));
|
|
if (removed) {
|
|
ConsoleJS.SERVER.info("TC-KubeJS-Bridge: removed infusion recipe(s) for research '" + research + "'");
|
|
}
|
|
// 允许该键后续重新注册
|
|
REGISTERED_RESEARCH.remove(research);
|
|
return removed;
|
|
}
|
|
|
|
/** 按 research 键从 TC 配方列表中移除已有的奥术(有序/无序)配方 */
|
|
public static boolean removeArcane(String research) {
|
|
boolean removed = ThaumcraftApi.getCraftingRecipes().removeIf(r -> {
|
|
if (r instanceof thaumcraft.api.crafting.ShapedArcaneRecipe sr) return research.equals(sr.research);
|
|
if (r instanceof thaumcraft.api.crafting.ShapelessArcaneRecipe slr) return research.equals(slr.research);
|
|
return false;
|
|
});
|
|
if (removed) {
|
|
ConsoleJS.SERVER.info("TC-KubeJS-Bridge: removed arcane recipe(s) for research '" + research + "'");
|
|
}
|
|
// 允许该键后续重新注册
|
|
REGISTERED_RESEARCH.remove(research);
|
|
return removed;
|
|
}
|
|
|
|
public static void registerInfusion(String research, ItemStack result, int instability,
|
|
AspectList aspects, ItemStack central, ItemStack... components) {
|
|
if (alreadyRegistered(research)) {
|
|
ConsoleJS.SERVER.info("TC-KubeJS-Bridge: infusion recipe '" + research + "' already registered, skipping");
|
|
return;
|
|
}
|
|
ThaumcraftApi.addInfusionCraftingRecipe(research, result, instability, aspects, central, components);
|
|
ConsoleJS.SERVER.info("TC-KubeJS-Bridge: registered infusion recipe '" + research + "'");
|
|
}
|
|
|
|
public static void registerCrucible(String research, ItemStack result, Object catalyst, AspectList aspects) {
|
|
if (alreadyRegistered(research)) {
|
|
ConsoleJS.SERVER.info("TC-KubeJS-Bridge: crucible recipe '" + research + "' already registered, skipping");
|
|
return;
|
|
}
|
|
ThaumcraftApi.addCrucibleRecipe(research, result, catalyst, aspects);
|
|
ConsoleJS.SERVER.info("TC-KubeJS-Bridge: registered crucible recipe '" + research + "'");
|
|
}
|
|
|
|
public static void registerArcane(String research, ItemStack result, AspectList aspects, Object... recipe) {
|
|
if (alreadyRegistered(research)) {
|
|
ConsoleJS.SERVER.info("TC-KubeJS-Bridge: arcane recipe '" + research + "' already registered, skipping");
|
|
return;
|
|
}
|
|
ThaumcraftApi.addArcaneCraftingRecipe(research, result, aspects, recipe);
|
|
ConsoleJS.SERVER.info("TC-KubeJS-Bridge: registered arcane recipe '" + research + "'");
|
|
}
|
|
|
|
public static void registerArcaneShapeless(String research, ItemStack result, AspectList aspects, Object... recipe) {
|
|
if (alreadyRegistered(research)) {
|
|
ConsoleJS.SERVER.info("TC-KubeJS-Bridge: arcane shapeless recipe '" + research + "' already registered, skipping");
|
|
return;
|
|
}
|
|
ThaumcraftApi.addShapelessArcaneCraftingRecipe(research, result, aspects, recipe);
|
|
ConsoleJS.SERVER.info("TC-KubeJS-Bridge: registered arcane shapeless recipe '" + research + "'");
|
|
}
|
|
|
|
/**
|
|
* 生成无害占位配方 JSON,替换 KubeJS 对自定义配方自动生成的 JSON,
|
|
* 使 RecipeManager 能正常解析但不会产生任何实际效果。
|
|
*/
|
|
public static JsonObject placeholderRecipe() {
|
|
JsonObject json = new JsonObject();
|
|
json.addProperty("type", "minecraft:crafting_shapeless");
|
|
json.add("ingredients", new com.google.gson.JsonArray());
|
|
JsonObject result = new JsonObject();
|
|
result.addProperty("item", "minecraft:air");
|
|
json.add("result", result);
|
|
return json;
|
|
}
|
|
}
|