Solved How can I get the block where my sign is ?

Discussion in 'Plugin Development' started by hugoGentil, Dec 14, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    hugoGentil

    Hello,

    I develop a plugin for 1.15 and i want to get the block who its use as a wall by the sign. I don't know how to do it. I tried to get the facing of the block with org.bukkit.material.Sign but eclipse say its deprecated and generate an error in my console


    Code:
        @EventHandler
        public void onInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Action action = event.getAction();
           
            if(event.getClickedBlock() != null && action == Action.RIGHT_CLICK_BLOCK) {
                BlockState bs = event.getClickedBlock().getState();
               
                if(bs instanceof Sign) {
                    Sign sign = (Sign) bs;
                    if(sign.getType() == Material.OAK_WALL_SIGN) {
                        //Get block where the sign is on
                    }
                   
                   
                }
               
            }
           
        }
    Thank you for your time
     
  2. @hugoGentil I'm not sure i understand what you mean but, you can get the blocks next to your specified block by looping through BlockFace#values() and then use block#getRelative(BlockFace)
     
  3. Offline

    Strahan

    I had to get the block a sign is attached to for one of my plugins, I did it like this:

    Code:
    @EventHandler
    public void onInteract(PlayerInteractEvent e) {
      Block attached = getAttached(e.getClickedBlock());
      if (attached == null) return;
    
      e.getPlayer().sendMessage("Sign attached to " + attached.getType().name());
      return;
    }
    
    private Block getAttached(Block b) {
      if (!(b.getBlockData() instanceof Sign) && !(b.getBlockData() instanceof WallSign)) return null;
    
      if (b.getBlockData() instanceof Directional) {
        Directional d = (Directional)b.getBlockData();
        return b.getRelative(d.getFacing().getOppositeFace());
      }
    
      return b.getRelative(BlockFace.DOWN);
    }
     
  4. Offline

    hugoGentil

    Thank you so much ! @Strahan It work perfectly
     
Thread Status:
Not open for further replies.

Share This Page