rename default name of placed block

Discussion in 'Plugin Development' started by bladerik, Apr 15, 2013.

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

    bladerik

    hello, what i need to do is simple
    as we can change name of item in inventory, can we somehow change the name of placed block?
    i'm trying to do so, but without success
    Code:
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent evt){
    if (evt.getClickedBlock().getTypeId() == 54 && evt.getAction() == Action.LEFT_CLICK_BLOCK) {
      String blockName = "testName";
      Chest ch = (Chest) evt.getClickedBlock().getState();
      Inventory chest_inv = ch.getInventory();
      if (InventoryUtils.isEmptyInventory(chest_inv)) {
        ch.getBlock().breakNaturally();
        Location loc = ch.getLocation();
        MaterialData chData = ch.getData();
        ItemStack is = new ItemStack(Material.CHEST, 1);
        is.setData(chData);
        ItemMeta im = is.getItemMeta();
        im.setDisplayName(blockName);
        is.setItemMeta(im);
        p.getInventory().addItem(is);
        InventoryUtils.placeItemStack(is, loc);
        }
    }
    }
    Code:
    public class InventoryUtils {
        public static boolean isEmptyInventory(Inventory inv) {
            for (ItemStack item : inv.getContents()) {
              if (item != null) {
                return false;
              }
            }
            return true;
          }
     
        public static void placeItemStack(ItemStack is, Location loc) {
            if (is.getType().isBlock()) {
                Block b = loc.getBlock();
                b.setType(is.getType());
                b.setData(is.getData().getData());
            }
        }
    }
    imagine some kind of command /renameblock someName or /renamechest someName.
    what i'm trying to do is remove existing chest, if is empty, then i create new itemstack and define the name using itemmeta. then i'm trying to place this chest on the same location and player recieve second chest to the inventory. received chest is named corretly, but the placed one is not.
    can someone explain why?
    btw. how can i get custom name of any block?
    thanks :)
     
  2. Offline

    NoLiver92

    i believe when you place the chest you need to set it as a block type to place it, then use the block meta data rather then the item stack meta data.
     
  3. Offline

    ZeusAllMighty11

Thread Status:
Not open for further replies.

Share This Page