Block ItemMeta

Discussion in 'Plugin Development' started by 17nhammond, Jan 9, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    17nhammond

    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
     
  2. Offline

    JRL1004

    17nhammond Would this work? I have tested it.
    Code:java
    1.  
    2.  
    3. @EventHandler
    4. public void onPlayerDeathEvent(BlockPlaceEvent event)
    5. {
    6. event.setCancelled(true);
    7. ItemMeta meta = event.getPlayer().getItemInHand().getItemMeta();
    8. event.setCancelled(false);
    9. }
     
  3. Offline

    Warreo

  4. Offline

    17nhammond

    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!
     
  5. Offline

    Warreo

    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? :)
     
  6. Offline

    17nhammond

    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!
     
  7. Offline

    JRL1004

    17nhammond Use a HashMap<Location, ItemMeta> ior <Location, ItemStack> ?
     
  8. Offline

    Warreo

    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. :)
     
  9. Offline

    17nhammond

  10. Offline

    JRL1004

    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
    1. public class Demo implements Listener {
    2. public static final HashMap<Location, ItemStack> blockStore = new HashMap<Location, ItemStack>();
    3.  
    4. @EventHandler
    5. public void blockPlaced(BlockPlaceEvent event) {
    6. if (event.getBlock().getState() instanceof Furnace) {
    7. Location location = event.getBlock().getLocation();
    8. event.setCancelled(true);
    9. ItemStack stack = event.getPlayer().getItemInHand();
    10. stack.setAmount(1);
    11. blockStore.put(location, stack);
    12. event.setCancelled(false);
    13. }
    14. }
    15.  
    16. @EventHandler
    17. public void blockBroke(BlockBreakEvent event) {
    18. if (event.getBlock().getState() instanceof Furnace && blockStore.containsKey(event.getBlock().getLocation())) {
    19. event.setCancelled(true);
    20. event.getBlock().setType(Material.AIR);
    21. event.getPlayer().getWorld().dropItemNaturally(event.getBlock().getLocation().add(0.5, 0.5, 0.5), blockStore.get(event.getBlock().getLocation()));
    22. blockStore.remove(event.getBlock().getLocation());
    23. }
    24. }
    25. }
    26.  
     
  11. Offline

    17nhammond

    JRL1004
    Okay I am going to test it.
    Thanks man!
     
  12. Offline

    Warreo

    Here is a very basic version and I'm not sure if it will work:
    Code:java
    1. HashMap<Location, String> data = new HashMap();
    2.  
    3. @EventHandler
    4. public void onPlaced(BlockPlaceEvent event) {
    5. if (event.getItemInHand() != null && event.getItemInHand().hasItemMeta()) {
    6. if (event.getItemInHand().getItemMeta().hasDisplayName()) {
    7. data.put(event.getBlock().getLocation(), event.getItemInHand().getItemMeta().getDisplayName());
    8. }
    9. }
    10. }
    11.  
    12. @EventHandler
    13. public void onBlockBroken(BlockBreakEvent event) {
    14. for (Location loc : data.keySet()) {
    15. if (event.getBlock().getLocation().equals(loc)) {
    16. try {
    17. for (Entity ent : event.getBlock().getLocation().getWorld().getEntities()) {
    18. if (ent instanceof Item) {
    19. Item item = (Item) ent;
    20. ItemStack j = item.getItemStack();
    21. if (item.getLocation().equals(event.getBlock().getLocation()) && j.getType() == event.getBlock().getType()) {
    22. ItemMeta k = j.getItemMeta();
    23. k.setDisplayName(data.get(loc));
    24. j.setItemMeta(k);
    25. //Sucess
    26. }
    27. //else no items, or couldn't find it
    28. }
    29. }
    30. } catch (Exception ex) {
    31. //Couldn't find the item
    32. }
    33. return;
    34. }
    35. }
    36. }
     
  13. Offline

    17nhammond

    JRL1004
    When I mine the object I can't even pick it up the object that I created.
    Thanks!
     
  14. Offline

    JRL1004

  15. Offline

    17nhammond

    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
    1. package me.NateD101.furnacespeeder;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7.  
    8.  
    9.  
    10. import org.bukkit.block.Block;
    11. import org.bukkit.block.Furnace;
    12. import org.bukkit.entity.Entity;
    13. import org.bukkit.entity.Item;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.block.BlockBreakEvent;
    17. import org.bukkit.event.block.BlockPlaceEvent;
    18. import org.bukkit.event.inventory.FurnaceBurnEvent;
    19. import org.bukkit.event.inventory.FurnaceSmeltEvent;
    20. import org.bukkit.event.inventory.InventoryClickEvent;
    21. import org.bukkit.inventory.ItemStack;
    22. import org.bukkit.inventory.meta.ItemMeta;
    23.  
    24.  
    25.  
    26. public class EventListener implements Listener {
    27. HashMap<Location, String> data = new HashMap();
    28.  
    29. @EventHandler
    30. public void onPlaced(BlockPlaceEvent event) {
    31. if (event.getItemInHand() != null && event.getItemInHand().hasItemMeta()) {
    32. if (event.getItemInHand().getItemMeta().hasDisplayName()) {
    33. data.put(event.getBlock().getLocation(), event.getItemInHand().getItemMeta().getDisplayName());
    34. }
    35. }
    36. }
    37.  
    38. @EventHandler
    39. public void onBlockBroken(BlockBreakEvent event) {
    40. for (Location loc : data.keySet()) {
    41. if (event.getBlock().getLocation().equals(loc)) {
    42. try {
    43. for (Entity ent : event.getBlock().getLocation().getWorld().getEntities()) {
    44. if (ent instanceof Item) {
    45. Item item = (Item) ent;
    46. ItemStack j = item.getItemStack();
    47. if (item.getLocation().equals(event.getBlock().getLocation()) && j.getType() == event.getBlock().getType()) {
    48. ItemMeta k = j.getItemMeta();
    49. k.setDisplayName(data.get(loc));
    50. j.setItemMeta(k);
    51. //Sucess
    52. }
    53. //else no items, or couldn't find it
    54. }
    55. }
    56. } catch (Exception ex) {
    57. //Couldn't find the item
    58. }
    59. return;
    60. }
    61. }
    62. }
    63.  
    64. Short cooktime = (short)100;
    65.  
    66. @EventHandler
    67. public void furnaceBurn(FurnaceBurnEvent event) {
    68. Furnace furnace = (Furnace) event.getBlock().getState();
    69. furnace.setCookTime(cooktime);
    70. }
    71.  
    72. @EventHandler
    73. public void furnaceSmeltEvent(FurnaceSmeltEvent event) {
    74. Furnace furnace = (Furnace) event.getBlock().getState();
    75. furnace.setCookTime(cooktime);
    76. }
    77.  
    78. @EventHandler
    79. public void onInventoryClick(InventoryClickEvent event) {
    80. Block blocktype = event.getWhoClicked().getTargetBlock(null, 10);
    81.  
    82. if (blocktype.getType() == Material.FURNACE || blocktype.getType() == Material.BURNING_FURNACE) {
    83. if ((event.getSlot() == 0 || event.getSlot() == 1) && event.getCursor().getType() != Material.AIR) {
    84. Furnace furnace = (Furnace) blocktype.getState();
    85. furnace.setCookTime(cooktime);
    86. furnace.getMetadata(null);
    87. }
    88. }
    89. }
    90.  
    91.  
    92.  
    93. }






    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.
     
    Last edited by a moderator: Jun 6, 2016
  16. Offline

    JRL1004

    17nhammond Just cast the furnace when it is placed and do setCookTime()
     
  17. Offline

    17nhammond

    JRL1004
    Could I please have an example?
    Thanks!
    NateD101
     
  18. Offline

    JRL1004

    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
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page