I am trying to get text from an in-game sign as a string, when the player clicks on that sign. Following the new changes made in Minecraft, which allow signs to be edited on both sides, many of the old methods for doing this have been deprecated. With the new signs, you first have to getSide()
before reading the sign lines. Unfortunately, it doesn't seem that the PlayerInteract event offers a method for returning a SignSide, with the closest thing being a BlockFace.
My current code looks like this:
@EventHandler(ignoreCancelled = true)
void onPlayerInteract(PlayerInteractEvent event) {
//Check that the player clicked a block
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return;
}
//Check that the block is a sign
Block block = event.getClickedBlock();
if (block == null) {
return;
}
if (!(block.getState() instanceof Sign)) {
return;
}
//Here is where I need to get the clicked face of the sign, and return a given
//line on the sign as a String
}
If anyone could provide a bit more info on how this could be done, it would be much appreciated.
Answering this myself, since the feature has now been added to spigot. See here for more information:
With this method, you can parse in the player object and get the side of the signed returned which the player clicked on.
An example of this within a PlayerInteract event can look something like this:
@EventHandler(ignoreCancelled = true)
void onPlayerInteract(PlayerInteractEvent event) {
// Check that the player clicked a block
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) {
return;
}
// Get clicked block from event
Block block = event.getClickedBlock();
// Check that the block is a sign
if (block == null || !(block.getState() instanceof Sign)) {
return;
}
// Cast block to sign
Sign sign = (Sign) block.getState();
//Get the side of the sign which was clicked
SignSide side = sign.getTargetSide(event.getPlayer());
// Read the text on the given sign side
String line = side.getLine(1);
// Alternatively, get all lines as an array
String[] lines = side.getLines();
}
More information relating to SignSide can be found on the Spigot Javadocs here https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/block/sign/SignSide.html