Skip to content

Commit

Permalink
create capabilityfixer for fluids
Browse files Browse the repository at this point in the history
  • Loading branch information
Lothrazar committed Feb 17, 2025
1 parent 328943c commit 7a52168
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
47 changes: 32 additions & 15 deletions src/main/java/com/lothrazar/cyclic/block/BlockCyclic.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lothrazar.cyclic.block;

import com.lothrazar.cyclic.config.ClientConfigCyclic;
import com.lothrazar.cyclic.fixers.CapabilityFixer;
import com.lothrazar.cyclic.registry.BlockRegistry;
import com.lothrazar.library.block.EntityBlockFlib;
import com.lothrazar.library.util.SoundUtil;
Expand All @@ -10,11 +11,9 @@
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.*;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
Expand All @@ -24,6 +23,9 @@
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.fluids.FluidUtil;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;

public class BlockCyclic extends EntityBlockFlib {

Expand Down Expand Up @@ -77,13 +79,26 @@ public BlockState rotate(BlockState state, LevelAccessor world, BlockPos pos, Ro
return newState;
}

// protected ItemInteractionResult useItemOn(ItemStack p_316304_, BlockState p_316362_, Level p_316459_, BlockPos p_316366_, Player p_316132_, InteractionHand p_316595_, BlockHitResult p_316140_) {
@Override
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
public ItemInteractionResult useItemOn(ItemStack st, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (hasFluidInteract) {
if (!world.isClientSide) {
BlockEntity tankHere = world.getBlockEntity(pos);
if (!level.isClientSide) {
BlockEntity tankHere = level.getBlockEntity(pos);
if (tankHere != null) {
IFluidHandler handler = tankHere.getCapability(ForgeCapabilities.FLUID_HANDLER, hit.getDirection()).orElse(null);

/**************
*
*
*
* getting fluid capability from a block
*
*
*
*
*/
IFluidHandler handler = CapabilityFixer.fluid(level,pos,hit); // level.getCapability(Capabilities.FluidHandler.BLOCK, pos, hit.getDirection());

if (handler != null) {
if (FluidUtil.interactWithFluidHandler(player, hand, handler)) {
if (player instanceof ServerPlayer) {
Expand All @@ -101,22 +116,23 @@ public InteractionResult use(BlockState state, Level world, BlockPos pos, Player
}
}
if (FluidUtil.getFluidHandler(player.getItemInHand(hand)).isPresent()) { // reverted to how 1.16.5 does it fix sapphys bug
return InteractionResult.SUCCESS;
return ItemInteractionResult.SUCCESS;
}
}
if (this.hasGui) {
if (!world.isClientSide) {
BlockEntity tileEntity = world.getBlockEntity(pos);
if (tileEntity instanceof MenuProvider) {
NetworkHooks.openScreen((ServerPlayer) player, (MenuProvider) tileEntity, tileEntity.getBlockPos());
if (!level.isClientSide) {
BlockEntity tileEntity = level.getBlockEntity(pos);
if (tileEntity instanceof MenuProvider mp) {
player.openMenu(mp);
// NetworkHooks.openScreen((ServerPlayer) player, (MenuProvider) tileEntity, tileEntity.getBlockPos());
}
else {
throw new IllegalStateException("Our named container provider is missing!");
}
}
return InteractionResult.SUCCESS;
return ItemInteractionResult.SUCCESS;
}
return super.use(state, world, pos, player, hand, hit);
return super.useItemOn(st,state, level, pos, player, hand, hit);
}

private void displayClientFluidMessage(Player player, IFluidHandler handler) {
Expand All @@ -131,6 +147,7 @@ public void onRemove(BlockState state, Level worldIn, BlockPos pos, BlockState n
if (state.getBlock() != newState.getBlock()) {
BlockEntity tileentity = worldIn.getBlockEntity(pos);
if (tileentity != null) {

IItemHandler items = tileentity.getCapability(ForgeCapabilities.ITEM_HANDLER).orElse(null);
if (items != null) {
for (int i = 0; i < items.getSlots(); ++i) {
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/lothrazar/cyclic/fixers/CapabilityFixer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.lothrazar.cyclic.fixers;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.phys.BlockHitResult;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
import net.minecraft.world.level.Level;

public class CapabilityFixer {


public static IFluidHandler fluid(net.minecraft.world.level.Level level, BlockPos pos, BlockHitResult dir) {
return fluid(level,pos,dir.getDirection());
}

public static IFluidHandler fluid(net.minecraft.world.level.Level level, BlockPos pos, Direction dir) {
return level.getCapability(Capabilities.FluidHandler.BLOCK, pos, dir);
}



}

0 comments on commit 7a52168

Please sign in to comment.