Solved Listen for Interaction with Block Display

Discussion in 'Plugin Development' started by Smeary_Subset, Aug 3, 2024.

  1. Offline

    Smeary_Subset

    Anyone know how to listen for interaction with Block Display? PlayerInteractEvent, PlayerInteractEntityEvent, nor PlayerInteractAtEntityEvent works. Works for armor stands just not my BlockDisplays.
     
  2. Offline

    DopeBrot

    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;
                    }
                }
            }
    
    
        }
    
     

    Attached Files:

    • gif.gif
      gif.gif
      File size:
      306.5 KB
      Views:
      4
  3. Offline

    Smeary_Subset


    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.
     
  4. Offline

    timtower Administrator Administrator Moderator

    Can you prove it?
     
  5. Offline

    Smeary_Subset


    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.
     
    DopeBrot likes this.
  6. Offline

    Hellborn5456

    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...
     

Share This Page