Root Cause of removeBlock

Trying to remove a block using Location.removeBlock(Cause).

Keep getting this error:
[Server] INFO java.lang.IllegalArgumentException: PluginContainer must be at the ROOT of a cause!

Code
block.getLocation().get().removeBlock(Cause.source(Sponge.getPluginManager().fromInstance(plugin)).build());

The code above was found in this thread:

Also the code example from the docs does not work:
https://docs.spongepowered.org/master/en-GB/plugin/blocks/modifying.html

Must be a simple mistake…?

Using API: 5.1.0-SNAPSHOT

This returns an Optional<PluginContainer>, you need to call get() on it

block.getLocation().get().removeBlock(Cause.source(Sponge.getPluginManager().fromInstance(plugin).get()).build());
1 Like

Thanks. That fixed that but now it doesn’t error but just doesn’t remove the block.

This code is executed in the block break event (ChangeBlockEvent.Break) just after the event is cancelled. Is that likely to cause a problem?

PS: It does reach the spawnBlocks call.

Yes, it is. Cancelling an event cancels all the actions undertaken because of it.

1 Like

What’s the best way around this?

Start a schedule to remove the block immediately?

Well, that raises the question - what are you trying to do by canceling it?

Essentially a long-standing (and reported) bug caused by sponge with a mod causes 3x the amount of blocks to be dropped than is expected (you cannot change this via the mod’s config).

I was looking to cancel the break event if the block and tool are correct then set the block to air and drop the correct amount of blocks.

Don’t change the break event, then; that’s not what you’re looking to modify. Instead, store the Location (and invalidate it after one tick). Then, check for a DropItemEvent.Destruct; if it’s at that location, invalidate the location there as well, and modify the drops from there.

1 Like

That sounds good.

Another question (entirely unrelated): how do you teleport someone to the world’s spawn.

I know how to teleport and I know how to spawn but they seem to give/take incompatible types (Vector3i and Location).

Thanks

Location is relative to an Extent (World in this case), while Vector3i is simply a set of 3 integers. Location has a method getBlockPosition() which converts it to a Vector3i, and Extent has a method getLocation() that converts a Vector3i to a Location.

1 Like