Discussion in 'Spigot Plugin Development' started by Creepermanthe3rd, Apr 28, 2017.

  1. Why is this not working?
    Code (Text):

        public void onPlace(BlockPlaceEvent e) {
            Block b = e.getBlockPlaced();
            Player p = e.getPlayer();
            if(b.getType() == Material.CHEST) {
                if(p.getItemInHand().hasItemMeta()) {
                    if(p.getItemInHand().getItemMeta().hasDisplayName()) {
                        String name = p.getItemInHand().getItemMeta().getDisplayName();
                        if(name.equals(Main.color("&3g"))) {
    The event is registered i get all the debug msg's and if i remove the if check for the name then the code unneath all runs.
    Not sure what is is anyhelp would be appreciated.
    • Agree Agree x 1
  2. Because at this point in the code there is no item in hand it's being placed. You should put this in player interact event.

    If action == Action.right_click
    You also need to check if item in hand is null before checking the items type or checking if it has meta.

    Sent from my iPhone using Tapatalk
    • Agree Agree x 1
    • Funny Funny x 1
  3. How do I get the action? there isnt a e.getAction what should i use
  4. In playerInteractEvent there should be e.getAction()

    You'll want to limit it to Right click block not just right click.

    Sent from my iPhone using Tapatalk
  5. FrostedSnowman

    Resource Staff

    try what @ChefJava said
  6. Im using blockplaceevent
    • Optimistic Optimistic x 1
  7. Hey @Creepermanthe3rd there isnt a "getAction()" over that event, please, would you be so kind to upload the code to so I can check it? I'd be glad to help.
  8. Ah yes if there's one in the event it's the one you want, not the player inventory because it's already gone. Try e.getItemInHand()

    Sent from my iPhone using Tapatalk
  9. FrostedSnowman

    Resource Staff

    ??? he put the code in the OP

    use what @ChefJava suggested..
  10. FrostedSnowman

    Resource Staff

    what for..? that's not needed. he told us the outputs and that he registered events, etc
  11. But I can check if the item in players hand has a displayanme or itemmeta but cant check the name... strange
  12. @Creepermanthe3rd So let's see: You want to add an item to a new placed chest? if so let's do some magic!
    Error: inv.addItem(new ItemStack(Material.STONE));
    Fix inv.addItem(new ItemStack(Material.STONE, AMOUNT));

    I think that should work!

    Also some personal recommendations in...
    if(p.getItemInHand().getItemMeta().hasDisplayName() && p.getItemInHand().getItemMeta().getDisplayName().equals(Main.color("&3g"))) {
  13. there is no errors the stone is getting placed in the chest, Its just its not checking the display name but If i dont check for the display name it works perfect
  14. @Creepermanthe3rd
    The best way to do so is like this:

    //We create the item with the desired data to compare
    ItemStack item = new ItemStack(Material.WOOL, 1);
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName(ChatColor.RED + "WASMAKE");
    List<String> lore = new ArrayList<String>();
    lore.add(ChatColor.RED + "IM A LORE LORE 1");
    lore.add(ChatColor.RED + "IM A LORE LORE 2");

    ItemStack item2 = (ItemStack) p.getItemInHand();

    and compare them
    if(item == item2) {
    } else {

    Tell me if works

  15. overall, there is no need for any of this. he can directly check for the contents of certain item meta elements.

    produces same output. by default, specifying no amount in the constructor, defaults to 1.

    did you attempt in checking the itemstack from the event itself?

    Code (Java):

    public void onBlockPlace(BlockPlaceEvent event) {
         ItemStack eventItem = event.getItemInHand();

         if (eventItem.getItemMeta().hasDisplayName() && eventItem.getItemMeta().getDisplayName().equals(...)) {
              //other checks
    #17 ChefJava, Apr 28, 2017
    Last edited: Apr 28, 2017
    • Winner Winner x 1
  16. didnt work
  17. debug it. output the item itself, its item meta as well, see whats being presented upon checking your conditions.
  18. The thing is, you're checking for colorcodes with a '&' symbol; while that will obviously not work.
    Try using the ChatColor enum for the colorcodes instead, like so:
    Code (Java):
    if (item.hasItemMeta() && item.getItemMeta().hasDisplayName() && item.getItemMeta().getDisplayName().equals(ChatColor.AQUA + "g")) {
    // Do stuff?
    Pretty ugly IF-statement, honestly, I'd split it up.