As said, I am trying to make a item that would, when I right click a block with it, check the blocks code for a certain method, and if that method was there, then it would call it, I am not sure if this is possible, but I am not sure, so I thought I would ask, any input would be appreciated
-Thank You-
Edit: I was looking around in the classes and I found something that is almost what I want:
if (var11 == Block.sapling.blockID)
{
if (!par3World.isRemote)
{
((BlockSapling)Block.sapling).growTree(par3World, par4, par5, par6, par3World.rand);
--par1ItemStack.stackSize;
}
This is from the bonemeal code, the only problem is that I want it to not care what the block is, so I can't have the:
((BlockSapling)Block.sapling)
Part, so I am looking for some way of doing it without that.
If it's a method that doesn't exist in every single Block you can't have that. It will give you null pointer exceptions (errors) every time.
Yeah, I was thinking about that, and I think I could edit the Block.java (or use forge) to make a new method in the Block class that would return nothing, and then override it in the blocks that use it
I was thinking about it more, and I think I could make something like EntityMob to use with this, so i would add a class that would extend the Block class (like entitymob does to entityliving) and then I could add my method into it, and then just have all the blocks I wanted to have it work on extend that class
Edit: So I want ahead and tried it, this is the calling code:
package net.minecraft.src;
import java.util.Random;
public class BlockGrowing extends Block {
public BlockGrowing(int par1, int par2, Material par3Material) {
super(par1, par2, Material.plants);
}
public void onGetFerted (World par1World, int par2, int par3, int par4, Random par5Random)
{
}
}
Then I use it in a different class like this:
public void onGetFerted (World par1World, int par2, int par3, int par4, Random par5Random)
{
par1World.setBlock(par2, par3 + 1, par4, this.blockID);
}
However, I am getting an error on the "BlockGrowing.onGetFerted(par3World, par4, par5, par6, par3World.rand);" line (In the calling code), and eclipse says: "Cannot make a static reference to the non-static method onGetFerted(World, int, int, int, Random) from the type BlockGrowing" and recomends that I change onGetFerted to a static method, but if I do that, I can no longer use it properly in my block class as it will then say that "This instance method cannot override the static method from BlockGrowing" and will recomend that I change onGetFerted to a non-static method , so, if anyone knows how I could get around this... Again, any input is appreciated
Edit2: So I desided to try making the onGetFerted method in the blocks class static, just to test, it got rid of the errors, so I can now test it in-game, so I added some "System.out.println"s to test what was happening, and it turns out that it was only calling the method in the BlockGrowing class, so I changed the calling method to look like this:
However, that didn't change anything, it may be because I made the method static, but I am not sure, regardless, if anyone has a idea of what I should do, please post it
-Thank You-
A bit more information would be useful. What kind of method would you wanna call in those blocks? And why do you wanna check if the targetted block has that method instead of checking if the block is one of the blocks having that method (by using the blockID)?
If you're afraid of NullPointers there's always try&catch btw.
Ok, for what I am trying to do: I made a composter/fertalizer mod (link in the sig if you want to look at it) and I was thinking that it might be possible for me to add a certain chunk of code to the fertilizer item that would allow for other modders with plants to add a certain method to their plant blocks that would allow for my fertilizer to work on it.
As for the try/catch, I am unaware of how that works, as I am not all that well learned in java
Well, there are two ways of achieving that that I can think of right now.
1.) Look into try/catch. Learning more ways of coding is always good anyway.
2.) Create an array(list) of IDs and check if the block's ID is among them when you use the item on it. Additionally add a method to add an ID to that array which would allow others to add their plants to it.
I think I will look into try/catch, and thanks allot for your imput
Ok thanks. I think I have that working, now I just need to figure out the other part of it, namely, how to allow for other people to add their items to the "smelting" (composting) recipes/
-Thank You-
Edit: I was looking around in the classes and I found something that is almost what I want:
This is from the bonemeal code, the only problem is that I want it to not care what the block is, so I can't have the:
Part, so I am looking for some way of doing it without that.
Yeah, I was thinking about that, and I think I could edit the Block.java (or use forge) to make a new method in the Block class that would return nothing, and then override it in the blocks that use it
Edit: So I want ahead and tried it, this is the calling code:
And this is the BlockGrowing code:
Then I use it in a different class like this:
However, I am getting an error on the "BlockGrowing.onGetFerted(par3World, par4, par5, par6, par3World.rand);" line (In the calling code), and eclipse says: "Cannot make a static reference to the non-static method onGetFerted(World, int, int, int, Random) from the type BlockGrowing" and recomends that I change onGetFerted to a static method, but if I do that, I can no longer use it properly in my block class as it will then say that "This instance method cannot override the static method from BlockGrowing" and will recomend that I change onGetFerted to a non-static method , so, if anyone knows how I could get around this... Again, any input is appreciated
Edit2: So I desided to try making the onGetFerted method in the blocks class static, just to test, it got rid of the errors, so I can now test it in-game, so I added some "System.out.println"s to test what was happening, and it turns out that it was only calling the method in the BlockGrowing class, so I changed the calling method to look like this:
However, that didn't change anything, it may be because I made the method static, but I am not sure, regardless, if anyone has a idea of what I should do, please post it
-Thank You-
Ok, for what I am trying to do: I made a composter/fertalizer mod (link in the sig if you want to look at it) and I was thinking that it might be possible for me to add a certain chunk of code to the fertilizer item that would allow for other modders with plants to add a certain method to their plant blocks that would allow for my fertilizer to work on it.
As for the try/catch, I am unaware of how that works, as I am not all that well learned in java
I think I will look into try/catch, and thanks allot for your imput