Hi all, I am brand-new to the world of modding and am looking for help on how to get a custom ore block to drop XP. Bear in mind that the entirety of my Java knowledge has come from trawling around on internet forums and YouTube videos, so while I've been able to create the block, generate it in-world, and give it custom drops, the XP part of it has eluded me simply because I haven't found a forum that's worked for me yet. Given that, any suggestions based on the below code would be greatly appreciated!
package com.nygarussky.expandeduniverse.blocks;
import java.util.Random;
import com.nygarussky.expandeduniverse.init.ModBlocks;
import com.nygarussky.expandeduniverse.init.ModItems;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.Item;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
public class AncientMetalOre extends BlockBase
{
public AncientMetalOre(String name, Material material)
{
super(name, material);
setSoundType(SoundType.STONE);
setHardness(3.0f);
setResistance(15.0f);
setHarvestLevel("pickaxe", 2);
}
@Override
public Item getItemDropped(IBlockState state, Random rand, int fortune)
{
return ModItems.ANCIENT_METAL_SHARD;
}
@Override
public int quantityDropped(Random rand)
{
int max = 3;
int min = 1;
return rand.nextInt(max) + min;
}
@Override public int getExpDrop(IBlockState state, IBlockAccess world, BlockPos pos, int fortune) { return super.getExpDrop(state, world, pos, (int) 5.0f); }
}
Now according to Eclipse there aren't any 'errors', here - the code just don't do anything in-game. I can't say I'm surprised: I actually wrote this code based on a vaguely worded comment on an obscure YouTube video. In other words, just tell me what I did wrong :/
The other issue might be, this is supposed to override something in my BlockBase class (same path as above but ...blocks/BlockBase), so I threw up a blank constructor and let it alone. I wasn't sure if the constructor actually needed to do something to be overridden, but I figured I'd include the code for that just in case.
public int getExpDrop(IBlockAccess world, int metadata, int fortune)
{
return 0;
}
If anyone has any thoughts as to where I'm going wrong, it would be greatly appreciated. Please don't just roll your eyes and tell me 'take a class, noob' - some of us don't have the time and money for that sort of thing. That said, if any of you have any recommendations on cheap or free resources to learn Java, I'd really appreciate it
You just have to return an integer in the getExpDrop() method. You returned the super, which just calls the method from the parent class. The parent class is the Block.class class. And there they return 0. And that's why you're not getting XP from removing your block. My Java knowledge isn't very big either so this explanation could be completely wrong. However, I'm sure that by just returning an integer in that one method, the block will drop XP (:
Thank you for your suggestion Poseidon! I ended up finding a solution from the MCP code: for anyone having a similar problem, writing the following code in the Ore class got my ore block dropping XP!
public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
{
super.dropBlockAsItemWithChance(worldIn, pos, state, chance, fortune);
if (this.getItemDropped(state, worldIn.rand, fortune) != Item.getItemFromBlock(this))
{
int i = 0;
if (this == ModBlocks.ANCIENT_METAL_ORE)
//NOTE this is the location and id of your custom ore block. So my custom ore, Ancient Metal Ore, is located in the ModBlocks package. You'll want to fill this section of the code with whatever your block name and location is.
{
i = MathHelper.getInt(worldIn.rand,3, 6);
//NOTE the integers after rand are the min and max XP drops for individual ore blocks. In Vanilla, Coal drops between 0 and 2, whereas Diamond drops between 3 and 6. Basically, change these based on how much XP you want your ore block dropping.
}
Now according to Eclipse there aren't any 'errors', here - the code just don't do anything in-game. I can't say I'm surprised: I actually wrote this code based on a vaguely worded comment on an obscure YouTube video. In other words, just tell me what I did wrong :/
The other issue might be, this is supposed to override something in my BlockBase class (same path as above but ...blocks/BlockBase), so I threw up a blank constructor and let it alone. I wasn't sure if the constructor actually needed to do something to be overridden, but I figured I'd include the code for that just in case.
If anyone has any thoughts as to where I'm going wrong, it would be greatly appreciated. Please don't just roll your eyes and tell me 'take a class, noob' - some of us don't have the time and money for that sort of thing. That said, if any of you have any recommendations on cheap or free resources to learn Java, I'd really appreciate it
Hello,
You just have to return an integer in the getExpDrop() method. You returned the super, which just calls the method from the parent class. The parent class is the Block.class class. And there they return 0. And that's why you're not getting XP from removing your block. My Java knowledge isn't very big either so this explanation could be completely wrong. However, I'm sure that by just returning an integer in that one method, the block will drop XP (:
Have a nice day
Thank you for your suggestion Poseidon! I ended up finding a solution from the MCP code: for anyone having a similar problem, writing the following code in the Ore class got my ore block dropping XP!
public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
{
super.dropBlockAsItemWithChance(worldIn, pos, state, chance, fortune);
if (this.getItemDropped(state, worldIn.rand, fortune) != Item.getItemFromBlock(this))
{
int i = 0;
if (this == ModBlocks.ANCIENT_METAL_ORE)
//NOTE this is the location and id of your custom ore block. So my custom ore, Ancient Metal Ore, is located in the ModBlocks package. You'll want to fill this section of the code with whatever your block name and location is.
{
i = MathHelper.getInt(worldIn.rand,3, 6);
//NOTE the integers after rand are the min and max XP drops for individual ore blocks. In Vanilla, Coal drops between 0 and 2, whereas Diamond drops between 3 and 6. Basically, change these based on how much XP you want your ore block dropping.
}
this.dropXpOnBlockBreak(worldIn, pos, i);
}
}