Jump to content

[1.8.9] Custom Ore Drop Problems


Monstrous_Apple

Recommended Posts

Okay I have three problems:

 

1). When mining my sapphire/ruby ore using a silk touch pickaxe it DOES drop the ore instead of gem however it still grants exp, how can I cancel that out?

 

2). When breaking a sapphire/ruby ore whilst in gamemode creative it still grants exp, how can I stop that?

 

3). The sapphire and ruby can only be mined using a iron or higher pickaxe, and normally using a stone or lower pickaxe would mean no drop for the player, however when doing this with silk touch it still grants exp with no ore or gem drop, how do I stop the exp?

 

Sorry if that doesn't make much sense. :P

 

package com.MonstrousApple.mod.gems;

import java.util.Random;

import com.MonstrousApple.mod.MAGlobal;
import com.MonstrousApple.mod.items.MAItems;
import com.MonstrousApple.mod.ores.MAOres;

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

public class MAGem extends Block {

public MAGem(String unlocalizedName, Material material, float hardness, float resistance) {
	super(material);

	this.setCreativeTab(MAGlobal.maCreativeTabOres);
	this.setUnlocalizedName(unlocalizedName);
	this.setHardness(hardness);
	this.setResistance(resistance);
	}

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

@Override
public Item getItemDropped(IBlockState state, Random random, int fortune) {
	if (this == MAGems.SapphireOre) {
		return MAItems.Sapphire;
		}
		else if (this == MAGems.RubyOre) {
		return MAItems.Ruby;
		}
		else {
		return null;
		}
	}

    public int quantityDropped(Random random)
    {
        return 1;
    }

    public int quantityDroppedWithBonus(int fortune, Random random)
    {
        if (fortune > 0)
        {
            int j = random.nextInt(fortune + 2) - 1;

            if (j < 0)
            {
                j = 0;
            }

            return quantityDropped(random) * (j + 1);
        }
        else
        {
            return quantityDropped(random);
        }
    }

/**public int quantityDropped(IBlockState state, int fortune, Random random) {
	if (this == MAGems.SapphireOre) {
		return 1 + random.nextInt(1);
		}
		else if (this == MAGems.RubyOre) {
		return 1 + random.nextInt(1);
		}
		else {
		return 0;
		}
	}*/
}

Link to comment
Share on other sites

1). When mining my sapphire/ruby ore using a silk touch pickaxe it DOES drop the ore instead of gem however it still grants exp, how can I cancel that out?

 

2). When breaking a sapphire/ruby ore whilst in gamemode creative it still grants exp, how can I stop that?

 

3). The sapphire and ruby can only be mined using a iron or higher pickaxe, and normally using a stone or lower pickaxe would mean no drop for the player, however when doing this with silk touch it still grants exp with no ore or gem drop, how do I stop the exp?

 

The solution to all three problems: Don't call

Block#dropXpOnBlockBreak

, Minecraft does that for you.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

I didn't say anything about changing which class you extend. Delete your override of

Block#onBlockHarvested

(i.e. the method that calls

Block#dropXpOnBlockBreak

).

 

To control how much XP your block drops, override

Block#getExpDrop

.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Okay I know this isn't correct, there's no errors but I tested it and I don't get exp drops, could you explain what I did wrong please?

 

public void getExpDrop(World world, BlockPos pos, IBlockState state, EntityPlayer player, int fortune)
{
	this.getExpDrop(world, pos, 3 + world.rand.nextInt(5));
}

Link to comment
Share on other sites

Do you actually know Java?

 

There's no method with that signature in the

Block

class, so you've created a new method that's never called from anywhere. Always annotate override methods with

@Override

so you get a compilation error if they don't override a super method.

 

I told you to override

Block#getExpDrop

, not call it.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Again, there's no method with that signature so your method doesn't override anything. Look at the source of

Block

class or use your IDE to auto-generate the override method.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Sorry just as you sent that I figured it out, well at least this works and gives me exp, would you say this is the correct way to do it?

 

@Override
    public int getExpDrop(IBlockAccess world, BlockPos metadata, int fortune)
    {
    super.getExpDrop(world, metadata, fortune);
    return 2;
    }

Link to comment
Share on other sites

That will work, but why are you calling the super method? It does literally nothing except return 0, which you're not even using.

 

It will also give a constant amount of XP rather than the random amount you were trying to achieve before.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

This was the only way to make it drop 2 exp though for me I mean, also yeah I still need to drop random amounts (same as emeralds)

 

Edit: Wait I changed "super." to "this." will that be better then?

 

Edit 2: Never mind changing it to this makes me drop no exp, why shouldn't I be using super though? It seems to make it work?

Link to comment
Share on other sites

The super method does literally nothing, so there's no reason to call it. It doesn't actually break anything, though.

 

Calling a method from itself unconditionally is infinite recursion. This will cause a stack overflow error.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Within 2 seconds, going from Block via CTRL+F to emerald ore, to the BlockOre class of minecraft I found this:

 

MathHelper.getRandomIntegerInRange(rand, 3, 7);

 

which is used to determine how much EXP is generated by mining emerald ore.

 

Sincerely -pick

Since English is not my mother tongue, my sentences may are confusing.

 

I'm coding java for a long time now - just MC and forge stop me sometimes.

Link to comment
Share on other sites

Within 2 seconds, going from Block via CTRL+F to emerald ore, to the BlockOre class of minecraft I found this:

 

MathHelper.getRandomIntegerInRange(rand, 3, 7);

 

which is used to determine how much EXP is generated by mining emerald ore.

 

Sincerely -pick

 

Is this correct so far then?

 

@Override
    public int getExpDrop(IBlockAccess world, BlockPos metadata, int fortune)
    {
    super.getExpDrop(world, metadata, fortune);
    MathHelper.getRandomIntegerInRange(rand, 3, 7);
    //return 20;
    }

Link to comment
Share on other sites

Do you actually know Java?

 

1. Why do you post code with *obvious* compilation error(s)?

 

2. Why do you call super ? You are not using it.

 

3. Why do you not use the random number?

 

So the answer is *no*.

 

I'd highly suggest to first learn java or at least how object oriented programming works. Then come back and learn forge.

 

Sincerely -pick

Since English is not my mother tongue, my sentences may are confusing.

 

I'm coding java for a long time now - just MC and forge stop me sometimes.

Link to comment
Share on other sites

So is this how I do it? I had a look at the minecraft code itself (I couldn't find it before) and I used this code and changed it so that it does my ores instead.

 

@Override
    public int getExpDrop(net.minecraft.world.IBlockAccess world, BlockPos pos, int fortune)
    {
        IBlockState state = world.getBlockState(pos);
        Random rand = world instanceof World ? ((World)world).rand : new Random();
        if (this.getItemDropped(state, rand, fortune) != Item.getItemFromBlock(this))
        {
            int i = 0;

            if (this == MAGems.SapphireOre)
            {
                i = MathHelper.getRandomIntegerInRange(rand, 1, 3);
            }
            
            else if (this == MAGems.RubyOre)
            {
                i = MathHelper.getRandomIntegerInRange(rand, 30, 40);
            }

            return i;
        }
        return 0;
    }

    public int getDamageValue(World worldIn, BlockPos pos)
    {
        return 0;
    }

Link to comment
Share on other sites

Sort of...

 

First of all, you should avoid calling

obj == other

except for null comparison.

 

And second, what is the intention of the most outer if-statement?

Since English is not my mother tongue, my sentences may are confusing.

 

I'm coding java for a long time now - just MC and forge stop me sometimes.

Link to comment
Share on other sites

So what should I do?

I think the others were too ambitious when they told you to learn Java. First you must learn object-oriented programming (and its concept of inheritance). *Then* learn Java, and then look at your code again. After applying what you learned, your questions will pertain to Forge knowledge gaps rather than fundamental coding errors, and we will be happy to help.

 

As the forum description says, this is not a Java classroom.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

So what should I do?

I think the others were too ambitious when they told you to learn Java. First you must learn object-oriented programming (and its concept of inheritance). *Then* learn Java, and then look at your code again. After applying what you learned, your questions will pertain to Forge knowledge gaps rather than fundamental coding errors, and we will be happy to help.

 

As the forum description says, this is not a Java classroom.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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.