How Do I Check the name of a item

Discussion in 'Plugin Development' started by MikeTheCreator69, Jul 23, 2014.

Thread Status:
Not open for further replies.
  1. Code:
        public void onRight(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Action a = event.getAction();
            if (a.equals(Action.RIGHT_CLICK_BLOCK)) {
                Block block = event.getClickedBlock();
                Material m = player.getItemInHand().getType();
                if (m == Material.FLINT) {
                    FallingBlock fall = block.getWorld().spawnFallingBlock(block.getLocation(), block.getType(), block.getData());
                    Vector veloc = fall.getVelocity();
                    veloc.setY(veloc.getY() + 1);
                    fall.setVelocity(veloc);
                    block.setType(Material.AIR);
                }
            }
        }
    I was wondering how do I check the name of the "Flint" and if its name is "BlockLauncher" then it will launch the block but if its name isnt "BlockLauncher I dont want it to do anything"
     
  2. Offline

    CPUSKILLZ

    Ok so, this is what you do:
    Code:java
    1. public void onRight(PlayerInteractEvent event) {
    2. Player player = event.getPlayer();
    3. Action a = event.getAction();
    4. if (a.equals(Action.RIGHT_CLICK_BLOCK)) {
    5. Block block = event.getClickedBlock();
    6. Material m = player.getItemInHand().getType();
    7. if (m == Material.FLINT) {
    8. ItemStack item = player.getItemInHand();
    9. ItemMeta im = item.getItemMeta();
    10. if(im.hasDisplayName() && im.getDisplayName().equals("BlockLauncher")) {
    11. FallingBlock fall = block.getWorld().spawnFallingBlock(block.getLocation(), block.getType(), block.getData());
    12. Vector veloc = fall.getVelocity();
    13. veloc.setY(veloc.getY() + 1);
    14. fall.setVelocity(veloc);
    15. block.setType(Material.AIR);
    16. }
    17. }
    18. }
    19. }
     
  3. Offline

    DannyDog

    Look into itemMeta.
     
  4. Offline

    Niknea

    CPUSKILLZ Please don't spoonfeed, and if you do, at least give the person code that won't give any errors. You need to check both if the item has ItemMeta, and if the display name isint null.
     
  5. Offline

    CPUSKILLZ

    i knew i left something out :p

    I just wrote this quickly without using eclipse so it was mostly from memory. I haven't done Meta in a while

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page