Why is this not working? Code (Text): @EventHandler public void onPlace(BlockPlaceEvent e) { Bukkit.broadcastMessage("g"); Block b = e.getBlockPlaced(); Player p = e.getPlayer(); if(b.getType() == Material.CHEST) { Bukkit.broadcastMessage("gg"); if(p.getItemInHand().hasItemMeta()) { if(p.getItemInHand().getItemMeta().hasDisplayName()) { Bukkit.broadcastMessage("ggg"); String name = p.getItemInHand().getItemMeta().getDisplayName(); if(name.equals(Main.color("&3g"))) { Bukkit.broadcastMessage("gggg"); 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.
try getting the itemstack directly from the event. https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/BlockPlaceEvent.html#getItemInHand()
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
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
Hey @Creepermanthe3rd there isnt a "getAction()" over that event, please, would you be so kind to upload the code to http://hastebin.com so I can check it? I'd be glad to help.
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
But I can check if the item in players hand has a displayanme or itemmeta but cant check the name... strange
@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"))) {
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
@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"); meta.setLore(lore); item.setItemMeta(meta); Then...... ItemStack item2 = (ItemStack) p.getItemInHand(); and compare them if(item == item2) { //CODE IF SAME } else { // CODE IF NOT THE SAME } Tell me if works
https://hub.spigotmc.org/javadocs/b...tml#isSimilar(org.bukkit.inventory.ItemStack) (overall would be good to compare an itemstack(s)) Arrays#asList (lore) (easier to manage in this case since it's not being applied from an external source, etc) 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): @EventHandler public void onBlockPlace(BlockPlaceEvent event) { ItemStack eventItem = event.getItemInHand(); if (eventItem.getItemMeta().hasDisplayName() && eventItem.getItemMeta().getDisplayName().equals(...)) { //other checks }
debug it. output the item itself, its item meta as well, see whats being presented upon checking your conditions.
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.