Jump to content

[1.8] [SOLVED] Dropping XP only if the block was destroyed with the right tool


NLBuescher

Recommended Posts

Hello,

 

I'm working on a mod to incorporate a few custom materials and I'm currently having problems with one of my custom sapphire ore. I've managed to get the ore to drop the proper item (a sapphire) when mined, as well as respond properly to the fortune enchantment modifier. I'm now working on getting the ore to drop XP (3-7) when mined like the vanilla ores, which I've also managed to implement. I've tested all of this compared with the vanilla emerald ore and the emerald ore doesn't drop XP when you mine it with anything less than an iron pick, unlike my sapphire ore, which drops XP if it is broken by anything, doesn't matter what.

 

TL;DR

Sapphire Ore can drop XP when mined but still drops XP when destroyed with the wrong tool (which it shouldn't) although it doesn't drop items.

 

I've tried using the "onBlockDestroyedByPlayer(World, BlockPos, IBlockState)" method with the same result.

 

Any help is appreciated.

 

UPDATE: The Sapphire Ore also drops XP when picked up with a Silk Touch Pick, which it also shouldn't

 

Code below:

package com.nlbuescher.materealistic;


import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;

public class BlockSapphireOre extends Block
{
public BlockSapphireOre(Material material)
{
	super(material);
	this.setCreativeTab(CreativeTabs.tabBlock);
	this.setHardness(3);
	this.setHarvestLevel("pickaxe", 2);
	this.setStepSound(soundTypeStone);
	this.setUnlocalizedName("Sapphire Ore");
}

@Override
public Item getItemDropped(IBlockState state, Random random, int fortune)
{
        return Materealistic.sapphire;
}

@Override
public void onBlockHarvested(World world, BlockPos pos, IBlockState state, EntityPlayer player)
{
    this.dropXpOnBlockBreak(world, pos, 3 + world.rand.nextInt(5));
}

@Override
public int damageDropped(IBlockState state)
{
    return 0;
}

@Override
public int quantityDropped(IBlockState state, int fortune, Random random)
{
    return 1 + random.nextInt(fortune + 1);
}
}

Link to comment
Share on other sites

extend BlockOre, it's alot easier than hardcoding your own ore.

 

So I went with this solution simply because my goal was to create a vanilla style ore, with all the same works. I also finally managed to get a look at the Minecraft source code (I wasn't able to before because I set up the workspace wrong :P ), which was immeasurably helpful in figuring out how to go about extending the BlockOre class

 

I ended up extending "BlockOre" instead of "Block" and overriding the "getExpDrop(IBlockAccess, BlockPos, int)" method, which worked wonderfully since this way the game automatically knows not to drop XP if the block isn't properly destroyed.

 

EDIT:

extend BlockOre, it's alot easier than hardcoding your own ore.

No way dude, that was meant for vanilla ores.

 

To clarify, the vanilla "BlockOre.getExpDrop(...)" method did have all of the vanilla ores hardcoded into the calculations for how much XP to drop, but overriding that method will fix that problem.

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.