Skip to content

Commit

Permalink
Fix NPE in WaterPotionStorage#isWaterPotion (#4313)
Browse files Browse the repository at this point in the history
* Fix NPE in WaterPotionStorage#isWaterPotion

* Make checkstyle happy
  • Loading branch information
Technici4n authored Dec 24, 2024
1 parent cbf6036 commit efa825c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@

package net.fabricmc.fabric.impl.transfer.fluid;

import java.util.Optional;

import org.jetbrains.annotations.Nullable;

import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.PotionContentsComponent;
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;
Expand All @@ -52,9 +48,9 @@ public static WaterPotionStorage find(ContainerItemContext context) {

private static boolean isWaterPotion(ContainerItemContext context) {
ItemVariant variant = context.getItemVariant();
Optional<? extends PotionContentsComponent> potionContents = variant.getComponents().get(DataComponentTypes.POTION_CONTENTS);
RegistryEntry<Potion> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FluidVariant> defaultStorage = new InventoryContainerItem(testInventory, 0).find(FluidStorage.ITEM);

if (StorageUtil.findStoredResource(defaultStorage) != null) {
throw new AssertionError("Found a resource in empty potion.");
}
}

@Test
Expand Down

0 comments on commit efa825c

Please sign in to comment.