Changing a block type has incorrect cause

Hello, I am having an issue trying locate the cause of a block modification.

@Listener
public void onInteract(InteractBlockEvent event, @First Player player) {
    event.getTargetBlock().getLocation().get().setBlockType(BlockTypes.SOUL_SAND);
}

@Listener
public void onBlockModify(ChangeBlockEvent event) {
    event.getCause().all().forEach(c -> {
        System.out.println(c); // Line being referenced
    });
    System.out.println("+-----+");
}

Whenever I right-click a block, you would think that the ChangeBlockEvent listener would print out a cause that says a plugin modified the block. Instead it only lists that a I (the player) modified the block. Is there any way that I can get the cause to show my plugin as what modified the block?

You should use the methods available in Extent to set a block with a Cause.

2 Likes

@gabizou Thanks, but now I am getting an error: PluginContainer must be at the ROOT of a cause!.

@Listener
public void onInteract(InteractBlockEvent event, @First Player player) {
    Location<World> loc = event.getTargetBlock().getLocation().get();
    loc.getExtent().setBlockType(loc.getBlockPosition(), BlockTypes.AIR, true, Cause.of(NamedCause.owner(this)));
}

@Listener
public void onBlockModify(ChangeBlockEvent event) {
    event.getCause().all().forEach(c -> {
        System.out.println(c);
    });
    System.out.println("+-----+");
}

The code is running in the main plugin class.

You need to use your PluginContainer that can be optionally @Injected to your plugin instance class, not the plugin instance itself.

2 Likes