From 78f579d7d7684334646ee98cfdb7f124c4562a48 Mon Sep 17 00:00:00 2001 From: Light_Quanta Date: Sun, 6 Apr 2025 03:19:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0JEI=E8=81=94=E5=8A=A8?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jei/PotionMortarShellRecipeMaker.java | 45 +++++++++ .../compat/jei/SbwJEIPlugin.java | 93 +++++++++++++++++++ .../item/common/ammo/PotionMortarShell.java | 5 +- 3 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/atsuishio/superbwarfare/compat/jei/PotionMortarShellRecipeMaker.java create mode 100644 src/main/java/com/atsuishio/superbwarfare/compat/jei/SbwJEIPlugin.java diff --git a/src/main/java/com/atsuishio/superbwarfare/compat/jei/PotionMortarShellRecipeMaker.java b/src/main/java/com/atsuishio/superbwarfare/compat/jei/PotionMortarShellRecipeMaker.java new file mode 100644 index 000000000..2bb256e71 --- /dev/null +++ b/src/main/java/com/atsuishio/superbwarfare/compat/jei/PotionMortarShellRecipeMaker.java @@ -0,0 +1,45 @@ +package com.atsuishio.superbwarfare.compat.jei; + +import com.atsuishio.superbwarfare.init.ModItems; +import net.minecraft.core.Holder; +import net.minecraft.core.NonNullList; +import net.minecraft.core.component.DataComponents; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.item.alchemy.PotionContents; +import net.minecraft.world.item.crafting.*; + +import java.util.List; +import java.util.Optional; + +public class PotionMortarShellRecipeMaker { + + public static List> createRecipes() { + String group = "jei.potion_mortar_shell"; + Ingredient ingredient = Ingredient.of(new ItemStack(ModItems.MORTAR_SHELL.get())); + + return BuiltInRegistries.POTION.stream().map(potion -> { + ItemStack input = new ItemStack(Items.LINGERING_POTION); + input.set(DataComponents.POTION_CONTENTS, new PotionContents(Holder.direct(potion))); + + ItemStack output = new ItemStack(ModItems.POTION_MORTAR_SHELL.get(), 4); + output.set(DataComponents.POTION_CONTENTS, new PotionContents(Holder.direct(potion))); + + Ingredient potionIngredient = Ingredient.of(input); + NonNullList inputs = NonNullList.of(Ingredient.EMPTY, + Ingredient.EMPTY, ingredient, Ingredient.EMPTY, + ingredient, potionIngredient, ingredient, + Ingredient.EMPTY, ingredient, Ingredient.EMPTY + ); + + ResourceLocation id = ResourceLocation.withDefaultNamespace(group + "." + output.getDescriptionId()); + return new RecipeHolder<>(id, (CraftingRecipe) new ShapedRecipe(group, + CraftingBookCategory.MISC, + new ShapedRecipePattern(3, 3, inputs, Optional.empty()), + output + )); + }).toList(); + } +} diff --git a/src/main/java/com/atsuishio/superbwarfare/compat/jei/SbwJEIPlugin.java b/src/main/java/com/atsuishio/superbwarfare/compat/jei/SbwJEIPlugin.java new file mode 100644 index 000000000..c3de433b3 --- /dev/null +++ b/src/main/java/com/atsuishio/superbwarfare/compat/jei/SbwJEIPlugin.java @@ -0,0 +1,93 @@ +package com.atsuishio.superbwarfare.compat.jei; + +import com.atsuishio.superbwarfare.Mod; +import com.atsuishio.superbwarfare.init.ModItems; +import com.atsuishio.superbwarfare.tools.NBTTool; +import mezz.jei.api.IModPlugin; +import mezz.jei.api.JeiPlugin; +import mezz.jei.api.constants.RecipeTypes; +import mezz.jei.api.ingredients.subtypes.ISubtypeInterpreter; +import mezz.jei.api.ingredients.subtypes.UidContext; +import mezz.jei.api.registration.IRecipeRegistration; +import mezz.jei.api.registration.ISubtypeRegistration; +import net.minecraft.core.Holder; +import net.minecraft.core.component.DataComponents; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.alchemy.PotionContents; +import org.jetbrains.annotations.NotNull; + +import javax.annotation.ParametersAreNonnullByDefault; + +@JeiPlugin +public class SbwJEIPlugin implements IModPlugin { + + @Override + public @NotNull ResourceLocation getPluginUid() { + return Mod.loc("jei_plugin"); + } + + // TODO 正确注册subtypes + @Override + public void registerRecipes(@NotNull IRecipeRegistration registration) { + registration.addItemStackInfo(new ItemStack(ModItems.ANCIENT_CPU.get()), Component.translatable("jei.superbwarfare.ancient_cpu")); + registration.addItemStackInfo(new ItemStack(ModItems.CHARGING_STATION.get()), Component.translatable("jei.superbwarfare.charging_station")); + + var specialCraftingRecipes = PotionMortarShellRecipeMaker.createRecipes(); + registration.addRecipes(RecipeTypes.CRAFTING, specialCraftingRecipes); + } + + @Override + public void registerItemSubtypes(ISubtypeRegistration registration) { + registration.registerSubtypeInterpreter(ModItems.CONTAINER.get(), new ISubtypeInterpreter<>() { + @Override + @ParametersAreNonnullByDefault + public @NotNull Object getSubtypeData(ItemStack ingredient, UidContext context) { + var data = ingredient.get(DataComponents.BLOCK_ENTITY_DATA); + var tag = data != null ? data.copyTag() : new CompoundTag(); + if (tag.contains("EntityType")) { + return tag.getString("EntityType"); + } + return ""; + } + + @Override + @ParametersAreNonnullByDefault + public @NotNull String getLegacyStringSubtypeInfo(ItemStack ingredient, UidContext context) { + return (String) getSubtypeData(ingredient, context); + } + }); + + + registration.registerSubtypeInterpreter(ModItems.POTION_MORTAR_SHELL.get(), new ISubtypeInterpreter<>() { + @Override + @ParametersAreNonnullByDefault + public @NotNull Object getSubtypeData(ItemStack ingredient, UidContext context) { + var potion = ingredient.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY); + return potion.potion().map(Holder::getRegisteredName); + } + + @Override + @ParametersAreNonnullByDefault + public @NotNull String getLegacyStringSubtypeInfo(ItemStack ingredient, UidContext context) { + return (String) getSubtypeData(ingredient, context); + } + }); + + registration.registerSubtypeInterpreter(ModItems.C4_BOMB.get(), new ISubtypeInterpreter<>() { + @Override + @ParametersAreNonnullByDefault + public @NotNull Object getSubtypeData(ItemStack ingredient, UidContext context) { + return NBTTool.getTag(ingredient).getBoolean("Control"); + } + + @Override + @ParametersAreNonnullByDefault + public @NotNull String getLegacyStringSubtypeInfo(ItemStack ingredient, UidContext context) { + return (String) getSubtypeData(ingredient, context); + } + }); + } +} diff --git a/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/PotionMortarShell.java b/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/PotionMortarShell.java index 75d40614a..e8260df75 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/PotionMortarShell.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/common/ammo/PotionMortarShell.java @@ -13,7 +13,9 @@ import net.neoforged.api.distmarker.Dist; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.neoforge.client.event.RegisterColorHandlersEvent; +import org.jetbrains.annotations.NotNull; +import javax.annotation.ParametersAreNonnullByDefault; import java.util.List; @EventBusSubscriber(bus = EventBusSubscriber.Bus.MOD, value = Dist.CLIENT) @@ -24,13 +26,14 @@ public class PotionMortarShell extends MortarShell { } @Override - public ItemStack getDefaultInstance() { + public @NotNull ItemStack getDefaultInstance() { ItemStack itemstack = super.getDefaultInstance(); itemstack.set(DataComponents.POTION_CONTENTS, new PotionContents(Potions.POISON)); return itemstack; } @Override + @ParametersAreNonnullByDefault public void appendHoverText(ItemStack stack, Item.TooltipContext context, List tooltipComponents, TooltipFlag tooltipFlag) { PotionContents potioncontents = stack.get(DataComponents.POTION_CONTENTS); if (potioncontents != null) {