Hi, I am wondering if there is a way to get the ItemMeta from a block after it has been placed. For example you place a block and you need a way to check if it has a certain name or lore. Thanks, NateD101
17nhammond Would this work? I have tested it. Code:java @EventHandler public void onPlayerDeathEvent(BlockPlaceEvent event) { event.setCancelled(true); ItemMeta meta = event.getPlayer().getItemInHand().getItemMeta(); event.setCancelled(false); }
17nhammond In BlockPlaceEvent getItemInHand() will return the item in the player's hand when the placed the block. http://jd.bukkit.org/rb/apidocs/org/bukkit/event/block/BlockPlaceEvent.html#itemInHand Code:java @EventHandlerpublic void onBlockPlaced(BlockPlaceEvent event) {ItemStack item = event.getItemInHand();ItemMeta meta = item.getItemMeta();//DoStuff}
Warreo JRL1004 Thanks for the advice, but this is not exactly what I need. I was wondering how to get the ItemMeta after the block has been mined? Like after it has been mined I need the ItemMeta of it before it was mined. Thanks!
17nhammond It would no longer exist, it's just how Minecraft works, but there's plenty of ways around whatever it is you're trying to accomplish, I'm willing to bet. So what exactly is it you need the ItemMeta for?
Warreo JRL1004 I need it to keep the name of the item. For example I renamed a furnace to a different name and when I mine it I want to to still hold that name, but now when I mine something when its renamed it doesn't keep the name? Tell me if this doesn't make sense. Thanks!
17nhammond Like I said, as soon as you place the block it looses all of its ItemMeta, since it is now a block. My recommendation would be to do something like this: When a block with the custom name is placed, add it to a HashMap<Location, String>. Then whenever a block is mined check its location to all of those in the HashMap if its location matches one, get that location's String from the HashMap and set the ItemStack's, which was mined, name to the String.
17nhammond Okay, I made this really basic class to provide an example (I've cropped the registering, imports, and package to save space on this post: Code:java public class Demo implements Listener { public static final HashMap<Location, ItemStack> blockStore = new HashMap<Location, ItemStack>(); @EventHandler public void blockPlaced(BlockPlaceEvent event) { if (event.getBlock().getState() instanceof Furnace) { Location location = event.getBlock().getLocation(); event.setCancelled(true); ItemStack stack = event.getPlayer().getItemInHand(); stack.setAmount(1); blockStore.put(location, stack); event.setCancelled(false); } } @EventHandler public void blockBroke(BlockBreakEvent event) { if (event.getBlock().getState() instanceof Furnace && blockStore.containsKey(event.getBlock().getLocation())) { event.setCancelled(true); event.getBlock().setType(Material.AIR); event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation().add(0.5, 0.5, 0.5), blockStore.get(event.getBlock().getLocation())); blockStore.remove(event.getBlock().getLocation()); } }}
Here is a very basic version and I'm not sure if it will work: Code:java HashMap<Location, String> data = new HashMap(); @EventHandler public void onPlaced(BlockPlaceEvent event) { if (event.getItemInHand() != null && event.getItemInHand().hasItemMeta()) { if (event.getItemInHand().getItemMeta().hasDisplayName()) { data.put(event.getBlock().getLocation(), event.getItemInHand().getItemMeta().getDisplayName()); } } } @EventHandler public void onBlockBroken(BlockBreakEvent event) { for (Location loc : data.keySet()) { if (event.getBlock().getLocation().equals(loc)) { try { for (Entity ent : event.getBlock().getLocation().getWorld().getEntities()) { if (ent instanceof Item) { Item item = (Item) ent; ItemStack j = item.getItemStack(); if (item.getLocation().equals(event.getBlock().getLocation()) && j.getType() == event.getBlock().getType()) { ItemMeta k = j.getItemMeta(); k.setDisplayName(data.get(loc)); j.setItemMeta(k); //Sucess } //else no items, or couldn't find it } } } catch (Exception ex) { //Couldn't find the item } return; } } }
JRL1004 Wait I am still trying the new one you just posted, but the first one is the one that didn't work. Thanks! JRL1004 Here is the Event Listener! Code:java package me.NateD101.furnacespeeder; import java.util.HashMap; import org.bukkit.Location;import org.bukkit.Material; import org.bukkit.block.Block;import org.bukkit.block.Furnace;import org.bukkit.entity.Entity;import org.bukkit.entity.Item;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.block.BlockBreakEvent;import org.bukkit.event.block.BlockPlaceEvent;import org.bukkit.event.inventory.FurnaceBurnEvent;import org.bukkit.event.inventory.FurnaceSmeltEvent;import org.bukkit.event.inventory.InventoryClickEvent;import org.bukkit.inventory.ItemStack;import org.bukkit.inventory.meta.ItemMeta; public class EventListener implements Listener { HashMap<Location, String> data = new HashMap(); @EventHandler public void onPlaced(BlockPlaceEvent event) { if (event.getItemInHand() != null && event.getItemInHand().hasItemMeta()) { if (event.getItemInHand().getItemMeta().hasDisplayName()) { data.put(event.getBlock().getLocation(), event.getItemInHand().getItemMeta().getDisplayName()); } } } @EventHandler public void onBlockBroken(BlockBreakEvent event) { for (Location loc : data.keySet()) { if (event.getBlock().getLocation().equals(loc)) { try { for (Entity ent : event.getBlock().getLocation().getWorld().getEntities()) { if (ent instanceof Item) { Item item = (Item) ent; ItemStack j = item.getItemStack(); if (item.getLocation().equals(event.getBlock().getLocation()) && j.getType() == event.getBlock().getType()) { ItemMeta k = j.getItemMeta(); k.setDisplayName(data.get(loc)); j.setItemMeta(k); //Sucess } //else no items, or couldn't find it } } } catch (Exception ex) { //Couldn't find the item } return; } } } Short cooktime = (short)100; @EventHandler public void furnaceBurn(FurnaceBurnEvent event) { Furnace furnace = (Furnace) event.getBlock().getState(); furnace.setCookTime(cooktime); } @EventHandler public void furnaceSmeltEvent(FurnaceSmeltEvent event) { Furnace furnace = (Furnace) event.getBlock().getState(); furnace.setCookTime(cooktime); } @EventHandler public void onInventoryClick(InventoryClickEvent event) { Block blocktype = event.getWhoClicked().getTargetBlock(null, 10); if (blocktype.getType() == Material.FURNACE || blocktype.getType() == Material.BURNING_FURNACE) { if ((event.getSlot() == 0 || event.getSlot() == 1) && event.getCursor().getType() != Material.AIR) { Furnace furnace = (Furnace) blocktype.getState(); furnace.setCookTime(cooktime); furnace.getMetadata(null); } } } } Here is the main class! package me.NateD101.furnacespeeder; import java.util.ArrayList; import java.util.logging.Logger; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ShapedRecipe; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; public class Furnacespeeder extends JavaPlugin { public final Logger logger = Logger.getLogger("Minecraft"); public static Furnacespeeder plugin; public void onDisable() { PluginDescriptionFile pdfFile = getDescription(); this.logger.info(pdfFile.getName() + " Has Been Disabled!"); getServer().clearRecipes(); } public void onEnable() { getServer().getPluginManager().registerEvents(new EventListener(), this); PluginDescriptionFile pdfFile = getDescription(); this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!"); ItemStack ftwo = new ItemStack(Material.FURNACE, 1); ItemMeta furnacetwo = ftwo.getItemMeta(); furnacetwo.setDisplayName(ChatColor.RESET + "Furnace Two"); ArrayList<String> loreftwo = new ArrayList(); loreftwo.add(ChatColor.GOLD + "Furnace with Faster Cooking Time"); furnacetwo.setLore(loreftwo); ftwo.setItemMeta(furnacetwo); ShapedRecipe furnacetwo1 = new ShapedRecipe(new ItemStack(ftwo)); furnacetwo1.shape(new String[]{"SSS", "S S", "SSS"}); furnacetwo1.setIngredient('S', Material.STONE); ItemStack fthree = new ItemStack(Material.FURNACE, 1); ItemMeta furnacethree = fthree.getItemMeta(); furnacethree.setDisplayName(ChatColor.RESET + "Furnace Three"); ArrayList<String> lorefthree = new ArrayList(); lorefthree.add(ChatColor.GOLD + "Furnace with Faster Cooking Time"); furnacethree.setLore(lorefthree); fthree.setItemMeta(furnacethree); ShapedRecipe furnacethree1 = new ShapedRecipe(new ItemStack(fthree)); furnacethree1.shape(new String[]{"CCC", "C C", "CCC"}); furnacethree1.setIngredient('C', Material.COAL); getServer().addRecipe(furnacetwo1); getServer().addRecipe(furnacethree1); } } Okay so this is what I am doing. I am trying to make a new furnace that will speed up the cooking time. I just put your code in the event listener and it still will not show the correct name. Thank you! EDIT by Moderator: merged posts, please use the edit button instead of double posting.
17nhammond Spoonfeed! because I am too lazy to explain it: Code: @EventHandler public void onPlace(BlockPlaceEvent event) { if(event.getBlock().getState() instanceof Furnace) { Furnace furnace = (Furnace) event.getBlock().getState(); furnace.setCookTime(Short.MAX_VALUE); //Because I want to make people wait 5 years for something to smelt } }