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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • https://mclo.gs/4UC49Ao
    • Way back in the Forge 1.17 days, work started for adding JPMS (Java Platform Module Support) to ModLauncher and ForgeModLoader. This has been used internally by Forge and some libraries for a while now, but mods (those with mods.toml specifically) have not been able to take advantage of it. As of Forge 1.21.1 and 1.21.3, this is now possible!   What is JPMS and what does it mean for modders? JPMS is the Java Platform Module System, introduced in Java 9. It allows you to define modules, which are collections of packages and resources that can be exported or hidden from other modules. This allows for much more fine-tuned control over visibility, cleaner syntax for service declarations and support for sealed types across packages. For example, you might have a mod with a module called `com.example.mod` that exports `com.example.mod.api` and `com.example.mod.impl` to other mods, but hides `com.example.mod.internal` from them. This would allow you to have a clean API for other mods to use, while keeping your internal implementation details hidden from IDE hints, helping prevent accidental usage of internals that might break without prior notice. This is particularly useful if you'd like to use public records with module-private constructors or partially module-private record components, as you can create a sealed interface that only your record implements, having the interface be exported and the record hidden. It's also nice for declaring and using services, as you'll get compile-time errors from the Java compiler for typos and the like, rather than deferring to runtime errors. In more advanced cases, you can also have public methods that are only accessible to specific other modules -- handy if you want internal interactions between multiple of your own mods.   How do I bypass it? We understand there may be drama in implementing a system that prevents mods from accessing each other's internals when necessary (like when a mod is abandoned or you need to fix a compat issue) -- after all, we are already modding a game that doesn't have explicit support for Java mods yet. We have already thought of this and are offering APIs from day one to selectively bypass module restrictions. Let me be clear: Forge mods are not required to use JPMS. If you don't want to use it, you don't have to. The default behaviour is to have fully open, fully exported automatic modules. In Java, you can use the `Add-Opens` and `Add-Exports` manifest attributes to selectively bypass module restrictions of other mods at launch time, and we've added explicit support for these when loading your Forge mods. At compile-time, you can use existing solutions such as the extra-java-module-info Gradle plugin to deal with non-modular dependencies and add extra opens and exports to other modules. Here's an example on how to make the internal package `com.example.examplemod.internal` open to your mod in your build.gradle: tasks.named('jar', Jar) { manifest { attributes([ 'Add-Opens' : 'com.example.examplemod/com.example.examplemod.internal' 'Specification-Title' : mod_id, 'Specification-Vendor' : mod_authors // (...) ]) } } With the above in your mod's jar manifest, you can now reflectively access the classes inside that internal package. Multiple entries are separated with a space, as per Java's official spec. You can also use Add-Exports to directly call without reflection, however you'd need to use the Gradle plugin mentioned earlier to be able to compile. The syntax for Add-Exports is the same as Add-Opens, and instructions for the compile-time step with the Gradle plugin are detailed later in this post. Remember to prefer the opens and exports keywords inside module-info.java for sources you control. The Add-Opens/Add-Exports attributes are only intended for forcing open other mods.   What else is new with module support? Previously, the runtime module name was always forced to the first mod ID in your `mods.toml` file and all packages were forced fully open and exported. Module names are now distinguished from mod IDs, meaning the module name in your module-info.java can be different from the mod ID in your `mods.toml`. This allows you to have a more descriptive module name that doesn't have to be the same as your mod ID, however we strongly recommend including your mod ID as part of your module name to aid troubleshooting. The `Automatic-Module-Name` manifest attribute is now also honoured, allowing you to specify a module name for your mod without needing to create a `module-info.java` file. This is particularly useful for mods that don't care about JPMS features but want to have a more descriptive module name and easier integration with other mods that do use JPMS.   How do I use it? The first step is to create a `module-info.java` file in your mod's source directory. This file should be in the same package as your main mod class, and should look something like this: open module com.example.examplemod { requires net.minecraftforge.eventbus; requires net.minecraftforge.fmlcore; requires net.minecraftforge.forge; requires net.minecraftforge.javafmlmod; requires net.minecraftforge.mergetool.api; requires org.slf4j; requires logging; } For now, we're leaving the whole module open to reflection, which is a good starting point. When we know we want to close something off, we can remove the open modifier from the module and open or export individual packages instead. Remember that you need to be open to Forge (module name net.minecraftforge.forge), otherwise it can't call your mod's constructor. Next is fixing modules in Gradle. While Forge and Java support modules properly, Gradle does not put automatic modules on the module path by default, meaning that the logging module (from com.mojang:logging) is not found. To fix this, add the Gradle plugin and add a compile-time module definition for that Mojang library: plugins { // (...) id 'org.gradlex.extra-java-module-info' version "1.9" } // (...) extraJavaModuleInfo { failOnMissingModuleInfo = false automaticModule("com.mojang:logging", "logging") } The automatic module override specified in your build.gradle should match the runtime one to avoid errors. You can do the same for any library or mod dependency that is missing either a module-info or explicit Automatic-Module-Name, however be aware that you may need to update your mod once said library adds one. That's all you need to get started with module support in your mods. You can learn more about modules and how to use them at dev.java.
    • Faire la mise à jour grâce à ce lien m'a aider personnellement, merci à @Paint_Ninja. https://www.amd.com/en/support 
    • When I came across the 'Exit Code: I got a 1 error in my Minecraft mods, so I decided to figure out what was wrong. First, I took a look at the logs. In the mods folder (usually where you'd find logs or crash reports), I found the latest.log file or the corresponding crash report. I read it through carefully, looking for any lines with errors or warnings. Then I checked the Minecraft Forge support site, where you can often find info on what causes errors and how to fix them. I then disabled half of my mods and tried running the game. If the error disappeared, it meant that the problem was with the disabled mod. I repeated this several times to find the problem mod.
    • I have no idea - switch to a pre-configured modpack and use it as working base    
  • Topics

×
×
  • Create New...

Important Information

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