From efa825c9d79e472aa24e50ead8dd87dca1b8b3fc Mon Sep 17 00:00:00 2001 From: Bruno Ploumhans <13494793+Technici4n@users.noreply.github.com> Date: Tue, 24 Dec 2024 14:23:34 +0100 Subject: [PATCH] Fix NPE in WaterPotionStorage#isWaterPotion (#4313) * Fix NPE in WaterPotionStorage#isWaterPotion * Make checkstyle happy --- .../fabric/impl/transfer/fluid/WaterPotionStorage.java | 10 +++------- .../fabric/test/transfer/unittests/FluidItemTests.java | 8 ++++++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/impl/transfer/fluid/WaterPotionStorage.java b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/impl/transfer/fluid/WaterPotionStorage.java index 922abf68e4..de790a8344 100644 --- a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/impl/transfer/fluid/WaterPotionStorage.java +++ b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/impl/transfer/fluid/WaterPotionStorage.java @@ -16,8 +16,6 @@ package net.fabricmc.fabric.impl.transfer.fluid; -import java.util.Optional; - import org.jetbrains.annotations.Nullable; import net.minecraft.component.DataComponentTypes; @@ -25,9 +23,7 @@ import net.minecraft.fluid.Fluids; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; -import net.minecraft.potion.Potion; import net.minecraft.potion.Potions; -import net.minecraft.registry.entry.RegistryEntry; import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext; import net.fabricmc.fabric.api.transfer.v1.fluid.FluidConstants; @@ -52,9 +48,9 @@ public static WaterPotionStorage find(ContainerItemContext context) { private static boolean isWaterPotion(ContainerItemContext context) { ItemVariant variant = context.getItemVariant(); - Optional potionContents = variant.getComponents().get(DataComponentTypes.POTION_CONTENTS); - RegistryEntry potion = potionContents.map(PotionContentsComponent::potion).orElse(null).orElse(null); - return variant.isOf(Items.POTION) && potion == Potions.WATER; + PotionContentsComponent potionContents = variant.getComponentMap() + .getOrDefault(DataComponentTypes.POTION_CONTENTS, PotionContentsComponent.DEFAULT); + return variant.isOf(Items.POTION) && potionContents.potion().orElse(null) == Potions.WATER; } private final ContainerItemContext context; diff --git a/fabric-transfer-api-v1/src/test/java/net/fabricmc/fabric/test/transfer/unittests/FluidItemTests.java b/fabric-transfer-api-v1/src/test/java/net/fabricmc/fabric/test/transfer/unittests/FluidItemTests.java index 16d7c2fc0a..8b0fdb98f2 100644 --- a/fabric-transfer-api-v1/src/test/java/net/fabricmc/fabric/test/transfer/unittests/FluidItemTests.java +++ b/fabric-transfer-api-v1/src/test/java/net/fabricmc/fabric/test/transfer/unittests/FluidItemTests.java @@ -173,6 +173,14 @@ public void testWaterPotion() { if (StorageUtil.findStoredResource(luckyStorage) != null) { throw new AssertionError("Found a resource in an unhandled potion."); } + + // Make sure extraction returns nothing for no potion at all + testInventory.setStack(0, new ItemStack(Items.POTION)); + Storage defaultStorage = new InventoryContainerItem(testInventory, 0).find(FluidStorage.ITEM); + + if (StorageUtil.findStoredResource(defaultStorage) != null) { + throw new AssertionError("Found a resource in empty potion."); + } } @Test