Jump to content

Custom fluid into bucket


sham1

Recommended Posts

Good day(or night if looked from my perspective when I write this). Before I go into my problems, let me introduce myself: I am Sham1, you can call me Sham if you want, I have modded Minecraft before using Modloader and I have made some Bukkit plugins so this is not unknown stuff for me. Anyways, here is my problem:

I try make liquid called "Liquid Mana" what i will use to power my machines later on, i just try make bucket version now before i need it. Placing the liquid into world with my custom bucket full of liquid mana can be done, but when i try to pick the liquid back, i just get bucket of water what is not that good:

here is my main mod file:

package sham1.minecraft.magiweapons;

import net.minecraft.block.Block;
import net.minecraft.item.EnumToolMaterial;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidContainerRegistry.FluidContainerData;
import net.minecraftforge.fluids.FluidRegistry;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;

@Mod(modid = "Magicweapon", name = "Magic weapons mod", version = "v0.0")
@NetworkMod(clientSideRequired=true, serverSideRequired=true)
public class mod_magicWeapons {
final static Fluid liquidMana = new LiquidMana();
final static Item steelSpear = new ModSpear(1000,EnumToolMaterial.IRON,"iron_spear");
public static Item manaBucket = new ItemManaBucket(3000);
final static Block blockLiquidMana = new BlockLiquidMana(2000);

//Used by Forge to be able to use this mod
@Instance("Magicweapon")
public static mod_magicWeapons instance;

@SidedProxy(clientSide="sham1.minecraft.magiweapons.ClientProxy", serverSide="sham1.minecraft.magiweapons.CommonProxy")
public static CommonProxy proxy;

@EventHandler
public void preInit(FMLPreInitializationEvent event){

}

@EventHandler
public void load(FMLInitializationEvent event){
	proxy.registerRenderers();
	LanguageRegistry.addName(steelSpear, "Iron Spear");
	LanguageRegistry.addName(manaBucket, "Bucket o' mana");
	FluidContainerRegistry.registerFluidContainer(liquidMana, new ItemStack(manaBucket));
}

@EventHandler
public void postInit(FMLPostInitializationEvent event){

}
}

Here is my Fluid-class:

package sham1.minecraft.magiweapons;

import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;

public class LiquidMana extends Fluid {

public LiquidMana(){
	super("LiquidMana");
	setDensity(10); // How tick the fluid is, affects movement inside the liquid.
	setViscosity(1000); // How fast the fluid flows.
	FluidRegistry.registerFluid(this); // Registering inside it self, keeps things neat 
}
}

My actual fluid-block:

package sham1.minecraft.magiweapons;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.util.Icon;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.fluids.BlockFluidClassic;

public class BlockLiquidMana extends BlockFluidClassic{
public BlockLiquidMana(int id){
	super(id, mod_magicWeapons.liquidMana, Material.water);
	mod_magicWeapons.liquidMana.setBlockID(id);
}

@Override
@SideOnly(Side.CLIENT)
public Icon getIcon(int side, int meta){
	return Block.waterMoving.getIcon(side, meta);
}

@Override
public int colorMultiplier(IBlockAccess iBlockAccess, int x, int y, int z){
	return 0xFF0099;
}
}

My custom full bucket of liquid mana:

package sham1.minecraft.magiweapons;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBucket;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumMovingObjectType;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.Event.Result;
import net.minecraftforge.event.Event;
import net.minecraftforge.event.EventPriority;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.player.FillBucketEvent;
import net.minecraftforge.fluids.ItemFluidContainer;

public class ItemManaBucket extends ItemFluidContainer{
int isFull;
public ItemManaBucket(int id){
	super(id);
	func_111206_d("magicweapon:bucket_mana");
	isFull = 2000;
	setCreativeTab(CreativeTabs.tabMisc);
}
@Override
public ItemStack onItemRightClick(ItemStack item, World world,
		EntityPlayer player) {
	// TODO Auto-generated method stub
	MovingObjectPosition movingobjectposition = this.getMovingObjectPositionFromPlayer(world, player, true);

	if (movingobjectposition == null)
	{
	return item;
	}
	else
	{
	FillBucketEvent event = new FillBucketEvent(player, item, world, movingobjectposition);
	if (MinecraftForge.EVENT_BUS.post(event))
	{
	return item;
	}

	if (event.getResult() == Event.Result.ALLOW)
	{
	if (player.capabilities.isCreativeMode)
	{
	return item;
	}

	if (--item.stackSize <= 0)
	{
	return event.result;
	}

	if (!player.inventory.addItemStackToInventory(event.result))
	{
	player.dropPlayerItem(event.result);
	}

	return item;
	}

	if (movingobjectposition.typeOfHit == EnumMovingObjectType.TILE)
	{
	int x = movingobjectposition.blockX;
	int y = movingobjectposition.blockY;
	int z = movingobjectposition.blockZ;

	if (!world.canMineBlock(player, x, y, z))
	{
	return item;
	}


	if (movingobjectposition.sideHit == 0)
	{
	--y;
	}

	if (movingobjectposition.sideHit == 1)
	{
	++y;
	}

	if (movingobjectposition.sideHit == 2)
	{
	--z;
	}

	if (movingobjectposition.sideHit == 3)
	{
	++z;
	}

	if (movingobjectposition.sideHit == 4)
	{
	--x;
	}

	if (movingobjectposition.sideHit == 5)
	{
	++x;
	}

	if (!player.canPlayerEdit(x, y, z, movingobjectposition.sideHit, item))
	{
	return item;
	}

	if (this.tryPlaceContainedLiquid(world, x, y, z) && !player.capabilities.isCreativeMode)
	{
	return new ItemStack(Item.bucketEmpty);
	}

	}

	return item;
	}
}
private boolean tryPlaceContainedLiquid(World world, int x, int y, int z) {
	if (this.isFull <= 0)
        {
            return false;
        }
        else
        {
            Material material = world.getBlockMaterial(x, y, z);
            boolean flag = !material.isSolid();

            if (!world.isAirBlock(x, y, z) && !flag)
            {
                return false;
            }
            else
            {
                if (world.provider.isHellWorld && this.isFull == Block.waterMoving.blockID)
                {
                    world.playSoundEffect((double)((float)x + 0.5F), (double)((float)y + 0.5F), (double)((float)z + 0.5F), "random.fizz", 0.5F, 2.6F + (world.rand.nextFloat() - world.rand.nextFloat()) * 0.8F);

                    for (int l = 0; l < 8; ++l)
                    {
                        world.spawnParticle("largesmoke", (double)x + Math.random(), (double)y + Math.random(), (double)z + Math.random(), 0.0D, 0.0D, 0.0D);
                    }
                }
                else
                {
                    if (!world.isRemote && flag && !material.isLiquid())
                    {
                        world.destroyBlock(x, y, z, true);
                    }

                    world.setBlock(x, y, z, this.isFull, 0, 3);
                }

                return true;
            }
        }
}
}

 

If you would be able to help me, it would be appreaciated

If my post helped you, please press that "Thank You"-button to show your appreciation.

 

Also if you don't know Java, I would suggest you read the official tutorials by Oracle to get an idea of how to do this. Thanks, and good modding!

 

Also if you haven't, set up a Git repo for your mod not only for convinience but also to make it easier to help you.

Link to comment
Share on other sites

you can use a forge hook for the bucket handler. add this line to your init:

 MinecraftForge.EVENT_BUS.register(new YourBucketHandler());

 

Then create the class. it should look something like:

public class YourBucketHandler {

@ForgeSubscribe
public void onBucketFill(FillBucketEvent event) {

	ItemStack result = fillCustomBucket(event.world, event.target);

	if (result == null)
		return;

	event.result = result;
	event.setResult(Result.ALLOW);
}

public ItemStack fillCustomBucket(World world, MovingObjectPosition pos) {
	int blockID = world.getBlockId(pos.blockX, pos.blockY, pos.blockZ);

	if ((blockID == You.liquidStill.blockID || blockID == You.liquidFlowing.blockID)
			&& world.getBlockMetadata(pos.blockX, pos.blockY, pos.blockZ) == 0) {
		world.setBlock(pos.blockX, pos.blockY, pos.blockZ, 0);
		return new ItemStack(You.bucketLiquid);
	} else
		return null;
}

} 

 

Of course, you would need to create your own bucketLiquid itemstack. Hope this helps :)

Link to comment
Share on other sites

  • 2 weeks later...

Hey, just wanted to report that I had this problem as well. Do you think this is a bug with forge, or are we just going to have to live with it?

 

[EDIT]

I should have run my tests before my OP, but oh well. The code above fixed the picking up mechanic, but I can't get my bucket to place in the world. I'll try something really quick, but it's looking to be a much bigger headache than it should be.

 

[EDIT]

 

I got it to work, but I had to entirely circumvent Forge's Item Containers. I find it is best to use a class extending ItemBucket, and apply the above patch note to that class. The FluidContainerRegistry doesn't appear to serve any function beyond compatibility, but I think that I'd rather live with a few complaints on it not playing nice with other mods than live with a buggy, broken item.

 

The source that solved my problem: (names changed)

package mod.yourmod.item;

import net.minecraft.item.ItemBucket;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.Event.Result;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.entity.player.FillBucketEvent;

public class CustomBucketItem extends ItemBucket{

public Block contains;

public CustomBucketItem(int itemID, Block contains) {
	super(itemID, contains.blockID);
	this.contains = contains;
	MinecraftForge.EVENT_BUS.register(this);
}

@ForgeSubscribe
public void onBucketFill(FillBucketEvent event) {

	ItemStack result = fillCustomBucket(event.world, event.target);

	if (result == null)
		return;

	event.result = result;
	event.setResult(Result.ALLOW);
}

public ItemStack fillCustomBucket(World world, MovingObjectPosition pos) {
	int blockID = world.getBlockId(pos.blockX, pos.blockY, pos.blockZ);

	if ((blockID == contains.blockID)
			&& world.getBlockMetadata(pos.blockX, pos.blockY, pos.blockZ) == 0) {
		world.setBlock(pos.blockX, pos.blockY, pos.blockZ, 0);
		return new ItemStack(this);
	} else
		return null;
}
}

 

And I would just like to say that this doesn't feel like a solution, it feels like a bandage that I'll have to redress later.

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.