From cb9a97e97acb72f38297c6e84598d03ca6d99fc5 Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Wed, 2 Apr 2025 21:05:25 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E6=A4=8D=E8=8D=AF=E6=B0=B4=E5=BC=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../atsuishio/superbwarfare/init/ModTabs.java | 26 ++++++---- .../item/common/ammo/PotionMortarShell.java | 49 +++++++++++++------ 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/init/ModTabs.java b/src/main/java/com/atsuishio/superbwarfare/init/ModTabs.java index b14547660..b6abd5d6d 100644 --- a/src/main/java/com/atsuishio/superbwarfare/init/ModTabs.java +++ b/src/main/java/com/atsuishio/superbwarfare/init/ModTabs.java @@ -2,11 +2,16 @@ package com.atsuishio.superbwarfare.init; import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.item.*; +import net.minecraft.core.HolderLookup; import net.minecraft.core.registries.Registries; import net.minecraft.network.chat.Component; +import net.minecraft.world.flag.FeatureFlagSet; import net.minecraft.world.item.CreativeModeTab; import net.minecraft.world.item.CreativeModeTabs; +import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.alchemy.Potion; +import net.minecraft.world.item.alchemy.PotionContents; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent; @@ -52,9 +57,10 @@ public class ModTabs { } }); - // TODO potion mortar shell -// param.holders().lookup(Registries.POTION) -// .ifPresent(potion -> generatePotionEffectTypes(output, potion, ModItems.POTION_MORTAR_SHELL.get())); + param.holders().lookup(Registries.POTION) + .ifPresent(potion -> generatePotionEffectTypes(output, potion, ModItems.POTION_MORTAR_SHELL.get(), + CreativeModeTab.TabVisibility.PARENT_AND_SEARCH_TABS, + param.enabledFeatures())); }) .build()); @@ -97,10 +103,12 @@ public class ModTabs { } } - // TODO potion shell -// private static void generatePotionEffectTypes(CreativeModeTab.Output output, HolderLookup potions, Item potionItem) { -// potions.listElements().filter(potion -> !potion.is(Potions.EMPTY_ID)) -// .map(potion -> PotionUtils.setPotion(new ItemStack(potionItem), potion.value())) -// .forEach(output::accept); -// } + private static void generatePotionEffectTypes( + CreativeModeTab.Output output, HolderLookup potions, Item item, CreativeModeTab.TabVisibility visibility, FeatureFlagSet requiredFeatures + ) { + potions.listElements() + .filter(potion -> potion.value().isEnabled(requiredFeatures)) + .map(potion -> PotionContents.createItemStack(item, potion)) + .forEach(itemStack -> output.accept(itemStack, visibility)); + } } 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 0d8ca591a..75d40614a 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 @@ -1,25 +1,46 @@ package com.atsuishio.superbwarfare.item.common.ammo; -//@EventBusSubscriber(bus = EventBusSubscriber.Bus.MOD, value = Dist.CLIENT) +import com.atsuishio.superbwarfare.init.ModItems; +import net.minecraft.core.component.DataComponents; +import net.minecraft.network.chat.Component; +import net.minecraft.util.FastColor; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.TooltipFlag; +import net.minecraft.world.item.alchemy.PotionContents; +import net.minecraft.world.item.alchemy.Potions; +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 java.util.List; + +@EventBusSubscriber(bus = EventBusSubscriber.Bus.MOD, value = Dist.CLIENT) public class PotionMortarShell extends MortarShell { public PotionMortarShell() { super(); } - // TODO default instance -// @Override -// public ItemStack getDefaultInstance() { -// return PotionUtils.setPotion(super.getDefaultInstance(), Potions.POISON); -// } + @Override + public ItemStack getDefaultInstance() { + ItemStack itemstack = super.getDefaultInstance(); + itemstack.set(DataComponents.POTION_CONTENTS, new PotionContents(Potions.POISON)); + return itemstack; + } -// @Override -// public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List pTooltip, TooltipFlag pFlag) { -// PotionUtils.addPotionTooltip(pStack, pTooltip, 0.125F); -// } + @Override + public void appendHoverText(ItemStack stack, Item.TooltipContext context, List tooltipComponents, TooltipFlag tooltipFlag) { + PotionContents potioncontents = stack.get(DataComponents.POTION_CONTENTS); + if (potioncontents != null) { + potioncontents.addPotionTooltip(tooltipComponents::add, 0.125F, context.tickRate()); + } + } -// @SubscribeEvent -// public static void onRegisterColorHandlers(final RegisterColorHandlersEvent.Item event) { -// event.register((stack, layer) -> layer == 1 ? PotionUtils.getColor(stack) : -1, ModItems.POTION_MORTAR_SHELL.get()); -// } + @SubscribeEvent + public static void onRegisterColorHandlers(final RegisterColorHandlersEvent.Item event) { + event.register((stack, layer) -> layer == 1 ? FastColor.ARGB32.opaque(stack.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY).getColor()) : -1, + ModItems.POTION_MORTAR_SHELL.get()); + } }