Introduction
Fluid storage is one of the most important aspects of creating custom mods with MCreator. It allows players to store and transport fluids such as water, oil, and lava. This tutorial will show you the basics of fluid storage in MCreator and provide some tips to help you get started. We will be covering the basics of setting up a fluid storage block, creating a fluid handler, and linking the two together. So let’s get started!
Creating a Fluid Storage Block
The first step in creating a fluid storage block is to create a new block. To do this, open MCreator and select the “New Block” option from the sidebar. From here, you can customize the properties of the block, such as its name, texture, and other attributes. For this tutorial, we will be using a basic stone block as our fluid storage block.
Once you have created the block, you will need to add the necessary code to make it a fluid storage block. To do this, open the MCreator code editor and add the following code to the block’s class:
@Override public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, Entity entityIn) { if (entityIn instanceof FluidEntity) { FluidEntity fluidEntity = (FluidEntity)entityIn; if (fluidEntity.canBeStoredIn(this)) { FluidStack fluidStack = fluidEntity.getFluidStack(); this.addFluid(fluidStack); fluidEntity.setDead(); } } }
This code will allow the block to store fluids when they come into contact with it. Now, you will need to create a fluid handler for the block. The fluid handler will be responsible for managing the fluid storage and will allow players to interact with the block.
Creating a Fluid Handler
The next step is to create a fluid handler for the block. To do this, open MCreator and select the “New Fluid Handler” option from the sidebar. From here, you can customize the properties of the fluid handler, such as its name and other attributes. Once you have created the fluid handler, you will need to add the necessary code to make it a fluid storage handler. To do this, open the MCreator code editor and add the following code to the fluid handler’s class:
@Override public int getFluidCapacity() { return 1000; // This is the maximum amount of fluid the block can store } @Override public FluidStack getFluidStack() { return this.fluidStack; } @Override public void setFluidStack(FluidStack fluidStack) { this.fluidStack = fluidStack; } public boolean canFillFluidType(FluidStack fluidStack) { return true; // This allows the block to accept any type of fluid } public int fill(FluidStack fluidStack, boolean doFill) { if (fluidStack == null || fluidStack.amount == 0) return 0; int totalFilled = 0; if (this.fluidStack == null) { this.fluidStack = fluidStack; totalFilled = fluidStack.amount; } else { if (this.fluidStack.amount + fluidStack.amount <= getFluidCapacity()) { this.fluidStack.amount += fluidStack.amount; totalFilled = fluidStack.amount; } else { totalFilled = getFluidCapacity() - this.fluidStack.amount; this.fluidStack.amount = getFluidCapacity(); } } return totalFilled; }
This code will allow the block to store and retrieve fluids. Now, you will need to link the block and the fluid handler together. This can be done by adding the following code to the block's class:
public FluidHandler getFluidHandler(IBlockAccess world, BlockPos pos) { return new FluidHandler(this); }
This code will allow the block to access the fluid handler when it is placed in the world. Now, you will need to add some additional code to the block's class to make it interact with other blocks and items. To do this, open the MCreator code editor and add the following code to the block's class:
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) { FluidHandler fluidHandler = getFluidHandler(worldIn, pos); if (fluidHandler != null) { FluidUtils.interactWithFluidHandlers(worldIn, pos, placer, stack, fluidHandler); } } public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) { FluidHandler fluidHandler = getFluidHandler(worldIn, pos); if (fluidHandler != null) { return FluidUtils.interactWithFluidHandlers(worldIn, pos, playerIn, heldItem, fluidHandler); } return false; }
This code will allow players to interact with the block using items such as buckets and other fluid containers. Now, you will need to add a few additional lines of code to the block's class to make it work properly. To do this, open the MCreator code editor and add the following code to the block's class:
public boolean hasTileEntity(IBlockState state) { return true; } public TileEntity createTileEntity(World world, IBlockState state) { return new FluidHandlerTileEntity(this); }
This code will create a tile entity for the block, which will be responsible for storing the fluid. Now, you have successfully created a fluid storage block in MCreator!
Conclusion
In this tutorial, we have gone over the basics of setting up a fluid storage block in MCreator. We have covered the basics of creating a fluid storage block, creating a fluid handler, and linking the two together. We have also discussed how to make the block interact with other blocks and items. With this knowledge, you should now be able to create your own fluid storage blocks in MCreator.