How to detect when a specific block is being hit with a specific item in hand?

Discussion in 'Spigot Plugin Development' started by LittleBigBug_, May 7, 2015.

  1. The title explains most of it.

    Example:

    I have a flower with a hidden unique 'id' that no other flowers have.
    I right click a Grass block and it does nothing
    but I right click a wood block with another hidden 'id' then that triggers a method
    to tell the player hello.

    Is this possible?

    Or is there a better way to do this?

    Thank you in advance!
     
  2. Check when the player interacts with a blocks if the item they are holding is the correct item, and the block they are interacting with is the correct block.
     
  3. Okay, but lets say I want it to be a chest block. I don't want it to be all chest blocks just that one.
    Same with the item
    There are alot of seeds but I want it to recognize just one.

    Edit:

    Should I store the block data in a config with the position? So like, when a block is right clicked with a flower, go to the config and run through all of the data files and if it matches the position go along with the method?

    So what would i do if my config was:

    Code (Text):
    ItemId:
        Name: "&3Epic Block" #Display name, if you need it
        Position: xyz (coords)
    CoolerId:
        Name: "&2Best Block Ever"
        Position: xyz
    But my question is, I want the server admin to configure 'ItemID' so what if it was anything?
    How can I check the position in each block?
     
    #3 LittleBigBug_, May 7, 2015
    Last edited: May 7, 2015
  4. this is only example how to open the chest with flower
    Someone to correct me if im wrong
    Code (Java):

      @EventHandler
      public void onPlayerInteracttt(PlayerInteractEvent e)
      {
        Player p = e.getPlayer();
            if ((e.getItem().getType() == Material.FLOWER_POT_ITEM) && (e.getItem().getItemMeta().getDisplayName().equals("The amazing flower"))) { //check if the material is flower and if it's called "the amazing flower"
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) //check if the action is rignt click
            {
              if (p.hasPermission("permission.yey")) //if the player have the permission
              {
                 if (e.getClickedBlock().getType() == Material.CHEST){
                //open your menu here
                e.setCancelled(true); //cancel the event so, he will not put the chest on the floor
                p.sendMessage("You opened the menu. YEY"); //send message that player opened the menu
                return;
              }
              }
              p.sendMessage("You dont have permission"); //no permission message
            }
            return;
          }
        }
     
     
    • Friendly Friendly x 1

  5. Thank you but, I dont want the flower to be used with any chest. But this is okay. I found an alternative, checking the display name of the 'flower' like you did, but check the chest coords from a configuration.
     
    • Like Like x 1
  6. A few tips:
    - Displayname might be changed through anvil
    - You can set a specific lore to identify the item
    - If you use formatting codes, your identify string isn't visible to players
    - You can also remove lines of lores through ProtocolLib
    - Via nms you can add / check for nbt tags
     
    • Useful Useful x 1

  7. Thank you!
    This is understandable.
    I will use this type of method instead