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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I am not using hardcoded recipes, I'm using Vanilla's already existing code for leather armor dying. (via extending and implementing DyeableArmorItem / DyeableLeatherItem respectively) I have actually figured out that it's something to do with registering item colors to the ItemColors instance, but I'm trying to figure out where exactly in my mod's code I would be placing a call to the required event handler. Unfortunately the tutorial is criminally undescriptive. The most I've found is that it has to be done during client initialization. I'm currently trying to do the necessary setup via hijacking the item registry since trying to modify the item classes directly (via using SubscribeEvent in the item's constructor didn't work. Class so far: // mrrp mrow - mcmod item painter v1.0 - catzrule ch package catzadvitems.init; import net.minecraft.client.color.item.ItemColors; import net.minecraft.world.item.Item; import net.minecraftforge.registries.ObjectHolder; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.client.event.ColorHandlerEvent; import catzadvitems.item.DyeableWoolArmorItem; @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) public class Painter { @ObjectHolder("cai:dyeable_wool_chestplate") public static final Item W_CHEST = null; @ObjectHolder("cai:dyeable_wool_leggings") public static final Item W_LEGS = null; @ObjectHolder("cai:dyeable_wool_boots") public static final Item W_SOCKS = null; public Painter() { // left blank, idk if forge throws a fit if constructors are missing, not taking the chance of it happening. } @SubscribeEvent public static void init(FMLClientSetupEvent event) { new Painter(); } @Mod.EventBusSubscriber private static class ForgeBusEvents { @SubscribeEvent public static void registerItemColors(ColorHandlerEvent.Item event) { ItemColors col = event.getItemColors(); col.register(DyeableUnderArmorItem::getItemDyedColor, W_CHEST, W_LEGS, W_SOCKS); //placeholder for other dye-able items here later.. } } } (for those wondering, i couldn't think of a creative wool helmet name)
    • nvm found out it was because i had create h and not f
    • Maybe there's something happening in the 'leather armor + dye' recipe itself that would be updating the held item texture?
    • @SubscribeEvent public static void onRenderPlayer(RenderPlayerEvent.Pre e) { e.setCanceled(true); model.renderToBuffer(e.getPoseStack(), pBuffer, e.getPackedLight(), 0f, 0f, 0f, 0f, 0f); //ToaPlayerRenderer.render(); } Since getting the render method from a separate class is proving to be bit of a brick wall for me (but seems to be the solution in older versions of minecraft/forge) I've decided to try and pursue using the renderToBuffer method directly from the model itself. I've tried this route before but can't figure out what variables to feed it for the vertexConsumer and still can't seem to figure it out; if this is even a path to pursue.  The vanilla model files do not include any form of render methods, and seem to be fully constructed from their layer definitions? Their renderer files seem to take their layers which are used by the render method in the vanilla MobRenderer class. But for modded entities we @Override this function and don't have to feed the method variables because of that? I assume that the render method in the extended renderer takes the layer definitions from the renderer classes which take those from the model files. Or maybe instead of trying to use a render method I should be calling the super from the renderer like   new ToaPlayerRenderer(context, false); Except I'm not sure what I would provide for context? There's a context method in the vanilla EntityRendererProvider class which doesn't look especially helpful. I've been trying something like <e.getEntity(), model<e.getEntity()>> since that generally seems to be what is provided to the renderers for context, but I don't know if it's THE context I'm looking for? Especially since the method being called doesn't want to take this or variations of this.   In short; I feel like I'm super super close but I have to be missing something obvious? Maybe this insane inane ramble post will provide some insight into this puzzle?
  • Topics

×
×
  • Create New...

Important Information

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