I am making a mod that adds different types of minerals to Minecraft. All of the minerals are represented as one item, HydrothermalMineralsItem. If I get some minerals from the creative inventory, ones with the same metadata will stack. I can throw some on the ground, pick them back up, and then stack them with the other minerals. However, the mod also adds a hammer and chisel that generates a mineral item with the correct metadata when a HydrothermalMineralsBlock is right clicked (HydrothermalMineralsItem is not the ItemBlock version of HydrothermalMineralsBlock). The minerals obtained using the tool will stack neither with each other nor with other minerals with the same metadata.
public HammerAndChisel(ToolMaterial material) {
super(material);
this.setUnlocalizedName("HammerAndChisel");
// TODO Auto-generated constructor stub
}
/**
* Callback for item usage. If the item does something special on right clicking, he will have one of those. Return
* True if something happen and false if it don't. This is for ITEMS, not BLOCKS
*/
public boolean onItemUse(ItemStack chisel, EntityPlayer player, World world, int blockx, int blocky, int blockz, int p_77648_7_, float p_77648_8_, float p_77648_9_, float p_77648_10_)
{
if (!player.canPlayerEdit(blockx, blocky, blockz, p_77648_7_, chisel))
{
return false;
}
else
{
UseChiselEvent event = new UseChiselEvent(player, chisel, world, blockx, blocky, blockz);
if (MinecraftForge.EVENT_BUS.post(event))
{
return false;
}
if (event.getResult() == Result.ALLOW)
{
chisel.damageItem(1, player);
return true;
}
ItemStack MineralStack = new ItemStack(new HydrothermalMineralsItem(), 1, world.getBlockMetadata(blockx, blocky, blockz));
You should never be creating instances of Item classes outside of the initial loading process. Items should be instantiated once and then stored so they can be referred to by code like this.
I'm not sure what the structure of your mod is, but the line above should look something like this:
ItemStack mineralStack = new ItemStack(ModItems.hydrothermalMinerals, 1, world.getBlockMetadata(blockx, blocky, blockz));
ModItems.hydrothermalMinerals is a reference to the instance of HydrothermalMineralsItem.
As a side note, local variables should be in camelCase, not PascalCase (only types [classes, interfaces, enums] should use PascalCase).
Rollback Post to RevisionRollBack
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
I am making a mod that adds different types of minerals to Minecraft. All of the minerals are represented as one item, HydrothermalMineralsItem. If I get some minerals from the creative inventory, ones with the same metadata will stack. I can throw some on the ground, pick them back up, and then stack them with the other minerals. However, the mod also adds a hammer and chisel that generates a mineral item with the correct metadata when a HydrothermalMineralsBlock is right clicked (HydrothermalMineralsItem is not the ItemBlock version of HydrothermalMineralsBlock). The minerals obtained using the tool will stack neither with each other nor with other minerals with the same metadata.
HydrothermalMineralsItem.java:
package minerals;
import java.util.List;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
public class HydrothermalMineralsItem extends Item {
private final static String[] subNames = {"hematite", "sphalerite", "pyrrhotite",
"quartz", "chalcopyrite", "pyrite", "cassiterite", "galena"};
public HydrothermalMineralsItem(){
this.setHasSubtypes(true);
this.setMaxDamage(0);
this.setMaxStackSize(64);
this.setCreativeTab(CreativeTabs.tabMaterials);
this.setUnlocalizedName("hydrothermalMineralsItem");
}
public String getUnlocalizedName(ItemStack itemstack) {
return this.getUnlocalizedName() + "." + subNames[itemstack.getItemDamage()];
}
@Override
public int getMetadata (int damageValue) {
return damageValue;
}
@SideOnly(Side.CLIENT)
public void getSubItems(Item item, CreativeTabs tabs, List list){
for(int i = 0; i < 8; i++){
list.add(new ItemStack(item, 1, i));
}
}
}
HammerAndChisel.java:
package minerals;
import java.util.Set;
import cpw.mods.fml.common.eventhandler.Event.Result;
import net.minecraft.block.Block;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemHoe;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.UseHoeEvent;
public class HammerAndChisel extends ItemHoe{
public HammerAndChisel(ToolMaterial material) {
super(material);
this.setUnlocalizedName("HammerAndChisel");
// TODO Auto-generated constructor stub
}
/**
* Callback for item usage. If the item does something special on right clicking, he will have one of those. Return
* True if something happen and false if it don't. This is for ITEMS, not BLOCKS
*/
public boolean onItemUse(ItemStack chisel, EntityPlayer player, World world, int blockx, int blocky, int blockz, int p_77648_7_, float p_77648_8_, float p_77648_9_, float p_77648_10_)
{
if (!player.canPlayerEdit(blockx, blocky, blockz, p_77648_7_, chisel))
{
return false;
}
else
{
UseChiselEvent event = new UseChiselEvent(player, chisel, world, blockx, blocky, blockz);
if (MinecraftForge.EVENT_BUS.post(event))
{
return false;
}
if (event.getResult() == Result.ALLOW)
{
chisel.damageItem(1, player);
return true;
}
Block block = world.getBlock(blockx, blocky, blockz);
if (p_77648_7_ != 0 && block == MineralsMod.hydrothermal)
{
Block block1 = Blocks.stone;
world.playSoundEffect((double)((float)blockx + 0.5F), (double)((float)blocky + 0.5F), (double)((float)blockz + 0.5F), block1.stepSound.getStepResourcePath(), (block1.stepSound.getVolume() + 1.0F) / 2.0F, block1.stepSound.getPitch() * 0.8F);
if (world.isRemote)
{
return true;
}
else
{
chisel.damageItem(1, player);
ItemStack MineralStack = new ItemStack(new HydrothermalMineralsItem(), 1, world.getBlockMetadata(blockx, blocky, blockz));
float f = 0.7F;
double d0 = (double)(world.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
double d1 = (double)(world.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
double d2 = (double)(world.rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
EntityItem entityitem = new EntityItem(world, (double)blockx + d0, (double)blocky + d1, (double)blockz + d2, MineralStack);
entityitem.delayBeforeCanPickup = 10;
world.spawnEntityInWorld(entityitem);
world.setBlock(blockx, blocky, blockz, block1);
return true;
}
}
else
{
return false;
}
}
}
}
Please tell me what I am doing wrong.
You should never be creating instances of Item classes outside of the initial loading process. Items should be instantiated once and then stored so they can be referred to by code like this.
I'm not sure what the structure of your mod is, but the line above should look something like this:
ModItems.hydrothermalMinerals is a reference to the instance of HydrothermalMineralsItem.
As a side note, local variables should be in camelCase, not PascalCase (only types [classes, interfaces, enums] should use PascalCase).
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.