Handle Player Block Destroy Events

Hey!

I was wondering if anyone could show me how to handle when players destroy a block. I want my plugin to be able to determine if a player has the ability to edit somewhere (based on a file I save and manage). Basically, could somebody show me the method I should use that’s called when a player destroys a block? And then how I should prevent the block from being destroyed please?

Anything you can show me or tell me will be greatly appreciated. Thank you so much!

How to work with Events

The specific event you’re looking for is the PlayerBlockBreakEvent

Moved to the appropriate category.

Perfect thanks! I was looking at BlockBreakEvent on accident, not PlayerBlockBreakEvent!

Okay, I successfully canceled their block edit, however when I do the following, it doesn’t send the player a message. Any tips?

Player p = event.getPlayer();
p.sendMessage(Texts.of(TextColors.RED, "You don't have permission for this command"));

Also I’m getting the error
[Server thread/WARN] [io.netty.channel.ChannelOutboundBuffer]: Failed to mark a promise as success because it is done already: DefaultChannelPromise@7ca2299a(success)
if that means anything, a lot!

Thank you again for any help.

first show me the whole block code for the event, so I can see why the player isn’t sent the message.
second that is a Netty issue, you can read about it here… netty is bumping this issue because they don't think it's important, but spamming every modded minecraft player with nonsense matters · Issue #1848 · MinecraftForge/MinecraftForge · GitHub

Sorry, here’s all of my Main:

package com.MyName.SpongePlugin;

import org.spongepowered.api.Game;
import org.spongepowered.api.entity.player.Player;
import org.spongepowered.api.event.Subscribe;
import org.spongepowered.api.event.entity.player.PlayerBreakBlockEvent;
import org.spongepowered.api.event.state.ServerStartingEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.text.Texts;
import org.spongepowered.api.text.format.TextColors;

import com.google.inject.Inject;

import org.slf4j.Logger;

@Plugin(id = "PluginID", name = "PluginName", version = "1.0", dependencies = "")
public class Main {
	
	@Inject
	Game game;

        @Inject
	Logger logger;
	
	@Subscribe
	public void onServerStarting(ServerStartingEvent event) {
		logger.info("WOOOOHOHOOHOHOOOO CUSTOM PLUGIN WORKED!");
	}
	public Logger getLogger() {
	    return logger;
	}
	
	@Subscribe
	public void onPlayerBreakBlockEvent(final PlayerBreakBlockEvent event) {
		event.setCancelled(true);
		Player p = event.getPlayer();
		p.sendMessage(Texts.of(TextColors.RED, "You don't have permission for this command"));
	}
}

Sorry about the weird spacing. And thank you so much again!

In the line Player p = event.getPlayer(); getPlayer() isn’t a method withing event.
use Player p = event.getUser();
Also I would add a check to see if the player has permission inside of the event so at least certain people can break blocks.

I should’ve seen this before without looking at your entire class.

Awesome, that makes a lot of sense. I was just trying to see if I could disable editing at all before checking permissions. Also, do you have any resources on how to properly send a message? I looked at the documentation and forums and couldn’t find something say the proper way. And while I’m asking, do you know of any tutorials on how to read permissions and/or read/write to files? If reading/writing is just like normal java, let me know, but it’d really help to have something to refer to. Thanks @Azewilous, you’re the best!

If you know how to make a config directory and multiple config, you could make a permissions config file, then write permission strings to them. I have an example if you want.

Idk if that’s how you want to do permissions though

Yeah I’d love to see that! And also if you have any information on managing permissions that would be great too!

Config Files are already explained in the Sponge Docs. The Docs on the Permissions API are unfortunately not completed yet.

Oh okay, well I’ll just keep watching for info on the permissions API. Thanks for the help!