Skip to content

Commit

Permalink
fixed that the fluid capability in NeoForge was not working correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
cech12 committed Jan 29, 2024
1 parent 044a4d0 commit 620ccee
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 14 additions & 8 deletions neoforge/src/main/java/de/cech12/bucketlib/BucketLibMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);

Expand All @@ -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())) {
Expand Down Expand Up @@ -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<Item, List<ICapabilityProvider<ItemStack, Void, IFluidHandlerItem>>> providers = (Map<Item, List<ICapabilityProvider<ItemStack, Void, IFluidHandlerItem>>>) 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
Expand Down

0 comments on commit 620ccee

Please sign in to comment.