Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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;
		}
	}*/
}

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.

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.

  • Author

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));
}

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.

  • Author

This tells me to remove the @override? Am I going in the right direction though?

 

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

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.

  • Author

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;
    }

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.

  • Author

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?

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.

  • Author

So what should I do? And also once I get the set amount of exp done, could you help me with making it so that its like emerald ore where it drops a random amount?

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.

  • Author

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;
    }

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.

  • Author

If I don't call super then what do I call? When I call super it makes my ore drop exp, even if it's a set amount (instead of a random amount like 3-7) but if I take away super it doesn't drop exp so it may not be the best way but at least it's making my exp drop...

Well, you *actually* call nothing. You should return the random amount of XP you want to get dropped.

 

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.

  • Author

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;
    }

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.

The 'soft of' is related to the above mentioned questions.

 

If it works fine for you, let it as it is.

 

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.

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.

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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.