Jump to content

[SOLVED]How do I make only the player who place the block can break the Block?


AustinTan

Recommended Posts

How do I make only the player who place the block can break the Block?

I dont know the problem is that I used the wrong method to get the name of the player who placed the block or that I used the canHarvestBlock in a wrong way.

I am very new to this !

 

This is the onBlockPlacedBy and canHarvestBlock:

	@Override
	public void onBlockPlacedBy(World worldIn, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
		if(placer instanceof PlayerEntity) {
			permission = placer.getName().toString();
		}
	}	
	
	@Override
	public boolean canHarvestBlock(BlockState state, IBlockReader world, BlockPos pos, PlayerEntity player) {
		if(player.getName().getString() == permission) {
			return state.getBlock().canHarvestBlock(state, world, pos, player);
		}
		return false;
	}

Permission variable: 

public static String permission = null;

 

EDIT: I solve the problem above but now my problem is that I the method I used was wrong! I don't know what method should I use in order to not let player to break the block

Edited by AustinTan
new update
Link to comment
Share on other sites

Sorry for the late reply!
Now that I store the data in the title entity and then I realized that I used the wrong method for what I am trying to do I want to set so that the player cannot break the block not cannot get the block after it break do you know what method should I use? Because I don't and I've been searching for quite sometime and I still have no idea!

Link to comment
Share on other sites

28 minutes ago, diesieben07 said:

You need to subscribe to PlayerEvent.BreakSpeed and set the speed to 0 if you want it to be unminable.

I used this but it's still the same what did I do wrong?

event.setNewSpeed(0);

EDIT: it worked I fixed it Thanks you!

Edited by AustinTan
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.