Jump to content

[Solved] [1.3.2] Forge 4.1.1.251 Help setting quantityDropped based on Metadata?


magico13

Recommended Posts

Hello everyone. I'm trying to make a simple little mod that adds the normal overworld ores (and a custom one or two) to the nether (I know it's been done, but I'm also trying to learn Java/Forge modding). Most of the ores use a value of 1 in quantityDropped, but for one of my custom ores I want it to drop 1 to 2, and for the "Nether Redstone" I want it to drop 4 to 5. Since metadata isn't one of the parameters of quantityDropped I can't easily use it in a switch statement and all of the other functions using metadata I've looked at either aren't used when the block is being broken or (in the case of idDropped) are called afterward so I can't just put an integer in the class and call it in qunatityDropped. (It seems strange to me that idDropped would be after quantityDropped but when I was getting the metadata from that then mining a redstone I'd get 1 drop and then mining any of my other ores would result in 4 to 5 drops, hinting to me that it's called afterward).

If anyone could either point me to something that has differing drop amounts based on metadata that I can look at, or can otherwise help me out it would be greatly appreciated :)

Thank you.

 

Here is my code (hopefully, I haven't really used many forums so don't know how to use spoiler tags)

NetherOreMod.java

 

 

package magico13.mods.NetherOreMod;

import net.minecraftforge.client.MinecraftForgeClient;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.common.MinecraftForge;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.Mod.Instance;
import cpw.mods.fml.common.Mod.PostInit;
import cpw.mods.fml.common.Mod.PreInit;
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.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
import net.minecraft.client.Minecraft;
import net.minecraft.src.Block;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraft.src.ModLoader;
import java.io.File;


@Mod( modid = "NetherOreMod", name="Nether Ore Mod", version="0.0.1")
@NetworkMod(clientSideRequired = true, serverSideRequired = false)

public class NetherOreMod {
@SidedProxy(clientSide = "magico13.mods.NetherOreMod.ClientProxy", serverSide = "magico13.mods.NetherOreMod.CommonProxy")
public static CommonProxy proxy; //This object will be populated with the class that you choose for the environment
@Instance
public static NetherOreMod instance; //The instance of the mod that will be defined, populated, and callable

// Create Config File
static Configuration configuration = new Configuration(new File("config/magico13_NetherOreMod.cfg"));
// Config ID setup
// Blocks
public static int NetherOreID;
// Items

// General



// Block Definitions
public static Block blockNetherOres;

// Item Definitions


    @Init
    public void load(FMLInitializationEvent event)
    {
    	
    	
    	 // loading the configuration from its file
        configuration.load();
       
        // Get or create IDs
        // Blocks
        int NetherOreID = configuration.getOrCreateIntProperty("Nether Ores ID",configuration.CATEGORY_BLOCK, 170).getInt();
        
        // Items
        // Get or create general settings
       
        // saving the configuration to its file
        configuration.save();
        
        // Create new blocks and items
        blockNetherOres = new BlockNetherOres(NetherOreID, 0);
        
    	// Register Blocks
        Item.itemsList[NetherOreID] = new ItemBlockNetherOres(NetherOreID-256, blockNetherOres).setItemName("NetherOre");
  	
    	// Add Names
        LanguageRegistry.addName(new ItemStack(blockNetherOres,1,0), "Nether Eisenerz");
        LanguageRegistry.addName(new ItemStack(blockNetherOres,1,1), "Ender Erz");
        LanguageRegistry.addName(new ItemStack(blockNetherOres,1,2), "Nether Diamant");
        LanguageRegistry.addName(new ItemStack(blockNetherOres,1,3), "Nether Golderz");
        LanguageRegistry.addName(new ItemStack(blockNetherOres,1,4), "Nether Rotstein");
     
        // Generate Ores
        GameRegistry.registerWorldGenerator(new WorldGeneratorNetherOres());
        
        // Set Harvest Levels
        MinecraftForge.setBlockHarvestLevel(blockNetherOres, 0, "pickaxe", 1);
        for (int i=1; i<=4; i++)
        	MinecraftForge.setBlockHarvestLevel(blockNetherOres, i, "pickaxe", 2);
        
        // Create Recipes
    }
}

 

 

BlockNetherOres.java

 

package magico13.mods.NetherOreMod;

import java.util.List;
import java.util.Random;

import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.asm.SideOnly;

import net.minecraft.src.Block;
import net.minecraft.src.CreativeTabs;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraft.src.Material;

public class BlockNetherOres extends Block
{
    public BlockNetherOres(int ID, int par2)
    {
        super(ID, par2, Material.rock);
        this.setCreativeTab(CreativeTabs.tabBlock);
        this.setRequiresSelfNotify();
        this.setHardness(3.0f);
        this.setResistance(5.0f);
    }
    private int blockMetaData;
    public int getBlockTextureFromSideAndMetadata(int side, int metadata) 
    {
    	
    	// I've tried setting blockMetaData = metadata; here but it didn't work
    	switch (metadata) 
    	{
    	case 0:		//Nether Iron
    		return 0;
    	case 1:		//Ender Ore
    		return 1;
    	case 2:		//Nether Diamond
    		return 2;
    	case 3:		//Nether Gold
    		return 3;
    	case 4:		//Nether Redstone
    		return 4;
    	default:	//Default is NOTHING! Will be fixed.
    		return 100;
    	}
    }
    
    public String getTextureFile()
    {
            return "/magico13/mods/NetherOreMod/terrain.png";
    }
    
    public int idDropped(int metadata, Random par2Random, int par3)
    {
    	// Get the block's metadata for use in quantityDropped
    	// Putting it here effects the NEXT ore mined, not the current
    //	blockMetaData = par1;
    	
    	switch(metadata)
    	{
    	case 0:
    		return Block.oreIron.blockID;
    	case 1:
    //		blockMetaData = 1;
    		return Item.enderPearl.shiftedIndex;
    	case 2:
    		return Item.diamond.shiftedIndex;
    	case 3:
    		return Block.oreGold.blockID;
    	case 4:
    //		blockMetaData = 4;
    		return Item.redstone.shiftedIndex;
    	default: 
    		return Block.netherrack.blockID;
    	}
    }
    
    public int quantityDropped(Random par1Random)
    {
    	// If the block is Ender Ore drop 1 to 2 items, Redstone drop 4 to 5, otherwise 1
    	//blockMetaData = 1 ? 1 + par1Random.nextInt(2): (blockMetaData == 4 ?4 + par1Random.nextInt(2) : 1);
    	
    /*	This switch and the code above should do the same thing, neither function correctly without the metadata ahead of time
    	switch (blockMetaData)
    	{
    	case 1:
    		return 1 + par1Random.nextInt(2);
    	case 4:
    		return 4 + par1Random.nextInt(2);
        default:
        	return 1;
    	}
    	*/
    	return 1;
    }
    
    
    @SideOnly(Side.CLIENT)
    public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
    {
        for (int var4 = 0; var4 < 5; ++var4)
        {
            par3List.add(new ItemStack(par1, 1, var4));
        }
    }
}

 

ItemBlockNetherOres.java

 

package magico13.mods.NetherOreMod;

import java.util.List;
import java.util.Random;

import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.asm.SideOnly;

import net.minecraft.src.Block;
import net.minecraft.src.CreativeTabs;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraft.src.Material;

public class BlockNetherOres extends Block
{
    public BlockNetherOres(int ID, int par2)
    {
        super(ID, par2, Material.rock);
        this.setCreativeTab(CreativeTabs.tabBlock);
        this.setRequiresSelfNotify();
        this.setHardness(3.0f);
        this.setResistance(5.0f);
    }
    private int blockMetaData;
    public int getBlockTextureFromSideAndMetadata(int side, int metadata) 
    {
    	
    	// I've tried setting blockMetaData = metadata; here but it didn't work
    	switch (metadata) 
    	{
    	case 0:		//Nether Iron
    		return 0;
    	case 1:		//Ender Ore
    		return 1;
    	case 2:		//Nether Diamond
    		return 2;
    	case 3:		//Nether Gold
    		return 3;
    	case 4:		//Nether Redstone
    		return 4;
    	default:	//Default is NOTHING! Will be fixed.
    		return 100;
    	}
    }
    
    public String getTextureFile()
    {
            return "/magico13/mods/NetherOreMod/terrain.png";
    }
    
    public int idDropped(int metadata, Random par2Random, int par3)
    {
    	// Get the block's metadata for use in quantityDropped
    	// Putting it here effects the NEXT ore mined, not the current
    //	blockMetaData = par1;
    	
    	switch(metadata)
    	{
    	case 0:
    		return Block.oreIron.blockID;
    	case 1:
    //		blockMetaData = 1;
    		return Item.enderPearl.shiftedIndex;
    	case 2:
    		return Item.diamond.shiftedIndex;
    	case 3:
    		return Block.oreGold.blockID;
    	case 4:
    //		blockMetaData = 4;
    		return Item.redstone.shiftedIndex;
    	default: 
    		return Block.netherrack.blockID;
    	}
    }
    
    public int quantityDropped(Random par1Random)
    {
    	// If the block is Ender Ore drop 1 to 2 items, Redstone drop 4 to 5, otherwise 1
    	//blockMetaData = 1 ? 1 + par1Random.nextInt(2): (blockMetaData == 4 ?4 + par1Random.nextInt(2) : 1);
    	
    /*	This switch and the code above should do the same thing, neither function correctly without the metadata ahead of time
    	switch (blockMetaData)
    	{
    	case 1:
    		return 1 + par1Random.nextInt(2);
    	case 4:
    		return 4 + par1Random.nextInt(2);
        default:
        	return 1;
    	}
    	*/
    	return 1;
    }
    
    
    @SideOnly(Side.CLIENT)
    public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
    {
        for (int var4 = 0; var4 < 5; ++var4)
        {
            par3List.add(new ItemStack(par1, 1, var4));
        }
    }
}

 

WorldGeneratorNetherOres.java (you probably don't need this, but just to be thorough)

 

package magico13.mods.NetherOreMod;

import java.util.Random;

import net.minecraft.src.IChunkProvider;
import net.minecraft.src.World;
import net.minecraft.src.WorldGenMinable;
import cpw.mods.fml.common.IWorldGenerator;

public class WorldGeneratorNetherOres implements IWorldGenerator 
{
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider)
{
	switch (world.provider.worldType)
	{
	case -1: generateNether(world, random, chunkX*16, chunkZ*16);
	case 0: generateSurface(world, random, chunkX*16, chunkZ*16);
	}
}



private void generateSurface(World world, Random random, int blockX, int blockZ) 
{

}

private void generateNether(World world, Random random, int blockX, int blockZ) 
{
	// Generate Nether Iron (between 0 and 125, in netherrack. 8 per vein, 10 attempts per chunk)
	for (int i=0; i < 10; i++)
	{
	int Xcoord = blockX + random.nextInt(16);
	int Ycoord = random.nextInt(126);
	int Zcoord = blockZ + random.nextInt(16);
	//NetherOreMod.blockNetherOre.blockID, 0, 8 <-- 8 is the amount that spawn in a group, 0 is the metadata
	(new WorldGenMinableNether(NetherOreMod.blockNetherOres.blockID, 0, ).generate(world, random, Xcoord, Ycoord, Zcoord);
	}
	/*
	// Generate Ender Ore (between 0 and 120, in Soul Sand, 2 per vein, 20 attempts per chunk)
	for (int i=0; i < 20; i++)
	{
	int Xcoord = blockX + random.nextInt(16);
	int Ycoord = random.nextInt(120);
	int Zcoord = blockZ + random.nextInt(16);
	(new WorldGenMinableNether(NetherOreMod.blockNetherOres.blockID, 1, 2)).generate(world, random, Xcoord, Ycoord, Zcoord);
	}
	*/

	// Generate Ender Ore (between 100 and 127, in netherrack, 4 per vein, 10 attempts per chunk)
	for (int i=0; i < 10; i++)
	{
	int Xcoord = blockX + random.nextInt(16);
	int Ycoord = 100 + random.nextInt(27);
	int Zcoord = blockZ + random.nextInt(16);
	(new WorldGenMinableNether(NetherOreMod.blockNetherOres.blockID, 1, 4)).generate(world, random, Xcoord, Ycoord, Zcoord);
	}

	// Generate Nether Diamond (between 23 and 30 (lava layer), in netherrack, 5 per vein, 2 attempts per chunk)
	for (int i=0; i < 2; i++)
	{
	int Xcoord = blockX + random.nextInt(16);
	int Ycoord = 23 + random.nextInt(;
	int Zcoord = blockZ + random.nextInt(16);
	(new WorldGenMinableNether(NetherOreMod.blockNetherOres.blockID, 2, 5)).generate(world, random, Xcoord, Ycoord, Zcoord);
	}

	// Generate Nether Gold (between 0 and 45, in netherrack, 5 per vein, 3 attempts per chunk)
	for (int i=0; i < 3; i++)
	{
	int Xcoord = blockX + random.nextInt(16);
	int Ycoord = random.nextInt(46);
	int Zcoord = blockZ + random.nextInt(16);
	(new WorldGenMinableNether(NetherOreMod.blockNetherOres.blockID, 3, 5)).generate(world, random, Xcoord, Ycoord, Zcoord);
	}

	// Generate Nether Redstone (between 0 and 30, in netherrack, 10 per vein, 4 attempts per chunk)
	for (int i=0; i < 4; i++)
	{
	int Xcoord = blockX + random.nextInt(16);
	int Ycoord = random.nextInt(30);
	int Zcoord = blockZ + random.nextInt(16);
	(new WorldGenMinableNether(NetherOreMod.blockNetherOres.blockID, 4, 10)).generate(world, random, Xcoord, Ycoord, Zcoord);
	}
}

}

 

 

Also, if anyone knows how to change hardness and resistance for different metadata, that would be useful as well. The suggestion on the topic here: http://www.minecraftforum.net/topic/1482075-forgemetadata-hardnessresistance/    doesn't work, but this isn't a huge concern for me right now so I haven't looked too far into it.

Thank you for your help,

Magico13

 

P.S. In the preview the spoiler tags aren't opening. I have everything in code tags inside the spoilers, so not sure if that's an issue. Am going to post anyway and hope it works in the final post, if not I'll try to edit it so it does work.

Link to comment
Share on other sites

How about rather than all that, just use:

 

/**
 * ejects contained items into the world, and notifies neighbours of an update, as appropriate
 */
@Override
public void breakBlock(World world, int x, int y, int z, int par5, int par6)
{
	//Do stuff here :-P
	//then use "world.getBlockMetadata(x, y, z);" to access your metadata :-)
}

(you would put that into your BlockNetherOres.java)

 

And then in there you can code it to manually drop however many you choose. (Although I'm not quite sure why you need to access the metadata to determine the drop amount? :-P)

 

Also, here is the code from "BlockOre.java", which I believe does what you asked...

    /**
     * Returns the quantity of items to drop on block destruction.
     */
    public int quantityDropped(Random par1Random)
    {
        return this.blockID == Block.oreLapis.blockID ? 4 + par1Random.nextInt(5) : 1;
    }

 

Don't worry about the spoiler tags not working, they're a little weird with me too, but they work after awhile :-P

 

 

 

Link to comment
Share on other sites

Thank you Bandayd. I will give that a try tomorrow and let you know how it goes (about to go to bed, class in the morning :/) The code from BlockOre.java is what I was trying to use (semi-successfully) in  my BlockNetherOres.java (and it's just commented out for now since it wasn't functioning correctly for my purpose). The reason I need metadata is that I'm storing the redstone ore which needs to drop 4 to 5 redstone in the same ID as the other ores that need to drop just one block. So I need to know which block is being broken to define the amount of drops correctly (I don't want it dropping 4 to 5 iron, or only one redstone). I'm trying to just use one ID for the whole thing for convenience.

But yeah, I'll try that out since it looks like it's exactly what I needed.

Thanks again!

Magico13

 

Update:

I just (finally) got a chance to test it out and it's working exactly as I need it! Thank you. For anyone else who may come across this in the future with the same problem, I'll post my updated code:

BlockNetherOres.java

 

package magico13.mods.NetherOreMod;

import java.util.List;
import java.util.Random;

import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.asm.SideOnly;

import net.minecraft.src.Block;
import net.minecraft.src.CreativeTabs;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraft.src.Material;
import net.minecraft.src.World;

public class BlockNetherOres extends Block
{
    public BlockNetherOres(int ID, int par2)
    {
        super(ID, par2, Material.rock);
        this.setCreativeTab(CreativeTabs.tabBlock);
        this.setRequiresSelfNotify();
        this.setHardness(3.0f);
        this.setResistance(5.0f);
    }
    
    public int getBlockTextureFromSideAndMetadata(int side, int metadata) 
    {
    	
    	// I've tried setting blockMetaData = metadata; here but it didn't work
    	switch (metadata) 
    	{
    	case 0:		//Nether Iron
    		return 0;
    	case 1:		//Ender Ore
    		return 1;
    	case 2:		//Nether Diamond
    		return 2;
    	case 3:		//Nether Gold
    		return 3;
    	case 4:		//Nether Redstone
    		return 4;
    	default:	//Default is NOTHING! Will be fixed.
    		return 100;
    	}
    }
    
    public String getTextureFile()
    {
            return "/magico13/mods/NetherOreMod/terrain.png";
    }
    
    private int blockMetaData;
    /**
 * ejects contained items into the world, and notifies neighbours of an update, as appropriate
 */
@Override
public void breakBlock(World world, int x, int y, int z, int par5, int par6)
{
	blockMetaData = world.getBlockMetadata(x, y, z);
}

    public int idDropped(int metadata, Random par2Random, int par3)
    {
    	switch(metadata)
    	{
    	case 0:
    		return Block.oreIron.blockID;
    	case 1:
    		return Item.enderPearl.shiftedIndex;
    	case 2:
    		return Item.diamond.shiftedIndex;
    	case 3:
    		return Block.oreGold.blockID;
    	case 4:
    		return Item.redstone.shiftedIndex;
    	default: 
    		return Block.netherrack.blockID;
    	}
    }
    
    public int quantityDropped(Random par1Random)
    {
    	// If the block is Ender Ore drop 1 to 2 items, Redstone drop 4 to 5, otherwise 1
    	switch (blockMetaData)
    	{
    	case 1:
    		return 1 + par1Random.nextInt(2);
    	case 4:
    		return 4 + par1Random.nextInt(2);
        default:
        	return 1;
    	}
    }
    
    
    @SideOnly(Side.CLIENT)
    public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
    {
        for (int var4 = 0; var4 < 5; ++var4)
        {
            par3List.add(new ItemStack(par1, 1, var4));
        }
    }
}

 

As you can see, all I had to do was use breakBlock to get the metadata, then used it in a switch statement under quantityDropped. There may be a better way to do this, but this appears to be fully functional!

Thank you again Bandayd!

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



×
×
  • Create New...

Important Information

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