From 620cceefae626c3f545d6cea87d3167a8278814c Mon Sep 17 00:00:00 2001 From: cech12 Date: Mon, 29 Jan 2024 12:57:36 +0100 Subject: [PATCH] fixed that the fluid capability in NeoForge was not working correctly --- CHANGELOG.md | 4 ++++ gradle.properties | 2 +- .../de/cech12/bucketlib/BucketLibMod.java | 22 ++++++++++++------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2aaed1c..0760a24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Forge Recommended Versioning](https://mcforge.readthedocs.io/en/latest/conventions/versioning/). +## [1.20.4-3.1.0.1] - 2024-01-29 +### Fixed +- Fluid capability in NeoForge was not working correctly + ## [1.20.4-3.1.0.0] - 2024-01-29 ### Changed - Update to Minecraft 1.20.4 (Forge 49.0.22, Neoforge 20.4.138-beta) diff --git a/gradle.properties b/gradle.properties index 9d3d29f..ed284c5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ # Project group=de.cech12.bucketlib -mod_version=3.1.0.0 +mod_version=3.1.0.1 mod_id=bucketlib mod_name=BucketLib mod_author=Cech12 diff --git a/neoforge/src/main/java/de/cech12/bucketlib/BucketLibMod.java b/neoforge/src/main/java/de/cech12/bucketlib/BucketLibMod.java index 598a292..292aec5 100644 --- a/neoforge/src/main/java/de/cech12/bucketlib/BucketLibMod.java +++ b/neoforge/src/main/java/de/cech12/bucketlib/BucketLibMod.java @@ -28,20 +28,24 @@ import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; import net.neoforged.fml.event.lifecycle.InterModProcessEvent; import net.neoforged.neoforge.capabilities.Capabilities; -import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent; +import net.neoforged.neoforge.capabilities.ICapabilityProvider; +import net.neoforged.neoforge.capabilities.ItemCapability; import net.neoforged.neoforge.client.extensions.common.IClientFluidTypeExtensions; import net.neoforged.neoforge.common.NeoForgeMod; import net.neoforged.neoforge.common.crafting.IngredientType; import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent; import net.neoforged.neoforge.fluids.FluidUtil; +import net.neoforged.neoforge.fluids.capability.IFluidHandlerItem; import net.neoforged.neoforge.registries.DeferredRegister; import net.neoforged.neoforge.registries.NeoForgeRegistries; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Optional; @Mod(BucketLib.MOD_ID) @@ -64,7 +68,6 @@ public BucketLibMod(IEventBus eventBus) { CommonLoader.init(); eventBus.addListener(this::commonSetup); - eventBus.addListener(this::registerCapabilities); eventBus.addListener(this::processIMC); eventBus.addListener(this::addItemsToTabs); @@ -83,12 +86,6 @@ private void commonSetup(FMLCommonSetupEvent event) { BucketLibTags.init(); } - private void registerCapabilities(RegisterCapabilitiesEvent event) { - for (Item item : buckets) { - event.registerItem(Capabilities.FluidHandler.ITEM, (stack, ctx) -> new UniversalBucketFluidHandler(stack), item); - } - } - private void processIMC(final InterModProcessEvent event) { event.getIMCStream().forEach(imcMessage -> { if (!BucketLib.MOD_ID.equals(imcMessage.modId())) { @@ -118,6 +115,15 @@ private void processIMC(final InterModProcessEvent event) { private void registerBucket(UniversalBucketItem bucket) { buckets.add(bucket); + //register item capability, because RegisterCapabilitiesEvent fires before InterModProcessEvent + try { + Field providersField = ItemCapability.class.getDeclaredField("providers"); + providersField.setAccessible(true); + Map>> providers = (Map>>) providersField.get(Capabilities.FluidHandler.ITEM); + (providers.computeIfAbsent(bucket, (i) -> new ArrayList<>())).add((stack, ctx) -> new UniversalBucketFluidHandler(stack)); + } catch (IllegalAccessException | NoSuchFieldException ex) { + LOGGER.error("Bucket could not be registered completely. The capability provider registration failed.", ex); + } //register dispense behaviour DispenserBlock.registerBehavior(bucket, UniversalBucketDispenseBehaviour.getInstance()); //register color