Jump to content

"Enforce" the required level to break a Block and forbidding others?


Recommended Posts

Posted

Hi ! First of all, I use 1.8.

 

When you override getHarvestLevel(), you set the minimum metal level to harvest something from the block. But previous level can still break the block (which don't drop anything but is still broken).

I don't want it to happen. I would like to :

Ideally, make the level beneath the required very slow to mine. Like breaking a stone with your hands. There should be a higher hardness difference between them.

If not possible, forbid the lower levels to break the block at all.

 

Some events could make it, but I wonder if there was a clean way to do it in the class of the block ?

Posted

It is unlikely you'll be able to do this at the block level, but there is

getPlayerRelativeBlockHardness(EntityPlayer, World, int, int, int)

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

You might try to capture a mining event:

 

import net.minecraftforge.event.entity.player.PlayerEvent.BreakSpeed;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

<snip>

@SubscribeEvent
  public void onHitting(BreakSpeed e) {
    <snip>
         e.newSpeed = 0.0F;
  }

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.

Posted

i am not familiar with the changes in 1.8 but in 1.7 you were able to use the BreakBlock event it is called as soon as you start hitting a block. just make a simple check to see if the player has the required level to do so and if not cancel the event.

Posted

I still haven't found a solution to this problem. BreakSpeed doesn't change the hardness, and getPlayerRelativeBlockHardness acts strangely.

Try yourself, if you put

	@Override
public float getPlayerRelativeBlockHardness(EntityPlayer playerIn,
		World worldIn, BlockPos pos) {
	return 5.5F;
}

In your Block, then it will have a 0.0F and not a 5.5F.

Posted

I still haven't found a solution to this problem. BreakSpeed doesn't change the hardness, and getPlayerRelativeBlockHardness acts strangely.

Try yourself, if you put

	@Override
public float getPlayerRelativeBlockHardness(EntityPlayer playerIn,
		World worldIn, BlockPos pos) {
	return 5.5F;
}

In your Block, then it will have a 0.0F and not a 5.5F.

 

That's not how that function works, actually.

 

The reason it "acts" like 0.0 is because in a single tick you said that the player does "5.5 breaking strength worth of progress" towards breaking the block (once progress > hardness, the block breaks).  To make the block act like it has 5.5 hardness, instead of its standard you'd have to do this:

 

public float getPlayerRelativeBlockHardness(EntityPlayer player, World world, int x, int y, int z) {
    return 1 / 5.5f / 30;
}

 

For example, here's a block I have that I wanted to have a correct-tool for the hoe, but the hoe isn't a tool, so I had to figure out this math and series of calls on my own from other functions:

 

    public float getPlayerRelativeBlockHardness(EntityPlayer player, World world, int x, int y, int z) {
//get the default breaking speed for this block
//this is approximately (1 / hardness / 30) but there are other modifiers, such as being under water, jumping, and potions
        float f = ForgeHooks.blockStrength(this, player, world, x, y, z);
        ItemStack s = player.getCurrentEquippedItem();
//check for correct tool, this is required as the Hoe does not natively have a dig speed, thus not checked by ForgeHooks.blockStrength()
    	if(s != null && s.getItem() instanceof ItemHoe) {
	//get the tool material and effetiveness manually because hoes aren't tools.
		ToolMaterial m = ToolMaterial.valueOf(((ItemHoe)s.getItem()).getToolMaterialName());
		f += m.getEfficiencyOnProperMaterial();
	//get enchantments
		int i = EnchantmentHelper.getEfficiencyModifier(player);

            if (i > 0 && s != null)
            {
	//enchantments have weird effects.  This is accurate!
                float f1 = (float)(i * i + 1);

                boolean canHarvest = ForgeHooks.canToolHarvestBlock(this, 0, s);

	//if we can't actually harvest the block
                if (!canHarvest && f <= 1.0F)
                {
                    f += f1 * 0.08F;
                }
                else
                {
                    f += f1;
                }
            }
		//finally, take the speed value (f), divide by hardness, and divide again by 30.
		return f / this.blockHardness / 30;
	}
        return f;
    }

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Thank you, that's very interesting.

I'm not sure I understand this step, though :

				
if (!canHarvest && f <= 1.0F) {
f += f1 * 0.08F;
}

You're adding a diminished version of the enchantment speed, why ?

Also, does this take in account the water, jumping and potions you mention in the comments or are they discarded, or does blockStrength() take care of that ?

 

At the moment, my code crash the game as soon as I try to hit a block, but I find the problem is not too big.

EDIT :

float hardness = this.getBlockHardness(worldIn, pos);

is the problem.

EDIT 3 : Success ! Here is the latest version of my function : http://hastebin.com/takokuyawa.java

It's basically a simple "If not the proper tool, then multiply the default speed by X" (Here X=1/3, not 0 so you can still break a block in a long time if you placed yours in front of you by mistake and you don't have any tool).

Thank you a lot.

EDIT 4 : Right now I have a bug, the breaking animation doesn't display on blocks anymore. All blocks. So I think it's unrelated, but I need to find where the Test client is to check as the "run" folder seems to not contain it entirely.

Posted

EDIT 4 : Right now I have a bug, the breaking animation doesn't display on blocks anymore. All blocks. So I think it's unrelated, but I need to find where the Test client is to check as the "run" folder seems to not contain it entirely.

 

No idea.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.