Custom block name

Discussion in 'Plugin Development' started by xXLightbulbXx, Nov 6, 2015.

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

    xXLightbulbXx

    Hey, I'm having trouble trying to set the name of a block that's spawning. Does anyone have any idea how i can spawn a block with a custom name?
    This is as far as i got.
    Code:
    public void spawnChest(Player player){
            World world = player.getWorld();
           
            double x = player.getLocation().getX();
            double y = player.getLocation().getY();
            double z = player.getLocation().getZ();
           
            Location chestLocation = new Location(world, x, y, z);
            chestLocation.getBlock().setType(Material.CHEST);
            Chest chest = (Chest) chestLocation.getBlock().getState();
           
        }
     
  2. Offline

    Scimiguy

  3. Offline

    AppleBabies

    You can't with a chest. You can only do it with a custom inventory GUI.
     
  4. Offline

    nbrandwine

    You can set an items metadata.

    As for @Scimiguy's post, if he's correct and you need NMS (I've gotten around it I think, but it wasn't a chest), you'll need this.

    Code:
    public static void setChestName(Location loc, String name)
    {
    try
    {
    loc.getBlock().setType(Material.CHEST);
    
    Field inventoryField = CraftChest.class.getDeclaredField("chest");
    inventoryField.setAccessible(true);
    TileEntityChest teChest = ((TileEntityChest) inventoryField.get((CraftChest) loc.getBlock().getState()));
    teChest.a(name);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
     
Thread Status:
Not open for further replies.

Share This Page