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.

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

Featured Replies

Posted

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

  • Author

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!

  • Author
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

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.