Jump to content

Forge fluids, help with creative tab, name, and crafting recipe. 1.11.2


Cmonster

Recommended Posts

Hello, I have created a fluid called mercury, and have enabled universal bucket and have done FluidRegistry.addBucketForFluid(ModFluids.FluidMercury.instance);

However, I don't know how to name the filled bucket using the lang file or putting it in the creative tab. Also, I would like to know the way to add a crafting recipe for the filled mercury bucket.

Any help would be greatly appreciated, as well as example code for these items.

Fluid class:

Spoiler

package init;

import com.Cmonster.OreMod.Reference;
import com.Cmonster.OreMod.creativetabs.TabElements;

import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.ItemMeshDefinition;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.block.statemap.StateMapperBase;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fluids.BlockFluidClassic;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class ModFluids {
    
    private static ModelResourceLocation mercury_location = new ModelResourceLocation(Reference.MOD_ID + ":" + BlockMercury.name, "fluid");
    
    public static void register() {
        FluidRegistry.registerFluid(FluidMercury.instance);
        GameRegistry.register(BlockMercury.instance, mercury_location);
        Item mercury = Item.getItemFromBlock(BlockMercury.instance);
        ModelLoader.setCustomMeshDefinition(mercury, new ItemMeshDefinition() {
            
            @Override
            public ModelResourceLocation getModelLocation(ItemStack stack) {
                return mercury_location;
            }
        });
        ModelLoader.setCustomStateMapper(BlockMercury.instance, new StateMapperBase() {
            
            @Override
            protected ModelResourceLocation getModelResourceLocation(IBlockState state) {
                return mercury_location;
            }
        });
        FluidRegistry.addBucketForFluid(FluidMercury.instance);
    }
    
    public static final class FluidMercury extends Fluid {
        
        public static final String name = "mercury";
        public static final FluidMercury instance = new FluidMercury();
        
        public FluidMercury() {
            super(name, new ResourceLocation(Reference.MOD_ID + ":" + "blocks/" + name  + "_still"), new ResourceLocation(Reference.MOD_ID + ":" + "blocks/" + name  + "_flow"));
            this.setViscosity(100);
        }
    }
    
    public static final class BlockMercury extends BlockFluidClassic {
        
        public static final BlockMercury instance = new BlockMercury();
        public static final String name = "mercury";
        
        public BlockMercury() {
            super(FluidMercury.instance, Material.WATER);
            setUnlocalizedName("blockmercury");

        }
        
        @Override
        public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn) {
            if(entityIn instanceof EntityLivingBase) {
                ((EntityLivingBase)entityIn).addPotionEffect(new PotionEffect(Potion.getPotionById(19), 100, 2, false, false));
            }
        }
        
        @Override
        public EnumBlockRenderType getRenderType(IBlockState state) {
            return EnumBlockRenderType.MODEL;
        }
    }
}

Mod class:

Spoiler

package com.Cmonster.OreMod;

import com.Cmonster.OreMod.proxy.CommonProxy;
import com.Cmonster.OreMod.worldgen.OreGen;

import handlers.RecipeHandler;
import init.ModArmor;
import init.ModBlocks;
import init.ModFluids;
import init.ModItems;
import init.ModTools;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;

@Mod(modid = Reference.MOD_ID, name = Reference.NAME, version = Reference.VERSION, acceptedMinecraftVersions = Reference.ACCEPTED_VERSIONS)
public class OreMod {

    @Instance
    public static OreMod instance;
    
    @SidedProxy(clientSide = Reference.CLIENT_PROXY_CLASS, serverSide = Reference.SERVER_PROXY_CLASS)
    public static CommonProxy proxy;
    
    static {
        FluidRegistry.enableUniversalBucket();
    }
    
    @EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
        ModFluids.register();
        FluidRegistry.addBucketForFluid(ModFluids.FluidMercury.instance);
        ModBlocks.init();
        ModBlocks.register();
        ModItems.init();
        ModItems.register();
        ModTools.init();
        ModTools.register();
        ModArmor.init();
        ModArmor.register();
        
    }
    
    @EventHandler
    public void Init(FMLInitializationEvent event)
    {
        proxy.init();
        
        GameRegistry.registerWorldGenerator(new OreGen(), 0);
        RecipeHandler.registerCraftingRecipes();
        RecipeHandler.registerFurnaceRecipes();
    }
    
    @EventHandler
    public void PostInit(FMLPostInitializationEvent event)
    {
        
    }

}
 

 

Link to comment
Share on other sites

To add a bucket to your fluid you would call FluidRegistry::addBucketForFluid. As you are doing. Is something not working correctly?

It will be added to a creative tab that all buckets are at(I believe it is the Miscellaneous tab) if you call that method.

Do not name the bucket, name the fuild(Fluid::setUnlocalizedName) and localize that, the bucket will be localized from there. 

Forge uses capabilities to store fluids in the bucket and so will you. Create an itemstack of a bucket, access it's capabilities and add your fluid into it using the capability. After that just return that stack as a result of your recipe.

 

15 minutes ago, Cmonster said:

 public static void register()

 

15 minutes ago, Cmonster said:

 ModelLoader.setCustomMeshDefinition

You can't call client side code from a common class or you will crash the server.

 

15 minutes ago, Cmonster said:

 public void preInit(FMLPreInitializationEvent event)
    {
        ModFluids.register();
        FluidRegistry.addBucketForFluid(ModFluids.FluidMercury.instance);

 

15 minutes ago, Cmonster said:

public static void register() {
        FluidRegistry.registerFluid(FluidMercury.instance);
        GameRegistry.register(BlockMercury.instance, mercury_location);
        ...
        FluidRegistry.addBucketForFluid(FluidMercury.instance);

You are adding a bucket twice. Granted it should not matter as bucketFluids is a set and set can't usually contain equal elements within but that is still redundant.

 

15 minutes ago, Cmonster said:

 ((EntityLivingBase)entityIn).addPotionEffect(new PotionEffect(Potion.getPotionById(19), 100, 2, false, false));

Potion.getPotionById(19)? What is 19? Where is this magical number coming from? Why are you even using Potion::getPotionById? You have the MobEffects class at net.minecraft.init that contains all potion instances.

Edited by V0idWa1k3r
Link to comment
Share on other sites

22 minutes ago, V0idWa1k3r said:

Potion.getPotionById(19)? What is 19? Where is this magical number coming from? Why are you even using Potion::getPotionById? You have the MobEffects class at net.minecraft.init that contains all potion instances.

I will fix the rest of the code, but the reason i'm using 19 is because it's the effect id for poison. Unless this way is any different from using the potion instances from the MobEffects class, I don't think I need to change it for my purposes.

 

Anyway, thanks for the response and tips.

Link to comment
Share on other sites

Just now, Cmonster said:

Unless this way is any different from using the potion instances from the MobEffects class, I don't think I need to change it for my purposes.

Getting a registry object by ID requires a map lookup every time you do so. Getting an effect from a field requires... getting it from a field. The performance is way better if you use the later.

It also generates a more readable code. Imagine yourself looking at this code 6 months later. Will you remember what this 19 is there for? What if IDs change? You will have to spend time figuring it out and that is not productive.

Link to comment
Share on other sites

On 6/11/2017 at 2:19 PM, V0idWa1k3r said:

Forge uses capabilities to store fluids in the bucket and so will you. Create an itemstack of a bucket, access it's capabilities and add your fluid into it using the capability. After that just return that stack as a result of your recipe.

Honestly, I'm not sure where to start here. I have fixed most other things, but i've been stuck here for a while. I'm new to modding, so if you could get me started with this, that would be great.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.