Anyone know how to listen for interaction with Block Display? PlayerInteractEvent, PlayerInteractEntityEvent, nor PlayerInteractAtEntityEvent works. Works for armor stands just not my BlockDisplays.
sorry for the messy code. my idea was to make a ray to check if the blockdisplay is in that ray. the problem is that the origin or hitbox of the blockdisplay isnt in the middle of the displayed block. the gif shows exactly that problem. Code: @EventHandler public void onBlockDisplayClick(PlayerInteractEvent event) { float errorMargin = 1f; int rayLength = 8; Location playerLocation = event.getPlayer().getEyeLocation().clone(); Vector defaultLocation = playerLocation.getDirection().clone().add(playerLocation.getDirection().clone()); for (int i = 0; i < rayLength; i++) { playerLocation.add(defaultLocation.clone().add(playerLocation.getDirection().clone().multiply(0.25f))); for (Entity ent : playerLocation.getWorld().getNearbyEntities(playerLocation, errorMargin, errorMargin, errorMargin)) { if (ent != null && ent instanceof BlockDisplay display) { // display was hit return; } } } }
Ok, makes sense. Thanks for the code. Too bad there isn't a way to use PlayerInteractAtEntityEvent but PlayerInteractEvent will do. I'll test this out in a bit.
Code: @EventHandler public void onBlockDisplayClick(PlayerInteractEvent event) { float errorMargin = 1f; int rayLength = 8; Location playerLocation = event.getPlayer().getEyeLocation().clone(); Vector defaultLocation = playerLocation.getDirection().clone().add(playerLocation.getDirection().clone()); for(int i = 0; i < rayLength; i++) { playerLocation.add(defaultLocation.clone().add(playerLocation.getDirection().clone().multiply(0.25f))); for (Entity ent : playerLocation.getWorld().getNearbyEntities(playerLocation, errorMargin, errorMargin, errorMargin)) { if(ent instanceof BlockDisplay) { event.getPlayer().sendMessage("Block Display hit"); return; } } } } Yes, it does. Only thing is that the event fires twice. I removed ent != null because it was redundant, and also added a #sendMessage() statement to the player letting them know it works. It can be modified to detect BlockDisplays from further away, currently it only works from distances of one or or two blocks away. I also think you could probably use the BlockIterator class instead of the for-loop.
Make sure to check if one hand is being used, interactEvent fires for each hand. rayLength would be the length of how far location will go. haha, didn't see that this was solved...