Modding: Custom block drops

In this tutorial I will teach you how to change the block drop. I will be using this file.


package Tutorial.common;

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

public class BlockOres extends Block
{
    public BlockOres(int par1, int par2)
    {
        super(par1, par2, Material.rock);
        this.setCreativeTab(CreativeTabs.tabBlock);
    }
    
    public String getTextureFile()
    {
            return "/TutTextures.png";
    }
}


To change the item drop you will have to add this.


public int idDropped(int par1, Random par2Random, int par3)
    {
        return Tutorial.youritem.shiftedIndex;
    }


This method is used for the drop of the item.
The thing you return is the item it will drop. In this case the block will drop the item we have made before.
The full file should now look like this.


package Tutorial.common;

import java.util.Random;

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

public class BlockOres extends Block
{
    public BlockOres(int par1, int par2)
    {
        super(par1, par2, Material.rock);
        this.setCreativeTab(CreativeTabs.tabBlock);
    }
    
    public String getTextureFile()
    {
            return "/TutTextures.png";
    }
    
    public int idDropped(int par1, Random par2Random, int par3)
    {
        return Tutorial.youritem.shiftedIndex;
    }
}


You can also change the amount of items it drops by adding this.


public int quantityDropped(Random par1Random)
    {
        return 3;
    }


This will return 3 items. By using the Random you can return a random amount of the item.
To get the random to work you should use something like this.


public int quantityDropped(Random par1Random)
    {
        return par1Random.nextInt(2) + 1;
    }


When you use this code the block will drop 1 or 2 items when you destroy it. It doesn't drop 3, because java starts counting at 0. The random will choose between the amount of numbers between the brackets.

When you are done you should go back to the tutorials list here.

5 opmerkingen:

  1. How would you code this if you wanted a block drop different things based on a percent chance? Like for example if I wanted to make a coal block sometimes drop a larger chunk of coal or something.

    BeantwoordenVerwijderen
    Reacties
    1. I did this via a random number:
      public int idDropped(int par1, Random par2Random, int par3)
      {

      double r = Math.random();

      if (r < 0.25){

      return mod_Darknesscraft.AirEssence.shiftedIndex;

      }else if (r < 0.5){

      return mod_Darknesscraft.EarthEssence.shiftedIndex;

      }else if (r < 0.75){
      return mod_Darknesscraft.WaterEssence.shiftedIndex;
      }else{
      return mod_Darknesscraft.FireEssence.shiftedIndex;
      }
      }
      something like that, you just need to exchange my names

      Verwijderen
  2. if I want it to drop a block, what should I write if not "shiftindex"?

    thx :)

    BeantwoordenVerwijderen
  3. Will blocks drop themselves if nothing is specified? Also how do I specify a metadata block or item to drop?

    BeantwoordenVerwijderen