Getting sign lines from ChangeBlockEvent.Break

I’m trying to get the first line of a sign when the sign is broken. This used to be possible by means of bs.getOriginal().get(Keys.SIGN_LINES).get() But this no longer seems possible.

I have also seen people use bs.getOriginal().get(ImmutableSignData.class).get() But this no longer seems possible either.

Some people have also used the ImmutableSignData on the tileentity but you can’t do this with the break event as the tileentity is removed before it can be checked.

What can be used now to get the sign lines from the Break event now?

Code so far:

@Listener
public void onEvent(ChangeBlockEvent.Break e, @Root Player player) {

	for (Transaction<BlockSnapshot> bs : e.getTransactions()) {
		if (bs.getOriginal().getState().getType().equals(BlockTypes.WALL_SIGN)) {
			
			
			
			if (bs.getOriginal().get(ImmutableSignData.class).get().get(0).get().equals(Text.of(TextColors.BLUE, "[portal]"))) {
				Location<World> signLocation = bs.getFinal().getLocation().get();

				Direction direction = bs.getOriginal().get(Keys.DIRECTION).get().getOpposite();

				Location<World> relative = signLocation.getRelative(direction);

				for (Location<World> loc : Portal.getBlocks(
						relative.copy().add(0, -1, 0).getRelative(DirectionExtender.rotateLeft(direction)),
						relative.copy().add(0, -4, 0).getRelative(DirectionExtender.rotateRight(direction)))) {

					loc.setBlock(BlockTypes.WATER.getDefaultState());
				}
			}
		}

	}
}

Your code should work as-is. If not, file a bug report.

Listen to ChangeBlockEvent.Break.Pre.

Listening for the Pre event doesn’t seem to work
It isn’t getting anything when the player breaks a block, it only gets when things like water breaks a block

@Listener
public void onEvent(ChangeBlockEvent.Break.Pre e/*, @Root Player player*/) {
	System.out.println("Brocken " + e.getLocations());
	for (Location<World> loc : e.getLocations()) {
		
		if (loc.getBlock().getType().equals(BlockTypes.WALL_SIGN)) {
			if (loc.getBlock().get(ImmutableSignData.class).get().get(0).get().equals(Text.of(TextColors.BLUE, "[portal]"))) {
				Direction direction = loc.get(Keys.DIRECTION).get().getOpposite();
				Location<World> relative = loc.getRelative(direction);
				for (Location<World> locBlock : Portal.getBlocks(
						relative.copy().add(0, -1, 0).getRelative(DirectionExtender.rotateLeft(direction)),
						relative.copy().add(0, -4, 0).getRelative(DirectionExtender.rotateRight(direction)))) {
					locBlock.setBlock(BlockTypes.WATER.getDefaultState());
				}
			}
		}
	}
}

The code you had in your first post should have worked fine. This is a Sponge problem; I’ll take a look at it.

1 Like