Why i cant do custom block on block place event ?

Discussion in 'Spigot Help' started by Liku, Jul 11, 2018.

  1. @EventHandler

    public void onBlockPlace(BlockPlaceEvent e) {
    Player p = (Player) e.getPlayer();
    Block b = e.getBlock();

    ItemStack stoniarka1 = new ItemStack(Material.STONE , 1);
    ItemMeta stoniarka1Meta = stoniarka1.getItemMeta();
    stoniarka1Meta.setDisplayName("§6§lStandardowa Stoniarka");
    stoniarka1.setItemMeta(stoniarka1Meta);

    // What is going on here >>
    if(b.getType().equals(
    stoniarka1)) {
    // What is going on here
    p.sendMessage("§c§lPostawiles standardowa stoniarke!");
    }
    }


    }
    The red code is the problem
    screen to code in eclipse : https://imgur.com/a/cA8hV21
     
  2. You are comparing the Material type of Block b to stoniarka1, which is an ItemStack, not a Material. That's why you're receiving an unlikely value warning. If you're trying to check that they're the same type of item, use stoniarka1.getType(), not stoniarka1.
     
  3. But when I put a stone it detects its stoniarka1. I dont want it
     
  4. I don't understand what you're getting at. The issue should be a simple fix by changing this:
    Code (Java):
    if(b.getType().equals(stoniarka1)) {[/COLOR]
    to this
    Code (Java):
    if(b.getType() == stoniarka1.getType()) {
     
  5. gzx

    gzx

    You're trying to treat a Block as an ItemStack. Try getting the ItemStack that the player placed and comparing its type to stoniarka1.
     
  6. But when im placing a normal stone its detecting stoniarka1
     
  7. gzx

    gzx

    That's because the ItemStack is of type Stone and the Block is of type Stone. The ItemMeta is ignored, since you're only comparing the materials. You need to get the placed block as an ItemStack to compare the meta.
     
    #7 gzx, Jul 12, 2018
    Last edited: Jul 12, 2018
  8. something like this if(b.getType() == stoniarka1Meta) { ?
    it didnt work
     
  9. That's because b.getType() returns a Material, and you're trying to compare a Material against an ItemMeta now. You need to get the ItemStack being used when placing the the block (NOT the block placed), compare the ItemMeta of the ItemStack to stoniarka1Meta using ItemMeta#equals(ItemMeta), and then do whatever you want with the custom block.
     
  10. I do this if(stoniarka1Meta.equals(stoniarka1.getItemMeta())) {
    and still when im placing stone its detecting stoniarka1
     
  11. Please learn the basics of Java, it'll save you a lot of time in the long run.
    stoniarka1Meta is just the ItemMeta of stoniarka1. You're comparing the same objects. If you were doing it properly, you'd be comparing two different objects with the same information.
    You need to get the ItemStack that is being placed - you need to get the ItemStack in the player's hand when/before they place the block, get the ItemMeta of that ItemStack, and then compare it to stoniarka1's ItemMeta/stoniarka1Meta.
     
  12. I finally understand thank you very much. Sorry for the wasted time and i will learn for basics now in free time.
     

Share This Page