I know, this is simple, but I DID look online and still couldn't solve it. Anyways, I am trying to get my custom torch to turn into a different custom torch when right clicked with flint. Here is my code:
public class BlockTorchUnlit extends BlockTorch
{
public BlockTorchUnlit()
{
super();
this.setStepSound(soundTypeStone);
this.setCreativeTab(CreativeTabs.tabBlock);
this.setBlockName("torchUnlit");
this.setBlockTextureName(Reference.MODID + ":" + getUnlocalizedName().substring(5));
}
@Override
@SideOnly(Side.CLIENT)
public void randomDisplayTick(World p_149734_1_, int p_149734_2_, int p_149734_3_, int p_149734_4_, Random p_149734_5_)
{
}
public boolean blockActivated(World world, int i, int j, int k, EntityPlayer player)
{
//???
}
}
The block I am trying to change it into is BlockTorchLit. If you need any more info, just say.
Ok, that makes sense, but this is what I got when I tried it:
It said there was an error, and to change the second "getCurrentItem()" to "currentItem" so I did. But then it said, "Incompatible operand types int and Item." I think this means it's something like adding a color to a number, incompatible data types or something. My code:
@Override
@SideOnly(Side.CLIENT)
public void randomDisplayTick(World p_149734_1_, int p_149734_2_, int p_149734_3_, int p_149734_4_, Random p_149734_5_)
{
}
public boolean blockActivated(World world, int i, int j, int k, EntityPlayer player)
{
if (player.inventory.getCurrentItem() != null)
{
if (player.inventory.currentItem == Items.flint)
{
//error
}
}
}
}
Also, how would I use my block in the world event? I can do vanilla blocks, like Block.anvil, but how would this work if I was using a block from my mod?
EDIT: Leviathan143 already solved this, I didn't see it at first.
Ok, that takes away the error, but somehow this code still isn't working. Here is my code right now:
public boolean blockActivated(World world, int i, int j, int k, EntityPlayer player)
{
if (player.inventory.getCurrentItem() != null)
{
if (player.inventory.getCurrentItem().getItem() == Items.flint)
{
world.setBlock(i, j, k, ModBlocks.torchLit);
}
}
return false;
}
Your method public boolean blockActivated(World world, int i, int j, int k, EntityPlayer player) does'nt exist in the superclass so it's never called ...
The right method is this:
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9)
Your method public boolean blockActivated(World world, int i, int j, int k, EntityPlayer player) does'nt exist in the superclass so it's never called ...
The right method is this:
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9)
Your method public boolean blockActivated(World world, int i, int j, int k, EntityPlayer player) does'nt exist in the superclass so it's never called ...
The right method is this:
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9)
Also you should return true on the end.
TechMage66
That worked, thanks! I just have a few more questions:
1. What do par6,7,8,9 do?
2. If I wanted to check if the player is holding a certain block, how would I do that? getBlock()? This code gives me the error, "The method getBlock() is undefined for the type ItemStack"
public boolean onBlockActivated(World world, int i, int j, int k, EntityPlayer player, int par6, float par7, float par8, float par9)
{
if (player.inventory.getCurrentItem() != null)
{
if (player.inventory.getCurrentItem().getItem() == Items.flint || player.inventory.getCurrentItem().getBlock() == ModBlocks.torchLit)
{
world.setBlock(i, j, k, ModBlocks.torchLit);
}
}
return true;
}
2. If I wanted to check if the player is holding a certain block, how would I do that? getBlock()? This code gives me the error, "The method getBlock() is undefined for the type ItemStack"
If you want to get a block from Item you should use if(Block.getBlockFromItem(player.inventory.getCurrentItem().getItem()) == Blocks.whatever_block)
OR
if(player.inventory.getCurrentItem().getItem() == Item.getItemFromBlock(Blocks.whatever_block)) to get Item from Block
sorry not sure what the 3 floats are but the integer will be Block Metadata
sorry not sure what the 3 floats are but the integer will be Block Metadata
Thanks, that works perfectly! I know I said only one more question, but, I realize that I forgot to ask in the OP how to remove the item from your hand when you right click the torch IF it is not another torch. I can't find the method (or whatever it's called) to handle the players inventory.
Is there some place to find all of these events? Like a list or something?
Anyways if anyone knows how to do that, I think my problem will be completely solved.
EDIT: I actually figured part of it out, but how would this make it so it only removes the item if your not in creative?
public boolean onBlockActivated(World world, int i, int j, int k, EntityPlayer player, int par6, float par7, float par8, float par9)
{
if (player.inventory.getCurrentItem() != null)
{
if (player.inventory.getCurrentItem().getItem() == Items.flint ||
player.inventory.getCurrentItem().getItem() == Item.getItemFromBlock(ModBlocks.torchLit))
{
world.setBlock(i, j, k, ModBlocks.torchLit);
}
Thanks, that works perfectly! I know I said only one more question, but, I realize that I forgot to ask in the OP how to remove the item from your hand when you right click the torch IF it is not another torch. I can't find the method (or whatever it's called) to handle the players inventory.
Is there some place to find all of these events? Like a list or something?
Anyways if anyone knows how to do that, I think my problem will be completely solved.
It's all the the source which you can find in your IDE under Forge..... .jar
Technically it was if (player.capabilities.isCreativeMode), (and I added a ! to indicate survival/adventure) but It worked!
Again thank you to everyone who helped me with this. This is my first mod, and this was very helpful. I marked the problem as solved so people can use it in the future.
I know, this is simple, but I DID look online and still couldn't solve it. Anyways, I am trying to get my custom torch to turn into a different custom torch when right clicked with flint. Here is my code:
package com.github.wolfiewaffle.hardcoretorches.blocks;
import java.util.Random;
import net.minecraft.block.BlockTorch;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import com.github.wolfiewaffle.hardcoretorches.help.Reference;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class BlockTorchUnlit extends BlockTorch
{
public BlockTorchUnlit()
{
super();
this.setStepSound(soundTypeStone);
this.setCreativeTab(CreativeTabs.tabBlock);
this.setBlockName("torchUnlit");
this.setBlockTextureName(Reference.MODID + ":" + getUnlocalizedName().substring(5));
}
@Override
@SideOnly(Side.CLIENT)
public void randomDisplayTick(World p_149734_1_, int p_149734_2_, int p_149734_3_, int p_149734_4_, Random p_149734_5_)
{
}
public boolean blockActivated(World world, int i, int j, int k, EntityPlayer player)
{
//???
}
}
The block I am trying to change it into is BlockTorchLit. If you need any more info, just say.
world.setblock(i, j, k, YourBlockClass.YourBlock);
i, j & k are x, y and z coords.
Please don't PM me asking for help, I will just redirect you to the appropriate forum, where there are others who are far more skilled than me.
This is not the signature you are looking for.
Banners and such things
I'l give you the basic idea behind this:
First of you have to check if the player has a flint in his hand, this should look something like this:
if (player.inventory.getCurrentItem() != null)
{
if (player.inventory.getCurrentItem == Items.flint)
{
}
}
If this condition is true you want to change your torch. ... something like this:
world.setBlock (this.xCord, this.yCord, this.zCord, YourNewTorch)
That's the idea behind this. ( There might be some syntax errors because I wrote this with my mobile phone)
TechMage66
It said there was an error, and to change the second "getCurrentItem()" to "currentItem" so I did. But then it said, "Incompatible operand types int and Item." I think this means it's something like adding a color to a number, incompatible data types or something. My code:
player.inventory.getCurrentItem() returns an itemstack your checking for an item so you want to add .getItem() to get an Item from an ItemStack
if(player.inventory.getCurrentItem().getItem() == items.flint)
It says I need to return something, would I return true or false? What would each do?
Also, how would I use my block in the world event? I can do vanilla blocks, like Block.anvil, but how would this work if I was using a block from my mod?
EDIT: Leviathan143 already solved this, I didn't see it at first.
Ok, that takes away the error, but somehow this code still isn't working. Here is my code right now:
Your method public boolean blockActivated(World world, int i, int j, int k, EntityPlayer player) does'nt exist in the superclass so it's never called ...
The right method is this:
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9)
Also you should return true on the end.
TechMage66
What do par6,7,8,9 do?
Anyways, It works! Thanks
Ok, one more question:
If I wanted to use a block from the hand, how would I do that? I tried:
But that gave me "The method getBlock() is undefined for the type ItemStack"
Ok, something went strange, and I don't know if my previous post actually posted. Anyways, If you already saw this, just ignore it.
That worked, thanks! I just have a few more questions:
1. What do par6,7,8,9 do?
2. If I wanted to check if the player is holding a certain block, how would I do that? getBlock()? This code gives me the error, "The method getBlock() is undefined for the type ItemStack"
If you want to get a block from Item you should use if(Block.getBlockFromItem(player.inventory.getCurrentItem().getItem()) == Blocks.whatever_block)
OR
if(player.inventory.getCurrentItem().getItem() == Item.getItemFromBlock(Blocks.whatever_block)) to get Item from Block
sorry not sure what the 3 floats are but the integer will be Block Metadata
Thanks, that works perfectly! I know I said only one more question, but, I realize that I forgot to ask in the OP how to remove the item from your hand when you right click the torch IF it is not another torch. I can't find the method (or whatever it's called) to handle the players inventory.
Is there some place to find all of these events? Like a list or something?
Anyways if anyone knows how to do that, I think my problem will be completely solved.
EDIT: I actually figured part of it out, but how would this make it so it only removes the item if your not in creative?
It's all the the source which you can find in your IDE under Forge..... .jar
Decrease Stack size - player.inventory.decrStackSize(inventory slot, amount);
Get Current Items Inventory Slot - player.inventory.currentItem;
Decrease your ItemStack size by 1
player.inventory.decrStackSize(player.inventory.currentItem, 1);
Thanks, that works better than my method :P. Is there a way to get the players gamemode, so I can make it so igniters aren't consumed on creative?
if(player.capabilities.isCreativeMode)
Technically it was if (player.capabilities.isCreativeMode), (and I added a ! to indicate survival/adventure) but It worked!
Again thank you to everyone who helped me with this. This is my first mod, and this was very helpful. I marked the problem as solved so people can use it in the future.